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