Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/overlays/OverlayGroup.cc @ 2907

Last change on this file since 2907 was 2890, checked in by landauf, 16 years ago
  • Added overlay element for the Pong Gametype
  • Changed the type of the overlay's owner to BaseObject (former ControllableEntity) to allow other classes to own an overlay (for example a Gametype)
  • OverlayGroup does now use a std::set instead of a std::map to store it's overlay elements. Therefore a name is not anymore compulsory for an overlay element.
  • Property svn:eol-style set to native
File size: 6.0 KB
RevLine 
[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
[1505]34#include "OrxonoxStableHeaders.h"
[1601]35#include "OverlayGroup.h"
[1505]36
[1747]37#include "util/Debug.h"
[1505]38#include "core/ConsoleCommand.h"
[1588]39#include "core/CoreIncludes.h"
[1747]40#include "core/Iterator.h"
[1616]41#include "core/XMLPort.h"
[1604]42#include "OrxonoxOverlay.h"
[1505]43
44namespace orxonox
45{
[1615]46    CreateFactory(OverlayGroup);
[1588]47
[1747]48    SetConsoleCommand(OverlayGroup, toggleVisibility, false).accessLevel(AccessLevel::User);
49    SetConsoleCommand(OverlayGroup, scaleGroup, false).accessLevel(AccessLevel::User);
50    SetConsoleCommand(OverlayGroup, scrollGroup, false).accessLevel(AccessLevel::User);
[1505]51
[2087]52    OverlayGroup::OverlayGroup(BaseObject* creator)
53        : BaseObject(creator)
[1615]54    {
55        RegisterObject(OverlayGroup);
[2087]56
[2662]57        this->owner_ = 0;
58
[2087]59        setScale(Vector2(1.0, 1.0));
60        setScroll(Vector2(0.0, 0.0));
[1615]61    }
[1505]62
[2087]63    OverlayGroup::~OverlayGroup()
64    {
[2890]65        for (std::set<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
66            delete (*it);
[2087]67    }
68
[1623]69    /**
70    @brief
71        Loads the group and all its children OrxonoxOverlays.
72    @copydoc
73        BaseObject::XMLPort()
74    */
[1615]75    void OverlayGroup::XMLPort(Element& xmlElement, XMLPort::Mode mode)
76    {
[1747]77        SUPER(OverlayGroup, XMLPort, xmlElement, mode);
[1588]78
[2087]79        XMLPortParam(OverlayGroup, "scale",  setScale,  getScale,  xmlElement, mode);
80        XMLPortParam(OverlayGroup, "scroll", setScroll, getScroll, xmlElement, mode);
[1623]81        // loads all the child elements
[1854]82        XMLPortObject(OverlayGroup, OrxonoxOverlay, "", addElement, getElement, xmlElement, mode);
[1615]83    }
[1505]84
[2890]85    //! Scales every element in the set.
[1615]86    void OverlayGroup::setScale(const Vector2& scale)
87    {
[2890]88        for (std::set<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
89            (*it)->scale(scale / this->scale_);
[1615]90        this->scale_ = scale;
91    }
[1588]92
[2890]93    //! Scrolls every element in the set.
[1615]94    void OverlayGroup::setScroll(const Vector2& scroll)
95    {
[2890]96        for (std::set<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
97            (*it)->scroll(scroll - this->scroll_);
[1615]98        this->scroll_ = scroll;
99    }
[1505]100
[1623]101    /**
102    @brief
[2890]103        Adds an element to the set (used when loading with XMLPort).
[1623]104    @remarks
105        The names of the OrxonoxOverlays have to be unique!
106    */
[1615]107    void OverlayGroup::addElement(OrxonoxOverlay* element)
[1564]108    {
[2890]109        hudElements_.insert(element);
110        element->setVisible(this->isVisible());
111        if (this->owner_)
112            element->setOwner(this->owner_);
[1588]113    }
[1564]114
[1623]115    //! Returns a different element as long as index < hudElements_.size().
[1615]116    OrxonoxOverlay* OverlayGroup::getElement(unsigned int index)
[1588]117    {
[1615]118        if (index < this->hudElements_.size())
119        {
[2890]120            std::set<OrxonoxOverlay*>::const_iterator it = hudElements_.begin();
[1615]121            for (unsigned int i = 0; i != index; ++it, ++i)
122                ;
[2890]123            return (*it);
[1615]124        }
125        else
126            return 0;
[1505]127    }
128
[1633]129    //! Changes the visibility of all elements
130    void OverlayGroup::changedVisibility()
131    {
[2890]132        for (std::set<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
133            (*it)->setVisible(this->isVisible());
[1633]134    }
[1614]135
[2890]136    void OverlayGroup::setOwner(BaseObject* owner)
[2662]137    {
138        this->owner_ = owner;
[1633]139
[2890]140        for (std::set<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
141            (*it)->setOwner(owner);
[2662]142    }
143
[1623]144    //########### Console commands ############
145
146    /**
147    @brief
148        Hides/shows an overlay group by its name.
149    @param name
150        The name of the group defined BaseObject::setName() (usually done with the "name"
151        attribute in the xml file).
152    */
[1615]153    /*static*/ void OverlayGroup::toggleVisibility(const std::string& name)
[1614]154    {
[1747]155        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
[1615]156        {
157            if ((*it)->getName() == name)
[1625]158                (*it)->setVisible(!((*it)->isVisible()));
[1615]159        }
[1614]160    }
[1505]161
[1623]162    /**
163    @brief
164        Scales 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::scaleGroup(const std::string& name, float scale)
[1564]170    {
[1747]171        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
[1615]172        {
173            if ((*it)->getName() == name)
174                (*it)->scale(Vector2(scale, scale));
175        }
[1564]176    }
[1615]177
[1623]178    /**
179    @brief
180        Scrolls 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::scrollGroup(const std::string& name, const Vector2& scroll)
186    {
[1747]187        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
[1615]188        {
189            if ((*it)->getName() == name)
190                (*it)->scroll(scroll);
191        }
192    }
[1505]193}
Note: See TracBrowser for help on using the repository browser.