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/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>
31 namespace // unnamed namespace
34 const char* DEFAULT_FRAME_IMAGE_PATH = DALI_IMAGE_DIR "magnifier-image-frame.png";
36 const float IMAGE_BORDER_INDENT = 14.0f; ///< Indent of border in pixels.
39 * ImageBorderSizeConstraint
41 struct ImageBorderSizeConstraint
43 ImageBorderSizeConstraint()
44 : mSizeOffset(Vector3(IMAGE_BORDER_INDENT - 1, IMAGE_BORDER_INDENT - 1, 0.0f) * 2.0f)
48 Vector3 operator()(const Vector3& current,
49 const PropertyInput& referenceSizeProperty)
51 const Vector3& referenceSize = referenceSizeProperty.GetVector3();
53 return referenceSize + mSizeOffset;
56 Vector3 mSizeOffset; ///< The amount to offset the size from referenceSize
59 struct CameraActorPositionConstraint
61 CameraActorPositionConstraint(const Vector2& stageSize, float defaultCameraDistance = 0.0f)
62 : mStageSize(stageSize),
63 mDefaultCameraDistance(defaultCameraDistance)
67 Vector3 operator()(const Vector3& current,
68 const PropertyInput& sourcePositionProperty)
70 const Vector3& sourcePosition = sourcePositionProperty.GetVector3();
72 return Vector3( sourcePosition.x + mStageSize.x * 0.5f,
73 sourcePosition.y + mStageSize.y * 0.5f,
74 sourcePosition.z + mDefaultCameraDistance);
78 float mDefaultCameraDistance;
82 struct RenderTaskViewportPositionConstraint
84 RenderTaskViewportPositionConstraint(const Vector2& stageSize)
85 : mStageSize(stageSize)
89 Vector2 operator()(const Vector2& current,
90 const PropertyInput& positionProperty,
91 const PropertyInput& magnifierSizeProperty,
92 const PropertyInput& magnifierScaleProperty)
94 Vector2 position(positionProperty.GetVector3()); // World position?
96 //position -= mStageSize * 0.5f;
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();
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;
113 struct RenderTaskViewportSizeConstraint
115 RenderTaskViewportSizeConstraint()
119 Vector2 operator()(const Vector2& current,
120 const PropertyInput& magnifierSizeProperty,
121 const PropertyInput& magnifierScaleProperty)
123 return Vector2(magnifierSizeProperty.GetVector3() * magnifierScaleProperty.GetVector3());
138 ///////////////////////////////////////////////////////////////////////////////////////////////////
140 ///////////////////////////////////////////////////////////////////////////////////////////////////
142 Dali::Toolkit::Magnifier Magnifier::New()
144 // Create the implementation
145 MagnifierPtr magnifier(new Magnifier());
147 // Pass ownership to CustomActor via derived handle
148 Dali::Toolkit::Magnifier handle(*magnifier);
150 // Second-phase init of the implementation
151 // This can only be done after the CustomActor connection has been made...
152 magnifier->Initialize();
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)
166 void Magnifier::SetSourceActor(Actor actor)
168 mTask.SetSourceActor( actor );
171 void Magnifier::SetSourcePosition(const Vector3& position)
173 Self().SetProperty(mPropertySourcePosition, position);
176 void Magnifier::Initialize()
179 mPropertySourcePosition = self.RegisterProperty( Toolkit::Magnifier::SOURCE_POSITION_PROPERTY_NAME, Vector3::ZERO );
180 Vector2 stageSize(Stage::GetCurrent().GetSize());
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
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);
200 // create the render task this will render content on top of everything
201 // based on camera source position.
202 InitializeRenderTask();
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)
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)
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);
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);
227 constraint = Constraint::New<Vector3>( Actor::Property::POSITION,
228 Source( mSourceActor, Actor::Property::WORLD_POSITION ),
229 CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
230 mCameraActor.ApplyConstraint(constraint);
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);
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);
248 Magnifier::~Magnifier()
253 void Magnifier::InitializeRenderTask()
255 Stage stage = Stage::GetCurrent();
257 RenderTaskList taskList = stage.GetRenderTaskList();
259 mTask = taskList.CreateTask();
260 mTask.SetInputEnabled(false);
261 mTask.SetClearColor(Vector4(0.5f, 0.5f, 0.5f, 1.0f));
262 mTask.SetClearEnabled(true);
264 mCameraActor = CameraActor::New();
265 mCameraActor.SetType(Camera::FREE_LOOK);
267 stage.Add(mCameraActor);
268 mTask.SetCameraActor( mCameraActor );
270 SetFrameVisibility(true);
273 bool Magnifier::GetFrameVisibility() const
278 void Magnifier::SetFrameVisibility(bool visible)
280 if(visible && !mFrame)
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);
292 Constraint constraint = Constraint::New<Vector3>( Actor::Property::POSITION,
293 ParentSource( Actor::Property::WORLD_POSITION ),
294 EqualToConstraint() );
295 mFrame.ApplyConstraint( constraint );
297 mFrame.SetNinePatchBorder( Vector4::ONE * IMAGE_BORDER_INDENT );
300 Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
301 mFrame.SetSize(mActorSize + sizeOffset);
303 else if(!visible && mFrame)
305 UnparentAndReset(mFrame);
309 void Magnifier::OnControlSizeSet(const Vector3& targetSize)
313 Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
314 mFrame.SetSize(targetSize + sizeOffset);
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;
326 float Magnifier::GetMagnificationFactor() const
328 return mMagnificationFactor;
331 void Magnifier::SetMagnificationFactor(float value)
333 mMagnificationFactor = value;
337 void Magnifier::Update()
339 // TODO: Make Camera settings (fieldofview/aspectratio) as animatable constraints.
341 // should be updated when:
342 // Magnifier's world size/scale changes.
344 Vector3 worldSize = mActorSize * self.GetCurrentWorldScale();
346 // Adjust field of view to scale content
355 // |/ <--- fov/2 radians.
357 const float fov = atanf( 0.5f * worldSize.height / mDefaultCameraDistance / mMagnificationFactor) * 2.0f;
358 mCameraActor.SetFieldOfView( fov );
360 // Adjust aspect ratio to compensate for rectangular viewports.
361 mCameraActor.SetAspectRatio( worldSize.width / worldSize.height );
364 } // namespace Internal
366 } // namespace Toolkit