/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
*/
-//External includes
+// EXTERNAL INCLUDES
#include <cstdio>
#include <string>
+#include <cstdint> // uint32_t, uint16_t etc
-//Internal includes
-#include <dali/dali.h>
-#include <dali/devel-api/rendering/renderer.h>
-#include <dali-toolkit/dali-toolkit.h>
+#include <dali/public-api/rendering/renderer.h>
+#include <dali/public-api/rendering/texture.h>
+#include <dali/public-api/rendering/texture-set.h>
+#include <dali/public-api/rendering/frame-buffer.h>
+#include <dali/public-api/math/random.h>
-#include "shared/view.h"
+// INTERNAL INCLUDES
+#include "shared/utility.h" // DemoHelper::LoadTexture
using namespace Dali;
-using namespace Dali::Toolkit;
-namespace
+namespace // unnamed namespace for constants
{
+// background image
const char * const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-2.jpg" );
-const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
-
-const float GRAVITY_X(0);
-const float GRAVITY_Y(-0.09);
-}
-
-#define METABALL_NUMBER 6
+// number of metaballs
+constexpr uint32_t METABALL_NUMBER = 6;
-const char*const METABALL_VERTEX_SHADER = DALI_COMPOSE_SHADER (
+/**
+ * Vertex shader code for metaball
+ */
+const char* const METABALL_VERTEX_SHADER = DALI_COMPOSE_SHADER (
attribute mediump vec2 aPosition;\n
attribute mediump vec2 aTexture;\n
uniform mediump mat4 uMvpMatrix;\n
}\n
);
-
-const char*const METABALL_FRAG_SHADER = DALI_COMPOSE_SHADER (
+/**
+ * Fragment shader code for metaball
+ */
+const char* const METABALL_FRAG_SHADER = DALI_COMPOSE_SHADER (
precision mediump float;\n
varying vec2 vTexCoord;\n
uniform vec2 uPositionMetaball;\n
}\n
);
-const char*const REFRACTION_FRAG_SHADER = DALI_COMPOSE_SHADER (
+/**
+ * Fragment shader code for metaball and background composition with refraction effect
+ */
+const char* const REFRACTION_FRAG_SHADER = DALI_COMPOSE_SHADER (
precision highp float;\n
varying vec2 vTexCoord;\n
uniform sampler2D sTexture;\n
}\n
);
-const char*const FRAG_SHADER = DALI_COMPOSE_SHADER (
- precision mediump float;\n
- void main()\n
- {\n
- gl_FragColor = texture2D(sTexture, vTexCoord);\n
- }\n
-);
-
-
+/**
+ * Metadata for each ball
+ */
struct MetaballInfo
{
Actor actor;
Property::Index positionVarIndex;
};
+} // unnamed namespace
-/**************************************************************************/
-/* Demo using Metaballs ***********/
-/* When the metaball is clicked it explodes in different balls ***********/
-/**************************************************************************/
+/**
+ * Demo using Metaballs
+ *
+ * When the metaball is clicked it explodes to smaller balls
+ */
class MetaballExplosionController : public ConnectionTracker
{
public:
+
+ /**
+ * Constructor
+ * @param application
+ */
MetaballExplosionController( Application& application );
- ~MetaballExplosionController();
/**
- * Main create function, it creates the metaballs and all the related data
+ * Destructor
+ */
+ virtual ~MetaballExplosionController();
+
+ /**
+ * Creates the metaballs and initializes the scene
*/
void Create( Application& app );
/**
- * Touch event function
+ * Touch event handler to center metaballs at touch position
+ * and start explosion animation on release
*/
- bool OnTouch( Actor actor, const TouchEvent& touch );
+ bool OnTouch( Actor actor, const TouchData& touch );
/**
- * Key event function
+ * Key event handler to quit application on escape or back key
*/
void OnKeyEvent(const KeyEvent& event);
+private: // Data
-private:
Application& mApplication;
Vector2 mScreenSize;
- Layer mContentLayer;
-
- Image mBackImage;
- FrameBufferImage mMetaballFBO;
+ Texture mBackgroundTexture;
+ FrameBuffer mMetaballFBO;
Actor mMetaballRoot;
MetaballInfo mMetaballs[METABALL_NUMBER];
//Animations
Animation mPositionVarAnimation[METABALL_NUMBER];
- int mDispersion;
+ uint32_t mDispersion;
Animation mDispersionAnimation[METABALL_NUMBER];
Timer mTimerDispersion;
- float mTimeMult;
+ float mTimeMultiplier;
- //Private functions
+ // Private helper functions
/**
* Create a mesh data with the geometry for the metaball rendering
+ * @param aspectMappedTexture whether texture coords should be mapped based on aspect ratio
*/
- Geometry CreateGeometry();
+ Geometry CreateGeometry( bool aspectMappedTexture = true );
/**
- * Create a mesh data with the geometry for the final composition
+ * Create a actors and renderers for the metaballs
*/
- Geometry CreateGeometryComposition();
-
- /**
- * Create a mesh actor for the metaballs
- */
- void CreateMetaballActors();
+ void CreateMetaballActors();
/**
* Create the render task and FBO to render the metaballs into a texture
*/
- void CreateMetaballImage();
+ void CreateMetaballImage();
/**
- * Create a mesh image to render the final composition
+ * Create the the final composition
*/
- void AddRefractionImage();
+ void CreateComposition();
/**
* Function to create animations for the small variations of position inside the metaball
*/
- void CreateAnimations();
+ void CreateAnimations();
/**
* Function to reset metaball state
*/
- void ResetMetaballs(bool resetAnims);
+ void ResetMetaballs( bool resetAnims );
/**
* Function to create disperse each of the ball that compose the metaball when exploding
*/
- void DisperseBallAnimation(int ball);
+ void DisperseBallAnimation( uint32_t ball );
/**
* Function to make metaballs come back to reset position
*/
- void LaunchResetMetaballPosition(Animation &source);
+ void LaunchResetMetaballPosition( Animation& source );
/**
* Function to set things at the end of the animation
*/
- void EndDisperseAnimation(Animation &source);
+ void EndDisperseAnimation( Animation& source );
/**
* Function to init dispersion of the metaballs one by one using a timer
* (so not all the balls begin moving at the same time)
*/
- bool OnTimerDispersionTick();
+ bool OnTimerDispersionTick();
/**
* Function to set the actual position of the metaballs when the user clicks the screen
*/
- void SetPositionToMetaballs(Vector2 & metaballCenter);
+ void SetPositionToMetaballs( const Vector2& metaballCenter );
};
-
-//-----------------------------------------------------------------------------------------------
-//
-// IMPLEMENTATION
-//
-//----------------
+/**
+ * Implementation
+ */
MetaballExplosionController::MetaballExplosionController( Application& application )
- : mApplication( application )
+: mApplication( application ),
+ mScreenSize(),
+ mBackgroundTexture(),
+ mMetaballFBO(),
+ mMetaballRoot(),
+ mMetaballs(),
+ mPositionIndex(),
+ mCompositionActor(),
+ mCurrentTouchPosition(),
+ mMetaballPosVariation(),
+ mMetaballPosVariationFrom(),
+ mMetaballPosVariationTo(),
+ mMetaballCenter(),
+ mPositionVarAnimation(),
+ mDispersion( 0 ),
+ mDispersionAnimation(),
+ mTimerDispersion(),
+ mTimeMultiplier( 1.0f )
{
// Connect to the Application's Init signal
mApplication.InitSignal().Connect( this, &MetaballExplosionController::Create );
void MetaballExplosionController::Create( Application& app )
{
- Stage stage = Stage::GetCurrent();
+ Window window = app.GetWindow();
- stage.KeyEventSignal().Connect(this, &MetaballExplosionController::OnKeyEvent);
+ window.KeyEventSignal().Connect( this, &MetaballExplosionController::OnKeyEvent );
- mScreenSize = stage.GetSize();
+ mScreenSize = window.GetSize();
- mTimeMult = 1.0f;
+ mTimeMultiplier = 1.0f;
- stage.SetBackgroundColor(Color::BLACK);
+ window.SetBackgroundColor(Color::BLACK);
- //Set background image for the view
- mBackImage = ResourceImage::New( BACKGROUND_IMAGE );
+ // Load background texture
+ mBackgroundTexture = DemoHelper::LoadTexture( BACKGROUND_IMAGE );
- srand((unsigned)time(0));
+ srand( static_cast<uint32_t>( time(0) ) );
//Create internal data
CreateMetaballActors();
CreateMetaballImage();
- AddRefractionImage();
+ CreateComposition();
CreateAnimations();
mDispersion = 0;
mTimerDispersion = Timer::New( 150 );
- mTimerDispersion.TickSignal().Connect(this, &MetaballExplosionController::OnTimerDispersionTick);
+ mTimerDispersion.TickSignal().Connect( this, &MetaballExplosionController::OnTimerDispersionTick );
// Connect the callback to the touch signal on the mesh actor
- stage.GetRootLayer().TouchedSignal().Connect( this, &MetaballExplosionController::OnTouch );
+ window.GetRootLayer().TouchSignal().Connect( this, &MetaballExplosionController::OnTouch );
}
-Geometry MetaballExplosionController::CreateGeometry()
+Geometry MetaballExplosionController::CreateGeometry( bool aspectMappedTexture )
{
- float aspect = (float)mScreenSize.y / (float)mScreenSize.x;
+ const float aspect = mScreenSize.y / mScreenSize.x;
// Create vertices and specify their color
- float xsize = mScreenSize.x * 0.5;
+ const float xsize = mScreenSize.x * 0.5;
- //We create the meshdata for the metaballs
+ // Create the meshdata for the metaballs
struct VertexPosition { Vector2 position; };
struct VertexTexture { Vector2 texture; };
- struct VertexNormal { Vector3 normal; };
-
- VertexPosition vertices[] = {
- { Vector2( -xsize, -xsize * aspect) },
- { Vector2( xsize, -xsize * aspect) },
- { Vector2( -xsize, xsize * aspect) },
- { Vector2( xsize, xsize * aspect) }
- };
-
- VertexTexture textures[] = {
- { Vector2(0.0f, 0.0f) },
- { Vector2(1.0f, 0.0f) },
- { Vector2(0.0f, 1.0f * aspect) },
- { Vector2(1.0f, 1.0f * aspect) }
- };
- int indices[] = { 0, 3, 1, 0, 2, 3 };
-
- unsigned int numberOfVertices = sizeof(vertices)/sizeof(VertexPosition);
-
- //Vertices
- Property::Map positionVertexFormat;
- positionVertexFormat["aPosition"] = Property::VECTOR2;
- PropertyBuffer positionVertices = PropertyBuffer::New( positionVertexFormat );
- positionVertices.SetData( vertices, numberOfVertices );
-
- //Textures
- Property::Map textureVertexFormat;
- textureVertexFormat["aTexture"] = Property::VECTOR2;
- PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat );
- textureVertices.SetData( textures, numberOfVertices );
-
- //Indices
- Property::Map indicesVertexFormat;
- indicesVertexFormat["aIndices"] = Property::INTEGER;
- PropertyBuffer indicesToVertices = PropertyBuffer::New( indicesVertexFormat );
- indicesToVertices.SetData( indices, 6 );
-
- // Create the geometry object
- Geometry texturedQuadGeometry = Geometry::New();
- texturedQuadGeometry.AddVertexBuffer( positionVertices );
- texturedQuadGeometry.AddVertexBuffer( textureVertices );
-
- texturedQuadGeometry.SetIndexBuffer ( indicesToVertices );
-
- return texturedQuadGeometry;
-}
-
-Geometry MetaballExplosionController::CreateGeometryComposition()
-{
- float aspect = (float)mScreenSize.y / (float)mScreenSize.x;
-
- // Create vertices and specify their color
- float xsize = mScreenSize.x * 0.5;
-
- //We create the meshdata for the metaballs
- struct VertexPosition { Vector2 position; };
- struct VertexTexture { Vector2 texture; };
- struct VertexNormal { Vector3 normal; };
-
- VertexPosition vertices[] = {
- { Vector2( -xsize, -xsize * aspect) },
- { Vector2( xsize, -xsize * aspect) },
- { Vector2( -xsize, xsize * aspect) },
- { Vector2( xsize, xsize * aspect) }
+ VertexPosition vertices[] =
+ {
+ { Vector2( -xsize, -xsize * aspect ) },
+ { Vector2( xsize, -xsize * aspect ) },
+ { Vector2( -xsize, xsize * aspect ) },
+ { Vector2( xsize, xsize * aspect ) }
};
- VertexTexture textures[] = {
- { Vector2(0.0f, 0.0f) },
- { Vector2(1.0f, 0.0f) },
- { Vector2(0.0f, 1.0f) },
- { Vector2(1.0f, 1.0f) }
+ const float textureAspect = (aspectMappedTexture) ? aspect : 1.0f;
+ VertexTexture textures[] =
+ {
+ { Vector2( 0.0f, 0.0f ) },
+ { Vector2( 1.0f, 0.0f ) },
+ { Vector2( 0.0f, 1.0f * textureAspect ) },
+ { Vector2( 1.0f, 1.0f * textureAspect ) }
};
- int indices[] = { 0, 3, 1, 0, 2, 3 };
-
- unsigned int numberOfVertices = sizeof(vertices)/sizeof(VertexPosition);
+ uint32_t numberOfVertices = sizeof(vertices)/sizeof(VertexPosition);
- //Vertices
+ // Vertices
Property::Map positionVertexFormat;
positionVertexFormat["aPosition"] = Property::VECTOR2;
PropertyBuffer positionVertices = PropertyBuffer::New( positionVertexFormat );
positionVertices.SetData( vertices, numberOfVertices );
- //Textures
+ // Textures
Property::Map textureVertexFormat;
textureVertexFormat["aTexture"] = Property::VECTOR2;
PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat );
textureVertices.SetData( textures, numberOfVertices );
- //Indices
- Property::Map indicesVertexFormat;
- indicesVertexFormat["aIndices"] = Property::INTEGER;
- PropertyBuffer indicesToVertices = PropertyBuffer::New( indicesVertexFormat );
- indicesToVertices.SetData( indices, 6 );
+ // Indices
+ const uint16_t indices[] = { 0, 3, 1, 0, 2, 3 };
// Create the geometry object
Geometry texturedQuadGeometry = Geometry::New();
texturedQuadGeometry.AddVertexBuffer( positionVertices );
texturedQuadGeometry.AddVertexBuffer( textureVertices );
- texturedQuadGeometry.SetIndexBuffer ( indicesToVertices );
+ texturedQuadGeometry.SetIndexBuffer ( &indices[0], sizeof( indices )/ sizeof( indices[0] ) );
return texturedQuadGeometry;
}
-float randomNumber(float lowest, float highest)
-{
- float range=(highest-lowest);
- return lowest+range*rand()/RAND_MAX;
-}
-
void MetaballExplosionController::CreateMetaballActors()
{
- //Create the shader for the metaballs
- Shader shader = Shader::New( METABALL_VERTEX_SHADER, METABALL_FRAG_SHADER );
+ // Create the shader for the metaballs, tell DALi that shader modifies geometry so we dont need to set a meaningless size
+ Shader shader = Shader::New( METABALL_VERTEX_SHADER, METABALL_FRAG_SHADER, Shader::Hint::MODIFIES_GEOMETRY );
Geometry metaballGeom = CreateGeometry();
+ // Reuse same renderer for each actor
Renderer renderer = Renderer::New( metaballGeom, shader );
- renderer.SetProperty( Renderer::Property::BLENDING_MODE, BlendingMode::ON );
- renderer.SetBlendFunc(BlendingFactor::ONE, BlendingFactor::ONE, BlendingFactor::ONE, BlendingFactor::ONE);
+ renderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
+ renderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_RGB, BlendFactor::ONE );
+ renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ONE );
+ renderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_ALPHA, BlendFactor::ONE );
+ renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_ALPHA, BlendFactor::ONE );
+
//Initialization of each of the metaballs
- for( int i = 0; i < METABALL_NUMBER; i++ )
+ for( uint32_t i = 0; i < METABALL_NUMBER; i++ )
{
mMetaballs[i].position = Vector2(0.0f, 0.0f);
- mMetaballs[i].radius = mMetaballs[i].initRadius = randomNumber(0.05f,0.07f);
+ mMetaballs[i].radius = mMetaballs[i].initRadius = Random::Range(0.05f,0.07f);
mMetaballs[i].actor = Actor::New( );
- mMetaballs[i].actor.SetName("Metaball");
- mMetaballs[i].actor.SetScale( 1.0f );
- mMetaballs[i].actor.SetParentOrigin( ParentOrigin::CENTER );
+ mMetaballs[i].actor.SetProperty( Dali::Actor::Property::NAME, "Metaball" );
+ mMetaballs[i].actor.SetProperty( Actor::Property::SCALE, 1.0f );
+ mMetaballs[i].actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
mMetaballs[i].actor.AddRenderer( renderer );
mMetaballs[i].positionIndex = mMetaballs[i].actor.RegisterProperty( "uPositionMetaball", mMetaballs[i].position );
mMetaballs[i].positionVarIndex = mMetaballs[i].actor.RegisterProperty( "uPositionVar", Vector2(0.f,0.f) );
- mMetaballs[i].actor.RegisterProperty( "uGravityVector", Vector2(randomNumber(-0.2,0.2),randomNumber(-0.2,0.2)) );
-
+ mMetaballs[i].actor.RegisterProperty( "uGravityVector", Vector2(Random::Range(-0.2,0.2),Random::Range(-0.2,0.2)) );
mMetaballs[i].actor.RegisterProperty( "uRadius", mMetaballs[i].radius );
-
mMetaballs[i].actor.RegisterProperty( "uRadiusVar", 0.f );
-
- mMetaballs[i].actor.SetSize(400, 400);
}
- //Root creation
+ // Root creation
mMetaballRoot = Actor::New();
- mMetaballRoot.SetParentOrigin( ParentOrigin::CENTER );
- for( int i = 0; i < METABALL_NUMBER; i++ )
+ mMetaballRoot.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
+ for( uint32_t i = 0; i < METABALL_NUMBER; i++ )
{
mMetaballRoot.Add( mMetaballs[i].actor );
}
- //Initialization of variables related to metaballs
- mMetaballPosVariation = Vector2(0,0);
- mMetaballPosVariationFrom = Vector2(0,0);
- mMetaballPosVariationTo = Vector2(0,0);
- mCurrentTouchPosition = Vector2(0,0);
}
void MetaballExplosionController::CreateMetaballImage()
{
- //We create an FBO and a render task to create to render the metaballs with a fragment shader
- Stage stage = Stage::GetCurrent();
- mMetaballFBO = FrameBufferImage::New(mScreenSize.x, mScreenSize.y, Pixel::RGBA8888, RenderBuffer::COLOR_DEPTH);
+ // Create an FBO and a render task to create to render the metaballs with a fragment shader
+ Window window = mApplication.GetWindow();
+ mMetaballFBO = FrameBuffer::New( mScreenSize.x, mScreenSize.y );
- stage.Add(mMetaballRoot);
+ window.Add(mMetaballRoot);
- //Creation of the render task used to render the metaballs
- RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
+ // Create the render task used to render the metaballs
+ RenderTaskList taskList = window.GetRenderTaskList();
RenderTask task = taskList.CreateTask();
task.SetRefreshRate( RenderTask::REFRESH_ALWAYS );
task.SetSourceActor( mMetaballRoot );
- task.SetExclusive(true);
+ task.SetExclusive( true );
task.SetClearColor( Color::BLACK );
task.SetClearEnabled( true );
- task.SetTargetFrameBuffer( mMetaballFBO );
+ task.SetFrameBuffer( mMetaballFBO );
}
-void MetaballExplosionController::AddRefractionImage()
+void MetaballExplosionController::CreateComposition()
{
- //Create Gaussian blur for the rendered image
- FrameBufferImage fbo;
- fbo = FrameBufferImage::New( mScreenSize.x, mScreenSize.y, Pixel::RGBA8888, RenderBuffer::COLOR_DEPTH);
-
- GaussianBlurView gbv = GaussianBlurView::New(5, 2.0f, Pixel::RGBA8888, 0.5f, 0.5f, true);
- gbv.SetBackgroundColor(Color::TRANSPARENT);
- gbv.SetUserImageAndOutputRenderTarget( mMetaballFBO, fbo );
- gbv.SetSize(mScreenSize.x, mScreenSize.y);
- Stage::GetCurrent().Add(gbv);
- gbv.Activate();
-
//Create new shader
Shader shader = Shader::New( METABALL_VERTEX_SHADER, REFRACTION_FRAG_SHADER );
- //Create new texture set
- TextureSet textureSet = TextureSet::New();
- textureSet.SetImage( 0u, mBackImage );
- textureSet.SetImage( 1u, fbo );
+ // Create new texture set
+ auto textureSet = TextureSet::New();
+ textureSet.SetTexture( 0u, mBackgroundTexture );
+ textureSet.SetTexture( 1u, mMetaballFBO.GetColorTexture() );
- //Create geometry
- Geometry metaballGeom = CreateGeometryComposition();
+ // Create geometry
+ Geometry metaballGeom = CreateGeometry( false );
Renderer mRenderer = Renderer::New( metaballGeom, shader );
mRenderer.SetTextures( textureSet );
+ // Create actor
mCompositionActor = Actor::New( );
- mCompositionActor.SetParentOrigin(ParentOrigin::CENTER);
- mCompositionActor.SetPosition(Vector3(0.0f, 0.0f, 0.0f));
- mCompositionActor.SetSize(mScreenSize.x, mScreenSize.y);
-
+ mCompositionActor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
+ mCompositionActor.SetProperty( Actor::Property::POSITION, Vector3(0.0f, 0.0f, 0.0f));
+ mCompositionActor.SetProperty( Actor::Property::SIZE, Vector2(mScreenSize.x, mScreenSize.y) );
mCompositionActor.AddRenderer( mRenderer );
Vector2 metaballCenter(0.0,0);
metaballCenter.x = metaballCenter.x * 0.5;
metaballCenter.y = metaballCenter.y * 0.5;
-
mPositionIndex = mCompositionActor.RegisterProperty( "uPositionMetaball", metaballCenter );
- SetPositionToMetaballs(metaballCenter);
+ SetPositionToMetaballs( metaballCenter );
- mCompositionActor.SetSize(mScreenSize.x, mScreenSize.y);
+ mCompositionActor.SetProperty( Actor::Property::SIZE, Vector2(mScreenSize.x, mScreenSize.y) );
- Stage stage = Stage::GetCurrent();
- stage.Add( mCompositionActor );
+ Window window = mApplication.GetWindow();
+ window.Add( mCompositionActor );
}
void MetaballExplosionController::CreateAnimations()
{
Vector2 direction;
- for( int i = 0; i < METABALL_NUMBER; i++ )
+ for( uint32_t i = 0; i < METABALL_NUMBER; i++ )
{
- float key;
KeyFrames keySinCosVariation = KeyFrames::New();
- Vector2 sinCosVariation(0,0);
+ Vector2 sinCosVariation( 0,0 );
- direction.x = randomNumber(-100.f,100.f);
- direction.y = randomNumber(-100.f,100.f);
+ direction.x = Random::Range( -100.f,100.f );
+ direction.y = Random::Range( -100.f,100.f );
direction.Normalize();
direction *= 0.1f;
- for( int j = 0; j < 360; j++ )
+ for( uint32_t j = 0; j < 360; j++ )
{
- sinCosVariation.x = sin(j * Math::PI/180.f) * direction.x;
- sinCosVariation.y = cos(j * Math::PI/180.f) * direction.y;
- key = j/360.f;
- keySinCosVariation.Add(key, sinCosVariation);
+ sinCosVariation.x = sinf( j * Math::PI/180.f ) * direction.x;
+ sinCosVariation.y = cosf( j * Math::PI/180.f ) * direction.y;
+ float key = j/360.f;
+ keySinCosVariation.Add( key, sinCosVariation );
}
- mPositionVarAnimation[i] = Animation::New(3.f);
- mPositionVarAnimation[i].AnimateBetween(Property( mMetaballs[i].actor, mMetaballs[i].positionVarIndex ), keySinCosVariation);
+ mPositionVarAnimation[i] = Animation::New( 3.f );
+ mPositionVarAnimation[i].AnimateBetween( Property( mMetaballs[i].actor, mMetaballs[i].positionVarIndex ), keySinCosVariation );
mPositionVarAnimation[i].SetLooping( true );
mPositionVarAnimation[i].Play();
}
}
-void MetaballExplosionController::ResetMetaballs(bool resetAnims)
+void MetaballExplosionController::ResetMetaballs( bool resetAnims )
{
- for( int i = 0; i < METABALL_NUMBER; i++ )
+ for( uint32_t i = 0; i < METABALL_NUMBER; i++ )
{
- if (mDispersionAnimation[i])
+ if( mDispersionAnimation[i] )
+ {
mDispersionAnimation[i].Clear();
+ }
- mMetaballs[i].position = Vector2(0.0f, 0.0f);
- mMetaballs[i].actor.SetProperty(mMetaballs[i].positionIndex, mMetaballs[i].position);
+ mMetaballs[i].position = Vector2( 0.0f, 0.0f );
+ mMetaballs[i].actor.SetProperty( mMetaballs[i].positionIndex, mMetaballs[i].position );
}
mTimerDispersion.Stop();
mDispersion = 0;
mCompositionActor.SetProperty( mPositionIndex, Vector2(0,0) );
}
-void MetaballExplosionController::DisperseBallAnimation(int ball)
+void MetaballExplosionController::DisperseBallAnimation( uint32_t ball )
{
Vector2 position;
- position.x = randomNumber(-1.5f,1.5f);
- position.y = randomNumber(-1.5f,1.5f);
+ position.x = Random::Range(-1.5f,1.5f);
+ position.y = Random::Range(-1.5f,1.5f);
- mDispersionAnimation[ball] = Animation::New(2.0f * mTimeMult);
+ mDispersionAnimation[ball] = Animation::New(2.0f * mTimeMultiplier);
mDispersionAnimation[ball].AnimateTo( Property(mMetaballs[ball].actor, mMetaballs[ball].positionIndex), position);
mDispersionAnimation[ball].Play();
if( ball == METABALL_NUMBER - 1 )
+ {
mDispersionAnimation[ball].FinishedSignal().Connect( this, &MetaballExplosionController::LaunchResetMetaballPosition );
+ }
}
-void MetaballExplosionController::LaunchResetMetaballPosition(Animation &source)
+void MetaballExplosionController::LaunchResetMetaballPosition( Animation& source )
{
- for( int i = 0; i < METABALL_NUMBER; i++ )
+ for( uint32_t i = 0; i < METABALL_NUMBER; i++ )
{
- mDispersionAnimation[i] = Animation::New(1.5f + i*0.25f*mTimeMult);
- mDispersionAnimation[i].AnimateTo(Property(mMetaballs[i].actor, mMetaballs[i].positionIndex), Vector2(0,0));
+ mDispersionAnimation[i] = Animation::New( 1.5f + i * 0.25f * mTimeMultiplier );
+ mDispersionAnimation[i].AnimateTo(Property( mMetaballs[i].actor, mMetaballs[i].positionIndex), Vector2(0,0) );
mDispersionAnimation[i].Play();
if( i == METABALL_NUMBER - 1 )
+ {
mDispersionAnimation[i].FinishedSignal().Connect( this, &MetaballExplosionController::EndDisperseAnimation );
+ }
}
}
-void MetaballExplosionController::EndDisperseAnimation(Animation &source)
+void MetaballExplosionController::EndDisperseAnimation( Animation& source )
{
mCompositionActor.SetProperty( mPositionIndex, Vector2(0,0) );
}
{
if( mDispersion < METABALL_NUMBER )
{
- DisperseBallAnimation(mDispersion);
+ DisperseBallAnimation( mDispersion );
mDispersion++;
}
return true;
}
-void MetaballExplosionController::SetPositionToMetaballs(Vector2 & metaballCenter)
+void MetaballExplosionController::SetPositionToMetaballs( const Vector2& metaballCenter )
{
//We set the position for the metaballs based on click position
- for( int i = 0; i < METABALL_NUMBER; i++ )
+ for( uint32_t i = 0; i < METABALL_NUMBER; i++ )
{
mMetaballs[i].position = metaballCenter;
- mMetaballs[i].actor.SetProperty(mMetaballs[i].positionIndex, mMetaballs[i].position);
+ mMetaballs[i].actor.SetProperty( mMetaballs[i].positionIndex, mMetaballs[i].position );
}
mCompositionActor.SetProperty( mPositionIndex, metaballCenter );
}
-bool MetaballExplosionController::OnTouch( Actor actor, const TouchEvent& touch )
+bool MetaballExplosionController::OnTouch( Actor actor, const TouchData& touch )
{
- const TouchPoint &point = touch.GetPoint(0);
float aspectR = mScreenSize.y / mScreenSize.x;
- switch( point.state )
+ switch( touch.GetState( 0 ) )
{
- case TouchPoint::Down:
+ case PointState::DOWN:
{
ResetMetaballs(true);
- Vector2 metaballCenter = Vector2((point.screen.x / mScreenSize.x) - 0.5, (aspectR * (mScreenSize.y - point.screen.y) / mScreenSize.y) - 0.5) * 2.0;
+ const Vector2 screen = touch.GetScreenPosition( 0 );
+ Vector2 metaballCenter = Vector2( (screen.x / mScreenSize.x) - 0.5f, (aspectR * (mScreenSize.y - screen.y) / mScreenSize.y) - 0.5f ) * 2.0f;
SetPositionToMetaballs(metaballCenter);
break;
}
- case TouchPoint::Motion:
+ case PointState::MOTION:
{
- Vector2 metaballCenter = Vector2((point.screen.x / mScreenSize.x) - 0.5, (aspectR * (mScreenSize.y - point.screen.y) / mScreenSize.y) - 0.5) * 2.0;
+ const Vector2 screen = touch.GetScreenPosition( 0 );
+ Vector2 metaballCenter = Vector2( (screen.x / mScreenSize.x) - 0.5f, (aspectR * (mScreenSize.y - screen.y) / mScreenSize.y) - 0.5f ) * 2.0f;
SetPositionToMetaballs(metaballCenter);
break;
}
- case TouchPoint::Up:
- case TouchPoint::Leave:
- case TouchPoint::Interrupted:
+ case PointState::UP:
+ case PointState::LEAVE:
+ case PointState::INTERRUPTED:
{
mTimerDispersion.Start();
break;
}
}
-
-//-----------------------------------------------------------------------------------------------
-//
-// Main functions
-//
-//-----------------------------------------------------------------------------------------------
-
-void RunTest( Application& application )
+/**
+ * Main entry point
+ */
+int32_t DALI_EXPORT_API main( int argc, char **argv )
{
+ Application application = Application::New( &argc, &argv );
+
MetaballExplosionController test( application );
application.MainLoop();
-}
-
-// Entry point for Linux & Tizen applications
-//
-int DALI_EXPORT_API main( int argc, char **argv )
-{
- Application application = Application::New( &argc, &argv );
-
- RunTest( application );
return 0;
}