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-effect-cross.png",
45 DALI_IMAGE_DIR "icon-item-view-layout-spiral.png",
46 DALI_IMAGE_DIR "icon-replace.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 FittingMode::SCALE_TO_FILL to resize the image at
57 * load time to cover the entire stage with pixels with no borders,
58 * and filter mode BOX_THEN_LINEAR to sample the image with
61 ResourceImage LoadStageFillingImage( const char * const imagePath )
63 Size stageSize = Stage::GetCurrent().GetSize();
64 return ResourceImage::New( imagePath, Dali::ImageDimensions( stageSize.x, stageSize.y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR );
69 // This example shows the usage of BubbleEmitter which displays lots of moving bubbles on the stage.
70 class BubbleEffectExample : public ConnectionTracker
73 BubbleEffectExample(Application &app)
75 mHSVDelta( Vector3( 0.f, 0.f, 0.5f ) ),
77 mCurrentBackgroundImageId( 0 ),
78 mCurrentBubbleShapeImageId( 0 ),
79 mNeedNewAnimation( true )
81 // Connect to the Application's Init signal
82 app.InitSignal().Connect(this, &BubbleEffectExample::Create);
85 ~BubbleEffectExample()
91 // The Init signal is received once (only) during the Application lifetime
92 void Create(Application& app)
94 DemoHelper::RequestThemeChange();
96 Stage stage = Stage::GetCurrent();
97 Vector2 stageSize = stage.GetSize();
99 stage.KeyEventSignal().Connect(this, &BubbleEffectExample::OnKeyEvent);
101 // Creates a default view with a default tool bar.
102 // The view is added to the stage.
103 Toolkit::ToolBar toolBar;
104 Toolkit::Control view;
105 Layer content = DemoHelper::CreateView( app,
112 // Add a button to change background. (right of toolbar)
113 mChangeBackgroundButton = Toolkit::PushButton::New();
114 mChangeBackgroundButton.SetBackgroundImage( ResourceImage::New( CHANGE_BACKGROUND_ICON ) );
115 mChangeBackgroundButton.ClickedSignal().Connect( this, &BubbleEffectExample::OnChangeIconClicked );
116 toolBar.AddControl( mChangeBackgroundButton,
117 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
118 Toolkit::Alignment::HorizontalRight,
119 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
120 // Add a button to change bubble shape. ( left of bar )
121 mChangeBubbleShapeButton = Toolkit::PushButton::New();
122 mChangeBubbleShapeButton.SetBackgroundImage( ResourceImage::New( CHANGE_BUBBLE_SHAPE_ICON ) );
123 mChangeBubbleShapeButton.ClickedSignal().Connect( this, &BubbleEffectExample::OnChangeIconClicked );
124 toolBar.AddControl( mChangeBubbleShapeButton,
125 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
126 Toolkit::Alignment::HorizontalLeft,
127 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
129 // Create and initialize the BubbleEmitter object
130 mBubbleEmitter = Toolkit::BubbleEmitter::New( stageSize,
131 ResourceImage::New( BUBBLE_SHAPE_IMAGES[mCurrentBubbleShapeImageId] ),
132 DEFAULT_NUMBER_OF_BUBBLES,
133 DEFAULT_BUBBLE_SIZE);
134 mBackgroundImage = LoadStageFillingImage( BACKGROUND_IMAGES[mCurrentBackgroundImageId] );
135 mBubbleEmitter.SetBackground( mBackgroundImage, mHSVDelta );
137 // Get the root actor of all bubbles, and add it to stage.
138 Actor bubbleRoot = mBubbleEmitter.GetRootActor();
139 bubbleRoot.SetParentOrigin(ParentOrigin::CENTER);
140 bubbleRoot.SetZ(0.1f); // Make sure the bubbles displayed on top og the background.
141 content.Add( bubbleRoot );
143 // Add the background image actor to stage
144 mBackgroundActor = ImageActor::New( mBackgroundImage );
145 mBackgroundActor.SetParentOrigin( ParentOrigin::CENTER );
146 content.Add(mBackgroundActor);
148 // Set up the timer to emit bubble regularly when the finger is touched down but not moved
149 mTimerForBubbleEmission = Timer::New( mTimerInterval );
150 mTimerForBubbleEmission.TickSignal().Connect(this, &BubbleEffectExample::OnTimerTick);
152 // Connect the callback to the touch signal on the background
153 mBackgroundActor.TouchedSignal().Connect( this, &BubbleEffectExample::OnTouch );
161 // Set up the animation of emitting bubbles, to be efficient, every animation controls multiple emission ( 4 here )
162 void SetUpAnimation( Vector2 emitPosition, Vector2 direction )
164 if( mNeedNewAnimation )
166 float duration = Random::Range(1.f, 1.5f);
167 mEmitAnimation = Animation::New( duration );
168 mNeedNewAnimation = false;
169 mAnimateComponentCount = 0;
172 mBubbleEmitter.EmitBubble( mEmitAnimation, emitPosition, direction + Vector2(0.f, 30.f) /* upwards */, Vector2(300, 600) );
174 mAnimateComponentCount++;
176 if( mAnimateComponentCount % 4 ==0 )
178 mEmitAnimation.Play();
179 mNeedNewAnimation = true;
183 // Emit bubbles when the finger touches down but keep stationary.
184 // And stops emitting new bubble after being stationary for 2 seconds
187 if(mEmitPosition == mCurrentTouchPosition) // finger is not moving
190 if(mNonMovementCount < (1000 / mTimerInterval)) // 1 seconds
192 for(int i = 0; i < 4; i++) // emit 4 bubbles every timer tick
194 SetUpAnimation( mCurrentTouchPosition+Vector2(rand()%5, rand()%5), Vector2(rand()%60-30, rand()%100-50) );
200 mNonMovementCount = 0;
201 mEmitPosition = mCurrentTouchPosition;
207 // Callback function of the touch signal on the background
208 bool OnTouch(Dali::Actor actor, const Dali::TouchEvent& event)
210 const TouchPoint &point = event.GetPoint(0);
213 case TouchPoint::Down:
215 mCurrentTouchPosition = point.screen;
216 mEmitPosition = point.screen;
217 mTimerForBubbleEmission.Start();
218 mNonMovementCount = 0;
222 case TouchPoint::Motion:
224 Vector2 displacement = point.screen - mCurrentTouchPosition;
225 mCurrentTouchPosition = point.screen;
226 //emit multiple bubbles along the moving direction when the finger moves quickly
227 float step = std::min(5.f, displacement.Length());
228 for( float i=0.25f; i<step; i=i+1.f)
230 SetUpAnimation( mCurrentTouchPosition+displacement*(i/step), displacement );
235 case TouchPoint::Leave:
236 case TouchPoint::Interrupted:
238 mTimerForBubbleEmission.Stop();
239 mEmitAnimation.Play();
240 mNeedNewAnimation = true;
241 mAnimateComponentCount = 0;
244 case TouchPoint::Stationary:
245 case TouchPoint::Last:
255 bool OnChangeIconClicked( Toolkit::Button button )
257 if(button == mChangeBackgroundButton)
259 mBackgroundImage = LoadStageFillingImage( BACKGROUND_IMAGES[ ++mCurrentBackgroundImageId % NUM_BACKGROUND_IMAGES ] );
261 mBubbleEmitter.SetBackground( mBackgroundImage, mHSVDelta );
263 mBackgroundActor.SetImage( mBackgroundImage );
265 else if( button == mChangeBubbleShapeButton )
267 mBubbleEmitter.SetShapeImage( ResourceImage::New( BUBBLE_SHAPE_IMAGES[ ++mCurrentBubbleShapeImageId % NUM_BUBBLE_SHAPE_IMAGES ] ) );
273 * Main key event handler
275 void OnKeyEvent(const KeyEvent& event)
277 if(event.state == KeyEvent::Down)
279 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
289 Image mBackgroundImage;
290 ImageActor mBackgroundActor;
292 Toolkit::BubbleEmitter mBubbleEmitter;
293 Animation mEmitAnimation;
294 Toolkit::PushButton mChangeBackgroundButton;
295 Toolkit::PushButton mChangeBubbleShapeButton;
296 Timer mTimerForBubbleEmission;
299 Vector2 mCurrentTouchPosition;
300 Vector2 mEmitPosition;
302 unsigned int mAnimateComponentCount;
303 unsigned int mNonMovementCount;
304 unsigned int mTimerInterval;
305 unsigned int mCurrentBackgroundImageId;
306 unsigned int mCurrentBubbleShapeImageId;
308 bool mNeedNewAnimation;
311 /*****************************************************************************/
314 RunTest(Application& app)
316 BubbleEffectExample theApp(app);
320 /*****************************************************************************/
323 main(int argc, char **argv)
325 Application app = Application::New(&argc, &argv);