bubble-effect-example.cpp
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Copyright (c) 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <dali/dali.h>
#include <dali-toolkit/dali-toolkit.h>
#include "shared/view.h"
using namespace Dali;
namespace
{
const char * const TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
const char * const APPLICATION_TITLE( "Bubble Effect" );
const char * const CHANGE_BACKGROUND_ICON( DALI_IMAGE_DIR "icon-change.png" );
const char * const CHANGE_BUBBLE_SHAPE_ICON( DALI_IMAGE_DIR "icon-replace.png" );
const char* BACKGROUND_IMAGES[]=
{
DALI_IMAGE_DIR "background-1.jpg",
DALI_IMAGE_DIR "background-2.jpg",
DALI_IMAGE_DIR "background-3.jpg",
DALI_IMAGE_DIR "background-4.jpg",
DALI_IMAGE_DIR "background-5.jpg",
};
const unsigned int NUM_BACKGROUND_IMAGES( sizeof( BACKGROUND_IMAGES ) / sizeof( BACKGROUND_IMAGES[0] ) );
const char* BUBBLE_SHAPE_IMAGES[] =
{
DALI_IMAGE_DIR "bubble-ball.png",
DALI_IMAGE_DIR "icon-item-view-layout-spiral.png",
DALI_IMAGE_DIR "icon-replace.png",
DALI_IMAGE_DIR "icon-effect-cross.png"
};
const unsigned int NUM_BUBBLE_SHAPE_IMAGES( sizeof( BUBBLE_SHAPE_IMAGES ) / sizeof( BUBBLE_SHAPE_IMAGES[0] ) );
const Vector2 DEFAULT_BUBBLE_SIZE( 10.f, 30.f );
const unsigned int DEFAULT_NUMBER_OF_BUBBLES( 1000 );
/**
* @brief Load an image, scaled-down to no more than the stage dimensions.
*
* Uses image scaling mode ImageAttributes::ScaleToFill to resize the image at
* load time to cover the entire stage with pixels with no borders,
* and filter mode ImageAttributes::BoxThenLinear to sample the image with
* maximum quality.
*/
ResourceImage LoadStageFillingImage( const char * const imagePath )
{
Size stageSize = Stage::GetCurrent().GetSize();
ImageAttributes attributes;
attributes.SetSize( stageSize.x, stageSize.y );
attributes.SetFilterMode( ImageAttributes::BoxThenLinear );
attributes.SetScalingMode( ImageAttributes::ScaleToFill );
return ResourceImage::New( imagePath, attributes );
}
}// end LOCAL_STUFF
// This example shows the usage of BubbleEmitter which displays lots of moving bubbles on the stage.
class BubbleEffectExample : public ConnectionTracker
{
public:
BubbleEffectExample(Application &app)
: mApp(app),
mHSVDelta( Vector3( 0.f, 0.f, 0.5f ) ),
mNeedNewAnimation( true ),
mTimerInterval( 16 ),
mCurrentBackgroundImageId( 0 ),
mCurrentBubbleShapeImageId( 0 )
{
// Connect to the Application's Init signal
app.InitSignal().Connect(this, &BubbleEffectExample::Create);
}
~BubbleEffectExample()
{
}
private:
// The Init signal is received once (only) during the Application lifetime
void Create(Application& app)
{
DemoHelper::RequestThemeChange();
Stage stage = Stage::GetCurrent();
Vector2 stageSize = stage.GetSize();
stage.KeyEventSignal().Connect(this, &BubbleEffectExample::OnKeyEvent);
// Creates a default view with a default tool bar.
// The view is added to the stage.
Toolkit::ToolBar toolBar;
Toolkit::View view;
Layer content = DemoHelper::CreateView( app,
view,
toolBar,
"",
TOOLBAR_IMAGE,
APPLICATION_TITLE );
// Add a button to change background. (right of toolbar)
mChangeBackgroundButton = Toolkit::PushButton::New();
mChangeBackgroundButton.SetBackgroundImage( ResourceImage::New( CHANGE_BACKGROUND_ICON ) );
mChangeBackgroundButton.ClickedSignal().Connect( this, &BubbleEffectExample::OnChangeIconClicked );
toolBar.AddControl( mChangeBackgroundButton,
DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
Toolkit::Alignment::HorizontalRight,
DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
// Add a button to change bubble shape. ( left of bar )
mChangeBubbleShapeButton = Toolkit::PushButton::New();
mChangeBubbleShapeButton.SetBackgroundImage( ResourceImage::New( CHANGE_BUBBLE_SHAPE_ICON ) );
mChangeBubbleShapeButton.ClickedSignal().Connect( this, &BubbleEffectExample::OnChangeIconClicked );
toolBar.AddControl( mChangeBubbleShapeButton,
DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
Toolkit::Alignment::HorizontalLeft,
DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
// Create and initialize the BubbleEmitter object
mBubbleEmitter = Toolkit::BubbleEmitter::New( stageSize,
ResourceImage::New( BUBBLE_SHAPE_IMAGES[mCurrentBubbleShapeImageId] ),
DEFAULT_NUMBER_OF_BUBBLES,
DEFAULT_BUBBLE_SIZE);
mBackgroundImage = LoadStageFillingImage( BACKGROUND_IMAGES[mCurrentBackgroundImageId] );
mBubbleEmitter.SetBackground( mBackgroundImage, mHSVDelta );
// Get the root actor of all bubbles, and add it to stage.
Actor bubbleRoot = mBubbleEmitter.GetRootActor();
bubbleRoot.SetParentOrigin(ParentOrigin::CENTER);
bubbleRoot.SetZ(0.1f); // Make sure the bubbles displayed on top og the background.
content.Add( bubbleRoot );
// Add the background image actor to stage
mBackgroundActor = ImageActor::New( mBackgroundImage );
view.SetBackground( mBackgroundActor );
// Set up the timer to emit bubble regularly when the finger is touched down but not moved
mTimerForBubbleEmission = Timer::New( mTimerInterval );
mTimerForBubbleEmission.TickSignal().Connect(this, &BubbleEffectExample::OnTimerTick);
// Connect the callback to the touch signal on the background
mBackgroundActor.TouchedSignal().Connect( this, &BubbleEffectExample::OnTouch );
}
/***********
* Emit bubbles
*****************/
// Set up the animation of emitting bubbles, to be efficient, every animation controls multiple bubbles ( 4 here )
void SetUpAnimation( Vector2 emitPosition, Vector2 direction )
{
if( mNeedNewAnimation )
{
float duration = Random::Range(1.f, 1.5f);
mEmitAnimation = Animation::New( duration );
mNeedNewAnimation = false;
mAnimateComponentCount = 0;
}
mBubbleEmitter.EmitBubble( mEmitAnimation, emitPosition, direction + Vector2(0.f, 30.f) /* upwards */, Vector2(300, 600) );
mAnimateComponentCount++;
if( mAnimateComponentCount % 4 ==0 )
{
mEmitAnimation.Play();
mNeedNewAnimation = true;
}
}
// Emit bubbles when the finger touches down but keep stationary.
// And stops emitting new bubble after being stationary for 2 seconds
bool OnTimerTick()
{
if(mEmitPosition == mCurrentTouchPosition) // finger is not moving
{
mNonMovementCount++;
if(mNonMovementCount < (1000 / mTimerInterval)) // 1 seconds
{
for(int i = 0; i < 4; i++) // emit 4 bubbles every timer tick
{
SetUpAnimation( mCurrentTouchPosition+Vector2(rand()%5, rand()%5), Vector2(rand()%60-30, rand()%100-50) );
}
}
}
else
{
mNonMovementCount = 0;
mEmitPosition = mCurrentTouchPosition;
}
return true;
}
// Callback function of the touch signal on the background
bool OnTouch(Dali::Actor actor, const Dali::TouchEvent& event)
{
const TouchPoint &point = event.GetPoint(0);
switch(point.state)
{
case TouchPoint::Down:
{
mCurrentTouchPosition = point.screen;
mEmitPosition = point.screen;
mTimerForBubbleEmission.Start();
mNonMovementCount = 0;
break;
}
case TouchPoint::Motion:
{
Vector2 displacement = point.screen - mCurrentTouchPosition;
mCurrentTouchPosition = point.screen;
//emit multiple bubbles along the moving direction when the finger moves quickly
float step = std::min(5.f, displacement.Length());
for( float i=0.25f; i<step; i=i+1.f)
{
SetUpAnimation( mCurrentTouchPosition+displacement*(i/step), displacement );
}
break;
}
case TouchPoint::Up:
case TouchPoint::Leave:
case TouchPoint::Interrupted:
{
mTimerForBubbleEmission.Stop();
break;
}
case TouchPoint::Stationary:
case TouchPoint::Last:
default:
{
break;
}
}
return true;
}
bool OnChangeIconClicked( Toolkit::Button button )
{
if(button == mChangeBackgroundButton)
{
mBackgroundImage = LoadStageFillingImage( BACKGROUND_IMAGES[ ++mCurrentBackgroundImageId % NUM_BACKGROUND_IMAGES ] );
mBubbleEmitter.SetBackground( mBackgroundImage, mHSVDelta );
mBackgroundActor.SetImage( mBackgroundImage );
}
else if( button == mChangeBubbleShapeButton )
{
mBubbleEmitter.SetShapeImage( ResourceImage::New( BUBBLE_SHAPE_IMAGES[ ++mCurrentBubbleShapeImageId % NUM_BUBBLE_SHAPE_IMAGES ] ) );
}
return true;
}
/**
* Main key event handler
*/
void OnKeyEvent(const KeyEvent& event)
{
if(event.state == KeyEvent::Down)
{
if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
{
mApp.Quit();
}
}
}
private:
Application& mApp;
Image mBackgroundImage;
ImageActor mBackgroundActor;
Toolkit::BubbleEmitter mBubbleEmitter;
Vector3 mHSVDelta;
Animation mEmitAnimation;
unsigned int mAnimateComponentCount;
bool mNeedNewAnimation;
Timer mTimerForBubbleEmission;
unsigned int mNonMovementCount;
unsigned int mTimerInterval;
Vector2 mCurrentTouchPosition;
Vector2 mEmitPosition;
Toolkit::PushButton mChangeBackgroundButton;
Toolkit::PushButton mChangeBubbleShapeButton;
unsigned int mCurrentBackgroundImageId;
unsigned int mCurrentBubbleShapeImageId;
};
/*****************************************************************************/
static void
RunTest(Application& app)
{
BubbleEffectExample theApp(app);
app.MainLoop();
}
/*****************************************************************************/
int
main(int argc, char **argv)
{
Application app = Application::New(&argc, &argv);
RunTest(app);
return 0;
}