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();
57 //Set background image for the view
58 ImageAttributes attributes;
59 Image image = ResourceImage::New( BACKGROUND_IMAGE, attributes );
62 Dali::ImageActor backgroundImageActor = Dali::ImageActor::New( image );
63 mView.SetBackground( backgroundImageActor );
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 );
70 stage.KeyEventSignal().Connect(this, &AnimatedShapesExample::OnKeyEvent);
73 void CreateCircleMorph( Vector3 center, float radius )
75 Toolkit::QuadraticBezier shader = Toolkit::QuadraticBezier::New(16, true);
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));
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));
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));
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));
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));
98 shader.SetColor(Vector4(1.0f,0.0f,0.0f,1.0f) );
99 shader.SetLineWidth(2.0f);
101 ////Generate the mesh
102 Dali::MeshData::VertexContainer vertices;
103 for( unsigned int i(0); i<12; i+=3 )
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)));
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)));
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) );
118 Dali::Material material = Material::New("Material");
119 material.SetDiffuseColor( Vector4(1.0f,1.0f,1.0f,1.0f));
121 //Create the Mesh object
123 data.SetVertices(vertices);
124 data.SetFaceIndices( index );
125 data.SetMaterial( material );
126 data.SetHasNormals( true );
127 Mesh mesh = Mesh::New( data );
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 );
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 );
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 );
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 );
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 );
164 animation.RotateBy(meshActor,Degree(90.0f), Vector3::ZAXIS );
165 animation.SetLooping( true );
169 void CreateTriangleMorph( Vector3 center, float side )
171 float h = (side *0.5f)/0.866f;
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);
177 Vector3 v3 = v0 + ((v1-v0) * 0.5f);
178 Vector3 v4 = v1 + ((v2-v1) * 0.5f);
179 Vector3 v5 = v2 + ((v0-v2) * 0.5f);
181 Toolkit::QuadraticBezier shader = Toolkit::QuadraticBezier::New(12, true);
183 shader.SetPoint(0,v0);
184 shader.SetPoint(1,v3);
185 shader.SetPoint(2,v1);
187 shader.SetPoint(3,v1);
188 shader.SetPoint(4,v4);
189 shader.SetPoint(5,v2);
191 shader.SetPoint(6,v2);
192 shader.SetPoint(7,v5);
193 shader.SetPoint(8,v0);
195 shader.SetPoint(9, v0);
196 shader.SetPoint(10,v1);
197 shader.SetPoint(11,v2);
199 shader.SetColor(Vector4(0.0f,1.0f,0.0f,1.0f));
200 shader.SetLineWidth(2.0f);
202 ////Generate the mesh
203 Dali::MeshData::VertexContainer vertices;
204 for( unsigned int i(0);i<9;i+=3 )
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) ) );
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) ) );
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) );
219 Dali::Material material = Material::New("Material");
220 material.SetDiffuseColor( Vector4(1.0f,1.0f,1.0f,1.0f));
222 //Create the Mesh object
224 data.SetVertices(vertices);
225 data.SetFaceIndices( index );
226 data.SetMaterial( material );
227 data.SetHasNormals( true );
228 Mesh mesh = Mesh::New( data );
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 );
239 Animation animation = Animation::New(5.0f);
241 KeyFrames k0 = KeyFrames::New();
243 k0.Add( 0.5f, v3 + Vector3(-200.0f,-200.0f,0.0f));
245 animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(1)),k0,AlphaFunctions::EaseInOutSine );
247 k0 = KeyFrames::New();
249 k0.Add( 0.5f, v4 + Vector3(200.0f,-200.0f,0.0f));
251 animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(4)),k0,AlphaFunctions::EaseInOutSine );
253 k0 = KeyFrames::New();
255 k0.Add( 0.5f, v5 + Vector3(0.0,200.0f,0.0f));
257 animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(7)),k0,AlphaFunctions::EaseInOutSine );
258 animation.SetLooping( true );
262 void CreatePathMorph( Vector3 center, float radius )
264 Toolkit::QuadraticBezier shader = Toolkit::QuadraticBezier::New(12, false);
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));
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));
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));
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));
282 shader.SetColor(Vector4(1.0f,1.0f,0.0f,1.0f) );
283 shader.SetLineWidth(1.5f);
285 ////Generate the mesh/S
286 Dali::MeshData::VertexContainer vertices;
287 for( unsigned int i(0); i<12; i+=3 )
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)));
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) );
299 Dali::Material material = Material::New("Material");
300 material.SetDiffuseColor( Vector4(1.0f,1.0f,1.0f,1.0f));
302 //Create the Mesh object
304 data.SetVertices(vertices);
305 data.SetFaceIndices( index );
306 data.SetMaterial( material );
307 data.SetHasNormals( true );
308 Mesh mesh = Mesh::New( data );
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 );
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 );
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 );
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 );
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 );
345 animation.RotateBy(meshActor,Degree(-90.0f), Vector3::ZAXIS );
346 animation.SetLooping( true );
351 * Main key event handler
353 void OnKeyEvent(const KeyEvent& event)
355 if( event.state == KeyEvent::Down && (IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK )) )
362 Application& mApplication;
366 void RunTest( Application& application )
368 AnimatedShapesExample test( application );
369 application.MainLoop();
372 int main( int argc, char **argv )
374 Application application = Application::New( &argc, &argv );
375 RunTest( application );