[dali_1.0.36] Merge branch 'tizen'
[platform/core/uifw/dali-demo.git] / examples / magnifier / magnifier-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 // EXTERNAL INCLUDES
19
20 // INTERNAL INCLUDES
21 #include "shared/view.h"
22
23 #include <dali-toolkit/dali-toolkit.h>
24
25 using namespace Dali;
26
27 namespace
28 {
29 const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-magnifier.jpg" );
30 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
31 const char* APPLICATION_TITLE( "Magnifier Example" );
32 const Vector3 MAGNIFIER_SIZE(0.25f, 0.25f, 0.0f);       ///< Magnifier sides should be 25% of the width of the stage
33 const float ANIMATION_DURATION(60.0f);                  ///< Run animation for a minute before repeating.
34 const float MAGNIFIER_DISPLAY_DURATION(0.125f);         ///< Duration in seconds for show/hide manual magnifier animation
35
36 const float MAGNIFICATION_FACTOR(2.0f);                 ///< Amount to magnify by.
37 const float MAGNIFIER_INDENT(10.0f);                    ///< Indentation around edge of stage to define where magnifiers may move.
38 const float FINGER_RADIUS_INCHES(0.25f);                ///< Average finger radius in inches from the center of index finger to edge.
39
40 /**
41  * MagnifierPathConstraint
42  * This constraint governs the position of the
43  * animating magnifier in a swirly pattern around
44  * the stage.
45  */
46 struct MagnifierPathConstraint
47 {
48   /**
49    * Constraint constructor
50    * @param[in] stageSize The stage size so that the constraint can create a path
51    * within stage bounds.
52    */
53   MagnifierPathConstraint(const Vector3& stageSize,
54                           Vector3 offset = Vector3::ZERO)
55   : mStageSize(stageSize),
56     mOffset(offset)
57   {
58   }
59
60   Vector3 operator()(const Vector3&    current,
61                      const PropertyInput& sizeProperty,
62                      const PropertyInput& animationTimeProperty)
63   {
64     float time = animationTimeProperty.GetFloat();
65     const Vector3& size = sizeProperty.GetVector3();
66
67     Vector3 range(mStageSize - size - Vector3::ONE * MAGNIFIER_INDENT * 2.0f);
68     Vector3 position(mOffset);
69
70     position.x += 0.5f * sinf(time * 0.471f) * range.width;
71     position.y += 0.5f * sinf(time * 0.8739f) * range.height;
72
73     return position;
74   }
75
76   Vector3 mStageSize;     ///< Keep track of the stage size for determining path within stage bounds
77   Vector3 mOffset;        ///< Amount to offset magnifier path
78 };
79
80 /**
81  * Confine Actor to boundaries of reference actor (e.g. Parent)
82  * Actor bounds (top-left position + size) are confined to reference Actor's
83  * bounds.
84  */
85 struct ConfinementConstraint
86 {
87   /**
88    * Confinement constraint constructor.
89    * @param[in] offsetOrigin (optional) Whether to offset the parent origin or not.
90    * @param[in] topLeftMargin (optional) Top-Left margins (defaults to 0.0f, 0.0f)
91    * @param[in] bottomRightMargin (optional) Bottom-Right margins (defaults to 0.0f, 0.0f)
92    * @param[in] flipHorizontal (optional) whether to flip Actor to the other side X if near edge, and by
93    * how much (defaults to 0.0f i.e. no flip)
94    * @param[in] flipVertical (optional) whether to flip Actor to the other side Y if near edge, and by
95    * how much (defaults to 0.0f i.e. no flip)
96    */
97   ConfinementConstraint(Vector3 offsetOrigin = Vector3::ZERO, Vector2 topLeftMargin = Vector2::ZERO, Vector2 bottomRightMargin = Vector2::ZERO, bool flipHorizontal = false, bool flipVertical = false)
98   : mOffsetOrigin(offsetOrigin),
99     mMinIndent(topLeftMargin),
100     mMaxIndent(bottomRightMargin),
101     mFlipHorizontal(flipHorizontal),
102     mFlipVertical(flipVertical)
103   {
104   }
105
106   Vector3 operator()(const Vector3&    constPosition,
107                      const PropertyInput& sizeProperty,
108                      const PropertyInput& parentOriginProperty,
109                      const PropertyInput& anchorPointProperty,
110                      const PropertyInput& referenceSizeProperty)
111   {
112     const Vector3& size = sizeProperty.GetVector3();
113     const Vector3 origin = parentOriginProperty.GetVector3();
114     const Vector3& anchor = anchorPointProperty.GetVector3();
115     const Vector3& referenceSize = referenceSizeProperty.GetVector3();
116
117     Vector3 offset(mOffsetOrigin * referenceSize);
118
119     Vector3 newPosition( constPosition + offset );
120
121     // Get actual position of Actor relative to parent's Top-Left.
122     Vector3 position(constPosition + offset + origin * referenceSize);
123
124     // if top-left corner is outside of Top-Left bounds, then push back in screen.
125     Vector3 corner(position - size * anchor - mMinIndent);
126
127     if(mFlipHorizontal && corner.x < 0.0f)
128     {
129       corner.x = 0.0f;
130       newPosition.x += size.width;
131     }
132
133     if(mFlipVertical && corner.y < 0.0f)
134     {
135       corner.y = 0.0f;
136       newPosition.y += size.height;
137     }
138
139     newPosition.x -= std::min(corner.x, 0.0f);
140     newPosition.y -= std::min(corner.y, 0.0f);
141
142     // if bottom-right corner is outside of Bottom-Right bounds, then push back in screen.
143     corner += size - referenceSize + mMinIndent + mMaxIndent;
144
145     if(mFlipHorizontal && corner.x > 0.0f)
146     {
147       corner.x = 0.0f;
148       newPosition.x -= size.width;
149     }
150
151     if(mFlipVertical && corner.y > 0.0f)
152     {
153       corner.y = 0.0f;
154       newPosition.y -= size.height;
155     }
156
157     newPosition.x -= std::max(corner.x, 0.0f);
158     newPosition.y -= std::max(corner.y, 0.0f);
159
160     return newPosition;
161   }
162
163   Vector3 mOffsetOrigin;                                ///< Manual Parent Offset Origin.
164   Vector3 mMinIndent;                                   ///< Top-Left Margin
165   Vector3 mMaxIndent;                                   ///< Bottom-Right Margin.
166   bool mFlipHorizontal;                                 ///< Whether to flip actor's position if exceeds horizontal screen bounds
167   bool mFlipVertical;                                   ///< Whether to flip actor's position if exceeds vertical screen bounds
168 };
169
170 }
171
172 // This example shows how to use the Magnifier component.
173 //
174 class ExampleController : public ConnectionTracker
175 {
176 public:
177
178   /**
179    * The example controller constructor.
180    * @param[in] application The application instance
181    */
182   ExampleController( Application& application )
183   : mApplication( application ),
184     mView(),
185     mAnimationTime(0.0f),
186     mMagnifierShown(false)
187   {
188     // Connect to the Application's Init signal
189     mApplication.InitSignal().Connect( this, &ExampleController::Create );
190   }
191
192   /**
193    * The example controller destructor
194    */
195   ~ExampleController()
196   {
197     // Nothing to do here;
198   }
199
200   /**
201    * Invoked upon creation of application
202    * @param[in] application The application instance
203    */
204   void Create( Application& application )
205   {
206     Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
207
208     mStageSize = Stage::GetCurrent().GetSize();
209
210     // The Init signal is received once (only) during the Application lifetime
211
212     // Hide the indicator bar
213     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
214
215     // Creates a default view with a default tool bar.
216     // The view is added to the stage.
217     Toolkit::ToolBar toolBar;
218     mContent = DemoHelper::CreateView( application,
219                                        mView,
220                                        toolBar,
221                                        BACKGROUND_IMAGE,
222                                        TOOLBAR_IMAGE,
223                                        APPLICATION_TITLE );
224
225     mContent.SetLeaveRequired(true);
226     mContent.TouchedSignal().Connect( this, &ExampleController::OnTouched );
227
228     // Create magnifier (controlled by human touch)
229     Layer overlay = Layer::New();
230     overlay.SetRelayoutEnabled( false );
231     overlay.SetSensitive(false);
232     overlay.SetParentOrigin( ParentOrigin::CENTER );
233     overlay.SetSize(mStageSize);
234     Stage::GetCurrent().Add(overlay);
235
236     mMagnifier = Toolkit::Magnifier::New();
237     mMagnifier.SetRelayoutEnabled( false );
238     mMagnifier.SetSourceActor( mView.GetBackgroundLayer() );
239     mMagnifier.SetSize( MAGNIFIER_SIZE * mStageSize.width );  // Size of magnifier is in relation to stage width
240     mMagnifier.SetMagnificationFactor( MAGNIFICATION_FACTOR );
241     mMagnifier.SetScale(Vector3::ZERO);
242     overlay.Add( mMagnifier );
243
244     // Apply constraint to animate the position of the magnifier.
245     Constraint constraint = Constraint::New<Vector3>(Actor::Property::POSITION,
246                                                      LocalSource(Actor::Property::SIZE),
247                                                      LocalSource(Actor::Property::PARENT_ORIGIN),
248                                                      LocalSource(Actor::Property::ANCHOR_POINT),
249                                                      ParentSource(Actor::Property::SIZE),
250                                                      ConfinementConstraint(ParentOrigin::CENTER, Vector2::ONE * MAGNIFIER_INDENT, Vector2::ONE * MAGNIFIER_INDENT));
251     constraint.SetRemoveAction(Constraint::Discard);
252     mMagnifier.ApplyConstraint( constraint );
253
254     // Create bouncing magnifier automatically bounces around screen.
255     mBouncingMagnifier = Toolkit::Magnifier::New();
256     mBouncingMagnifier.SetRelayoutEnabled( false );
257     mBouncingMagnifier.SetSourceActor( mView.GetBackgroundLayer() );
258     mBouncingMagnifier.SetSize( MAGNIFIER_SIZE * mStageSize.width ); // Size of magnifier is in relation to stage width
259     mBouncingMagnifier.SetMagnificationFactor( MAGNIFICATION_FACTOR );
260     overlay.Add( mBouncingMagnifier );
261
262     mAnimationTimeProperty = mBouncingMagnifier.RegisterProperty("animation-time", 0.0f);
263     ContinueAnimation();
264
265     // Apply constraint to animate the position of the magnifier.
266     constraint = Constraint::New<Vector3>(Actor::Property::POSITION,
267                                           LocalSource(Actor::Property::SIZE),
268                                           LocalSource(mAnimationTimeProperty),
269                                           MagnifierPathConstraint(mStageSize, mStageSize * 0.5f));
270     mBouncingMagnifier.ApplyConstraint( constraint );
271
272     // Apply constraint to animate the source of the magnifier.
273     constraint = Constraint::New<Vector3>(mBouncingMagnifier.GetPropertyIndex( Toolkit::Magnifier::SOURCE_POSITION_PROPERTY_NAME ),
274                                           LocalSource(Actor::Property::SIZE),
275                                           LocalSource(mAnimationTimeProperty),
276                                           MagnifierPathConstraint(mStageSize));
277     mBouncingMagnifier.ApplyConstraint( constraint );
278   }
279
280   /**
281    * Invoked whenever the animation finishes (every 60 seconds)
282    * @param[in] animation The animation
283    */
284   void OnAnimationFinished( Animation& animation )
285   {
286     animation.FinishedSignal().Disconnect(this, &ExampleController::OnAnimationFinished);
287     animation.Clear();
288     ContinueAnimation();
289   }
290
291   /**
292    * Resumes animation for another ANIMATION_DURATION seconds.
293    */
294   void ContinueAnimation()
295   {
296     Animation animation = Animation::New(ANIMATION_DURATION);
297     mAnimationTime += ANIMATION_DURATION;
298     animation.AnimateTo( Property(mBouncingMagnifier, mAnimationTimeProperty), mAnimationTime );
299     animation.Play();
300     animation.FinishedSignal().Connect(this, &ExampleController::OnAnimationFinished);
301   }
302
303   /**
304    * Invoked whenever the quit button is clicked
305    * @param[in] button the quit button
306    */
307   bool OnQuitButtonClicked( Toolkit::Button button )
308   {
309     // quit the application
310     mApplication.Quit();
311     return true;
312   }
313
314   /**
315    * Invoked whenever the content (screen) is touched
316    * @param[in] actor The actor that received the touch
317    * @param[in] event The touch-event information
318    */
319   bool OnTouched( Actor actor, const TouchEvent& event )
320   {
321     if(event.GetPointCount() > 0)
322     {
323       const TouchPoint& point = event.GetPoint(0);
324       switch(point.state)
325       {
326         case TouchPoint::Down:
327         case TouchPoint::Motion:
328         {
329           ShowMagnifier();
330           break;
331         }
332         case TouchPoint::Up:
333         case TouchPoint::Leave:
334         case TouchPoint::Interrupted:
335         {
336           HideMagnifier();
337           break;
338         }
339         default:
340         {
341           break;
342         }
343       } // end switch
344
345       Vector3 touchPoint(point.screen);
346
347       SetMagnifierPosition(touchPoint - mStageSize * 0.5f);
348     }
349
350     return false;
351   }
352
353   /**
354    * Shows the magnifier
355    */
356   void ShowMagnifier()
357   {
358     if(!mMagnifierShown)
359     {
360       Animation animation = Animation::New(MAGNIFIER_DISPLAY_DURATION);
361       animation.AnimateTo(Property(mMagnifier, Actor::Property::SCALE), Vector3::ONE, AlphaFunctions::EaseIn);
362       animation.Play();
363       mMagnifierShown = true;
364     }
365   }
366
367   /**
368    * Hides the magnifier
369    */
370   void HideMagnifier()
371   {
372     if(mMagnifierShown)
373     {
374       Animation animation = Animation::New(MAGNIFIER_DISPLAY_DURATION);
375       animation.AnimateTo(Property(mMagnifier, Actor::Property::SCALE), Vector3::ZERO, AlphaFunctions::EaseOut);
376       animation.Play();
377       mMagnifierShown = false;
378     }
379   }
380
381   /**
382    * Manually sets the magnifier position
383    * @param[in] position The magnifier's position relative to center of stage
384    */
385   void SetMagnifierPosition(const Vector3 position)
386   {
387     mMagnifier.SetSourcePosition( position );
388
389     // position magnifier glass such that bottom edge is touching/near top of finger.
390     Vector3 glassPosition(position);
391     glassPosition.y -= mStageSize.width * MAGNIFIER_SIZE.height * 0.5f + Stage::GetCurrent().GetDpi().height * FINGER_RADIUS_INCHES;
392
393     mMagnifier.SetPosition( glassPosition );
394   }
395
396   void OnKeyEvent(const KeyEvent& event)
397   {
398     if(event.state == KeyEvent::Down)
399     {
400       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
401       {
402         mApplication.Quit();
403       }
404     }
405   }
406
407 private:
408
409   Application&  mApplication;                             ///< Application instance
410   Toolkit::View mView;                                    ///< The view
411   Layer mContent;                                         ///< The content layer
412   Toolkit::Magnifier mMagnifier;                          ///< The manually controlled magnifier
413   Toolkit::Magnifier mBouncingMagnifier;                  ///< The animating magnifier (swirly animation)
414   Vector3 mStageSize;                                     ///< The size of the stage
415   float mAnimationTime;                                   ///< Keep track of start animation time.
416   Property::Index mAnimationTimeProperty;                 ///< Animation time property (responsible for swirly animation)
417   bool mMagnifierShown;                                   ///< Flag indicating whether the magnifier is being shown or not.
418
419 };
420
421 void RunTest( Application& application )
422 {
423   ExampleController test( application );
424
425   application.MainLoop();
426 }
427
428 // Entry point for Linux & Tizen applications
429 //
430 int main( int argc, char **argv )
431 {
432   Application application = Application::New( &argc, &argv );
433
434   RunTest( application );
435
436   return 0;
437 }