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