[Tizen] Add InterceptKeyEvent
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-input-impl.h
1 #ifndef DALI_INTERNAL_PROPERTY_INPUT_IMPL_H
2 #define DALI_INTERNAL_PROPERTY_INPUT_IMPL_H
3
4 /*
5  * Copyright (c) 2021 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 <iostream>
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/common/buffer-index.h>
26 #include <dali/public-api/math/matrix.h>
27 #include <dali/public-api/math/matrix3.h>
28 #include <dali/public-api/math/quaternion.h>
29 #include <dali/public-api/math/vector2.h>
30 #include <dali/public-api/math/vector3.h>
31 #include <dali/public-api/math/vector4.h>
32 #include <dali/public-api/object/property-input.h>
33
34 #if defined(ANDROID) || defined(WIN32) || defined(__APPLE__)
35 namespace std
36 {
37 uint64_t _Hash_bytes(const void* bytes, uint64_t size, uint64_t seed);
38
39 }
40 #endif
41
42 namespace Dali
43 {
44 namespace Internal
45 {
46 /**
47  * An abstract interface for receiving property values, and for querying whether
48  * a property value has changed i.e. whether a constraint needs to be reapplied.
49  */
50 class PropertyInputImpl
51 {
52 public:
53   /**
54    * Virtual destructor.
55    */
56   virtual ~PropertyInputImpl() = default;
57
58   /**
59    * Query the type of property input.
60    * @return The property type.
61    */
62   virtual Property::Type GetType() const = 0;
63
64   /**
65    * Query the input value (for a constraint) has been initialized.
66    * @return True if initialized, otherwise the constraint should be skipped.
67    */
68   virtual bool InputInitialized() const = 0;
69
70   /**
71    * Query the input value (for a constraint) has changed.
72    * @return True if the input value has changed.
73    */
74   virtual bool InputChanged() const = 0;
75
76   /**
77    * Retrieve a boolean value.
78    * @pre GetType() returns Property::BOOLEAN.
79    * @param[in] bufferIndex The buffer to read from.
80    * @return The boolean value.
81    */
82   virtual const bool& GetBoolean(BufferIndex bufferIndex) const
83   {
84     // the return will never be executed, it's just to keep the compiler happy
85     return reinterpret_cast<const bool&>(*this);
86   }
87
88   /**
89    * Retrieve an integer value.
90    * @pre GetType() returns Property::INTEGER.
91    * @param[in] bufferIndex The buffer to read from.
92    * @return The integer value.
93    */
94   virtual const int& GetInteger(BufferIndex bufferIndex) const
95   {
96     // the return will never be executed, it's just to keep the compiler happy
97     return reinterpret_cast<const int&>(*this);
98   }
99
100   /**
101    * Retrieve a float value.
102    * @pre GetType() returns Property::FLOAT.
103    * @param[in] bufferIndex The buffer to read from.
104    * @return The float value.
105    */
106   virtual const float& GetFloat(BufferIndex bufferIndex) const
107   {
108     // the return will never be executed, it's just to keep the compiler happy
109     return reinterpret_cast<const float&>(*this);
110   }
111
112   /**
113    * Retrieve a Vector2 value.
114    * @pre GetType() returns Property::VECTOR2.
115    * @param[in] bufferIndex The buffer to read from.
116    * @return The Vector2 value.
117    */
118   virtual const Vector2& GetVector2(BufferIndex bufferIndex) const
119   {
120     // the return will never be executed, it's just to keep the compiler happy
121     return reinterpret_cast<const Vector2&>(*this);
122   }
123
124   /**
125    * Retrieve a Vector3 value.
126    * @pre GetType() returns Property::VECTOR3.
127    * @param[in] bufferIndex The buffer to read from.
128    * @return The Vector3 value.
129    */
130   virtual const Vector3& GetVector3(BufferIndex bufferIndex) const
131   {
132     // the return will never be executed, it's just to keep the compiler happy
133     return reinterpret_cast<const Vector3&>(*this);
134   }
135
136   /**
137    * Retrieve a Vector4 value.
138    * @pre GetType() returns Property::VECTOR4.
139    * @param[in] bufferIndex The buffer to read from.
140    * @return The Vector4 value.
141    */
142   virtual const Vector4& GetVector4(BufferIndex bufferIndex) const
143   {
144     // the return will never be executed, it's just to keep the compiler happy
145     return reinterpret_cast<const Vector4&>(*this);
146   }
147
148   /**
149    * Retrieve a Quaternion value.
150    * @pre GetType() returns Property::Quaternion.
151    * @param[in] bufferIndex The buffer to read from.
152    * @return The Quaternion value.
153    */
154   virtual const Quaternion& GetQuaternion(BufferIndex bufferIndex) const
155   {
156     // the return will never be executed, it's just to keep the compiler happy
157     return reinterpret_cast<const Quaternion&>(*this);
158   }
159
160   /**
161    * Retrieve a Matrix value.
162    * @pre GetType() returns Property::Matrix.
163    * @param[in] bufferIndex The buffer to read from.
164    * @return The Matrix value.
165    */
166   virtual const Matrix3& GetMatrix3(BufferIndex bufferIndex) const
167   {
168     // the return will never be executed, it's just to keep the compiler happy
169     return reinterpret_cast<const Matrix3&>(*this);
170   }
171
172   /**
173    * Retrieve a Matrix value.
174    * @pre GetType() returns Property::Matrix.
175    * @param[in] bufferIndex The buffer to read from.
176    * @return The Matrix value.
177    */
178   virtual const Matrix& GetMatrix(BufferIndex bufferIndex) const
179   {
180     // the return will never be executed, it's just to keep the compiler happy
181     return reinterpret_cast<const Matrix&>(*this);
182   }
183
184   // Accessors for Constraint functions
185
186   /**
187    * Retrieve a boolean input for a constraint function.
188    * @note For inherited properties, this method should be overriden to return the value
189    * from the previous frame i.e. not from the current update buffer.
190    * @pre GetType() returns Property::BOOLEAN.
191    * @param[in] updateBufferIndex The current update buffer index.
192    * @return The boolean value.
193    */
194   virtual const bool& GetConstraintInputBoolean(BufferIndex updateBufferIndex) const
195   {
196     return GetBoolean(updateBufferIndex);
197   }
198
199   /**
200    * Retrieve an integer input for a constraint function.
201    * @note For inherited properties, this method should be overriden to return the value
202    * from the previous frame i.e. not from the current update buffer.
203    * @pre GetType() returns Property::INTEGER.
204    * @param[in] updateBufferIndex The current update buffer index.
205    * @return The integer value.
206    */
207   virtual const int& GetConstraintInputInteger(BufferIndex updateBufferIndex) const
208   {
209     return GetInteger(updateBufferIndex);
210   }
211
212   /**
213    * Retrieve a float input for a constraint function.
214    * @note For inherited properties, this method should be overriden to return the value
215    * from the previous frame i.e. not from the current update buffer.
216    * @pre GetType() returns Property::FLOAT.
217    * @param[in] updateBufferIndex The current update buffer index.
218    * @return The float value.
219    */
220   virtual const float& GetConstraintInputFloat(BufferIndex updateBufferIndex) const
221   {
222     return GetFloat(updateBufferIndex);
223   }
224
225   /**
226    * Retrieve a Vector2 input for a constraint function.
227    * @note For inherited properties, this method should be overriden to return the value
228    * from the previous frame i.e. not from the current update buffer.
229    * @pre GetType() returns Property::VECTOR2.
230    * @param[in] updateBufferIndex The buffer to read from.
231    * @return The Vector2 value.
232    */
233   virtual const Vector2& GetConstraintInputVector2(BufferIndex updateBufferIndex) const
234   {
235     return GetVector2(updateBufferIndex);
236   }
237
238   /**
239    * Retrieve a Vector3 input for a constraint function.
240    * @note For inherited properties, this method should be overriden to return the value
241    * from the previous frame i.e. not from the current update buffer.
242    * @pre GetType() returns Property::VECTOR3.
243    * @param[in] updateBufferIndex The buffer to read from.
244    * @return The Vector3 value.
245    */
246   virtual const Vector3& GetConstraintInputVector3(BufferIndex updateBufferIndex) const
247   {
248     return GetVector3(updateBufferIndex);
249   }
250
251   /**
252    * Retrieve a Vector4 input for a constraint function.
253    * @note For inherited properties, this method should be overriden to return the value
254    * from the previous frame i.e. not from the current update buffer.
255    * @pre GetType() returns Property::VECTOR4.
256    * @param[in] updateBufferIndex The buffer to read from.
257    * @return The Vector4 value.
258    */
259   virtual const Vector4& GetConstraintInputVector4(BufferIndex updateBufferIndex) const
260   {
261     return GetVector4(updateBufferIndex);
262   }
263
264   /**
265    * Retrieve a Quaternion input for a constraint function.
266    * @note For inherited properties, this method should be overriden to return the value
267    * from the previous frame i.e. not from the current update buffer.
268    * @pre GetType() returns Property::Quaternion.
269    * @param[in] updateBufferIndex The buffer to read from.
270    * @return The Quaternion value.
271    */
272   virtual const Quaternion& GetConstraintInputQuaternion(BufferIndex updateBufferIndex) const
273   {
274     return GetQuaternion(updateBufferIndex);
275   }
276
277   /**
278    * Retrieve a Matrix3 input for a constraint function.
279    * @note For inherited properties, this method should be overriden to return the value
280    * from the previous frame i.e. not from the current update buffer.
281    * @pre GetType() returns Property::Matrix.
282    * @param[in] updateBufferIndex The buffer to read from.
283    * @return The Matrix value.
284    */
285   virtual const Matrix3& GetConstraintInputMatrix3(BufferIndex updateBufferIndex) const
286   {
287     return GetMatrix3(updateBufferIndex);
288   }
289
290   /**
291    * Retrieve a Matrix input for a constraint function.
292    * @note For inherited properties, this method should be overriden to return the value
293    * from the previous frame i.e. not from the current update buffer.
294    * @pre GetType() returns Property::Matrix.
295    * @param[in] updateBufferIndex The buffer to read from.
296    * @return The Matrix value.
297    */
298   virtual const Matrix& GetConstraintInputMatrix(BufferIndex updateBufferIndex) const
299   {
300     return GetMatrix(updateBufferIndex);
301   }
302
303   /**
304    * Query whether the property belongs to the
305    * transform manager or not.
306    * @return True if it is a transform manager property, false otherwise
307    */
308   virtual bool IsTransformManagerProperty() const
309   {
310     return false;
311   }
312
313   std::uint64_t Hash(BufferIndex bufferIndex, uint64_t seed) const
314   {
315     switch(GetType())
316     {
317       case Property::BOOLEAN:
318       {
319         return std::_Hash_bytes(&GetBoolean(bufferIndex), sizeof(bool), seed);
320       }
321
322       case Property::INTEGER:
323       {
324         return std::_Hash_bytes(&GetInteger(bufferIndex), sizeof(int), seed);
325       }
326
327       case Property::FLOAT:
328       {
329         return std::_Hash_bytes(&GetFloat(bufferIndex), sizeof(float), seed);
330       }
331
332       case Property::VECTOR2:
333       {
334         return std::_Hash_bytes(&GetVector2(bufferIndex), sizeof(Vector2), seed);
335       }
336
337       case Property::VECTOR3:
338       {
339         return std::_Hash_bytes(&GetVector3(bufferIndex), sizeof(Vector3), seed);
340       }
341
342       case Property::VECTOR4:
343       {
344         return std::_Hash_bytes(&GetVector4(bufferIndex), sizeof(Vector4), seed);
345       }
346
347       case Property::ROTATION:
348       {
349         return std::_Hash_bytes(&GetQuaternion(bufferIndex), sizeof(Quaternion), seed);
350       }
351
352       case Property::MATRIX:
353       {
354         return std::_Hash_bytes(&GetMatrix(bufferIndex), sizeof(Matrix), seed);
355       }
356
357       case Property::MATRIX3:
358       {
359         return std::_Hash_bytes(&GetMatrix3(bufferIndex), sizeof(Matrix3), seed);
360       }
361
362       default:
363         break; // print nothing
364     }
365
366     return seed;
367   }
368
369   /**
370    * Print the property value using a stream.
371    * @param[in] debugStream The output stream.
372    * @param[in] bufferIndex The buffer to read from.
373    * @todo Place this far-too-large-to-be-inlined function in a cpp and remove <iostream> header dependency from this file.
374    */
375   void DebugPrint(std::ostream& debugStream, BufferIndex bufferIndex) const
376   {
377     switch(GetType())
378     {
379       case Property::BOOLEAN:
380       {
381         debugStream << GetBoolean(bufferIndex);
382         break;
383       }
384
385       case Property::INTEGER:
386       {
387         debugStream << GetInteger(bufferIndex);
388         break;
389       }
390
391       case Property::FLOAT:
392       {
393         debugStream << GetFloat(bufferIndex);
394         break;
395       }
396
397       case Property::VECTOR2:
398       {
399         debugStream << GetVector2(bufferIndex);
400         break;
401       }
402
403       case Property::VECTOR3:
404       {
405         debugStream << GetVector3(bufferIndex);
406         break;
407       }
408
409       case Property::VECTOR4:
410       {
411         debugStream << GetVector4(bufferIndex);
412         break;
413       }
414
415       case Property::ROTATION:
416       {
417         debugStream << GetQuaternion(bufferIndex);
418         break;
419       }
420
421       case Property::MATRIX:
422       {
423         debugStream << GetMatrix(bufferIndex);
424         break;
425       }
426
427       case Property::MATRIX3:
428       {
429         debugStream << GetMatrix3(bufferIndex);
430         break;
431       }
432
433       default:
434         break; // print nothing
435     }
436   }
437 };
438
439 } // namespace Internal
440
441 } // namespace Dali
442
443 #endif // DALI_INTERNAL_PROPERTY_INPUT_IMPL_H