Merge "Combine Internal::ProxyObject & Internal::Object" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / modeling / model-data-impl.h
1 #ifndef __DALI_INTERNAL_MODEL_DATA_H__
2 #define __DALI_INTERNAL_MODEL_DATA_H__
3
4 /*
5  * Copyright (c) 2014 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 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/public-api/common/light.h>
24 #include <dali/public-api/modeling/material.h>
25 #include <dali/public-api/modeling/entity.h>
26 #include <dali/public-api/modeling/model-data.h>
27 #include <dali/public-api/modeling/model-animation-map.h>
28 #include <dali/public-api/object/base-object.h>
29 #include <dali/internal/event/resources/resource-ticket.h>
30 #include <dali/internal/common/owner-container.h>
31
32 namespace Dali
33 {
34
35 class Light;
36 class Entity;
37
38 namespace Internal
39 {
40
41 class ModelData;
42 class ResourceClient;
43
44 typedef IntrusivePtr<ModelData> ModelDataPtr;
45
46 /**
47  * Encapsulates a Dali 3D model/scene.
48  */
49 class ModelData : public BaseObject
50 {
51 public:
52   /**
53    * Constructor
54    */
55   ModelData();
56
57   /**
58    * Constructor
59    * @param[in] name The name of the model.
60    * @return          None.
61    */
62   static ModelDataPtr New(const std::string& name);
63
64   /**
65    * Sets the model name
66    * @param[in] name The model name
67    */
68   void SetName(const std::string& name);
69
70   /**
71    * Returns the name of the model
72    * @return the model name
73    */
74   const std::string& GetName() const;
75
76   /**
77    * Set the root Entity of the model.
78    * @param root - the root entity
79    */
80   void SetRootEntity(Dali::Entity root);
81
82   /**
83    * Get the root Entity of the model.
84    * @return the root entity
85    */
86   Dali::Entity GetRootEntity() const;
87
88   /**
89    * Add a mesh to the model
90    * @param mesh - the mesh to add.
91    */
92   void AddMesh(const Dali::MeshData& mesh);
93
94   /**
95    * Add a mesh ticket to the model. ModelFactory should ensure that the index
96    * of this ticket is the same as the MeshData it represents.
97    * @param[in] ticket   The ticket of the mesh resource.
98    */
99   void AddMeshTicket(ResourceTicketPtr ticket);
100
101   /**
102    * Get a mesh by index.
103    * @param[in] index The zero based index to a mesh
104    * @return          The MeshData object.
105    */
106   const Dali::MeshData& GetMesh(unsigned int index) const;
107
108   /**
109    * Get a mesh by index.
110    * @param[in] index The zero based index to a mesh
111    * @return          The MeshData object.
112    */
113   Dali::MeshData& GetMesh(unsigned int index);
114
115   /**
116    * Get a mesh ticket by index
117    * @param[in] index The index of a mesh
118    * @return    The resource ticket for that mesh
119    */
120   const ResourceTicketPtr GetMeshTicket(unsigned int index) const;
121
122   /**
123    * Get Mesh count
124    * @returns number of meshes
125    */
126   unsigned int NumberOfMeshes() const;
127
128   /**
129    * Add material to the model
130    * @param[in] material - the material to add to the model
131    */
132   void AddMaterial(Dali::Material material);
133
134   /**
135    * Get a material by index.
136    * @param[in] index The zero based index to a material
137    * @return          A pointer to a material, or NULL.
138    */
139   Dali::Material GetMaterial(unsigned int index) const;
140
141   /**
142    * Get material count
143    * @return number of materials
144    */
145   unsigned int NumberOfMaterials() const;
146
147   /**
148    * Get animation map container.
149    * @return reference to the animation map container.
150    */
151    ModelAnimationMapContainer& GetAnimationMapContainer();
152
153   /**
154    * Get the animation for the given index. Returns a null pointer
155    * if the index is out of range.
156    * @param[in] index The index of the animation
157    * @return A pointer to the animation data
158    */
159   const ModelAnimationMap* GetAnimationMap (unsigned int index) const;
160
161   /**
162    * Get the animation for the given name. Returns a null pointer
163    * if the name is not found.
164    * @param[in] name Name of animation for which to search
165    * @return A pointer to the animation data
166    */
167   const ModelAnimationMap* GetAnimationMap (const std::string& name) const;
168
169   /**
170    * Get the duration for a given animation. Returns zero if not found.
171    * @param[in] animationIndex - the index of the animation
172    * @return the duration in seconds, or zero if not found
173    */
174   float GetAnimationDuration(size_t animationIndex) const;
175
176   /**
177    * Get the index for the given name. Returns false if not found.
178    * @param[in] name The name of the animation to find
179    * @param[out] index The index of the named animation if found
180    * @return true if the animation was foud, false if not found.
181    */
182   bool FindAnimation (const std::string& name, unsigned int& index) const;
183
184   /**
185    * Get the number of animation maps
186    * @return the number of animation maps
187    */
188   unsigned int NumberOfAnimationMaps() const;
189
190   /**
191    * Add a light to the model
192    * @param light - the light to add to the model
193    */
194   void AddLight(Dali::Light light);
195
196   /**
197    * Get a light by index.
198    * @param[in] index The zero based index to a light
199    * @return          A pointer to a light, or NULL.
200    */
201   Dali::Light GetLight(unsigned int index) const;
202
203   /**
204    * Get the number of lights contained in the model.
205    * @return The number of lights contained in the model.
206    */
207   unsigned int NumberOfLights() const;
208
209   /**
210    * Unpacking is where new resources are created, for the meshes provided in the model data.
211    * This should be called after the model data has loaded.
212    * @param[in] resourceClient Used to create new resource tickets.
213    */
214   void Unpack( ResourceClient& resourceClient );
215
216   /**
217    * @copydoc Dali::ModelData::Read()
218    */
219   bool Read(std::streambuf& buf);
220
221   /**
222    * @copydoc Dali::ModelData::Write()
223    */
224   bool Write(std::streambuf& buf) const;
225
226 protected:
227
228   /**
229    * A reference counted object may only be deleted by calling Unreference()
230    */
231   virtual ~ModelData();
232
233 private:
234
235   std::string                    mName;            ///< 3D model name
236   std::vector<Dali::Material>    mMaterials;       ///< Container of Materials
237   OwnerContainer< MeshData* >    mMeshes;          ///< Container of Meshes
238   std::vector<Dali::Light>       mLights;          ///< Collection of Lights
239   ModelAnimationMapContainer     mAnimationMaps;   ///< List of Animation maps
240   Dali::Entity                   mRoot;            ///< Root of the entity hierarchy tree
241
242   bool                           mUnpacked;        ///< True if resources have been created, for the meshes provided in the model data.
243   std::vector<ResourceTicketPtr> mMeshTickets;     ///< List of Mesh tickets to scene graph objects
244 }; // class ModelData
245
246 } // namespace Internal
247
248 // Helpers for public-api forwarding methods
249
250 inline Internal::ModelData& GetImplementation(Dali::ModelData& modelData)
251 {
252   DALI_ASSERT_ALWAYS( modelData && "ModelData handle is empty" );
253
254   BaseObject& handle = modelData.GetBaseObject();
255
256   return static_cast<Internal::ModelData&>(handle);
257 }
258
259 inline const Internal::ModelData& GetImplementation(const Dali::ModelData& modelData)
260 {
261   DALI_ASSERT_ALWAYS( modelData && "ModelData handle is empty" );
262
263   const BaseObject& handle = modelData.GetBaseObject();
264
265   return static_cast<const Internal::ModelData&>(handle);
266 }
267
268
269 } // namespace Dali
270
271
272 #endif // __DALI_INTERNAL_MODEL_DATA_H__