Fixed SVACE and related issues in dali-scene-loader.
[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()
48   {}
49
50   virtual void Register(ResourceType::Value type, Index id) = 0;
51 };
52
53 /**
54  * @brief Interface to report modifiable resource ids to.
55  * @note These are supposed to be transient. Obviously, the references collected
56  *  this way must not outlive the objects that they came from.
57  */
58 class DALI_SCENE_LOADER_API IResourceReflector
59 {
60 public:
61   virtual ~IResourceReflector()
62   {}
63
64   virtual void Reflect(ResourceType::Value type, Index& id) = 0;
65 };
66
67 /**
68  * @brief Intermediate representation for a constraint that shall be
69  *  set up after the Actors were created. The target of the constraint
70  *  is the node definition that carries it.
71  */
72 struct DALI_SCENE_LOADER_API ConstraintDefinition
73 {
74   std::string mProperty;  ///< name of the property to constrain.
75   Index mSourceIdx;  ///< index of the node to serve as the source of the constraint.
76
77   bool operator<(const ConstraintDefinition& other) const
78   {
79     return mProperty < other.mProperty;
80   }
81
82   bool operator==(const ConstraintDefinition& other) const
83   {
84     return mSourceIdx == other.mSourceIdx && mProperty == other.mProperty;
85   }
86
87   bool operator!=(const ConstraintDefinition& other) const
88   {
89     return !operator==(other);
90   }
91 };
92
93 struct DALI_SCENE_LOADER_API Transforms
94 {
95   MatrixStack modelStack;
96   const ViewProjection& viewProjection;
97 };
98
99 /**
100  * @brief Information about a skeleton and the shader that needs to be configured with it.
101  * @note Multiple skeletons shalt not share the same shader.
102  */
103 struct DALI_SCENE_LOADER_API SkinningShaderConfigurationRequest
104 {
105   Index mSkeletonIdx;
106   Shader mShader;
107
108   bool operator<(const SkinningShaderConfigurationRequest& other) const
109   {
110     return mShader < other.mShader;
111   }
112 };
113
114 /**
115  * @brief Needed to configure blend shape properties.
116  */
117 struct DALI_SCENE_LOADER_API BlendshapeShaderConfigurationRequest
118 {
119   std::string mNodeName;
120   Index mMeshIdx;
121   Shader mShader;
122
123   bool operator<(const BlendshapeShaderConfigurationRequest& other) const
124   {
125     return mShader < other.mShader;
126   }
127 };
128
129 /**
130  * @brief Request for creating a constraint, output from NodeDefinition::OnCreate.
131  */
132 struct DALI_SCENE_LOADER_API ConstraintRequest
133 {
134   const ConstraintDefinition* const mConstraint;  ///< Definition of the constraint to create.
135   Actor mTarget;  ///< Target of the constraint.
136 };
137
138 /**
139  * @brief Defines a node, consisting of a name, a transform, a size, a list of child nodes,
140  *  and slots for customization and rendering logic, which are mutually exclusive in the
141  *  current implementation.
142  */
143 struct DALI_SCENE_LOADER_API NodeDefinition
144 {
145 public:  // TYPES
146   using Vector = std::vector<NodeDefinition>;
147
148   struct CreateParams
149   {
150   public: // input
151     const ResourceBundle& mResources;
152     Transforms& mXforms;
153
154   public: // output
155     std::vector<ConstraintRequest> mConstrainables;
156     std::vector<SkinningShaderConfigurationRequest> mSkinnables;
157     std::vector<BlendshapeShaderConfigurationRequest> mBlendshapeRequests;
158   };
159
160   class DALI_SCENE_LOADER_API Renderable
161   {
162   public: // DATA
163     Index mShaderIdx = INVALID_INDEX;
164
165   public: // METHODS
166     virtual ~Renderable() {}
167
168     virtual void RegisterResources(IResourceReceiver& receiver) const;
169     virtual void ReflectResources(IResourceReflector& reflector);
170     virtual void OnCreate(const NodeDefinition& node, CreateParams& params, Actor& actor) const;
171   };
172
173   struct CustomizationDefinition
174   {
175     std::string mTag;
176
177     Index GetChildId(const Customization::Choices& choices, const NodeDefinition& node)
178     {
179       auto choice = choices.Get(mTag);
180       return std::min(choice != Customization::NONE ? choice : 0,
181         static_cast<Index>(node.mChildren.size() - 1));
182     }
183   };
184
185   class IVisitor
186   {
187   public:
188     virtual void Start(NodeDefinition& n) = 0;
189     virtual void Finish(NodeDefinition& n) = 0;
190
191   protected:
192     ~IVisitor() {}
193   };
194
195   class IConstVisitor
196   {
197   public:
198     virtual void Start(const NodeDefinition& n) = 0;
199     virtual void Finish(const NodeDefinition& n) = 0;
200
201   protected:
202     ~IConstVisitor() {}
203   };
204
205   struct Extra
206   {
207     std::string mKey;
208     Property::Value mValue;
209
210     bool operator<(const Extra& other) const
211     {
212       return mKey < other.mKey;
213     }
214   };
215
216 public:  // METHODS
217   /**
218    * @brief Creates a DALi Actor from this definition only.
219    * @note Not recursive.
220    */
221   Actor CreateActor(CreateParams& params) const;
222
223   Matrix GetLocalSpace() const;
224
225 public: // DATA
226   static const std::string ORIGINAL_MATRIX_PROPERTY_NAME;
227
228   std::string mName;
229
230   Vector3 mPosition = Vector3::ZERO;
231   Quaternion mOrientation = Quaternion::IDENTITY;
232   Vector3 mScale = Vector3::ONE;
233   Vector3 mSize = Vector3::ONE;
234
235   bool mIsVisible = true;
236
237   std::unique_ptr<Renderable> mRenderable;
238   std::unique_ptr<CustomizationDefinition> mCustomization;
239   std::vector<Extra> mExtras;
240   std::vector<ConstraintDefinition> mConstraints;
241
242   std::vector<Index> mChildren;
243   Index mParentIdx = INVALID_INDEX;
244 };
245
246 class DALI_SCENE_LOADER_API ModelNode : public NodeDefinition::Renderable
247 {
248 public: // DATA
249   Vector4 mColor = Color::WHITE;
250   Index mMeshIdx = INVALID_INDEX;
251   Index mMaterialIdx = INVALID_INDEX;
252
253 public: // METHODS
254   void RegisterResources(IResourceReceiver& receiver) const override;
255   void ReflectResources(IResourceReflector& reflector) override;
256   void OnCreate(const NodeDefinition& node, NodeDefinition::CreateParams& params, Actor& actor) const override;
257 };
258
259 /**
260  * @brief Parameters for an Arc node.
261  */
262 class DALI_SCENE_LOADER_API ArcNode : public ModelNode
263 {
264 public: // DATA
265   bool mAntiAliasing = true;
266   int mArcCaps = 0;
267   float mStartAngleDegrees = .0f;
268   float mEndAngleDegrees = .0f;
269   float mRadius = .0f;
270
271 public: // METHODS
272   static void GetEndVectorWithDiffAngle(float startAngle, float endAngle, Vector2& endVector);
273
274   void OnCreate(const NodeDefinition& node, NodeDefinition::CreateParams& params, Actor& actor) const override;
275 };
276
277 }
278 }
279
280 #endif //DALI_SCENE_LOADER_NODE_DEFINITION_H_