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.
24 #include <dali/dali.h>
25 #include <dali-toolkit/dali-toolkit.h>
26 #include "shared/view.h"
29 using namespace Dali::Toolkit;
30 using namespace DemoHelper;
34 const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-blocks.jpg" );
35 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
36 const char* APPLICATION_TITLE( "DALi Blocks" );
37 const char* BALL_IMAGE = DALI_IMAGE_DIR "blocks-ball.png";
38 const char* PADDLE_IMAGE = DALI_IMAGE_DIR "blocks-paddle.png";
39 const char* PADDLE_HANDLE_IMAGE = DALI_IMAGE_DIR "blocks-paddle-handle.png";
41 const char* BRICK_IMAGE_PATH[] = { DALI_IMAGE_DIR "blocks-brick-1.png",
42 DALI_IMAGE_DIR "blocks-brick-2.png",
43 DALI_IMAGE_DIR "blocks-brick-3.png",
44 DALI_IMAGE_DIR "blocks-brick-4.png" };
46 const int TOTAL_BRICKS(4); ///< Total bricks in game.
47 const Vector3 ICON_SIZE(100.0f, 100.0f, 0.0f);
49 const float SCREEN_MARGIN = 10.0f; ///< Margin indentation around screen
50 const Vector3 MENU_BUTTON_SIZE = Vector3(0.15f, 0.05f, 1.0f); ///< Standard Menu Buttons.
52 const float MAX_ANIMATION_DURATION = 60.0f; ///< 60 seconds animations. Long enough for ball to hit an obstacle.
53 const float BALL_VELOCITY = 300.0f; ///< Ball velocity in pixels/second.
54 const float MAX_VELOCITY = 500.0f; ///< Max. velocity in pixels/second.
55 const Vector3 PADDLE_COLLISION_MARGIN(0.0f, 0.0f, 0.0f); ///< Collision margin for ball-paddle detection.
56 const Vector3 BRICK_COLLISION_MARGIN(0.0f, 0.0f, 0.0f); ///< Collision margin for ball-brick detection.
57 const Vector3 INITIAL_BALL_DIRECTION(1.0f, 1.0f, 0.0f); ///< Initial ball direction.
59 const std::string WOBBLE_PROPERTY_NAME("wobble-property"); ///< Wobble property name.
60 const std::string COLLISION_PROPERTY_NAME("collision-property"); ///< Collision property name.
62 const Vector2 BRICK_SIZE(0.1f, 0.05f ); ///< Brick size relative to width of stage.
63 const Vector2 BALL_SIZE( 0.05f, 0.05f ); ///< Ball size relative to width of stage.
64 const Vector2 PADDLE_SIZE( 0.2f, 0.05f ); ///< Paddle size relative to width of stage.
65 const Vector2 PADDLE_HANDLE_SIZE( 0.3f, 0.3f ); ///< Paddle handle size relative to width of stage.
66 const Vector2 BALL_START_POSITION(0.5f, 0.8f); ///< Ball start position relative to stage size.
67 const Vector2 PADDLE_START_POSITION(0.5f, 0.9f); ///< Paddler start position relative to stage size.
68 const Vector2 PADDLE_HIT_MARGIN( 0.1, 0.15f ); ///< Extra hit Area for Paddle when touching.
70 const int TOTAL_LIVES(3); ///< Total lives in game before it's game over!
71 const int TOTAL_LEVELS(3); ///< 3 Levels total, then repeats.
73 // constraints ////////////////////////////////////////////////////////////////
76 * CollisionConstraint generates a collision vector
77 * between two actors a and b, assuming they're rectangular
78 * based on their size.
80 struct CollisionConstraint
83 * Collision Constraint constructor
84 * The adjust (optional) parameter can be used to add a margin
85 * to the actors. A +ve size will result in larger collisions,
86 * while a -ve size will result in tighter collisions.
88 * @param[in] adjust (optional) Adjusts the rectangular size detection
90 CollisionConstraint(Vector3 adjust = Vector3::ZERO)
96 * Generates collision vector indicating whether Actor's A and B
97 * have overlapped eachother, and the relative position of Actor B to A.
99 * @param[in] current The current collision-property (ignored)
100 * @param[in] propertyA Actor A's Position property.
101 * @param[in] propertyB Actor B's Position property.
102 * @param[in] propertySizeA Actor A's Size property.
103 * @param[in] propertySizeB Actor B's Size property.
104 * @return The collision vector is returned.
106 Vector3 operator()(const Vector3& current,
107 const PropertyInput& propertyA,
108 const PropertyInput& propertyB,
109 const PropertyInput& propertySizeA,
110 const PropertyInput& propertySizeB)
112 const Vector3& a = propertyA.GetVector3();
113 const Vector3& b = propertyB.GetVector3();
114 const Vector3& sizeA = propertySizeA.GetVector3();
115 const Vector3& sizeB = propertySizeB.GetVector3();
116 const Vector3 sizeComb = (sizeA + sizeB + mAdjust) * 0.5f;
118 // get collision relative to a.
119 Vector3 delta = b - a;
121 // Check if not overlapping Actors.
122 if( (fabsf(delta.x) > sizeComb.width) ||
123 (fabsf(delta.y) > sizeComb.height) )
125 delta = Vector3::ZERO; // not overlapping
128 return delta; // overlapping, return overlap vector relative to actor a.
131 const Vector3 mAdjust; ///< Size Adjustment value
135 * CollisionCircleRectangleConstraint generates a collision vector
136 * between two actors a (circle) and b (rectangle)
138 struct CollisionCircleRectangleConstraint
141 * Collision Constraint constructor
142 * The adjust (optional) parameter can be used to add a margin
143 * to the actors. A +ve size will result in larger collisions,
144 * while a -ve size will result in tighter collisions.
146 * @param[in] adjustPosition (optional) Adjusts the position offset of detection
147 * @param[in] adjustSize (optional) Adjusts the rectangular size of detection
149 CollisionCircleRectangleConstraint(Vector3 adjustPosition = Vector3::ZERO,
150 Vector3 adjustSize = Vector3::ZERO)
151 : mAdjustPosition(adjustPosition),
152 mAdjustSize(adjustSize)
157 * Generates collision vector indicating whether Actor's A and B
158 * have overlapped eachother, and the relative position of Actor B to A.
160 * @param[in] current The current collision-property (ignored)
161 * @param[in] propertyA Actor A's Position property.
162 * @param[in] propertyB Actor B's Position property.
163 * @param[in] propertySizeA Actor A's Size property.
164 * @param[in] propertySizeB Actor B's Size property.
165 * @return The collision vector is returned.
167 Vector3 operator()(const Vector3& current,
168 const PropertyInput& propertyA,
169 const PropertyInput& propertyB,
170 const PropertyInput& propertySizeA,
171 const PropertyInput& propertySizeB)
173 const Vector3& a = propertyA.GetVector3();
174 const Vector3 b = propertyB.GetVector3() + mAdjustPosition;
175 const Vector3& sizeA = propertySizeA.GetVector3();
176 const Vector3& sizeB = propertySizeB.GetVector3();
177 const Vector3 sizeA2 = sizeA * 0.5f; // circle radius
178 const Vector3 sizeB2 = (sizeB + mAdjustSize) * 0.5f; // rectangle half rectangle.
180 // get collision relative to a (rectangle).
181 Vector3 delta = a - b;
183 // reduce rectangle to 0.
184 if (delta.x > sizeB2.x)
188 else if (delta.x < -sizeB2.x)
197 if (delta.y > sizeB2.y)
201 else if (delta.y < -sizeB2.y)
210 // now calculate collision vector vs origin. (assume A is a circle, not ellipse)
211 if(delta.Length() < sizeA2.x)
217 return Vector3::ZERO;
220 const Vector3 mAdjustPosition; ///< Position Adjustment value
221 const Vector3 mAdjustSize; ///< Size Adjustment value
225 * WobbleConstraint generates a decaying sinusoidial rotation.
226 * The result when applied to an Actor, is the Actor rotating left/right
227 * initially a large amount (deviation degrees, when wobble property is 0.0f)
228 * then eventually coming to a stop (once wobble property reaches 1.0f)
230 struct WobbleConstraint
233 * Wobble Constraint constructor
234 * Generates a sinusoidial rotation that starts with
235 * high amplitude (deviation), and then decays to zero over input 0.0f to 1.0f
237 * @param[in] deviation The max. deviation of wobble effect in degrees.
239 WobbleConstraint(float deviation)
240 : mDeviation(Radian(Degree(deviation)))
246 * @param[in] current The current rotation property (ignored)
247 * @param[in] propertyWobble The wobble property (value from 0.0f to 1.0f)
248 * @return The rotation (quaternion) is generated.
250 Quaternion operator()(const Quaternion& current,
251 const PropertyInput& propertyWobble)
253 const float& wobble = propertyWobble.GetFloat();
255 float f = sinf(wobble * 10.0f) * (1.0f-wobble);
257 Quaternion q(mDeviation * f, Vector3::ZAXIS);
262 const float mDeviation; ///< Deviation factor in radians.
265 } // unnamed namespace
268 * This example shows how to use PropertyNotifications
270 class ExampleController : public ConnectionTracker
276 * @param application Application class, stored as reference
278 ExampleController( Application& application )
279 : mApplication( application ),
282 // Connect to the Application's Init and orientation changed signal
283 mApplication.InitSignal().Connect(this, &ExampleController::Create);
287 * This method gets called once the main loop of application is up and running
288 * @param[in] application Reference to the application instance
290 void Create(Application& application)
292 DemoHelper::RequestThemeChange();
294 Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
296 // Creates a default view with a default tool bar.
297 // The view is added to the stage.
298 Toolkit::ToolBar toolBar;
299 mContentLayer = DemoHelper::CreateView( application,
306 // Add an extra space on the right to center the title text.
307 toolBar.AddControl( Actor::New(), DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight );
309 // Create the content layer, which is where game actors appear.
316 * Adds a new layer to the stage, containing game actors.
318 void AddContentLayer()
320 Stage stage = Stage::GetCurrent();
321 const Vector3 stageSize(stage.GetSize());
324 mBallStartPosition = stageSize * Vector3( BALL_START_POSITION );
325 mBall = CreateImage(BALL_IMAGE);
326 mBall.SetPosition( mBallStartPosition );
327 mBall.SetSize( BALL_SIZE * stageSize.width );
328 mContentLayer.Add(mBall);
329 mBallVelocity = Vector3::ZERO;
332 mPaddleHitMargin = Vector2(stageSize) * PADDLE_HIT_MARGIN;
333 mPaddle = Actor::New();
334 mPaddleHandle = CreateImage(PADDLE_HANDLE_IMAGE);
335 mPaddleImage = CreateImage(PADDLE_IMAGE);
336 mPaddle.Add( mPaddleHandle );
337 mPaddle.Add( mPaddleImage );
338 mPaddleHandle.SetParentOrigin( ParentOrigin::TOP_CENTER );
339 mPaddleHandle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
340 mPaddleHandle.SetPosition( 0.0f, stageSize.width * 0.0125f );
341 mPaddleImage.SetParentOrigin( ParentOrigin::TOP_CENTER );
342 mPaddleImage.SetAnchorPoint( AnchorPoint::TOP_CENTER );
343 mPaddle.SetParentOrigin( ParentOrigin::TOP_LEFT );
344 mPaddle.SetAnchorPoint( AnchorPoint::CENTER );
345 mPaddleFullSize = PADDLE_SIZE * stageSize.width;
346 mPaddle.SetSize( mPaddleFullSize + mPaddleHitMargin );
347 mPaddleHandle.SetSize( PADDLE_HANDLE_SIZE * stageSize.width );
348 mPaddleImage.SetSize( mPaddleFullSize );
350 mWobbleProperty = mPaddle.RegisterProperty(WOBBLE_PROPERTY_NAME, 0.0f);
351 Constraint wobbleConstraint = Constraint::New<Quaternion>( Actor::Property::ORIENTATION,
352 LocalSource(mWobbleProperty),
353 WobbleConstraint(10.0f));
354 mPaddle.ApplyConstraint(wobbleConstraint);
356 mPaddle.SetPosition( stageSize * Vector3( PADDLE_START_POSITION ) );
357 mContentLayer.Add(mPaddle);
358 mPaddle.TouchedSignal().Connect(this, &ExampleController::OnTouchPaddle);
359 mContentLayer.TouchedSignal().Connect(this, &ExampleController::OnTouchLayer);
361 const float margin(BALL_SIZE.width * stageSize.width * 0.5f);
363 // Set up notifications for ball's collisions against walls.
364 PropertyNotification leftNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_X, LessThanCondition(margin) );
365 leftNotification.NotifySignal().Connect( this, &ExampleController::OnHitLeftWall );
367 PropertyNotification rightNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_X, GreaterThanCondition(stageSize.width - margin) );
368 rightNotification.NotifySignal().Connect( this, &ExampleController::OnHitRightWall );
370 PropertyNotification topNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_Y, LessThanCondition(margin) );
371 topNotification.NotifySignal().Connect( this, &ExampleController::OnHitTopWall );
373 PropertyNotification bottomNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_Y, GreaterThanCondition(stageSize.height + margin) );
374 bottomNotification.NotifySignal().Connect( this, &ExampleController::OnHitBottomWall );
376 // Set up notification for ball colliding against paddle.
377 Actor delegate = Actor::New();
379 Property::Index property = delegate.RegisterProperty(COLLISION_PROPERTY_NAME, Vector3::ZERO);
380 Constraint constraint = Constraint::New<Vector3>( property,
381 Source(mBall, Actor::Property::POSITION),
382 Source(mPaddle, Actor::Property::POSITION),
383 Source(mBall, Actor::Property::SIZE),
384 Source(mPaddle, Actor::Property::SIZE),
385 CollisionCircleRectangleConstraint( -Vector3(0.0f, mPaddleHitMargin.height * 0.575f, 0.0f),-Vector3(mPaddleHitMargin) ));
386 delegate.ApplyConstraint(constraint);
388 PropertyNotification paddleNotification = delegate.AddPropertyNotification( property, GreaterThanCondition(0.0f) );
389 paddleNotification.NotifySignal().Connect( this, &ExampleController::OnHitPaddle );
396 * Resets Lives count and other stats, and loads level
400 mLives = TOTAL_LIVES;
402 mBall.SetPosition( mBallStartPosition );
403 mBallVelocity = Vector3::ZERO;
404 mPaddle.SetSize( mPaddleFullSize + mPaddleHitMargin );
405 mPaddleImage.SetSize( mPaddleFullSize );
412 * All existing level content is removed, and new bricks
414 * @param[in] level Level index to load.
416 void LoadLevel(int level)
418 if(mLevelContainer && mLevelContainer.GetParent() == mContentLayer)
420 mContentLayer.Remove( mLevelContainer );
423 mLevelContainer = Actor::New();
424 mLevelContainer.SetAnchorPoint( AnchorPoint::CENTER );
425 mLevelContainer.SetParentOrigin( ParentOrigin::CENTER );
426 mLevelContainer.SetRelayoutEnabled( true );
427 mLevelContainer.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
428 mContentLayer.Add( mLevelContainer );
432 switch(level%TOTAL_LEVELS)
459 void GenerateLevel0()
461 Vector2 stageSize(Stage::GetCurrent().GetSize());
462 const Vector2 brickSize(BRICK_SIZE * stageSize.width);
464 const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
465 const int rows = (0.3f * stageSize.height) / brickSize.height; // 30 percent of the height of the screen covered with bricks.
466 const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
467 stageSize.y * 0.125f );
469 for(int j = 0; j < rows; j++)
471 for(int i = 0; i < columns; i++)
473 Actor brick = CreateBrick(Vector2(i * brickSize.width + offset.x, j * brickSize.height + offset.y) + (brickSize * 0.5f), j % TOTAL_BRICKS );
474 mLevelContainer.Add(brick);
483 void GenerateLevel1()
485 Vector2 stageSize(Stage::GetCurrent().GetSize());
486 const Vector2 brickSize(BRICK_SIZE * stageSize.width);
488 const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
489 const int rows = (0.3f * stageSize.height) / brickSize.height; // 30 percent of the height of the screen covered with bricks.
490 const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
491 stageSize.y * 0.125f );
493 for(int j = 0; j < rows; j++)
495 for(int i = 0; i < columns; i++)
497 int i2 = columns - i - 1;
498 int j2 = rows - j - 1;
499 int brickIndex = std::min( std::min(i, j), std::min(i2, j2) ) % TOTAL_BRICKS;
501 Actor brick = CreateBrick(Vector2(i * brickSize.width + offset.x, j * brickSize.height + offset.y) + (brickSize * 0.5f), brickIndex );
503 mLevelContainer.Add(brick);
512 void GenerateLevel2()
514 Vector2 stageSize(Stage::GetCurrent().GetSize());
515 const Vector2 brickSize(BRICK_SIZE * stageSize.width);
517 const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
518 const int rows = (0.3f * stageSize.height) / brickSize.height; // 30 percent of the height of the screen covered with bricks.
519 const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
520 stageSize.y * 0.125f );
522 // lays down bricks in a spiral formation starting at i,j = (0,0) top left corner
523 // travelling right di,dj = (1,0) initially
529 // contracting boundaries
531 int right = columns - 1;
533 int bottom = rows - 1;
535 // length of current line. we stop laying down bricks when the length is 1 brick or less.
539 Actor brick = CreateBrick(Vector2(i * brickSize.width + offset.x, j * brickSize.height + offset.y) + (brickSize * 0.5f), 0 );
540 mLevelContainer.Add(brick);
544 if((i==right) && (di==1))
549 if((j==bottom) && (dj==1))
554 if((i==left) && (di==-1))
559 if((j==top) && (dj==-1))
566 // turn 90 degrees clockwise.
582 * Creates a brick at a specified position on the stage
583 * @param[in] position the position for the brick
584 * @param[in] type the type of brick
585 * @return The Brick Actor is returned.
587 Actor CreateBrick( const Vector2& position, int type )
589 Vector2 stageSize(Stage::GetCurrent().GetSize());
590 const Vector2 brickSize(BRICK_SIZE * Vector2(stageSize.x, stageSize.x));
592 ImageAttributes attr;
593 attr.SetSize( 128, 64 );
594 attr.SetScalingMode( ImageAttributes::ScaleToFill );
595 Image img = ResourceImage::New(BRICK_IMAGE_PATH[type], attr);
596 ImageActor brick = ImageActor::New(img);
597 brick.SetParentOrigin(ParentOrigin::TOP_LEFT);
598 brick.SetAnchorPoint(AnchorPoint::CENTER);
599 brick.SetRelayoutEnabled( false );
600 brick.SetSize( brickSize );
601 brick.SetPosition( Vector3( position ) );
603 // Add a constraint on the brick between it and the ball generating a collision-property
604 Property::Index property = brick.RegisterProperty(COLLISION_PROPERTY_NAME, Vector3::ZERO);
605 Constraint constraint = Constraint::New<Vector3>( property,
606 Source(mBall, Actor::Property::POSITION),
607 Source(brick, Actor::Property::POSITION),
608 Source(mBall, Actor::Property::SIZE),
609 Source(brick, Actor::Property::SIZE),
610 CollisionCircleRectangleConstraint(BRICK_COLLISION_MARGIN));
611 brick.ApplyConstraint(constraint);
613 // Now add a notification on this collision-property
615 PropertyNotification brickNotification = brick.AddPropertyNotification( property, GreaterThanCondition(0.0f) );
616 brickNotification.NotifySignal().Connect( this, &ExampleController::OnHitBrick );
622 * Creates an Image (Helper)
624 * @param[in] filename the path of the image.
626 ImageActor CreateImage(const std::string& filename)
628 Image img = ResourceImage::New(filename);
629 ImageActor actor = ImageActor::New(img);
630 actor.SetParentOrigin(ParentOrigin::TOP_LEFT);
631 actor.SetAnchorPoint(AnchorPoint::CENTER);
632 actor.SetRelayoutEnabled( false );
637 * Continue animation (based on current velocity)
639 void ContinueAnimation()
643 mBallAnimation.Clear();
646 mBallAnimation = Animation::New(MAX_ANIMATION_DURATION);
647 mBallAnimation.AnimateBy( Property( mBall, Actor::Property::POSITION ), mBallVelocity * MAX_ANIMATION_DURATION);
648 mBallAnimation.Play();
652 * Signal invoked whenever user touches the Paddle.
653 * @param[in] actor The actor touched
654 * @param[in] event The touch event
656 bool OnTouchPaddle(Actor actor, const TouchEvent& event)
658 if(event.GetPointCount()>0)
660 const TouchPoint& point = event.GetPoint(0);
661 if(point.state==TouchPoint::Down) // Commence dragging
663 // Get point where user touched paddle (relative to paddle's center)
664 mRelativeDragPoint = Vector3(point.screen.x, point.screen.y, 0.0f);
665 mRelativeDragPoint -= actor.GetCurrentPosition();
668 mDragAnimation = Animation::New(0.25f);
669 mDragAnimation.AnimateTo( Property(mDragActor, Actor::Property::SCALE), Vector3(1.1f, 1.1f, 1.0f), AlphaFunctions::EaseOut);
670 mDragAnimation.AnimateTo( Property(mPaddleHandle, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 0.0f), AlphaFunctions::EaseOut);
671 mDragAnimation.Play();
678 * Signal invoked whenever user touches anywhere on the screen.
679 * @param[in] actor The actor touched
680 * @param[in] event The touch event
682 bool OnTouchLayer(Actor actor, const TouchEvent& event)
684 if(event.GetPointCount()>0)
686 const TouchPoint& point = event.GetPoint(0);
689 Vector3 position(point.screen.x, point.screen.y, 0.0f);
690 mPaddle.SetPosition( position - mRelativeDragPoint );
692 if(point.state==TouchPoint::Up) // Stop dragging
694 mDragAnimation = Animation::New(0.25f);
695 mDragAnimation.AnimateTo( Property(mDragActor, Actor::Property::SCALE), Vector3(1.0f, 1.0f, 1.0f), AlphaFunctions::EaseIn);
696 mDragAnimation.AnimateTo( Property(mPaddleHandle, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 1.0f), AlphaFunctions::EaseOut);
697 mDragAnimation.Play();
706 * Notification: Ball hit left wall
707 * @param source The notification
709 void OnHitLeftWall(PropertyNotification& source)
711 mBallVelocity.x = fabsf(mBallVelocity.x);
716 * Notification: Ball hit right wall
717 * @param source The notification
719 void OnHitRightWall(PropertyNotification& source)
721 mBallVelocity.x = -fabsf(mBallVelocity.x);
726 * Notification: Ball hit top wall
727 * @param source The notification
729 void OnHitTopWall(PropertyNotification& source)
731 mBallVelocity.y = fabsf(mBallVelocity.y);
736 * Notification: Ball hit bottom wall
737 * @param source The notification
739 void OnHitBottomWall(PropertyNotification& source)
743 mBallAnimation.Clear();
749 const float f(static_cast<float>(mLives) / TOTAL_LIVES);
750 mBallVelocity = Vector3::ZERO;
752 Animation shrink = Animation::New(0.5f);
753 shrink.AnimateTo( Property(mPaddle, Actor::Property::SIZE_WIDTH), mPaddleFullSize.x * f + mPaddleHitMargin.x);
754 shrink.AnimateTo( Property(mPaddleImage, Actor::Property::SIZE_WIDTH), mPaddleFullSize.x * f );
756 shrink.FinishedSignal().Connect( this, &ExampleController::OnPaddleShrunk );
762 * Paddle Shrink Animation complete.
763 * @param[in] source The animation responsible for shrinking the paddle.
765 void OnPaddleShrunk( Animation &source )
767 // Reposition Ball in start position, and make ball appear.
768 mBall.SetPosition( mBallStartPosition );
769 mBall.SetColor( Vector4(1.0f, 1.0f, 1.0f, 0.1f) );
770 Animation appear = Animation::New(0.5f);
771 appear.AnimateTo( Property(mBall, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 1.0f) );
781 * Notification: Ball hit paddle
782 * @param source The notification
784 void OnHitPaddle(PropertyNotification& source)
786 Actor delegate = Actor::DownCast(source.GetTarget());
787 Vector3 collisionVector = delegate.GetProperty<Vector3>(source.GetTargetProperty());
788 Vector3 ballRelativePosition(mBall.GetCurrentPosition() - mPaddle.GetCurrentPosition());
789 ballRelativePosition.Normalize();
791 collisionVector.x += ballRelativePosition.x * 0.5f;
793 if(mBallVelocity.LengthSquared() < Math::MACHINE_EPSILON_1)
795 mBallVelocity += collisionVector * BALL_VELOCITY;
799 const float normalVelocity = fabsf(mBallVelocity.Dot(collisionVector));
800 mBallVelocity += collisionVector * normalVelocity * 2.0f;
801 const float currentSpeed = mBallVelocity.Length();
802 const float limitedSpeed = std::min( currentSpeed, MAX_VELOCITY );
803 mBallVelocity = mBallVelocity * limitedSpeed / currentSpeed;
809 mWobbleAnimation = Animation::New(0.5f);
810 mWobbleAnimation.AnimateTo( Property( mPaddle, mWobbleProperty ), 1.0f );
811 mWobbleAnimation.Play();
812 mPaddle.SetProperty(mWobbleProperty, 0.0f);
816 * Notification: Ball hit brick
817 * @param source The notification
819 void OnHitBrick(PropertyNotification& source)
821 Actor brick = Actor::DownCast(source.GetTarget());
822 Vector3 collisionVector = brick.GetProperty<Vector3>(source.GetTargetProperty());
824 const float normalVelocity = fabsf(mBallVelocity.Dot(collisionVector));
825 mBallVelocity += collisionVector * normalVelocity * 2.0f;
826 const float currentSpeed = mBallVelocity.Length();
827 const float limitedSpeed = std::min( currentSpeed, MAX_VELOCITY );
828 mBallVelocity = mBallVelocity * limitedSpeed / currentSpeed;
832 // remove collision-constraint and notification.
833 brick.RemovePropertyNotification(source);
834 brick.RemoveConstraints();
836 // fade brick (destroy)
837 Animation destroyAnimation = Animation::New(0.5f);
838 destroyAnimation.AnimateTo( Property( brick, Actor::Property::COLOR_ALPHA ), 0.0f, AlphaFunctions::EaseIn );
839 destroyAnimation.Play();
840 destroyAnimation.FinishedSignal().Connect( this, &ExampleController::OnBrickDestroyed );
841 mDestroyAnimationMap[destroyAnimation] = brick;
845 * Brick Destruction Animation complete.
846 * @param[in] source The animation responsible for destroying the brick
848 void OnBrickDestroyed( Animation& source )
850 // Remove brick from stage, it's constraint and property notification should also remove themselves.
851 Actor brick = mDestroyAnimationMap[source];
852 mDestroyAnimationMap.erase(source);
853 brick.GetParent().Remove(brick);
864 * Main key event handler
866 void OnKeyEvent(const KeyEvent& event)
868 if(event.state == KeyEvent::Down)
870 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
879 Application& mApplication; ///< Application instance
880 Toolkit::View mView; ///< The View instance.
881 Layer mContentLayer; ///< The content layer (contains game actors)
882 ImageActor mBall; ///< The Moving ball image.
883 Vector3 mBallStartPosition; ///< Ball Start position
884 Vector3 mBallVelocity; ///< Ball's current direction.
885 Animation mBallAnimation; ///< Ball's animation
886 Actor mPaddle; ///< The paddle including hit area.
887 ImageActor mPaddleImage; ///< The paddle's image.
888 ImageActor mPaddleHandle; ///< The paddle's handle (where the user touches)
889 Vector2 mPaddleHitMargin; ///< The paddle hit margin.
890 Animation mWobbleAnimation; ///< Paddle's animation when hit (wobbles)
891 Property::Index mWobbleProperty; ///< The wobble property (generated from animation)
892 Actor mLevelContainer; ///< The level container (contains bricks)
894 // actor - dragging functionality
896 Animation mDragAnimation; ///< Animation for dragging. (grows - affects ACTOR::SCALE)
897 Actor mDragActor; ///< The actor which is being dragged (if any)
898 Vector3 mRelativeDragPoint; ///< The point the user touched, relative to the actor.
899 std::map<Animation, Actor> mDestroyAnimationMap; ///< Keep track of which actors are to be destroyed.
900 Vector2 mPaddleFullSize; ///< Initial 100% size of the paddle.
901 int mLevel; ///< Current level
902 int mLives; ///< Total lives.
903 int mBrickCount; ///< Total bricks on screen.
906 void RunTest(Application& app)
908 ExampleController test(app);
913 int main(int argc, char **argv)
915 Application app = Application::New(&argc, &argv);