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