DALi Version 2.1.31
[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) 2021 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 void RegisterResources(IResourceReceiver& receiver) const;
166     virtual void ReflectResources(IResourceReflector& reflector);
167     virtual void OnCreate(const NodeDefinition& node, CreateParams& params, Actor& actor) const;
168   };
169
170   struct CustomizationDefinition
171   {
172     std::string mTag;
173
174     Index GetChildId(const Customization::Choices& choices, const NodeDefinition& node)
175     {
176       auto choice = choices.Get(mTag);
177       return std::min(choice != Customization::NONE ? choice : 0,
178                       static_cast<Index>(node.mChildren.size() - 1));
179     }
180   };
181
182   class IVisitor
183   {
184   public:
185     virtual void Start(NodeDefinition& n)  = 0;
186     virtual void Finish(NodeDefinition& n) = 0;
187
188   protected:
189     ~IVisitor() = default; // deliberately non-virtual these are transient objects and we don't want to pay for the vtable.
190   };
191
192   class IConstVisitor
193   {
194   public:
195     virtual void Start(const NodeDefinition& n)  = 0;
196     virtual void Finish(const NodeDefinition& n) = 0;
197
198   protected:
199     ~IConstVisitor() = default; // deliberately non-virtual these are transient objects and we don't want to pay for the vtable.
200   };
201
202   struct Extra
203   {
204     std::string     mKey;
205     Property::Value mValue;
206
207     bool operator<(const Extra& other) const
208     {
209       return mKey < other.mKey;
210     }
211   };
212
213 public: // METHODS
214   /**
215    * @brief Creates a DALi Actor from this definition only.
216    * @note Not recursive.
217    */
218   Actor CreateActor(CreateParams& params) const;
219
220   Matrix GetLocalSpace() const;
221
222 public: // DATA
223   static const std::string ORIGINAL_MATRIX_PROPERTY_NAME;
224
225   std::string mName;
226
227   Vector3    mPosition    = Vector3::ZERO;
228   Quaternion mOrientation = Quaternion::IDENTITY;
229   Vector3    mScale       = Vector3::ONE;
230   Vector3    mSize        = Vector3::ONE;
231
232   bool mIsVisible = true;
233
234   std::unique_ptr<Renderable>              mRenderable;
235   std::unique_ptr<CustomizationDefinition> mCustomization;
236   std::vector<Extra>                       mExtras;
237   std::vector<ConstraintDefinition>        mConstraints;
238
239   std::vector<Index> mChildren;
240   Index              mParentIdx = INVALID_INDEX;
241 };
242
243 class DALI_SCENE_LOADER_API ModelNode : public NodeDefinition::Renderable
244 {
245 public: // DATA
246   Vector4 mColor       = Color::WHITE;
247   Index   mMeshIdx     = INVALID_INDEX;
248   Index   mMaterialIdx = INVALID_INDEX;
249
250 public: // METHODS
251   void RegisterResources(IResourceReceiver& receiver) const override;
252   void ReflectResources(IResourceReflector& reflector) override;
253   void OnCreate(const NodeDefinition& node, NodeDefinition::CreateParams& params, Actor& actor) const override;
254 };
255
256 /**
257  * @brief Parameters for an Arc node.
258  */
259 class DALI_SCENE_LOADER_API ArcNode : public ModelNode
260 {
261 public: // DATA
262   bool  mAntiAliasing      = true;
263   int   mArcCaps           = 0;
264   float mStartAngleDegrees = .0f;
265   float mEndAngleDegrees   = .0f;
266   float mRadius            = .0f;
267
268 public: // METHODS
269   static void GetEndVectorWithDiffAngle(float startAngle, float endAngle, Vector2& endVector);
270
271   void OnCreate(const NodeDefinition& node, NodeDefinition::CreateParams& params, Actor& actor) const override;
272 };
273
274 } // namespace SceneLoader
275 } // namespace Dali
276
277 #endif //DALI_SCENE_LOADER_NODE_DEFINITION_H_