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