2 * Copyright (c) 2017 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 <dali-toolkit/devel-api/controls/buttons/button-devel.h>
22 #include "shared/view.h"
23 #include "shared/utility.h"
29 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
30 const char * const APPLICATION_TITLE( "Bubble Effect" );
31 const char * const CHANGE_BACKGROUND_ICON( DEMO_IMAGE_DIR "icon-change.png" );
32 const char * const CHANGE_BACKGROUND_ICON_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
33 const char * const CHANGE_BUBBLE_SHAPE_ICON( DEMO_IMAGE_DIR "icon-replace.png" );
34 const char * const CHANGE_BUBBLE_SHAPE_ICON_SELECTED( DEMO_IMAGE_DIR "icon-replace-selected.png" );
36 const char* BACKGROUND_IMAGES[]=
38 DEMO_IMAGE_DIR "background-1.jpg",
39 DEMO_IMAGE_DIR "background-2.jpg",
40 DEMO_IMAGE_DIR "background-3.jpg",
41 DEMO_IMAGE_DIR "background-4.jpg",
42 DEMO_IMAGE_DIR "background-5.jpg",
44 const unsigned int NUM_BACKGROUND_IMAGES( sizeof( BACKGROUND_IMAGES ) / sizeof( BACKGROUND_IMAGES[0] ) );
46 const char* BUBBLE_SHAPE_IMAGES[] =
48 DEMO_IMAGE_DIR "bubble-ball.png",
49 DEMO_IMAGE_DIR "icon-effect-cross.png",
50 DEMO_IMAGE_DIR "icon-item-view-layout-spiral.png",
51 DEMO_IMAGE_DIR "icon-replace.png"
53 const unsigned int NUM_BUBBLE_SHAPE_IMAGES( sizeof( BUBBLE_SHAPE_IMAGES ) / sizeof( BUBBLE_SHAPE_IMAGES[0] ) );
55 const Vector2 DEFAULT_BUBBLE_SIZE( 10.f, 30.f );
56 const unsigned int DEFAULT_NUMBER_OF_BUBBLES( 1000 );
60 // This example shows the usage of BubbleEmitter which displays lots of moving bubbles on the stage.
61 class BubbleEffectExample : public ConnectionTracker
64 BubbleEffectExample(Application &app)
69 mChangeBackgroundButton(),
70 mChangeBubbleShapeButton(),
71 mTimerForBubbleEmission(),
72 mHSVDelta( Vector3( 0.f, 0.f, 0.5f ) ),
73 mCurrentTouchPosition(),
75 mAnimateComponentCount( 0 ),
76 mNonMovementCount( 0 ),
78 mCurrentBackgroundImageId( 0 ),
79 mCurrentBubbleShapeImageId( 0 ),
80 mNeedNewAnimation( true )
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 Layer content = DemoHelper::CreateView( app,
110 // Add a button to change background. (right of toolbar)
111 mChangeBackgroundButton = Toolkit::PushButton::New();
112 mChangeBackgroundButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_BACKGROUND_ICON );
113 mChangeBackgroundButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_BACKGROUND_ICON_SELECTED );
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.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_BUBBLE_SHAPE_ICON );
122 mChangeBubbleShapeButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_BUBBLE_SHAPE_ICON_SELECTED );
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 DemoHelper::LoadTexture( BUBBLE_SHAPE_IMAGES[mCurrentBubbleShapeImageId] ),
132 DEFAULT_NUMBER_OF_BUBBLES,
133 DEFAULT_BUBBLE_SIZE);
135 mBubbleEmitter.SetBackground( DemoHelper::LoadStageFillingTexture( BACKGROUND_IMAGES[mCurrentBackgroundImageId] ), 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 // Set the application background
144 mBackground.SetProperty( Toolkit::Control::Property::BACKGROUND, BACKGROUND_IMAGES[ mCurrentBackgroundImageId ] );
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 mBackground.TouchSignal().Connect( this, &BubbleEffectExample::OnTouch );
159 // Set up the animation of emitting bubbles, to be efficient, every animation controls multiple emission ( 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::TouchData& event)
208 switch( event.GetState( 0 ) )
210 case PointState::DOWN:
212 mCurrentTouchPosition = mEmitPosition = event.GetScreenPosition( 0 );
213 mTimerForBubbleEmission.Start();
214 mNonMovementCount = 0;
218 case PointState::MOTION:
220 Vector2 displacement = event.GetScreenPosition( 0 ) - mCurrentTouchPosition;
221 mCurrentTouchPosition = event.GetScreenPosition( 0 );
222 //emit multiple bubbles along the moving direction when the finger moves quickly
223 float step = std::min(5.f, displacement.Length());
224 for( float i=0.25f; i<step; i=i+1.f)
226 SetUpAnimation( mCurrentTouchPosition+displacement*(i/step), displacement );
231 case PointState::LEAVE:
232 case PointState::INTERRUPTED:
234 mTimerForBubbleEmission.Stop();
235 mEmitAnimation.Play();
236 mNeedNewAnimation = true;
237 mAnimateComponentCount = 0;
240 case PointState::STATIONARY:
250 bool OnChangeIconClicked( Toolkit::Button button )
252 if(button == mChangeBackgroundButton)
254 mCurrentBackgroundImageId = (mCurrentBackgroundImageId+1) % NUM_BACKGROUND_IMAGES;
256 //Update bubble emitter background
257 mBubbleEmitter.SetBackground( DemoHelper::LoadStageFillingTexture( BACKGROUND_IMAGES[ mCurrentBackgroundImageId ] ), mHSVDelta );
259 // Set the application background
260 mBackground.SetProperty( Toolkit::Control::Property::BACKGROUND, BACKGROUND_IMAGES[ mCurrentBackgroundImageId ] );
262 else if( button == mChangeBubbleShapeButton )
264 mBubbleEmitter.SetBubbleShape( DemoHelper::LoadTexture( BUBBLE_SHAPE_IMAGES[ ++mCurrentBubbleShapeImageId % NUM_BUBBLE_SHAPE_IMAGES ] ) );
270 * Main key event handler
272 void OnKeyEvent(const KeyEvent& event)
274 if(event.state == KeyEvent::Down)
276 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
286 Dali::Toolkit::Control mBackground;
288 Toolkit::BubbleEmitter mBubbleEmitter;
289 Animation mEmitAnimation;
290 Toolkit::PushButton mChangeBackgroundButton;
291 Toolkit::PushButton mChangeBubbleShapeButton;
292 Timer mTimerForBubbleEmission;
295 Vector2 mCurrentTouchPosition;
296 Vector2 mEmitPosition;
298 unsigned int mAnimateComponentCount;
299 unsigned int mNonMovementCount;
300 unsigned int mTimerInterval;
301 unsigned int mCurrentBackgroundImageId;
302 unsigned int mCurrentBubbleShapeImageId;
304 bool mNeedNewAnimation;
307 /*****************************************************************************/
309 int DALI_EXPORT_API main(int argc, char **argv)
311 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
312 BubbleEffectExample theApp(app);