Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2236 was 2087, checked in by landauf, 16 years ago

merged objecthierarchy branch back to trunk

  • 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
57        setScale(Vector2(1.0, 1.0));
58        setScroll(Vector2(0.0, 0.0));
[1615]59    }
[1505]60
[2087]61    OverlayGroup::~OverlayGroup()
62    {
63        for (std::map<std::string, OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
64            delete it->second;
65    }
66
[1623]67    /**
68    @brief
69        Loads the group and all its children OrxonoxOverlays.
70    @copydoc
71        BaseObject::XMLPort()
72    */
[1615]73    void OverlayGroup::XMLPort(Element& xmlElement, XMLPort::Mode mode)
74    {
[1747]75        SUPER(OverlayGroup, XMLPort, xmlElement, mode);
[1588]76
[2087]77        XMLPortParam(OverlayGroup, "scale",  setScale,  getScale,  xmlElement, mode);
78        XMLPortParam(OverlayGroup, "scroll", setScroll, getScroll, xmlElement, mode);
[1623]79        // loads all the child elements
[1854]80        XMLPortObject(OverlayGroup, OrxonoxOverlay, "", addElement, getElement, xmlElement, mode);
[1615]81    }
[1505]82
[1623]83    //! Scales every element in the map.
[1615]84    void OverlayGroup::setScale(const Vector2& scale)
85    {
86        for (std::map<std::string, OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
87            (*it).second->scale(scale / this->scale_);
88        this->scale_ = scale;
89    }
[1588]90
[1623]91    //! Scrolls every element in the map.
[1615]92    void OverlayGroup::setScroll(const Vector2& scroll)
93    {
94        for (std::map<std::string, OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
95            (*it).second->scroll(scroll - this->scroll_);
96        this->scroll_ = scroll;
97    }
[1505]98
[1623]99    /**
100    @brief
101        Adds an element to the map (used when loading with XMLPort).
102    @remarks
103        The names of the OrxonoxOverlays have to be unique!
104    */
[1615]105    void OverlayGroup::addElement(OrxonoxOverlay* element)
[1564]106    {
[1615]107        if (hudElements_.find(element->getName()) != hudElements_.end())
108        {
109            COUT(1) << "Ambiguous names encountered while load the HUD overlays" << std::endl;
110        }
111        else
[2087]112        {
[1615]113            hudElements_[element->getName()] = element;
[2087]114            element->setVisible(this->isVisible());
115        }
[1588]116    }
[1564]117
[1623]118    //! Returns a different element as long as index < hudElements_.size().
[1615]119    OrxonoxOverlay* OverlayGroup::getElement(unsigned int index)
[1588]120    {
[1615]121        if (index < this->hudElements_.size())
122        {
123            std::map<std::string, OrxonoxOverlay*>::const_iterator it = hudElements_.begin();
124            for (unsigned int i = 0; i != index; ++it, ++i)
125                ;
126            return (*it).second;
127        }
128        else
129            return 0;
[1505]130    }
131
[1633]132    //! Changes the visibility of all elements
133    void OverlayGroup::changedVisibility()
134    {
135        for (std::map<std::string, OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
136            (*it).second->setVisible(this->isVisible());
137    }
[1614]138
[1633]139
[1623]140    //########### Console commands ############
141
142    /**
143    @brief
144        Hides/shows an overlay group by its name.
145    @param name
146        The name of the group defined BaseObject::setName() (usually done with the "name"
147        attribute in the xml file).
148    */
[1615]149    /*static*/ void OverlayGroup::toggleVisibility(const std::string& name)
[1614]150    {
[1747]151        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
[1615]152        {
153            if ((*it)->getName() == name)
[1625]154                (*it)->setVisible(!((*it)->isVisible()));
[1615]155        }
[1614]156    }
[1505]157
[1623]158    /**
159    @brief
160        Scales an overlay group by its name.
161    @param name
162        The name of the group defined BaseObject::setName() (usually done with the "name"
163        attribute in the xml file).
164    */
[1615]165    /*static*/ void OverlayGroup::scaleGroup(const std::string& name, float scale)
[1564]166    {
[1747]167        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
[1615]168        {
169            if ((*it)->getName() == name)
170                (*it)->scale(Vector2(scale, scale));
171        }
[1564]172    }
[1615]173
[1623]174    /**
175    @brief
176        Scrolls an overlay group by its name.
177    @param name
178        The name of the group defined BaseObject::setName() (usually done with the "name"
179        attribute in the xml file).
180    */
[1615]181    /*static*/ void OverlayGroup::scrollGroup(const std::string& name, const Vector2& scroll)
182    {
[1747]183        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
[1615]184        {
185            if ((*it)->getName() == name)
186                (*it)->scroll(scroll);
187        }
188    }
[1505]189}
Note: See TracBrowser for help on using the repository browser.