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