2 * Copyright (c) 2016 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/images/texture-set-image.h>
20 #include <dali/public-api/rendering/renderer.h>
21 #include <dali-toolkit/dali-toolkit.h>
25 #include "shared/utility.h"
28 using namespace Dali::Toolkit;
32 const char * const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-2.jpg" );
33 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
35 const float GRAVITY_X(0);
36 const float GRAVITY_Y(-0.09);
39 #define METABALL_NUMBER 4
41 const char*const METABALL_VERTEX_SHADER = DALI_COMPOSE_SHADER (
42 attribute mediump vec2 aPosition;\n
43 attribute mediump vec2 aTexture;\n
44 attribute mediump vec3 aNormal;\n
45 uniform mediump mat4 uMvpMatrix;\n
46 uniform mediump vec3 uSize;\n
47 uniform lowp vec4 uColor;\n
48 varying mediump vec2 vTexCoord;\n
52 mediump vec4 vertexPosition = vec4(aPosition.x, aPosition.y, 0.0, 1.0);\n
53 vertexPosition = uMvpMatrix * vertexPosition;\n
54 gl_Position = vertexPosition;\n
55 vTexCoord = aTexture;\n
60 const char*const METABALL_FRAG_SHADER = DALI_COMPOSE_SHADER (
61 precision mediump float;\n
62 varying vec2 vTexCoord;\n
63 uniform vec2 uPositionMetaball;\n
64 uniform vec2 uPositionVar;\n
65 uniform vec2 uGravityVector;\n
66 uniform float uRadius;\n
67 uniform float uRadiusVar;\n
68 uniform float uAspect;\n
71 vec2 adjustedCoords = vTexCoord * 2.0 - 1.0;\n
72 vec2 finalMetaballPosition = uPositionMetaball + uGravityVector + uPositionVar;\n
74 float distance = (adjustedCoords.x - finalMetaballPosition.x) * (adjustedCoords.x - finalMetaballPosition.x) +
75 (adjustedCoords.y - finalMetaballPosition.y) * (adjustedCoords.y - finalMetaballPosition.y);\n
76 float finalRadius = uRadius + uRadiusVar;\n
77 float color = finalRadius / sqrt( distance );\n
78 vec2 bordercolor = vec2(0.0,0.0);\n
79 if (vTexCoord.x < 0.1)\n
81 bordercolor.x = (0.1 - vTexCoord.x) * 0.8;\n
83 if (vTexCoord.x > 0.9)\n
85 bordercolor.x = (vTexCoord.x - 0.9) * 0.8;\n
87 if (vTexCoord.y < 0.1)\n
89 bordercolor.y = (0.1 - vTexCoord.y) * 0.8;\n
91 if (vTexCoord.y > (0.9 * uAspect))\n
93 bordercolor.y = (vTexCoord.y - (0.9 * uAspect)) * 0.8;\n
95 float border = (bordercolor.x + bordercolor.y) * 0.5;\n
96 gl_FragColor = vec4(color + border,color + border,color + border,1.0);\n
100 const char*const REFRACTION_FRAG_SHADER = DALI_COMPOSE_SHADER (
101 precision mediump float;\n
102 varying vec2 vTexCoord;\n
103 uniform sampler2D sTexture;\n
104 uniform sampler2D sEffect;\n
107 vec4 metaColor = texture2D(sEffect, vTexCoord);\n
109 float bright = 1.0;\n
110 if (metaColor.r > 0.85)\n
112 zoomCoords = ((vTexCoord - 0.5) * 0.95) + 0.5;\n
114 else if (metaColor.r > 0.81)\n
116 float interpolation = mix(0.95, 1.05, (0.85 - metaColor.r) * 50.0);\n
117 zoomCoords = ((vTexCoord - 0.5) * interpolation) + 0.5;\n
122 zoomCoords = vTexCoord;\n
125 gl_FragColor = texture2D(sTexture, zoomCoords) * bright;\n
129 const char*const FRAG_SHADER = DALI_COMPOSE_SHADER (
130 precision mediump float;\n
131 varying vec2 vTexCoord;\n
132 uniform sampler2D sTexture;\n
135 gl_FragColor = texture2D(sTexture, vTexCoord);\n
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;
189 Vector2 mCurrentTouchPosition;
190 Vector2 mMetaballPosVariation;
191 Vector2 mMetaballPosVariationFrom;
192 Vector2 mMetaballPosVariationTo;
193 Vector2 mMetaballCenter;
198 Renderer mRendererRefraction;
199 TextureSet mTextureSetRefraction;
200 Shader mShaderRefraction;
201 TextureSet mTextureSetNormal;
202 Shader mShaderNormal;
205 Animation mGravityAnimation[METABALL_NUMBER];
206 Animation mRadiusDecAnimation[METABALL_NUMBER];
207 Animation mRadiusIncFastAnimation[METABALL_NUMBER];
208 Animation mRadiusIncSlowAnimation[METABALL_NUMBER];
209 Animation mRadiusVarAnimation[METABALL_NUMBER];
210 Animation mPositionVarAnimation[METABALL_NUMBER];
213 Geometry CreateGeometry();
214 Geometry CreateGeometryComposition();
216 void CreateMetaballActors();
217 void CreateMetaballImage();
218 void AddRefractionImage();
219 void CreateAnimations();
221 void LaunchRadiusIncSlowAnimations(Animation &source);
222 void LaunchGetBackToPositionAnimation(Animation &source);
224 void StopClickAnimations();
225 void StopAfterClickAnimations();
227 void ResetMetaballsState();
229 void SetPositionToMetaballs(Vector2 & metaballCenter);
233 //-----------------------------------------------------------------------------------------------
239 MetaballRefracController::MetaballRefracController( Application& application )
240 : mApplication( application )
242 // Connect to the Application's Init signal
243 mApplication.InitSignal().Connect( this, &MetaballRefracController::Create );
246 MetaballRefracController::~MetaballRefracController()
248 // Nothing to do here;
252 * Setter function for gravity
254 void MetaballRefracController::SetGravity(const Vector2 & gravity)
260 * Main create function, it creates the metaballs and all the
262 void MetaballRefracController::Create( Application& app )
264 Stage stage = Stage::GetCurrent();
266 stage.KeyEventSignal().Connect(this, &MetaballRefracController::OnKeyEvent);
268 mScreenSize = stage.GetSize();
270 stage.SetBackgroundColor(Color::BLACK);
272 //Set background image for the view
273 mBackImage = DemoHelper::LoadImage( BACKGROUND_IMAGE );
275 mGravity = Vector2(GRAVITY_X,GRAVITY_Y);
276 mGravityVar = Vector2(0,0);
278 //Create internal data
279 CreateMetaballActors();
280 CreateMetaballImage();
281 AddRefractionImage();
285 // Connect the callback to the touch signal on the mesh actor
286 stage.GetRootLayer().TouchSignal().Connect( this, &MetaballRefracController::OnTouch );
290 * Create a mesh data with the geometry for the metaball rendering
292 Geometry MetaballRefracController::CreateGeometry()
294 float aspect = (float)mScreenSize.y / (float)mScreenSize.x;
296 // Create vertices and specify their color
297 float xsize = mScreenSize.x * 0.5;
299 //We create the meshdata for the metaballs
300 struct VertexPosition { Vector2 position; };
301 struct VertexTexture { Vector2 texture; };
302 struct VertexNormal { Vector3 normal; };
304 VertexPosition vertices[] = {
305 { Vector2( -xsize, -xsize * aspect) },
306 { Vector2( xsize, -xsize * aspect) },
307 { Vector2( -xsize, xsize * aspect) },
308 { Vector2( xsize, xsize * aspect) }
311 VertexTexture textures[] = {
312 { Vector2(0.0f, 0.0f) },
313 { Vector2(1.0f, 0.0f) },
314 { Vector2(0.0f, 1.0f * aspect) },
315 { Vector2(1.0f, 1.0f * aspect) }
318 VertexNormal normals [] = {
319 { Vector3(0.0f, 0.0f, 1.0f) },
320 { Vector3(0.0f, 0.0f, 1.0f) },
321 { Vector3(0.0f, 0.0f, 1.0f) },
322 { Vector3(0.0f, 0.0f, 1.0f) }
325 unsigned int numberOfVertices = sizeof(vertices)/sizeof(VertexPosition);
328 Property::Map positionVertexFormat;
329 positionVertexFormat["aPosition"] = Property::VECTOR2;
330 PropertyBuffer positionVertices = PropertyBuffer::New( positionVertexFormat );
331 positionVertices.SetData( vertices, numberOfVertices );
334 Property::Map textureVertexFormat;
335 textureVertexFormat["aTexture"] = Property::VECTOR2;
336 PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat );
337 textureVertices.SetData( textures, numberOfVertices );
340 Property::Map normalVertexFormat;
341 normalVertexFormat["aNormal"] = Property::VECTOR3;
342 PropertyBuffer normalVertices = PropertyBuffer::New( normalVertexFormat );
343 normalVertices.SetData( normals, numberOfVertices );
346 unsigned short indices[] = { 0, 3, 1, 0, 2, 3 };
348 // Create the geometry object
349 Geometry texturedQuadGeometry = Geometry::New();
350 texturedQuadGeometry.AddVertexBuffer( positionVertices );
351 texturedQuadGeometry.AddVertexBuffer( textureVertices );
352 texturedQuadGeometry.AddVertexBuffer( normalVertices );
354 texturedQuadGeometry.SetIndexBuffer ( &indices[0], 6 );
356 return texturedQuadGeometry;
360 * Create a mesh data with the geometry for the metaball rendering
362 Geometry MetaballRefracController::CreateGeometryComposition()
364 float aspect = (float)mScreenSize.y / (float)mScreenSize.x;
366 // Create vertices and specify their color
367 float xsize = mScreenSize.x * 0.5;
369 //We create the meshdata for the metaballs
370 struct VertexPosition { Vector2 position; };
371 struct VertexTexture { Vector2 texture; };
372 struct VertexNormal { Vector3 normal; };
374 VertexPosition vertices[] = {
375 { Vector2( -xsize, -xsize * aspect) },
376 { Vector2( xsize, -xsize * aspect) },
377 { Vector2( -xsize, xsize * aspect) },
378 { Vector2( xsize, xsize * aspect) }
381 VertexTexture textures[] = {
382 { Vector2(0.0f, 0.0f) },
383 { Vector2(1.0f, 0.0f) },
384 { Vector2(0.0f, 1.0f) },
385 { Vector2(1.0f, 1.0f) }
388 VertexNormal normals [] = {
389 { Vector3(0.0f, 0.0f, 1.0f) },
390 { Vector3(0.0f, 0.0f, 1.0f) },
391 { Vector3(0.0f, 0.0f, 1.0f) },
392 { Vector3(0.0f, 0.0f, 1.0f) }
395 unsigned int numberOfVertices = sizeof(vertices)/sizeof(VertexPosition);
398 Property::Map positionVertexFormat;
399 positionVertexFormat["aPosition"] = Property::VECTOR2;
400 PropertyBuffer positionVertices = PropertyBuffer::New( positionVertexFormat );
401 positionVertices.SetData( vertices, numberOfVertices );
404 Property::Map textureVertexFormat;
405 textureVertexFormat["aTexture"] = Property::VECTOR2;
406 PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat );
407 textureVertices.SetData( textures, numberOfVertices );
410 Property::Map normalVertexFormat;
411 normalVertexFormat["aNormal"] = Property::VECTOR3;
412 PropertyBuffer normalVertices = PropertyBuffer::New( normalVertexFormat );
413 normalVertices.SetData( normals, numberOfVertices );
416 unsigned short indices[] = { 0, 3, 1, 0, 2, 3 };
418 // Create the geometry object
419 Geometry texturedQuadGeometry = Geometry::New();
420 texturedQuadGeometry.AddVertexBuffer( positionVertices );
421 texturedQuadGeometry.AddVertexBuffer( textureVertices );
422 texturedQuadGeometry.AddVertexBuffer( normalVertices );
424 texturedQuadGeometry.SetIndexBuffer ( &indices[0], sizeof( indices )/ sizeof( indices[0] ) );
426 return texturedQuadGeometry;
430 * Create a mesh actor for the metaballs
432 void MetaballRefracController::CreateMetaballActors()
434 //We create metaball structures
435 //With MeshData Textured
436 float aspect = (float)mScreenSize.y / (float)mScreenSize.x;
438 //Create the renderer for the metaballs
439 Shader shader = Shader::New( METABALL_VERTEX_SHADER, METABALL_FRAG_SHADER );
440 Geometry metaballGeom = CreateGeometry();
441 Renderer renderer = Renderer::New( metaballGeom, shader );
442 renderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
443 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_RGB, BlendFactor::ONE );
444 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ONE );
445 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_ALPHA, BlendFactor::ONE );
446 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_ALPHA, BlendFactor::ONE );
448 //Each metaball has a different radius
449 mMetaballs[0].radius = mMetaballs[0].initRadius = 0.0145f;
450 mMetaballs[1].radius = mMetaballs[1].initRadius = 0.012f;
451 mMetaballs[2].radius = mMetaballs[2].initRadius = 0.0135f;
452 mMetaballs[3].radius = mMetaballs[3].initRadius = 0.0135f;
454 //Initialization of each of the metaballs
455 for (int i = 0 ; i < METABALL_NUMBER ; i++)
457 mMetaballs[i].position = Vector2(0.0f, 0.0f);
459 mMetaballs[i].actor = Actor::New( );
460 mMetaballs[i].actor.SetName("Metaball");
461 mMetaballs[i].actor.SetScale( 1.0f );
462 mMetaballs[i].actor.SetParentOrigin( ParentOrigin::CENTER );
465 mMetaballs[i].actor.AddRenderer( renderer );
467 mMetaballs[i].positionIndex = mMetaballs[i].actor.RegisterProperty( "uPositionMetaball", mMetaballs[i].position );
469 mMetaballs[i].positionVarIndex = mMetaballs[i].actor.RegisterProperty( "uPositionVar", Vector2(0.f,0.f) );
471 mMetaballs[i].gravityIndex = mMetaballs[i].actor.RegisterProperty( "uGravityVector", Vector2(0.f,0.f) );
473 mMetaballs[i].radiusIndex = mMetaballs[i].actor.RegisterProperty( "uRadius", mMetaballs[i].radius );
475 mMetaballs[i].radiusVarIndex = mMetaballs[i].actor.RegisterProperty( "uRadiusVar", 0.f );
477 mMetaballs[i].aspectIndex = mMetaballs[i].actor.RegisterProperty( "uAspect", aspect );
479 mMetaballs[i].actor.SetSize(400, 400);
483 mMetaballRoot = Actor::New();
484 mMetaballRoot.SetParentOrigin( ParentOrigin::CENTER );
485 for (int i = 0 ; i < METABALL_NUMBER ; i++)
487 mMetaballRoot.Add( mMetaballs[i].actor );
490 //Initialization of variables related to metaballs
491 mMetaballPosVariation = Vector2(0,0);
492 mMetaballPosVariationFrom = Vector2(0,0);
493 mMetaballPosVariationTo = Vector2(0,0);
494 mCurrentTouchPosition = Vector2(0,0);
498 * Create the render task and FBO to render the metaballs into a texture
500 void MetaballRefracController::CreateMetaballImage()
502 //We create an FBO and a render task to create to render the metaballs with a fragment shader
503 Stage stage = Stage::GetCurrent();
504 mMetaballFBO = FrameBufferImage::New(mScreenSize.x, mScreenSize.y );
506 stage.Add(mMetaballRoot);
508 //Creation of the render task used to render the metaballs
509 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
510 RenderTask task = taskList.CreateTask();
511 task.SetRefreshRate( RenderTask::REFRESH_ALWAYS );
512 task.SetSourceActor( mMetaballRoot );
513 task.SetExclusive(true);
514 task.SetClearColor( Color::BLACK );
515 task.SetClearEnabled( true );
516 task.SetTargetFrameBuffer( mMetaballFBO );
520 * Create a mesh image to render the final composition
522 void MetaballRefracController::AddRefractionImage()
524 //Creation of the composition image
527 Geometry metaballGeom = CreateGeometryComposition();
529 //Create Refraction shader and renderer
530 mShaderRefraction = Shader::New( METABALL_VERTEX_SHADER, REFRACTION_FRAG_SHADER );
532 //Create new texture set
533 mTextureSetRefraction = TextureSet::New();
534 TextureSetImage( mTextureSetRefraction, 0u, mBackImage );
535 TextureSetImage( mTextureSetRefraction, 1u, mMetaballFBO );
537 //Create normal shader
538 mShaderNormal = Shader::New( METABALL_VERTEX_SHADER, FRAG_SHADER );
540 //Create new texture set
541 mTextureSetNormal = TextureSet::New();
542 TextureSetImage( mTextureSetNormal, 0u, mBackImage );
545 mCompositionActor = Actor::New( );
546 mCompositionActor.SetParentOrigin(ParentOrigin::CENTER);
547 mCompositionActor.SetPosition(Vector3(0.0f, 0.0f, 0.0f));
548 mCompositionActor.SetSize(mScreenSize.x, mScreenSize.y);
550 mRendererRefraction = Renderer::New( metaballGeom, mShaderNormal );
551 mRendererRefraction.SetTextures( mTextureSetNormal );
552 mCompositionActor.AddRenderer( mRendererRefraction );
554 Stage stage = Stage::GetCurrent();
555 stage.Add( mCompositionActor );
559 * Creation of all the metaballs animations (gravity, movement, size, etc.)
561 void MetaballRefracController::CreateAnimations()
566 mPositionVarAnimation[1] = Animation::New(2.f);
567 mPositionVarAnimation[1].SetLooping( false );
568 mPositionVarAnimation[1].Pause();
569 mPositionVarAnimation[1].FinishedSignal().Connect( this, &MetaballRefracController::LaunchGetBackToPositionAnimation );
571 KeyFrames keySinCosVariation = KeyFrames::New();
572 Vector2 sinCosVariation(0,0);
573 for ( i = 0 ; i < 360 ; i++)
575 sinCosVariation.x = 0.05f * (-sin(i * Math::PI/180.f) + cos(i * Math::PI/180.f));
576 sinCosVariation.y = 0.05f * (sin(i * Math::PI/180.f) - cos(i * Math::PI/180.f));
578 keySinCosVariation.Add(key, sinCosVariation);
581 mPositionVarAnimation[2] = Animation::New(6.f);
582 mPositionVarAnimation[2].AnimateBetween(Property( mMetaballs[2].actor, mMetaballs[2].positionVarIndex ), keySinCosVariation);
583 mPositionVarAnimation[2].SetLooping( true );
584 mPositionVarAnimation[2].Pause();
586 KeyFrames keyCosSinVariation = KeyFrames::New();
587 Vector2 cosSinVariation(0,0);
588 for ( i = 0 ; i < 360 ; i++)
590 cosSinVariation.x = 0.05f * (-sin(i * Math::PI/180.f) - cos(i * Math::PI/180.f));
591 cosSinVariation.y = 0.05f * (sin(i * Math::PI/180.f) + cos(i * Math::PI/180.f));
593 keyCosSinVariation.Add(key, cosSinVariation);
596 mPositionVarAnimation[3] = Animation::New(6.f);
597 mPositionVarAnimation[3].AnimateBetween(Property( mMetaballs[3].actor, mMetaballs[3].positionVarIndex ), keyCosSinVariation);
598 mPositionVarAnimation[3].SetLooping( true );
599 mPositionVarAnimation[3].Pause();
601 //Animations for gravity
602 for ( i = 0 ; i < METABALL_NUMBER ; i++)
604 mGravityAnimation[i] = Animation::New(25.f);
605 mGravityAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].gravityIndex ), mGravity * 25.f * 3.f);
606 mGravityAnimation[i].SetLooping( false );
607 mGravityAnimation[i].Pause();
610 //Animation to decrease size of metaballs when there is no click
611 for ( i = 0 ; i < METABALL_NUMBER ; i++)
613 mRadiusDecAnimation[i] = Animation::New(25.f);
614 mRadiusDecAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].radiusIndex ), -0.004f * 25.f * 3.f);
615 mRadiusDecAnimation[i].SetLooping( false );
616 mRadiusDecAnimation[i].Pause();
619 //Animation to grow the size of the metaballs the first second of the click
620 for ( i = 0 ; i < METABALL_NUMBER ; i++)
622 mRadiusIncFastAnimation[i] = Animation::New(0.3f);
623 mRadiusIncFastAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].radiusIndex ), 0.06f);
624 mRadiusIncFastAnimation[i].SetLooping( false );
625 mRadiusIncFastAnimation[i].Pause();
627 mRadiusIncFastAnimation[0].FinishedSignal().Connect( this, &MetaballRefracController::LaunchRadiusIncSlowAnimations );
629 //Animation to grow the size of the metaballs afterwards
630 for ( i = 0 ; i < METABALL_NUMBER ; i++)
632 mRadiusIncSlowAnimation[i] = Animation::New(20.f);
633 mRadiusIncSlowAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].radiusIndex ), 0.04f);
634 mRadiusIncSlowAnimation[i].SetLooping( false );
635 mRadiusIncSlowAnimation[i].Pause();
638 //keyframes of a sin function
639 KeyFrames keySin = KeyFrames::New();
641 for ( i = 0 ; i < 360 ; i++)
643 val = 0.01f * sin(i * Math::PI/180.f);
645 keySin.Add(key, val);
648 //Animation to change the size of the metaball
649 mRadiusVarAnimation[2] = Animation::New(8.f);
650 mRadiusVarAnimation[2].AnimateBetween(Property( mMetaballs[2].actor, mMetaballs[2].radiusVarIndex ), keySin);
651 mRadiusVarAnimation[2].SetLooping( true );
653 //keyframes of a cos function
654 KeyFrames keyCos = KeyFrames::New();
655 for ( i = 0 ; i < 360 ; i++)
657 val = 0.01f * cos(i * Math::PI/180.f);
659 keyCos.Add(key, val);
662 //Animation to change the size of the metaball
663 mRadiusVarAnimation[3] = Animation::New(8.f);
664 mRadiusVarAnimation[3].AnimateBetween(Property( mMetaballs[3].actor, mMetaballs[3].radiusVarIndex ), keyCos);
665 mRadiusVarAnimation[3].SetLooping( true );
669 * Function to launch the animation to get the metaball[1] back to the center
671 void MetaballRefracController::LaunchGetBackToPositionAnimation(Animation &source)
673 mMetaballPosVariationTo = Vector2(0,0);
675 mPositionVarAnimation[1] = Animation::New(1.f);
676 mPositionVarAnimation[1].SetLooping( false );
677 mPositionVarAnimation[1].AnimateTo(Property( mMetaballs[1].actor, mMetaballs[1].positionVarIndex ), Vector2(0,0));
678 mPositionVarAnimation[1].Play();
682 * Function to launch the gro slow radius for the metaballs, and also the small variations for metaball[2] and [3]
684 void MetaballRefracController::LaunchRadiusIncSlowAnimations(Animation &source)
686 for (int i = 0 ; i < METABALL_NUMBER ; i++)
688 mRadiusIncSlowAnimation[i].Play();
690 mPositionVarAnimation[2].Play();
691 mPositionVarAnimation[3].Play();
695 * Function to stop all animations related to the click of the user in the screen
697 void MetaballRefracController::StopClickAnimations()
699 for (int i = 0 ; i < METABALL_NUMBER ; i++)
701 mRadiusIncSlowAnimation[i].Stop();
702 mRadiusIncFastAnimation[i].Stop();
704 mPositionVarAnimation[1].Stop();
705 mPositionVarAnimation[2].Stop();
706 mPositionVarAnimation[3].Stop();
710 * Function to stop all animations related to the after click of the user in the screen
712 void MetaballRefracController::StopAfterClickAnimations()
714 for (int i = 0 ; i < METABALL_NUMBER ; i++)
716 mGravityAnimation[i].Stop();
717 mRadiusDecAnimation[i].Stop();
719 mMetaballs[i].radius = mMetaballs[i].initRadius;
721 mMetaballs[i].actor.SetProperty(mMetaballs[i].gravityIndex, Vector2(0,0));
722 mMetaballs[i].actor.SetProperty(mMetaballs[i].radiusIndex, mMetaballs[i].radius);
723 mMetaballs[i].actor.SetProperty(mMetaballs[i].radiusVarIndex, 0.f);
725 mRadiusVarAnimation[2].Stop();
726 mRadiusVarAnimation[3].Stop();
730 * Function that resets the sate of the different Metaballs
732 void MetaballRefracController::ResetMetaballsState()
734 mRendererRefraction.SetTextures(mTextureSetNormal);
735 mRendererRefraction.SetShader( mShaderNormal );
737 for (int i = 0 ; i < METABALL_NUMBER ; i++)
739 mMetaballs[i].radius = mMetaballs[i].initRadius;
742 mMetaballPosVariationTo = Vector2(0,0);
743 mMetaballPosVariationFrom = Vector2(0,0);
744 mMetaballPosVariation = Vector2(0,0);
746 mGravityVar = Vector2(0,0);
750 * Function to set the actual position of the metaballs when the user clicks the screen
752 void MetaballRefracController::SetPositionToMetaballs(Vector2 & metaballCenter)
754 //We set the position for the metaballs based on click position
755 for (int i = 0 ; i < METABALL_NUMBER ; i++)
757 mMetaballs[i].position = metaballCenter;
758 mMetaballs[i].actor.SetProperty(mMetaballs[i].positionIndex, mMetaballs[0].position); // 0 y no i ?!?!?!
762 bool MetaballRefracController::OnTouch( Actor actor, const TouchData& touch )
764 float aspectR = mScreenSize.y / mScreenSize.x;
765 switch( touch.GetState( 0 ) )
767 case PointState::DOWN:
769 StopAfterClickAnimations();
770 for (int i = 0 ; i < METABALL_NUMBER ; i++)
771 mRadiusIncFastAnimation[i].Play();
772 mRadiusVarAnimation[2].Play();
773 mRadiusVarAnimation[3].Play();
775 //We draw with the refraction-composition shader
776 mRendererRefraction.SetTextures(mTextureSetRefraction);
777 mRendererRefraction.SetShader( mShaderRefraction );
778 mCurrentTouchPosition = touch.GetScreenPosition( 0 );
780 //we use the click position for the metaballs
781 Vector2 metaballCenter = Vector2((mCurrentTouchPosition.x / mScreenSize.x) - 0.5, (aspectR * (mScreenSize.y - mCurrentTouchPosition.y) / mScreenSize.y) - 0.5) * 2.0;
782 SetPositionToMetaballs(metaballCenter);
785 case PointState::MOTION:
787 Vector2 screen = touch.GetScreenPosition( 0 );
788 Vector2 displacement = screen - mCurrentTouchPosition;
789 mCurrentTouchPosition = screen;
791 mMetaballPosVariationTo.x += (displacement.x / mScreenSize.x) * 2.2;
792 mMetaballPosVariationTo.y += (- displacement.y / mScreenSize.y) * 2.2;
794 if (mPositionVarAnimation[1])
796 mPositionVarAnimation[1].FinishedSignal().Disconnect( this, &MetaballRefracController::LaunchGetBackToPositionAnimation );
797 mPositionVarAnimation[1].Stop();
799 mPositionVarAnimation[1] = Animation::New(1.f);
800 mPositionVarAnimation[1].SetLooping( false );
801 mPositionVarAnimation[1].AnimateTo(Property( mMetaballs[1].actor, mMetaballs[1].positionVarIndex ), mMetaballPosVariationTo);
802 mPositionVarAnimation[1].FinishedSignal().Connect( this, &MetaballRefracController::LaunchGetBackToPositionAnimation );
803 mPositionVarAnimation[1].Play();
805 //we use the click position for the metaballs
806 Vector2 metaballCenter = Vector2((screen.x / mScreenSize.x) - 0.5, (aspectR * (mScreenSize.y - screen.y) / mScreenSize.y) - 0.5) * 2.0;
807 SetPositionToMetaballs(metaballCenter);
811 case PointState::LEAVE:
812 case PointState::INTERRUPTED:
814 //Stop click animations
815 StopClickAnimations();
817 //Launch out of screen animations
818 for (int i = 0 ; i < METABALL_NUMBER ; i++)
819 mGravityAnimation[i].Play();
821 for (int i = 0 ; i < METABALL_NUMBER ; i++)
822 mRadiusDecAnimation[i].Play();
833 void MetaballRefracController::OnKeyEvent(const KeyEvent& event)
835 if(event.state == KeyEvent::Down)
837 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
847 //-----------------------------------------------------------------------------------------------
849 void RunTest( Application& application )
851 MetaballRefracController test( application );
853 application.MainLoop();
856 // Entry point for Linux & Tizen applications
858 int DALI_EXPORT_API main( int argc, char **argv )
860 Application application = Application::New( &argc, &argv );
862 RunTest( application );