Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud/src/orxonox/overlays/HUDOverlay.cc @ 1600

Last change on this file since 1600 was 1600, checked in by rgrieder, 16 years ago

again…

  • Property svn:eol-style set to native
File size: 4.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 "HUDOverlay.h"
31
32#include <OgreOverlayManager.h>
33#include "util/Convert.h"
34#include "core/CoreIncludes.h"
35#include "GraphicsEngine.h"
36
37namespace orxonox
38{
39  unsigned int HUDOverlay::hudOverlayCounter_s = 0;
40
41  HUDOverlay::HUDOverlay()
42    : overlay_(0)
43    , windowAspectRatio_(1.0f)
44    , bCorrectAspect_(false)
45    , size_(1.0f, 1.0f)
46    , sizeCorrection_(1.0f, 1.0f)
47    , angle_(0.0f)
48    , position_(0.0f, 0.0f)
49    , origin_(0.5f, 0.5f)
50  {
51    RegisterObject(HUDOverlay);
52  }
53
54  void HUDOverlay::XMLPort(Element& xmlElement, XMLPort::Mode mode)
55  {
56    BaseObject::XMLPort(xmlElement, mode);
57
58    if (mode == XMLPort::LoadObject)
59    {
60      overlay_ = Ogre::OverlayManager::getSingleton().create("HUDOverlay"
61            + convertToString(hudOverlayCounter_s++) + "_" + this->getName());
62
63      this->windowResized(GraphicsEngine::getSingleton().getWindowWidth(),
64            GraphicsEngine::getSingleton().getWindowHeight());
65    }
66
67    XMLPortParam(HUDOverlay, "correctAspect", setAspectCorrection, getAspectCorrection, xmlElement, mode);
68    XMLPortParam(HUDOverlay, "size", setSize, getSize, xmlElement, mode);
69    XMLPortParam(HUDOverlay, "rotation", setRotation, getRotation, xmlElement, mode);
70    XMLPortParam(HUDOverlay, "origin", setOrigin, getOrigin, xmlElement, mode);
71    XMLPortParam(HUDOverlay, "position", setPosition, getPosition, xmlElement, mode);
72
73    if (mode == XMLPort::LoadObject)
74    {
75      this->overlay_->show();
76      if (!this->isVisible())
77          this->overlay_->hide();
78
79      this->sizeChanged();
80    }
81  }
82
83  HUDOverlay::~HUDOverlay()
84  {
85  }
86
87  void HUDOverlay::changedVisibility()
88  {
89    if (this->overlay_)
90    {
91      if (this->isVisible())
92        this->overlay_->show();
93      else
94        this->overlay_->hide();
95    }
96  }
97
98  void HUDOverlay::windowResized(int newWidth, int newHeight)
99  {
100    this->windowAspectRatio_ = newWidth/(float)newHeight;
101
102    this->setAspectCorrection(this->bCorrectAspect_);
103  }
104
105  void HUDOverlay::setAspectCorrection(bool val)
106  {
107    if (val)
108    {
109      // note: this is only an approximation that is mostly valid when the
110      // magnitude of the width is about the magnitude of the height.
111      // Correctly we would have to take the square root of width*height
112      this->sizeCorrection_.x = 2.0 / (this->windowAspectRatio_ + 1.0);
113      this->sizeCorrection_.y = this->windowAspectRatio_ * this->sizeCorrection_.x;
114    }
115    else
116    {
117      this->sizeCorrection_ = Vector2::UNIT_SCALE;
118    }
119
120    this->bCorrectAspect_ = val;
121    this->sizeChanged();
122  }
123
124  /**
125    @remarks
126      This function can be overriden by any derivative.
127  */
128  void HUDOverlay::sizeChanged()
129  {
130    this->overlay_->setScale(size_.x * sizeCorrection_.x, size_.y * sizeCorrection_.y);
131  }
132
133  /**
134    @remarks
135      This function can be overriden by any derivative.
136  */
137  void HUDOverlay::angleChanged()
138  {
139    this->overlay_->setRotate(this->angle_);
140  }
141
142  /**
143    @remarks
144      This function can be overriden by any derivative.
145  */
146  void HUDOverlay::positionChanged()
147  {
148    Vector2 scroll = (position_ - 0.5 - size_ * (origin_ - 0.5)) * 2.0;
149    this->overlay_->setScroll(scroll.x, -scroll.y);
150  }
151}
Note: See TracBrowser for help on using the repository browser.