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>
30 namespace // unnamed namespace
33 const char* DEFAULT_FRAME_IMAGE_PATH = DALI_IMAGE_DIR "magnifier-image-frame.png";
35 const float IMAGE_BORDER_INDENT = 14.0f; ///< Indent of border in pixels.
37 struct CameraActorPositionConstraint
39 CameraActorPositionConstraint(const Vector2& stageSize, float defaultCameraDistance = 0.0f)
40 : mStageSize(stageSize),
41 mDefaultCameraDistance(defaultCameraDistance)
45 void operator()( Vector3& current, const PropertyInputContainer& inputs )
47 const Vector3& sourcePosition = inputs[0]->GetVector3();
49 current.x = sourcePosition.x + mStageSize.x * 0.5f;
50 current.y = sourcePosition.y + mStageSize.y * 0.5f;
51 current.z = sourcePosition.z + mDefaultCameraDistance;
55 float mDefaultCameraDistance;
59 struct RenderTaskViewportPositionConstraint
61 RenderTaskViewportPositionConstraint(const Vector2& stageSize)
62 : mStageSize(stageSize)
66 void operator()( Vector2& current, const PropertyInputContainer& inputs )
68 current = inputs[0]->GetVector3(); // World position?
70 // should be updated when:
71 // Magnifier's world position/size/scale/parentorigin/anchorpoint changes.
72 // or Magnifier camera's world position changes.
73 Vector3 size = inputs[1]->GetVector3() * inputs[2]->GetVector3(); /* magnifier-size * magnifier-scale */
75 // Reposition, and resize viewport to reflect the world bounds of this Magnifier actor.
76 current.x += ( mStageSize.width - size.width ) * 0.5f;
77 current.y += ( mStageSize.height - size.height ) * 0.5f;
83 struct RenderTaskViewportSizeConstraint
85 RenderTaskViewportSizeConstraint()
89 void operator()( Vector2& current, const PropertyInputContainer& inputs )
91 current = inputs[0]->GetVector3() * inputs[1]->GetVector3(); /* magnifier-size * magnifier-scale */
106 ///////////////////////////////////////////////////////////////////////////////////////////////////
108 ///////////////////////////////////////////////////////////////////////////////////////////////////
110 Dali::Toolkit::Magnifier Magnifier::New()
112 // Create the implementation
113 MagnifierPtr magnifier(new Magnifier());
115 // Pass ownership to CustomActor via derived handle
116 Dali::Toolkit::Magnifier handle(*magnifier);
118 // Second-phase init of the implementation
119 // This can only be done after the CustomActor connection has been made...
120 magnifier->Initialize();
125 Magnifier::Magnifier()
126 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
127 mPropertySourcePosition(Property::INVALID_INDEX),
128 mDefaultCameraDistance(1000.f),
129 mActorSize(Vector3::ZERO),
130 mMagnificationFactor(1.0f)
134 void Magnifier::SetSourceActor(Actor actor)
136 mTask.SetSourceActor( actor );
139 void Magnifier::SetSourcePosition(const Vector3& position)
141 Self().SetProperty(mPropertySourcePosition, position);
144 void Magnifier::Initialize()
147 mPropertySourcePosition = self.RegisterProperty( Toolkit::Magnifier::SOURCE_POSITION_PROPERTY_NAME, Vector3::ZERO );
148 Vector2 stageSize(Stage::GetCurrent().GetSize());
151 // sourceActor is a dummy delegate actor that takes the source property position,
152 // and generates a WORLD_POSITION, which is 1 frame behind the source property.
153 // This way the constraints for determining the camera position (source) and those
154 // for determining viewport position use the same 1 frame old values.
155 // A simple i) CameraPos = f(B), ii) B = f(A) set of constraints wont suffice as
156 // although CameraPos will use B, which is A's previous value. The constraint will
157 // not realise that B is still dirty as far as constraint (i) is concerned.
158 // Perhaps this is a bug in the way the constraint system factors into what is dirty
160 mSourceActor = Actor::New();
161 Stage().GetCurrent().Add(mSourceActor);
162 mSourceActor.SetParentOrigin(ParentOrigin::CENTER);
163 Constraint constraint = Constraint::New<Vector3>( mSourceActor, Actor::Property::POSITION, EqualToConstraint() );
164 constraint.AddSource( Source( self, mPropertySourcePosition ) );
167 // create the render task this will render content on top of everything
168 // based on camera source position.
169 InitializeRenderTask();
171 // set up some constraints to:
172 // i) reposition (dest) frame actor based on magnifier actor's world position (this is 1 frame delayed)
173 // ii) reposition and resize (dest) the render task's viewport based on magnifier actor's world position (1 frame delayed) & size.
174 // iii) reposition (source) camera actor based on magnifier source actor's world position (this is 1 frame delayed)
176 // Apply constraint to camera's position
177 // Position our camera at the same distance from its target as the default camera is.
178 // The camera position doesn't affect how we render, just what we render (due to near and far clip planes)
179 // NOTE: We can't interrogate the default camera's position as it is not known initially (takes 1 frame
180 // for value to update).
181 // But we can determine the initial position using the same formula:
182 // distance = stage.height * 0.5 / tan(FOV * 0.5)
184 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
185 RenderTask renderTask = taskList.GetTask(0u);
186 float fov = renderTask.GetCameraActor().GetFieldOfView();
187 mDefaultCameraDistance = (stageSize.height * 0.5f) / tanf(fov * 0.5f);
189 // Use a 1 frame delayed source position to determine the camera actor's position.
190 // This is necessary as the viewport is determined by the Magnifier's Actor's World position (which is computed
191 // at the end of the update cycle i.e. after constraints have been applied.)
192 //Property::Index propertySourcePositionDelayed = mCameraActor.RegisterProperty("delayed-source-position", Vector3::ZERO);
194 constraint = Constraint::New<Vector3>( mCameraActor, Actor::Property::POSITION, CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
195 constraint.AddSource( Source( mSourceActor, Actor::Property::WORLD_POSITION ) );
198 // Apply constraint to render-task viewport position
199 constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_POSITION, RenderTaskViewportPositionConstraint(stageSize) );
200 constraint.AddSource( Source( self, Actor::Property::WORLD_POSITION ) );
201 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
202 constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
205 // Apply constraint to render-task viewport position
206 constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_SIZE, RenderTaskViewportSizeConstraint() );
207 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
208 constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
212 Magnifier::~Magnifier()
217 void Magnifier::InitializeRenderTask()
219 Stage stage = Stage::GetCurrent();
221 RenderTaskList taskList = stage.GetRenderTaskList();
223 mTask = taskList.CreateTask();
224 mTask.SetInputEnabled(false);
225 mTask.SetClearColor(Vector4(0.5f, 0.5f, 0.5f, 1.0f));
226 mTask.SetClearEnabled(true);
228 mCameraActor = CameraActor::New();
229 mCameraActor.SetType(Camera::FREE_LOOK);
231 stage.Add(mCameraActor);
232 mTask.SetCameraActor( mCameraActor );
234 SetFrameVisibility(true);
237 bool Magnifier::GetFrameVisibility() const
242 void Magnifier::SetFrameVisibility(bool visible)
244 if(visible && !mFrame)
248 Image image = ResourceImage::New( DEFAULT_FRAME_IMAGE_PATH );
249 mFrame = ImageActor::New( image );
250 mFrame.SetDrawMode(DrawMode::OVERLAY);
251 mFrame.SetStyle( ImageActor::STYLE_NINE_PATCH );
252 mFrame.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
253 mFrame.SetInheritScale(true);
254 mFrame.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
255 Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
256 mFrame.SetSizeModeFactor( sizeOffset );
258 Constraint constraint = Constraint::New<Vector3>( mFrame, Actor::Property::POSITION, EqualToConstraint() );
259 constraint.AddSource( ParentSource( Actor::Property::WORLD_POSITION ) );
262 mFrame.SetNinePatchBorder( Vector4::ONE * IMAGE_BORDER_INDENT );
265 else if(!visible && mFrame)
267 UnparentAndReset(mFrame);
271 void Magnifier::OnControlSizeSet(const Vector3& targetSize)
273 // TODO: Once Camera/CameraActor properties function as proper animatable properties
274 // this code can disappear.
275 // whenever the size of the magnifier changes, the field of view needs to change
276 // to compensate for the new size of the viewport. this cannot be done within
277 // a constraint yet as Camera/CameraActor properties are not animatable/constrainable.
278 mActorSize = targetSize;
282 float Magnifier::GetMagnificationFactor() const
284 return mMagnificationFactor;
287 void Magnifier::SetMagnificationFactor(float value)
289 mMagnificationFactor = value;
293 void Magnifier::Update()
295 // TODO: Make Camera settings (fieldofview/aspectratio) as animatable constraints.
297 // should be updated when:
298 // Magnifier's world size/scale changes.
300 Vector3 worldSize = mActorSize * self.GetCurrentWorldScale();
302 // Adjust field of view to scale content
311 // |/ <--- fov/2 radians.
313 const float fov = atanf( 0.5f * worldSize.height / mDefaultCameraDistance / mMagnificationFactor) * 2.0f;
314 mCameraActor.SetFieldOfView( fov );
316 // Adjust aspect ratio to compensate for rectangular viewports.
317 mCameraActor.SetAspectRatio( worldSize.width / worldSize.height );
320 } // namespace Internal
322 } // namespace Toolkit