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