Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / cpp / Sample / Tizen C++ / UiVisualElement / UiVisualElement / project / src / TransformMatrixForm.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.tizenopensource.org/license
10 //
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.
16 //
17
18 #include "FormManager.h"
19 #include "TransformMatrixForm.h"
20
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;
27
28 #define ALMOST_ZERO_FLOAT   0.00001f
29
30 TransformMatrixForm::TransformMatrixForm(void)
31         : __pRed(null)
32         , __pYellow(null)
33         , __pGreen(null)
34         , __pButton(null)
35         , __isPerspective(false)
36         , __isAnimating(false)
37 {
38 }
39
40 TransformMatrixForm::~TransformMatrixForm(void)
41 {
42         SetVisualElement(null);
43         if (__pVisualElement)
44         {
45                 __pVisualElement->Destroy();
46                 __pVisualElement = null;
47         }
48 }
49
50 bool
51 TransformMatrixForm::Initialize()
52 {
53         Construct(FORM_STYLE_NORMAL | FORM_STYLE_HEADER | FORM_STYLE_FOOTER | FORM_STYLE_INDICATOR);
54         InitializeFooter();
55         SetFormBackEventListener(this);
56
57         return true;
58 }
59
60 result
61 TransformMatrixForm::OnInitializing(void)
62 {
63         result r = E_SUCCESS;
64
65         Header* pHeader = Form::GetHeader();
66         pHeader->SetStyle(HEADER_STYLE_TITLE);
67         pHeader->SetTitleText(L"Transform Matrix");
68
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();
77
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);
82
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);
89
90         __pRed = new (std::nothrow) VisualElement();
91         __pRed->Construct();
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);
97
98         Canvas* pCanvas = __pRed->GetCanvasN();
99         pCanvas->FillRectangle(Color::GetColor(COLOR_ID_RED), Rectangle(0, 0, 250, 250));
100         delete pCanvas;
101
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);
109
110         pCanvas = __pYellow->GetCanvasN();
111         pCanvas->FillRectangle(Color::GetColor(COLOR_ID_YELLOW), Rectangle(0, 0, 250, 250));
112         delete pCanvas;
113
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);
121
122         pCanvas = __pGreen->GetCanvasN();
123         pCanvas->FillRectangle(Color::GetColor(COLOR_ID_GREEN), Rectangle(0, 0, 250, 250));
124         delete pCanvas;
125
126         __orthogonalMatrix = __pBlack->GetChildrenTransformMatrix();
127         PerspectiveMatrix(__perspectiveMatrix, 60.0f, 1.0f, 0.01f, 100.0f);
128         return r;
129 }
130
131 void
132 TransformMatrixForm::OnActionPerformed(const Control& source, int actionId)
133 {
134         switch(actionId)
135         {
136         case ID_FOOTER_BUTTON_TRANSFORM:
137                 if (__isAnimating == false)
138                         ChangeTransformMatrix();
139                 break;
140
141         default:
142                 break;
143         }
144 }
145
146 void
147 TransformMatrixForm::OnFormBackRequested(Form& source)
148 {
149         Frame* pFrame = UiApp::GetInstance()->GetFrameAt(0);
150         if (pFrame != null)
151         {
152                 FormManager* pFormManager = static_cast<FormManager*>(pFrame->GetControl(L"FormManager"));
153                 if (pFormManager != null)
154                 {
155                         pFormManager->SendUserEvent(FormManager::REQUEST_ID_MAIN_FORM, null);
156                 }
157         }
158 }
159
160 void
161 TransformMatrixForm::InitializeFooter(void)
162 {
163         Footer* pFooter = Form::GetFooter();
164         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
165         pFooter->SetBackButton();
166         pFooter->AddActionEventListener(*this);
167
168         FooterItem  footerItem1;
169         footerItem1.Construct(ID_FOOTER_BUTTON_TRANSFORM);
170         footerItem1.SetText(L"Transform Matrix");
171
172         pFooter->AddItem(footerItem1);
173 }
174
175 void
176 TransformMatrixForm::OnAnimationTransactionFinished(int transactionId)
177 {
178         __isAnimating = false;
179 }
180
181 void
182 TransformMatrixForm::ChangeTransformMatrix(void)
183 {
184         String text;
185
186         AnimationTransaction::Begin();
187         AnimationTransaction::SetCurrentTransactionEventListener(this);
188
189         if (__isPerspective)
190         {
191                 __pBlack->SetChildrenTransformMatrix(__orthogonalMatrix);
192                 __isPerspective = false;
193
194                 text.Append(L"Orthogonal Matrix");
195         }
196         else
197         {
198                 __pBlack->SetChildrenTransformMatrix(__perspectiveMatrix);
199                 __isPerspective = true;
200
201                 text.Append(L"Perspective Matrix");
202         }
203
204         AnimationTransaction::Commit();
205         __isAnimating = true;
206
207         __pButton->SetText(text);
208         __pButton->Draw();
209 }
210
211 void
212 TransformMatrixForm::PerspectiveMatrix(FloatMatrix4& m, float fov, float aspect, float nearZ, float farZ)
213 {
214         float frustumW, frustumH;
215
216         frustumH = tan(fov / 360.0f * M_PI) * nearZ;
217         frustumW = frustumH * aspect;
218
219         FrustumMatrix(m, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ);
220 }
221
222 void
223 TransformMatrixForm::FrustumMatrix(FloatMatrix4& m, float left, float right, float bottom, float top, float nearZ, float farZ)
224 {
225         float w, h, d;
226
227         w = right - left;
228         h = top - bottom;
229         d = farZ - nearZ;
230
231         if (w == 0.0f)
232         {
233                 w = ALMOST_ZERO_FLOAT;
234         }
235         
236         if (h == 0.0f)
237         {
238                 h = ALMOST_ZERO_FLOAT;
239         }
240         
241         if (d == 0.0f)
242         {
243                 d = ALMOST_ZERO_FLOAT;
244         }
245
246         m.matrix[0][0] = 2.0f * nearZ / w;
247
248         m.matrix[1][1] = 2.0f * nearZ / h;
249
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;
254
255         m.matrix[3][2] = -(2.0f * farZ * nearZ) / d;
256         m.matrix[3][3] = 0.0f;
257 }