Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud/src/orxonox/overlays/hud/HUDNavigation.cc @ 1621

Last change on this file since 1621 was 1621, checked in by rgrieder, 16 years ago
  • rewrote the time measure in the main loop frame smoothing is gone for good and the fps display updates faster
  • changed "render time ratio" to "tick time in ms" which represents the time necessary to tick() one frame that's saying a lot more to me
  • Property svn:eol-style set to native
File size: 10.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 *      Felix Schulthess
24 *   Co-authors:
25 *      Reto Grieder
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "HUDNavigation.h"
31
32#include <OgreOverlayManager.h>
33#include <OgreTextAreaOverlayElement.h>
34#include <OgrePanelOverlayElement.h>
35
36#include "util/Math.h"
37#include "util/String.h"
38#include "util/Convert.h"
39#include "core/ConsoleCommand.h"
40#include "core/CoreIncludes.h"
41#include "core/XMLPort.h"
42#include "objects/SpaceShip.h"
43#include "objects/Projectile.h"
44#include "objects/CameraHandler.h"
45#include "Radar.h"
46
47namespace orxonox
48{
49    CreateFactory(HUDNavigation);
50
51    HUDNavigation::HUDNavigation()
52        : navMarker_(0)
53        , aimMarker_(0)
54        , navText_(0)
55    {
56        RegisterObject(HUDNavigation);
57    }
58
59    HUDNavigation::~HUDNavigation()
60    {
61        if (this->navMarker_)
62            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navMarker_);
63        if (this->navText_)
64            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navText_);
65        if (this->aimMarker_)
66            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_);
67    }
68
69    void HUDNavigation::XMLPort(Element& xmlElement, XMLPort::Mode mode)
70    {
71        OrxonoxOverlay::XMLPort(xmlElement, mode);
72
73        if (mode == XMLPort::LoadObject)
74        {
75            // create nav text
76            navText_ = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton()
77                .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberStr()));
78            navText_->setCharHeight(0.05f);
79            navText_->setFontName("Monofur");
80
81            // create nav marker
82            navMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
83                .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberStr()));
84            navMarker_->setMaterialName("Orxonox/NavArrows");
85            navMarkerSize_ = 0.05; //default
86            wasOutOfView_ = true; // just to ensure the material is changed right the first time..
87
88            // create aim marker
89            aimMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
90                .createOverlayElement("Panel", "HUDNavigation_aimMarker_" + getUniqueNumberStr()));
91            aimMarker_->setMaterialName("Orxonox/NavCrosshair");
92            aimMarkerSize_ = 0.04; // default
93           
94            background_->addChild(navMarker_);
95            background_->addChild(aimMarker_);
96            background_->addChild(navText_);
97
98            // hide at first
99            this->setVisibility(false);
100        }
101
102        XMLPortParam(HUDNavigation, "font", setFont, getFont, xmlElement, mode);
103        XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlElement, mode);
104        XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlElement, mode);
105        XMLPortParam(HUDNavigation, "aimMarkerSize", setAimMarkerSize, getAimMarkerSize, xmlElement, mode);
106
107        if (mode == XMLPort::LoadObject)
108        {
109            this->sizeChanged();
110        }
111    }
112
113    void HUDNavigation::setFont(const std::string& font)
114    {
115        if (this->navText_ && font != "")
116            this->navText_->setFontName(font);
117    }
118
119    const std::string& HUDNavigation::getFont() const
120    {
121        if (this->navText_)
122            return this->navText_->getFontName();
123        else
124            return blankString;
125    }
126
127    void HUDNavigation::setTextSize(float size)
128    {
129        if (this->navText_ && size >= 0.0f)
130            this->navText_->setCharHeight(size);
131    }
132
133    float HUDNavigation::getTextSize() const
134    {
135        if (this->navText_)
136            return this->navText_->getCharHeight();
137        else
138            return 0.0f;
139    }
140
141    void HUDNavigation::tick(float dt)
142    {
143        if (!Radar::getInstance().getFocus())
144        {
145            this->overlay_->hide();
146            return;
147        }
148        else
149        {
150            this->overlay_->show();
151        }
152
153        // set text
154        int dist = (int) getDist2Focus();
155        navText_->setCaption(convertToString(dist));
156        float textLength = convertToString(dist).size() * navText_->getCharHeight() * 0.3;
157
158        Ogre::Camera* navCam = SpaceShip::getLocalShip()->getCamera()->cam_;
159        Matrix4 transformationMatrix = navCam->getProjectionMatrix() * navCam->getViewMatrix();
160        // transform to screen coordinates
161        Vector3 pos = transformationMatrix * Radar::getInstance().getFocus()->getWorldPosition();
162
163        bool outOfView;
164        if (pos.z > 1.0)
165        {
166            // z > 1.0 means that the object is behind the camera
167            outOfView = true;
168            // we have to switch all coordinates (if you don't know why,
169            // try linear algebra lectures, because I can't explain..)
170            pos.x = -pos.x;
171            pos.y = -pos.y;
172        }
173        else
174            outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0;
175
176        if (outOfView)
177        {
178            // object is not in view
179            aimMarker_->hide();
180
181            if (!wasOutOfView_)
182            {
183                navMarker_->setMaterialName("Orxonox/NavArrows");
184                wasOutOfView_ = true;
185            }
186
187            if (pos.x < pos.y)
188            {
189                if (pos.y > -pos.x)
190                {
191                    // up
192                    float position = pos.x / pos.y + 1.0;
193                    navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 0.0);
194                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
195                    navText_->setLeft((position - textLength) * 0.5);
196                    navText_->setTop(navMarker_->getHeight());
197                }
198                else
199                {
200                    // left
201                    float position = pos.y / pos.x + 1.0;
202                    navMarker_->setPosition(0.0, (position - navMarker_->getWidth()) * 0.5);
203                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
204                    navText_->setLeft(navMarker_->getWidth() + 0.01);
205                    navText_->setTop((position - navText_->getCharHeight()) * 0.5);
206                }
207            }
208            else
209            {
210                if (pos.y < -pos.x)
211                {
212                    // down
213                    float position = -pos.x / pos.y + 1.0;
214                    navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 1.0 - navMarker_->getHeight());
215                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
216                    navText_->setLeft((position - textLength) * 0.5);
217                    navText_->setTop(1.0 - navMarker_->getHeight() - navText_->getCharHeight());
218                }
219                else
220                {
221                    // right
222                    float position = -pos.y / pos.x + 1.0;
223                    navMarker_->setPosition(1.0 - navMarker_->getWidth(), (position - navMarker_->getHeight()) * 0.5);
224                    navMarker_->setUV(0.5, 0.5, 1.0, 1.0);
225                    navText_->setLeft(1.0 - navMarker_->getWidth() - textLength - 0.01);
226                    navText_->setTop((position - navText_->getCharHeight()) * 0.5);
227                }
228            }
229        }
230        else
231        {
232            // object is in view
233
234            Vector3 aimpos = transformationMatrix * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(),
235                    Projectile::getSpeed(), Radar::getInstance().getFocus()->getWorldPosition(), Radar::getInstance().getFocus()->getOrientedVelocity());
236
237            if (wasOutOfView_)
238            {
239                navMarker_->setMaterialName("Orxonox/NavTDC");
240                wasOutOfView_ = false;
241            }
242
243            // object is in view
244            navMarker_->setUV(0.0, 0.0, 1.0, 1.0);
245            navMarker_->setLeft((pos.x + 1.0 - navMarker_->getWidth()) * 0.5);
246            navMarker_->setTop((-pos.y + 1.0 - navMarker_->getHeight()) * 0.5);
247
248            aimMarker_->show();
249            aimMarker_->setLeft((aimpos.x + 1.0 - aimMarker_->getWidth()) * 0.5);
250            aimMarker_->setTop((-aimpos.y + 1.0 - aimMarker_->getHeight()) * 0.5);
251
252            navText_->setLeft((pos.x + 1.0 + navMarker_->getWidth()) * 0.5);
253            navText_->setTop((-pos.y + 1.0 + navMarker_->getHeight()) * 0.5);
254        }
255    }
256
257    float HUDNavigation::getDist2Focus() const
258    {
259        if (Radar::getInstance().getFocus())
260            return (Radar::getInstance().getFocus()->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length();
261        else
262            return 0;
263    }
264
265    /**
266    @brief Overridden method of OrxonoxOverlay. Usually the entire overlay
267           scales with scale(). Here we obviously have to adjust this.
268    */
269    void HUDNavigation::sizeChanged()
270    {
271        // use size to compensate for apspect ratio if enabled.
272        float xScale = this->getSize().x;
273        float yScale = this->getSize().y;
274        if (this->navMarker_)
275            navMarker_->setDimensions(navMarkerSize_ * xScale, navMarkerSize_ * yScale);
276        if (this->aimMarker_)
277            aimMarker_->setDimensions(aimMarkerSize_ * xScale, aimMarkerSize_ * yScale);
278        if (this->navText_)
279            navText_->setCharHeight(navText_->getCharHeight() * yScale);
280    }
281}
Note: See TracBrowser for help on using the repository browser.