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