2 * Copyright (c) 2016 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/public-api/object/type-registry-helper.h>
29 #include <dali/public-api/object/property-map.h>
32 #include <dali-toolkit/public-api/visuals/border-visual-properties.h>
33 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
34 #include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
45 namespace // unnamed namespace
49 Dali::BaseHandle Create()
51 return Toolkit::Magnifier::New();
54 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Magnifier, Toolkit::Control, Create )
56 DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "frameVisibility", BOOLEAN, FRAME_VISIBILITY )
57 DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "magnificationFactor", FLOAT, MAGNIFICATION_FACTOR )
59 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, Magnifier, "sourcePosition", VECTOR3, SOURCE_POSITION )
61 DALI_TYPE_REGISTRATION_END()
63 const float IMAGE_BORDER_INDENT = 5.0f; ///< Indent of border in pixels.
65 struct CameraActorPositionConstraint
67 CameraActorPositionConstraint(const Vector2& stageSize, float defaultCameraDistance = 0.0f)
68 : mStageSize(stageSize),
69 mDefaultCameraDistance(defaultCameraDistance)
73 void operator()( Vector3& current, const PropertyInputContainer& inputs )
75 const Vector3& sourcePosition = inputs[0]->GetVector3();
77 current.x = sourcePosition.x + mStageSize.x * 0.5f;
78 current.y = sourcePosition.y + mStageSize.y * 0.5f;
79 current.z = sourcePosition.z + mDefaultCameraDistance;
83 float mDefaultCameraDistance;
87 struct RenderTaskViewportPositionConstraint
89 RenderTaskViewportPositionConstraint(const Vector2& stageSize)
90 : mStageSize(stageSize)
94 void operator()( Vector2& current, const PropertyInputContainer& inputs )
96 current = inputs[0]->GetVector3(); // World position?
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 = inputs[1]->GetVector3() * inputs[2]->GetVector3(); /* magnifier-size * magnifier-scale */
103 // Reposition, and resize viewport to reflect the world bounds of this Magnifier actor.
104 current.x += ( mStageSize.width - size.width ) * 0.5f;
105 current.y += ( mStageSize.height - size.height ) * 0.5f;
111 struct RenderTaskViewportSizeConstraint
113 RenderTaskViewportSizeConstraint()
117 void operator()( Vector2& current, const PropertyInputContainer& inputs )
119 current = inputs[0]->GetVector3() * inputs[1]->GetVector3(); /* magnifier-size * magnifier-scale */
123 } // unnamed namespace
125 ///////////////////////////////////////////////////////////////////////////////////////////////////
127 ///////////////////////////////////////////////////////////////////////////////////////////////////
129 Dali::Toolkit::Magnifier Magnifier::New()
131 // Create the implementation
132 MagnifierPtr magnifier(new Magnifier());
134 // Pass ownership to CustomActor via derived handle
135 Dali::Toolkit::Magnifier handle(*magnifier);
137 // Second-phase init of the implementation
138 // This can only be done after the CustomActor connection has been made...
139 magnifier->Initialize();
144 Magnifier::Magnifier()
145 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
146 mDefaultCameraDistance(1000.f),
147 mActorSize(Vector3::ZERO),
148 mMagnificationFactor(1.0f)
152 void Magnifier::SetSourceActor(Actor actor)
154 mTask.SetSourceActor( actor );
157 void Magnifier::Initialize()
160 Vector2 stageSize(Stage::GetCurrent().GetSize());
163 // sourceActor is a dummy delegate actor that takes the source property position,
164 // and generates a WORLD_POSITION, which is 1 frame behind the source property.
165 // This way the constraints for determining the camera position (source) and those
166 // for determining viewport position use the same 1 frame old values.
167 // A simple i) CameraPos = f(B), ii) B = f(A) set of constraints wont suffice as
168 // although CameraPos will use B, which is A's previous value. The constraint will
169 // not realise that B is still dirty as far as constraint (i) is concerned.
170 // Perhaps this is a bug in the way the constraint system factors into what is dirty
172 mSourceActor = Actor::New();
173 Stage().GetCurrent().Add(mSourceActor);
174 mSourceActor.SetParentOrigin(ParentOrigin::CENTER);
175 Constraint constraint = Constraint::New<Vector3>( mSourceActor, Actor::Property::POSITION, EqualToConstraint() );
176 constraint.AddSource( Source( self, Toolkit::Magnifier::Property::SOURCE_POSITION ) );
179 // create the render task this will render content on top of everything
180 // based on camera source position.
181 InitializeRenderTask();
183 // set up some constraints to:
184 // i) reposition (dest) frame actor based on magnifier actor's world position (this is 1 frame delayed)
185 // ii) reposition and resize (dest) the render task's viewport based on magnifier actor's world position (1 frame delayed) & size.
186 // iii) reposition (source) camera actor based on magnifier source actor's world position (this is 1 frame delayed)
188 // Apply constraint to camera's position
189 // Position our camera at the same distance from its target as the default camera is.
190 // The camera position doesn't affect how we render, just what we render (due to near and far clip planes)
191 // NOTE: We can't interrogate the default camera's position as it is not known initially (takes 1 frame
192 // for value to update).
193 // But we can determine the initial position using the same formula:
194 // distance = stage.height * 0.5 / tan(FOV * 0.5)
196 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
197 RenderTask renderTask = taskList.GetTask(0u);
198 float fov = renderTask.GetCameraActor().GetFieldOfView();
199 mDefaultCameraDistance = (stageSize.height * 0.5f) / tanf(fov * 0.5f);
201 // Use a 1 frame delayed source position to determine the camera actor's position.
202 // This is necessary as the viewport is determined by the Magnifier's Actor's World position (which is computed
203 // at the end of the update cycle i.e. after constraints have been applied.)
204 //Property::Index propertySourcePositionDelayed = mCameraActor.RegisterProperty("delayedSourcePosition", Vector3::ZERO);
206 constraint = Constraint::New<Vector3>( mCameraActor, Actor::Property::POSITION, CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
207 constraint.AddSource( Source( mSourceActor, Actor::Property::WORLD_POSITION ) );
210 // Apply constraint to render-task viewport position
211 constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_POSITION, RenderTaskViewportPositionConstraint(stageSize) );
212 constraint.AddSource( Source( self, Actor::Property::WORLD_POSITION ) );
213 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
214 constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
217 // Apply constraint to render-task viewport position
218 constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_SIZE, RenderTaskViewportSizeConstraint() );
219 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
220 constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
224 Magnifier::~Magnifier()
229 void Magnifier::InitializeRenderTask()
231 Stage stage = Stage::GetCurrent();
233 RenderTaskList taskList = stage.GetRenderTaskList();
235 mTask = taskList.CreateTask();
236 mTask.SetInputEnabled(false);
237 mTask.SetClearColor(Vector4(0.5f, 0.5f, 0.5f, 1.0f));
238 mTask.SetClearEnabled(true);
240 mCameraActor = CameraActor::New();
241 mCameraActor.SetType(Camera::FREE_LOOK);
243 stage.Add(mCameraActor);
244 mTask.SetCameraActor( mCameraActor );
246 SetFrameVisibility(true);
249 bool Magnifier::GetFrameVisibility() const
254 void Magnifier::SetFrameVisibility(bool visible)
256 if(visible && !mFrame)
260 mFrame = Actor::New( );
261 mFrame.SetInheritPosition(false);
262 mFrame.SetInheritScale(true);
263 mFrame.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
264 Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
265 mFrame.SetSizeModeFactor( sizeOffset );
267 Toolkit::VisualFactory visualFactory = Toolkit::VisualFactory::Get();
270 map[ Toolkit::VisualProperty::TYPE ] = Toolkit::Visual::BORDER;
271 map[ Toolkit::BorderVisual::Property::COLOR ] = Color::WHITE;
272 map[ Toolkit::BorderVisual::Property::SIZE ] = IMAGE_BORDER_INDENT;
273 Toolkit::Visual::Base borderVisual = visualFactory.CreateVisual( map );
274 borderVisual.SetOnStage( mFrame );
276 Constraint constraint = Constraint::New<Vector3>( mFrame, Actor::Property::POSITION, EqualToConstraint() );
277 constraint.AddSource( ParentSource( Actor::Property::WORLD_POSITION ) );
282 else if(!visible && mFrame)
284 UnparentAndReset(mFrame);
288 void Magnifier::OnSizeSet(const Vector3& targetSize)
290 Control::OnSizeSet( targetSize );
292 // TODO: Once Camera/CameraActor properties function as proper animatable properties
293 // this code can disappear.
294 // whenever the size of the magnifier changes, the field of view needs to change
295 // to compensate for the new size of the viewport. this cannot be done within
296 // a constraint yet as Camera/CameraActor properties are not animatable/constrainable.
297 mActorSize = targetSize;
301 float Magnifier::GetMagnificationFactor() const
303 return mMagnificationFactor;
306 void Magnifier::SetMagnificationFactor(float value)
308 mMagnificationFactor = value;
312 void Magnifier::Update()
314 // TODO: Make Camera settings (fieldofview/aspectratio) as animatable constraints.
316 // should be updated when:
317 // Magnifier's world size/scale changes.
319 Vector3 worldSize = mActorSize * self.GetCurrentWorldScale();
321 // Adjust field of view to scale content
330 // |/ <--- fov/2 radians.
332 const float fov = atanf( 0.5f * worldSize.height / mDefaultCameraDistance / mMagnificationFactor) * 2.0f;
333 mCameraActor.SetFieldOfView( fov );
335 // Adjust aspect ratio to compensate for rectangular viewports.
336 mCameraActor.SetAspectRatio( worldSize.width / worldSize.height );
339 void Magnifier::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
341 Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
345 Magnifier& magnifierImpl( GetImpl( magnifier ) );
348 case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
350 magnifierImpl.SetFrameVisibility( value.Get< bool >() );
353 case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
355 magnifierImpl.SetMagnificationFactor( value.Get< float >() );
362 Property::Value Magnifier::GetProperty( BaseObject* object, Property::Index index )
364 Property::Value value;
366 Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
370 Magnifier& magnifierImpl( GetImpl( magnifier ) );
373 case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
375 value = magnifierImpl.GetFrameVisibility();
378 case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
380 value = magnifierImpl.GetMagnificationFactor();
389 } // namespace Internal
391 } // namespace Toolkit