Reducing boilerplate on default property metadata
[platform/core/uifw/dali-core.git] / dali / internal / event / common / object-impl-helper.h
1 #ifndef DALI_INTERNAL_OBJECT_IMPL_HELPER_H
2 #define DALI_INTERNAL_OBJECT_IMPL_HELPER_H
3
4 /*
5  * Copyright (c) 2018 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 // EXTERNAL INCLUDEs
22 #include <cstring>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/object/property.h> // Dali::Property
26 #include <dali/internal/event/common/event-thread-services.h>
27 #include <dali/internal/update/common/animatable-property.h>
28 #include <dali/internal/update/common/property-owner-messages.h>
29 #include <dali/internal/update/manager/update-manager.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 class PropertyMetadata;
36 class AnimatablePropertyMetadata;
37 class CustomPropertyMetadata;
38 class PropertyInputImpl;
39
40 namespace SceneGraph
41 {
42
43 class PropertyBase;
44 class PropertyOwner;
45
46 } // namespace SceneGraph
47
48 // methods needed as parameters
49 using FindAnimatablePropertyMethod = AnimatablePropertyMetadata* (Object::*)(Property::Index index) const;
50 using FindCustomPropertyMethod = CustomPropertyMetadata* (Object::*)(Property::Index index) const;
51
52 /**
53  * Helper utilities to be used by class that implement Object
54  */
55 namespace ObjectImplHelper
56 {
57   // Get the (animatable) scene graph property. (All registered scene graph properties are animatable)
58   inline const SceneGraph::PropertyBase* GetRegisteredSceneGraphProperty( const Object* object,
59                                                                           FindAnimatablePropertyMethod findAnimatablePropertyMethod,
60                                                                           FindCustomPropertyMethod findCustomPropertyMethod,
61                                                                           Property::Index index )
62   {
63     const SceneGraph::PropertyBase* property = NULL;
64     if ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX && index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX )
65     {
66       AnimatablePropertyMetadata* animatable = (object->*findAnimatablePropertyMethod)( index );
67       DALI_ASSERT_ALWAYS( animatable && "Property index is invalid" );
68       property = animatable->GetSceneGraphProperty();
69     }
70     else if ( ( index > CHILD_PROPERTY_REGISTRATION_START_INDEX ) && // Child properties are also stored as custom properties
71               ( index <= PROPERTY_CUSTOM_MAX_INDEX ) )
72     {
73       CustomPropertyMetadata* custom = (object->*findCustomPropertyMethod)( index );
74       DALI_ASSERT_ALWAYS( custom && "Property index is invalid" );
75       property = custom->GetSceneGraphProperty();
76     }
77     return property;
78   }
79
80   inline void SetSceneGraphProperty( EventThreadServices& eventThreadServices,
81                                      const Object* object,
82                                      Property::Index index,
83                                      const PropertyMetadata& entry,
84                                      const Property::Value& value )
85   {
86     const SceneGraph::PropertyOwner* sceneObject = object->GetSceneObject();
87
88     switch ( entry.GetType() )
89     {
90       case Property::BOOLEAN:
91       {
92         const SceneGraph::AnimatableProperty<bool>* property = dynamic_cast< const SceneGraph::AnimatableProperty<bool>* >( entry.GetSceneGraphProperty() );
93         DALI_ASSERT_DEBUG( NULL != property );
94
95         // property is being used in a separate thread; queue a message to set the property
96         SceneGraph::AnimatablePropertyMessage<bool>::Send( eventThreadServices, sceneObject, property, &SceneGraph::AnimatableProperty<bool>::Bake, value.Get<bool>() );
97
98         break;
99       }
100
101       case Property::FLOAT:
102       {
103         const SceneGraph::AnimatableProperty<float>* property = dynamic_cast< const SceneGraph::AnimatableProperty<float>* >( entry.GetSceneGraphProperty() );
104         DALI_ASSERT_DEBUG( NULL != property );
105
106         // property is being used in a separate thread; queue a message to set the property
107         SceneGraph::AnimatablePropertyMessage<float>::Send( eventThreadServices, sceneObject, property, &SceneGraph::AnimatableProperty<float>::Bake, value.Get<float>() );
108
109         break;
110       }
111
112       case Property::INTEGER:
113       {
114         const SceneGraph::AnimatableProperty<int>* property = dynamic_cast< const SceneGraph::AnimatableProperty<int>* >( entry.GetSceneGraphProperty() );
115         DALI_ASSERT_DEBUG( NULL != property );
116
117         // property is being used in a separate thread; queue a message to set the property
118         SceneGraph::AnimatablePropertyMessage<int>::Send( eventThreadServices, sceneObject, property, &SceneGraph::AnimatableProperty<int>::Bake, value.Get<int>() );
119
120         break;
121       }
122
123       case Property::VECTOR2:
124       {
125         const SceneGraph::AnimatableProperty<Vector2>* property = dynamic_cast< const SceneGraph::AnimatableProperty<Vector2>* >( entry.GetSceneGraphProperty() );
126         DALI_ASSERT_DEBUG( NULL != property );
127
128         // property is being used in a separate thread; queue a message to set the property
129         SceneGraph::AnimatablePropertyMessage<Vector2>::Send( eventThreadServices, sceneObject, property, &SceneGraph::AnimatableProperty<Vector2>::Bake, value.Get<Vector2>() );
130
131         break;
132       }
133
134       case Property::VECTOR3:
135       {
136         const SceneGraph::AnimatableProperty<Vector3>* property = dynamic_cast< const SceneGraph::AnimatableProperty<Vector3>* >( entry.GetSceneGraphProperty() );
137         DALI_ASSERT_DEBUG( NULL != property );
138
139         // property is being used in a separate thread; queue a message to set the property
140         SceneGraph::AnimatablePropertyMessage<Vector3>::Send( eventThreadServices, sceneObject, property, &SceneGraph::AnimatableProperty<Vector3>::Bake, value.Get<Vector3>() );
141
142         break;
143       }
144
145       case Property::VECTOR4:
146       {
147         const SceneGraph::AnimatableProperty<Vector4>* property = dynamic_cast< const SceneGraph::AnimatableProperty<Vector4>* >( entry.GetSceneGraphProperty() );
148         DALI_ASSERT_DEBUG( NULL != property );
149
150         // property is being used in a separate thread; queue a message to set the property
151         SceneGraph::AnimatablePropertyMessage<Vector4>::Send( eventThreadServices, sceneObject, property, &SceneGraph::AnimatableProperty<Vector4>::Bake, value.Get<Vector4>() );
152
153         break;
154       }
155
156       case Property::ROTATION:
157       {
158         const SceneGraph::AnimatableProperty<Quaternion>* property = dynamic_cast< const SceneGraph::AnimatableProperty<Quaternion>* >( entry.GetSceneGraphProperty() );
159         DALI_ASSERT_DEBUG( NULL != property );
160
161         // property is being used in a separate thread; queue a message to set the property
162         SceneGraph::AnimatablePropertyMessage<Quaternion>::Send( eventThreadServices, sceneObject, property,&SceneGraph::AnimatableProperty<Quaternion>::Bake,  value.Get<Quaternion>() );
163
164         break;
165       }
166
167       case Property::MATRIX:
168       {
169         const SceneGraph::AnimatableProperty<Matrix>* property = dynamic_cast< const SceneGraph::AnimatableProperty<Matrix>* >( entry.GetSceneGraphProperty() );
170         DALI_ASSERT_DEBUG( NULL != property );
171
172         // property is being used in a separate thread; queue a message to set the property
173         SceneGraph::AnimatablePropertyMessage<Matrix>::Send( eventThreadServices, sceneObject, property,&SceneGraph::AnimatableProperty<Matrix>::Bake,  value.Get<Matrix>() );
174
175         break;
176       }
177
178       case Property::MATRIX3:
179       {
180         const SceneGraph::AnimatableProperty<Matrix3>* property = dynamic_cast< const SceneGraph::AnimatableProperty<Matrix3>* >( entry.GetSceneGraphProperty() );
181         DALI_ASSERT_DEBUG( NULL != property );
182
183         // property is being used in a separate thread; queue a message to set the property
184         SceneGraph::AnimatablePropertyMessage<Matrix3>::Send( eventThreadServices, sceneObject, property, &SceneGraph::AnimatableProperty<Matrix3>::Bake,  value.Get<Matrix3>() );
185
186         break;
187       }
188
189       default:
190       {
191         // ignore non-scene-graph types
192       }
193     }
194   }
195 };
196
197
198
199 } // namespace Internal
200
201 } // namespace Dali
202
203 #endif // DALI_INTERNAL_OBJECT_IMPL_HELPER_H