Context.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // Copyright (c) 2008-2014 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. // Remove subsystems that use SDL in reverse order of construction, so that Graphics can shut down SDL last
  56. /// \todo Context should not need to know about subsystems
  57. RemoveSubsystem("Audio");
  58. RemoveSubsystem("UI");
  59. RemoveSubsystem("Input");
  60. RemoveSubsystem("Renderer");
  61. RemoveSubsystem("Graphics");
  62. subsystems_.Clear();
  63. factories_.Clear();
  64. // Delete allocated event data maps
  65. for (PODVector<VariantMap*>::Iterator i = eventDataMaps_.Begin(); i != eventDataMaps_.End(); ++i)
  66. delete *i;
  67. eventDataMaps_.Clear();
  68. }
  69. SharedPtr<Object> Context::CreateObject(ShortStringHash objectType)
  70. {
  71. HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
  72. if (i != factories_.End())
  73. return i->second_->CreateObject();
  74. else
  75. return SharedPtr<Object>();
  76. }
  77. void Context::RegisterFactory(ObjectFactory* factory)
  78. {
  79. if (!factory)
  80. return;
  81. factories_[factory->GetType()] = factory;
  82. }
  83. void Context::RegisterFactory(ObjectFactory* factory, const char* category)
  84. {
  85. if (!factory)
  86. return;
  87. RegisterFactory(factory);
  88. if (String::CStringLength(category))
  89. objectCategories_[category].Push(factory->GetType());
  90. }
  91. void Context::RegisterSubsystem(Object* object)
  92. {
  93. if (!object)
  94. return;
  95. subsystems_[object->GetType()] = object;
  96. }
  97. void Context::RemoveSubsystem(ShortStringHash objectType)
  98. {
  99. HashMap<ShortStringHash, SharedPtr<Object> >::Iterator i = subsystems_.Find(objectType);
  100. if (i != subsystems_.End())
  101. subsystems_.Erase(i);
  102. }
  103. void Context::RegisterAttribute(ShortStringHash objectType, const AttributeInfo& attr)
  104. {
  105. // None or Pointer types can not be supported
  106. if (attr.type_ == VAR_NONE || attr.type_ == VAR_PTR)
  107. return;
  108. attributes_[objectType].Push(attr);
  109. if (attr.mode_ & AM_NET)
  110. networkAttributes_[objectType].Push(attr);
  111. }
  112. void Context::RemoveAttribute(ShortStringHash objectType, const char* name)
  113. {
  114. RemoveNamedAttribute(attributes_, objectType, name);
  115. RemoveNamedAttribute(networkAttributes_, objectType, name);
  116. }
  117. void Context::UpdateAttributeDefaultValue(ShortStringHash objectType, const char* name, const Variant& defaultValue)
  118. {
  119. AttributeInfo* info = GetAttribute(objectType, name);
  120. if (info)
  121. info->defaultValue_ = defaultValue;
  122. }
  123. VariantMap& Context::GetEventDataMap()
  124. {
  125. unsigned nestingLevel = eventSenders_.Size();
  126. while (eventDataMaps_.Size() < nestingLevel + 1)
  127. eventDataMaps_.Push(new VariantMap());
  128. VariantMap& ret = *eventDataMaps_[nestingLevel];
  129. ret.Clear();
  130. return ret;
  131. }
  132. void Context::CopyBaseAttributes(ShortStringHash baseType, ShortStringHash derivedType)
  133. {
  134. const Vector<AttributeInfo>* baseAttributes = GetAttributes(baseType);
  135. if (baseAttributes)
  136. {
  137. for (unsigned i = 0; i < baseAttributes->Size(); ++i)
  138. {
  139. const AttributeInfo& attr = baseAttributes->At(i);
  140. attributes_[derivedType].Push(attr);
  141. if (attr.mode_ & AM_NET)
  142. networkAttributes_[derivedType].Push(attr);
  143. }
  144. }
  145. }
  146. Object* Context::GetSubsystem(ShortStringHash type) const
  147. {
  148. HashMap<ShortStringHash, SharedPtr<Object> >::ConstIterator i = subsystems_.Find(type);
  149. if (i != subsystems_.End())
  150. return i->second_;
  151. else
  152. return 0;
  153. }
  154. Object* Context::GetEventSender() const
  155. {
  156. if (!eventSenders_.Empty())
  157. return eventSenders_.Back();
  158. else
  159. return 0;
  160. }
  161. const String& Context::GetTypeName(ShortStringHash objectType) const
  162. {
  163. // Search factories to find the hash-to-name mapping
  164. HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
  165. return i != factories_.End() ? i->second_->GetTypeName() : String::EMPTY;
  166. }
  167. AttributeInfo* Context::GetAttribute(ShortStringHash objectType, const char* name)
  168. {
  169. HashMap<ShortStringHash, Vector<AttributeInfo> >::Iterator i = attributes_.Find(objectType);
  170. if (i == attributes_.End())
  171. return 0;
  172. Vector<AttributeInfo>& infos = i->second_;
  173. for (Vector<AttributeInfo>::Iterator j = infos.Begin(); j != infos.End(); ++j)
  174. {
  175. if (!j->name_.Compare(name, true))
  176. return &(*j);
  177. }
  178. return 0;
  179. }
  180. void Context::AddEventReceiver(Object* receiver, StringHash eventType)
  181. {
  182. eventReceivers_[eventType].Insert(receiver);
  183. }
  184. void Context::AddEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  185. {
  186. specificEventReceivers_[sender][eventType].Insert(receiver);
  187. }
  188. void Context::RemoveEventSender(Object* sender)
  189. {
  190. HashMap<Object*, HashMap<StringHash, HashSet<Object*> > >::Iterator i = specificEventReceivers_.Find(sender);
  191. if (i != specificEventReceivers_.End())
  192. {
  193. for (HashMap<StringHash, HashSet<Object*> >::Iterator j = i->second_.Begin(); j != i->second_.End(); ++j)
  194. {
  195. for (HashSet<Object*>::Iterator k = j->second_.Begin(); k != j->second_.End(); ++k)
  196. (*k)->RemoveEventSender(sender);
  197. }
  198. specificEventReceivers_.Erase(i);
  199. }
  200. }
  201. void Context::RemoveEventReceiver(Object* receiver, StringHash eventType)
  202. {
  203. HashSet<Object*>* group = GetEventReceivers(eventType);
  204. if (group)
  205. group->Erase(receiver);
  206. }
  207. void Context::RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  208. {
  209. HashSet<Object*>* group = GetEventReceivers(sender, eventType);
  210. if (group)
  211. group->Erase(receiver);
  212. }
  213. }