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/devel-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.h>
24 #include "shared/utility.h"
27 using namespace Dali::Toolkit;
31 const char * const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-2.jpg" );
32 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
34 const float GRAVITY_X(0);
35 const float GRAVITY_Y(-0.09);
38 #define METABALL_NUMBER 4
40 const char*const METABALL_VERTEX_SHADER = DALI_COMPOSE_SHADER (
41 attribute mediump vec2 aPosition;\n
42 attribute mediump vec2 aTexture;\n
43 attribute mediump vec3 aNormal;\n
44 uniform mediump mat4 uMvpMatrix;\n
45 uniform mediump vec3 uSize;\n
46 uniform lowp vec4 uColor;\n
47 varying mediump vec2 vTexCoord;\n
51 mediump vec4 vertexPosition = vec4(aPosition.x, aPosition.y, 0.0, 1.0);\n
52 vertexPosition = uMvpMatrix * vertexPosition;\n
53 gl_Position = vertexPosition;\n
54 vTexCoord = aTexture;\n
59 const char*const METABALL_FRAG_SHADER = DALI_COMPOSE_SHADER (
60 precision mediump float;\n
61 varying vec2 vTexCoord;\n
62 uniform vec2 uPositionMetaball;\n
63 uniform vec2 uPositionVar;\n
64 uniform vec2 uGravityVector;\n
65 uniform float uRadius;\n
66 uniform float uRadiusVar;\n
67 uniform float uAspect;\n
70 vec2 adjustedCoords = vTexCoord * 2.0 - 1.0;\n
71 vec2 finalMetaballPosition = uPositionMetaball + uGravityVector + uPositionVar;\n
73 float distance = (adjustedCoords.x - finalMetaballPosition.x) * (adjustedCoords.x - finalMetaballPosition.x) +
74 (adjustedCoords.y - finalMetaballPosition.y) * (adjustedCoords.y - finalMetaballPosition.y);\n
75 float finalRadius = uRadius + uRadiusVar;\n
76 float color = finalRadius / sqrt( distance );\n
77 vec2 bordercolor = vec2(0.0,0.0);\n
78 if (vTexCoord.x < 0.1)\n
80 bordercolor.x = (0.1 - vTexCoord.x) * 0.8;\n
82 if (vTexCoord.x > 0.9)\n
84 bordercolor.x = (vTexCoord.x - 0.9) * 0.8;\n
86 if (vTexCoord.y < 0.1)\n
88 bordercolor.y = (0.1 - vTexCoord.y) * 0.8;\n
90 if (vTexCoord.y > (0.9 * uAspect))\n
92 bordercolor.y = (vTexCoord.y - (0.9 * uAspect)) * 0.8;\n
94 float border = (bordercolor.x + bordercolor.y) * 0.5;\n
95 gl_FragColor = vec4(color + border,color + border,color + border,1.0);\n
99 const char*const REFRACTION_FRAG_SHADER = DALI_COMPOSE_SHADER (
100 precision mediump float;\n
101 varying vec2 vTexCoord;\n
102 uniform sampler2D sTexture;\n
103 uniform sampler2D sEffect;\n
106 vec4 metaColor = texture2D(sEffect, vTexCoord);\n
108 float bright = 1.0;\n
109 if (metaColor.r > 0.85)\n
111 zoomCoords = ((vTexCoord - 0.5) * 0.95) + 0.5;\n
113 else if (metaColor.r > 0.81)\n
115 float interpolation = mix(0.95, 1.05, (0.85 - metaColor.r) * 50.0);\n
116 zoomCoords = ((vTexCoord - 0.5) * interpolation) + 0.5;\n
121 zoomCoords = vTexCoord;\n
124 gl_FragColor = texture2D(sTexture, zoomCoords) * bright;\n
128 const char*const FRAG_SHADER = DALI_COMPOSE_SHADER (
129 precision mediump float;\n
130 varying vec2 vTexCoord;\n
131 uniform sampler2D sTexture;\n
134 gl_FragColor = texture2D(sTexture, vTexCoord);\n
141 //ShaderEffect shader;
147 //Properties needed for animations
148 Property::Index positionIndex;
149 Property::Index positionVarIndex;
150 Property::Index gravityIndex;
151 Property::Index radiusIndex;
152 Property::Index radiusVarIndex;
153 Property::Index aspectIndex;
157 /***************************************************************************/
158 /* Demo using Metaballs for Refraction when clicking the screen ************/
159 /* The concept is similar to the Note 5 ScreenLock ************/
160 /***************************************************************************/
161 class MetaballRefracController : public ConnectionTracker
164 MetaballRefracController( Application& application );
165 ~MetaballRefracController();
167 void Create( Application& app );
168 bool OnTouch( Actor actor, const TouchData& touch );
169 void OnKeyEvent(const KeyEvent& event);
171 void SetGravity(const Vector2 & gravity);
175 Application& mApplication;
181 FrameBufferImage mMetaballFBO;
184 MetaballInfo mMetaballs[METABALL_NUMBER];
186 Actor mCompositionActor;
190 Vector2 mCurrentTouchPosition;
191 Vector2 mMetaballPosVariation;
192 Vector2 mMetaballPosVariationFrom;
193 Vector2 mMetaballPosVariationTo;
194 Vector2 mMetaballCenter;
199 Renderer mRendererRefraction;
200 TextureSet mTextureSetRefraction;
201 Shader mShaderRefraction;
202 TextureSet mTextureSetNormal;
203 Shader mShaderNormal;
206 Animation mGravityAnimation[METABALL_NUMBER];
207 Animation mRadiusDecAnimation[METABALL_NUMBER];
208 Animation mRadiusIncFastAnimation[METABALL_NUMBER];
209 Animation mRadiusIncSlowAnimation[METABALL_NUMBER];
210 Animation mRadiusVarAnimation[METABALL_NUMBER];
211 Animation mPositionVarAnimation[METABALL_NUMBER];
214 Geometry CreateGeometry();
215 Geometry CreateGeometryComposition();
217 void CreateMetaballActors();
218 void CreateMetaballImage();
219 void AddRefractionImage();
220 void CreateAnimations();
222 void LaunchRadiusIncSlowAnimations(Animation &source);
223 void LaunchGetBackToPositionAnimation(Animation &source);
225 void StopClickAnimations();
226 void StopAfterClickAnimations();
228 void ResetMetaballsState();
230 void SetPositionToMetaballs(Vector2 & metaballCenter);
234 //-----------------------------------------------------------------------------------------------
240 MetaballRefracController::MetaballRefracController( Application& application )
241 : mApplication( application )
243 // Connect to the Application's Init signal
244 mApplication.InitSignal().Connect( this, &MetaballRefracController::Create );
247 MetaballRefracController::~MetaballRefracController()
249 // Nothing to do here;
253 * Setter function for gravity
255 void MetaballRefracController::SetGravity(const Vector2 & gravity)
261 * Main create function, it creates the metaballs and all the
263 void MetaballRefracController::Create( Application& app )
265 Stage stage = Stage::GetCurrent();
267 stage.KeyEventSignal().Connect(this, &MetaballRefracController::OnKeyEvent);
269 mScreenSize = stage.GetSize();
271 stage.SetBackgroundColor(Color::BLACK);
273 //Set background image for the view
274 mBackImage = DemoHelper::LoadImage( BACKGROUND_IMAGE );
276 mGravity = Vector2(GRAVITY_X,GRAVITY_Y);
277 mGravityVar = Vector2(0,0);
279 //Create internal data
280 CreateMetaballActors();
281 CreateMetaballImage();
282 AddRefractionImage();
286 // Connect the callback to the touch signal on the mesh actor
287 stage.GetRootLayer().TouchSignal().Connect( this, &MetaballRefracController::OnTouch );
291 * Create a mesh data with the geometry for the metaball rendering
293 Geometry MetaballRefracController::CreateGeometry()
295 float aspect = (float)mScreenSize.y / (float)mScreenSize.x;
297 // Create vertices and specify their color
298 float xsize = mScreenSize.x * 0.5;
300 //We create the meshdata for the metaballs
301 struct VertexPosition { Vector2 position; };
302 struct VertexTexture { Vector2 texture; };
303 struct VertexNormal { Vector3 normal; };
305 VertexPosition vertices[] = {
306 { Vector2( -xsize, -xsize * aspect) },
307 { Vector2( xsize, -xsize * aspect) },
308 { Vector2( -xsize, xsize * aspect) },
309 { Vector2( xsize, xsize * aspect) }
312 VertexTexture textures[] = {
313 { Vector2(0.0f, 0.0f) },
314 { Vector2(1.0f, 0.0f) },
315 { Vector2(0.0f, 1.0f * aspect) },
316 { Vector2(1.0f, 1.0f * aspect) }
319 VertexNormal normals [] = {
320 { Vector3(0.0f, 0.0f, 1.0f) },
321 { Vector3(0.0f, 0.0f, 1.0f) },
322 { Vector3(0.0f, 0.0f, 1.0f) },
323 { Vector3(0.0f, 0.0f, 1.0f) }
326 unsigned int numberOfVertices = sizeof(vertices)/sizeof(VertexPosition);
329 Property::Map positionVertexFormat;
330 positionVertexFormat["aPosition"] = Property::VECTOR2;
331 PropertyBuffer positionVertices = PropertyBuffer::New( positionVertexFormat );
332 positionVertices.SetData( vertices, numberOfVertices );
335 Property::Map textureVertexFormat;
336 textureVertexFormat["aTexture"] = Property::VECTOR2;
337 PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat );
338 textureVertices.SetData( textures, numberOfVertices );
341 Property::Map normalVertexFormat;
342 normalVertexFormat["aNormal"] = Property::VECTOR3;
343 PropertyBuffer normalVertices = PropertyBuffer::New( normalVertexFormat );
344 normalVertices.SetData( normals, numberOfVertices );
347 unsigned short indices[] = { 0, 3, 1, 0, 2, 3 };
349 // Create the geometry object
350 Geometry texturedQuadGeometry = Geometry::New();
351 texturedQuadGeometry.AddVertexBuffer( positionVertices );
352 texturedQuadGeometry.AddVertexBuffer( textureVertices );
353 texturedQuadGeometry.AddVertexBuffer( normalVertices );
355 texturedQuadGeometry.SetIndexBuffer ( &indices[0], 6 );
357 return texturedQuadGeometry;
361 * Create a mesh data with the geometry for the metaball rendering
363 Geometry MetaballRefracController::CreateGeometryComposition()
365 float aspect = (float)mScreenSize.y / (float)mScreenSize.x;
367 // Create vertices and specify their color
368 float xsize = mScreenSize.x * 0.5;
370 //We create the meshdata for the metaballs
371 struct VertexPosition { Vector2 position; };
372 struct VertexTexture { Vector2 texture; };
373 struct VertexNormal { Vector3 normal; };
375 VertexPosition vertices[] = {
376 { Vector2( -xsize, -xsize * aspect) },
377 { Vector2( xsize, -xsize * aspect) },
378 { Vector2( -xsize, xsize * aspect) },
379 { Vector2( xsize, xsize * aspect) }
382 VertexTexture textures[] = {
383 { Vector2(0.0f, 0.0f) },
384 { Vector2(1.0f, 0.0f) },
385 { Vector2(0.0f, 1.0f) },
386 { Vector2(1.0f, 1.0f) }
389 VertexNormal normals [] = {
390 { Vector3(0.0f, 0.0f, 1.0f) },
391 { Vector3(0.0f, 0.0f, 1.0f) },
392 { Vector3(0.0f, 0.0f, 1.0f) },
393 { Vector3(0.0f, 0.0f, 1.0f) }
396 unsigned int numberOfVertices = sizeof(vertices)/sizeof(VertexPosition);
399 Property::Map positionVertexFormat;
400 positionVertexFormat["aPosition"] = Property::VECTOR2;
401 PropertyBuffer positionVertices = PropertyBuffer::New( positionVertexFormat );
402 positionVertices.SetData( vertices, numberOfVertices );
405 Property::Map textureVertexFormat;
406 textureVertexFormat["aTexture"] = Property::VECTOR2;
407 PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat );
408 textureVertices.SetData( textures, numberOfVertices );
411 Property::Map normalVertexFormat;
412 normalVertexFormat["aNormal"] = Property::VECTOR3;
413 PropertyBuffer normalVertices = PropertyBuffer::New( normalVertexFormat );
414 normalVertices.SetData( normals, numberOfVertices );
417 unsigned short indices[] = { 0, 3, 1, 0, 2, 3 };
419 // Create the geometry object
420 Geometry texturedQuadGeometry = Geometry::New();
421 texturedQuadGeometry.AddVertexBuffer( positionVertices );
422 texturedQuadGeometry.AddVertexBuffer( textureVertices );
423 texturedQuadGeometry.AddVertexBuffer( normalVertices );
425 texturedQuadGeometry.SetIndexBuffer ( &indices[0], sizeof( indices )/ sizeof( indices[0] ) );
427 return texturedQuadGeometry;
431 * Create a mesh actor for the metaballs
433 void MetaballRefracController::CreateMetaballActors()
435 //We create metaball structures
436 //With MeshData Textured
437 float aspect = (float)mScreenSize.y / (float)mScreenSize.x;
439 //Create the renderer for the metaballs
440 Shader shader = Shader::New( METABALL_VERTEX_SHADER, METABALL_FRAG_SHADER );
441 Geometry metaballGeom = CreateGeometry();
442 Renderer renderer = Renderer::New( metaballGeom, shader );
443 renderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
444 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_RGB, BlendFactor::ONE );
445 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ONE );
446 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_ALPHA, BlendFactor::ONE );
447 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_ALPHA, BlendFactor::ONE );
449 //Each metaball has a different radius
450 mMetaballs[0].radius = mMetaballs[0].initRadius = 0.0145f;
451 mMetaballs[1].radius = mMetaballs[1].initRadius = 0.012f;
452 mMetaballs[2].radius = mMetaballs[2].initRadius = 0.0135f;
453 mMetaballs[3].radius = mMetaballs[3].initRadius = 0.0135f;
455 //Initialization of each of the metaballs
456 for (int i = 0 ; i < METABALL_NUMBER ; i++)
458 mMetaballs[i].position = Vector2(0.0f, 0.0f);
460 mMetaballs[i].actor = Actor::New( );
461 mMetaballs[i].actor.SetName("Metaball");
462 mMetaballs[i].actor.SetScale( 1.0f );
463 mMetaballs[i].actor.SetParentOrigin( ParentOrigin::CENTER );
466 mMetaballs[i].actor.AddRenderer( renderer );
468 mMetaballs[i].positionIndex = mMetaballs[i].actor.RegisterProperty( "uPositionMetaball", mMetaballs[i].position );
470 mMetaballs[i].positionVarIndex = mMetaballs[i].actor.RegisterProperty( "uPositionVar", Vector2(0.f,0.f) );
472 mMetaballs[i].gravityIndex = mMetaballs[i].actor.RegisterProperty( "uGravityVector", Vector2(0.f,0.f) );
474 mMetaballs[i].radiusIndex = mMetaballs[i].actor.RegisterProperty( "uRadius", mMetaballs[i].radius );
476 mMetaballs[i].radiusVarIndex = mMetaballs[i].actor.RegisterProperty( "uRadiusVar", 0.f );
478 mMetaballs[i].aspectIndex = mMetaballs[i].actor.RegisterProperty( "uAspect", aspect );
480 mMetaballs[i].actor.SetSize(400, 400);
484 mMetaballRoot = Actor::New();
485 mMetaballRoot.SetParentOrigin( ParentOrigin::CENTER );
486 for (int i = 0 ; i < METABALL_NUMBER ; i++)
488 mMetaballRoot.Add( mMetaballs[i].actor );
491 //Initialization of variables related to metaballs
492 mMetaballPosVariation = Vector2(0,0);
493 mMetaballPosVariationFrom = Vector2(0,0);
494 mMetaballPosVariationTo = Vector2(0,0);
495 mCurrentTouchPosition = Vector2(0,0);
499 * Create the render task and FBO to render the metaballs into a texture
501 void MetaballRefracController::CreateMetaballImage()
503 //We create an FBO and a render task to create to render the metaballs with a fragment shader
504 Stage stage = Stage::GetCurrent();
505 mMetaballFBO = FrameBufferImage::New(mScreenSize.x, mScreenSize.y );
507 stage.Add(mMetaballRoot);
509 //Creation of the render task used to render the metaballs
510 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
511 RenderTask task = taskList.CreateTask();
512 task.SetRefreshRate( RenderTask::REFRESH_ALWAYS );
513 task.SetSourceActor( mMetaballRoot );
514 task.SetExclusive(true);
515 task.SetClearColor( Color::BLACK );
516 task.SetClearEnabled( true );
517 task.SetTargetFrameBuffer( mMetaballFBO );
521 * Create a mesh image to render the final composition
523 void MetaballRefracController::AddRefractionImage()
525 //Creation of the composition image
528 Geometry metaballGeom = CreateGeometryComposition();
530 //Create Refraction shader and renderer
531 mShaderRefraction = Shader::New( METABALL_VERTEX_SHADER, REFRACTION_FRAG_SHADER );
533 //Create new texture set
534 mTextureSetRefraction = TextureSet::New();
535 mTextureSetRefraction.SetImage( 0u, mBackImage );
536 mTextureSetRefraction.SetImage( 1u, mMetaballFBO );
538 //Create normal shader
539 mShaderNormal = Shader::New( METABALL_VERTEX_SHADER, FRAG_SHADER );
541 //Create new texture set
542 mTextureSetNormal = TextureSet::New();
543 mTextureSetNormal.SetImage( 0u, mBackImage );
546 mCompositionActor = Actor::New( );
547 mCompositionActor.SetParentOrigin(ParentOrigin::CENTER);
548 mCompositionActor.SetPosition(Vector3(0.0f, 0.0f, 0.0f));
549 mCompositionActor.SetSize(mScreenSize.x, mScreenSize.y);
551 mRendererRefraction = Renderer::New( metaballGeom, mShaderNormal );
552 mRendererRefraction.SetTextures( mTextureSetNormal );
553 mCompositionActor.AddRenderer( mRendererRefraction );
555 Stage stage = Stage::GetCurrent();
556 stage.Add( mCompositionActor );
560 * Creation of all the metaballs animations (gravity, movement, size, etc.)
562 void MetaballRefracController::CreateAnimations()
567 mPositionVarAnimation[1] = Animation::New(2.f);
568 mPositionVarAnimation[1].SetLooping( false );
569 mPositionVarAnimation[1].Pause();
570 mPositionVarAnimation[1].FinishedSignal().Connect( this, &MetaballRefracController::LaunchGetBackToPositionAnimation );
572 KeyFrames keySinCosVariation = KeyFrames::New();
573 Vector2 sinCosVariation(0,0);
574 for ( i = 0 ; i < 360 ; i++)
576 sinCosVariation.x = 0.05f * (-sin(i * Math::PI/180.f) + cos(i * Math::PI/180.f));
577 sinCosVariation.y = 0.05f * (sin(i * Math::PI/180.f) - cos(i * Math::PI/180.f));
579 keySinCosVariation.Add(key, sinCosVariation);
582 mPositionVarAnimation[2] = Animation::New(6.f);
583 mPositionVarAnimation[2].AnimateBetween(Property( mMetaballs[2].actor, mMetaballs[2].positionVarIndex ), keySinCosVariation);
584 mPositionVarAnimation[2].SetLooping( true );
585 mPositionVarAnimation[2].Pause();
587 KeyFrames keyCosSinVariation = KeyFrames::New();
588 Vector2 cosSinVariation(0,0);
589 for ( i = 0 ; i < 360 ; i++)
591 cosSinVariation.x = 0.05f * (-sin(i * Math::PI/180.f) - cos(i * Math::PI/180.f));
592 cosSinVariation.y = 0.05f * (sin(i * Math::PI/180.f) + cos(i * Math::PI/180.f));
594 keyCosSinVariation.Add(key, cosSinVariation);
597 mPositionVarAnimation[3] = Animation::New(6.f);
598 mPositionVarAnimation[3].AnimateBetween(Property( mMetaballs[3].actor, mMetaballs[3].positionVarIndex ), keyCosSinVariation);
599 mPositionVarAnimation[3].SetLooping( true );
600 mPositionVarAnimation[3].Pause();
602 //Animations for gravity
603 for ( i = 0 ; i < METABALL_NUMBER ; i++)
605 mGravityAnimation[i] = Animation::New(25.f);
606 mGravityAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].gravityIndex ), mGravity * 25.f * 3.f);
607 mGravityAnimation[i].SetLooping( false );
608 mGravityAnimation[i].Pause();
611 //Animation to decrease size of metaballs when there is no click
612 for ( i = 0 ; i < METABALL_NUMBER ; i++)
614 mRadiusDecAnimation[i] = Animation::New(25.f);
615 mRadiusDecAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].radiusIndex ), -0.004f * 25.f * 3.f);
616 mRadiusDecAnimation[i].SetLooping( false );
617 mRadiusDecAnimation[i].Pause();
620 //Animation to grow the size of the metaballs the first second of the click
621 for ( i = 0 ; i < METABALL_NUMBER ; i++)
623 mRadiusIncFastAnimation[i] = Animation::New(0.3f);
624 mRadiusIncFastAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].radiusIndex ), 0.06f);
625 mRadiusIncFastAnimation[i].SetLooping( false );
626 mRadiusIncFastAnimation[i].Pause();
628 mRadiusIncFastAnimation[0].FinishedSignal().Connect( this, &MetaballRefracController::LaunchRadiusIncSlowAnimations );
630 //Animation to grow the size of the metaballs afterwards
631 for ( i = 0 ; i < METABALL_NUMBER ; i++)
633 mRadiusIncSlowAnimation[i] = Animation::New(20.f);
634 mRadiusIncSlowAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].radiusIndex ), 0.04f);
635 mRadiusIncSlowAnimation[i].SetLooping( false );
636 mRadiusIncSlowAnimation[i].Pause();
639 //keyframes of a sin function
640 KeyFrames keySin = KeyFrames::New();
642 for ( i = 0 ; i < 360 ; i++)
644 val = 0.01f * sin(i * Math::PI/180.f);
646 keySin.Add(key, val);
649 //Animation to change the size of the metaball
650 mRadiusVarAnimation[2] = Animation::New(8.f);
651 mRadiusVarAnimation[2].AnimateBetween(Property( mMetaballs[2].actor, mMetaballs[2].radiusVarIndex ), keySin);
652 mRadiusVarAnimation[2].SetLooping( true );
654 //keyframes of a cos function
655 KeyFrames keyCos = KeyFrames::New();
656 for ( i = 0 ; i < 360 ; i++)
658 val = 0.01f * cos(i * Math::PI/180.f);
660 keyCos.Add(key, val);
663 //Animation to change the size of the metaball
664 mRadiusVarAnimation[3] = Animation::New(8.f);
665 mRadiusVarAnimation[3].AnimateBetween(Property( mMetaballs[3].actor, mMetaballs[3].radiusVarIndex ), keyCos);
666 mRadiusVarAnimation[3].SetLooping( true );
670 * Function to launch the animation to get the metaball[1] back to the center
672 void MetaballRefracController::LaunchGetBackToPositionAnimation(Animation &source)
674 mMetaballPosVariationTo = Vector2(0,0);
676 mPositionVarAnimation[1] = Animation::New(1.f);
677 mPositionVarAnimation[1].SetLooping( false );
678 mPositionVarAnimation[1].AnimateTo(Property( mMetaballs[1].actor, mMetaballs[1].positionVarIndex ), Vector2(0,0));
679 mPositionVarAnimation[1].Play();
683 * Function to launch the gro slow radius for the metaballs, and also the small variations for metaball[2] and [3]
685 void MetaballRefracController::LaunchRadiusIncSlowAnimations(Animation &source)
687 for (int i = 0 ; i < METABALL_NUMBER ; i++)
689 mRadiusIncSlowAnimation[i].Play();
691 mPositionVarAnimation[2].Play();
692 mPositionVarAnimation[3].Play();
696 * Function to stop all animations related to the click of the user in the screen
698 void MetaballRefracController::StopClickAnimations()
700 for (int i = 0 ; i < METABALL_NUMBER ; i++)
702 mRadiusIncSlowAnimation[i].Stop();
703 mRadiusIncFastAnimation[i].Stop();
705 mPositionVarAnimation[1].Stop();
706 mPositionVarAnimation[2].Stop();
707 mPositionVarAnimation[3].Stop();
711 * Function to stop all animations related to the after click of the user in the screen
713 void MetaballRefracController::StopAfterClickAnimations()
715 for (int i = 0 ; i < METABALL_NUMBER ; i++)
717 mGravityAnimation[i].Stop();
718 mRadiusDecAnimation[i].Stop();
720 mMetaballs[i].radius = mMetaballs[i].initRadius;
722 mMetaballs[i].actor.SetProperty(mMetaballs[i].gravityIndex, Vector2(0,0));
723 mMetaballs[i].actor.SetProperty(mMetaballs[i].radiusIndex, mMetaballs[i].radius);
724 mMetaballs[i].actor.SetProperty(mMetaballs[i].radiusVarIndex, 0.f);
726 mRadiusVarAnimation[2].Stop();
727 mRadiusVarAnimation[3].Stop();
731 * Function that resets the sate of the different Metaballs
733 void MetaballRefracController::ResetMetaballsState()
735 mRendererRefraction.SetTextures(mTextureSetNormal);
736 mRendererRefraction.SetShader( mShaderNormal );
738 for (int i = 0 ; i < METABALL_NUMBER ; i++)
740 mMetaballs[i].radius = mMetaballs[i].initRadius;
743 mMetaballPosVariationTo = Vector2(0,0);
744 mMetaballPosVariationFrom = Vector2(0,0);
745 mMetaballPosVariation = Vector2(0,0);
747 mGravityVar = Vector2(0,0);
751 * Function to set the actual position of the metaballs when the user clicks the screen
753 void MetaballRefracController::SetPositionToMetaballs(Vector2 & metaballCenter)
755 //We set the position for the metaballs based on click position
756 for (int i = 0 ; i < METABALL_NUMBER ; i++)
758 mMetaballs[i].position = metaballCenter;
759 mMetaballs[i].actor.SetProperty(mMetaballs[i].positionIndex, mMetaballs[0].position); // 0 y no i ?!?!?!
763 bool MetaballRefracController::OnTouch( Actor actor, const TouchData& touch )
765 float aspectR = mScreenSize.y / mScreenSize.x;
766 switch( touch.GetState( 0 ) )
768 case PointState::DOWN:
770 StopAfterClickAnimations();
771 for (int i = 0 ; i < METABALL_NUMBER ; i++)
772 mRadiusIncFastAnimation[i].Play();
773 mRadiusVarAnimation[2].Play();
774 mRadiusVarAnimation[3].Play();
776 //We draw with the refraction-composition shader
777 mRendererRefraction.SetTextures(mTextureSetRefraction);
778 mRendererRefraction.SetShader( mShaderRefraction );
779 mCurrentTouchPosition = touch.GetScreenPosition( 0 );
781 //we use the click position for the metaballs
782 Vector2 metaballCenter = Vector2((mCurrentTouchPosition.x / mScreenSize.x) - 0.5, (aspectR * (mScreenSize.y - mCurrentTouchPosition.y) / mScreenSize.y) - 0.5) * 2.0;
783 SetPositionToMetaballs(metaballCenter);
786 case PointState::MOTION:
788 Vector2 screen = touch.GetScreenPosition( 0 );
789 Vector2 displacement = screen - mCurrentTouchPosition;
790 mCurrentTouchPosition = screen;
792 mMetaballPosVariationTo.x += (displacement.x / mScreenSize.x) * 2.2;
793 mMetaballPosVariationTo.y += (- displacement.y / mScreenSize.y) * 2.2;
795 if (mPositionVarAnimation[1])
797 mPositionVarAnimation[1].FinishedSignal().Disconnect( this, &MetaballRefracController::LaunchGetBackToPositionAnimation );
798 mPositionVarAnimation[1].Stop();
800 mPositionVarAnimation[1] = Animation::New(1.f);
801 mPositionVarAnimation[1].SetLooping( false );
802 mPositionVarAnimation[1].AnimateTo(Property( mMetaballs[1].actor, mMetaballs[1].positionVarIndex ), mMetaballPosVariationTo);
803 mPositionVarAnimation[1].FinishedSignal().Connect( this, &MetaballRefracController::LaunchGetBackToPositionAnimation );
804 mPositionVarAnimation[1].Play();
806 //we use the click position for the metaballs
807 Vector2 metaballCenter = Vector2((screen.x / mScreenSize.x) - 0.5, (aspectR * (mScreenSize.y - screen.y) / mScreenSize.y) - 0.5) * 2.0;
808 SetPositionToMetaballs(metaballCenter);
812 case PointState::LEAVE:
813 case PointState::INTERRUPTED:
815 //Stop click animations
816 StopClickAnimations();
818 //Launch out of screen animations
819 for (int i = 0 ; i < METABALL_NUMBER ; i++)
820 mGravityAnimation[i].Play();
822 for (int i = 0 ; i < METABALL_NUMBER ; i++)
823 mRadiusDecAnimation[i].Play();
834 void MetaballRefracController::OnKeyEvent(const KeyEvent& event)
836 if(event.state == KeyEvent::Down)
838 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
848 //-----------------------------------------------------------------------------------------------
850 void RunTest( Application& application )
852 MetaballRefracController test( application );
854 application.MainLoop();
857 // Entry point for Linux & Tizen applications
859 int DALI_EXPORT_API main( int argc, char **argv )
861 Application application = Application::New( &argc, &argv );
863 RunTest( application );