Added PathConstraint object.
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / path-constraint-impl.h
1 #ifndef __DALI_INTERNAL_PATH_CONSTRAINT_H__
2 #define __DALI_INTERNAL_PATH_CONSTRAINT_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/animation/path-impl.h>
23 #include <dali/internal/event/common/object-impl.h>
24 #include <dali/public-api/animation/path-constraint.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 typedef IntrusivePtr<PathConstraint>  PathConstraintPtr;
33 typedef Dali::Vector<Object*>         ObjectContainer;
34 typedef ObjectContainer::Iterator     ObjectIter;
35
36 /**
37  * @brief Constraint functor to constraint properties to paths.
38  *
39  * Vector3 properties will be constrained to the position of the path and
40  * Rotation properties will be constrained to follow the tangent of the path
41  * given a forward vector in object's local space.
42  */
43 struct PathConstraintFunctor
44 {
45   /**
46    * @brief Constructor.
47    *
48    * @param[in] path The path used in the constraint
49    * @param[in] range The range of values in the input property which will be mapped to 0..1
50    */
51
52   PathConstraintFunctor(PathPtr path, const Vector2& range ):mPath(path),mRange(range){}
53
54   /**
55    * @brief Constructor.
56    *
57    * @param[in] path The path used in the constraint
58    * @param[in] range The range of values in the input property which will be mapped to 0..1
59    * @param[in] forward Vector in object space which will be aligned with the tangent of the path
60    */
61   PathConstraintFunctor(PathPtr path, const Vector2& range,const Vector3& forward ):mPath(path),mForward(forward),mRange(range){}
62
63   /**
64    * @brief Functor operator for Vector3 properties
65    *
66    * @param[in] current Current value of the property
67    * @param[in] property The input property used as the parameter for the path
68    *
69    * @return The position of the path at the given parameter.
70    */
71   Vector3 operator()(const Vector3& current,
72                      const PropertyInput& property)
73   {
74     float t = ( property.GetFloat() - mRange.x ) / (mRange.y-mRange.x);
75     Vector3 position, tangent;
76     mPath->Sample( t, position, tangent );
77     return position;
78   }
79
80   /**
81    * @brief Functor operator for Quaternion properties
82    *
83    * @param[in] current Current value of the property
84    * @param[in] property The input property used as the parameter for the path
85    *
86    * @return The rotation which will align the forward vector and the tangent of the path at the given parameter.
87    */
88   Quaternion operator()( const Quaternion& current,
89                          const PropertyInput& property)
90   {
91     float t = ( property.GetFloat() - mRange.x ) / (mRange.y-mRange.x);
92     Vector3 position, tangent;
93     mPath->Sample( t, position, tangent );
94     return Quaternion( mForward, tangent );
95   }
96
97   PathPtr     mPath;      ///< The path used
98   Vector3     mForward;   ///< Vector in object space which will be aligned with the tangent of the path
99   Vector2     mRange;     ///< The range of values in the input property which will be mapped to 0..1
100 };
101
102 /**
103  * @brief A PathConstraint used to constraint properties to a path
104  */
105 class PathConstraint : public Object, public Object::Observer
106 {
107 public:
108
109   /**
110    * Create a new PathConstraint
111    * @param[in] path The path used in the constraint
112    * @param[in] range The range of values in the input property which will be mapped to 0..1
113    * @return A smart-pointer to the newly allocated PathConstraint.
114    */
115   static PathConstraint* New( Path& path, const Vector2& range );
116
117
118
119 protected:
120   /**
121    * virtual destructor
122    */
123   virtual ~PathConstraint();
124
125 private:
126   /**
127    * @copydoc Dali::Internal::Object::Observer::SceneObjectAdded()
128    */
129   virtual void SceneObjectAdded(Object& object){}
130
131   /**
132    * @copydoc Dali::Internal::Object::Observer::SceneObjectAdded()
133    */
134   virtual void SceneObjectRemoved(Object& object){}
135
136   /**
137    * @copydoc Dali::Internal::Object::Observer::ObjectDestroyed()
138    */
139   virtual void ObjectDestroyed(Object& object);
140
141   /**
142    * @copydoc Dali::Internal::Object::GetDefaultPropertyCount()
143    */
144   virtual unsigned int GetDefaultPropertyCount() const;
145
146   /**
147    * @copydoc Dali::Internal::Object::GetDefaultPropertyIndices()
148    */
149   virtual void GetDefaultPropertyIndices( Property::IndexContainer& indices ) const;
150
151   /**
152    * @copydoc Dali::Internal::Object::GetDefaultPropertyName()
153    */
154   virtual const char* GetDefaultPropertyName(Property::Index index) const;
155
156   /**
157    * @copydoc Dali::Internal::Object::GetDefaultPropertyIndex()
158    */
159   virtual Property::Index GetDefaultPropertyIndex(const std::string& name) const;
160
161   /**
162    * @copydoc Dali::Internal::Object::IsDefaultPropertyWritable()
163    */
164   virtual bool IsDefaultPropertyWritable(Property::Index index) const;
165
166   /**
167    * @copydoc Dali::Internal::Object::IsDefaultPropertyAnimatable()
168    */
169   virtual bool IsDefaultPropertyAnimatable(Property::Index index) const;
170
171   /**
172    * @copydoc Dali::Internal::Object::IsDefaultPropertyAConstraintInput()
173    */
174   virtual bool IsDefaultPropertyAConstraintInput( Property::Index index ) const;
175
176   /**
177    * @copydoc Dali::Internal::Object::GetDefaultPropertyType()
178    */
179   virtual Property::Type GetDefaultPropertyType(Property::Index index) const;
180
181   /**
182    * @copydoc Dali::Internal::Object::SetDefaultProperty()
183    */
184   virtual void SetDefaultProperty(Property::Index index, const Property::Value& propertyValue);
185
186    /**
187    * @copydoc Dali::Internal::Object::GetDefaultProperty()
188    */
189   virtual Property::Value GetDefaultProperty( Property::Index index ) const;
190
191   /**
192    * @copydoc Dali::Internal::Object::GetSceneObject()
193    */
194   virtual const SceneGraph::PropertyOwner* GetSceneObject() const{ return NULL; }
195
196   /**
197    * @copydoc Dali::Internal::Object::GetSceneObjectAnimatableProperty()
198    */
199   virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const{ return NULL; }
200
201   /**
202    * @copydoc Dali::Internal::Object::GetSceneObjectInputProperty()
203    */
204   virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const{ return NULL; }
205
206 public:
207
208   /**
209    * @copydoc Dali::PathConstraint::Apply
210    */
211   void Apply( Property source, Property target, const Vector3& forward );
212
213   /**
214    * @copydoc Dali::PathConstraint::Remove
215    */
216   void Remove( Dali::Handle& target );
217
218
219 private:
220   /**
221    * Constructor
222    * @param[in] path The path used in the constraint
223    * @param[in] range The range of values in the input property which will be mapped to 0..1
224    */
225   PathConstraint( Path& path, const Vector2& range );
226
227   // Undefined
228   PathConstraint();
229
230   // Undefined
231   PathConstraint(const PathConstraint&);
232
233   // Undefined
234   PathConstraint& operator=(const PathConstraint& rhs);
235
236   PathPtr           mPath;              ///< The path used to constrain objects
237   ObjectContainer   mObservedObjects;   ///< The list of object which have been constrained by the PathConstraint
238   Vector2           mRange;             ///< The range of values in the input property which will be mapped to 0..1
239 };
240
241 } // Internal
242
243 // Get impl of handle
244 inline Internal::PathConstraint& GetImplementation(Dali::PathConstraint& pathConstraint)
245 {
246   DALI_ASSERT_ALWAYS( pathConstraint && "PathConstraint handle is empty" );
247   Dali::RefObject& object = pathConstraint.GetBaseObject();
248   return static_cast<Internal::PathConstraint&>(object);
249 }
250
251 inline const Internal::PathConstraint& GetImplementation(const Dali::PathConstraint& pathConstraint)
252 {
253   DALI_ASSERT_ALWAYS( pathConstraint && "PathConstraint handle is empty" );
254   const Dali::RefObject& object = pathConstraint.GetBaseObject();
255   return static_cast<const Internal::PathConstraint&>(object);
256 }
257
258 } // Dali
259
260 #endif //__DALI_INTERNAL_KEY_FRAMES_H__