2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/controls/magnifier/magnifier-impl.h>
22 #include <dali/public-api/actors/image-actor.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>
29 namespace // unnamed namespace
32 const char* DEFAULT_FRAME_IMAGE_PATH = DALI_IMAGE_DIR "magnifier-image-frame.png";
34 const float IMAGE_BORDER_INDENT = 14.0f; ///< Indent of border in pixels.
37 * ImageBorderSizeConstraint
39 struct ImageBorderSizeConstraint
41 ImageBorderSizeConstraint()
42 : mSizeOffset(Vector3(IMAGE_BORDER_INDENT - 1, IMAGE_BORDER_INDENT - 1, 0.0f) * 2.0f)
46 Vector3 operator()(const Vector3& current,
47 const PropertyInput& referenceSizeProperty)
49 const Vector3& referenceSize = referenceSizeProperty.GetVector3();
51 return referenceSize + mSizeOffset;
54 Vector3 mSizeOffset; ///< The amount to offset the size from referenceSize
57 struct CameraActorPositionConstraint
59 CameraActorPositionConstraint(const Vector2& stageSize, float defaultCameraDistance = 0.0f)
60 : mStageSize(stageSize),
61 mDefaultCameraDistance(defaultCameraDistance)
65 Vector3 operator()(const Vector3& current,
66 const PropertyInput& sourcePositionProperty)
68 const Vector3& sourcePosition = sourcePositionProperty.GetVector3();
70 return Vector3( sourcePosition.x + mStageSize.x * 0.5f,
71 sourcePosition.y + mStageSize.y * 0.5f,
72 sourcePosition.z + mDefaultCameraDistance);
76 float mDefaultCameraDistance;
80 struct RenderTaskViewportPositionConstraint
82 RenderTaskViewportPositionConstraint(const Vector2& stageSize)
83 : mStageSize(stageSize)
87 Vector2 operator()(const Vector2& current,
88 const PropertyInput& positionProperty,
89 const PropertyInput& magnifierSizeProperty,
90 const PropertyInput& magnifierScaleProperty)
92 Vector2 position(positionProperty.GetVector3()); // World position?
94 //position -= mStageSize * 0.5f;
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 = magnifierSizeProperty.GetVector3() * magnifierScaleProperty.GetVector3();
101 // Reposition, and resize viewport to reflect the world bounds of this Magnifier actor.
102 position.x += (mStageSize.width - size.width) * 0.5f;
103 position.y += (mStageSize.height - size.height) * 0.5f;
111 struct RenderTaskViewportSizeConstraint
113 RenderTaskViewportSizeConstraint()
117 Vector2 operator()(const Vector2& current,
118 const PropertyInput& magnifierSizeProperty,
119 const PropertyInput& magnifierScaleProperty)
121 return Vector2(magnifierSizeProperty.GetVector3() * magnifierScaleProperty.GetVector3());
136 ///////////////////////////////////////////////////////////////////////////////////////////////////
138 ///////////////////////////////////////////////////////////////////////////////////////////////////
140 Dali::Toolkit::Magnifier Magnifier::New()
142 // Create the implementation
143 MagnifierPtr magnifier(new Magnifier());
145 // Pass ownership to CustomActor via derived handle
146 Dali::Toolkit::Magnifier handle(*magnifier);
148 // Second-phase init of the implementation
149 // This can only be done after the CustomActor connection has been made...
150 magnifier->Initialize();
155 Magnifier::Magnifier()
156 : Control( REQUIRES_TOUCH_EVENTS ),
157 mPropertySourcePosition(Property::INVALID_INDEX),
158 mActorSize(Vector3::ZERO),
159 mMagnificationFactor(1.0f)
163 void Magnifier::SetSourceActor(Actor actor)
165 mTask.SetSourceActor( actor );
168 void Magnifier::SetSourcePosition(const Vector3& position)
170 Self().SetProperty(mPropertySourcePosition, position);
173 void Magnifier::Initialize()
176 mPropertySourcePosition = self.RegisterProperty( Toolkit::Magnifier::SOURCE_POSITION_PROPERTY_NAME, Vector3::ZERO );
177 Vector2 stageSize(Stage::GetCurrent().GetSize());
179 Layer dummyLayer = Layer::New();
180 Stage().GetCurrent().Add(dummyLayer);
181 dummyLayer.SetParentOrigin(ParentOrigin::CENTER);
184 // sourceActor is a dummy delegate actor that takes the source property position,
185 // and generates a WORLD_POSITION, which is 1 frame behind the source property.
186 // This way the constraints for determining the camera position (source) and those
187 // for determining viewport position use the same 1 frame old values.
188 // A simple i) CameraPos = f(B), ii) B = f(A) set of constraints wont suffice as
189 // although CameraPos will use B, which is A's previous value. The constraint will
190 // not realise that B is still dirty as far as constraint (i) is concerned.
191 // Perhaps this is a bug in the way the constraint system factors into what is dirty
193 mSourceActor = Actor::New();
194 dummyLayer.Add(mSourceActor);
195 mSourceActor.SetParentOrigin(ParentOrigin::CENTER);
196 Constraint constraint = Constraint::New<Vector3>( Actor::POSITION,
197 Source( self, mPropertySourcePosition ),
198 EqualToConstraint() );
199 mSourceActor.ApplyConstraint(constraint);
201 // create the render task this will render content on top of everything
202 // based on camera source position.
203 InitializeRenderTask();
205 // set up some constraints to:
206 // i) reposition (dest) frame actor based on magnifier actor's world position (this is 1 frame delayed)
207 // ii) reposition and resize (dest) the render task's viewport based on magnifier actor's world position (1 frame delayed) & size.
208 // iii) reposition (source) camera actor based on magnifier source actor's world position (this is 1 frame delayed)
210 // Apply constraint to camera's position
211 // Position our camera at the same distance from its target as the default camera is.
212 // The camera position doesn't affect how we render, just what we render (due to near and far clip planes)
213 // NOTE: We can't interrogate the default camera's position as it is not known initially (takes 1 frame
214 // for value to update).
215 // But we can determine the initial position using the same formula:
216 // distance = stage.height * 0.5 / tan(FOV * 0.5)
218 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
219 RenderTask renderTask = taskList.GetTask(0u);
220 float fov = renderTask.GetCameraActor().GetFieldOfView();
221 mDefaultCameraDistance = (stageSize.height * 0.5f) / tanf(fov * 0.5f);
223 // Use a 1 frame delayed source position to determine the camera actor's position.
224 // This is necessary as the viewport is determined by the Magnifier's Actor's World position (which is computed
225 // at the end of the update cycle i.e. after constraints have been applied.)
226 //Property::Index propertySourcePositionDelayed = mCameraActor.RegisterProperty("delayed-source-position", Vector3::ZERO);
228 constraint = Constraint::New<Vector3>( Actor::POSITION,
229 Source( mSourceActor, Actor::WORLD_POSITION ),
230 CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
231 mCameraActor.ApplyConstraint(constraint);
233 // Apply constraint to render-task viewport position
234 constraint = Constraint::New<Vector2>( RenderTask::VIEWPORT_POSITION,
235 Source( self, Actor::WORLD_POSITION ),//mPropertySourcePosition ),
236 Source( self, Actor::SIZE ),
237 Source( self, Actor::WORLD_SCALE),
238 RenderTaskViewportPositionConstraint(stageSize) );
239 mTask.ApplyConstraint(constraint);
241 // Apply constraint to render-task viewport position
242 constraint = Constraint::New<Vector2>( RenderTask::VIEWPORT_SIZE,
243 Source( self, Actor::SIZE ),
244 Source( self, Actor::WORLD_SCALE),
245 RenderTaskViewportSizeConstraint() );
246 mTask.ApplyConstraint(constraint);
249 Magnifier::~Magnifier()
254 void Magnifier::InitializeRenderTask()
256 Stage stage = Stage::GetCurrent();
258 RenderTaskList taskList = stage.GetRenderTaskList();
260 mTask = taskList.CreateTask();
261 mTask.SetInputEnabled(false);
262 mTask.SetClearColor(Vector4(0.5f, 0.5f, 0.5f, 1.0f));
263 mTask.SetClearEnabled(true);
265 mCameraActor = CameraActor::New();
266 mCameraActor.SetType(Camera::FREE_LOOK);
267 mCameraActor.SetRotation(Quaternion(M_PI, Vector3::YAXIS)); // Look at stage
269 stage.Add(mCameraActor);
270 mTask.SetCameraActor( mCameraActor );
272 SetFrameVisibility(true);
275 bool Magnifier::GetFrameVisibility() const
280 void Magnifier::SetFrameVisibility(bool visible)
282 if(visible && !mFrameLayer)
286 Layer mFrameLayer = Layer::New();
287 mFrameLayer.SetParentOrigin( ParentOrigin::CENTER );
288 Stage::GetCurrent().Add(mFrameLayer);
290 Image image = Image::New( DEFAULT_FRAME_IMAGE_PATH );
291 ImageActor frame = ImageActor::New( image );
292 frame.SetDrawMode(DrawMode::OVERLAY);
293 frame.SetStyle( ImageActor::STYLE_NINE_PATCH );
295 frame.SetNinePatchBorder( Vector4::ONE * IMAGE_BORDER_INDENT );
296 mFrameLayer.Add(frame);
298 // Apply position constraint to the frame
299 Constraint constraint = Constraint::New<Vector3>( Actor::POSITION,
300 Source( self, Actor::WORLD_POSITION ),
301 EqualToConstraint() );
302 frame.ApplyConstraint(constraint);
304 // Apply scale constraint to the frame
305 constraint = Constraint::New<Vector3>( Actor::SCALE,
306 Source( self, Actor::SCALE ),
307 EqualToConstraint() );
308 frame.ApplyConstraint(constraint);
310 Source(self, Actor::SCALE),
312 // Apply size constraint to the the frame
313 constraint = Constraint::New<Vector3>(Actor::SIZE,
314 Source(self, Actor::SIZE),
315 ImageBorderSizeConstraint());
316 frame.ApplyConstraint(constraint);
318 else if(!visible && mFrameLayer)
320 Stage::GetCurrent().Remove(mFrameLayer);
325 void Magnifier::OnControlSizeSet(const Vector3& targetSize)
327 // TODO: Once Camera/CameraActor properties function as proper animatable properties
328 // this code can disappear.
329 // whenever the size of the magnifier changes, the field of view needs to change
330 // to compensate for the new size of the viewport. this cannot be done within
331 // a constraint yet as Camera/CameraActor properties are not animatable/constrainable.
332 mActorSize = targetSize;
336 float Magnifier::GetMagnificationFactor() const
338 return mMagnificationFactor;
341 void Magnifier::SetMagnificationFactor(float value)
343 mMagnificationFactor = value;
347 void Magnifier::Update()
349 // TODO: Make Camera settings (fieldofview/aspectratio) as animatable constraints.
351 // should be updated when:
352 // Magnifier's world size/scale changes.
354 Vector3 worldSize = mActorSize * self.GetCurrentWorldScale();
356 // Adjust field of view to scale content
365 // |/ <--- fov/2 radians.
367 const float fov = atanf( 0.5f * worldSize.height / mDefaultCameraDistance / mMagnificationFactor) * 2.0f;
368 mCameraActor.SetFieldOfView( fov );
370 // Adjust aspect ratio to compensate for rectangular viewports.
371 mCameraActor.SetAspectRatio( worldSize.width / worldSize.height );
374 } // namespace Internal
376 } // namespace Toolkit