Merge "Add support for new accessibility actions" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / magnifier / magnifier-impl.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 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/magnifier/magnifier-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/animation/constraint.h>
23 #include <dali/public-api/animation/constraints.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/public-api/render-tasks/render-task-list.h>
26 #include <dali/public-api/images/resource-image.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/devel-api/object/type-registry-helper.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 namespace // unnamed namespace
40 {
41
42
43 Dali::BaseHandle Create()
44 {
45   return Toolkit::Magnifier::New();
46 }
47
48 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Magnifier, Toolkit::Control, Create )
49
50 DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "frame-visibility",     BOOLEAN, FRAME_VISIBILITY     )
51 DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "magnification-factor", FLOAT,   MAGNIFICATION_FACTOR )
52
53 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, Magnifier, "source-position", VECTOR3, SOURCE_POSITION )
54
55 DALI_TYPE_REGISTRATION_END()
56
57 const char* DEFAULT_FRAME_IMAGE_PATH = DALI_IMAGE_DIR "magnifier-image-frame.png";
58
59 const float IMAGE_BORDER_INDENT = 14.0f;            ///< Indent of border in pixels.
60
61 struct CameraActorPositionConstraint
62 {
63   CameraActorPositionConstraint(const Vector2& stageSize, float defaultCameraDistance = 0.0f)
64   : mStageSize(stageSize),
65     mDefaultCameraDistance(defaultCameraDistance)
66   {
67   }
68
69   void operator()( Vector3& current, const PropertyInputContainer& inputs )
70   {
71     const Vector3& sourcePosition = inputs[0]->GetVector3();
72
73     current.x = sourcePosition.x + mStageSize.x * 0.5f;
74     current.y = sourcePosition.y + mStageSize.y * 0.5f;
75     current.z = sourcePosition.z + mDefaultCameraDistance;
76   }
77
78   Vector2 mStageSize;
79   float mDefaultCameraDistance;
80
81 };
82
83 struct RenderTaskViewportPositionConstraint
84 {
85   RenderTaskViewportPositionConstraint(const Vector2& stageSize)
86   : mStageSize(stageSize)
87   {
88   }
89
90   void operator()( Vector2& current, const PropertyInputContainer& inputs )
91   {
92     current = inputs[0]->GetVector3(); // World position?
93
94     // should be updated when:
95     // Magnifier's world position/size/scale/parentorigin/anchorpoint changes.
96     // or Magnifier camera's world position changes.
97     Vector3 size = inputs[1]->GetVector3() * inputs[2]->GetVector3(); /* magnifier-size * magnifier-scale */
98
99     // Reposition, and resize viewport to reflect the world bounds of this Magnifier actor.
100     current.x += ( mStageSize.width - size.width ) * 0.5f;
101     current.y += ( mStageSize.height - size.height ) * 0.5f;
102   }
103
104   Vector2 mStageSize;
105 };
106
107 struct RenderTaskViewportSizeConstraint
108 {
109   RenderTaskViewportSizeConstraint()
110   {
111   }
112
113   void operator()( Vector2& current, const PropertyInputContainer& inputs )
114   {
115     current = inputs[0]->GetVector3() * inputs[1]->GetVector3(); /* magnifier-size * magnifier-scale */
116   }
117 };
118
119 } // unnamed namespace
120
121 ///////////////////////////////////////////////////////////////////////////////////////////////////
122 // Magnifier
123 ///////////////////////////////////////////////////////////////////////////////////////////////////
124
125 Dali::Toolkit::Magnifier Magnifier::New()
126 {
127   // Create the implementation
128   MagnifierPtr magnifier(new Magnifier());
129
130   // Pass ownership to CustomActor via derived handle
131   Dali::Toolkit::Magnifier handle(*magnifier);
132
133   // Second-phase init of the implementation
134   // This can only be done after the CustomActor connection has been made...
135   magnifier->Initialize();
136
137   return handle;
138 }
139
140 Magnifier::Magnifier()
141 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
142   mDefaultCameraDistance(1000.f),
143   mActorSize(Vector3::ZERO),
144   mMagnificationFactor(1.0f)
145 {
146 }
147
148 void Magnifier::SetSourceActor(Actor actor)
149 {
150   mTask.SetSourceActor( actor );
151 }
152
153 void Magnifier::Initialize()
154 {
155   Actor self = Self();
156   Vector2 stageSize(Stage::GetCurrent().GetSize());
157
158   // NOTE:
159   // sourceActor is a dummy delegate actor that takes the source property position,
160   // and generates a WORLD_POSITION, which is 1 frame behind the source property.
161   // This way the constraints for determining the camera position (source) and those
162   // for determining viewport position use the same 1 frame old values.
163   // A simple i) CameraPos = f(B), ii) B = f(A) set of constraints wont suffice as
164   // although CameraPos will use B, which is A's previous value. The constraint will
165   // not realise that B is still dirty as far as constraint (i) is concerned.
166   // Perhaps this is a bug in the way the constraint system factors into what is dirty
167   // and what is not.
168   mSourceActor = Actor::New();
169   Stage().GetCurrent().Add(mSourceActor);
170   mSourceActor.SetParentOrigin(ParentOrigin::CENTER);
171   Constraint constraint = Constraint::New<Vector3>( mSourceActor, Actor::Property::POSITION, EqualToConstraint() );
172   constraint.AddSource( Source( self, Toolkit::Magnifier::Property::SOURCE_POSITION ) );
173   constraint.Apply();
174
175   // create the render task this will render content on top of everything
176   // based on camera source position.
177   InitializeRenderTask();
178
179   // set up some constraints to:
180   // i) reposition (dest) frame actor based on magnifier actor's world position (this is 1 frame delayed)
181   // ii) reposition and resize (dest) the render task's viewport based on magnifier actor's world position (1 frame delayed) & size.
182   // iii) reposition (source) camera actor based on magnifier source actor's world position (this is 1 frame delayed)
183
184   // Apply constraint to camera's position
185   // Position our camera at the same distance from its target as the default camera is.
186   // The camera position doesn't affect how we render, just what we render (due to near and far clip planes)
187   // NOTE: We can't interrogate the default camera's position as it is not known initially (takes 1 frame
188   // for value to update).
189   // But we can determine the initial position using the same formula:
190   // distance = stage.height * 0.5 / tan(FOV * 0.5)
191
192   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
193   RenderTask renderTask = taskList.GetTask(0u);
194   float fov = renderTask.GetCameraActor().GetFieldOfView();
195   mDefaultCameraDistance = (stageSize.height * 0.5f) / tanf(fov * 0.5f);
196
197   // Use a 1 frame delayed source position to determine the camera actor's position.
198   // This is necessary as the viewport is determined by the Magnifier's Actor's World position (which is computed
199   // at the end of the update cycle i.e. after constraints have been applied.)
200   //Property::Index propertySourcePositionDelayed = mCameraActor.RegisterProperty("delayed-source-position", Vector3::ZERO);
201
202   constraint = Constraint::New<Vector3>( mCameraActor, Actor::Property::POSITION, CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
203   constraint.AddSource( Source( mSourceActor, Actor::Property::WORLD_POSITION ) );
204   constraint.Apply();
205
206   // Apply constraint to render-task viewport position
207   constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_POSITION, RenderTaskViewportPositionConstraint(stageSize) );
208   constraint.AddSource( Source( self, Actor::Property::WORLD_POSITION ) );
209   constraint.AddSource( Source( self, Actor::Property::SIZE ) );
210   constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
211   constraint.Apply();
212
213   // Apply constraint to render-task viewport position
214   constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_SIZE, RenderTaskViewportSizeConstraint() );
215   constraint.AddSource( Source( self, Actor::Property::SIZE ) );
216   constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
217   constraint.Apply();
218 }
219
220 Magnifier::~Magnifier()
221 {
222
223 }
224
225 void Magnifier::InitializeRenderTask()
226 {
227   Stage stage = Stage::GetCurrent();
228
229   RenderTaskList taskList = stage.GetRenderTaskList();
230
231   mTask = taskList.CreateTask();
232   mTask.SetInputEnabled(false);
233   mTask.SetClearColor(Vector4(0.5f, 0.5f, 0.5f, 1.0f));
234   mTask.SetClearEnabled(true);
235
236   mCameraActor = CameraActor::New();
237   mCameraActor.SetType(Camera::FREE_LOOK);
238
239   stage.Add(mCameraActor);
240   mTask.SetCameraActor( mCameraActor );
241
242   SetFrameVisibility(true);
243 }
244
245 bool Magnifier::GetFrameVisibility() const
246 {
247   return mFrame;
248 }
249
250 void Magnifier::SetFrameVisibility(bool visible)
251 {
252   if(visible && !mFrame)
253   {
254     Actor self(Self());
255
256     Image image = ResourceImage::New( DEFAULT_FRAME_IMAGE_PATH );
257     mFrame = ImageActor::New( image );
258     mFrame.SetDrawMode(DrawMode::OVERLAY);
259     mFrame.SetStyle( ImageActor::STYLE_NINE_PATCH );
260     mFrame.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
261     mFrame.SetInheritScale(true);
262     mFrame.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
263     Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
264     mFrame.SetSizeModeFactor( sizeOffset );
265
266     Constraint constraint = Constraint::New<Vector3>( mFrame, Actor::Property::POSITION, EqualToConstraint() );
267     constraint.AddSource( ParentSource( Actor::Property::WORLD_POSITION ) );
268     constraint.Apply();
269
270     mFrame.SetNinePatchBorder( Vector4::ONE * IMAGE_BORDER_INDENT );
271     self.Add(mFrame);
272   }
273   else if(!visible && mFrame)
274   {
275     UnparentAndReset(mFrame);
276   }
277 }
278
279 void Magnifier::OnSizeSet(const Vector3& targetSize)
280 {
281   // TODO: Once Camera/CameraActor properties function as proper animatable properties
282   // this code can disappear.
283   // whenever the size of the magnifier changes, the field of view needs to change
284   // to compensate for the new size of the viewport. this cannot be done within
285   // a constraint yet as Camera/CameraActor properties are not animatable/constrainable.
286   mActorSize = targetSize;
287   Update();
288 }
289
290 float Magnifier::GetMagnificationFactor() const
291 {
292   return mMagnificationFactor;
293 }
294
295 void Magnifier::SetMagnificationFactor(float value)
296 {
297   mMagnificationFactor = value;
298   Update();
299 }
300
301 void Magnifier::Update()
302 {
303   // TODO: Make Camera settings (fieldofview/aspectratio) as animatable constraints.
304
305   // should be updated when:
306   // Magnifier's world size/scale changes.
307   Actor self(Self());
308   Vector3 worldSize = mActorSize * self.GetCurrentWorldScale();
309
310   // Adjust field of view to scale content
311
312   // size.height / 2
313   // |------/
314   // |d    /
315   // |i   /
316   // |s  /
317   // |t /
318   // |./
319   // |/ <--- fov/2 radians.
320   //
321   const float fov = atanf( 0.5f * worldSize.height / mDefaultCameraDistance / mMagnificationFactor) * 2.0f;
322   mCameraActor.SetFieldOfView( fov );
323
324   // Adjust aspect ratio to compensate for rectangular viewports.
325   mCameraActor.SetAspectRatio( worldSize.width / worldSize.height );
326 }
327
328 void Magnifier::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
329 {
330   Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
331
332   if( magnifier )
333   {
334     Magnifier& magnifierImpl( GetImpl( magnifier ) );
335     switch( index )
336     {
337       case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
338       {
339         magnifierImpl.SetFrameVisibility( value.Get< bool >() );
340         break;
341       }
342       case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
343       {
344         magnifierImpl.SetMagnificationFactor( value.Get< float >() );
345         break;
346       }
347     }
348   }
349 }
350
351 Property::Value Magnifier::GetProperty( BaseObject* object, Property::Index index )
352 {
353   Property::Value value;
354
355   Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
356
357   if( magnifier )
358   {
359     Magnifier& magnifierImpl( GetImpl( magnifier ) );
360     switch( index )
361     {
362       case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
363       {
364         value = magnifierImpl.GetFrameVisibility();
365         break;
366       }
367       case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
368       {
369         value = magnifierImpl.GetMagnificationFactor();
370         break;
371       }
372     }
373   }
374
375   return value;
376 }
377
378 } // namespace Internal
379
380 } // namespace Toolkit
381
382 } // namespace Dali