[5640] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[7401] | 29 | /** |
---|
| 30 | @file |
---|
[10513] | 31 | @brief Implementation of orxonox::ScopeManager. |
---|
[7401] | 32 | */ |
---|
| 33 | |
---|
[10462] | 34 | #include "ScopeManager.h" |
---|
| 35 | |
---|
[5640] | 36 | #include "Scope.h" |
---|
| 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
[10462] | 40 | /* static */ ScopeManager& ScopeManager::getInstance() |
---|
[10412] | 41 | { |
---|
[10462] | 42 | static ScopeManager instance; |
---|
| 43 | return instance; |
---|
[10412] | 44 | } |
---|
[10462] | 45 | |
---|
[10463] | 46 | void ScopeManager::addScope(ScopeID::Value scope) |
---|
| 47 | { |
---|
| 48 | this->activeScopes_.insert(scope); |
---|
[10513] | 49 | this->activateListenersForScope(scope); |
---|
[10463] | 50 | } |
---|
| 51 | |
---|
| 52 | void ScopeManager::removeScope(ScopeID::Value scope) |
---|
| 53 | { |
---|
| 54 | this->activeScopes_.erase(scope); |
---|
[10513] | 55 | this->deactivateListenersForScope(scope); |
---|
[10463] | 56 | } |
---|
| 57 | |
---|
| 58 | bool ScopeManager::isActive(ScopeID::Value scope) |
---|
| 59 | { |
---|
| 60 | return this->activeScopes_.find(scope) != this->activeScopes_.end(); |
---|
| 61 | } |
---|
| 62 | |
---|
[10514] | 63 | void ScopeManager::addListener(ScopeListener* listener, ScopeID::Value scope) |
---|
[10412] | 64 | { |
---|
[10514] | 65 | this->listeners_[scope].insert(listener); |
---|
[10412] | 66 | } |
---|
[10462] | 67 | |
---|
[10514] | 68 | void ScopeManager::removeListener(ScopeListener* listener, ScopeID::Value scope) |
---|
[10462] | 69 | { |
---|
[10514] | 70 | this->listeners_[scope].erase(listener); |
---|
[10462] | 71 | } |
---|
[10513] | 72 | |
---|
| 73 | void ScopeManager::activateListenersForScope(ScopeID::Value scope) |
---|
| 74 | { |
---|
[10516] | 75 | for (std::set<ScopeListener*>::iterator it = this->listeners_[scope].begin(); it != this->listeners_[scope].end(); ++it) |
---|
[10513] | 76 | this->activateListener(*it); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void ScopeManager::deactivateListenersForScope(ScopeID::Value scope) |
---|
| 80 | { |
---|
[10516] | 81 | for (std::set<ScopeListener*>::iterator it = this->listeners_[scope].begin(); it != this->listeners_[scope].end(); ++it) |
---|
[10517] | 82 | if ((*it)->bActivated_) |
---|
| 83 | this->deactivateListener(*it); |
---|
[10513] | 84 | } |
---|
| 85 | |
---|
| 86 | void ScopeManager::activateListener(ScopeListener* listener) |
---|
| 87 | { |
---|
| 88 | listener->activated(); |
---|
| 89 | listener->bActivated_ = true; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | void ScopeManager::deactivateListener(ScopeListener* listener) |
---|
| 93 | { |
---|
[10517] | 94 | try |
---|
| 95 | { listener->deactivated(); } |
---|
| 96 | catch (...) |
---|
| 97 | { orxout(internal_warning) << "ScopeListener::deactivated() failed! This MUST NOT happen, fix it!" << endl; } |
---|
| 98 | listener->bActivated_ = false; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | void ScopeManager::updateListeners() |
---|
| 102 | { |
---|
| 103 | std::map<ScopeID::Value, std::set<ScopeListener*> >::iterator it1; |
---|
| 104 | for (it1 = this->listeners_.begin(); it1 != this->listeners_.end(); ++it1) |
---|
[10515] | 105 | { |
---|
[10517] | 106 | const ScopeID::Value& scope = it1->first; |
---|
| 107 | const std::set<ScopeListener*>& listeners = it1->second; |
---|
| 108 | |
---|
| 109 | bool scopeIsActive = this->isActive(scope); |
---|
| 110 | for (std::set<ScopeListener*>::const_iterator it2 = listeners.begin(); it2 != listeners.end(); ++it2) |
---|
| 111 | { |
---|
| 112 | ScopeListener* listener = (*it2); |
---|
| 113 | |
---|
| 114 | if (scopeIsActive && !listener->bActivated_) |
---|
| 115 | this->activateListener(listener); |
---|
| 116 | else if (!scopeIsActive && listener->bActivated_) |
---|
| 117 | this->deactivateListener(listener); |
---|
| 118 | } |
---|
[10515] | 119 | } |
---|
[10513] | 120 | } |
---|
[5640] | 121 | } |
---|