Internal::Control cleanup, remove dead an non-needed methods and adding missing test...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / magnifier / magnifier-impl.cpp
1 /*
2  * Copyright (c) 2014 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
28 using namespace Dali;
29
30 namespace // unnamed namespace
31 {
32
33 const char* DEFAULT_FRAME_IMAGE_PATH = DALI_IMAGE_DIR "magnifier-image-frame.png";
34
35 const float IMAGE_BORDER_INDENT = 14.0f;            ///< Indent of border in pixels.
36
37 struct CameraActorPositionConstraint
38 {
39   CameraActorPositionConstraint(const Vector2& stageSize, float defaultCameraDistance = 0.0f)
40   : mStageSize(stageSize),
41     mDefaultCameraDistance(defaultCameraDistance)
42   {
43   }
44
45   void operator()( Vector3& current, const PropertyInputContainer& inputs )
46   {
47     const Vector3& sourcePosition = inputs[0]->GetVector3();
48
49     current.x = sourcePosition.x + mStageSize.x * 0.5f;
50     current.y = sourcePosition.y + mStageSize.y * 0.5f;
51     current.z = sourcePosition.z + mDefaultCameraDistance;
52   }
53
54   Vector2 mStageSize;
55   float mDefaultCameraDistance;
56
57 };
58
59 struct RenderTaskViewportPositionConstraint
60 {
61   RenderTaskViewportPositionConstraint(const Vector2& stageSize)
62   : mStageSize(stageSize)
63   {
64   }
65
66   void operator()( Vector2& current, const PropertyInputContainer& inputs )
67   {
68     current = inputs[0]->GetVector3(); // World position?
69
70     // should be updated when:
71     // Magnifier's world position/size/scale/parentorigin/anchorpoint changes.
72     // or Magnifier camera's world position changes.
73     Vector3 size = inputs[1]->GetVector3() * inputs[2]->GetVector3(); /* magnifier-size * magnifier-scale */
74
75     // Reposition, and resize viewport to reflect the world bounds of this Magnifier actor.
76     current.x += ( mStageSize.width - size.width ) * 0.5f;
77     current.y += ( mStageSize.height - size.height ) * 0.5f;
78   }
79
80   Vector2 mStageSize;
81 };
82
83 struct RenderTaskViewportSizeConstraint
84 {
85   RenderTaskViewportSizeConstraint()
86   {
87   }
88
89   void operator()( Vector2& current, const PropertyInputContainer& inputs )
90   {
91     current = inputs[0]->GetVector3() * inputs[1]->GetVector3(); /* magnifier-size * magnifier-scale */
92   }
93 };
94
95 }
96
97 namespace Dali
98 {
99
100 namespace Toolkit
101 {
102
103 namespace Internal
104 {
105
106 ///////////////////////////////////////////////////////////////////////////////////////////////////
107 // Magnifier
108 ///////////////////////////////////////////////////////////////////////////////////////////////////
109
110 Dali::Toolkit::Magnifier Magnifier::New()
111 {
112   // Create the implementation
113   MagnifierPtr magnifier(new Magnifier());
114
115   // Pass ownership to CustomActor via derived handle
116   Dali::Toolkit::Magnifier handle(*magnifier);
117
118   // Second-phase init of the implementation
119   // This can only be done after the CustomActor connection has been made...
120   magnifier->Initialize();
121
122   return handle;
123 }
124
125 Magnifier::Magnifier()
126 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
127   mPropertySourcePosition(Property::INVALID_INDEX),
128   mDefaultCameraDistance(1000.f),
129   mActorSize(Vector3::ZERO),
130   mMagnificationFactor(1.0f)
131 {
132 }
133
134 void Magnifier::SetSourceActor(Actor actor)
135 {
136   mTask.SetSourceActor( actor );
137 }
138
139 void Magnifier::SetSourcePosition(const Vector3& position)
140 {
141   Self().SetProperty(mPropertySourcePosition, position);
142 }
143
144 void Magnifier::Initialize()
145 {
146   Actor self = Self();
147   mPropertySourcePosition = self.RegisterProperty( Toolkit::Magnifier::SOURCE_POSITION_PROPERTY_NAME, Vector3::ZERO );
148   Vector2 stageSize(Stage::GetCurrent().GetSize());
149
150   // NOTE:
151   // sourceActor is a dummy delegate actor that takes the source property position,
152   // and generates a WORLD_POSITION, which is 1 frame behind the source property.
153   // This way the constraints for determining the camera position (source) and those
154   // for determining viewport position use the same 1 frame old values.
155   // A simple i) CameraPos = f(B), ii) B = f(A) set of constraints wont suffice as
156   // although CameraPos will use B, which is A's previous value. The constraint will
157   // not realise that B is still dirty as far as constraint (i) is concerned.
158   // Perhaps this is a bug in the way the constraint system factors into what is dirty
159   // and what is not.
160   mSourceActor = Actor::New();
161   Stage().GetCurrent().Add(mSourceActor);
162   mSourceActor.SetParentOrigin(ParentOrigin::CENTER);
163   Constraint constraint = Constraint::New<Vector3>( mSourceActor, Actor::Property::POSITION, EqualToConstraint() );
164   constraint.AddSource( Source( self, mPropertySourcePosition ) );
165   constraint.Apply();
166
167   // create the render task this will render content on top of everything
168   // based on camera source position.
169   InitializeRenderTask();
170
171   // set up some constraints to:
172   // i) reposition (dest) frame actor based on magnifier actor's world position (this is 1 frame delayed)
173   // ii) reposition and resize (dest) the render task's viewport based on magnifier actor's world position (1 frame delayed) & size.
174   // iii) reposition (source) camera actor based on magnifier source actor's world position (this is 1 frame delayed)
175
176   // Apply constraint to camera's position
177   // Position our camera at the same distance from its target as the default camera is.
178   // The camera position doesn't affect how we render, just what we render (due to near and far clip planes)
179   // NOTE: We can't interrogate the default camera's position as it is not known initially (takes 1 frame
180   // for value to update).
181   // But we can determine the initial position using the same formula:
182   // distance = stage.height * 0.5 / tan(FOV * 0.5)
183
184   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
185   RenderTask renderTask = taskList.GetTask(0u);
186   float fov = renderTask.GetCameraActor().GetFieldOfView();
187   mDefaultCameraDistance = (stageSize.height * 0.5f) / tanf(fov * 0.5f);
188
189   // Use a 1 frame delayed source position to determine the camera actor's position.
190   // This is necessary as the viewport is determined by the Magnifier's Actor's World position (which is computed
191   // at the end of the update cycle i.e. after constraints have been applied.)
192   //Property::Index propertySourcePositionDelayed = mCameraActor.RegisterProperty("delayed-source-position", Vector3::ZERO);
193
194   constraint = Constraint::New<Vector3>( mCameraActor, Actor::Property::POSITION, CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
195   constraint.AddSource( Source( mSourceActor, Actor::Property::WORLD_POSITION ) );
196   constraint.Apply();
197
198   // Apply constraint to render-task viewport position
199   constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_POSITION, RenderTaskViewportPositionConstraint(stageSize) );
200   constraint.AddSource( Source( self, Actor::Property::WORLD_POSITION ) );
201   constraint.AddSource( Source( self, Actor::Property::SIZE ) );
202   constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
203   constraint.Apply();
204
205   // Apply constraint to render-task viewport position
206   constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_SIZE, RenderTaskViewportSizeConstraint() );
207   constraint.AddSource( Source( self, Actor::Property::SIZE ) );
208   constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
209   constraint.Apply();
210 }
211
212 Magnifier::~Magnifier()
213 {
214
215 }
216
217 void Magnifier::InitializeRenderTask()
218 {
219   Stage stage = Stage::GetCurrent();
220
221   RenderTaskList taskList = stage.GetRenderTaskList();
222
223   mTask = taskList.CreateTask();
224   mTask.SetInputEnabled(false);
225   mTask.SetClearColor(Vector4(0.5f, 0.5f, 0.5f, 1.0f));
226   mTask.SetClearEnabled(true);
227
228   mCameraActor = CameraActor::New();
229   mCameraActor.SetType(Camera::FREE_LOOK);
230
231   stage.Add(mCameraActor);
232   mTask.SetCameraActor( mCameraActor );
233
234   SetFrameVisibility(true);
235 }
236
237 bool Magnifier::GetFrameVisibility() const
238 {
239   return mFrame;
240 }
241
242 void Magnifier::SetFrameVisibility(bool visible)
243 {
244   if(visible && !mFrame)
245   {
246     Actor self(Self());
247
248     Image image = ResourceImage::New( DEFAULT_FRAME_IMAGE_PATH );
249     mFrame = ImageActor::New( image );
250     mFrame.SetDrawMode(DrawMode::OVERLAY);
251     mFrame.SetStyle( ImageActor::STYLE_NINE_PATCH );
252     mFrame.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
253     mFrame.SetInheritScale(true);
254     mFrame.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
255     Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
256     mFrame.SetSizeModeFactor( sizeOffset );
257
258     Constraint constraint = Constraint::New<Vector3>( mFrame, Actor::Property::POSITION, EqualToConstraint() );
259     constraint.AddSource( ParentSource( Actor::Property::WORLD_POSITION ) );
260     constraint.Apply();
261
262     mFrame.SetNinePatchBorder( Vector4::ONE * IMAGE_BORDER_INDENT );
263     self.Add(mFrame);
264   }
265   else if(!visible && mFrame)
266   {
267     UnparentAndReset(mFrame);
268   }
269 }
270
271 void Magnifier::OnSizeSet(const Vector3& targetSize)
272 {
273   // TODO: Once Camera/CameraActor properties function as proper animatable properties
274   // this code can disappear.
275   // whenever the size of the magnifier changes, the field of view needs to change
276   // to compensate for the new size of the viewport. this cannot be done within
277   // a constraint yet as Camera/CameraActor properties are not animatable/constrainable.
278   mActorSize = targetSize;
279   Update();
280 }
281
282 float Magnifier::GetMagnificationFactor() const
283 {
284   return mMagnificationFactor;
285 }
286
287 void Magnifier::SetMagnificationFactor(float value)
288 {
289   mMagnificationFactor = value;
290   Update();
291 }
292
293 void Magnifier::Update()
294 {
295   // TODO: Make Camera settings (fieldofview/aspectratio) as animatable constraints.
296
297   // should be updated when:
298   // Magnifier's world size/scale changes.
299   Actor self(Self());
300   Vector3 worldSize = mActorSize * self.GetCurrentWorldScale();
301
302   // Adjust field of view to scale content
303
304   // size.height / 2
305   // |------/
306   // |d    /
307   // |i   /
308   // |s  /
309   // |t /
310   // |./
311   // |/ <--- fov/2 radians.
312   //
313   const float fov = atanf( 0.5f * worldSize.height / mDefaultCameraDistance / mMagnificationFactor) * 2.0f;
314   mCameraActor.SetFieldOfView( fov );
315
316   // Adjust aspect ratio to compensate for rectangular viewports.
317   mCameraActor.SetAspectRatio( worldSize.width / worldSize.height );
318 }
319
320 } // namespace Internal
321
322 } // namespace Toolkit
323
324 } // namespace Dali