Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3144 was 3144, checked in by rgrieder, 15 years ago

Header file section clean up in orxonox/overlays (without stats). Also reduced dependencies as much as possible.

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