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