[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / model-motion / motion-value-impl.cpp
1 /*
2  * Copyright (c) 2024 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-scene3d/internal/model-motion/motion-value-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/animation/key-frames-devel.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/public-api/object/type-registry.h>
25
26 // INTERNAL INCLUDES
27
28 namespace Dali
29 {
30 namespace Scene3D
31 {
32 namespace Internal
33 {
34 namespace
35 {
36 /**
37  * Creates control through type registry
38  */
39 BaseHandle Create()
40 {
41   return Scene3D::MotionValue::New();
42 }
43
44 // Setup properties, signals and actions using the type-registry.
45 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::MotionValue, Dali::BaseHandle, Create);
46 DALI_TYPE_REGISTRATION_END()
47
48 } // unnamed namespace
49
50 MotionValuePtr MotionValue::New()
51 {
52   MotionValuePtr motionValue = new MotionValue();
53
54   motionValue->Initialize();
55
56   return motionValue;
57 }
58
59 MotionValue::MotionValue()
60 {
61 }
62
63 MotionValue::~MotionValue()
64 {
65 }
66
67 void MotionValue::Initialize()
68 {
69 }
70
71 // Public Method
72
73 Scene3D::MotionValue::ValueType MotionValue::GetValueType() const
74 {
75   return mType;
76 }
77
78 void MotionValue::SetValue(Property::Value propertyValue)
79 {
80   mType = Scene3D::MotionValue::ValueType::PROPERTY_VALUE;
81
82   mKeyFrames.Reset();
83   mPropertyValue = propertyValue;
84 }
85
86 void MotionValue::SetValue(Dali::KeyFrames keyFrames)
87 {
88   mType = Scene3D::MotionValue::ValueType::KEY_FRAMES;
89
90   mKeyFrames     = keyFrames;
91   mPropertyValue = Property::Value();
92 }
93
94 void MotionValue::Clear()
95 {
96   mType = Scene3D::MotionValue::ValueType::INVALID;
97
98   mKeyFrames.Reset();
99   mPropertyValue = Property::Value();
100 }
101
102 Property::Value MotionValue::GetPropertyValue() const
103 {
104   if(mType == Scene3D::MotionValue::ValueType::PROPERTY_VALUE)
105   {
106     return mPropertyValue;
107   }
108   else if(mType == Scene3D::MotionValue::ValueType::KEY_FRAMES)
109   {
110     // Get last value of keyframes
111     auto keyFrameCount = DevelKeyFrames::GetKeyFrameCount(mKeyFrames);
112     if(keyFrameCount > 0u)
113     {
114       float           dummyProgress;
115       Property::Value propertyValue;
116       DevelKeyFrames::GetKeyFrame(mKeyFrames, keyFrameCount - 1u, dummyProgress, propertyValue);
117
118       return propertyValue;
119     }
120   }
121   return Property::Value();
122 }
123
124 KeyFrames MotionValue::GetKeyFrames() const
125 {
126   if(mType == Scene3D::MotionValue::ValueType::KEY_FRAMES)
127   {
128     return mKeyFrames;
129   }
130   else if(mType == Scene3D::MotionValue::ValueType::PROPERTY_VALUE)
131   {
132     // Generate stable keyframe animation here.
133     // TODO : Should we check property value is animatable type or not?
134     KeyFrames keyFrames = KeyFrames::New();
135     keyFrames.Add(0.0f, mPropertyValue);
136     keyFrames.Add(1.0f, mPropertyValue);
137     return keyFrames;
138   }
139   return Dali::KeyFrames();
140 }
141
142 } // namespace Internal
143
144 } // namespace Scene3D
145
146 } // namespace Dali