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/constraints.h>
23 #include <dali/public-api/common/stage.h>
24 #include <dali/public-api/render-tasks/render-task-list.h>
28 namespace // unnamed namespace
31 const char* DEFAULT_FRAME_IMAGE_PATH = DALI_IMAGE_DIR "magnifier-image-frame.png";
33 const float IMAGE_BORDER_INDENT = 14.0f; ///< Indent of border in pixels.
36 * ImageBorderSizeConstraint
38 struct ImageBorderSizeConstraint
40 ImageBorderSizeConstraint()
41 : mSizeOffset(Vector3(IMAGE_BORDER_INDENT - 1, IMAGE_BORDER_INDENT - 1, 0.0f) * 2.0f)
45 Vector3 operator()(const Vector3& current,
46 const PropertyInput& referenceSizeProperty)
48 const Vector3& referenceSize = referenceSizeProperty.GetVector3();
50 return referenceSize + mSizeOffset;
53 Vector3 mSizeOffset; ///< The amount to offset the size from referenceSize
56 struct CameraActorPositionConstraint
58 CameraActorPositionConstraint(const Vector2& stageSize, float defaultCameraDistance = 0.0f)
59 : mStageSize(stageSize),
60 mDefaultCameraDistance(defaultCameraDistance)
64 Vector3 operator()(const Vector3& current,
65 const PropertyInput& sourcePositionProperty)
67 const Vector3& sourcePosition = sourcePositionProperty.GetVector3();
69 return Vector3( sourcePosition.x + mStageSize.x * 0.5f,
70 sourcePosition.y + mStageSize.y * 0.5f,
71 sourcePosition.z + mDefaultCameraDistance);
75 float mDefaultCameraDistance;
79 struct RenderTaskViewportPositionConstraint
81 RenderTaskViewportPositionConstraint(const Vector2& stageSize)
82 : mStageSize(stageSize)
86 Vector2 operator()(const Vector2& current,
87 const PropertyInput& positionProperty,
88 const PropertyInput& magnifierSizeProperty,
89 const PropertyInput& magnifierScaleProperty)
91 Vector2 position(positionProperty.GetVector3()); // World position?
93 //position -= mStageSize * 0.5f;
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();
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;
110 struct RenderTaskViewportSizeConstraint
112 RenderTaskViewportSizeConstraint()
116 Vector2 operator()(const Vector2& current,
117 const PropertyInput& magnifierSizeProperty,
118 const PropertyInput& magnifierScaleProperty)
120 return Vector2(magnifierSizeProperty.GetVector3() * magnifierScaleProperty.GetVector3());
135 ///////////////////////////////////////////////////////////////////////////////////////////////////
137 ///////////////////////////////////////////////////////////////////////////////////////////////////
139 Dali::Toolkit::Magnifier Magnifier::New()
141 // Create the implementation
142 MagnifierPtr magnifier(new Magnifier());
144 // Pass ownership to CustomActor via derived handle
145 Dali::Toolkit::Magnifier handle(*magnifier);
147 // Second-phase init of the implementation
148 // This can only be done after the CustomActor connection has been made...
149 magnifier->Initialize();
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)
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());
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
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);
197 // create the render task this will render content on top of everything
198 // based on camera source position.
199 InitializeRenderTask();
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)
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)
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);
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);
224 constraint = Constraint::New<Vector3>( Actor::POSITION,
225 Source( mSourceActor, Actor::WORLD_POSITION ),
226 CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
227 mCameraActor.ApplyConstraint(constraint);
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);
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);
245 Magnifier::~Magnifier()
250 void Magnifier::InitializeRenderTask()
252 Stage stage = Stage::GetCurrent();
254 RenderTaskList taskList = stage.GetRenderTaskList();
256 mTask = taskList.CreateTask();
257 mTask.SetInputEnabled(false);
258 mTask.SetClearColor(Vector4(0.5f, 0.5f, 0.5f, 1.0f));
259 mTask.SetClearEnabled(true);
261 mCameraActor = CameraActor::New();
262 mCameraActor.SetType(Camera::FREE_LOOK);
263 mCameraActor.SetRotation(Quaternion(M_PI, Vector3::YAXIS)); // Look at stage
265 stage.Add(mCameraActor);
266 mTask.SetCameraActor( mCameraActor );
268 SetFrameVisibility(true);
271 bool Magnifier::GetFrameVisibility() const
276 void Magnifier::SetFrameVisibility(bool visible)
278 if(visible && !mFrame)
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(DONT_INHERIT_POSITION);
287 mFrame.SetInheritScale(true);
289 Constraint constraint = Constraint::New<Vector3>( Actor::POSITION,
290 ParentSource(Actor::WORLD_POSITION),
291 EqualToConstraint());
292 mFrame.ApplyConstraint( constraint );
294 mFrame.SetNinePatchBorder( Vector4::ONE * IMAGE_BORDER_INDENT );
297 Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
298 mFrame.SetSize(mActorSize + sizeOffset);
300 else if(!visible && mFrame)
302 UnparentAndReset(mFrame);
306 void Magnifier::OnControlSizeSet(const Vector3& targetSize)
310 Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
311 mFrame.SetSize(targetSize + sizeOffset);
314 // TODO: Once Camera/CameraActor properties function as proper animatable properties
315 // this code can disappear.
316 // whenever the size of the magnifier changes, the field of view needs to change
317 // to compensate for the new size of the viewport. this cannot be done within
318 // a constraint yet as Camera/CameraActor properties are not animatable/constrainable.
319 mActorSize = targetSize;
323 float Magnifier::GetMagnificationFactor() const
325 return mMagnificationFactor;
328 void Magnifier::SetMagnificationFactor(float value)
330 mMagnificationFactor = value;
334 void Magnifier::Update()
336 // TODO: Make Camera settings (fieldofview/aspectratio) as animatable constraints.
338 // should be updated when:
339 // Magnifier's world size/scale changes.
341 Vector3 worldSize = mActorSize * self.GetCurrentWorldScale();
343 // Adjust field of view to scale content
352 // |/ <--- fov/2 radians.
354 const float fov = atanf( 0.5f * worldSize.height / mDefaultCameraDistance / mMagnificationFactor) * 2.0f;
355 mCameraActor.SetFieldOfView( fov );
357 // Adjust aspect ratio to compensate for rectangular viewports.
358 mCameraActor.SetAspectRatio( worldSize.width / worldSize.height );
361 } // namespace Internal
363 } // namespace Toolkit