Remove OnControl methods & add up-calls
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / magnifier / magnifier-impl.cpp
index bb57902..dfc7e1b 100644 (file)
@@ -1,50 +1,63 @@
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
+// CLASS HEADER
 #include <dali-toolkit/internal/controls/magnifier/magnifier-impl.h>
 
-using namespace Dali;
+// EXTERNAL INCLUDES
+#include <dali/public-api/animation/constraint.h>
+#include <dali/public-api/animation/constraints.h>
+#include <dali/public-api/common/stage.h>
+#include <dali/public-api/render-tasks/render-task-list.h>
+#include <dali/public-api/images/resource-image.h>
+#include <dali/public-api/object/type-registry.h>
+#include <dali/devel-api/object/type-registry-helper.h>
 
-namespace // unnamed namespace
+// INTERNAL INCLUDES
+#include <dali-toolkit/devel-api/controls/renderer-factory/renderer-factory.h>
+
+namespace Dali
 {
 
-const char* DEFAULT_FRAME_IMAGE_PATH = DALI_IMAGE_DIR "magnifier-image-frame.png";
+namespace Toolkit
+{
 
-const float IMAGE_BORDER_INDENT = 14.0f;            ///< Indent of border in pixels.
+namespace Internal
+{
 
-/**
- * ImageBorderSizeConstraint
- */
-struct ImageBorderSizeConstraint
+namespace // unnamed namespace
 {
-  ImageBorderSizeConstraint()
-  : mSizeOffset(Vector3(IMAGE_BORDER_INDENT - 1, IMAGE_BORDER_INDENT - 1, 0.0f) * 2.0f)
-  {
-  }
 
-  Vector3 operator()(const Vector3&    current,
-                     const PropertyInput& referenceSizeProperty)
-  {
-    const Vector3& referenceSize = referenceSizeProperty.GetVector3();
 
-    return referenceSize + mSizeOffset;
-  }
+Dali::BaseHandle Create()
+{
+  return Toolkit::Magnifier::New();
+}
 
-  Vector3 mSizeOffset;                        ///< The amount to offset the size from referenceSize
-};
+DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Magnifier, Toolkit::Control, Create )
+
+DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "frameVisibility",      BOOLEAN, FRAME_VISIBILITY     )
+DALI_PROPERTY_REGISTRATION( Toolkit, Magnifier, "magnificationFactor",  FLOAT,   MAGNIFICATION_FACTOR )
+
+DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, Magnifier, "sourcePosition",  VECTOR3, SOURCE_POSITION )
+
+DALI_TYPE_REGISTRATION_END()
+
+const float IMAGE_BORDER_INDENT = 5.0f;            ///< Indent of border in pixels.
 
 struct CameraActorPositionConstraint
 {
@@ -54,14 +67,13 @@ struct CameraActorPositionConstraint
   {
   }
 
-  Vector3 operator()(const Vector3& current,
-                     const PropertyInput& sourcePositionProperty)
+  void operator()( Vector3& current, const PropertyInputContainer& inputs )
   {
-    const Vector3& sourcePosition = sourcePositionProperty.GetVector3();
+    const Vector3& sourcePosition = inputs[0]->GetVector3();
 
-    return Vector3( sourcePosition.x + mStageSize.x * 0.5f,
-                    sourcePosition.y + mStageSize.y * 0.5f,
-                    sourcePosition.z + mDefaultCameraDistance);
+    current.x = sourcePosition.x + mStageSize.x * 0.5f;
+    current.y = sourcePosition.y + mStageSize.y * 0.5f;
+    current.z = sourcePosition.z + mDefaultCameraDistance;
   }
 
   Vector2 mStageSize;
@@ -76,25 +88,18 @@ struct RenderTaskViewportPositionConstraint
   {
   }
 
-  Vector2 operator()(const Vector2& current,
-                     const PropertyInput& positionProperty,
-                     const PropertyInput& magnifierSizeProperty,
-                     const PropertyInput& magnifierScaleProperty)
+  void operator()( Vector2& current, const PropertyInputContainer& inputs )
   {
-    Vector2 position(positionProperty.GetVector3()); // World position?
-
-    //position -= mStageSize * 0.5f;
+    current = inputs[0]->GetVector3(); // World position?
 
     // should be updated when:
     // Magnifier's world position/size/scale/parentorigin/anchorpoint changes.
     // or Magnifier camera's world position changes.
-    Vector3 size = magnifierSizeProperty.GetVector3() * magnifierScaleProperty.GetVector3();
+    Vector3 size = inputs[1]->GetVector3() * inputs[2]->GetVector3(); /* magnifier-size * magnifier-scale */
 
     // Reposition, and resize viewport to reflect the world bounds of this Magnifier actor.
-    position.x += (mStageSize.width - size.width) * 0.5f;
-    position.y += (mStageSize.height - size.height) * 0.5f;
-
-    return position;
+    current.x += ( mStageSize.width - size.width ) * 0.5f;
+    current.y += ( mStageSize.height - size.height ) * 0.5f;
   }
 
   Vector2 mStageSize;
@@ -106,40 +111,13 @@ struct RenderTaskViewportSizeConstraint
   {
   }
 
-  Vector2 operator()(const Vector2& current,
-                     const PropertyInput& magnifierSizeProperty,
-                     const PropertyInput& magnifierScaleProperty)
+  void operator()( Vector2& current, const PropertyInputContainer& inputs )
   {
-    return Vector2(magnifierSizeProperty.GetVector3() * magnifierScaleProperty.GetVector3());
+    current = inputs[0]->GetVector3() * inputs[1]->GetVector3(); /* magnifier-size * magnifier-scale */
   }
 };
 
