Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/overlays/hud/HUDNavigation.cc @ 2662

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

Merged presentation branch back to trunk.

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