[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 | |
---|
[1588] | 36 | #include "core/CoreIncludes.h" |
---|
[1616] | 37 | #include "core/XMLPort.h" |
---|
[10624] | 38 | #include "core/command/ConsoleCommandIncludes.h" |
---|
[1604] | 39 | #include "OrxonoxOverlay.h" |
---|
[10624] | 40 | #include "gametypes/Gametype.h" |
---|
[1505] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[11052] | 44 | namespace autocompletion |
---|
| 45 | { |
---|
| 46 | /** |
---|
| 47 | @brief Returns the names of all currently existing OverlayGroups. |
---|
| 48 | */ |
---|
| 49 | ARGUMENT_COMPLETION_FUNCTION_DECLARATION(overlaygroupnames)(); |
---|
| 50 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(overlaygroupnames)() |
---|
| 51 | { |
---|
| 52 | ArgumentCompletionList names; |
---|
[11071] | 53 | for (OverlayGroup* overlayGroup : ObjectList<OverlayGroup>()) |
---|
| 54 | names.push_back(ArgumentCompletionListElement(overlayGroup->getName(), getLowercase(overlayGroup->getName()))); |
---|
[11052] | 55 | return names; |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | SetConsoleCommand("OverlayGroup", "toggleVisibility", &OverlayGroup::toggleVisibility).argumentCompleter(0, autocompletion::overlaygroupnames()); |
---|
| 60 | SetConsoleCommand("OverlayGroup", "show", &OverlayGroup::show ).argumentCompleter(0, autocompletion::overlaygroupnames()); |
---|
| 61 | SetConsoleCommand("OverlayGroup", "scaleGroup", &OverlayGroup::scaleGroup ).argumentCompleter(0, autocompletion::overlaygroupnames()); |
---|
| 62 | SetConsoleCommand("OverlayGroup", "scrollGroup", &OverlayGroup::scrollGroup ).argumentCompleter(0, autocompletion::overlaygroupnames()); |
---|
| 63 | |
---|
[9667] | 64 | RegisterClass(OverlayGroup); |
---|
[1588] | 65 | |
---|
[9667] | 66 | OverlayGroup::OverlayGroup(Context* context) |
---|
| 67 | : BaseObject(context) |
---|
[1615] | 68 | { |
---|
| 69 | RegisterObject(OverlayGroup); |
---|
[2087] | 70 | |
---|
[11071] | 71 | this->owner_ = nullptr; |
---|
[2662] | 72 | |
---|
[2087] | 73 | setScale(Vector2(1.0, 1.0)); |
---|
| 74 | setScroll(Vector2(0.0, 0.0)); |
---|
[1615] | 75 | } |
---|
[1505] | 76 | |
---|
[2087] | 77 | OverlayGroup::~OverlayGroup() |
---|
| 78 | { |
---|
[11071] | 79 | for (OrxonoxOverlay* hudElement : hudElements_) |
---|
| 80 | hudElement->destroy(); |
---|
[6054] | 81 | this->hudElements_.clear(); |
---|
[2087] | 82 | } |
---|
| 83 | |
---|
[1623] | 84 | /** |
---|
| 85 | @brief |
---|
| 86 | Loads the group and all its children OrxonoxOverlays. |
---|
| 87 | @copydoc |
---|
| 88 | BaseObject::XMLPort() |
---|
| 89 | */ |
---|
[7401] | 90 | void OverlayGroup::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[1615] | 91 | { |
---|
[7401] | 92 | SUPER(OverlayGroup, XMLPort, xmlelement, mode); |
---|
[1588] | 93 | |
---|
[7401] | 94 | XMLPortParam(OverlayGroup, "scale", setScale, getScale, xmlelement, mode); |
---|
| 95 | XMLPortParam(OverlayGroup, "scroll", setScroll, getScroll, xmlelement, mode); |
---|
[1623] | 96 | // loads all the child elements |
---|
[7401] | 97 | XMLPortObject(OverlayGroup, OrxonoxOverlay, "", addElement, getElement, xmlelement, mode); |
---|
[1615] | 98 | } |
---|
[1505] | 99 | |
---|
[2890] | 100 | //! Scales every element in the set. |
---|
[1615] | 101 | void OverlayGroup::setScale(const Vector2& scale) |
---|
| 102 | { |
---|
[11071] | 103 | for (OrxonoxOverlay* hudElement : hudElements_) |
---|
| 104 | hudElement->scale(scale / this->scale_); |
---|
[1615] | 105 | this->scale_ = scale; |
---|
| 106 | } |
---|
[1588] | 107 | |
---|
[2890] | 108 | //! Scrolls every element in the set. |
---|
[1615] | 109 | void OverlayGroup::setScroll(const Vector2& scroll) |
---|
| 110 | { |
---|
[11071] | 111 | for (OrxonoxOverlay* hudElement : hudElements_) |
---|
| 112 | hudElement->scroll(scroll - this->scroll_); |
---|
[1615] | 113 | this->scroll_ = scroll; |
---|
| 114 | } |
---|
[1505] | 115 | |
---|
[1623] | 116 | /** |
---|
| 117 | @brief |
---|
[2890] | 118 | Adds an element to the set (used when loading with XMLPort). |
---|
[1623] | 119 | @remarks |
---|
| 120 | The names of the OrxonoxOverlays have to be unique! |
---|
| 121 | */ |
---|
[1615] | 122 | void OverlayGroup::addElement(OrxonoxOverlay* element) |
---|
[1564] | 123 | { |
---|
[10624] | 124 | hudElements_.insert(element); |
---|
[5980] | 125 | element->setOverlayGroup( this ); |
---|
[2890] | 126 | if (this->owner_) |
---|
| 127 | element->setOwner(this->owner_); |
---|
[1588] | 128 | } |
---|
[1564] | 129 | |
---|
[3034] | 130 | /** |
---|
[2911] | 131 | @brief |
---|
| 132 | Removes an element from the map. |
---|
[7401] | 133 | @param element |
---|
| 134 | A pointer to the element that is removed. |
---|
[2911] | 135 | @return |
---|
| 136 | Returns true if there was such an element to remove, false if not. |
---|
| 137 | */ |
---|
| 138 | bool OverlayGroup::removeElement(OrxonoxOverlay* element) |
---|
| 139 | { |
---|
[10624] | 140 | if(this->hudElements_.erase(element) == 0) |
---|
[2911] | 141 | return false; |
---|
| 142 | return true; |
---|
| 143 | } |
---|
| 144 | |
---|
[1623] | 145 | //! Returns a different element as long as index < hudElements_.size(). |
---|
[1615] | 146 | OrxonoxOverlay* OverlayGroup::getElement(unsigned int index) |
---|
[1588] | 147 | { |
---|
[1615] | 148 | if (index < this->hudElements_.size()) |
---|
| 149 | { |
---|
[11071] | 150 | std::set<StrongPtr<OrxonoxOverlay>>::const_iterator it = hudElements_.begin(); |
---|
[1615] | 151 | for (unsigned int i = 0; i != index; ++it, ++i) |
---|
| 152 | ; |
---|
[10624] | 153 | return *it; |
---|
[1615] | 154 | } |
---|
| 155 | else |
---|
[11071] | 156 | return nullptr; |
---|
[1505] | 157 | } |
---|
| 158 | |
---|
[1633] | 159 | //! Changes the visibility of all elements |
---|
| 160 | void OverlayGroup::changedVisibility() |
---|
| 161 | { |
---|
[5980] | 162 | SUPER( OverlayGroup, changedVisibility ); |
---|
[6417] | 163 | |
---|
[11071] | 164 | for (OrxonoxOverlay* hudElement : hudElements_) |
---|
| 165 | hudElement->changedVisibility(); //inform all Child Overlays that our visibility has changed |
---|
[1633] | 166 | } |
---|
[1614] | 167 | |
---|
[2890] | 168 | void OverlayGroup::setOwner(BaseObject* owner) |
---|
[2662] | 169 | { |
---|
| 170 | this->owner_ = owner; |
---|
[1633] | 171 | |
---|
[11071] | 172 | for (OrxonoxOverlay* hudElement : hudElements_) |
---|
| 173 | hudElement->setOwner(owner); |
---|
[2662] | 174 | } |
---|
| 175 | |
---|
[1623] | 176 | //########### Console commands ############ |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | @brief |
---|
| 180 | Hides/shows an overlay group by its name. |
---|
| 181 | @param name |
---|
| 182 | The name of the group defined BaseObject::setName() (usually done with the "name" |
---|
| 183 | attribute in the xml file). |
---|
| 184 | */ |
---|
[1615] | 185 | /*static*/ void OverlayGroup::toggleVisibility(const std::string& name) |
---|
[1614] | 186 | { |
---|
[11071] | 187 | for (OverlayGroup* group : ObjectList<OverlayGroup>()) |
---|
[1615] | 188 | { |
---|
[11071] | 189 | if (group->getName() == name) |
---|
| 190 | group->setVisible(!(group->isVisible())); |
---|
[1615] | 191 | } |
---|
[1614] | 192 | } |
---|
[8309] | 193 | |
---|
| 194 | /** |
---|
| 195 | @brief |
---|
| 196 | Shows an overlay group by its name. |
---|
| 197 | @param name |
---|
| 198 | The name of the group defined BaseObject::setName() (usually done with the "name" attribute in the xml file). |
---|
| 199 | */ |
---|
| 200 | /*static*/ void OverlayGroup::show(const std::string& name) |
---|
| 201 | { |
---|
[11071] | 202 | for (OverlayGroup* group : ObjectList<OverlayGroup>()) |
---|
[8309] | 203 | { |
---|
[11071] | 204 | if (group->getName() == name) |
---|
[8309] | 205 | { |
---|
[11071] | 206 | if(group->isVisible()) |
---|
| 207 | group->changedVisibility(); |
---|
[8309] | 208 | else |
---|
[11071] | 209 | group->setVisible(!(group->isVisible())); |
---|
[8309] | 210 | } |
---|
| 211 | } |
---|
| 212 | } |
---|
[1505] | 213 | |
---|
[1623] | 214 | /** |
---|
| 215 | @brief |
---|
| 216 | Scales an overlay group by its name. |
---|
| 217 | @param name |
---|
| 218 | The name of the group defined BaseObject::setName() (usually done with the "name" |
---|
| 219 | attribute in the xml file). |
---|
[7401] | 220 | @param scale |
---|
| 221 | The scaling factor |
---|
[1623] | 222 | */ |
---|
[1615] | 223 | /*static*/ void OverlayGroup::scaleGroup(const std::string& name, float scale) |
---|
[1564] | 224 | { |
---|
[11071] | 225 | for (OverlayGroup* group : ObjectList<OverlayGroup>()) |
---|
[1615] | 226 | { |
---|
[11071] | 227 | if (group->getName() == name) |
---|
| 228 | group->scale(Vector2(scale, scale)); |
---|
[1615] | 229 | } |
---|
[1564] | 230 | } |
---|
[1615] | 231 | |
---|
[1623] | 232 | /** |
---|
| 233 | @brief |
---|
| 234 | Scrolls an overlay group by its name. |
---|
| 235 | @param name |
---|
| 236 | The name of the group defined BaseObject::setName() (usually done with the "name" |
---|
| 237 | attribute in the xml file). |
---|
[7401] | 238 | @param scroll |
---|
| 239 | The relative translation of the overlay group |
---|
[1623] | 240 | */ |
---|
[1615] | 241 | /*static*/ void OverlayGroup::scrollGroup(const std::string& name, const Vector2& scroll) |
---|
| 242 | { |
---|
[11071] | 243 | for (OverlayGroup* group : ObjectList<OverlayGroup>()) |
---|
[1615] | 244 | { |
---|
[11071] | 245 | if (group->getName() == name) |
---|
| 246 | group->scroll(scroll); |
---|
[1615] | 247 | } |
---|
| 248 | } |
---|
[1505] | 249 | } |
---|