Context.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "DebugNew.h"
  25. namespace Urho3D
  26. {
  27. void RemoveNamedAttribute(HashMap<ShortStringHash, Vector<AttributeInfo> >& attributes, ShortStringHash objectType, const char* name)
  28. {
  29. HashMap<ShortStringHash, Vector<AttributeInfo> >::Iterator i = attributes.Find(objectType);
  30. if (i == attributes.End())
  31. return;
  32. Vector<AttributeInfo>& infos = i->second_;
  33. for (Vector<AttributeInfo>::Iterator j = infos.Begin(); j != infos.End(); ++j)
  34. {
  35. if (!j->name_.Compare(name, true))
  36. {
  37. infos.Erase(j);
  38. break;
  39. }
  40. }
  41. // If the vector became empty, erase the object type from the map
  42. if (infos.Empty())
  43. attributes.Erase(i);
  44. }
  45. Context::Context() :
  46. eventHandler_(0)
  47. {
  48. #ifdef ANDROID
  49. // Always reset the random seed on Android, as the Urho3D library might not be unloaded between runs
  50. SetRandomSeed(1);
  51. #endif
  52. }
  53. Context::~Context()
  54. {
  55. subsystems_.Clear();
  56. factories_.Clear();
  57. }
  58. SharedPtr<Object> Context::CreateObject(ShortStringHash objectType)
  59. {
  60. HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
  61. if (i != factories_.End())
  62. return i->second_->CreateObject();
  63. else
  64. return SharedPtr<Object>();
  65. }
  66. void Context::RegisterFactory(ObjectFactory* factory)
  67. {
  68. if (!factory)
  69. return;
  70. factories_[factory->GetType()] = factory;
  71. }
  72. void Context::RegisterFactory(ObjectFactory* factory, const char* category)
  73. {
  74. if (!factory)
  75. return;
  76. RegisterFactory(factory);
  77. if (String::CStringLength(category))
  78. objectCategories_[category].Push(factory->GetType());
  79. }
  80. void Context::RegisterSubsystem(Object* object)
  81. {
  82. if (!object)
  83. return;
  84. subsystems_[object->GetType()] = object;
  85. }
  86. void Context::RemoveSubsystem(ShortStringHash objectType)
  87. {
  88. HashMap<ShortStringHash, SharedPtr<Object> >::Iterator i = subsystems_.Find(objectType);
  89. if (i != subsystems_.End())
  90. subsystems_.Erase(i);
  91. }
  92. void Context::RegisterAttribute(ShortStringHash objectType, const AttributeInfo& attr)
  93. {
  94. // None or Pointer types can not be supported
  95. if (attr.type_ == VAR_NONE || attr.type_ == VAR_PTR)
  96. return;
  97. attributes_[objectType].Push(attr);
  98. if (attr.mode_ & AM_NET)
  99. networkAttributes_[objectType].Push(attr);
  100. }
  101. void Context::RemoveAttribute(ShortStringHash objectType, const char* name)
  102. {
  103. RemoveNamedAttribute(attributes_, objectType, name);
  104. RemoveNamedAttribute(networkAttributes_, objectType, name);
  105. }
  106. void Context::UpdateAttributeDefaultValue(ShortStringHash objectType, const char* name, const Variant& defaultValue)
  107. {
  108. AttributeInfo* info = GetAttribute(objectType, name);
  109. if (info)
  110. info->defaultValue_ = defaultValue;
  111. }
  112. void Context::CopyBaseAttributes(ShortStringHash baseType, ShortStringHash derivedType)
  113. {
  114. const Vector<AttributeInfo>* baseAttributes = GetAttributes(baseType);
  115. if (baseAttributes)
  116. {
  117. for (unsigned i = 0; i < baseAttributes->Size(); ++i)
  118. {
  119. const AttributeInfo& attr = baseAttributes->At(i);
  120. attributes_[derivedType].Push(attr);
  121. if (attr.mode_ & AM_NET)
  122. networkAttributes_[derivedType].Push(attr);
  123. }
  124. }
  125. }
  126. Object* Context::GetSubsystem(ShortStringHash type) const
  127. {
  128. HashMap<ShortStringHash, SharedPtr<Object> >::ConstIterator i = subsystems_.Find(type);
  129. if (i != subsystems_.End())
  130. return i->second_;
  131. else
  132. return 0;
  133. }
  134. Object* Context::GetEventSender() const
  135. {
  136. if (!eventSenders_.Empty())
  137. return eventSenders_.Back();
  138. else
  139. return 0;
  140. }
  141. const String& Context::GetTypeName(ShortStringHash type) const
  142. {
  143. // Search factories to find the hash-to-name mapping
  144. HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(type);
  145. return i != factories_.End() ? i->second_->GetTypeName() : String::EMPTY;
  146. }
  147. AttributeInfo* Context::GetAttribute(ShortStringHash objectType, const char* name)
  148. {
  149. HashMap<ShortStringHash, Vector<AttributeInfo> >::Iterator i = attributes_.Find(objectType);
  150. if (i == attributes_.End())
  151. return 0;
  152. Vector<AttributeInfo>& infos = i->second_;
  153. for (Vector<AttributeInfo>::Iterator j = infos.Begin(); j != infos.End(); ++j)
  154. {
  155. if (!j->name_.Compare(name, true))
  156. return &(*j);
  157. }
  158. return 0;
  159. }
  160. void Context::AddEventReceiver(Object* receiver, StringHash eventType)
  161. {
  162. eventReceivers_[eventType].Insert(receiver);
  163. }
  164. void Context::AddEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  165. {
  166. specificEventReceivers_[sender][eventType].Insert(receiver);
  167. }
  168. void Context::RemoveEventSender(Object* sender)
  169. {
  170. HashMap<Object*, HashMap<StringHash, HashSet<Object*> > >::Iterator i = specificEventReceivers_.Find(sender);
  171. if (i != specificEventReceivers_.End())
  172. {
  173. for (HashMap<StringHash, HashSet<Object*> >::Iterator j = i->second_.Begin(); j != i->second_.End(); ++j)
  174. {
  175. for (HashSet<Object*>::Iterator k = j->second_.Begin(); k != j->second_.End(); ++k)
  176. (*k)->RemoveEventSender(sender);
  177. }
  178. specificEventReceivers_.Erase(i);
  179. }
  180. }
  181. void Context::RemoveEventReceiver(Object* receiver, StringHash eventType)
  182. {
  183. HashSet<Object*>* group = GetEventReceivers(eventType);
  184. if (group)
  185. group->Erase(receiver);
  186. }
  187. void Context::RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  188. {
  189. HashSet<Object*>* group = GetEventReceivers(sender, eventType);
  190. if (group)
  191. group->Erase(receiver);
  192. }
  193. void Context::EndSendEvent()
  194. {
  195. eventSenders_.Pop();
  196. }
  197. }