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.
21 #include "shared/view.h"
23 #include <dali-toolkit/dali-toolkit.h>
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
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.
41 * MagnifierPathConstraint
42 * This constraint governs the position of the
43 * animating magnifier in a swirly pattern around
46 struct MagnifierPathConstraint
49 * Constraint constructor
50 * @param[in] stageSize The stage size so that the constraint can create a path
51 * within stage bounds.
53 MagnifierPathConstraint(const Vector3& stageSize,
54 Vector3 offset = Vector3::ZERO)
55 : mStageSize(stageSize),
60 Vector3 operator()(const Vector3& current,
61 const PropertyInput& sizeProperty,
62 const PropertyInput& animationTimeProperty)
64 float time = animationTimeProperty.GetFloat();
65 const Vector3& size = sizeProperty.GetVector3();
67 Vector3 range(mStageSize - size - Vector3::ONE * MAGNIFIER_INDENT * 2.0f);
68 Vector3 position(mOffset);
70 position.x += 0.5f * sinf(time * 0.471f) * range.width;
71 position.y += 0.5f * sinf(time * 0.8739f) * range.height;
76 Vector3 mStageSize; ///< Keep track of the stage size for determining path within stage bounds
77 Vector3 mOffset; ///< Amount to offset magnifier path
81 * Confine Actor to boundaries of reference actor (e.g. Parent)
82 * Actor bounds (top-left position + size) are confined to reference Actor's
85 struct ConfinementConstraint
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)
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)
106 Vector3 operator()(const Vector3& constPosition,
107 const PropertyInput& sizeProperty,
108 const PropertyInput& parentOriginProperty,
109 const PropertyInput& anchorPointProperty,
110 const PropertyInput& referenceSizeProperty)
112 const Vector3& size = sizeProperty.GetVector3();
113 const Vector3 origin = parentOriginProperty.GetVector3();
114 const Vector3& anchor = anchorPointProperty.GetVector3();
115 const Vector3& referenceSize = referenceSizeProperty.GetVector3();
117 Vector3 offset(mOffsetOrigin * referenceSize);
119 Vector3 newPosition( constPosition + offset );
121 // Get actual position of Actor relative to parent's Top-Left.
122 Vector3 position(constPosition + offset + origin * referenceSize);
124 // if top-left corner is outside of Top-Left bounds, then push back in screen.
125 Vector3 corner(position - size * anchor - mMinIndent);
127 if(mFlipHorizontal && corner.x < 0.0f)
130 newPosition.x += size.width;
133 if(mFlipVertical && corner.y < 0.0f)
136 newPosition.y += size.height;
139 newPosition.x -= std::min(corner.x, 0.0f);
140 newPosition.y -= std::min(corner.y, 0.0f);
142 // if bottom-right corner is outside of Bottom-Right bounds, then push back in screen.
143 corner += size - referenceSize + mMinIndent + mMaxIndent;
145 if(mFlipHorizontal && corner.x > 0.0f)
148 newPosition.x -= size.width;
151 if(mFlipVertical && corner.y > 0.0f)
154 newPosition.y -= size.height;
157 newPosition.x -= std::max(corner.x, 0.0f);
158 newPosition.y -= std::max(corner.y, 0.0f);
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
172 // This example shows how to use the Magnifier component.
174 class ExampleController : public ConnectionTracker
179 * The example controller constructor.
180 * @param[in] application The application instance
182 ExampleController( Application& application )
183 : mApplication( application ),
185 mAnimationTime(0.0f),
186 mMagnifierShown(false)
188 // Connect to the Application's Init signal
189 mApplication.InitSignal().Connect( this, &ExampleController::Create );
193 * The example controller destructor
197 // Nothing to do here;
201 * Invoked upon creation of application
202 * @param[in] application The application instance
204 void Create( Application& application )
206 Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
208 mStageSize = Stage::GetCurrent().GetSize();
210 // The Init signal is received once (only) during the Application lifetime
212 // Hide the indicator bar
213 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
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,
225 mContent.SetLeaveRequired(true);
226 mContent.TouchedSignal().Connect( this, &ExampleController::OnTouched );
228 // Create magnifier (controlled by human touch)
229 Layer overlay = Layer::New();
230 overlay.SetSensitive(false);
231 overlay.SetParentOrigin( ParentOrigin::CENTER );
232 overlay.SetSize(mStageSize);
233 Stage::GetCurrent().Add(overlay);
235 mMagnifier = Toolkit::Magnifier::New();
236 mMagnifier.SetSourceActor( mView.GetBackgroundLayer() );
237 mMagnifier.SetSize( MAGNIFIER_SIZE * mStageSize.width ); // Size of magnifier is in relation to stage width
238 mMagnifier.SetMagnificationFactor( MAGNIFICATION_FACTOR );
239 mMagnifier.SetScale(Vector3::ZERO);
240 overlay.Add( mMagnifier );
242 // Apply constraint to animate the position of the magnifier.
243 Constraint constraint = Constraint::New<Vector3>(Actor::POSITION,
244 LocalSource(Actor::SIZE),
245 LocalSource(Actor::PARENT_ORIGIN),
246 LocalSource(Actor::ANCHOR_POINT),
247 ParentSource(Actor::SIZE),
248 ConfinementConstraint(ParentOrigin::CENTER, Vector2::ONE * MAGNIFIER_INDENT, Vector2::ONE * MAGNIFIER_INDENT));
249 constraint.SetRemoveAction(Constraint::Discard);
250 mMagnifier.ApplyConstraint( constraint );
252 // Create bouncing magnifier automatically bounces around screen.
253 mBouncingMagnifier = Toolkit::Magnifier::New();
254 mBouncingMagnifier.SetSourceActor( mView.GetBackgroundLayer() );
255 mBouncingMagnifier.SetSize( MAGNIFIER_SIZE * mStageSize.width ); // Size of magnifier is in relation to stage width
256 mBouncingMagnifier.SetMagnificationFactor( MAGNIFICATION_FACTOR );
257 overlay.Add( mBouncingMagnifier );
259 mAnimationTimeProperty = mBouncingMagnifier.RegisterProperty("animation-time", 0.0f);
262 // Apply constraint to animate the position of the magnifier.
263 constraint = Constraint::New<Vector3>(Actor::POSITION,
264 LocalSource(Actor::SIZE),
265 LocalSource(mAnimationTimeProperty),
266 MagnifierPathConstraint(mStageSize, mStageSize * 0.5f));
267 mBouncingMagnifier.ApplyConstraint( constraint );
269 // Apply constraint to animate the source of the magnifier.
270 constraint = Constraint::New<Vector3>(mBouncingMagnifier.GetPropertyIndex( Toolkit::Magnifier::SOURCE_POSITION_PROPERTY_NAME ),
271 LocalSource(Actor::SIZE),
272 LocalSource(mAnimationTimeProperty),
273 MagnifierPathConstraint(mStageSize));
274 mBouncingMagnifier.ApplyConstraint( constraint );
278 * Invoked whenever the animation finishes (every 60 seconds)
279 * @param[in] animation The animation
281 void OnAnimationFinished( Animation& animation )
283 animation.FinishedSignal().Disconnect(this, &ExampleController::OnAnimationFinished);
289 * Resumes animation for another ANIMATION_DURATION seconds.
291 void ContinueAnimation()
293 Animation animation = Animation::New(ANIMATION_DURATION);
294 mAnimationTime += ANIMATION_DURATION;
295 animation.AnimateTo( Property(mBouncingMagnifier, mAnimationTimeProperty), mAnimationTime );
297 animation.FinishedSignal().Connect(this, &ExampleController::OnAnimationFinished);
301 * Invoked whenever the quit button is clicked
302 * @param[in] button the quit button
304 bool OnQuitButtonClicked( Toolkit::Button button )
306 // quit the application
312 * Invoked whenever the content (screen) is touched
313 * @param[in] actor The actor that received the touch
314 * @param[in] event The touch-event information
316 bool OnTouched( Actor actor, const TouchEvent& event )
318 if(event.GetPointCount() > 0)
320 const TouchPoint& point = event.GetPoint(0);
323 case TouchPoint::Down:
324 case TouchPoint::Motion:
330 case TouchPoint::Leave:
331 case TouchPoint::Interrupted:
342 Vector3 touchPoint(point.screen);
344 SetMagnifierPosition(touchPoint - mStageSize * 0.5f);
351 * Shows the magnifier
357 Animation animation = Animation::New(MAGNIFIER_DISPLAY_DURATION);
358 animation.AnimateTo(Property(mMagnifier, Actor::SCALE), Vector3::ONE, AlphaFunctions::EaseIn);
360 mMagnifierShown = true;
365 * Hides the magnifier
371 Animation animation = Animation::New(MAGNIFIER_DISPLAY_DURATION);
372 animation.AnimateTo(Property(mMagnifier, Actor::SCALE), Vector3::ZERO, AlphaFunctions::EaseOut);
374 mMagnifierShown = false;
379 * Manually sets the magnifier position
380 * @param[in] position The magnifier's position relative to center of stage
382 void SetMagnifierPosition(const Vector3 position)
384 mMagnifier.SetSourcePosition( position );
386 // position magnifier glass such that bottom edge is touching/near top of finger.
387 Vector3 glassPosition(position);
388 glassPosition.y -= mStageSize.width * MAGNIFIER_SIZE.height * 0.5f + Stage::GetCurrent().GetDpi().height * FINGER_RADIUS_INCHES;
390 mMagnifier.SetPosition( glassPosition );
393 void OnKeyEvent(const KeyEvent& event)
395 if(event.state == KeyEvent::Down)
397 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
406 Application& mApplication; ///< Application instance
407 Toolkit::View mView; ///< The view
408 Layer mContent; ///< The content layer
409 Toolkit::Magnifier mMagnifier; ///< The manually controlled magnifier
410 Toolkit::Magnifier mBouncingMagnifier; ///< The animating magnifier (swirly animation)
411 Vector3 mStageSize; ///< The size of the stage
412 float mAnimationTime; ///< Keep track of start animation time.
413 Property::Index mAnimationTimeProperty; ///< Animation time property (responsible for swirly animation)
414 bool mMagnifierShown; ///< Flag indicating whether the magnifier is being shown or not.
418 void RunTest( Application& application )
420 ExampleController test( application );
422 application.MainLoop();
425 // Entry point for Linux & SLP applications
427 int main( int argc, char **argv )
429 Application application = Application::New( &argc, &argv );
431 RunTest( application );