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>
31 #include <dali-toolkit/devel-api/controls/renderer-factory/renderer-factory.h>
42 namespace // unnamed namespace
46 Dali::BaseHandle Create()
48 return Toolkit::Magnifier::New();
51 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Magnifier, Toolkit::Control, Create )
53 DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "frame-visibility", BOOLEAN, FRAME_VISIBILITY )
54 DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "magnification-factor", FLOAT, MAGNIFICATION_FACTOR )
56 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, Magnifier, "source-position", VECTOR3, SOURCE_POSITION )
58 DALI_TYPE_REGISTRATION_END()
60 const float IMAGE_BORDER_INDENT = 5.0f; ///< Indent of border in pixels.
62 struct CameraActorPositionConstraint
64 CameraActorPositionConstraint(const Vector2& stageSize, float defaultCameraDistance = 0.0f)
65 : mStageSize(stageSize),
66 mDefaultCameraDistance(defaultCameraDistance)
70 void operator()( Vector3& current, const PropertyInputContainer& inputs )
72 const Vector3& sourcePosition = inputs[0]->GetVector3();
74 current.x = sourcePosition.x + mStageSize.x * 0.5f;
75 current.y = sourcePosition.y + mStageSize.y * 0.5f;
76 current.z = sourcePosition.z + mDefaultCameraDistance;
80 float mDefaultCameraDistance;
84 struct RenderTaskViewportPositionConstraint
86 RenderTaskViewportPositionConstraint(const Vector2& stageSize)
87 : mStageSize(stageSize)
91 void operator()( Vector2& current, const PropertyInputContainer& inputs )
93 current = inputs[0]->GetVector3(); // World position?
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 = inputs[1]->GetVector3() * inputs[2]->GetVector3(); /* magnifier-size * magnifier-scale */
100 // Reposition, and resize viewport to reflect the world bounds of this Magnifier actor.
101 current.x += ( mStageSize.width - size.width ) * 0.5f;
102 current.y += ( mStageSize.height - size.height ) * 0.5f;
108 struct RenderTaskViewportSizeConstraint
110 RenderTaskViewportSizeConstraint()
114 void operator()( Vector2& current, const PropertyInputContainer& inputs )
116 current = inputs[0]->GetVector3() * inputs[1]->GetVector3(); /* magnifier-size * magnifier-scale */
120 } // unnamed namespace
122 ///////////////////////////////////////////////////////////////////////////////////////////////////
124 ///////////////////////////////////////////////////////////////////////////////////////////////////
126 Dali::Toolkit::Magnifier Magnifier::New()
128 // Create the implementation
129 MagnifierPtr magnifier(new Magnifier());
131 // Pass ownership to CustomActor via derived handle
132 Dali::Toolkit::Magnifier handle(*magnifier);
134 // Second-phase init of the implementation
135 // This can only be done after the CustomActor connection has been made...
136 magnifier->Initialize();
141 Magnifier::Magnifier()
142 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
143 mDefaultCameraDistance(1000.f),
144 mActorSize(Vector3::ZERO),
145 mMagnificationFactor(1.0f)
149 void Magnifier::SetSourceActor(Actor actor)
151 mTask.SetSourceActor( actor );
154 void Magnifier::Initialize()
157 Vector2 stageSize(Stage::GetCurrent().GetSize());
160 // sourceActor is a dummy delegate actor that takes the source property position,
161 // and generates a WORLD_POSITION, which is 1 frame behind the source property.
162 // This way the constraints for determining the camera position (source) and those
163 // for determining viewport position use the same 1 frame old values.
164 // A simple i) CameraPos = f(B), ii) B = f(A) set of constraints wont suffice as
165 // although CameraPos will use B, which is A's previous value. The constraint will
166 // not realise that B is still dirty as far as constraint (i) is concerned.
167 // Perhaps this is a bug in the way the constraint system factors into what is dirty
169 mSourceActor = Actor::New();
170 Stage().GetCurrent().Add(mSourceActor);
171 mSourceActor.SetParentOrigin(ParentOrigin::CENTER);
172 Constraint constraint = Constraint::New<Vector3>( mSourceActor, Actor::Property::POSITION, EqualToConstraint() );
173 constraint.AddSource( Source( self, Toolkit::Magnifier::Property::SOURCE_POSITION ) );
176 // create the render task this will render content on top of everything
177 // based on camera source position.
178 InitializeRenderTask();
180 // set up some constraints to:
181 // i) reposition (dest) frame actor based on magnifier actor's world position (this is 1 frame delayed)
182 // ii) reposition and resize (dest) the render task's viewport based on magnifier actor's world position (1 frame delayed) & size.
183 // iii) reposition (source) camera actor based on magnifier source actor's world position (this is 1 frame delayed)
185 // Apply constraint to camera's position
186 // Position our camera at the same distance from its target as the default camera is.
187 // The camera position doesn't affect how we render, just what we render (due to near and far clip planes)
188 // NOTE: We can't interrogate the default camera's position as it is not known initially (takes 1 frame
189 // for value to update).
190 // But we can determine the initial position using the same formula:
191 // distance = stage.height * 0.5 / tan(FOV * 0.5)
193 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
194 RenderTask renderTask = taskList.GetTask(0u);
195 float fov = renderTask.GetCameraActor().GetFieldOfView();
196 mDefaultCameraDistance = (stageSize.height * 0.5f) / tanf(fov * 0.5f);
198 // Use a 1 frame delayed source position to determine the camera actor's position.
199 // This is necessary as the viewport is determined by the Magnifier's Actor's World position (which is computed
200 // at the end of the update cycle i.e. after constraints have been applied.)
201 //Property::Index propertySourcePositionDelayed = mCameraActor.RegisterProperty("delayed-source-position", Vector3::ZERO);
203 constraint = Constraint::New<Vector3>( mCameraActor, Actor::Property::POSITION, CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
204 constraint.AddSource( Source( mSourceActor, Actor::Property::WORLD_POSITION ) );
207 // Apply constraint to render-task viewport position
208 constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_POSITION, RenderTaskViewportPositionConstraint(stageSize) );
209 constraint.AddSource( Source( self, Actor::Property::WORLD_POSITION ) );
210 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
211 constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
214 // Apply constraint to render-task viewport position
215 constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_SIZE, RenderTaskViewportSizeConstraint() );
216 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
217 constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
221 Magnifier::~Magnifier()
226 void Magnifier::InitializeRenderTask()
228 Stage stage = Stage::GetCurrent();
230 RenderTaskList taskList = stage.GetRenderTaskList();
232 mTask = taskList.CreateTask();
233 mTask.SetInputEnabled(false);
234 mTask.SetClearColor(Vector4(0.5f, 0.5f, 0.5f, 1.0f));
235 mTask.SetClearEnabled(true);
237 mCameraActor = CameraActor::New();
238 mCameraActor.SetType(Camera::FREE_LOOK);
240 stage.Add(mCameraActor);
241 mTask.SetCameraActor( mCameraActor );
243 SetFrameVisibility(true);
246 bool Magnifier::GetFrameVisibility() const
251 void Magnifier::SetFrameVisibility(bool visible)
253 if(visible && !mFrame)
257 mFrame = Actor::New( );
258 mFrame.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
259 mFrame.SetInheritScale(true);
260 mFrame.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
261 Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
262 mFrame.SetSizeModeFactor( sizeOffset );
264 //TODO Set the renderer onto the control self when Actor::RemoveRenderer is supported
265 Toolkit::RendererFactory rendererFactory = Toolkit::RendererFactory::Get();
266 Toolkit::ControlRenderer borderRenderer = rendererFactory.GetControlRenderer(IMAGE_BORDER_INDENT, Color::WHITE);
267 borderRenderer.SetOnStage( mFrame );
269 Constraint constraint = Constraint::New<Vector3>( mFrame, Actor::Property::POSITION, EqualToConstraint() );
270 constraint.AddSource( ParentSource( Actor::Property::WORLD_POSITION ) );
275 else if(!visible && mFrame)
277 UnparentAndReset(mFrame);
281 void Magnifier::OnSizeSet(const Vector3& targetSize)
283 // TODO: Once Camera/CameraActor properties function as proper animatable properties
284 // this code can disappear.
285 // whenever the size of the magnifier changes, the field of view needs to change
286 // to compensate for the new size of the viewport. this cannot be done within
287 // a constraint yet as Camera/CameraActor properties are not animatable/constrainable.
288 mActorSize = targetSize;
292 float Magnifier::GetMagnificationFactor() const
294 return mMagnificationFactor;
297 void Magnifier::SetMagnificationFactor(float value)
299 mMagnificationFactor = value;
303 void Magnifier::Update()
305 // TODO: Make Camera settings (fieldofview/aspectratio) as animatable constraints.
307 // should be updated when:
308 // Magnifier's world size/scale changes.
310 Vector3 worldSize = mActorSize * self.GetCurrentWorldScale();
312 // Adjust field of view to scale content
321 // |/ <--- fov/2 radians.
323 const float fov = atanf( 0.5f * worldSize.height / mDefaultCameraDistance / mMagnificationFactor) * 2.0f;
324 mCameraActor.SetFieldOfView( fov );
326 // Adjust aspect ratio to compensate for rectangular viewports.
327 mCameraActor.SetAspectRatio( worldSize.width / worldSize.height );
330 void Magnifier::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
332 Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
336 Magnifier& magnifierImpl( GetImpl( magnifier ) );
339 case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
341 magnifierImpl.SetFrameVisibility( value.Get< bool >() );
344 case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
346 magnifierImpl.SetMagnificationFactor( value.Get< float >() );
353 Property::Value Magnifier::GetProperty( BaseObject* object, Property::Index index )
355 Property::Value value;
357 Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
361 Magnifier& magnifierImpl( GetImpl( magnifier ) );
364 case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
366 value = magnifierImpl.GetFrameVisibility();
369 case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
371 value = magnifierImpl.GetMagnificationFactor();
380 } // namespace Internal
382 } // namespace Toolkit