Merge "Combine Internal::ProxyObject & Internal::Object" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / modeling / entity-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/modeling/entity-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/geometry/mesh.h>
23 #include <dali/internal/event/modeling/model-data-impl.h>
24 #include <dali/internal/event/modeling/mesh-impl.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 namespace // unnamed namespace
33 {
34 static const float kBoundsDefault = 1e10f;
35 } // unnamed namespace
36
37
38
39 Entity::Entity()
40 : mName(""),
41   mModel(NULL),
42   mParent(NULL),
43   mLowerBounds( kBoundsDefault,  kBoundsDefault,  kBoundsDefault),
44   mUpperBounds(-kBoundsDefault, -kBoundsDefault, -kBoundsDefault),
45   mType(Dali::Entity::OBJECT)
46 {
47 }
48
49 EntityPtr Entity::New(const std::string name)
50 {
51   EntityPtr entity(new Entity());
52   entity->SetName(name);
53   return entity;
54 }
55
56 Entity::~Entity()
57 {
58   mChildren.clear();
59   mMeshes.clear();
60 }
61
62
63 Entity* Entity::Find(const std::string& name) const
64 {
65   Entity* entity = NULL;
66
67   if (mName == name)
68   {
69     entity = const_cast<Entity*>(this);
70   }
71   else if (mChildren.size() != 0)
72   {
73     for (EntityConstIter it = mChildren.begin(); it != mChildren.end() && !entity; ++it)
74     {
75       const Entity& child = GetImplementation(*it);
76       entity = child.Find(name);
77       if (entity)
78       {
79         break;
80       }
81     }
82   }
83
84   return entity;
85 }
86
87
88 void Entity::SetName(const char* name)
89 {
90   mName = name;
91 }
92
93 void Entity::SetName(const std::string& name)
94 {
95   mName = name;
96 }
97
98 const std::string& Entity::GetName() const
99 {
100   return mName;
101 }
102
103 const Vector3&  Entity::GetLowerBounds() const
104 {
105   return mLowerBounds;
106 }
107
108 void Entity::SetLowerBounds(const Vector3& bounds)
109 {
110   mLowerBounds = bounds;
111 }
112
113 const Vector3&  Entity::GetUpperBounds() const
114 {
115   return mUpperBounds;
116 }
117
118 void Entity::SetUpperBounds(const Vector3& bounds)
119 {
120   mUpperBounds = bounds;
121 }
122
123
124 void Entity::SetType(const Dali::Entity::EntityType type)
125 {
126   mType = type;
127 }
128
129 Dali::Entity::EntityType Entity::GetType() const
130 {
131   return mType;
132 }
133
134 void Entity::SetTransformMatrix(Matrix& matrix)
135 {
136   mTransformMatrix = matrix;
137 }
138
139 const Matrix& Entity::GetTransformMatrix() const
140 {
141   return mTransformMatrix;
142 }
143
144 bool Entity::HasChildren() const
145 {
146   return 0 != mChildren.size();
147 }
148
149 int Entity::NumberOfChildren() const
150 {
151   return mChildren.size();
152 }
153
154 const EntityContainer& Entity::GetChildren() const
155 {
156   return mChildren;
157 }
158
159 void Entity::Add(Entity& child)
160 {
161   if(this != child.mParent)
162   {
163     child.SetParent(this);
164     mChildren.push_back(Dali::Entity(&child));
165   }
166 }
167
168 void Entity::SetChildCapacity(unsigned int capacity)
169 {
170   mChildren.reserve(capacity);
171 }
172
173 void Entity::SetParent(Entity* parent)
174 {
175   if(parent)
176   {
177     DALI_ASSERT_ALWAYS( this != parent && "Cannot parent an entity to itself" );
178     DALI_ASSERT_ALWAYS(mParent == NULL && "Entity already has a parent" );
179     mParent = parent;
180   }
181 }
182
183 Entity* Entity::GetParent() const
184 {
185   return mParent;
186 }
187
188 bool Entity::HasMeshes() const
189 {
190   return 0 != mMeshes.size();
191 }
192
193 void Entity::SetMeshCapacity(unsigned int capacity)
194 {
195   mMeshes.reserve(capacity);
196 }
197
198 int Entity::NumberOfMeshes() const
199 {
200   return mMeshes.size();
201 }
202
203 void Entity::AddMeshIndex(unsigned int meshIdx)
204 {
205   mMeshes.push_back(meshIdx);
206 }
207
208 unsigned int Entity::GetMeshByIndex(unsigned int meshIndex) const
209 {
210   DALI_ASSERT_DEBUG( meshIndex < mMeshes.size());
211   return mMeshes[meshIndex];
212 }
213
214 const EntityMeshIndices& Entity::GetMeshes() const
215 {
216   return mMeshes;
217 }
218
219 void Entity::AddToBounds(Entity& child)
220 {
221   mLowerBounds.x = std::min(mLowerBounds.x, child.GetLowerBounds().x);
222   mLowerBounds.y = std::min(mLowerBounds.y, child.GetLowerBounds().y);
223   mLowerBounds.z = std::min(mLowerBounds.z, child.GetLowerBounds().z);
224   mUpperBounds.x = std::max(mUpperBounds.x, child.GetUpperBounds().x);
225   mUpperBounds.y = std::max(mUpperBounds.y, child.GetUpperBounds().y);
226   mUpperBounds.z = std::max(mUpperBounds.z, child.GetUpperBounds().z);
227 }
228
229 void Entity::AddToBounds( const Vector3& lowerBounds, const Vector3& upperBounds )
230 {
231   mLowerBounds.x = std::min(mLowerBounds.x, lowerBounds.x);
232   mLowerBounds.y = std::min(mLowerBounds.y, lowerBounds.y);
233   mLowerBounds.z = std::min(mLowerBounds.z, lowerBounds.z);
234   mUpperBounds.x = std::max(mUpperBounds.x, upperBounds.x);
235   mUpperBounds.y = std::max(mUpperBounds.y, upperBounds.y);
236   mUpperBounds.z = std::max(mUpperBounds.z, upperBounds.z);
237 }
238
239 } // namespace Internal
240 } // namespace Dali