1 #ifndef DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_H
2 #define DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_H
5 * Copyright (c) 2021 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/math/math-utils.h> // Clamp
28 #include <dali/public-api/object/property-input.h>
29 #include <dali/public-api/object/property-types.h>
30 #include <dali/public-api/object/property.h>
39 * An inherited Vector3 property.
41 class InheritedVector3 : public PropertyInputImpl
45 * Create an inherited Vector3.
49 mInheritedFlag(false),
50 mReinheritedFlag(true)
55 * Create an inherited Vector3.
56 * @param [in] initialValue The initial value of the property.
58 InheritedVector3(const Vector3& initialValue)
59 : mValue(initialValue),
60 mInheritedFlag(false),
61 mReinheritedFlag(true)
67 ~InheritedVector3() override = default;
70 * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
72 Dali::Property::Type GetType() const override
74 return Dali::PropertyTypes::Get<Vector3>();
78 * Called once per Update (only) if the property did not need to be re-inherited.
79 * @param[in] updateBufferIndex The current update buffer index.
81 void CopyPrevious(BufferIndex updateBufferIndex)
85 mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
87 mReinheritedFlag = false;
92 * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
94 virtual bool IsClean() const
96 return (false == mReinheritedFlag);
100 * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
102 bool InputInitialized() const override
104 // A constraint cannot use the property until it has been inherited (at least once).
105 return mInheritedFlag;
109 * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
110 * @note A constraint can only receive the inherited property from the previous frame.
112 bool InputChanged() const override
118 * @copydoc Dali::PropertyInput::GetVector3()
120 const Vector3& GetVector3(BufferIndex bufferIndex) const override
122 return mValue[bufferIndex];
126 * @copydoc Dali::PropertyInput::GetConstraintInputVector3()
128 const Vector3& GetConstraintInputVector3(BufferIndex bufferIndex) const override
130 // For inherited properties, constraints work with the value from the previous frame.
131 // This is because constraints are applied to position etc, before world-position is calculated.
132 BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
134 return mValue[eventBufferIndex];
138 * Set the property value. This will only persist for the current frame; the property
139 * will be reset with the base value, at the beginning of the next frame.
140 * @param[in] bufferIndex The buffer to write.
141 * @param[in] value The new property value.
143 void Set(BufferIndex bufferIndex, const Vector3& value)
145 mValue[bufferIndex] = value;
147 // The value has been inherited for the first time
148 mInheritedFlag = true;
150 mReinheritedFlag = true;
154 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
156 Vector3& Get(BufferIndex bufferIndex)
158 return mValue[bufferIndex];
162 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
164 const Vector3& Get(BufferIndex bufferIndex) const
166 return mValue[bufferIndex];
170 * Retrieve the property value.
171 * @param[in] bufferIndex The buffer to read.
172 * @return The property value.
174 const Vector3& operator[](BufferIndex bufferIndex) const
176 return mValue[bufferIndex];
181 InheritedVector3(const InheritedVector3& property);
184 InheritedVector3& operator=(const InheritedVector3& rhs);
187 DoubleBuffered<Vector3> mValue; ///< The double-buffered property value
189 bool mInheritedFlag : 1; ///< Flag whether the value has ever been inherited
190 bool mReinheritedFlag : 1; ///< Flag whether value was re-inherited in previous frame
194 * An inherited Color property.
196 class InheritedColor : public PropertyInputImpl
200 * Create an inherited property.
201 * @param [in] initialValue The initial value of the property.
203 InheritedColor(const Vector4& initialValue)
204 : mValue(initialValue),
205 mInheritedFlag(false),
206 mReinheritedFlag(true)
211 * Virtual destructor.
213 ~InheritedColor() override = default;
216 * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
218 Dali::Property::Type GetType() const override
220 return Dali::PropertyTypes::Get<Vector4>();
224 * Called once per Update (only) if the property did not need to be re-inherited.
225 * @param[in] updateBufferIndex The current update buffer index.
227 void CopyPrevious(BufferIndex updateBufferIndex)
231 mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
233 mReinheritedFlag = false;
238 * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
240 virtual bool IsClean() const
242 return (false == mReinheritedFlag);
246 * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
248 bool InputInitialized() const override
250 // A constraint cannot use the property until it has been inherited (at least once).
251 return mInheritedFlag;
255 * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
256 * @note A constraint can only receive the inherited property from the previous frame.
258 bool InputChanged() const override
264 * @copydoc Dali::PropertyInput::GetVector4()
266 const Vector4& GetVector4(BufferIndex bufferIndex) const override
268 return mValue[bufferIndex];
272 * @copydoc Dali::PropertyInput::GetConstraintInputVector4()
274 const Vector4& GetConstraintInputVector4(BufferIndex bufferIndex) const override
276 // For inherited properties, constraints work with the value from the previous frame.
277 // This is because constraints are applied to position etc, before world-position is calculated.
278 BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
280 return mValue[eventBufferIndex];
284 * Set the property value. This will only persist for the current frame; the property
285 * will be reset with the base value, at the beginning of the next frame.
286 * @param[in] bufferIndex The buffer to write.
287 * @param[in] value The new property value.
289 void Set(BufferIndex bufferIndex, const Vector4& value)
291 mValue[bufferIndex] = Clamp(value, 0.0f, 1.0f); // color values are clamped between 0 and 1
293 // The value has been inherited for the first time
294 mInheritedFlag = true;
295 mReinheritedFlag = true;
299 * Set the property value. This will only persist for the current frame; the property
300 * will be reset with the base value, at the beginning of the next frame.
301 * @param[in] bufferIndex The buffer to write.
302 * @param[in] r The new red value.
303 * @param[in] g The new green value.
304 * @param[in] b The new blue value.
305 * @param[in] a The new alpha value.
307 void Set(BufferIndex bufferIndex, float r, float g, float b, float a)
309 mValue[bufferIndex].r = Clamp(r, 0.0f, 1.0f); // color values are clamped between 0 and 1
310 mValue[bufferIndex].g = Clamp(g, 0.0f, 1.0f); // color values are clamped between 0 and 1
311 mValue[bufferIndex].b = Clamp(b, 0.0f, 1.0f); // color values are clamped between 0 and 1
312 mValue[bufferIndex].a = Clamp(a, 0.0f, 1.0f); // color values are clamped between 0 and 1
314 // The value has been inherited for the first time
315 mInheritedFlag = true;
316 mReinheritedFlag = true;
320 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
322 Vector4& Get(BufferIndex bufferIndex)
324 return mValue[bufferIndex];
328 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
330 const Vector4& Get(BufferIndex bufferIndex) const
332 return mValue[bufferIndex];
336 * Retrieve the property value.
337 * @param[in] bufferIndex The buffer to read.
338 * @return The property value.
340 const Vector4& operator[](BufferIndex bufferIndex) const
342 return mValue[bufferIndex];
347 InheritedColor(const InheritedColor& property);
349 InheritedColor& operator=(const InheritedColor& rhs);
352 DoubleBuffered<Vector4> mValue; ///< The double-buffered property value
354 bool mInheritedFlag : 1; ///< Flag whether the value has ever been inherited
355 bool mReinheritedFlag : 1; ///< Flag whether value was re-inherited in previous frame
359 * An inherited Quaternion property.
361 class InheritedQuaternion : public PropertyInputImpl
365 * Create an inherited property.
367 InheritedQuaternion()
369 mInheritedFlag(false),
370 mReinheritedFlag(true)
375 * Virtual destructor.
377 ~InheritedQuaternion() override = default;
380 * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
382 Dali::Property::Type GetType() const override
384 return Dali::PropertyTypes::Get<Quaternion>();
388 * Called once per Update (only) if the property did not need to be re-inherited.
389 * @param[in] updateBufferIndex The current update buffer index.
391 void CopyPrevious(BufferIndex updateBufferIndex)
395 mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
397 mReinheritedFlag = false;
402 * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
404 virtual bool IsClean() const
406 return (false == mReinheritedFlag);
410 * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
412 bool InputInitialized() const override
414 // A constraint cannot use the property until it has been inherited (at least once).
415 return mInheritedFlag;
419 * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
420 * @note A constraint can only receive the inherited property from the previous frame.
422 bool InputChanged() const override
428 * @copydoc Dali::PropertyInput::GetQuaternion()
430 const Quaternion& GetQuaternion(BufferIndex bufferIndex) const override
432 return mValue[bufferIndex];
436 * @copydoc Dali::PropertyInput::GetConstraintInputQuaternion()
438 const Quaternion& GetConstraintInputQuaternion(BufferIndex bufferIndex) const override
440 // For inherited properties, constraints work with the value from the previous frame.
441 // This is because constraints are applied to position etc, before world-position is calculated.
442 BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
444 return mValue[eventBufferIndex];
448 * Set the property value. This will only persist for the current frame; the property
449 * will be reset with the base value, at the beginning of the next frame.
450 * @param[in] bufferIndex The buffer to write.
451 * @param[in] value The new property value.
453 void Set(BufferIndex bufferIndex, const Quaternion& value)
455 mValue[bufferIndex] = value;
457 // The value has been inherited for the first time
458 mInheritedFlag = true;
460 mReinheritedFlag = true;
464 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
466 Quaternion& Get(BufferIndex bufferIndex)
468 return mValue[bufferIndex];
472 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
474 const Quaternion& Get(BufferIndex bufferIndex) const
476 return mValue[bufferIndex];
480 * Retrieve the property value.
481 * @param[in] bufferIndex The buffer to read.
482 * @return The property value.
484 const Quaternion& operator[](BufferIndex bufferIndex) const
486 return mValue[bufferIndex];
491 InheritedQuaternion(const InheritedQuaternion& property);
494 InheritedQuaternion& operator=(const InheritedQuaternion& rhs);
497 DoubleBuffered<Quaternion> mValue; ///< The double-buffered property value
499 bool mInheritedFlag : 1; ///< Flag whether the value has ever been inherited
500 bool mReinheritedFlag : 1; ///< Flag whether value was re-inherited in previous frame
504 * An inherited Matrix property.
506 class InheritedMatrix : public PropertyInputImpl
510 * Create an inherited property.
514 mInheritedFlag(false),
515 mReinheritedFlag(true)
520 * Virtual destructor.
522 ~InheritedMatrix() override = default;
525 * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
527 Dali::Property::Type GetType() const override
529 return Dali::PropertyTypes::Get<Matrix>();
533 * Called once per Update (only) if the property did not need to be re-inherited.
534 * @param[in] updateBufferIndex The current update buffer index.
536 void CopyPrevious(BufferIndex updateBufferIndex)
540 mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
542 mReinheritedFlag = false;
547 * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
549 virtual bool IsClean() const
551 return (false == mReinheritedFlag);
555 * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
557 bool InputInitialized() const override
559 // A constraint cannot use the property until it has been inherited (at least once).
560 return mInheritedFlag;
564 * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
565 * @note A constraint can only receive the inherited property from the previous frame.
567 bool InputChanged() const override
573 * @copydoc Dali::Internal::PropertyInputImpl::GetMatrix()
575 const Matrix& GetMatrix(BufferIndex bufferIndex) const override
577 return mValue[bufferIndex];
581 * @copydoc Dali::Internal::PropertyInputImpl::GetConstraintInputMatrix()
583 const Matrix& GetConstraintInputMatrix(BufferIndex bufferIndex) const override
585 // For inherited properties, constraints work with the value from the previous frame.
586 // This is because constraints are applied to position etc, before world-position is calculated.
587 BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
589 return mValue[eventBufferIndex];
593 * Set the property value. This will only persist for the current frame; the property
594 * will be reset with the base value, at the beginning of the next frame.
595 * @param[in] bufferIndex The buffer to write.
596 * @param[in] value The new property value.
598 void Set(BufferIndex bufferIndex, const Matrix& value)
600 mValue[bufferIndex] = value;
602 // The value has been inherited for the first time
603 mInheritedFlag = true;
605 mReinheritedFlag = true;
609 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
611 Matrix& Get(BufferIndex bufferIndex)
613 return mValue[bufferIndex];
617 * @copydoc Dali::SceneGraph::PropertyInterface::Get()
619 const Matrix& Get(BufferIndex bufferIndex) const
621 return mValue[bufferIndex];
625 * Retrieve the property value.
626 * @param[in] bufferIndex The buffer to read.
627 * @return The property value.
629 const Matrix& operator[](BufferIndex bufferIndex) const
631 return mValue[bufferIndex];
634 void SetDirty(BufferIndex bufferIndex)
636 mReinheritedFlag = true;
638 // The value has been inherited for the first time
639 mInheritedFlag = true;
644 InheritedMatrix(const InheritedMatrix& property);
647 InheritedMatrix& operator=(const InheritedMatrix& rhs);
650 DoubleBuffered<Matrix> mValue; ///< The double-buffered property value
652 bool mInheritedFlag : 1; ///< Flag whether the value has ever been inherited
653 bool mReinheritedFlag : 1; ///< Flag whether value was re-inherited in previous frame
656 } // namespace SceneGraph
658 } // namespace Internal
662 #endif // DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_H