1 #ifndef __DALI_INTERNAL_LINEAR_CONSTRAINER_H__
2 #define __DALI_INTERNAL_LINEAR_CONSTRAINER_H__
5 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/internal/event/animation/constrainer.h>
23 #include <dali/public-api/animation/linear-constrainer.h>
24 #include <dali/public-api/math/math-utils.h>
32 typedef IntrusivePtr<LinearConstrainer> LinearConstrainerPtr;
35 * @brief Constraint functor to constraint properties given a linear map.
37 struct LinearConstraintFunctor
42 * @param[in] value The list of values for the linear map (f(x) of the linear map)
43 * @param[in] progress Progress for each of the values normalized to [0,1] ( x of the linear map)
44 * @param[in] range The range of values in the input property which will be mapped to [0,1]
45 * @param[in] wrap Wrapping domain. Input property value will be wrapped in the domain [wrap.x,wrap.y] before mapping to [0,1]
47 * @note If progress is an empty vector, the values will be assumed to be equally spaced in the x-axis
49 LinearConstraintFunctor( Dali::Vector<float>& value, Dali::Vector<float>& progress, const Vector2& range, const Vector2& wrap )
57 * @brief Functor operator for float properties
59 * @param[out] value Current value of the property
60 * @param[in] inputs Contains the input property used as the parameter for the path
62 * @return The value of the linear map at the given parameter.
64 void operator()( float& value,
65 const PropertyInputContainer& inputs)
67 size_t valueCount( mValue.Size() );
72 else if(valueCount == 1 )
78 float inputWrapped = inputs[0]->GetFloat();
79 if( inputWrapped < mWrap.x || inputWrapped > mWrap.y )
81 inputWrapped = WrapInDomain(inputWrapped, mWrap.x, mWrap.y);
84 float t = (( inputWrapped - mRange.x ) / ( mRange.y-mRange.x ));
86 //Find min and max values and local t between them
90 if( mProgress.Size() < valueCount )
92 float step = 1.0f / (valueCount-1.0f);
93 float tLocation = t/step;
99 else if( tLocation >= valueCount-1 )
101 min = max = valueCount-1;
105 min = static_cast<size_t>(tLocation);
109 tLocal = (t - min*step) / step;
113 while( t >= mProgress[min] && min < valueCount-1 )
121 if( min >= valueCount-1)
123 min = max = valueCount-1;
128 tLocal =(t - mProgress[min]) / ( mProgress[max]-mProgress[min]);
132 //Linear interpolation
133 value = (mValue[max]-mValue[min])*tLocal + mValue[min];
138 Dali::Vector<float> mValue; ///< values for the linear map
139 Dali::Vector<float> mProgress; ///< Progress for each of the values normalized to [0,1]
140 Vector2 mRange; ///< The range of values in the input property which will be mapped to 0..1
141 Vector2 mWrap; ///< Wrapping domain. Input property will be wrapped in this domain before being mapped to [0,1]
145 * @brief A LinearConstrainer used to constraint properties given a linear map
147 class LinearConstrainer : public Constrainer
152 * Create a new LinearConstrainer
153 * @return A smart-pointer to the newly allocated LinearConstrainer.
155 static LinearConstrainer* New();
162 virtual ~LinearConstrainer();
167 * @copydoc Dali::Internal::Object::GetDefaultPropertyCount()
169 virtual unsigned int GetDefaultPropertyCount() const;
172 * @copydoc Dali::Internal::Object::GetDefaultPropertyIndices()
174 virtual void GetDefaultPropertyIndices( Property::IndexContainer& indices ) const;
177 * @copydoc Dali::Internal::Object::GetDefaultPropertyName()
179 virtual const char* GetDefaultPropertyName(Property::Index index) const;
182 * @copydoc Dali::Internal::Object::GetDefaultPropertyIndex()
184 virtual Property::Index GetDefaultPropertyIndex(const std::string& name) const;
187 * @copydoc Dali::Internal::Object::IsDefaultPropertyWritable()
189 virtual bool IsDefaultPropertyWritable(Property::Index index) const;
192 * @copydoc Dali::Internal::Object::IsDefaultPropertyAnimatable()
194 virtual bool IsDefaultPropertyAnimatable(Property::Index index) const;
197 * @copydoc Dali::Internal::Object::IsDefaultPropertyAConstraintInput()
199 virtual bool IsDefaultPropertyAConstraintInput( Property::Index index ) const;
202 * @copydoc Dali::Internal::Object::GetDefaultPropertyType()
204 virtual Property::Type GetDefaultPropertyType(Property::Index index) const;
207 * @copydoc Dali::Internal::Object::SetDefaultProperty()
209 virtual void SetDefaultProperty(Property::Index index, const Property::Value& propertyValue);
212 * @copydoc Dali::Internal::Object::GetDefaultProperty()
214 virtual Property::Value GetDefaultProperty( Property::Index index ) const;
217 * @copydoc Dali::Internal::Object::GetDefaultPropertyCurrentValue()
219 virtual Property::Value GetDefaultPropertyCurrentValue( Property::Index index ) const;
224 * @copydoc Dali::PathConstrainer::Apply
226 void Apply( Property target, Property source, const Vector2& range, const Vector2& wrap );
234 LinearConstrainer(const LinearConstrainer&);
237 LinearConstrainer& operator=(const LinearConstrainer& rhs);
239 Dali::Vector<float> mValue; ///< values for the linear map
240 Dali::Vector<float> mProgress; ///< Progress for each of the values normalized to [0,1]
245 // Get impl of handle
246 inline Internal::LinearConstrainer& GetImplementation(Dali::LinearConstrainer& linearConstrainer)
248 DALI_ASSERT_ALWAYS( linearConstrainer && "LinearConstrainer handle is empty" );
249 Dali::RefObject& object = linearConstrainer.GetBaseObject();
250 return static_cast<Internal::LinearConstrainer&>(object);
253 inline const Internal::LinearConstrainer& GetImplementation(const Dali::LinearConstrainer& linearConstrainer)
255 DALI_ASSERT_ALWAYS( linearConstrainer && "LinearConstrainer handle is empty" );
256 const Dali::RefObject& object = linearConstrainer.GetBaseObject();
257 return static_cast<const Internal::LinearConstrainer&>(object);
262 #endif //__DALI_INTERNAL_PATH_CONSTRAINER_H__