Revert "License conversion from Flora to Apache 2.0"
[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 Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/event/modeling/entity-impl.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/public-api/geometry/mesh.h>
22 #include <dali/internal/event/modeling/model-data-impl.h>
23 #include <dali/internal/event/modeling/mesh-impl.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 namespace // unnamed namespace
32 {
33 static const float kBoundsDefault = 1e10f;
34 } // unnamed namespace
35
36
37
38 Entity::Entity()
39 : mName(""),
40   mModel(NULL),
41   mParent(NULL),
42   mLowerBounds( kBoundsDefault,  kBoundsDefault,  kBoundsDefault),
43   mUpperBounds(-kBoundsDefault, -kBoundsDefault, -kBoundsDefault),
44   mType(Dali::Entity::OBJECT)
45 {
46 }
47
48 EntityPtr Entity::New(const std::string name)
49 {
50   EntityPtr entity(new Entity());
51   entity->SetName(name);
52   return entity;
53 }
54
55 Entity::~Entity()
56 {
57   mChildren.clear();
58   mMeshes.clear();
59 }
60
61
62 Entity* Entity::Find(const std::string& name) const
63 {
64   Entity* entity = NULL;
65
66   if (mName == name)
67   {
68     entity = const_cast<Entity*>(this);
69   }
70   else if (mChildren.size() != 0)
71   {
72     for (EntityConstIter it = mChildren.begin(); it != mChildren.end() && !entity; ++it)
73     {
74       const Entity& child = GetImplementation(*it);
75       entity = child.Find(name);
76       if (entity)
77       {
78         break;
79       }
80     }
81   }
82
83   return entity;
84 }
85
86
87 void Entity::SetName(const char* name)
88 {
89   mName = name;
90 }
91
92 void Entity::SetName(const std::string& name)
93 {
94   mName = name;
95 }
96
97 const std::string& Entity::GetName() const
98 {
99   return mName;
100 }
101
102 const Vector3&  Entity::GetLowerBounds() const
103 {
104   return mLowerBounds;
105 }
106
107 void Entity::SetLowerBounds(const Vector3& bounds)
108 {
109   mLowerBounds = bounds;
110 }
111
112 const Vector3&  Entity::GetUpperBounds() const
113 {
114   return mUpperBounds;
115 }
116
117 void Entity::SetUpperBounds(const Vector3& bounds)
118 {
119   mUpperBounds = bounds;
120 }
121
122
123 void Entity::SetType(const Dali::Entity::EntityType type)
124 {
125   mType = type;
126 }
127
128 Dali::Entity::EntityType Entity::GetType() const
129 {
130   return mType;
131 }
132
133 void Entity::SetTransformMatrix(Matrix& matrix)
134 {
135   mTransformMatrix = matrix;
136 }
137
138 const Matrix& Entity::GetTransformMatrix() const
139 {
140   return mTransformMatrix;
141 }
142
143 bool Entity::HasChildren() const
144 {
145   return 0 != mChildren.size();
146 }
147
148 int Entity::NumberOfChildren() const
149 {
150   return mChildren.size();
151 }
152
153 const EntityContainer& Entity::GetChildren() const
154 {
155   return mChildren;
156 }
157
158 void Entity::Add(Entity& child)
159 {
160   if(this != child.mParent)
161   {
162     child.SetParent(this);
163     mChildren.push_back(Dali::Entity(&child));
164   }
165 }
166
167 void Entity::SetChildCapacity(unsigned int capacity)
168 {
169   mChildren.reserve(capacity);
170 }
171
172 void Entity::SetParent(Entity* parent)
173 {
174   if(parent)
175   {
176     DALI_ASSERT_ALWAYS( this != parent && "Cannot parent an entity to itself" );
177     DALI_ASSERT_ALWAYS(mParent == NULL && "Entity already has a parent" );
178     mParent = parent;
179   }
180 }
181
182 Entity* Entity::GetParent() const
183 {
184   return mParent;
185 }
186
187 bool Entity::HasMeshes() const
188 {
189   return 0 != mMeshes.size();
190 }
191
192 void Entity::SetMeshCapacity(unsigned int capacity)
193 {
194   mMeshes.reserve(capacity);
195 }
196
197 int Entity::NumberOfMeshes() const
198 {
199   return mMeshes.size();
200 }
201
202 void Entity::AddMeshIndex(unsigned int meshIdx)
203 {
204   mMeshes.push_back(meshIdx);
205 }
206
207 unsigned int Entity::GetMeshByIndex(unsigned int meshIndex) const
208 {
209   DALI_ASSERT_DEBUG( meshIndex < mMeshes.size());
210   return mMeshes[meshIndex];
211 }
212
213 const EntityMeshIndices& Entity::GetMeshes() const
214 {
215   return mMeshes;
216 }
217
218 void Entity::AddToBounds(Entity& child)
219 {
220   mLowerBounds.x = std::min(mLowerBounds.x, child.GetLowerBounds().x);
221   mLowerBounds.y = std::min(mLowerBounds.y, child.GetLowerBounds().y);
222   mLowerBounds.z = std::min(mLowerBounds.z, child.GetLowerBounds().z);
223   mUpperBounds.x = std::max(mUpperBounds.x, child.GetUpperBounds().x);
224   mUpperBounds.y = std::max(mUpperBounds.y, child.GetUpperBounds().y);
225   mUpperBounds.z = std::max(mUpperBounds.z, child.GetUpperBounds().z);
226 }
227
228 void Entity::AddToBounds( const Vector3& lowerBounds, const Vector3& upperBounds )
229 {
230   mLowerBounds.x = std::min(mLowerBounds.x, lowerBounds.x);
231   mLowerBounds.y = std::min(mLowerBounds.y, lowerBounds.y);
232   mLowerBounds.z = std::min(mLowerBounds.z, lowerBounds.z);
233   mUpperBounds.x = std::max(mUpperBounds.x, upperBounds.x);
234   mUpperBounds.y = std::max(mUpperBounds.y, upperBounds.y);
235   mUpperBounds.z = std::max(mUpperBounds.z, upperBounds.z);
236 }
237
238 } // namespace Internal
239 } // namespace Dali