Compute min/max value if min/max is not defined.
[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) 2022 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 <functional>
27 #include <memory>
28 #include <string>
29 #include "dali/public-api/actors/actor.h"
30 #include "dali/public-api/math/matrix.h"
31 #include "dali/public-api/math/quaternion.h"
32 #include "dali/public-api/math/vector4.h"
33
34 namespace Dali
35 {
36 namespace SceneLoader
37 {
38 class ViewProjection;
39
40 /**
41  * @brief Interface to report (const) resource ids to.
42  */
43 class DALI_SCENE_LOADER_API IResourceReceiver
44 {
45 public:
46   virtual ~IResourceReceiver() = default;
47
48   virtual void Register(ResourceType::Value type, Index id) = 0;
49 };
50
51 /**
52  * @brief Interface to report modifiable resource ids to.
53  * @note These are supposed to be transient. Obviously, the references collected
54  *  this way must not outlive the objects that they came from.
55  */
56 class DALI_SCENE_LOADER_API IResourceReflector
57 {
58 public:
59   virtual ~IResourceReflector() = default;
60
61   virtual void Reflect(ResourceType::Value type, Index& id) = 0;
62 };
63
64 /**
65  * @brief Intermediate representation for a constraint that shall be
66  *  set up after the Actors were created. The target of the constraint
67  *  is the node definition that carries it.
68  */
69 struct DALI_SCENE_LOADER_API ConstraintDefinition
70 {
71   std::string mProperty;  ///< name of the property to constrain.
72   Index       mSourceIdx; ///< index of the node to serve as the source of the constraint.
73
74   bool operator<(const ConstraintDefinition& other) const
75   {
76     return mProperty < other.mProperty;
77   }
78
79   bool operator==(const ConstraintDefinition& other) const
80   {
81     return mSourceIdx == other.mSourceIdx && mProperty == other.mProperty;
82   }
83
84   bool operator!=(const ConstraintDefinition& other) const
85   {
86     return !operator==(other);
87   }
88 };
89
90 struct DALI_SCENE_LOADER_API Transforms
91 {
92   MatrixStack           modelStack;
93   const ViewProjection& viewProjection;
94 };
95
96 /**
97  * @brief Information about a skeleton and the shader that needs to be configured with it.
98  * @note Multiple skeletons shalt not share the same shader.
99  */
100 struct DALI_SCENE_LOADER_API SkinningShaderConfigurationRequest
101 {
102   Index  mSkeletonIdx;
103   Shader mShader;
104
105   bool operator<(const SkinningShaderConfigurationRequest& other) const
106   {
107     return mShader < other.mShader;
108   }
109 };
110
111 /**
112  * @brief Needed to configure blend shape properties.
113  */
114 struct DALI_SCENE_LOADER_API BlendshapeShaderConfigurationRequest
115 {
116   std::string mNodeName;
117   Index       mMeshIdx;
118   Shader      mShader;
119
120   bool operator<(const BlendshapeShaderConfigurationRequest& other) const
121   {
122     return mShader < other.mShader;
123   }
124 };
125
126 /**
127  * @brief Request for creating a constraint, output from NodeDefinition::OnCreate.
128  */
129 struct DALI_SCENE_LOADER_API ConstraintRequest
130 {
131   const ConstraintDefinition* const mConstraint; ///< Definition of the constraint to create.
132   Actor                             mTarget;     ///< Target of the constraint.
133 };
134
135 /**
136  * @brief Defines a node, consisting of a name, a transform, a size, a list of child nodes,
137  *  and slots for customization and rendering logic, which are mutually exclusive in the
138  *  current implementation.
139  */
140 struct DALI_SCENE_LOADER_API NodeDefinition
141 {
142 public: // TYPES
143   using Vector = std::vector<NodeDefinition>;
144
145   struct CreateParams
146   {
147   public: // input
148     const ResourceBundle& mResources;
149     Transforms&           mXforms;
150
151   public: // output
152     std::vector<ConstraintRequest>                    mConstrainables;
153     std::vector<SkinningShaderConfigurationRequest>   mSkinnables;
154     std::vector<BlendshapeShaderConfigurationRequest> mBlendshapeRequests;
155   };
156
157   class DALI_SCENE_LOADER_API Renderable
158   {
159   public: // DATA
160     Index mShaderIdx = INVALID_INDEX;
161
162   public: // METHODS
163     virtual ~Renderable() = default;
164
165     virtual bool GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const;
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   /**
222    * @brief Gets local space matrix of this node
223    * @return Matrix of local space.
224    */
225   Matrix GetLocalSpace() const;
226
227   /**
228    * @brief Retrieves minimum and maximum position of this node in local space.
229    * @param[in] resources ResourceBundle that contains mesh information of this node.
230    * @param[out] min Minimum position of the mesh of this node.
231    * @param[out] max Maximum position of the mesh of this node.
232    * @return true If the node has mesh.
233    */
234   bool GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const;
235
236   /**
237    * @brief Retrieves Scale Factor uniform name.
238    * This uniform name can be used to change scale factor for ibl.
239    * @return std::string_view of the scale factor uniform name.
240    */
241   static std::string_view GetIblScaleFactorUniformName();
242
243   /**
244    * @brief Retrieves ibl Ydirection uniform name.
245    * This uniform name can be used to flip y direction of ibl in shader.
246    * @return std::string_view of the YDirection uniform name.
247    */
248   static std::string_view GetIblYDirectionUniformName();
249
250 public: // DATA
251   static const std::string ORIGINAL_MATRIX_PROPERTY_NAME;
252
253   std::string mName;
254
255   Vector3    mPosition    = Vector3::ZERO;
256   Quaternion mOrientation = Quaternion::IDENTITY;
257   Vector3    mScale       = Vector3::ONE;
258   Vector3    mSize        = Vector3::ONE;
259
260   bool mIsVisible = true;
261
262   std::unique_ptr<Renderable>              mRenderable;
263   std::unique_ptr<CustomizationDefinition> mCustomization;
264   std::vector<Extra>                       mExtras;
265   std::vector<ConstraintDefinition>        mConstraints;
266
267   std::vector<Index> mChildren;
268   Index              mParentIdx = INVALID_INDEX;
269 };
270
271 class DALI_SCENE_LOADER_API ModelNode : public NodeDefinition::Renderable
272 {
273 public: // DATA
274   Vector4 mColor       = Color::WHITE;
275   Index   mMeshIdx     = INVALID_INDEX;
276   Index   mMaterialIdx = INVALID_INDEX;
277
278 public: // METHODS
279   bool GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const override;
280   void RegisterResources(IResourceReceiver& receiver) const override;
281   void ReflectResources(IResourceReflector& reflector) override;
282   void OnCreate(const NodeDefinition& node, NodeDefinition::CreateParams& params, Actor& actor) const override;
283 };
284
285 /**
286  * @brief Parameters for an Arc node.
287  */
288 class DALI_SCENE_LOADER_API ArcNode : public ModelNode
289 {
290 public: // DATA
291   bool  mAntiAliasing      = true;
292   int   mArcCaps           = 0;
293   float mStartAngleDegrees = .0f;
294   float mEndAngleDegrees   = .0f;
295   float mRadius            = .0f;
296
297 public: // METHODS
298   static void GetEndVectorWithDiffAngle(float startAngle, float endAngle, Vector2& endVector);
299
300   void OnCreate(const NodeDefinition& node, NodeDefinition::CreateParams& params, Actor& actor) const override;
301 };
302
303 } // namespace SceneLoader
304 } // namespace Dali
305
306 #endif //DALI_SCENE_LOADER_NODE_DEFINITION_H_