2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
25 const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-gradient.jpg" );
29 // This example shows resolution independent rendering and animation of curves using the gpu.
31 class AnimatedShapesExample : public ConnectionTracker
35 AnimatedShapesExample( Application& application )
36 : mApplication( application )
38 // Connect to the Application's Init signal
39 mApplication.InitSignal().Connect( this, &AnimatedShapesExample::Create );
42 ~AnimatedShapesExample()
44 // Nothing to do here;
47 // The Init signal is received once (only) during the Application lifetime
48 void Create( Application& application )
50 // Get a handle to the stage
51 Stage stage = Stage::GetCurrent();
54 mView = Dali::Toolkit::View::New();
55 mView.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
58 //Set background image for the view
59 ImageAttributes attributes;
60 Image image = ResourceImage::New( BACKGROUND_IMAGE, attributes );
63 Dali::ImageActor backgroundImageActor = Dali::ImageActor::New( image );
64 mView.SetBackground( backgroundImageActor );
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 );
71 stage.KeyEventSignal().Connect(this, &AnimatedShapesExample::OnKeyEvent);
74 void CreateCircleMorph( Vector3 center, float radius )
76 Toolkit::QuadraticBezier shader = Toolkit::QuadraticBezier::New(16, true);
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));
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));
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));
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));
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));
99 shader.SetColor(Vector4(1.0f,0.0f,0.0f,1.0f) );
100 shader.SetLineWidth(2.0f);
102 ////Generate the mesh
103 Dali::MeshData::VertexContainer vertices;
104 for( unsigned int i(0); i<12; i+=3 )
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)));
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)));
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) );
119 Dali::Material material = Material::New("Material");
120 material.SetDiffuseColor( Vector4(1.0f,1.0f,1.0f,1.0f));
122 //Create the Mesh object
124 data.SetVertices(vertices);
125 data.SetFaceIndices( index );
126 data.SetMaterial( material );
127 data.SetHasNormals( true );
128 Mesh mesh = Mesh::New( data );
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 );
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 );
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 );
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 );
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 );
165 animation.RotateBy(meshActor,Degree(90.0f), Vector3::ZAXIS );
166 animation.SetLooping( true );
170 void CreateTriangleMorph( Vector3 center, float side )
172 float h = (side *0.5f)/0.866f;
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);
178 Vector3 v3 = v0 + ((v1-v0) * 0.5f);
179 Vector3 v4 = v1 + ((v2-v1) * 0.5f);
180 Vector3 v5 = v2 + ((v0-v2) * 0.5f);
182 Toolkit::QuadraticBezier shader = Toolkit::QuadraticBezier::New(12, true);
184 shader.SetPoint(0,v0);
185 shader.SetPoint(1,v3);
186 shader.SetPoint(2,v1);
188 shader.SetPoint(3,v1);
189 shader.SetPoint(4,v4);
190 shader.SetPoint(5,v2);
192 shader.SetPoint(6,v2);
193 shader.SetPoint(7,v5);
194 shader.SetPoint(8,v0);
196 shader.SetPoint(9, v0);
197 shader.SetPoint(10,v1);
198 shader.SetPoint(11,v2);
200 shader.SetColor(Vector4(0.0f,1.0f,0.0f,1.0f));
201 shader.SetLineWidth(2.0f);
203 ////Generate the mesh
204 Dali::MeshData::VertexContainer vertices;
205 for( unsigned int i(0);i<9;i+=3 )
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) ) );
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) ) );
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) );
220 Dali::Material material = Material::New("Material");
221 material.SetDiffuseColor( Vector4(1.0f,1.0f,1.0f,1.0f));
223 //Create the Mesh object
225 data.SetVertices(vertices);
226 data.SetFaceIndices( index );
227 data.SetMaterial( material );
228 data.SetHasNormals( true );
229 Mesh mesh = Mesh::New( data );
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 );
240 Animation animation = Animation::New(5.0f);
242 KeyFrames k0 = KeyFrames::New();
244 k0.Add( 0.5f, v3 + Vector3(-200.0f,-200.0f,0.0f));
246 animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(1)),k0,AlphaFunctions::EaseInOutSine );
248 k0 = KeyFrames::New();
250 k0.Add( 0.5f, v4 + Vector3(200.0f,-200.0f,0.0f));
252 animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(4)),k0,AlphaFunctions::EaseInOutSine );
254 k0 = KeyFrames::New();
256 k0.Add( 0.5f, v5 + Vector3(0.0,200.0f,0.0f));
258 animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(7)),k0,AlphaFunctions::EaseInOutSine );
259 animation.SetLooping( true );
263 void CreatePathMorph( Vector3 center, float radius )
265 Toolkit::QuadraticBezier shader = Toolkit::QuadraticBezier::New(12, false);
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));
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));
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));
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));
283 shader.SetColor(Vector4(1.0f,1.0f,0.0f,1.0f) );
284 shader.SetLineWidth(1.5f);
286 ////Generate the mesh/S
287 Dali::MeshData::VertexContainer vertices;
288 for( unsigned int i(0); i<12; i+=3 )
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)));
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) );
300 Dali::Material material = Material::New("Material");
301 material.SetDiffuseColor( Vector4(1.0f,1.0f,1.0f,1.0f));
303 //Create the Mesh object
305 data.SetVertices(vertices);
306 data.SetFaceIndices( index );
307 data.SetMaterial( material );
308 data.SetHasNormals( true );
309 Mesh mesh = Mesh::New( data );
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 );
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 );
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 );
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 );
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 );
346 animation.RotateBy(meshActor,Degree(-90.0f), Vector3::ZAXIS );
347 animation.SetLooping( true );
352 * Main key event handler
354 void OnKeyEvent(const KeyEvent& event)
356 if( event.state == KeyEvent::Down && (IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK )) )
363 Application& mApplication;
367 void RunTest( Application& application )
369 AnimatedShapesExample test( application );
370 application.MainLoop();
373 int main( int argc, char **argv )
375 Application application = Application::New( &argc, &argv );
376 RunTest( application );