4575ee29edcbae64a46f1b4a4d437b7cfb25113c
[platform/core/uifw/dali-demo.git] / examples / bubble-effect / bubble-effect-example.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include "shared/view.h"
21
22 using namespace Dali;
23
24 namespace
25 {
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" );
30
31 const char* BACKGROUND_IMAGES[]=
32 {
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",
38 };
39 const unsigned int NUM_BACKGROUND_IMAGES( sizeof( BACKGROUND_IMAGES ) / sizeof( BACKGROUND_IMAGES[0] ) );
40
41 const char* BUBBLE_SHAPE_IMAGES[] =
42 {
43   DALI_IMAGE_DIR "bubble-ball.png",
44   DALI_IMAGE_DIR "icon-item-view-layout-spiral.png",
45   DALI_IMAGE_DIR "icon-replace.png",
46   DALI_IMAGE_DIR "icon-effect-cross.png"
47 };
48 const unsigned int NUM_BUBBLE_SHAPE_IMAGES( sizeof( BUBBLE_SHAPE_IMAGES ) / sizeof( BUBBLE_SHAPE_IMAGES[0] ) );
49
50 const Vector2 DEFAULT_BUBBLE_SIZE( 10.f, 30.f );
51 const unsigned int DEFAULT_NUMBER_OF_BUBBLES( 1000 );
52
53 /**
54  * @brief Load an image, scaled-down to no more than the stage dimensions.
55  *
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
59  * maximum quality.
60  */
61 ResourceImage LoadStageFillingImage( const char * const imagePath )
62 {
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 );
65 }
66
67 }// end LOCAL_STUFF
68
69 // This example shows the usage of BubbleEmitter which displays lots of moving bubbles on the stage.
70 class BubbleEffectExample : public ConnectionTracker
71 {
72 public:
73   BubbleEffectExample(Application &app)
74   : mApp(app),
75     mHSVDelta( Vector3( 0.f, 0.f, 0.5f ) ),
76     mNeedNewAnimation( true ),
77     mTimerInterval( 16 ),
78     mCurrentBackgroundImageId( 0 ),
79     mCurrentBubbleShapeImageId( 0 )
80   {
81     // Connect to the Application's Init signal
82     app.InitSignal().Connect(this, &BubbleEffectExample::Create);
83   }
84
85   ~BubbleEffectExample()
86   {
87   }
88
89 private:
90
91   // The Init signal is received once (only) during the Application lifetime
92   void Create(Application& app)
93   {
94     DemoHelper::RequestThemeChange();
95
96     Stage stage = Stage::GetCurrent();
97     Vector2 stageSize = stage.GetSize();
98
99     stage.KeyEventSignal().Connect(this, &BubbleEffectExample::OnKeyEvent);
100
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,
106                                             view,
107                                             toolBar,
108                                             "",
109                                             TOOLBAR_IMAGE,
110                                             APPLICATION_TITLE );
111
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  );
128
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 );
136
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 );
142
143     // Add the background image actor to stage
144     view.SetBackgroundImage( mBackgroundImage );
145     mBackgroundActor = ImageActor::DownCast( view.GetBackgroundActor() );
146
147     // Set up the timer to emit bubble regularly when the finger is touched down but not moved
148     mTimerForBubbleEmission = Timer::New( mTimerInterval );
149     mTimerForBubbleEmission.TickSignal().Connect(this, &BubbleEffectExample::OnTimerTick);
150
151     // Connect the callback to the touch signal on the background
152     mBackgroundActor.TouchedSignal().Connect( this, &BubbleEffectExample::OnTouch );
153   }
154
155
156 /***********
157  * Emit bubbles
158  *****************/
159
160   // Set up the animation of emitting bubbles, to be efficient, every animation controls multiple bubbles ( 4 here )
161   void SetUpAnimation( Vector2 emitPosition, Vector2 direction )
162   {
163     if( mNeedNewAnimation )
164     {
165       float duration = Random::Range(1.f, 1.5f);
166       mEmitAnimation = Animation::New( duration );
167       mNeedNewAnimation = false;
168       mAnimateComponentCount = 0;
169     }
170
171     mBubbleEmitter.EmitBubble( mEmitAnimation, emitPosition, direction + Vector2(0.f, 30.f) /* upwards */, Vector2(300, 600) );
172
173     mAnimateComponentCount++;
174
175     if( mAnimateComponentCount % 4 ==0 )
176     {
177       mEmitAnimation.Play();
178       mNeedNewAnimation = true;
179     }
180   }
181
182   // Emit bubbles when the finger touches down but keep stationary.
183   // And stops emitting new bubble after being stationary for 2 seconds
184   bool OnTimerTick()
185   {
186     if(mEmitPosition == mCurrentTouchPosition) // finger is not moving
187     {
188       mNonMovementCount++;
189       if(mNonMovementCount < (1000 / mTimerInterval)) // 1 seconds
190       {
191         for(int i = 0; i < 4; i++) // emit 4 bubbles every timer tick
192         {
193           SetUpAnimation( mCurrentTouchPosition+Vector2(rand()%5, rand()%5), Vector2(rand()%60-30, rand()%100-50) );
194         }
195       }
196     }
197     else
198     {
199       mNonMovementCount = 0;
200       mEmitPosition = mCurrentTouchPosition;
201     }
202
203     return true;
204   }
205
206   // Callback function of the touch signal on the background
207   bool OnTouch(Dali::Actor actor, const Dali::TouchEvent& event)
208   {
209     const TouchPoint &point = event.GetPoint(0);
210     switch(point.state)
211     {
212       case TouchPoint::Down:
213       {
214         mCurrentTouchPosition = point.screen;
215         mEmitPosition = point.screen;
216         mTimerForBubbleEmission.Start();
217         mNonMovementCount = 0;
218
219         break;
220       }
221       case TouchPoint::Motion:
222       {
223         Vector2 displacement = point.screen - mCurrentTouchPosition;
224         mCurrentTouchPosition = point.screen;
225         //emit multiple bubbles along the moving direction when the finger moves quickly
226         float step = std::min(5.f, displacement.Length());
227         for( float i=0.25f; i<step; i=i+1.f)
228         {
229           SetUpAnimation( mCurrentTouchPosition+displacement*(i/step), displacement );
230         }
231         break;
232       }
233       case TouchPoint::Up:
234       case TouchPoint::Leave:
235       case TouchPoint::Interrupted:
236       {
237         mTimerForBubbleEmission.Stop();
238         break;
239       }
240       case TouchPoint::Stationary:
241       case TouchPoint::Last:
242       default:
243       {
244         break;
245       }
246
247     }
248     return true;
249   }
250
251   bool OnChangeIconClicked( Toolkit::Button button )
252   {
253     if(button == mChangeBackgroundButton)
254     {
255       mBackgroundImage = LoadStageFillingImage( BACKGROUND_IMAGES[ ++mCurrentBackgroundImageId % NUM_BACKGROUND_IMAGES  ] );
256
257       mBubbleEmitter.SetBackground( mBackgroundImage, mHSVDelta );
258
259       mBackgroundActor.SetImage( mBackgroundImage );
260     }
261     else if( button == mChangeBubbleShapeButton )
262     {
263       mBubbleEmitter.SetShapeImage( ResourceImage::New( BUBBLE_SHAPE_IMAGES[ ++mCurrentBubbleShapeImageId % NUM_BUBBLE_SHAPE_IMAGES ] ) );
264     }
265     return true;
266   }
267
268   /**
269    * Main key event handler
270    */
271   void OnKeyEvent(const KeyEvent& event)
272   {
273     if(event.state == KeyEvent::Down)
274     {
275       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
276       {
277         mApp.Quit();
278       }
279     }
280   }
281
282 private:
283
284   Application&               mApp;
285   Image                      mBackgroundImage;
286   ImageActor                 mBackgroundActor;
287
288   Toolkit::BubbleEmitter     mBubbleEmitter;
289   Vector3                    mHSVDelta;
290
291   Animation                  mEmitAnimation;
292   unsigned int               mAnimateComponentCount;
293   bool                       mNeedNewAnimation;
294
295   Timer                      mTimerForBubbleEmission;
296   unsigned int               mNonMovementCount;
297   unsigned int               mTimerInterval;
298
299   Vector2                    mCurrentTouchPosition;
300   Vector2                    mEmitPosition;
301
302   Toolkit::PushButton        mChangeBackgroundButton;
303   Toolkit::PushButton        mChangeBubbleShapeButton;
304   unsigned int               mCurrentBackgroundImageId;
305   unsigned int               mCurrentBubbleShapeImageId;
306 };
307
308 /*****************************************************************************/
309
310 static void
311 RunTest(Application& app)
312 {
313   BubbleEffectExample theApp(app);
314   app.MainLoop();
315 }
316
317 /*****************************************************************************/
318
319 int
320 main(int argc, char **argv)
321 {
322   Application app = Application::New(&argc, &argv);
323
324   RunTest(app);
325
326   return 0;
327 }
328
329
330