(Properties) Added ability to add non-animatable event-thread only properties via...
[platform/core/uifw/dali-core.git] / dali / internal / event / actor-attachments / mesh-attachment-impl.h
1 #ifndef __DALI_INTERNAL_MESH_ATTACHMENT_H__
2 #define __DALI_INTERNAL_MESH_ATTACHMENT_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // INTERNAL INCLUDES
21 #include <dali/public-api/geometry/mesh.h>
22 #include <dali/public-api/modeling/material.h>
23 #include <dali/internal/common/owner-container.h>
24 #include <dali/internal/event/actors/actor-declarations.h>
25 #include <dali/internal/event/actor-attachments/actor-attachment-declarations.h>
26 #include <dali/internal/event/actor-attachments/renderable-attachment-impl.h>
27 #include <dali/internal/event/modeling/mesh-impl.h>
28 #include <dali/internal/event/modeling/material-impl.h>
29 #include <dali/internal/event/resources/resource-ticket.h>
30 #include <dali/internal/event/common/proxy-object.h>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 namespace SceneGraph
39 {
40 class MeshAttachment;
41 class Node;
42 }
43
44 typedef std::vector<std::string>               BoneNames;
45 typedef BoneNames::const_iterator              BoneNamesIter;
46
47 /**
48  * An attachment for rendering a 3D mesh with a material.
49  */
50 class MeshAttachment : public RenderableAttachment
51 {
52 public:
53
54   struct MeshAndCustomMaterial
55   {
56     Internal::MeshIPtr          mMesh;
57     Internal::MaterialIPtr      mMaterial;
58     Internal::MaterialIPtr      mCustomMaterial;
59     BoneNames                   mBoneNames;
60   };
61
62   /**
63    * Create an initialised MeshAttachment.
64    * @param[in] parentNode The node to attach a scene-object to.
65    * This will not be displayed until a mesh is added with SetMesh().
66    * @return A handle to a newly allocated Dali resource.
67    */
68   static MeshAttachmentPtr New( const SceneGraph::Node& parentNode );
69
70   /**
71    * Set the mesh to be rendererd by this attachment
72    * @param[in] meshPtr  A mesh impl (may be null)
73    * @param[in] meshId   The resource id of the mesh
74    * @param[in] bones    Container of bones for this mesh
75    * @param[in] material The material
76    */
77   void SetMesh( const MeshIPtr        meshPtr,
78                 ResourceId            meshId,
79                 const BoneContainer&  bones,
80                 const MaterialIPtr    material );
81
82   /**
83    * Add a mesh to the collection of meshes being rendered by this attachment
84    * @param [in] meshResource A mesh resource
85    * @param[in] bones    Container of bones for this mesh
86    * @param [in] material Material
87    */
88   void SetMesh( const ResourceTicketPtr     meshResource,
89                 const BoneContainer&  bones,
90                 const MaterialIPtr    material );
91
92   /**
93    * Set the material of the mesh
94    * @param[in] materialPtr - a custom material to apply to this mesh
95    */
96   void SetMaterial( Internal::MaterialIPtr materialPtr );
97
98   /**
99    * Get the material of the mesh
100    * @return The internal material pointer
101    */
102   Internal::MaterialIPtr GetMaterial( ) const;
103
104   /**
105    * Disconnect any connected material from the mesh
106    */
107   void DisconnectMaterial( );
108
109   /**
110    * Set whether this mesh actor should be affected by lights in the
111    * scene.  If it set to false, then the mesh will be unaffected by
112    * lighting, and will be evenly lit without any shading.
113    * This property is not inherited.
114    * @param[in] affectedByLighting Whether the actor should be lit by the scene lighting.
115    */
116   void SetAffectedByLighting( bool affectedByLighting );
117
118   /**
119    * @return true if the actor is lit by the scene, or false if evenly lit.
120    */
121   bool IsAffectedByLighting();
122
123   /**
124    * Search the actor tree for all named bones in the mesh  and connect them.
125    * @param[in] rootActor - the root actor of the actor tree.
126    */
127   void BindBonesToMesh( Internal::ActorPtr rootActor );
128
129   /**
130    * Connects a boneActor to the mesh (allows the mesh renderer to do
131    * skeletal animation by calculating the actor transforms)
132    * @param[in] boneActor - the actor containing the bone's current position
133    * @param[in] boneIdx   - the index of the bone in this mesh
134    * @param[in] boneCount - the bone count
135    */
136   void ConnectBoneActor( Internal::ActorPtr boneActor, size_t boneIdx, size_t boneCount );
137
138   const MeshAndCustomMaterial& GetMesh() const
139   {
140     return mMesh;
141   }
142
143 protected:
144
145   /**
146    * A reference counted object may only be deleted by calling Unreference()
147    */
148   virtual ~MeshAttachment();
149
150 private:
151
152   /**
153    * First stage construction of a MeshAttachment.
154    * @param[in] stage Used to send messages to scene-graph.
155    */
156   MeshAttachment( Stage& stage );
157
158   /**
159    * @copydoc Dali::Internal::RenderableAttachment::OnStageConnection2()
160    */
161   virtual void OnStageConnection2();
162
163   /**
164    * @copydoc Dali::Internal::RenderableAttachment::OnStageDisconnection2()
165    */
166   virtual void OnStageDisconnection2();
167
168   /**
169    * @copydoc Dali::Internal::RenderableAttachment::GetSceneObject()
170    */
171   virtual const SceneGraph::RenderableAttachment& GetSceneObject() const;
172
173 protected:
174   /**
175    * Set the node for given bone index in the scene graph object
176    * @param[in] node The node to set
177    * @param[in] boneIdx The bone index
178    */
179   void SetBoneNode( SceneGraph::Node* node, size_t boneIdx );
180
181   /**
182    * Helper class for connecting Nodes to the scene-graph MeshAttachment
183    */
184   class Connector : public ProxyObject::Observer
185   {
186   public:
187     /**
188      * Create the helper class
189      */
190     Connector( Internal::ActorPtr boneActor, size_t boneIdx, MeshAttachment& meshAttachment);
191
192     /**
193      * Copy constructor required for vector storage
194      */
195     Connector(const Connector&);
196
197     /**
198      * Assignment constructor required for vector storage
199      */
200     Connector& operator=(const Connector& rhs);
201
202     /**
203      * Destructor
204      */
205     ~Connector();
206
207     /**
208      * Update the scene-graph mesh attachment with this node
209      */
210     void ConnectNode();
211
212   public: // From ProxyObject::Observer
213
214     /**
215      * @copydoc Dali::Internal::ProxyObject::Observer::SceneObjectAdded
216      */
217     virtual void SceneObjectAdded( ProxyObject& proxy );
218
219     /**
220      * @copydoc Dali::Internal::ProxyObject::Observer::SceneObjectAdded
221      */
222     virtual void SceneObjectRemoved( ProxyObject& proxy );
223
224     /**
225      * @copydoc Dali::Internal::ProxyObject::Observer::ProxyDestroyed
226      */
227     virtual void ProxyDestroyed( ProxyObject& proxy );
228
229   public:
230     MeshAttachment& mMeshAttachment; ///< Parent
231     Actor* mActor;   ///< Raw-pointer to the actor; not owned.
232     size_t mBoneIdx; ///< Index of the bone with which this connecter is associated
233   };
234   typedef OwnerContainer< Connector* > ConnectorList;
235
236 private:
237
238   const SceneGraph::MeshAttachment* mSceneObject; ///< Not owned
239
240   MeshAndCustomMaterial mMesh;
241   bool                  mAffectedByLighting;
242   ConnectorList         mConnectors;
243 };
244
245 } // namespace Internal
246
247 } // namespace Dali
248
249 #endif // __DALI_INTERNAL_MESH_ATTACHMENT_H__