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