[dali_2.0.7] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / public-api / node-definition.h
1 #ifndef DALI_SCENE_LOADER_NODE_DEFINITION_H_
2 #define DALI_SCENE_LOADER_NODE_DEFINITION_H_
3 /*
4  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // INTERNAL INCLUDES
21 #include "dali-scene-loader/public-api/customization.h"
22 #include "dali-scene-loader/public-api/matrix-stack.h"
23 #include "dali-scene-loader/public-api/resource-bundle.h"
24
25 // EXTERNAL INCLUDES
26 #include "dali/public-api/math/quaternion.h"
27 #include "dali/public-api/math/matrix.h"
28 #include "dali/public-api/math/vector4.h"
29 #include "dali/public-api/actors/actor.h"
30 #include <string>
31 #include <memory>
32 #include <functional>
33
34 namespace Dali
35 {
36 namespace SceneLoader
37 {
38
39 class ViewProjection;
40
41 /**
42  * @brief Interface to report (const) resource ids to.
43  */
44 class DALI_SCENE_LOADER_API IResourceReceiver
45 {
46 public:
47   virtual ~IResourceReceiver() = default;
48
49   virtual void Register(ResourceType::Value type, Index id) = 0;
50 };
51
52 /**
53  * @brief Interface to report modifiable resource ids to.
54  * @note These are supposed to be transient. Obviously, the references collected
55  *  this way must not outlive the objects that they came from.
56  */
57 class DALI_SCENE_LOADER_API IResourceReflector
58 {
59 public:
60   virtual ~IResourceReflector() = default;
61
62   virtual void Reflect(ResourceType::Value type, Index& id) = 0;
63 };
64
65 /**
66  * @brief Intermediate representation for a constraint that shall be
67  *  set up after the Actors were created. The target of the constraint
68  *  is the node definition that carries it.
69  */
70 struct DALI_SCENE_LOADER_API ConstraintDefinition
71 {
72   std::string mProperty;  ///< name of the property to constrain.
73   Index mSourceIdx;  ///< index of the node to serve as the source of the constraint.
74
75   bool operator<(const ConstraintDefinition& other) const
76   {
77     return mProperty < other.mProperty;
78   }
79
80   bool operator==(const ConstraintDefinition& other) const
81   {
82     return mSourceIdx == other.mSourceIdx && mProperty == other.mProperty;
83   }
84
85   bool operator!=(const ConstraintDefinition& other) const
86   {
87     return !operator==(other);
88   }
89 };
90
91 struct DALI_SCENE_LOADER_API Transforms
92 {
93   MatrixStack modelStack;
94   const ViewProjection& viewProjection;
95 };
96
97 /**
98  * @brief Information about a skeleton and the shader that needs to be configured with it.
99  * @note Multiple skeletons shalt not share the same shader.
100  */
101 struct DALI_SCENE_LOADER_API SkinningShaderConfigurationRequest
102 {
103   Index mSkeletonIdx;
104   Shader mShader;
105
106   bool operator<(const SkinningShaderConfigurationRequest& other) const
107   {
108     return mShader < other.mShader;
109   }
110 };
111
112 /**
113  * @brief Needed to configure blend shape properties.
114  */
115 struct DALI_SCENE_LOADER_API BlendshapeShaderConfigurationRequest
116 {
117   std::string mNodeName;
118   Index mMeshIdx;
119   Shader mShader;
120
121   bool operator<(const BlendshapeShaderConfigurationRequest& other) const
122   {
123     return mShader < other.mShader;
124   }
125 };
126
127 /**
128  * @brief Request for creating a constraint, output from NodeDefinition::OnCreate.
129  */
130 struct DALI_SCENE_LOADER_API ConstraintRequest
131 {
132   const ConstraintDefinition* const mConstraint;  ///< Definition of the constraint to create.
133   Actor mTarget;  ///< Target of the constraint.
134 };
135
136 /**
137  * @brief Defines a node, consisting of a name, a transform, a size, a list of child nodes,
138  *  and slots for customization and rendering logic, which are mutually exclusive in the
139  *  current implementation.
140  */
141 struct DALI_SCENE_LOADER_API NodeDefinition
142 {
143 public:  // TYPES
144   using Vector = std::vector<NodeDefinition>;
145
146   struct CreateParams
147   {
148   public: // input
149     const ResourceBundle& mResources;
150     Transforms& mXforms;
151
152   public: // output
153     std::vector<ConstraintRequest> mConstrainables;
154     std::vector<SkinningShaderConfigurationRequest> mSkinnables;
155     std::vector<BlendshapeShaderConfigurationRequest> mBlendshapeRequests;
156   };
157
158   class DALI_SCENE_LOADER_API Renderable
159   {
160   public: // DATA
161     Index mShaderIdx = INVALID_INDEX;
162
163   public: // METHODS
164     virtual ~Renderable() = default;
165
166     virtual void RegisterResources(IResourceReceiver& receiver) const;
167     virtual void ReflectResources(IResourceReflector& reflector);
168     virtual void OnCreate(const NodeDefinition& node, CreateParams& params, Actor& actor) const;
169   };
170
171   struct CustomizationDefinition
172   {
173     std::string mTag;
174
175     Index GetChildId(const Customization::Choices& choices, const NodeDefinition& node)
176     {
177       auto choice = choices.Get(mTag);
178       return std::min(choice != Customization::NONE ? choice : 0,
179         static_cast<Index>(node.mChildren.size() - 1));
180     }
181   };
182
183   class IVisitor
184   {
185   public:
186     virtual void Start(NodeDefinition& n) = 0;
187     virtual void Finish(NodeDefinition& n) = 0;
188
189   protected:
190     ~IVisitor() = default; // deliberately non-virtual these are transient objects and we don't want to pay for the vtable.
191   };
192
193   class IConstVisitor
194   {
195   public:
196     virtual void Start(const NodeDefinition& n) = 0;
197     virtual void Finish(const NodeDefinition& n) = 0;
198
199   protected:
200     ~IConstVisitor() = default; // deliberately non-virtual these are transient objects and we don't want to pay for the vtable.
201   };
202
203   struct Extra
204   {
205     std::string mKey;
206     Property::Value mValue;
207
208     bool operator<(const Extra& other) const
209     {
210       return mKey < other.mKey;
211     }
212   };
213
214 public:  // METHODS
215   /**
216    * @brief Creates a DALi Actor from this definition only.
217    * @note Not recursive.
218    */
219   Actor CreateActor(CreateParams& params) const;
220
221   Matrix GetLocalSpace() const;
222
223 public: // DATA
224   static const std::string ORIGINAL_MATRIX_PROPERTY_NAME;
225
226   std::string mName;
227
228   Vector3 mPosition = Vector3::ZERO;
229   Quaternion mOrientation = Quaternion::IDENTITY;
230   Vector3 mScale = Vector3::ONE;
231   Vector3 mSize = Vector3::ONE;
232
233   bool mIsVisible = true;
234
235   std::unique_ptr<Renderable> mRenderable;
236   std::unique_ptr<CustomizationDefinition> mCustomization;
237   std::vector<Extra> mExtras;
238   std::vector<ConstraintDefinition> mConstraints;
239
240   std::vector<Index> mChildren;
241   Index mParentIdx = INVALID_INDEX;
242 };
243
244 class DALI_SCENE_LOADER_API ModelNode : public NodeDefinition::Renderable
245 {
246 public: // DATA
247   Vector4 mColor = Color::WHITE;
248   Index mMeshIdx = INVALID_INDEX;
249   Index mMaterialIdx = INVALID_INDEX;
250
251 public: // METHODS
252   void RegisterResources(IResourceReceiver& receiver) const override;
253   void ReflectResources(IResourceReflector& reflector) override;
254   void OnCreate(const NodeDefinition& node, NodeDefinition::CreateParams& params, Actor& actor) const override;
255 };
256
257 /**
258  * @brief Parameters for an Arc node.
259  */
260 class DALI_SCENE_LOADER_API ArcNode : public ModelNode
261 {
262 public: // DATA
263   bool mAntiAliasing = true;
264   int mArcCaps = 0;
265   float mStartAngleDegrees = .0f;
266   float mEndAngleDegrees = .0f;
267   float mRadius = .0f;
268
269 public: // METHODS
270   static void GetEndVectorWithDiffAngle(float startAngle, float endAngle, Vector2& endVector);
271
272   void OnCreate(const NodeDefinition& node, NodeDefinition::CreateParams& params, Actor& actor) const override;
273 };
274
275 }
276 }
277
278 #endif //DALI_SCENE_LOADER_NODE_DEFINITION_H_