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