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