2 * Copyright (c) 2020 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/devel-api/common/stage.h>
25 #include <dali/public-api/render-tasks/render-task-list.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/public-api/object/type-registry-helper.h>
28 #include <dali/public-api/object/property-map.h>
31 #include <dali-toolkit/public-api/visuals/border-visual-properties.h>
32 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
33 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
34 #include <dali-toolkit/public-api/visuals/visual-properties.h>
35 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
46 namespace // unnamed namespace
50 Dali::BaseHandle Create()
52 return Toolkit::Magnifier::New();
55 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Magnifier, Toolkit::Control, Create )
57 DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "frameVisibility", BOOLEAN, FRAME_VISIBILITY )
58 DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "magnificationFactor", FLOAT, MAGNIFICATION_FACTOR )
60 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, Magnifier, "sourcePosition", VECTOR3, SOURCE_POSITION )
62 DALI_TYPE_REGISTRATION_END()
64 const float IMAGE_BORDER_INDENT = 5.0f; ///< Indent of border in pixels.
66 struct CameraActorPositionConstraint
68 CameraActorPositionConstraint(const Vector2& stageSize, float defaultCameraDistance = 0.0f)
69 : mStageSize(stageSize),
70 mDefaultCameraDistance(defaultCameraDistance)
74 void operator()( Vector3& current, const PropertyInputContainer& inputs )
76 const Vector3& sourcePosition = inputs[0]->GetVector3();
78 current.x = sourcePosition.x + mStageSize.x * 0.5f;
79 current.y = sourcePosition.y + mStageSize.y * 0.5f;
80 current.z = sourcePosition.z + mDefaultCameraDistance;
84 float mDefaultCameraDistance;
88 struct RenderTaskViewportPositionConstraint
90 RenderTaskViewportPositionConstraint(const Vector2& stageSize)
91 : mStageSize(stageSize)
95 void operator()( Vector2& current, const PropertyInputContainer& inputs )
97 current = inputs[0]->GetVector3(); // World position?
99 // should be updated when:
100 // Magnifier's world position/size/scale/parentorigin/anchorpoint changes.
101 // or Magnifier camera's world position changes.
102 Vector3 size = inputs[1]->GetVector3() * inputs[2]->GetVector3(); /* magnifier-size * magnifier-scale */
104 // Reposition, and resize viewport to reflect the world bounds of this Magnifier actor.
105 current.x += ( mStageSize.width - size.width ) * 0.5f;
106 current.y += ( mStageSize.height - size.height ) * 0.5f;
112 struct RenderTaskViewportSizeConstraint
114 RenderTaskViewportSizeConstraint()
118 void operator()( Vector2& current, const PropertyInputContainer& inputs )
120 current = inputs[0]->GetVector3() * inputs[1]->GetVector3(); /* magnifier-size * magnifier-scale */
124 } // unnamed namespace
126 ///////////////////////////////////////////////////////////////////////////////////////////////////
128 ///////////////////////////////////////////////////////////////////////////////////////////////////
130 Dali::Toolkit::Magnifier Magnifier::New()
132 // Create the implementation
133 MagnifierPtr magnifier(new Magnifier());
135 // Pass ownership to CustomActor via derived handle
136 Dali::Toolkit::Magnifier handle(*magnifier);
138 // Second-phase init of the implementation
139 // This can only be done after the CustomActor connection has been made...
140 magnifier->Initialize();
145 Magnifier::Magnifier()
146 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
147 mDefaultCameraDistance(1000.f),
148 mActorSize(Vector3::ZERO),
149 mMagnificationFactor(1.0f)
153 void Magnifier::SetSourceActor(Actor actor)
155 mTask.SetSourceActor( actor );
158 void Magnifier::Initialize()
161 Vector2 stageSize(Stage::GetCurrent().GetSize());
164 // sourceActor is a dummy delegate actor that takes the source property position,
165 // and generates a WORLD_POSITION, which is 1 frame behind the source property.
166 // This way the constraints for determining the camera position (source) and those
167 // for determining viewport position use the same 1 frame old values.
168 // A simple i) CameraPos = f(B), ii) B = f(A) set of constraints wont suffice as
169 // although CameraPos will use B, which is A's previous value. The constraint will
170 // not realise that B is still dirty as far as constraint (i) is concerned.
171 // Perhaps this is a bug in the way the constraint system factors into what is dirty
173 mSourceActor = Actor::New();
174 Stage().GetCurrent().Add(mSourceActor);
175 mSourceActor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER );
176 Constraint constraint = Constraint::New<Vector3>( mSourceActor, Actor::Property::POSITION, EqualToConstraint() );
177 constraint.AddSource( Source( self, Toolkit::Magnifier::Property::SOURCE_POSITION ) );
180 // create the render task this will render content on top of everything
181 // based on camera source position.
182 InitializeRenderTask();
184 // set up some constraints to:
185 // i) reposition (dest) frame actor based on magnifier actor's world position (this is 1 frame delayed)
186 // ii) reposition and resize (dest) the render task's viewport based on magnifier actor's world position (1 frame delayed) & size.
187 // iii) reposition (source) camera actor based on magnifier source actor's world position (this is 1 frame delayed)
189 // Apply constraint to camera's position
190 // Position our camera at the same distance from its target as the default camera is.
191 // The camera position doesn't affect how we render, just what we render (due to near and far clip planes)
192 // NOTE: We can't interrogate the default camera's position as it is not known initially (takes 1 frame
193 // for value to update).
194 // But we can determine the initial position using the same formula:
195 // distance = stage.height * 0.5 / tan(FOV * 0.5)
197 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
198 RenderTask renderTask = taskList.GetTask(0u);
199 float fov = renderTask.GetCameraActor().GetFieldOfView();
200 mDefaultCameraDistance = (stageSize.height * 0.5f) / tanf(fov * 0.5f);
202 // Use a 1 frame delayed source position to determine the camera actor's position.
203 // This is necessary as the viewport is determined by the Magnifier's Actor's World position (which is computed
204 // at the end of the update cycle i.e. after constraints have been applied.)
205 //Property::Index propertySourcePositionDelayed = mCameraActor.RegisterProperty("delayedSourcePosition", Vector3::ZERO);
207 constraint = Constraint::New<Vector3>( mCameraActor, Actor::Property::POSITION, CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
208 constraint.AddSource( Source( mSourceActor, Actor::Property::WORLD_POSITION ) );
211 // Apply constraint to render-task viewport position
212 constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_POSITION, RenderTaskViewportPositionConstraint(stageSize) );
213 constraint.AddSource( Source( self, Actor::Property::WORLD_POSITION ) );
214 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
215 constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
218 // Apply constraint to render-task viewport position
219 constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_SIZE, RenderTaskViewportSizeConstraint() );
220 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
221 constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
224 DevelControl::SetAccessibilityConstructor( self, []( Dali::Actor actor ) {
225 return std::unique_ptr< Dali::Accessibility::Accessible >(
226 new Control::Impl::AccessibleImpl( actor, Dali::Accessibility::Role::FILLER ) );
230 Magnifier::~Magnifier()
235 void Magnifier::InitializeRenderTask()
237 Stage stage = Stage::GetCurrent();
239 RenderTaskList taskList = stage.GetRenderTaskList();
241 mTask = taskList.CreateTask();
242 mTask.SetInputEnabled(false);
243 mTask.SetClearColor(Vector4(0.5f, 0.5f, 0.5f, 1.0f));
244 mTask.SetClearEnabled(true);
246 mCameraActor = CameraActor::New();
247 mCameraActor.SetType(Camera::FREE_LOOK);
249 stage.Add(mCameraActor);
250 mTask.SetCameraActor( mCameraActor );
252 SetFrameVisibility(true);
255 bool Magnifier::GetFrameVisibility() const
260 void Magnifier::SetFrameVisibility(bool visible)
262 if(visible && !mFrame)
266 mFrame = Actor::New( );
267 mFrame.SetProperty( Actor::Property::INHERIT_POSITION, false );
268 mFrame.SetProperty( Actor::Property::INHERIT_SCALE, true );
269 mFrame.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
270 Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
271 mFrame.SetProperty( Actor::Property::SIZE_MODE_FACTOR, sizeOffset );
273 Toolkit::VisualFactory visualFactory = Toolkit::VisualFactory::Get();
276 map[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::BORDER;
277 map[ Toolkit::BorderVisual::Property::COLOR ] = Color::WHITE;
278 map[ Toolkit::BorderVisual::Property::SIZE ] = IMAGE_BORDER_INDENT;
279 Toolkit::Visual::Base borderVisual = visualFactory.CreateVisual( map );
280 Toolkit::GetImplementation(borderVisual).SetOnScene( mFrame );
282 Constraint constraint = Constraint::New<Vector3>( mFrame, Actor::Property::POSITION, EqualToConstraint() );
283 constraint.AddSource( ParentSource( Actor::Property::WORLD_POSITION ) );
288 else if(!visible && mFrame)
290 UnparentAndReset(mFrame);
294 void Magnifier::OnSizeSet(const Vector3& targetSize)
296 // TODO: Once Camera/CameraActor properties function as proper animatable properties
297 // this code can disappear.
298 // whenever the size of the magnifier changes, the field of view needs to change
299 // to compensate for the new size of the viewport. this cannot be done within
300 // a constraint yet as Camera/CameraActor properties are not animatable/constrainable.
301 mActorSize = targetSize;
304 Control::OnSizeSet( targetSize );
307 float Magnifier::GetMagnificationFactor() const
309 return mMagnificationFactor;
312 void Magnifier::SetMagnificationFactor(float value)
314 mMagnificationFactor = value;
318 void Magnifier::Update()
320 // TODO: Make Camera settings (fieldofview/aspectratio) as animatable constraints.
322 // should be updated when:
323 // Magnifier's world size/scale changes.
325 Vector3 worldSize = mActorSize * self.GetCurrentProperty< Vector3 >( Actor::Property::WORLD_SCALE );
327 // Adjust field of view to scale content
336 // |/ <--- fov/2 radians.
338 const float fov = atanf( 0.5f * worldSize.height / mDefaultCameraDistance / mMagnificationFactor) * 2.0f;
339 mCameraActor.SetFieldOfView( fov );
341 // Adjust aspect ratio to compensate for rectangular viewports.
342 mCameraActor.SetAspectRatio( worldSize.width / worldSize.height );
345 void Magnifier::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
347 Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
351 Magnifier& magnifierImpl( GetImpl( magnifier ) );
354 case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
356 magnifierImpl.SetFrameVisibility( value.Get< bool >() );
359 case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
361 magnifierImpl.SetMagnificationFactor( value.Get< float >() );
368 Property::Value Magnifier::GetProperty( BaseObject* object, Property::Index index )
370 Property::Value value;
372 Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
376 Magnifier& magnifierImpl( GetImpl( magnifier ) );
379 case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
381 value = magnifierImpl.GetFrameVisibility();
384 case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
386 value = magnifierImpl.GetMagnificationFactor();
395 } // namespace Internal
397 } // namespace Toolkit