| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- //
- // Copyright (c) 2008-2014 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Context.h"
- #include "DebugNew.h"
- namespace Urho3D
- {
- void RemoveNamedAttribute(HashMap<ShortStringHash, Vector<AttributeInfo> >& attributes, ShortStringHash objectType, const char* name)
- {
- HashMap<ShortStringHash, Vector<AttributeInfo> >::Iterator i = attributes.Find(objectType);
- if (i == attributes.End())
- return;
- Vector<AttributeInfo>& infos = i->second_;
- for (Vector<AttributeInfo>::Iterator j = infos.Begin(); j != infos.End(); ++j)
- {
- if (!j->name_.Compare(name, true))
- {
- infos.Erase(j);
- break;
- }
- }
- // If the vector became empty, erase the object type from the map
- if (infos.Empty())
- attributes.Erase(i);
- }
- Context::Context() :
- eventHandler_(0)
- {
- #ifdef ANDROID
- // Always reset the random seed on Android, as the Urho3D library might not be unloaded between runs
- SetRandomSeed(1);
- #endif
- }
- Context::~Context()
- {
- // Remove subsystems that use SDL in reverse order of construction, so that Graphics can shut down SDL last
- /// \todo Context should not need to know about subsystems
- RemoveSubsystem("Audio");
- RemoveSubsystem("UI");
- RemoveSubsystem("Input");
- RemoveSubsystem("Renderer");
- RemoveSubsystem("Graphics");
-
- subsystems_.Clear();
- factories_.Clear();
-
- // Delete allocated event data maps
- for (PODVector<VariantMap*>::Iterator i = eventDataMaps_.Begin(); i != eventDataMaps_.End(); ++i)
- delete *i;
- eventDataMaps_.Clear();
- }
- SharedPtr<Object> Context::CreateObject(ShortStringHash objectType)
- {
- HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
- if (i != factories_.End())
- return i->second_->CreateObject();
- else
- return SharedPtr<Object>();
- }
- void Context::RegisterFactory(ObjectFactory* factory)
- {
- if (!factory)
- return;
- factories_[factory->GetType()] = factory;
- }
- void Context::RegisterFactory(ObjectFactory* factory, const char* category)
- {
- if (!factory)
- return;
- RegisterFactory(factory);
- if (String::CStringLength(category))
- objectCategories_[category].Push(factory->GetType());
- }
- void Context::RegisterSubsystem(Object* object)
- {
- if (!object)
- return;
- subsystems_[object->GetType()] = object;
- }
- void Context::RemoveSubsystem(ShortStringHash objectType)
- {
- HashMap<ShortStringHash, SharedPtr<Object> >::Iterator i = subsystems_.Find(objectType);
- if (i != subsystems_.End())
- subsystems_.Erase(i);
- }
- void Context::RegisterAttribute(ShortStringHash objectType, const AttributeInfo& attr)
- {
- // None or Pointer types can not be supported
- if (attr.type_ == VAR_NONE || attr.type_ == VAR_PTR)
- return;
- attributes_[objectType].Push(attr);
- if (attr.mode_ & AM_NET)
- networkAttributes_[objectType].Push(attr);
- }
- void Context::RemoveAttribute(ShortStringHash objectType, const char* name)
- {
- RemoveNamedAttribute(attributes_, objectType, name);
- RemoveNamedAttribute(networkAttributes_, objectType, name);
- }
- void Context::UpdateAttributeDefaultValue(ShortStringHash objectType, const char* name, const Variant& defaultValue)
- {
- AttributeInfo* info = GetAttribute(objectType, name);
- if (info)
- info->defaultValue_ = defaultValue;
- }
- VariantMap& Context::GetEventDataMap()
- {
- unsigned nestingLevel = eventSenders_.Size();
- while (eventDataMaps_.Size() < nestingLevel + 1)
- eventDataMaps_.Push(new VariantMap());
-
- VariantMap& ret = *eventDataMaps_[nestingLevel];
- ret.Clear();
- return ret;
- }
- void Context::CopyBaseAttributes(ShortStringHash baseType, ShortStringHash derivedType)
- {
- const Vector<AttributeInfo>* baseAttributes = GetAttributes(baseType);
- if (baseAttributes)
- {
- for (unsigned i = 0; i < baseAttributes->Size(); ++i)
- {
- const AttributeInfo& attr = baseAttributes->At(i);
- attributes_[derivedType].Push(attr);
- if (attr.mode_ & AM_NET)
- networkAttributes_[derivedType].Push(attr);
- }
- }
- }
- Object* Context::GetSubsystem(ShortStringHash type) const
- {
- HashMap<ShortStringHash, SharedPtr<Object> >::ConstIterator i = subsystems_.Find(type);
- if (i != subsystems_.End())
- return i->second_;
- else
- return 0;
- }
- Object* Context::GetEventSender() const
- {
- if (!eventSenders_.Empty())
- return eventSenders_.Back();
- else
- return 0;
- }
- const String& Context::GetTypeName(ShortStringHash objectType) const
- {
- // Search factories to find the hash-to-name mapping
- HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
- return i != factories_.End() ? i->second_->GetTypeName() : String::EMPTY;
- }
- AttributeInfo* Context::GetAttribute(ShortStringHash objectType, const char* name)
- {
- HashMap<ShortStringHash, Vector<AttributeInfo> >::Iterator i = attributes_.Find(objectType);
- if (i == attributes_.End())
- return 0;
- Vector<AttributeInfo>& infos = i->second_;
- for (Vector<AttributeInfo>::Iterator j = infos.Begin(); j != infos.End(); ++j)
- {
- if (!j->name_.Compare(name, true))
- return &(*j);
- }
- return 0;
- }
- void Context::AddEventReceiver(Object* receiver, StringHash eventType)
- {
- eventReceivers_[eventType].Insert(receiver);
- }
- void Context::AddEventReceiver(Object* receiver, Object* sender, StringHash eventType)
- {
- specificEventReceivers_[sender][eventType].Insert(receiver);
- }
- void Context::RemoveEventSender(Object* sender)
- {
- HashMap<Object*, HashMap<StringHash, HashSet<Object*> > >::Iterator i = specificEventReceivers_.Find(sender);
- if (i != specificEventReceivers_.End())
- {
- for (HashMap<StringHash, HashSet<Object*> >::Iterator j = i->second_.Begin(); j != i->second_.End(); ++j)
- {
- for (HashSet<Object*>::Iterator k = j->second_.Begin(); k != j->second_.End(); ++k)
- (*k)->RemoveEventSender(sender);
- }
- specificEventReceivers_.Erase(i);
- }
- }
- void Context::RemoveEventReceiver(Object* receiver, StringHash eventType)
- {
- HashSet<Object*>* group = GetEventReceivers(eventType);
- if (group)
- group->Erase(receiver);
- }
- void Context::RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType)
- {
- HashSet<Object*>* group = GetEventReceivers(sender, eventType);
- if (group)
- group->Erase(receiver);
- }
- }
|