[1505] | 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: |
---|
[1604] | 23 | * Reto Grieder |
---|
[1505] | 24 | * Co-authors: |
---|
[1604] | 25 | * ... |
---|
[1505] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[1623] | 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Definition of the OverlayGroup class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[1505] | 34 | #include "OrxonoxStableHeaders.h" |
---|
[1601] | 35 | #include "OverlayGroup.h" |
---|
[1505] | 36 | |
---|
[1747] | 37 | #include "util/Debug.h" |
---|
[1505] | 38 | #include "core/ConsoleCommand.h" |
---|
[1588] | 39 | #include "core/CoreIncludes.h" |
---|
[1747] | 40 | #include "core/Iterator.h" |
---|
[1616] | 41 | #include "core/XMLPort.h" |
---|
[1604] | 42 | #include "OrxonoxOverlay.h" |
---|
[1505] | 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
[1615] | 46 | CreateFactory(OverlayGroup); |
---|
[1588] | 47 | |
---|
[1747] | 48 | SetConsoleCommand(OverlayGroup, toggleVisibility, false).accessLevel(AccessLevel::User); |
---|
| 49 | SetConsoleCommand(OverlayGroup, scaleGroup, false).accessLevel(AccessLevel::User); |
---|
| 50 | SetConsoleCommand(OverlayGroup, scrollGroup, false).accessLevel(AccessLevel::User); |
---|
[1505] | 51 | |
---|
[2087] | 52 | OverlayGroup::OverlayGroup(BaseObject* creator) |
---|
| 53 | : BaseObject(creator) |
---|
[1615] | 54 | { |
---|
| 55 | RegisterObject(OverlayGroup); |
---|
[2087] | 56 | |
---|
[2662] | 57 | this->owner_ = 0; |
---|
| 58 | |
---|
[2087] | 59 | setScale(Vector2(1.0, 1.0)); |
---|
| 60 | setScroll(Vector2(0.0, 0.0)); |
---|
[1615] | 61 | } |
---|
[1505] | 62 | |
---|
[2087] | 63 | OverlayGroup::~OverlayGroup() |
---|
| 64 | { |
---|
[2908] | 65 | for (std::map<std::string, OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) |
---|
| 66 | delete it->second; |
---|
[2087] | 67 | } |
---|
| 68 | |
---|
[1623] | 69 | /** |
---|
| 70 | @brief |
---|
| 71 | Loads the group and all its children OrxonoxOverlays. |
---|
| 72 | @copydoc |
---|
| 73 | BaseObject::XMLPort() |
---|
| 74 | */ |
---|
[1615] | 75 | void OverlayGroup::XMLPort(Element& xmlElement, XMLPort::Mode mode) |
---|
| 76 | { |
---|
[1747] | 77 | SUPER(OverlayGroup, XMLPort, xmlElement, mode); |
---|
[1588] | 78 | |
---|
[2087] | 79 | XMLPortParam(OverlayGroup, "scale", setScale, getScale, xmlElement, mode); |
---|
| 80 | XMLPortParam(OverlayGroup, "scroll", setScroll, getScroll, xmlElement, mode); |
---|
[1623] | 81 | // loads all the child elements |
---|
[1854] | 82 | XMLPortObject(OverlayGroup, OrxonoxOverlay, "", addElement, getElement, xmlElement, mode); |
---|
[1615] | 83 | } |
---|
[1505] | 84 | |
---|
[2908] | 85 | //! Scales every element in the map. |
---|
[1615] | 86 | void OverlayGroup::setScale(const Vector2& scale) |
---|
| 87 | { |
---|
[2908] | 88 | for (std::map<std::string, OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) |
---|
| 89 | (*it).second->scale(scale / this->scale_); |
---|
[1615] | 90 | this->scale_ = scale; |
---|
| 91 | } |
---|
[1588] | 92 | |
---|
[2908] | 93 | //! Scrolls every element in the map. |
---|
[1615] | 94 | void OverlayGroup::setScroll(const Vector2& scroll) |
---|
| 95 | { |
---|
[2908] | 96 | for (std::map<std::string, OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) |
---|
| 97 | (*it).second->scroll(scroll - this->scroll_); |
---|
[1615] | 98 | this->scroll_ = scroll; |
---|
| 99 | } |
---|
[1505] | 100 | |
---|
[1623] | 101 | /** |
---|
| 102 | @brief |
---|
[2908] | 103 | Adds an element to the map (used when loading with XMLPort). |
---|
[1623] | 104 | @remarks |
---|
| 105 | The names of the OrxonoxOverlays have to be unique! |
---|
| 106 | */ |
---|
[1615] | 107 | void OverlayGroup::addElement(OrxonoxOverlay* element) |
---|
[1564] | 108 | { |
---|
[2908] | 109 | this->insertElement(element, element->getName()); |
---|
[2779] | 110 | } |
---|
| 111 | |
---|
[2908] | 112 | /** |
---|
[2779] | 113 | @brief |
---|
[2908] | 114 | Adds an element to the map. |
---|
| 115 | @param element |
---|
| 116 | The element to be added. |
---|
| 117 | @param name |
---|
| 118 | The name of the element. |
---|
| 119 | @remarks |
---|
| 120 | The names of the OrxonoxOverlays have to be unique! |
---|
| 121 | */ |
---|
| 122 | void OverlayGroup::insertElement(OrxonoxOverlay* element, const std::string & name) |
---|
| 123 | { |
---|
| 124 | element->setName(name); |
---|
| 125 | if (hudElements_.find(name) != hudElements_.end()) |
---|
| 126 | { |
---|
| 127 | COUT(1) << "Ambiguous names encountered while load the HUD overlays" << std::endl; |
---|
| 128 | } |
---|
| 129 | else |
---|
| 130 | { |
---|
| 131 | hudElements_[name] = element; |
---|
| 132 | element->setVisible(this->isVisible()); |
---|
| 133 | if (this->owner_) |
---|
| 134 | element->setOwner(this->owner_); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | @brief |
---|
[2907] | 140 | Removes an element from the map. |
---|
[2908] | 141 | @param name |
---|
| 142 | The name of the element that is removed. |
---|
[2779] | 143 | @return |
---|
| 144 | Returns true if there was such an element to remove, false if not. |
---|
| 145 | */ |
---|
[2908] | 146 | bool OverlayGroup::removeElement(const std::string & name) |
---|
[2779] | 147 | { |
---|
[2908] | 148 | if(this->hudElements_.erase(name) == 0) |
---|
[2779] | 149 | return false; |
---|
| 150 | return true; |
---|
| 151 | } |
---|
| 152 | |
---|
[1623] | 153 | //! Returns a different element as long as index < hudElements_.size(). |
---|
[1615] | 154 | OrxonoxOverlay* OverlayGroup::getElement(unsigned int index) |
---|
[1588] | 155 | { |
---|
[1615] | 156 | if (index < this->hudElements_.size()) |
---|
| 157 | { |
---|
[2908] | 158 | std::map<std::string, OrxonoxOverlay*>::const_iterator it = hudElements_.begin(); |
---|
[1615] | 159 | for (unsigned int i = 0; i != index; ++it, ++i) |
---|
| 160 | ; |
---|
[2908] | 161 | return (*it).second; |
---|
[1615] | 162 | } |
---|
| 163 | else |
---|
| 164 | return 0; |
---|
[1505] | 165 | } |
---|
| 166 | |
---|
[1633] | 167 | //! Changes the visibility of all elements |
---|
| 168 | void OverlayGroup::changedVisibility() |
---|
| 169 | { |
---|
[2908] | 170 | for (std::map<std::string, OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) |
---|
| 171 | (*it).second->setVisible(this->isVisible()); |
---|
[1633] | 172 | } |
---|
[1614] | 173 | |
---|
[2908] | 174 | void OverlayGroup::setOwner(ControllableEntity* owner) |
---|
[2662] | 175 | { |
---|
| 176 | this->owner_ = owner; |
---|
[1633] | 177 | |
---|
[2908] | 178 | for (std::map<std::string, OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) |
---|
| 179 | (*it).second->setOwner(owner); |
---|
[2662] | 180 | } |
---|
| 181 | |
---|
[1623] | 182 | //########### Console commands ############ |
---|
| 183 | |
---|
| 184 | /** |
---|
| 185 | @brief |
---|
| 186 | Hides/shows an overlay group by its name. |
---|
| 187 | @param name |
---|
| 188 | The name of the group defined BaseObject::setName() (usually done with the "name" |
---|
| 189 | attribute in the xml file). |
---|
| 190 | */ |
---|
[1615] | 191 | /*static*/ void OverlayGroup::toggleVisibility(const std::string& name) |
---|
[1614] | 192 | { |
---|
[1747] | 193 | for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it) |
---|
[1615] | 194 | { |
---|
| 195 | if ((*it)->getName() == name) |
---|
[1625] | 196 | (*it)->setVisible(!((*it)->isVisible())); |
---|
[1615] | 197 | } |
---|
[1614] | 198 | } |
---|
[1505] | 199 | |
---|
[1623] | 200 | /** |
---|
| 201 | @brief |
---|
| 202 | Scales an overlay group by its name. |
---|
| 203 | @param name |
---|
| 204 | The name of the group defined BaseObject::setName() (usually done with the "name" |
---|
| 205 | attribute in the xml file). |
---|
| 206 | */ |
---|
[1615] | 207 | /*static*/ void OverlayGroup::scaleGroup(const std::string& name, float scale) |
---|
[1564] | 208 | { |
---|
[1747] | 209 | for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it) |
---|
[1615] | 210 | { |
---|
| 211 | if ((*it)->getName() == name) |
---|
| 212 | (*it)->scale(Vector2(scale, scale)); |
---|
| 213 | } |
---|
[1564] | 214 | } |
---|
[1615] | 215 | |
---|
[1623] | 216 | /** |
---|
| 217 | @brief |
---|
| 218 | Scrolls an overlay group by its name. |
---|
| 219 | @param name |
---|
| 220 | The name of the group defined BaseObject::setName() (usually done with the "name" |
---|
| 221 | attribute in the xml file). |
---|
| 222 | */ |
---|
[1615] | 223 | /*static*/ void OverlayGroup::scrollGroup(const std::string& name, const Vector2& scroll) |
---|
| 224 | { |
---|
[1747] | 225 | for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it) |
---|
[1615] | 226 | { |
---|
| 227 | if ((*it)->getName() == name) |
---|
| 228 | (*it)->scroll(scroll); |
---|
| 229 | } |
---|
| 230 | } |
---|
[1505] | 231 | } |
---|