Context.cpp 7.9 KB

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