2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include "shared/view.h"
26 const char * const TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
27 const char * const APPLICATION_TITLE( "Bubble Effect" );
28 const char * const CHANGE_BACKGROUND_ICON( DALI_IMAGE_DIR "icon-change.png" );
29 const char * const CHANGE_BUBBLE_SHAPE_ICON( DALI_IMAGE_DIR "icon-replace.png" );
31 const char* BACKGROUND_IMAGES[]=
33 DALI_IMAGE_DIR "background-1.jpg",
34 DALI_IMAGE_DIR "background-2.jpg",
35 DALI_IMAGE_DIR "background-3.jpg",
36 DALI_IMAGE_DIR "background-4.jpg",
37 DALI_IMAGE_DIR "background-5.jpg",
39 const unsigned int NUM_BACKGROUND_IMAGES( sizeof( BACKGROUND_IMAGES ) / sizeof( BACKGROUND_IMAGES[0] ) );
41 const char* BUBBLE_SHAPE_IMAGES[] =
43 DALI_IMAGE_DIR "bubble-ball.png",
44 DALI_IMAGE_DIR "icon-item-view-layout-spiral.png",
45 DALI_IMAGE_DIR "icon-replace.png",
46 DALI_IMAGE_DIR "icon-effect-cross.png"
48 const unsigned int NUM_BUBBLE_SHAPE_IMAGES( sizeof( BUBBLE_SHAPE_IMAGES ) / sizeof( BUBBLE_SHAPE_IMAGES[0] ) );
50 const Vector2 DEFAULT_BUBBLE_SIZE( 10.f, 30.f );
51 const unsigned int DEFAULT_NUMBER_OF_BUBBLES( 1000 );
54 * @brief Load an image, scaled-down to no more than the stage dimensions.
56 * Uses image scaling mode ImageAttributes::ScaleToFill to resize the image at
57 * load time to cover the entire stage with pixels with no borders,
58 * and filter mode ImageAttributes::BoxThenLinear to sample the image with
61 ResourceImage LoadStageFillingImage( const char * const imagePath )
63 Size stageSize = Stage::GetCurrent().GetSize();
64 ImageAttributes attributes;
65 attributes.SetSize( stageSize.x, stageSize.y );
66 attributes.SetFilterMode( ImageAttributes::BoxThenLinear );
67 attributes.SetScalingMode( ImageAttributes::ScaleToFill );
68 return ResourceImage::New( imagePath, attributes );
73 // This example shows the usage of BubbleEmitter which displays lots of moving bubbles on the stage.
74 class BubbleEffectExample : public ConnectionTracker
77 BubbleEffectExample(Application &app)
79 mHSVDelta( Vector3( 0.f, 0.f, 0.5f ) ),
80 mNeedNewAnimation( true ),
82 mCurrentBackgroundImageId( 0 ),
83 mCurrentBubbleShapeImageId( 0 )
85 // Connect to the Application's Init signal
86 app.InitSignal().Connect(this, &BubbleEffectExample::Create);
89 ~BubbleEffectExample()
95 // The Init signal is received once (only) during the Application lifetime
96 void Create(Application& app)
98 Stage stage = Stage::GetCurrent();
99 Vector2 stageSize = stage.GetSize();
101 stage.KeyEventSignal().Connect(this, &BubbleEffectExample::OnKeyEvent);
103 // Creates a default view with a default tool bar.
104 // The view is added to the stage.
105 Toolkit::ToolBar toolBar;
107 Layer content = DemoHelper::CreateView( app,
114 // Add a button to change background. (right of toolbar)
115 mChangeBackgroundButton = Toolkit::PushButton::New();
116 mChangeBackgroundButton.SetBackgroundImage( ResourceImage::New( CHANGE_BACKGROUND_ICON ) );
117 mChangeBackgroundButton.ClickedSignal().Connect( this, &BubbleEffectExample::OnChangeIconClicked );
118 toolBar.AddControl( mChangeBackgroundButton,
119 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
120 Toolkit::Alignment::HorizontalRight,
121 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
122 // Add a button to change bubble shape. ( left of bar )
123 mChangeBubbleShapeButton = Toolkit::PushButton::New();
124 mChangeBubbleShapeButton.SetBackgroundImage( ResourceImage::New( CHANGE_BUBBLE_SHAPE_ICON ) );
125 mChangeBubbleShapeButton.ClickedSignal().Connect( this, &BubbleEffectExample::OnChangeIconClicked );
126 toolBar.AddControl( mChangeBubbleShapeButton,
127 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
128 Toolkit::Alignment::HorizontalLeft,
129 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
131 // Create and initialize the BubbleEmitter object
132 mBubbleEmitter = Toolkit::BubbleEmitter::New( stageSize,
133 ResourceImage::New( BUBBLE_SHAPE_IMAGES[mCurrentBubbleShapeImageId] ),
134 DEFAULT_NUMBER_OF_BUBBLES,
135 DEFAULT_BUBBLE_SIZE);
136 mBackgroundImage = LoadStageFillingImage( BACKGROUND_IMAGES[mCurrentBackgroundImageId] );
137 mBubbleEmitter.SetBackground( mBackgroundImage, mHSVDelta );
139 // Get the root actor of all bubbles, and add it to stage.
140 Actor bubbleRoot = mBubbleEmitter.GetRootActor();
141 bubbleRoot.SetParentOrigin(ParentOrigin::CENTER);
142 bubbleRoot.SetZ(0.1f); // Make sure the bubbles displayed on top og the background.
143 content.Add( bubbleRoot );
145 // Add the background image actor to stage
146 mBackgroundActor = ImageActor::New( mBackgroundImage );
147 view.SetBackground( mBackgroundActor );
149 // Set up the timer to emit bubble regularly when the finger is touched down but not moved
150 mTimerForBubbleEmission = Timer::New( mTimerInterval );
151 mTimerForBubbleEmission.TickSignal().Connect(this, &BubbleEffectExample::OnTimerTick);
153 // Connect the callback to the touch signal on the background
154 mBackgroundActor.TouchedSignal().Connect( this, &BubbleEffectExample::OnTouch );
162 // Set up the animation of emitting bubbles, to be efficient, every animation controls multiple bubbles ( 4 here )
163 void SetUpAnimation( Vector2 emitPosition, Vector2 direction )
165 if( mNeedNewAnimation )
167 float duration = Random::Range(1.f, 1.5f);
168 mEmitAnimation = Animation::New( duration );
169 mNeedNewAnimation = false;
170 mAnimateComponentCount = 0;
173 mBubbleEmitter.EmitBubble( mEmitAnimation, emitPosition, direction + Vector2(0.f, 30.f) /* upwards */, Vector2(300, 600) );
175 mAnimateComponentCount++;
177 if( mAnimateComponentCount % 4 ==0 )
179 mEmitAnimation.Play();
180 mNeedNewAnimation = true;
184 // Emit bubbles when the finger touches down but keep stationary.
185 // And stops emitting new bubble after being stationary for 2 seconds
188 if(mEmitPosition == mCurrentTouchPosition) // finger is not moving
191 if(mNonMovementCount < (1000 / mTimerInterval)) // 1 seconds
193 for(int i = 0; i < 4; i++) // emit 4 bubbles every timer tick
195 SetUpAnimation( mCurrentTouchPosition+Vector2(rand()%5, rand()%5), Vector2(rand()%60-30, rand()%100-50) );
201 mNonMovementCount = 0;
202 mEmitPosition = mCurrentTouchPosition;
208 // Callback function of the touch signal on the background
209 bool OnTouch(Dali::Actor actor, const Dali::TouchEvent& event)
211 const TouchPoint &point = event.GetPoint(0);
214 case TouchPoint::Down:
216 mCurrentTouchPosition = point.screen;
217 mEmitPosition = point.screen;
218 mTimerForBubbleEmission.Start();
219 mNonMovementCount = 0;
223 case TouchPoint::Motion:
225 Vector2 displacement = point.screen - mCurrentTouchPosition;
226 mCurrentTouchPosition = point.screen;
227 //emit multiple bubbles along the moving direction when the finger moves quickly
228 float step = std::min(5.f, displacement.Length());
229 for( float i=0.25f; i<step; i=i+1.f)
231 SetUpAnimation( mCurrentTouchPosition+displacement*(i/step), displacement );
236 case TouchPoint::Leave:
237 case TouchPoint::Interrupted:
239 mTimerForBubbleEmission.Stop();
242 case TouchPoint::Stationary:
243 case TouchPoint::Last:
253 bool OnChangeIconClicked( Toolkit::Button button )
255 if(button == mChangeBackgroundButton)
257 mBackgroundImage = LoadStageFillingImage( BACKGROUND_IMAGES[ ++mCurrentBackgroundImageId % NUM_BACKGROUND_IMAGES ] );
259 mBubbleEmitter.SetBackground( mBackgroundImage, mHSVDelta );
261 mBackgroundActor.SetImage( mBackgroundImage );
263 else if( button == mChangeBubbleShapeButton )
265 mBubbleEmitter.SetShapeImage( ResourceImage::New( BUBBLE_SHAPE_IMAGES[ ++mCurrentBubbleShapeImageId % NUM_BUBBLE_SHAPE_IMAGES ] ) );
271 * Main key event handler
273 void OnKeyEvent(const KeyEvent& event)
275 if(event.state == KeyEvent::Down)
277 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
287 Image mBackgroundImage;
288 ImageActor mBackgroundActor;
290 Toolkit::BubbleEmitter mBubbleEmitter;
293 Animation mEmitAnimation;
294 unsigned int mAnimateComponentCount;
295 bool mNeedNewAnimation;
297 Timer mTimerForBubbleEmission;
298 unsigned int mNonMovementCount;
299 unsigned int mTimerInterval;
301 Vector2 mCurrentTouchPosition;
302 Vector2 mEmitPosition;
304 Toolkit::PushButton mChangeBackgroundButton;
305 Toolkit::PushButton mChangeBubbleShapeButton;
306 unsigned int mCurrentBackgroundImageId;
307 unsigned int mCurrentBubbleShapeImageId;
310 /*****************************************************************************/
313 RunTest(Application& app)
315 BubbleEffectExample theApp(app);
319 /*****************************************************************************/
322 main(int argc, char **argv)
324 Application app = Application::New(&argc, &argv);