Merge "Size negotiation patch 4: Remove SetRelayoutEnabled" into tizen
[platform/core/uifw/dali-demo.git] / examples / animated-shapes / animated-shapes-example.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20
21 #include "shared/view.h"
22
23 using namespace Dali;
24
25 namespace
26 {
27 const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-gradient.jpg" );
28
29 }
30
31 // This example shows resolution independent rendering and animation of curves using the gpu.
32 //
33 class AnimatedShapesExample : public ConnectionTracker
34 {
35 public:
36
37   AnimatedShapesExample( Application& application )
38 : mApplication( application )
39 {
40     // Connect to the Application's Init signal
41     mApplication.InitSignal().Connect( this, &AnimatedShapesExample::Create );
42 }
43
44   ~AnimatedShapesExample()
45   {
46     // Nothing to do here;
47   }
48
49   // The Init signal is received once (only) during the Application lifetime
50   void Create( Application& application )
51   {
52     DemoHelper::RequestThemeChange();
53
54     // Get a handle to the stage
55     Stage stage = Stage::GetCurrent();
56
57     //Create a view
58     mView = Dali::Toolkit::View::New();
59     mView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
60     stage.Add( mView );
61
62     //Set background image for the view
63     Image image = ResourceImage::New( BACKGROUND_IMAGE );
64
65
66     Dali::ImageActor backgroundImageActor = Dali::ImageActor::New( image );
67     mView.SetBackground( backgroundImageActor );
68
69     CreateTriangleMorph(Vector3( stage.GetSize().x*0.5f,stage.GetSize().y*0.15f,0.0f), 100.0f );
70     CreateCircleMorph( Vector3( stage.GetSize().x*0.5f,stage.GetSize().y*0.85f,0.0f), 60.0f );
71     CreatePathMorph( Vector3( stage.GetSize().x*0.5f,stage.GetSize().y*0.5f,0.0f), 55.0f );
72
73
74     stage.KeyEventSignal().Connect(this, &AnimatedShapesExample::OnKeyEvent);
75   }
76
77   void CreateCircleMorph( Vector3 center, float radius )
78   {
79     Toolkit::QuadraticBezier shader = Toolkit::QuadraticBezier::New(16, true);
80
81     shader.SetPoint(0, Vector3(-radius,-radius,0.0f));
82     shader.SetPoint(1, Vector3( 0.0f,-radius,0.0f));
83     shader.SetPoint(2, Vector3(radius,-radius,0.0f));
84
85     shader.SetPoint(3, Vector3(radius,-radius,0.0f));
86     shader.SetPoint(4, Vector3( radius,0.0f,0.0f));
87     shader.SetPoint(5, Vector3(radius,radius,0.0f));
88
89     shader.SetPoint(6, Vector3(radius,radius,0.0f));
90     shader.SetPoint(7, Vector3( 0.0f,radius,0.0f));
91     shader.SetPoint(8, Vector3( -radius,radius,0.0f));
92
93     shader.SetPoint(9,  Vector3( -radius,radius,0.0f));
94     shader.SetPoint(10, Vector3( -radius,0.0f,0.0f));
95     shader.SetPoint(11, Vector3(-radius,-radius,0.0f));
96
97     shader.SetPoint(12, Vector3(-radius,-radius,0.0f));
98     shader.SetPoint(13, Vector3(radius,-radius,0.0f));
99     shader.SetPoint(14, Vector3(radius,radius,0.0f));
100     shader.SetPoint(15, Vector3( -radius,radius,0.0f));
101
102     shader.SetColor(Vector4(1.0f,0.0f,0.0f,1.0f) );
103     shader.SetLineWidth(2.0f);
104
105     ////Generate the mesh
106     Dali::MeshData::VertexContainer vertices;
107     for( unsigned int i(0); i<12; i+=3 )
108     {
109       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,0.0f,i)  ));
110       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.5f,0.0f,i+1)));
111       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(1.0f,1.0f,i+2)));
112     }
113     vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,1.0f,12)  ));
114     vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,1.0f,13)));
115     vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,1.0f,14)));
116     vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,1.0f,15)));
117
118     short unsigned int indexArray[] = { 0,2,1, 3,5,4,6,8,7, 9, 11, 10, 12,15,14,12,14,13};
119     Dali::MeshData::FaceIndices index( indexArray, indexArray + sizeof(indexArray)/sizeof(short unsigned int) );
120
121     //Material
122     Dali::Material material = Material::New("Material");
123     material.SetDiffuseColor( Vector4(1.0f,1.0f,1.0f,1.0f));
124
125     //Create the Mesh object
126     Dali::MeshData data;
127     data.SetVertices(vertices);
128     data.SetFaceIndices( index );
129     data.SetMaterial( material );
130     data.SetHasNormals( true );
131     Mesh mesh = Mesh::New( data );
132
133     //Create the mesh actor
134     MeshActor meshActor = MeshActor::New(mesh);
135     meshActor.SetAnchorPoint( AnchorPoint::CENTER );
136     meshActor.SetShaderEffect(shader);
137     meshActor.SetPosition( center );
138     meshActor.SetBlendMode(BlendingMode::ON );
139     mView.Add( meshActor );
140
141
142     //Animation
143     Animation animation = Animation::New(5.0f);
144     KeyFrames k0 = KeyFrames::New();
145     k0.Add( 0.0f, Vector3( 0.0f,-radius, 0.0f) );
146     k0.Add( 0.5f, Vector3(0.0f, -radius*4.0f, 0.0f));
147     k0.Add( 1.0f, Vector3( 0.0f,-radius, 0.0f) );
148     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(1)),k0,AlphaFunctions::EaseInOutSine );
149
150     k0 = KeyFrames::New();
151     k0.Add( 0.0f, Vector3( radius, 0.0f, 0.0f) );
152     k0.Add( 0.5f, Vector3(radius*4.0f,0.0f, 0.0f));
153     k0.Add( 1.0f, Vector3( radius,0.0f, 0.0f));
154     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(4)),k0,AlphaFunctions::EaseInOutSine );
155
156     k0 = KeyFrames::New();
157     k0.Add( 0.0f, Vector3(0.0f,radius, 0.0f) );
158     k0.Add( 0.5f, Vector3(0.0f,radius*4.0f, 0.0f));
159     k0.Add( 1.0f, Vector3(0.0f,radius, 0.0f) );
160     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(7)),k0,AlphaFunctions::EaseInOutSine );
161
162     k0 = KeyFrames::New();
163     k0.Add( 0.0f, Vector3( -radius,  0.0f, 0.0f) );
164     k0.Add( 0.5f, Vector3(-radius*4.0f,0.0f, 0.0f));
165     k0.Add( 1.0f, Vector3( -radius,  0.0f, 0.0f) );
166     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(10)),k0,AlphaFunctions::EaseInOutSine );
167
168     animation.AnimateBy( Property( meshActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(90.0f) ), Vector3::ZAXIS ) );
169     animation.SetLooping( true );
170     animation.Play();
171   }
172
173   void CreateTriangleMorph( Vector3 center, float side )
174   {
175     float h = (side *0.5f)/0.866f;
176
177     Vector3 v0 = Vector3(-h,h,0.0f);
178     Vector3 v1 = Vector3(0.0f,-(side*0.366f),0.0f );
179     Vector3 v2 = Vector3(h,h,0.0f);
180
181     Vector3 v3 = v0 + ((v1-v0) * 0.5f);
182     Vector3 v4 = v1 + ((v2-v1) * 0.5f);
183     Vector3 v5 = v2 + ((v0-v2) * 0.5f);
184
185     Toolkit::QuadraticBezier shader = Toolkit::QuadraticBezier::New(12, true);
186
187     shader.SetPoint(0,v0);
188     shader.SetPoint(1,v3);
189     shader.SetPoint(2,v1);
190
191     shader.SetPoint(3,v1);
192     shader.SetPoint(4,v4);
193     shader.SetPoint(5,v2);
194
195     shader.SetPoint(6,v2);
196     shader.SetPoint(7,v5);
197     shader.SetPoint(8,v0);
198
199     shader.SetPoint(9, v0);
200     shader.SetPoint(10,v1);
201     shader.SetPoint(11,v2);
202
203     shader.SetColor(Vector4(0.0f,1.0f,0.0f,1.0f));
204     shader.SetLineWidth(2.0f);
205
206     ////Generate the mesh
207     Dali::MeshData::VertexContainer vertices;
208     for( unsigned int i(0);i<9;i+=3 )
209     {
210       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,0.0f,i)) );
211       vertices.push_back(  MeshData::Vertex( Vector3::ZERO, Vector2::ZERO,Vector3(0.5f,0.0f,i+1) ) );
212       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(1.0f,1.0f,i+2)  ) );
213     }
214
215     vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,1.0f,9)) );
216     vertices.push_back(  MeshData::Vertex( Vector3::ZERO, Vector2::ZERO,Vector3(0.0f,1.0f,10) ) );
217     vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,1.0f,11)  ) );
218
219     short unsigned int indexArray[] = { 0,2,1,3,5,4,6,8,7,9,11,10 };
220     Dali::MeshData::FaceIndices index( indexArray, indexArray + sizeof(indexArray)/sizeof(short unsigned int) );
221
222     //Material
223     Dali::Material material = Material::New("Material");
224     material.SetDiffuseColor( Vector4(1.0f,1.0f,1.0f,1.0f));
225
226     //Create the Mesh object
227     Dali::MeshData data;
228     data.SetVertices(vertices);
229     data.SetFaceIndices( index );
230     data.SetMaterial( material );
231     data.SetHasNormals( true );
232     Mesh mesh = Mesh::New( data );
233
234 //    //Create the mesh actor
235     MeshActor meshActor = MeshActor::New(mesh);
236     meshActor.SetAnchorPoint( AnchorPoint::CENTER );
237     meshActor.SetShaderEffect(shader);
238     meshActor.SetPosition( center );
239     meshActor.SetBlendMode(BlendingMode::ON );
240     mView.Add( meshActor );
241
242     //Animation
243     Animation animation = Animation::New(5.0f);
244
245     KeyFrames k0 = KeyFrames::New();
246     k0.Add( 0.0f,v3  );
247     k0.Add( 0.5f, v3 + Vector3(-200.0f,-200.0f,0.0f));
248     k0.Add( 1.0f, v3 );
249     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(1)),k0,AlphaFunctions::EaseInOutSine );
250
251     k0 = KeyFrames::New();
252     k0.Add( 0.0f,v4  );
253     k0.Add( 0.5f, v4 + Vector3(200.0f,-200.0f,0.0f));
254     k0.Add( 1.0f, v4 );
255     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(4)),k0,AlphaFunctions::EaseInOutSine );
256
257     k0 = KeyFrames::New();
258     k0.Add( 0.0f,v5  );
259     k0.Add( 0.5f, v5 + Vector3(0.0,200.0f,0.0f));
260     k0.Add( 1.0f, v5 );
261     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(7)),k0,AlphaFunctions::EaseInOutSine );
262     animation.SetLooping( true );
263     animation.Play();
264   }
265
266   void CreatePathMorph( Vector3 center, float radius )
267   {
268     Toolkit::QuadraticBezier shader = Toolkit::QuadraticBezier::New(12, false);
269
270     shader.SetPoint(0, Vector3(-radius,-radius,0.0f));
271     shader.SetPoint(1, Vector3( 0.0f,-radius,0.0f));
272     shader.SetPoint(2, Vector3(radius,-radius,0.0f));
273
274     shader.SetPoint(3, Vector3(radius,-radius,0.0f));
275     shader.SetPoint(4, Vector3( radius,0.0f,0.0f));
276     shader.SetPoint(5, Vector3(radius,radius,0.0f));
277
278     shader.SetPoint(6, Vector3(radius,radius,0.0f));
279     shader.SetPoint(7, Vector3( 0.0f,radius,0.0f));
280     shader.SetPoint(8, Vector3( -radius,radius,0.0f));
281
282     shader.SetPoint(9,  Vector3( -radius,radius,0.0f));
283     shader.SetPoint(10, Vector3( -radius,0.0f,0.0f));
284     shader.SetPoint(11, Vector3(-radius,-radius,0.0f));
285
286     shader.SetColor(Vector4(1.0f,1.0f,0.0f,1.0f) );
287     shader.SetLineWidth(1.5f);
288
289     ////Generate the mesh/S
290     Dali::MeshData::VertexContainer vertices;
291     for( unsigned int i(0); i<12; i+=3 )
292     {
293       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,0.0f,i)  ));
294       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.5f,0.0f,i+1)));
295       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(1.0f,1.0f,i+2)));
296     }
297
298
299     short unsigned int indexArray[] = { 0,2,1, 3,5,4,6,8,7, 9, 11, 10 };
300     Dali::MeshData::FaceIndices index( indexArray, indexArray + sizeof(indexArray)/sizeof(short unsigned int) );
301
302     //Material
303     Dali::Material material = Material::New("Material");
304     material.SetDiffuseColor( Vector4(1.0f,1.0f,1.0f,1.0f));
305
306     //Create the Mesh object
307     Dali::MeshData data;
308     data.SetVertices(vertices);
309     data.SetFaceIndices( index );
310     data.SetMaterial( material );
311     data.SetHasNormals( true );
312     Mesh mesh = Mesh::New( data );
313
314     //Create the mesh actor
315     MeshActor meshActor = MeshActor::New(mesh);
316     meshActor.SetAnchorPoint( AnchorPoint::CENTER );
317     meshActor.SetShaderEffect(shader);
318     meshActor.SetPosition( center );
319     meshActor.SetBlendMode(BlendingMode::ON );
320     mView.Add( meshActor );
321
322
323     //Animation
324     Animation animation = Animation::New(5.0f);
325     KeyFrames k0 = KeyFrames::New();
326     k0.Add( 0.0f, Vector3( 0.0f,-radius*2.0, 0.0f) );
327     k0.Add( 0.5f, Vector3(-radius*2.0, -radius*3.0f, 0.0f));
328     k0.Add( 1.0f, Vector3( 0.0f,-radius*2.0, 0.0f) );
329     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(1)),k0,AlphaFunctions::EaseInOutSine );
330
331     k0 = KeyFrames::New();
332     k0.Add( 0.0f, Vector3( radius*2.0, 0.0f, 0.0f) );
333     k0.Add( 0.5f, Vector3(radius*3.0f,-radius*2.0, 0.0f));
334     k0.Add( 1.0f, Vector3( radius*2.0,0.0f, 0.0f));
335     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(4)),k0,AlphaFunctions::EaseInOutSine );
336
337     k0 = KeyFrames::New();
338     k0.Add( 0.0f, Vector3(0.0f,radius*2.0, 0.0f) );
339     k0.Add( 0.5f, Vector3(radius*2.0,radius*3.0f, 0.0f));
340     k0.Add( 1.0f, Vector3(0.0f,radius*2.0, 0.0f) );
341     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(7)),k0,AlphaFunctions::EaseInOutSine );
342
343     k0 = KeyFrames::New();
344     k0.Add( 0.0f, Vector3( -radius*2.0,  0.0f, 0.0f) );
345     k0.Add( 0.5f, Vector3(-radius*3.0f,radius*2.0, 0.0f));
346     k0.Add( 1.0f, Vector3( -radius*2.0,  0.0f, 0.0f) );
347     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(10)),k0,AlphaFunctions::EaseInOutSine );
348
349     animation.AnimateBy( Property( meshActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(-90.0f) ), Vector3::ZAXIS ) );
350     animation.SetLooping( true );
351     animation.Play();
352   }
353
354   /**
355    * Main key event handler
356    */
357   void OnKeyEvent(const KeyEvent& event)
358   {
359     if( event.state == KeyEvent::Down && (IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ))  )
360     {
361       mApplication.Quit();
362     }
363   }
364
365 private:
366   Application&                mApplication;
367   Toolkit::View               mView;
368 };
369
370 void RunTest( Application& application )
371 {
372   AnimatedShapesExample test( application );
373   application.MainLoop();
374 }
375
376 int main( int argc, char **argv )
377 {
378   Application application = Application::New( &argc, &argv );
379   RunTest( application );
380
381   return 0;
382 }