Context.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "DebugNew.h"
  26. void RemoveNamedAttribute(HashMap<ShortStringHash, Vector<AttributeInfo> >& attributes, ShortStringHash objectType, const char* name)
  27. {
  28. HashMap<ShortStringHash, Vector<AttributeInfo> >::Iterator i = attributes.Find(objectType);
  29. if (i == attributes.End())
  30. return;
  31. Vector<AttributeInfo>& infos = i->second_;
  32. for (Vector<AttributeInfo>::Iterator j = infos.Begin(); j != infos.End(); ++j)
  33. {
  34. if (!String::Compare(j->name_, name, true))
  35. {
  36. infos.Erase(j);
  37. break;
  38. }
  39. }
  40. // If the vector became empty, erase the object type from the map
  41. if (infos.Empty())
  42. attributes.Erase(i);
  43. }
  44. Context::Context() :
  45. eventHandler_(0)
  46. {
  47. #ifdef ANDROID
  48. // Always reset the random seed on Android, as the Urho3D library might not be unloaded between runs
  49. SetRandomSeed(1);
  50. #endif
  51. }
  52. Context::~Context()
  53. {
  54. subsystems_.Clear();
  55. factories_.Clear();
  56. }
  57. SharedPtr<Object> Context::CreateObject(ShortStringHash objectType)
  58. {
  59. HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
  60. if (i != factories_.End())
  61. return i->second_->CreateObject();
  62. else
  63. return SharedPtr<Object>();
  64. }
  65. void Context::RegisterFactory(ObjectFactory* factory)
  66. {
  67. if (!factory)
  68. return;
  69. factories_[factory->GetType()] = factory;
  70. }
  71. void Context::RegisterSubsystem(Object* object)
  72. {
  73. if (!object)
  74. return;
  75. subsystems_[object->GetType()] = object;
  76. }
  77. void Context::RemoveSubsystem(ShortStringHash objectType)
  78. {
  79. HashMap<ShortStringHash, SharedPtr<Object> >::Iterator i = subsystems_.Find(objectType);
  80. if (i != subsystems_.End())
  81. subsystems_.Erase(i);
  82. }
  83. void Context::RegisterAttribute(ShortStringHash objectType, const AttributeInfo& attr)
  84. {
  85. // None or Pointer types can not be supported
  86. if (attr.type_ == VAR_NONE || attr.type_ == VAR_PTR)
  87. return;
  88. attributes_[objectType].Push(attr);
  89. if (attr.mode_ & AM_NET)
  90. networkAttributes_[objectType].Push(attr);
  91. }
  92. void Context::RemoveAttribute(ShortStringHash objectType, const char* name)
  93. {
  94. RemoveNamedAttribute(attributes_, objectType, name);
  95. RemoveNamedAttribute(networkAttributes_, objectType, name);
  96. }
  97. void Context::CopyBaseAttributes(ShortStringHash baseType, ShortStringHash derivedType)
  98. {
  99. const Vector<AttributeInfo>* baseAttributes = GetAttributes(baseType);
  100. if (baseAttributes)
  101. {
  102. for (unsigned i = 0; i < baseAttributes->Size(); ++i)
  103. {
  104. const AttributeInfo& attr = baseAttributes->At(i);
  105. attributes_[derivedType].Push(attr);
  106. if (attr.mode_ & AM_NET)
  107. networkAttributes_[derivedType].Push(attr);
  108. }
  109. }
  110. }
  111. Object* Context::GetSubsystem(ShortStringHash type) const
  112. {
  113. HashMap<ShortStringHash, SharedPtr<Object> >::ConstIterator i = subsystems_.Find(type);
  114. if (i != subsystems_.End())
  115. return i->second_;
  116. else
  117. return 0;
  118. }
  119. Object* Context::GetEventSender() const
  120. {
  121. if (!eventSenders_.Empty())
  122. return eventSenders_.Back();
  123. else
  124. return 0;
  125. }
  126. const String& Context::GetTypeName(ShortStringHash type) const
  127. {
  128. // Search factories to find the hash-to-name mapping
  129. HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(type);
  130. return i != factories_.End() ? i->second_->GetTypeName() : String::EMPTY;
  131. }
  132. void Context::AddEventReceiver(Object* receiver, StringHash eventType)
  133. {
  134. eventReceivers_[eventType].Insert(receiver);
  135. }
  136. void Context::AddEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  137. {
  138. specificEventReceivers_[sender][eventType].Insert(receiver);
  139. }
  140. void Context::RemoveEventSender(Object* sender)
  141. {
  142. HashMap<Object*, HashMap<StringHash, HashSet<Object*> > >::Iterator i = specificEventReceivers_.Find(sender);
  143. if (i != specificEventReceivers_.End())
  144. {
  145. for (HashMap<StringHash, HashSet<Object*> >::Iterator j = i->second_.Begin(); j != i->second_.End(); ++j)
  146. {
  147. for (HashSet<Object*>::Iterator k = j->second_.Begin(); k != j->second_.End(); ++k)
  148. (*k)->RemoveEventSender(sender);
  149. }
  150. specificEventReceivers_.Erase(i);
  151. }
  152. }
  153. void Context::RemoveEventReceiver(Object* receiver, StringHash eventType)
  154. {
  155. HashSet<Object*>* group = GetEventReceivers(eventType);
  156. if (group)
  157. group->Erase(receiver);
  158. }
  159. void Context::RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  160. {
  161. HashSet<Object*>* group = GetEventReceivers(sender, eventType);
  162. if (group)
  163. group->Erase(receiver);
  164. }
  165. void Context::EndSendEvent()
  166. {
  167. eventSenders_.Pop();
  168. }