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"
22 #include "shared/utility.h"
28 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
29 const char * const APPLICATION_TITLE( "Bubble Effect" );
30 const char * const CHANGE_BACKGROUND_ICON( DEMO_IMAGE_DIR "icon-change.png" );
31 const char * const CHANGE_BACKGROUND_ICON_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
32 const char * const CHANGE_BUBBLE_SHAPE_ICON( DEMO_IMAGE_DIR "icon-replace.png" );
33 const char * const CHANGE_BUBBLE_SHAPE_ICON_SELECTED( DEMO_IMAGE_DIR "icon-replace-selected.png" );
35 const char* BACKGROUND_IMAGES[]=
37 DEMO_IMAGE_DIR "background-1.jpg",
38 DEMO_IMAGE_DIR "background-2.jpg",
39 DEMO_IMAGE_DIR "background-3.jpg",
40 DEMO_IMAGE_DIR "background-4.jpg",
41 DEMO_IMAGE_DIR "background-5.jpg",
43 const unsigned int NUM_BACKGROUND_IMAGES( sizeof( BACKGROUND_IMAGES ) / sizeof( BACKGROUND_IMAGES[0] ) );
45 const char* BUBBLE_SHAPE_IMAGES[] =
47 DEMO_IMAGE_DIR "bubble-ball.png",
48 DEMO_IMAGE_DIR "icon-effect-cross.png",
49 DEMO_IMAGE_DIR "icon-item-view-layout-spiral.png",
50 DEMO_IMAGE_DIR "icon-replace.png"
52 const unsigned int NUM_BUBBLE_SHAPE_IMAGES( sizeof( BUBBLE_SHAPE_IMAGES ) / sizeof( BUBBLE_SHAPE_IMAGES[0] ) );
54 const Vector2 DEFAULT_BUBBLE_SIZE( 10.f, 30.f );
55 const unsigned int DEFAULT_NUMBER_OF_BUBBLES( 1000 );
59 // This example shows the usage of BubbleEmitter which displays lots of moving bubbles on the stage.
60 class BubbleEffectExample : public ConnectionTracker
63 BubbleEffectExample(Application &app)
65 mHSVDelta( Vector3( 0.f, 0.f, 0.5f ) ),
67 mCurrentBackgroundImageId( 0 ),
68 mCurrentBubbleShapeImageId( 0 ),
69 mNeedNewAnimation( true )
71 // Connect to the Application's Init signal
72 app.InitSignal().Connect(this, &BubbleEffectExample::Create);
75 ~BubbleEffectExample()
81 // The Init signal is received once (only) during the Application lifetime
82 void Create(Application& app)
84 Stage stage = Stage::GetCurrent();
85 Vector2 stageSize = stage.GetSize();
87 stage.KeyEventSignal().Connect(this, &BubbleEffectExample::OnKeyEvent);
89 // Creates a default view with a default tool bar.
90 // The view is added to the stage.
91 Toolkit::ToolBar toolBar;
92 Layer content = DemoHelper::CreateView( app,
99 // Add a button to change background. (right of toolbar)
100 mChangeBackgroundButton = Toolkit::PushButton::New();
101 mChangeBackgroundButton.SetUnselectedImage( CHANGE_BACKGROUND_ICON );
102 mChangeBackgroundButton.SetSelectedImage( CHANGE_BACKGROUND_ICON_SELECTED );
103 mChangeBackgroundButton.ClickedSignal().Connect( this, &BubbleEffectExample::OnChangeIconClicked );
104 toolBar.AddControl( mChangeBackgroundButton,
105 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
106 Toolkit::Alignment::HorizontalRight,
107 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
108 // Add a button to change bubble shape. ( left of bar )
109 mChangeBubbleShapeButton = Toolkit::PushButton::New();
110 mChangeBubbleShapeButton.SetUnselectedImage( CHANGE_BUBBLE_SHAPE_ICON );
111 mChangeBubbleShapeButton.SetSelectedImage( CHANGE_BUBBLE_SHAPE_ICON_SELECTED );
112 mChangeBubbleShapeButton.ClickedSignal().Connect( this, &BubbleEffectExample::OnChangeIconClicked );
113 toolBar.AddControl( mChangeBubbleShapeButton,
114 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
115 Toolkit::Alignment::HorizontalLeft,
116 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
118 // Create and initialize the BubbleEmitter object
119 mBubbleEmitter = Toolkit::BubbleEmitter::New( stageSize,
120 DemoHelper::LoadImage( BUBBLE_SHAPE_IMAGES[mCurrentBubbleShapeImageId] ),
121 DEFAULT_NUMBER_OF_BUBBLES,
122 DEFAULT_BUBBLE_SIZE);
123 mBackgroundImage = DemoHelper::LoadStageFillingImage( BACKGROUND_IMAGES[mCurrentBackgroundImageId] );
124 mBubbleEmitter.SetBackground( mBackgroundImage, mHSVDelta );
126 // Get the root actor of all bubbles, and add it to stage.
127 Actor bubbleRoot = mBubbleEmitter.GetRootActor();
128 bubbleRoot.SetParentOrigin(ParentOrigin::CENTER);
129 bubbleRoot.SetZ(0.1f); // Make sure the bubbles displayed on top og the background.
130 content.Add( bubbleRoot );
132 // Add the background image actor to stage
133 mBackground.SetBackgroundImage( mBackgroundImage );
135 // Set up the timer to emit bubble regularly when the finger is touched down but not moved
136 mTimerForBubbleEmission = Timer::New( mTimerInterval );
137 mTimerForBubbleEmission.TickSignal().Connect(this, &BubbleEffectExample::OnTimerTick);
139 // Connect the callback to the touch signal on the background
140 mBackground.TouchSignal().Connect( this, &BubbleEffectExample::OnTouch );
148 // Set up the animation of emitting bubbles, to be efficient, every animation controls multiple emission ( 4 here )
149 void SetUpAnimation( Vector2 emitPosition, Vector2 direction )
151 if( mNeedNewAnimation )
153 float duration = Random::Range(1.f, 1.5f);
154 mEmitAnimation = Animation::New( duration );
155 mNeedNewAnimation = false;
156 mAnimateComponentCount = 0;
159 mBubbleEmitter.EmitBubble( mEmitAnimation, emitPosition, direction + Vector2(0.f, 30.f) /* upwards */, Vector2(300, 600) );
161 mAnimateComponentCount++;
163 if( mAnimateComponentCount % 4 ==0 )
165 mEmitAnimation.Play();
166 mNeedNewAnimation = true;
170 // Emit bubbles when the finger touches down but keep stationary.
171 // And stops emitting new bubble after being stationary for 2 seconds
174 if(mEmitPosition == mCurrentTouchPosition) // finger is not moving
177 if(mNonMovementCount < (1000 / mTimerInterval)) // 1 seconds
179 for(int i = 0; i < 4; i++) // emit 4 bubbles every timer tick
181 SetUpAnimation( mCurrentTouchPosition+Vector2(rand()%5, rand()%5), Vector2(rand()%60-30, rand()%100-50) );
187 mNonMovementCount = 0;
188 mEmitPosition = mCurrentTouchPosition;
194 // Callback function of the touch signal on the background
195 bool OnTouch(Dali::Actor actor, const Dali::TouchData& event)
197 switch( event.GetState( 0 ) )
199 case PointState::DOWN:
201 mCurrentTouchPosition = mEmitPosition = event.GetScreenPosition( 0 );
202 mTimerForBubbleEmission.Start();
203 mNonMovementCount = 0;
207 case PointState::MOTION:
209 Vector2 displacement = event.GetScreenPosition( 0 ) - mCurrentTouchPosition;
210 mCurrentTouchPosition = event.GetScreenPosition( 0 );
211 //emit multiple bubbles along the moving direction when the finger moves quickly
212 float step = std::min(5.f, displacement.Length());
213 for( float i=0.25f; i<step; i=i+1.f)
215 SetUpAnimation( mCurrentTouchPosition+displacement*(i/step), displacement );
220 case PointState::LEAVE:
221 case PointState::INTERRUPTED:
223 mTimerForBubbleEmission.Stop();
224 mEmitAnimation.Play();
225 mNeedNewAnimation = true;
226 mAnimateComponentCount = 0;
229 case PointState::STATIONARY:
239 bool OnChangeIconClicked( Toolkit::Button button )
241 if(button == mChangeBackgroundButton)
243 mBackgroundImage = DemoHelper::LoadStageFillingImage( BACKGROUND_IMAGES[ ++mCurrentBackgroundImageId % NUM_BACKGROUND_IMAGES ] );
245 mBubbleEmitter.SetBackground( mBackgroundImage, mHSVDelta );
247 mBackground.SetBackgroundImage( mBackgroundImage );
249 else if( button == mChangeBubbleShapeButton )
251 mBubbleEmitter.SetShapeImage( DemoHelper::LoadImage( BUBBLE_SHAPE_IMAGES[ ++mCurrentBubbleShapeImageId % NUM_BUBBLE_SHAPE_IMAGES ] ) );
257 * Main key event handler
259 void OnKeyEvent(const KeyEvent& event)
261 if(event.state == KeyEvent::Down)
263 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
273 Image mBackgroundImage;
274 Dali::Toolkit::Control mBackground;
276 Toolkit::BubbleEmitter mBubbleEmitter;
277 Animation mEmitAnimation;
278 Toolkit::PushButton mChangeBackgroundButton;
279 Toolkit::PushButton mChangeBubbleShapeButton;
280 Timer mTimerForBubbleEmission;
283 Vector2 mCurrentTouchPosition;
284 Vector2 mEmitPosition;
286 unsigned int mAnimateComponentCount;
287 unsigned int mNonMovementCount;
288 unsigned int mTimerInterval;
289 unsigned int mCurrentBackgroundImageId;
290 unsigned int mCurrentBubbleShapeImageId;
292 bool mNeedNewAnimation;
295 /*****************************************************************************/
298 RunTest(Application& app)
300 BubbleEffectExample theApp(app);
304 /*****************************************************************************/
306 int DALI_EXPORT_API main(int argc, char **argv)
308 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);