-/**
- * Returns relative border (0.0f...1.0f x 0.0f...1.0f)
- * from an absolute pixel specified border.
- * @param[in] absolute A border using absolute pixel coordinates
- * @param[in] width The width of the texture
- * @param[in] height The height of the texture.
- * @return A border relative to the size of the Image texture dimensions.
- */
-Vector4 GetRelativeBorder(Vector4 absolute, float width, float height)
-{
-  return Vector4( absolute.x / width,
-                  absolute.y / height,
-                  absolute.z / width,
-                  absolute.w / height);
-}
-
-}
-
-namespace Dali
-{
-
-namespace Toolkit
-{
-
-namespace Internal
-{
+} // unnamed namespace
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Magnifier
@@ -161,8 +139,8 @@ Dali::Toolkit::Magnifier Magnifier::New()
 }
 
 Magnifier::Magnifier()
-: ControlImpl(true),
-  mPropertySourcePosition(Property::INVALID_INDEX),
+: Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
+  mDefaultCameraDistance(1000.f),
   mActorSize(Vector3::ZERO),
   mMagnificationFactor(1.0f)
 {
@@ -173,21 +151,11 @@ void Magnifier::SetSourceActor(Actor actor)
   mTask.SetSourceActor( actor );
 }
 
-void Magnifier::SetSourcePosition(const Vector3& position)
-{
-  Self().SetProperty(mPropertySourcePosition, position);
-}
-
 void Magnifier::Initialize()
 {
   Actor self = Self();
-  mPropertySourcePosition = self.RegisterProperty( Toolkit::Magnifier::SOURCE_POSITION_PROPERTY_NAME, Vector3::ZERO );
   Vector2 stageSize(Stage::GetCurrent().GetSize());
 
-  Layer dummyLayer = Layer::New();
-  Stage().GetCurrent().Add(dummyLayer);
-  dummyLayer.SetParentOrigin(ParentOrigin::CENTER);
-
   // NOTE:
   // sourceActor is a dummy delegate actor that takes the source property position,
   // and generates a WORLD_POSITION, which is 1 frame behind the source property.
@@ -199,12 +167,11 @@ void Magnifier::Initialize()
   // Perhaps this is a bug in the way the constraint system factors into what is dirty
   // and what is not.
   mSourceActor = Actor::New();
-  dummyLayer.Add(mSourceActor);
+  Stage().GetCurrent().Add(mSourceActor);
   mSourceActor.SetParentOrigin(ParentOrigin::CENTER);
-  Constraint constraint = Constraint::New<Vector3>( Actor::POSITION,
-                                                    Source( self, mPropertySourcePosition ),
-                                                    EqualToConstraint() );
-  mSourceActor.ApplyConstraint(constraint);
+  Constraint constraint = Constraint::New<Vector3>( mSourceActor, Actor::Property::POSITION, EqualToConstraint() );
+  constraint.AddSource( Source( self, Toolkit::Magnifier::Property::SOURCE_POSITION ) );
+  constraint.Apply();
 
   // create the render task this will render content on top of everything
   // based on camera source position.
@@ -231,27 +198,24 @@ void Magnifier::Initialize()
   // Use a 1 frame delayed source position to determine the camera actor's position.
   // This is necessary as the viewport is determined by the Magnifier's Actor's World position (which is computed
   // at the end of the update cycle i.e. after constraints have been applied.)
-  //Property::Index propertySourcePositionDelayed = mCameraActor.RegisterProperty("delayed-source-position", Vector3::ZERO);
+  //Property::Index propertySourcePositionDelayed = mCameraActor.RegisterProperty("delayedSourcePosition",   Vector3::ZERO);
 
-  constraint = Constraint::New<Vector3>( Actor::POSITION,
-                                                    Source( mSourceActor, Actor::WORLD_POSITION ),
-                                                    CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
-  mCameraActor.ApplyConstraint(constraint);
+  constraint = Constraint::New<Vector3>( mCameraActor, Actor::Property::POSITION, CameraActorPositionConstraint(stageSize, mDefaultCameraDistance) );
+  constraint.AddSource( Source( mSourceActor, Actor::Property::WORLD_POSITION ) );
+  constraint.Apply();
 
   // Apply constraint to render-task viewport position
-  constraint = Constraint::New<Vector2>( RenderTask::VIEWPORT_POSITION,
-                                         Source( self, Actor::WORLD_POSITION ),//mPropertySourcePosition ),
-                                         Source( self, Actor::SIZE ),
-                                         Source( self, Actor::WORLD_SCALE),
-                                         RenderTaskViewportPositionConstraint(stageSize) );
-  mTask.ApplyConstraint(constraint);
+  constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_POSITION, RenderTaskViewportPositionConstraint(stageSize) );
+  constraint.AddSource( Source( self, Actor::Property::WORLD_POSITION ) );
+  constraint.AddSource( Source( self, Actor::Property::SIZE ) );
+  constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
+  constraint.Apply();
 
   // Apply constraint to render-task viewport position
-  constraint = Constraint::New<Vector2>( RenderTask::VIEWPORT_SIZE,
-                                         Source( self, Actor::SIZE ),
-                                         Source( self, Actor::WORLD_SCALE),
-                                         RenderTaskViewportSizeConstraint() );
-  mTask.ApplyConstraint(constraint);
+  constraint = Constraint::New<Vector2>( mTask, RenderTask::Property::VIEWPORT_SIZE, RenderTaskViewportSizeConstraint() );
+  constraint.AddSource( Source( self, Actor::Property::SIZE ) );
+  constraint.AddSource( Source( self, Actor::Property::WORLD_SCALE ) );
+  constraint.Apply();
 }
 
 Magnifier::~Magnifier()
@@ -272,7 +236,6 @@ void Magnifier::InitializeRenderTask()
 
   mCameraActor = CameraActor::New();
   mCameraActor.SetType(Camera::FREE_LOOK);
-  mCameraActor.SetRotation(Quaternion(M_PI, Vector3::YAXIS)); // Look at stage
 
   stage.Add(mCameraActor);
   mTask.SetCameraActor( mCameraActor );
@@ -282,56 +245,43 @@ void Magnifier::InitializeRenderTask()
 
 bool Magnifier::GetFrameVisibility() const
 {
-  return mFrameLayer;
+  return mFrame;
 }
 
 void Magnifier::SetFrameVisibility(bool visible)
 {
-  if(visible && !mFrameLayer)
+  if(visible && !mFrame)
   {
     Actor self(Self());
 
-    Layer mFrameLayer = Layer::New();
-    mFrameLayer.SetParentOrigin( ParentOrigin::CENTER );
-    Stage::GetCurrent().Add(mFrameLayer);
-
-    Image image = Image::New( DEFAULT_FRAME_IMAGE_PATH );
-    ImageActor frame = ImageActor::New( image );
-    frame.SetDrawMode(DrawMode::OVERLAY);
-    frame.SetStyle( ImageActor::STYLE_NINE_PATCH );
-
-    frame.SetNinePatchBorder( Vector4::ONE * IMAGE_BORDER_INDENT );
-    mFrameLayer.Add(frame);
-
-    // Apply position constraint to the frame
-    Constraint constraint = Constraint::New<Vector3>( Actor::POSITION,
-                                                      Source( self, Actor::WORLD_POSITION ),
-                                                      EqualToConstraint() );
-    frame.ApplyConstraint(constraint);
-
-    // Apply scale constraint to the frame
-    constraint = Constraint::New<Vector3>( Actor::SCALE,
-                                           Source( self, Actor::SCALE ),
-                                           EqualToConstraint() );
-    frame.ApplyConstraint(constraint);
-
-    Source(self, Actor::SCALE),
-
-    // Apply size constraint to the the frame
-    constraint = Constraint::New<Vector3>(Actor::SIZE,
-                                          Source(self, Actor::SIZE),
-                                          ImageBorderSizeConstraint());
-    frame.ApplyConstraint(constraint);
+    mFrame = Actor::New( );
+    mFrame.SetInheritPosition(false);
+    mFrame.SetInheritScale(true);
+    mFrame.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
+    Vector3 sizeOffset(IMAGE_BORDER_INDENT*2.f - 2.f, IMAGE_BORDER_INDENT*2.f - 2.f, 0.0f);
+    mFrame.SetSizeModeFactor( sizeOffset );
+
+    //TODO Set the renderer onto the control self when Actor::RemoveRenderer is supported
+    Toolkit::RendererFactory rendererFactory = Toolkit::RendererFactory::Get();
+    Toolkit::ControlRenderer borderRenderer = rendererFactory.GetControlRenderer(IMAGE_BORDER_INDENT, Color::WHITE);
+    borderRenderer.SetOnStage( mFrame );
+
+    Constraint constraint = Constraint::New<Vector3>( mFrame, Actor::Property::POSITION, EqualToConstraint() );
+    constraint.AddSource( ParentSource( Actor::Property::WORLD_POSITION ) );
+    constraint.Apply();
+
+    self.Add(mFrame);
   }
-  else if(!visible && mFrameLayer)
+  else if(!visible && mFrame)
   {
-    Stage::GetCurrent().Remove(mFrameLayer);
-    mFrameLayer.Reset();
+    UnparentAndReset(mFrame);
   }
 }
 
-void Magnifier::OnControlSizeSet(const Vector3& targetSize)
+void Magnifier::OnSizeSet(const Vector3& targetSize)
 {
+  Control::OnSizeSet( targetSize );
+
   // TODO: Once Camera/CameraActor properties function as proper animatable properties
   // this code can disappear.
   // whenever the size of the magnifier changes, the field of view needs to change
@@ -379,6 +329,56 @@ void Magnifier::Update()
   mCameraActor.SetAspectRatio( worldSize.width / worldSize.height );
 }
 
+void Magnifier::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
+{
+  Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
+
+  if( magnifier )
+  {
+    Magnifier& magnifierImpl( GetImpl( magnifier ) );
+    switch( index )
+    {
+      case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
+      {
+        magnifierImpl.SetFrameVisibility( value.Get< bool >() );
+        break;
+      }
+      case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
+      {
+        magnifierImpl.SetMagnificationFactor( value.Get< float >() );
+        break;
+      }
+    }
+  }
+}
+
+Property::Value Magnifier::GetProperty( BaseObject* object, Property::Index index )
+{
+  Property::Value value;
+
+  Toolkit::Magnifier magnifier = Toolkit::Magnifier::DownCast( Dali::BaseHandle( object ) );
+
+  if( magnifier )
+  {
+    Magnifier& magnifierImpl( GetImpl( magnifier ) );
+    switch( index )
+    {
+      case Toolkit::Magnifier::Property::FRAME_VISIBILITY:
+      {
+        value = magnifierImpl.GetFrameVisibility();
+        break;
+      }
+      case Toolkit::Magnifier::Property::MAGNIFICATION_FACTOR:
+      {
+        value = magnifierImpl.GetMagnificationFactor();
+        break;
+      }
+    }
+  }
+
+  return value;
+}
+
 } // namespace Internal
 
 } // namespace Toolkit