2 * Copyright (c) 2017 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/internal/visuals/visual-factory-impl.h>
35 #include <dali-toolkit/public-api/visuals/visual-properties.h>
36 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
47 namespace // unnamed namespace
51 Dali::BaseHandle Create()
53 return Toolkit::Magnifier::New();
56 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Magnifier, Toolkit::Control, Create )
58 DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "frameVisibility", BOOLEAN, FRAME_VISIBILITY )
59 DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "magnificationFactor", FLOAT, MAGNIFICATION_FACTOR )
61 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, Magnifier, "sourcePosition", VECTOR3, SOURCE_POSITION )
63 DALI_TYPE_REGISTRATION_END()
65 const float IMAGE_BORDER_INDENT = 5.0f; ///< Indent of border in pixels.
67 struct CameraActorPositionConstraint
69 CameraActorPositionConstraint(const Vector2& stageSize, float defaultCameraDistance = 0.0f)
70 : mStageSize(stageSize),
71 mDefaultCameraDistance(defaultCameraDistance)
75 void operator()( Vector3& current, const PropertyInputContainer& inputs )
77 const Vector3& sourcePosition = inputs[0]->GetVector3();
79 current.x = sourcePosition.x + mStageSize.x * 0.5f;
80 current.y = sourcePosition.y + mStageSize.y * 0.5f;
81 current.z = sourcePosition.z + mDefaultCameraDistance;
85 float mDefaultCameraDistance;
89 struct RenderTaskViewportPositionConstraint
91 RenderTaskViewportPositionConstraint(const Vector2& stageSize)
92 : mStageSize(stageSize)
96 void operator()( Vector2& current, const PropertyInputContainer& inputs )
98 current = inputs[0]->GetVector3(); // World position?
100 // should be updated when:
101 // Magnifier's world position/size/scale/parentorigin/anchorpoint changes.
102 // or Magnifier camera's world position changes.
103 Vector3 size = inputs[1]->GetVector3() * inputs[2]->GetVector3(); /* magnifier-size * magnifier-scale */
105 // Reposition, and resize viewport to reflect the world bounds of this Magnifier actor.
106 current.x += ( mStageSize.width - size.width ) * 0.5f;
107 current.y += ( mStageSize.height - size.height ) * 0.5f;
113 struct RenderTaskViewportSizeConstraint
115 RenderTaskViewportSizeConstraint()
119 void operator()( Vector2& current, const PropertyInputContainer& inputs )
121 current = inputs[0]->GetVector3() * inputs[1]->GetVector3(); /* magnifier-size * magnifier-scale */
125 } // unnamed namespace
127 ///////////////////////////////////////////////////////////////////////////////////////////////////
129 ///////////////////////////////////////////////////////////////////////////////////////////////////
131 Dali::Toolkit::Magnifier Magnifier::New()
133 // Create the implementation
134 MagnifierPtr magnifier(new Magnifier());
136 // Pass ownership to CustomActor via derived handle
137 Dali::Toolkit::Magnifier handle(*magnifier);
139 // Second-phase init of the implementation
140 // This can only be done after the CustomActor connection has been made...
141 magnifier->Initialize();
146 Magnifier::Magnifier()
147 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
148 mDefaultCameraDistance(1000.f),
149 mActorSize(Vector3::ZERO),
150 mMagnificationFactor(1.0f)
152 DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor ) {
153 return std::unique_ptr< Dali::Accessibility::Accessible >(
154 new Control::Impl::AccessibleImpl( actor, Dali::Accessibility::Role::FILLER ) );
159 void Magnifier::SetSourceActor(Actor actor)
161 mTask.SetSourceActor( actor );
164 void Magnifier::Initialize()
167 Vector2 stageSize(Stage::GetCurrent().GetSize());
170 // sourceActor is a dummy delegate actor that takes the source property position,
171 // and generates a WORLD_POSITION, which is 1 frame behind the source property.
172 // This way the constraints for determining the camera position (source) and those
173 // for determining viewport position use the same 1 frame old values.
174 // A simple i) CameraPos = f(B), ii) B = f(A) set of constraints wont suffice as
175 // although CameraPos will use B, which is A's previous value. The constraint will
176 // not realise that B is still dirty as far as constraint (i) is concerned.
177 // Perhaps this is a bug in the way the constraint system factors into what is dirty
179 mSourceActor = Actor::New();
180 Stage().GetCurrent().Add(mSourceActor);
181 mSourceActor.SetParentOrigin(ParentOrigin::CENTER);
182 Constraint constraint = Constraint::New<Vector3>( mSourceActor, Actor::Property::POSITION, EqualToConstraint() );
183 constraint.AddSource( Source( self, Toolkit::Magnifier::Property::SOURCE_POSITION ) );
186 // create the render task this will render content on top of everything
187 // based on camera source position.
188 InitializeRenderTask();
190 // set up some constraints to:
191 // i) reposition (dest) frame actor based on magnifier actor's world position (this is 1 frame delayed)
192 // ii) reposition and resize (dest) the render task's viewport based on magnifier actor's world position (1 frame delayed) & size.
193 // iii) reposition (source) camera actor based on magnifier source actor's world position (this is 1 frame delayed)
195 // Apply constraint to camera's position
196 // Position our camera at the same distance from its target as the default camera is.
197 // The camera position doesn't affect how we render, just what we render (due to near and far clip planes)
198 // NOTE: We can't interrogate the default camera's position as it is not known initially (takes 1 frame
199 // for value to update).
200 // But we can determine the initial position using the same formula:
201 // distance = stage.height * 0.5 / tan(FOV * 0.5)
203 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
204 RenderTask renderTask = taskList.GetTask(0u);
205 float fov = renderTask.GetCameraActor().GetFieldOfView();
206 mDefaultCameraDistance = (stageSize.height * 0.5f) / tanf(fov * 0.5f);
208 // Use a 1 frame delayed source position to determine the camera actor's position.
209 // This is necessary as the viewport is determined by the Magnifier's Actor's World position (which is computed
210 // at the end of the update cycle i.e. after constraints have been applied.)
211 //Property::Index propertySourcePositionDelayed = mCameraActor.RegisterProperty("delayedSourcePosition", Vector3::ZERO);
213 constraint = Constraint::New<Vector3>( mCameraActor, Actor::Property::POSITION, CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
214 constraint.AddSource( Source( mSourceActor, Actor::Property::WORLD_POSITION ) );
217 // Apply constraint to render-task viewport position
218 constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_POSITION, RenderTaskViewportPositionConstraint(stageSize) );
219 constraint.AddSource( Source( self, Actor::Property::WORLD_POSITION ) );
220 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
221 constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
224 // Apply constraint to render-task viewport position
225 constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_SIZE, RenderTaskViewportSizeConstraint() );
226 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
227 constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
231 Magnifier::~Magnifier()
236 void Magnifier::InitializeRenderTask()
238 Stage stage = Stage::GetCurrent();
240 RenderTaskList taskList = stage.GetRenderTaskList();
242 mTask = taskList.CreateTask();
243 mTask.SetInputEnabled(false);
244 mTask.SetClearColor(Vector4(0.5f, 0.5f, 0.5f, 1.0f));
245 mTask.SetClearEnabled(true);
247 mCameraActor = CameraActor::New();
248 mCameraActor.SetType(Camera::FREE_LOOK);
250 stage.Add(mCameraActor);
251 mTask.SetCameraActor( mCameraActor );
253 SetFrameVisibility(true);
256 bool Magnifier::GetFrameVisibility() const
261 void Magnifier::SetFrameVisibility(bool visible)
263 if(visible && !mFrame)
267 mFrame = Actor::New( );
268 mFrame.SetInheritPosition(false);
269 mFrame.SetInheritScale(true);
270 mFrame.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
271 Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
272 mFrame.SetSizeModeFactor( sizeOffset );
274 Toolkit::VisualFactory visualFactory = Toolkit::VisualFactory::Get();
277 map[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::BORDER;
278 map[ Toolkit::BorderVisual::Property::COLOR ] = Color::WHITE;
279 map[ Toolkit::BorderVisual::Property::SIZE ] = IMAGE_BORDER_INDENT;
280 Toolkit::Visual::Base borderVisual = visualFactory.CreateVisual( map );
281 Toolkit::GetImplementation(borderVisual).SetOnStage( mFrame );
283 Constraint constraint = Constraint::New<Vector3>( mFrame, Actor::Property::POSITION, EqualToConstraint() );
284 constraint.AddSource( ParentSource( Actor::Property::WORLD_POSITION ) );
289 else if(!visible && mFrame)
291 UnparentAndReset(mFrame);
295 void Magnifier::OnSizeSet(const Vector3& targetSize)
297 // TODO: Once Camera/CameraActor properties function as proper animatable properties
298 // this code can disappear.
299 // whenever the size of the magnifier changes, the field of view needs to change
300 // to compensate for the new size of the viewport. this cannot be done within
301 // a constraint yet as Camera/CameraActor properties are not animatable/constrainable.
302 mActorSize = targetSize;
305 Control::OnSizeSet( targetSize );
308 float Magnifier::GetMagnificationFactor() const
310 return mMagnificationFactor;
313 void Magnifier::SetMagnificationFactor(float value)
315 mMagnificationFactor = value;
319 void Magnifier::Update()
321 // TODO: Make Camera settings (fieldofview/aspectratio) as animatable constraints.
323 // should be updated when:
324 // Magnifier's world size/scale changes.
326 Vector3 worldSize = mActorSize * self.GetCurrentWorldScale();
328 // Adjust field of view to scale content
337 // |/ <--- fov/2 radians.
339 const float fov = atanf( 0.5f * worldSize.height / mDefaultCameraDistance / mMagnificationFactor) * 2.0f;
340 mCameraActor.SetFieldOfView( fov );
342 // Adjust aspect ratio to compensate for rectangular viewports.
343 mCameraActor.SetAspectRatio( worldSize.width / worldSize.height );
346 void Magnifier::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
348 Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
352 Magnifier& magnifierImpl( GetImpl( magnifier ) );
355 case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
357 magnifierImpl.SetFrameVisibility( value.Get< bool >() );
360 case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
362 magnifierImpl.SetMagnificationFactor( value.Get< float >() );
369 Property::Value Magnifier::GetProperty( BaseObject* object, Property::Index index )
371 Property::Value value;
373 Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
377 Magnifier& magnifierImpl( GetImpl( magnifier ) );
380 case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
382 value = magnifierImpl.GetFrameVisibility();
385 case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
387 value = magnifierImpl.GetMagnificationFactor();
396 } // namespace Internal
398 } // namespace Toolkit