1 #ifndef DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_H
2 #define DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_H
5 * Copyright (c) 2019 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/common/property-input-impl.h>
23 #include <dali/internal/update/common/double-buffered.h>
24 #include <dali/internal/update/common/property-base.h>
25 #include <dali/internal/update/common/scene-graph-buffers.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/object/property.h>
28 #include <dali/public-api/object/property-input.h>
29 #include <dali/public-api/object/property-types.h>
30 #include <dali/public-api/math/math-utils.h> // Clamp
42 * An inherited Vector3 property.
44 class InheritedVector3 : public PropertyInputImpl
49 * Create an inherited Vector3.
53 mInheritedFlag( false ),
54 mReinheritedFlag( true )
59 * Create an inherited Vector3.
60 * @param [in] initialValue The initial value of the property.
62 InheritedVector3( const Vector3& initialValue )
63 : mValue( initialValue ),
64 mInheritedFlag( false ),
65 mReinheritedFlag( true )
71 ~InheritedVector3() override = default;
74 * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
76 Dali::Property::Type GetType() const override
78 return Dali::PropertyTypes::Get<Vector3>();
82 * Called once per Update (only) if the property did not need to be re-inherited.
83 * @param[in] updateBufferIndex The current update buffer index.
85 void CopyPrevious( BufferIndex updateBufferIndex )
87 if ( mReinheritedFlag )
89 mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
91 mReinheritedFlag = false;
96 * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
98 virtual bool IsClean() const
100 return ( false == mReinheritedFlag );
104 * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
106 bool InputInitialized() const override
108 // A constraint cannot use the property until it has been inherited (at least once).
109 return mInheritedFlag;
113 * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
114 * @note A constraint can only receive the inherited property from the previous frame.
116 bool InputChanged() const override
122 * @copydoc Dali::PropertyInput::GetVector3()
124 const Vector3& GetVector3( BufferIndex bufferIndex ) const override
126 return mValue[ bufferIndex ];
130 * @copydoc Dali::PropertyInput::GetConstraintInputVector3()
132 const Vector3& GetConstraintInputVector3( BufferIndex bufferIndex ) const override
134 // For inherited properties, constraints work with the value from the previous frame.
135 // This is because constraints are applied to position etc, before world-position is calculated.
136 BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
138 return mValue[ eventBufferIndex ];
142 * Set the property value. This will only persist for the current frame; the property
143 * will be reset with the base value, at the beginning of the next frame.
144 * @param[in] bufferIndex The buffer to write.
145 * @param[in] value The new property value.
147 void Set(BufferIndex bufferIndex, const Vector3& value)
149 mValue[bufferIndex] = value;
151 // The value has been inherited for the first time
152 mInheritedFlag = true;
154 mReinheritedFlag = true;
158 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
160 Vector3& Get( BufferIndex bufferIndex )
162 return mValue[bufferIndex];
166 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
168 const Vector3& Get( BufferIndex bufferIndex ) const
170 return mValue[bufferIndex];
174 * Retrieve the property value.
175 * @param[in] bufferIndex The buffer to read.
176 * @return The property value.
178 const Vector3& operator[]( BufferIndex bufferIndex ) const
180 return mValue[bufferIndex];
186 InheritedVector3(const InheritedVector3& property);
189 InheritedVector3& operator=(const InheritedVector3& rhs);
193 DoubleBuffered<Vector3> mValue; ///< The double-buffered property value
195 bool mInheritedFlag :1; ///< Flag whether the value has ever been inherited
196 bool mReinheritedFlag :1; ///< Flag whether value was re-inherited in previous frame
200 * An inherited Color property.
202 class InheritedColor : public PropertyInputImpl
207 * Create an inherited property.
208 * @param [in] initialValue The initial value of the property.
210 InheritedColor( const Vector4& initialValue )
211 : mValue( initialValue ),
212 mInheritedFlag( false ),
213 mReinheritedFlag( true )
218 * Virtual destructor.
220 ~InheritedColor() override = default;
223 * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
225 Dali::Property::Type GetType() const override
227 return Dali::PropertyTypes::Get<Vector4>();
231 * Called once per Update (only) if the property did not need to be re-inherited.
232 * @param[in] updateBufferIndex The current update buffer index.
234 void CopyPrevious( BufferIndex updateBufferIndex )
236 if ( mReinheritedFlag )
238 mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
240 mReinheritedFlag = false;
245 * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
247 virtual bool IsClean() const
249 return ( false == mReinheritedFlag );
253 * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
255 bool InputInitialized() const override
257 // A constraint cannot use the property until it has been inherited (at least once).
258 return mInheritedFlag;
262 * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
263 * @note A constraint can only receive the inherited property from the previous frame.
265 bool InputChanged() const override
271 * @copydoc Dali::PropertyInput::GetVector4()
273 const Vector4& GetVector4( BufferIndex bufferIndex ) const override
275 return mValue[ bufferIndex ];
279 * @copydoc Dali::PropertyInput::GetConstraintInputVector4()
281 const Vector4& GetConstraintInputVector4( BufferIndex bufferIndex ) const override
283 // For inherited properties, constraints work with the value from the previous frame.
284 // This is because constraints are applied to position etc, before world-position is calculated.
285 BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
287 return mValue[ eventBufferIndex ];
291 * Set the property value. This will only persist for the current frame; the property
292 * will be reset with the base value, at the beginning of the next frame.
293 * @param[in] bufferIndex The buffer to write.
294 * @param[in] value The new property value.
296 void Set(BufferIndex bufferIndex, const Vector4& value)
298 mValue[bufferIndex] = Clamp( value, 0.0f, 1.0f ); // color values are clamped between 0 and 1
300 // The value has been inherited for the first time
301 mInheritedFlag = true;
302 mReinheritedFlag = true;
306 * Set the property value. This will only persist for the current frame; the property
307 * will be reset with the base value, at the beginning of the next frame.
308 * @param[in] bufferIndex The buffer to write.
309 * @param[in] r The new red value.
310 * @param[in] g The new green value.
311 * @param[in] b The new blue value.
312 * @param[in] a The new alpha value.
314 void Set(BufferIndex bufferIndex, float r, float g, float b, float a )
316 mValue[bufferIndex].r = Clamp( r, 0.0f, 1.0f ); // color values are clamped between 0 and 1
317 mValue[bufferIndex].g = Clamp( g, 0.0f, 1.0f ); // color values are clamped between 0 and 1
318 mValue[bufferIndex].b = Clamp( b, 0.0f, 1.0f ); // color values are clamped between 0 and 1
319 mValue[bufferIndex].a = Clamp( a, 0.0f, 1.0f ); // color values are clamped between 0 and 1
321 // The value has been inherited for the first time
322 mInheritedFlag = true;
323 mReinheritedFlag = true;
327 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
329 Vector4& Get( BufferIndex bufferIndex )
331 return mValue[bufferIndex];
335 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
337 const Vector4& Get( BufferIndex bufferIndex ) const
339 return mValue[bufferIndex];
343 * Retrieve the property value.
344 * @param[in] bufferIndex The buffer to read.
345 * @return The property value.
347 const Vector4& operator[]( BufferIndex bufferIndex ) const
349 return mValue[bufferIndex];
355 InheritedColor(const InheritedColor& property);
357 InheritedColor& operator=(const InheritedColor& rhs);
361 DoubleBuffered<Vector4> mValue; ///< The double-buffered property value
363 bool mInheritedFlag :1; ///< Flag whether the value has ever been inherited
364 bool mReinheritedFlag :1; ///< Flag whether value was re-inherited in previous frame
368 * An inherited Quaternion property.
370 class InheritedQuaternion : public PropertyInputImpl
375 * Create an inherited property.
377 InheritedQuaternion()
379 mInheritedFlag( false ),
380 mReinheritedFlag( true )
385 * Virtual destructor.
387 ~InheritedQuaternion() override = default;
390 * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
392 Dali::Property::Type GetType() const override
394 return Dali::PropertyTypes::Get<Quaternion>();
398 * Called once per Update (only) if the property did not need to be re-inherited.
399 * @param[in] updateBufferIndex The current update buffer index.
401 void CopyPrevious( BufferIndex updateBufferIndex )
403 if ( mReinheritedFlag )
405 mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
407 mReinheritedFlag = false;
412 * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
414 virtual bool IsClean() const
416 return ( false == mReinheritedFlag );
420 * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
422 bool InputInitialized() const override
424 // A constraint cannot use the property until it has been inherited (at least once).
425 return mInheritedFlag;
429 * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
430 * @note A constraint can only receive the inherited property from the previous frame.
432 bool InputChanged() const override
438 * @copydoc Dali::PropertyInput::GetQuaternion()
440 const Quaternion& GetQuaternion( BufferIndex bufferIndex ) const override
442 return mValue[ bufferIndex ];
446 * @copydoc Dali::PropertyInput::GetConstraintInputQuaternion()
448 const Quaternion& GetConstraintInputQuaternion( BufferIndex bufferIndex ) const override
450 // For inherited properties, constraints work with the value from the previous frame.
451 // This is because constraints are applied to position etc, before world-position is calculated.
452 BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
454 return mValue[ eventBufferIndex ];
458 * Set the property value. This will only persist for the current frame; the property
459 * will be reset with the base value, at the beginning of the next frame.
460 * @param[in] bufferIndex The buffer to write.
461 * @param[in] value The new property value.
463 void Set(BufferIndex bufferIndex, const Quaternion& value)
465 mValue[bufferIndex] = value;
467 // The value has been inherited for the first time
468 mInheritedFlag = true;
470 mReinheritedFlag = true;
474 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
476 Quaternion& Get( BufferIndex bufferIndex )
478 return mValue[bufferIndex];
482 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
484 const Quaternion& Get( BufferIndex bufferIndex ) const
486 return mValue[bufferIndex];
490 * Retrieve the property value.
491 * @param[in] bufferIndex The buffer to read.
492 * @return The property value.
494 const Quaternion& operator[]( BufferIndex bufferIndex ) const
496 return mValue[bufferIndex];
502 InheritedQuaternion(const InheritedQuaternion& property);
505 InheritedQuaternion& operator=(const InheritedQuaternion& rhs);
509 DoubleBuffered<Quaternion> mValue; ///< The double-buffered property value
511 bool mInheritedFlag :1; ///< Flag whether the value has ever been inherited
512 bool mReinheritedFlag :1; ///< Flag whether value was re-inherited in previous frame
516 * An inherited Matrix property.
518 class InheritedMatrix : public PropertyInputImpl
523 * Create an inherited property.
527 mInheritedFlag( false ),
528 mReinheritedFlag( true )
533 * Virtual destructor.
535 ~InheritedMatrix() override = default;
538 * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
540 Dali::Property::Type GetType() const override
542 return Dali::PropertyTypes::Get<Matrix>();
546 * Called once per Update (only) if the property did not need to be re-inherited.
547 * @param[in] updateBufferIndex The current update buffer index.
549 void CopyPrevious( BufferIndex updateBufferIndex )
551 if ( mReinheritedFlag )
553 mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
555 mReinheritedFlag = false;
560 * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
562 virtual bool IsClean() const
564 return ( false == mReinheritedFlag );
568 * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
570 bool InputInitialized() const override
572 // A constraint cannot use the property until it has been inherited (at least once).
573 return mInheritedFlag;
577 * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
578 * @note A constraint can only receive the inherited property from the previous frame.
580 bool InputChanged() const override
586 * @copydoc Dali::Internal::PropertyInputImpl::GetMatrix()
588 const Matrix& GetMatrix( BufferIndex bufferIndex ) const override
590 return mValue[ bufferIndex ];
594 * @copydoc Dali::Internal::PropertyInputImpl::GetConstraintInputMatrix()
596 const Matrix& GetConstraintInputMatrix( BufferIndex bufferIndex ) const override
598 // For inherited properties, constraints work with the value from the previous frame.
599 // This is because constraints are applied to position etc, before world-position is calculated.
600 BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
602 return mValue[ eventBufferIndex ];
606 * Set the property value. This will only persist for the current frame; the property
607 * will be reset with the base value, at the beginning of the next frame.
608 * @param[in] bufferIndex The buffer to write.
609 * @param[in] value The new property value.
611 void Set(BufferIndex bufferIndex, const Matrix& value)
613 mValue[bufferIndex] = value;
615 // The value has been inherited for the first time
616 mInheritedFlag = true;
618 mReinheritedFlag = true;
622 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
624 Matrix& Get( BufferIndex bufferIndex )
626 return mValue[bufferIndex];
630 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
632 const Matrix& Get( BufferIndex bufferIndex ) const
634 return mValue[bufferIndex];
638 * Retrieve the property value.
639 * @param[in] bufferIndex The buffer to read.
640 * @return The property value.
642 const Matrix& operator[]( BufferIndex bufferIndex ) const
644 return mValue[bufferIndex];
647 void SetDirty( BufferIndex bufferIndex )
649 mReinheritedFlag = true;
651 // The value has been inherited for the first time
652 mInheritedFlag = true;
658 InheritedMatrix(const InheritedMatrix& property);
661 InheritedMatrix& operator=(const InheritedMatrix& rhs);
665 DoubleBuffered<Matrix> mValue; ///< The double-buffered property value
667 bool mInheritedFlag :1; ///< Flag whether the value has ever been inherited
668 bool mReinheritedFlag :1; ///< Flag whether value was re-inherited in previous frame
672 } // namespace SceneGraph
674 } // namespace Internal
678 #endif // DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_H