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