| [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; | 
|---|
 | 53 |             for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it) | 
|---|
 | 54 |                 names.push_back(ArgumentCompletionListElement(it->getName(), getLowercase(it->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 |  | 
|---|
| [9667] | 64 |     RegisterClass(OverlayGroup); | 
|---|
| [1588] | 65 |  | 
|---|
| [9667] | 66 |     OverlayGroup::OverlayGroup(Context* context) | 
|---|
 | 67 |         : BaseObject(context) | 
|---|
| [1615] | 68 |     { | 
|---|
 | 69 |         RegisterObject(OverlayGroup); | 
|---|
| [2087] | 70 |  | 
|---|
| [2662] | 71 |         this->owner_ = 0; | 
|---|
 | 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 |     { | 
|---|
| [10624] | 79 |         for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) | 
|---|
| [5929] | 80 |             (*it)->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 |     { | 
|---|
| [10624] | 103 |         for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) | 
|---|
| [2890] | 104 |             (*it)->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 |     { | 
|---|
| [10624] | 111 |         for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) | 
|---|
| [2890] | 112 |             (*it)->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 |         { | 
|---|
| [10624] | 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 | 
|---|
 | 156 |             return 0; | 
|---|
| [1505] | 157 |     } | 
|---|
 | 158 |  | 
|---|
| [1633] | 159 |     //! Changes the visibility of all elements | 
|---|
 | 160 |     void OverlayGroup::changedVisibility() | 
|---|
 | 161 |     { | 
|---|
| [5980] | 162 |         SUPER( OverlayGroup, changedVisibility ); | 
|---|
| [6417] | 163 |  | 
|---|
| [10624] | 164 |         for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) | 
|---|
| [5980] | 165 |             (*it)->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 |  | 
|---|
| [10624] | 172 |         for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) | 
|---|
| [2890] | 173 |             (*it)->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 |     { | 
|---|
| [1747] | 187 |         for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it) | 
|---|
| [1615] | 188 |         { | 
|---|
 | 189 |             if ((*it)->getName() == name) | 
|---|
| [1625] | 190 |                 (*it)->setVisible(!((*it)->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 |     { | 
|---|
 | 202 |         for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it) | 
|---|
 | 203 |         { | 
|---|
 | 204 |             if ((*it)->getName() == name) | 
|---|
 | 205 |             { | 
|---|
 | 206 |                 if((*it)->isVisible()) | 
|---|
 | 207 |                     (*it)->changedVisibility(); | 
|---|
 | 208 |                 else | 
|---|
 | 209 |                     (*it)->setVisible(!((*it)->isVisible())); | 
|---|
 | 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 |     { | 
|---|
| [1747] | 225 |         for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it) | 
|---|
| [1615] | 226 |         { | 
|---|
 | 227 |             if ((*it)->getName() == name) | 
|---|
 | 228 |                 (*it)->scale(Vector2(scale, scale)); | 
|---|
 | 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 |     { | 
|---|
| [1747] | 243 |         for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it) | 
|---|
| [1615] | 244 |         { | 
|---|
 | 245 |             if ((*it)->getName() == name) | 
|---|
 | 246 |                 (*it)->scroll(scroll); | 
|---|
 | 247 |         } | 
|---|
 | 248 |     } | 
|---|
| [1505] | 249 | } | 
|---|