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