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 <dali-toolkit/devel-api/controls/bubble-effect/bubble-emitter.h>
21 #include "shared/view.h"
27 const char * const TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
28 const char * const APPLICATION_TITLE( "Bubble Effect" );
29 const char * const CHANGE_BACKGROUND_ICON( DALI_IMAGE_DIR "icon-change.png" );
30 const char * const CHANGE_BUBBLE_SHAPE_ICON( DALI_IMAGE_DIR "icon-replace.png" );
32 const char* BACKGROUND_IMAGES[]=
34 DALI_IMAGE_DIR "background-1.jpg",
35 DALI_IMAGE_DIR "background-2.jpg",
36 DALI_IMAGE_DIR "background-3.jpg",
37 DALI_IMAGE_DIR "background-4.jpg",
38 DALI_IMAGE_DIR "background-5.jpg",
40 const unsigned int NUM_BACKGROUND_IMAGES( sizeof( BACKGROUND_IMAGES ) / sizeof( BACKGROUND_IMAGES[0] ) );
42 const char* BUBBLE_SHAPE_IMAGES[] =
44 DALI_IMAGE_DIR "bubble-ball.png",
45 DALI_IMAGE_DIR "icon-item-view-layout-spiral.png",
46 DALI_IMAGE_DIR "icon-replace.png",
47 DALI_IMAGE_DIR "icon-effect-cross.png"
49 const unsigned int NUM_BUBBLE_SHAPE_IMAGES( sizeof( BUBBLE_SHAPE_IMAGES ) / sizeof( BUBBLE_SHAPE_IMAGES[0] ) );
51 const Vector2 DEFAULT_BUBBLE_SIZE( 10.f, 30.f );
52 const unsigned int DEFAULT_NUMBER_OF_BUBBLES( 1000 );
55 * @brief Load an image, scaled-down to no more than the stage dimensions.
57 * Uses image scaling mode FittingMode::SCALE_TO_FILL to resize the image at
58 * load time to cover the entire stage with pixels with no borders,
59 * and filter mode BOX_THEN_LINEAR to sample the image with
62 ResourceImage LoadStageFillingImage( const char * const imagePath )
64 Size stageSize = Stage::GetCurrent().GetSize();
65 return ResourceImage::New( imagePath, Dali::ImageDimensions( stageSize.x, stageSize.y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR );
70 // This example shows the usage of BubbleEmitter which displays lots of moving bubbles on the stage.
71 class BubbleEffectExample : public ConnectionTracker
74 BubbleEffectExample(Application &app)
76 mHSVDelta( Vector3( 0.f, 0.f, 0.5f ) ),
77 mNeedNewAnimation( true ),
79 mCurrentBackgroundImageId( 0 ),
80 mCurrentBubbleShapeImageId( 0 )
82 // Connect to the Application's Init signal
83 app.InitSignal().Connect(this, &BubbleEffectExample::Create);
86 ~BubbleEffectExample()
92 // The Init signal is received once (only) during the Application lifetime
93 void Create(Application& app)
95 Stage stage = Stage::GetCurrent();
96 Vector2 stageSize = stage.GetSize();
98 stage.KeyEventSignal().Connect(this, &BubbleEffectExample::OnKeyEvent);
100 // Creates a default view with a default tool bar.
101 // The view is added to the stage.
102 Toolkit::ToolBar toolBar;
103 Toolkit::Control view;
104 Layer content = DemoHelper::CreateView( app,
111 // Add a button to change background. (right of toolbar)
112 mChangeBackgroundButton = Toolkit::PushButton::New();
113 mChangeBackgroundButton.SetBackgroundImage( ResourceImage::New( CHANGE_BACKGROUND_ICON ) );
114 mChangeBackgroundButton.ClickedSignal().Connect( this, &BubbleEffectExample::OnChangeIconClicked );
115 toolBar.AddControl( mChangeBackgroundButton,
116 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
117 Toolkit::Alignment::HorizontalRight,
118 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
119 // Add a button to change bubble shape. ( left of bar )
120 mChangeBubbleShapeButton = Toolkit::PushButton::New();
121 mChangeBubbleShapeButton.SetBackgroundImage( ResourceImage::New( CHANGE_BUBBLE_SHAPE_ICON ) );
122 mChangeBubbleShapeButton.ClickedSignal().Connect( this, &BubbleEffectExample::OnChangeIconClicked );
123 toolBar.AddControl( mChangeBubbleShapeButton,
124 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
125 Toolkit::Alignment::HorizontalLeft,
126 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
128 // Create and initialize the BubbleEmitter object
129 mBubbleEmitter = Toolkit::BubbleEmitter::New( stageSize,
130 ResourceImage::New( BUBBLE_SHAPE_IMAGES[mCurrentBubbleShapeImageId] ),
131 DEFAULT_NUMBER_OF_BUBBLES,
132 DEFAULT_BUBBLE_SIZE);
133 mBackgroundImage = LoadStageFillingImage( BACKGROUND_IMAGES[mCurrentBackgroundImageId] );
134 mBubbleEmitter.SetBackground( mBackgroundImage, mHSVDelta );
136 // Get the root actor of all bubbles, and add it to stage.
137 Actor bubbleRoot = mBubbleEmitter.GetRootActor();
138 bubbleRoot.SetParentOrigin(ParentOrigin::CENTER);
139 bubbleRoot.SetZ(0.1f); // Make sure the bubbles displayed on top og the background.
140 content.Add( bubbleRoot );
142 // Add the background image actor to stage
143 view.SetBackgroundImage( mBackgroundImage );
144 mBackgroundActor = ImageActor::DownCast( view.GetBackgroundActor() );
146 // Set up the timer to emit bubble regularly when the finger is touched down but not moved
147 mTimerForBubbleEmission = Timer::New( mTimerInterval );
148 mTimerForBubbleEmission.TickSignal().Connect(this, &BubbleEffectExample::OnTimerTick);
150 // Connect the callback to the touch signal on the background
151 mBackgroundActor.TouchedSignal().Connect( this, &BubbleEffectExample::OnTouch );
159 // Set up the animation of emitting bubbles, to be efficient, every animation controls multiple bubbles ( 4 here )
160 void SetUpAnimation( Vector2 emitPosition, Vector2 direction )
162 if( mNeedNewAnimation )
164 float duration = Random::Range(1.f, 1.5f);
165 mEmitAnimation = Animation::New( duration );
166 mNeedNewAnimation = false;
167 mAnimateComponentCount = 0;
170 mBubbleEmitter.EmitBubble( mEmitAnimation, emitPosition, direction + Vector2(0.f, 30.f) /* upwards */, Vector2(300, 600) );
172 mAnimateComponentCount++;
174 if( mAnimateComponentCount % 4 ==0 )
176 mEmitAnimation.Play();
177 mNeedNewAnimation = true;
181 // Emit bubbles when the finger touches down but keep stationary.
182 // And stops emitting new bubble after being stationary for 2 seconds
185 if(mEmitPosition == mCurrentTouchPosition) // finger is not moving
188 if(mNonMovementCount < (1000 / mTimerInterval)) // 1 seconds
190 for(int i = 0; i < 4; i++) // emit 4 bubbles every timer tick
192 SetUpAnimation( mCurrentTouchPosition+Vector2(rand()%5, rand()%5), Vector2(rand()%60-30, rand()%100-50) );
198 mNonMovementCount = 0;
199 mEmitPosition = mCurrentTouchPosition;
205 // Callback function of the touch signal on the background
206 bool OnTouch(Dali::Actor actor, const Dali::TouchEvent& event)
208 const TouchPoint &point = event.GetPoint(0);
211 case TouchPoint::Down:
213 mCurrentTouchPosition = point.screen;
214 mEmitPosition = point.screen;
215 mTimerForBubbleEmission.Start();
216 mNonMovementCount = 0;
220 case TouchPoint::Motion:
222 Vector2 displacement = point.screen - mCurrentTouchPosition;
223 mCurrentTouchPosition = point.screen;
224 //emit multiple bubbles along the moving direction when the finger moves quickly
225 float step = std::min(5.f, displacement.Length());
226 for( float i=0.25f; i<step; i=i+1.f)
228 SetUpAnimation( mCurrentTouchPosition+displacement*(i/step), displacement );
233 case TouchPoint::Leave:
234 case TouchPoint::Interrupted:
236 mTimerForBubbleEmission.Stop();
239 case TouchPoint::Stationary:
240 case TouchPoint::Last:
250 bool OnChangeIconClicked( Toolkit::Button button )
252 if(button == mChangeBackgroundButton)
254 mBackgroundImage = LoadStageFillingImage( BACKGROUND_IMAGES[ ++mCurrentBackgroundImageId % NUM_BACKGROUND_IMAGES ] );
256 mBubbleEmitter.SetBackground( mBackgroundImage, mHSVDelta );
258 mBackgroundActor.SetImage( mBackgroundImage );
260 else if( button == mChangeBubbleShapeButton )
262 mBubbleEmitter.SetShapeImage( ResourceImage::New( BUBBLE_SHAPE_IMAGES[ ++mCurrentBubbleShapeImageId % NUM_BUBBLE_SHAPE_IMAGES ] ) );
268 * Main key event handler
270 void OnKeyEvent(const KeyEvent& event)
272 if(event.state == KeyEvent::Down)
274 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
284 Image mBackgroundImage;
285 ImageActor mBackgroundActor;
287 Toolkit::BubbleEmitter mBubbleEmitter;
290 Animation mEmitAnimation;
291 unsigned int mAnimateComponentCount;
292 bool mNeedNewAnimation;
294 Timer mTimerForBubbleEmission;
295 unsigned int mNonMovementCount;
296 unsigned int mTimerInterval;
298 Vector2 mCurrentTouchPosition;
299 Vector2 mEmitPosition;
301 Toolkit::PushButton mChangeBackgroundButton;
302 Toolkit::PushButton mChangeBubbleShapeButton;
303 unsigned int mCurrentBackgroundImageId;
304 unsigned int mCurrentBubbleShapeImageId;
307 /*****************************************************************************/
310 RunTest(Application& app)
312 BubbleEffectExample theApp(app);
316 /*****************************************************************************/
319 main(int argc, char **argv)
321 Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);