Context.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  27. {
  28. void RemoveNamedAttribute(HashMap<ShortStringHash, Vector<AttributeInfo> >& attributes, ShortStringHash objectType, const char* name)
  29. {
  30. HashMap<ShortStringHash, 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 (!String::Compare(j->name_, 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. }
  54. Context::~Context()
  55. {
  56. subsystems_.Clear();
  57. factories_.Clear();
  58. }
  59. SharedPtr<Object> Context::CreateObject(ShortStringHash objectType)
  60. {
  61. HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
  62. if (i != factories_.End())
  63. return i->second_->CreateObject();
  64. else
  65. return SharedPtr<Object>();
  66. }
  67. void Context::RegisterFactory(ObjectFactory* factory)
  68. {
  69. if (!factory)
  70. return;
  71. factories_[factory->GetType()] = factory;
  72. }
  73. void Context::RegisterSubsystem(Object* object)
  74. {
  75. if (!object)
  76. return;
  77. subsystems_[object->GetType()] = object;
  78. }
  79. void Context::RemoveSubsystem(ShortStringHash objectType)
  80. {
  81. HashMap<ShortStringHash, SharedPtr<Object> >::Iterator i = subsystems_.Find(objectType);
  82. if (i != subsystems_.End())
  83. subsystems_.Erase(i);
  84. }
  85. void Context::RegisterAttribute(ShortStringHash objectType, const AttributeInfo& attr)
  86. {
  87. // None or Pointer types can not be supported
  88. if (attr.type_ == VAR_NONE || attr.type_ == VAR_PTR)
  89. return;
  90. attributes_[objectType].Push(attr);
  91. if (attr.mode_ & AM_NET)
  92. networkAttributes_[objectType].Push(attr);
  93. }
  94. void Context::RemoveAttribute(ShortStringHash objectType, const char* name)
  95. {
  96. RemoveNamedAttribute(attributes_, objectType, name);
  97. RemoveNamedAttribute(networkAttributes_, objectType, name);
  98. }
  99. void Context::CopyBaseAttributes(ShortStringHash baseType, ShortStringHash derivedType)
  100. {
  101. const Vector<AttributeInfo>* baseAttributes = GetAttributes(baseType);
  102. if (baseAttributes)
  103. {
  104. for (unsigned i = 0; i < baseAttributes->Size(); ++i)
  105. {
  106. const AttributeInfo& attr = baseAttributes->At(i);
  107. attributes_[derivedType].Push(attr);
  108. if (attr.mode_ & AM_NET)
  109. networkAttributes_[derivedType].Push(attr);
  110. }
  111. }
  112. }
  113. Object* Context::GetSubsystem(ShortStringHash type) const
  114. {
  115. HashMap<ShortStringHash, SharedPtr<Object> >::ConstIterator i = subsystems_.Find(type);
  116. if (i != subsystems_.End())
  117. return i->second_;
  118. else
  119. return 0;
  120. }
  121. Object* Context::GetEventSender() const
  122. {
  123. if (!eventSenders_.Empty())
  124. return eventSenders_.Back();
  125. else
  126. return 0;
  127. }
  128. const String& Context::GetTypeName(ShortStringHash type) const
  129. {
  130. // Search factories to find the hash-to-name mapping
  131. HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(type);
  132. return i != factories_.End() ? i->second_->GetTypeName() : String::EMPTY;
  133. }
  134. void Context::AddEventReceiver(Object* receiver, StringHash eventType)
  135. {
  136. eventReceivers_[eventType].Insert(receiver);
  137. }
  138. void Context::AddEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  139. {
  140. specificEventReceivers_[sender][eventType].Insert(receiver);
  141. }
  142. void Context::RemoveEventSender(Object* sender)
  143. {
  144. HashMap<Object*, HashMap<StringHash, HashSet<Object*> > >::Iterator i = specificEventReceivers_.Find(sender);
  145. if (i != specificEventReceivers_.End())
  146. {
  147. for (HashMap<StringHash, HashSet<Object*> >::Iterator j = i->second_.Begin(); j != i->second_.End(); ++j)
  148. {
  149. for (HashSet<Object*>::Iterator k = j->second_.Begin(); k != j->second_.End(); ++k)
  150. (*k)->RemoveEventSender(sender);
  151. }
  152. specificEventReceivers_.Erase(i);
  153. }
  154. }
  155. void Context::RemoveEventReceiver(Object* receiver, StringHash eventType)
  156. {
  157. HashSet<Object*>* group = GetEventReceivers(eventType);
  158. if (group)
  159. group->Erase(receiver);
  160. }
  161. void Context::RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  162. {
  163. HashSet<Object*>* group = GetEventReceivers(sender, eventType);
  164. if (group)
  165. group->Erase(receiver);
  166. }
  167. void Context::EndSendEvent()
  168. {
  169. eventSenders_.Pop();
  170. }
  171. }