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/constraint.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>
26 #include <dali/public-api/images/resource-image.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/devel-api/object/type-registry-helper.h>
39 namespace // unnamed namespace
43 Dali::BaseHandle Create()
45 return Toolkit::Magnifier::New();
48 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Magnifier, Toolkit::Control, Create )
50 DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "frame-visibility", BOOLEAN, FRAME_VISIBILITY )
51 DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "magnification-factor", FLOAT, MAGNIFICATION_FACTOR )
53 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, Magnifier, "source-position", VECTOR3, SOURCE_POSITION )
55 DALI_TYPE_REGISTRATION_END()
57 const char* DEFAULT_FRAME_IMAGE_PATH = DALI_IMAGE_DIR "magnifier-image-frame.png";
59 const float IMAGE_BORDER_INDENT = 14.0f; ///< Indent of border in pixels.
61 struct CameraActorPositionConstraint
63 CameraActorPositionConstraint(const Vector2& stageSize, float defaultCameraDistance = 0.0f)
64 : mStageSize(stageSize),
65 mDefaultCameraDistance(defaultCameraDistance)
69 void operator()( Vector3& current, const PropertyInputContainer& inputs )
71 const Vector3& sourcePosition = inputs[0]->GetVector3();
73 current.x = sourcePosition.x + mStageSize.x * 0.5f;
74 current.y = sourcePosition.y + mStageSize.y * 0.5f;
75 current.z = sourcePosition.z + mDefaultCameraDistance;
79 float mDefaultCameraDistance;
83 struct RenderTaskViewportPositionConstraint
85 RenderTaskViewportPositionConstraint(const Vector2& stageSize)
86 : mStageSize(stageSize)
90 void operator()( Vector2& current, const PropertyInputContainer& inputs )
92 current = inputs[0]->GetVector3(); // World position?
94 // should be updated when:
95 // Magnifier's world position/size/scale/parentorigin/anchorpoint changes.
96 // or Magnifier camera's world position changes.
97 Vector3 size = inputs[1]->GetVector3() * inputs[2]->GetVector3(); /* magnifier-size * magnifier-scale */
99 // Reposition, and resize viewport to reflect the world bounds of this Magnifier actor.
100 current.x += ( mStageSize.width - size.width ) * 0.5f;
101 current.y += ( mStageSize.height - size.height ) * 0.5f;
107 struct RenderTaskViewportSizeConstraint
109 RenderTaskViewportSizeConstraint()
113 void operator()( Vector2& current, const PropertyInputContainer& inputs )
115 current = inputs[0]->GetVector3() * inputs[1]->GetVector3(); /* magnifier-size * magnifier-scale */
119 } // unnamed namespace
121 ///////////////////////////////////////////////////////////////////////////////////////////////////
123 ///////////////////////////////////////////////////////////////////////////////////////////////////
125 Dali::Toolkit::Magnifier Magnifier::New()
127 // Create the implementation
128 MagnifierPtr magnifier(new Magnifier());
130 // Pass ownership to CustomActor via derived handle
131 Dali::Toolkit::Magnifier handle(*magnifier);
133 // Second-phase init of the implementation
134 // This can only be done after the CustomActor connection has been made...
135 magnifier->Initialize();
140 Magnifier::Magnifier()
141 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
142 mDefaultCameraDistance(1000.f),
143 mActorSize(Vector3::ZERO),
144 mMagnificationFactor(1.0f)
148 void Magnifier::SetSourceActor(Actor actor)
150 mTask.SetSourceActor( actor );
153 void Magnifier::Initialize()
156 Vector2 stageSize(Stage::GetCurrent().GetSize());
159 // sourceActor is a dummy delegate actor that takes the source property position,
160 // and generates a WORLD_POSITION, which is 1 frame behind the source property.
161 // This way the constraints for determining the camera position (source) and those
162 // for determining viewport position use the same 1 frame old values.
163 // A simple i) CameraPos = f(B), ii) B = f(A) set of constraints wont suffice as
164 // although CameraPos will use B, which is A's previous value. The constraint will
165 // not realise that B is still dirty as far as constraint (i) is concerned.
166 // Perhaps this is a bug in the way the constraint system factors into what is dirty
168 mSourceActor = Actor::New();
169 Stage().GetCurrent().Add(mSourceActor);
170 mSourceActor.SetParentOrigin(ParentOrigin::CENTER);
171 Constraint constraint = Constraint::New<Vector3>( mSourceActor, Actor::Property::POSITION, EqualToConstraint() );
172 constraint.AddSource( Source( self, Toolkit::Magnifier::Property::SOURCE_POSITION ) );
175 // create the render task this will render content on top of everything
176 // based on camera source position.
177 InitializeRenderTask();
179 // set up some constraints to:
180 // i) reposition (dest) frame actor based on magnifier actor's world position (this is 1 frame delayed)
181 // ii) reposition and resize (dest) the render task's viewport based on magnifier actor's world position (1 frame delayed) & size.
182 // iii) reposition (source) camera actor based on magnifier source actor's world position (this is 1 frame delayed)
184 // Apply constraint to camera's position
185 // Position our camera at the same distance from its target as the default camera is.
186 // The camera position doesn't affect how we render, just what we render (due to near and far clip planes)
187 // NOTE: We can't interrogate the default camera's position as it is not known initially (takes 1 frame
188 // for value to update).
189 // But we can determine the initial position using the same formula:
190 // distance = stage.height * 0.5 / tan(FOV * 0.5)
192 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
193 RenderTask renderTask = taskList.GetTask(0u);
194 float fov = renderTask.GetCameraActor().GetFieldOfView();
195 mDefaultCameraDistance = (stageSize.height * 0.5f) / tanf(fov * 0.5f);
197 // Use a 1 frame delayed source position to determine the camera actor's position.
198 // This is necessary as the viewport is determined by the Magnifier's Actor's World position (which is computed
199 // at the end of the update cycle i.e. after constraints have been applied.)
200 //Property::Index propertySourcePositionDelayed = mCameraActor.RegisterProperty("delayed-source-position", Vector3::ZERO);
202 constraint = Constraint::New<Vector3>( mCameraActor, Actor::Property::POSITION, CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
203 constraint.AddSource( Source( mSourceActor, Actor::Property::WORLD_POSITION ) );
206 // Apply constraint to render-task viewport position
207 constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_POSITION, RenderTaskViewportPositionConstraint(stageSize) );
208 constraint.AddSource( Source( self, Actor::Property::WORLD_POSITION ) );
209 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
210 constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
213 // Apply constraint to render-task viewport position
214 constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_SIZE, RenderTaskViewportSizeConstraint() );
215 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
216 constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
220 Magnifier::~Magnifier()
225 void Magnifier::InitializeRenderTask()
227 Stage stage = Stage::GetCurrent();
229 RenderTaskList taskList = stage.GetRenderTaskList();
231 mTask = taskList.CreateTask();
232 mTask.SetInputEnabled(false);
233 mTask.SetClearColor(Vector4(0.5f, 0.5f, 0.5f, 1.0f));
234 mTask.SetClearEnabled(true);
236 mCameraActor = CameraActor::New();
237 mCameraActor.SetType(Camera::FREE_LOOK);
239 stage.Add(mCameraActor);
240 mTask.SetCameraActor( mCameraActor );
242 SetFrameVisibility(true);
245 bool Magnifier::GetFrameVisibility() const
250 void Magnifier::SetFrameVisibility(bool visible)
252 if(visible && !mFrame)
256 Image image = ResourceImage::New( DEFAULT_FRAME_IMAGE_PATH );
257 mFrame = ImageActor::New( image );
258 mFrame.SetStyle( ImageActor::STYLE_NINE_PATCH );
259 mFrame.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
260 mFrame.SetInheritScale(true);
261 mFrame.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
262 Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
263 mFrame.SetSizeModeFactor( sizeOffset );
265 Constraint constraint = Constraint::New<Vector3>( mFrame, Actor::Property::POSITION, EqualToConstraint() );
266 constraint.AddSource( ParentSource( Actor::Property::WORLD_POSITION ) );
269 mFrame.SetNinePatchBorder( Vector4::ONE * IMAGE_BORDER_INDENT );
272 else if(!visible && mFrame)
274 UnparentAndReset(mFrame);
278 void Magnifier::OnSizeSet(const Vector3& targetSize)
280 // TODO: Once Camera/CameraActor properties function as proper animatable properties
281 // this code can disappear.
282 // whenever the size of the magnifier changes, the field of view needs to change
283 // to compensate for the new size of the viewport. this cannot be done within
284 // a constraint yet as Camera/CameraActor properties are not animatable/constrainable.
285 mActorSize = targetSize;
289 float Magnifier::GetMagnificationFactor() const
291 return mMagnificationFactor;
294 void Magnifier::SetMagnificationFactor(float value)
296 mMagnificationFactor = value;
300 void Magnifier::Update()
302 // TODO: Make Camera settings (fieldofview/aspectratio) as animatable constraints.
304 // should be updated when:
305 // Magnifier's world size/scale changes.
307 Vector3 worldSize = mActorSize * self.GetCurrentWorldScale();
309 // Adjust field of view to scale content
318 // |/ <--- fov/2 radians.
320 const float fov = atanf( 0.5f * worldSize.height / mDefaultCameraDistance / mMagnificationFactor) * 2.0f;
321 mCameraActor.SetFieldOfView( fov );
323 // Adjust aspect ratio to compensate for rectangular viewports.
324 mCameraActor.SetAspectRatio( worldSize.width / worldSize.height );
327 void Magnifier::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
329 Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
333 Magnifier& magnifierImpl( GetImpl( magnifier ) );
336 case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
338 magnifierImpl.SetFrameVisibility( value.Get< bool >() );
341 case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
343 magnifierImpl.SetMagnificationFactor( value.Get< float >() );
350 Property::Value Magnifier::GetProperty( BaseObject* object, Property::Index index )
352 Property::Value value;
354 Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
358 Magnifier& magnifierImpl( GetImpl( magnifier ) );
361 case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
363 value = magnifierImpl.GetFrameVisibility();
366 case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
368 value = magnifierImpl.GetMagnificationFactor();
377 } // namespace Internal
379 } // namespace Toolkit