3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.tizenopensource.org/license
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include "FormManager.h"
19 #include "TransformMatrixForm.h"
21 using namespace Osp::App;
22 using namespace Osp::Base;
23 using namespace Osp::Graphics;
24 using namespace Osp::Ui;
25 using namespace Osp::Ui::Animations;
26 using namespace Osp::Ui::Controls;
28 #define ALMOST_ZERO_FLOAT 0.00001f
30 TransformMatrixForm::TransformMatrixForm(void)
35 , __isPerspective(false)
36 , __isAnimating(false)
40 TransformMatrixForm::~TransformMatrixForm(void)
42 SetVisualElement(null);
45 __pVisualElement->Destroy();
46 __pVisualElement = null;
51 TransformMatrixForm::Initialize()
53 Construct(FORM_STYLE_NORMAL | FORM_STYLE_HEADER | FORM_STYLE_FOOTER | FORM_STYLE_INDICATOR);
55 SetFormBackEventListener(this);
61 TransformMatrixForm::OnInitializing(void)
65 Header* pHeader = Form::GetHeader();
66 pHeader->SetStyle(HEADER_STYLE_TITLE);
67 pHeader->SetTitleText(L"Transform Matrix");
69 __pVisualElement = new (std::nothrow) VisualElement();
70 __pVisualElement->Construct();
71 __pVisualElement->SetName(L"VisualElement");
72 __pVisualElement->SetBounds(FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f));
73 __pVisualElement->SetClipChildrenEnabled(false);
74 __pVisualElement->SetShowState(true);
75 SetVisualElement(__pVisualElement);
76 Rectangle clientRect = GetClientAreaBounds();
78 __pButton = new (std::nothrow) Button();
79 __pButton->Construct(Rectangle(50, clientRect.height - 125, clientRect.width - 100, 75));
80 __pButton->SetText(L"Orthogonal Matrix");
81 AddControl(*__pButton);
83 __pBlack = new (std::nothrow) VisualElement();
84 __pBlack->Construct();
85 __pBlack->SetName(L"Black");
86 __pBlack->SetBounds(FloatRectangle(clientRect.x, clientRect.y, clientRect.width, clientRect.width));
87 __pBlack->SetShowState(true);
88 __pVisualElement->AttachChild(*__pBlack);
90 __pRed = new (std::nothrow) VisualElement();
92 __pRed->SetName(L"Red");
93 __pRed->SetBounds(FloatRectangle(50.0f, 50.0f, 250.0f, 250.0f));
94 __pRed->SetShowState(true);
95 __pRed->SetZPosition(-2.3f);
96 __pBlack->AttachChild(*__pRed);
98 Canvas* pCanvas = __pRed->GetCanvasN();
99 pCanvas->FillRectangle(Color::GetColor(COLOR_ID_RED), Rectangle(0, 0, 250, 250));
102 __pYellow = new (std::nothrow) VisualElement();
103 __pYellow->Construct();
104 __pYellow->SetName(L"Yellow");
105 __pYellow->SetBounds(FloatRectangle(125.0f, 125.0f, 250.0f, 250.0f));
106 __pYellow->SetShowState(true);
107 __pYellow->SetZPosition(-2.0f);
108 __pBlack->AttachChild(*__pYellow);
110 pCanvas = __pYellow->GetCanvasN();
111 pCanvas->FillRectangle(Color::GetColor(COLOR_ID_YELLOW), Rectangle(0, 0, 250, 250));
114 __pGreen = new (std::nothrow) VisualElement();
115 __pGreen->Construct();
116 __pGreen->SetName(L"Green");
117 __pGreen->SetBounds(FloatRectangle(200.0f, 200.0f, 250.0f, 250.0f));
118 __pGreen->SetShowState(true);
119 __pGreen->SetZPosition(-1.7f);
120 __pBlack->AttachChild(*__pGreen);
122 pCanvas = __pGreen->GetCanvasN();
123 pCanvas->FillRectangle(Color::GetColor(COLOR_ID_GREEN), Rectangle(0, 0, 250, 250));
126 __orthogonalMatrix = __pBlack->GetChildrenTransformMatrix();
127 PerspectiveMatrix(__perspectiveMatrix, 60.0f, 1.0f, 0.01f, 100.0f);
132 TransformMatrixForm::OnActionPerformed(const Control& source, int actionId)
136 case ID_FOOTER_BUTTON_TRANSFORM:
137 if (__isAnimating == false)
138 ChangeTransformMatrix();
147 TransformMatrixForm::OnFormBackRequested(Form& source)
149 Frame* pFrame = UiApp::GetInstance()->GetFrameAt(0);
152 FormManager* pFormManager = static_cast<FormManager*>(pFrame->GetControl(L"FormManager"));
153 if (pFormManager != null)
155 pFormManager->SendUserEvent(FormManager::REQUEST_ID_MAIN_FORM, null);
161 TransformMatrixForm::InitializeFooter(void)
163 Footer* pFooter = Form::GetFooter();
164 pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
165 pFooter->SetBackButton();
166 pFooter->AddActionEventListener(*this);
168 FooterItem footerItem1;
169 footerItem1.Construct(ID_FOOTER_BUTTON_TRANSFORM);
170 footerItem1.SetText(L"Transform Matrix");
172 pFooter->AddItem(footerItem1);
176 TransformMatrixForm::OnAnimationTransactionFinished(int transactionId)
178 __isAnimating = false;
182 TransformMatrixForm::ChangeTransformMatrix(void)
186 AnimationTransaction::Begin();
187 AnimationTransaction::SetCurrentTransactionEventListener(this);
191 __pBlack->SetChildrenTransformMatrix(__orthogonalMatrix);
192 __isPerspective = false;
194 text.Append(L"Orthogonal Matrix");
198 __pBlack->SetChildrenTransformMatrix(__perspectiveMatrix);
199 __isPerspective = true;
201 text.Append(L"Perspective Matrix");
204 AnimationTransaction::Commit();
205 __isAnimating = true;
207 __pButton->SetText(text);
212 TransformMatrixForm::PerspectiveMatrix(FloatMatrix4& m, float fov, float aspect, float nearZ, float farZ)
214 float frustumW, frustumH;
216 frustumH = tan(fov / 360.0f * M_PI) * nearZ;
217 frustumW = frustumH * aspect;
219 FrustumMatrix(m, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ);
223 TransformMatrixForm::FrustumMatrix(FloatMatrix4& m, float left, float right, float bottom, float top, float nearZ, float farZ)
233 w = ALMOST_ZERO_FLOAT;
238 h = ALMOST_ZERO_FLOAT;
243 d = ALMOST_ZERO_FLOAT;
246 m.matrix[0][0] = 2.0f * nearZ / w;
248 m.matrix[1][1] = 2.0f * nearZ / h;
250 m.matrix[2][0] = (right + left) / w;
251 m.matrix[2][1] = (top + bottom) / h;
252 m.matrix[2][2] = -(farZ + nearZ) / d;
253 m.matrix[2][3] = -1.0f;
255 m.matrix[3][2] = -(2.0f * farZ * nearZ) / d;
256 m.matrix[3][3] = 0.0f;