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