Context.cpp 7.8 KB

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