Context.cpp 7.8 KB

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