Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud/src/orxonox/overlays/OrxonoxOverlay.cc @ 1614

Last change on this file since 1614 was 1614, checked in by rgrieder, 16 years ago
  • Dots on the Radar actually disappear now when a Ship gets destroyed…
  • svn save to keep History of HUDText when renaming AND moving
  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
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#include "OrxonoxStableHeaders.h"
30#include "OrxonoxOverlay.h"
31
32#include <OgreOverlayManager.h>
33#include <OgrePanelOverlayElement.h>
34#include "util/Convert.h"
35#include "core/CoreIncludes.h"
36#include "GraphicsEngine.h"
37
38namespace orxonox
39{
40  unsigned int OrxonoxOverlay::hudOverlayCounter_s = 0;
41
42  OrxonoxOverlay::OrxonoxOverlay()
43    : overlay_(0)
44    , background_(0)
45    , windowAspectRatio_(1.0f)
46    , bCorrectAspect_(false)
47    , size_(1.0f, 1.0f)
48    , sizeCorrection_(1.0f, 1.0f)
49    , angle_(0.0f)
50    , position_(0.0f, 0.0f)
51    , origin_(0.0f, 0.0f)
52  {
53    RegisterObject(OrxonoxOverlay);
54  }
55
56  OrxonoxOverlay::~OrxonoxOverlay()
57  {
58    if (this->background_)
59      Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->background_);
60  }
61
62  void OrxonoxOverlay::XMLPort(Element& xmlElement, XMLPort::Mode mode)
63  {
64    BaseObject::XMLPort(xmlElement, mode);
65
66    if (mode == XMLPort::LoadObject)
67    {
68      overlay_ = Ogre::OverlayManager::getSingleton().create("OrxonoxOverlay"
69            + convertToString(hudOverlayCounter_s++) + "_" + this->getName());
70
71      this->windowResized(GraphicsEngine::getSingleton().getWindowWidth(),
72            GraphicsEngine::getSingleton().getWindowHeight());
73
74      // create background
75      this->background_ = static_cast<Ogre::PanelOverlayElement*>(
76          Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", getUniqueNumberStr() + "_Background"));
77      this->overlay_->add2D(this->background_);
78    }
79
80    XMLPortParam(OrxonoxOverlay, "correctAspect", setAspectCorrection, getAspectCorrection, xmlElement, mode);
81    XMLPortParam(OrxonoxOverlay, "size", setSize, getUncorrectedSize, xmlElement, mode);
82    XMLPortParam(OrxonoxOverlay, "rotation", setRotation, getRotation, xmlElement, mode);
83    XMLPortParam(OrxonoxOverlay, "origin", setOrigin, getOrigin, xmlElement, mode);
84    XMLPortParam(OrxonoxOverlay, "position", setPosition, getPosition, xmlElement, mode);
85    XMLPortParam(OrxonoxOverlay, "background", setBackgroundMaterial, getBackgroundMaterial, xmlElement, mode);
86
87    if (mode == XMLPort::LoadObject)
88    {
89      this->overlay_->show();
90      if (!this->isVisible())
91          this->overlay_->hide();
92
93      this->sizeChanged();
94      this->positionChanged();
95      this->angleChanged();
96    }
97  }
98
99  void OrxonoxOverlay::setBackgroundMaterial(const std::string& material)
100  {
101    if (this->background_ && material != "")
102      this->background_->setMaterialName(material);
103  }
104
105  std::string OrxonoxOverlay::getBackgroundMaterial() const
106  {
107    if (this->background_)
108      return this->background_->getMaterialName();
109    else
110      return "";
111  }
112
113  void OrxonoxOverlay::changedVisibility()
114  {
115    if (this->overlay_)
116    {
117      if (this->isVisible())
118        this->overlay_->show();
119      else
120        this->overlay_->hide();
121    }
122  }
123
124  void OrxonoxOverlay::windowResized(int newWidth, int newHeight)
125  {
126    this->windowAspectRatio_ = newWidth/(float)newHeight;
127
128    this->setAspectCorrection(this->bCorrectAspect_);
129  }
130
131  void OrxonoxOverlay::setAspectCorrection(bool val)
132  {
133    if (val)
134    {
135      // note: this is only an approximation that is mostly valid when the
136      // magnitude of the width is about the magnitude of the height.
137      // Correctly we would have to take the square root of width*height
138      this->sizeCorrection_.x = 2.0 / (this->windowAspectRatio_ + 1.0);
139      this->sizeCorrection_.y = this->windowAspectRatio_ * this->sizeCorrection_.x;
140    }
141    else
142    {
143      this->sizeCorrection_ = Vector2::UNIT_SCALE;
144    }
145
146    this->bCorrectAspect_ = val;
147    this->sizeChanged();
148  }
149
150  /**
151    @remarks
152      This function can be overriden by any derivative.
153  */
154  void OrxonoxOverlay::sizeChanged()
155  {
156    this->overlay_->setScale(size_.x * sizeCorrection_.x, size_.y * sizeCorrection_.y);
157    positionChanged();
158  }
159
160  /**
161    @remarks
162      This function can be overriden by any derivative.
163  */
164  void OrxonoxOverlay::angleChanged()
165  {
166    this->overlay_->setRotate(this->angle_);
167  }
168
169  /**
170    @remarks
171      This function can be overriden by any derivative.
172  */
173  void OrxonoxOverlay::positionChanged()
174  {
175    Vector2 scroll = (position_ - 0.5 - size_ * sizeCorrection_ * (origin_ - 0.5)) * 2.0;
176    this->overlay_->setScroll(scroll.x, -scroll.y);
177  }
178}
Note: See TracBrowser for help on using the repository browser.