Context.cpp 8.3 KB

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