Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

default values for XMLPort work after all. There was just a bug that got fixed now.

  • Property svn:eol-style set to native
File size: 9.9 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
79            // create nav marker
80            navMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
81                .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberStr()));
82            navMarker_->setMaterialName("Orxonox/NavArrows");
83            wasOutOfView_ = true; // just to ensure the material is changed right the first time..
84
85            // create aim marker
86            aimMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
87                .createOverlayElement("Panel", "HUDNavigation_aimMarker_" + getUniqueNumberStr()));
88            aimMarker_->setMaterialName("Orxonox/NavCrosshair");
89           
90            background_->addChild(navMarker_);
91            background_->addChild(aimMarker_);
92            background_->addChild(navText_);
93
94            // hide at first
95            this->setVisible(false);
96        }
97
98        XMLPortParam(HUDNavigation, "font",     setFont,     getFont,     xmlElement, mode).defaultValues("Monofur");
99        XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlElement, mode).defaultValues(0.05f);
100        XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlElement, mode)
101            .defaultValues(0.05f);
102        XMLPortParam(HUDNavigation, "aimMarkerSize", setAimMarkerSize, getAimMarkerSize, xmlElement, mode)
103            .defaultValues(0.04f);
104    }
105
106    void HUDNavigation::setFont(const std::string& font)
107    {
108        if (this->navText_ && font != "")
109            this->navText_->setFontName(font);
110    }
111
112    const std::string& HUDNavigation::getFont() const
113    {
114        if (this->navText_)
115            return this->navText_->getFontName();
116        else
117            return blankString;
118    }
119
120    void HUDNavigation::setTextSize(float size)
121    {
122        if (this->navText_ && size >= 0.0f)
123            this->navText_->setCharHeight(size);
124    }
125
126    float HUDNavigation::getTextSize() const
127    {
128        if (this->navText_)
129            return this->navText_->getCharHeight();
130        else
131            return 0.0f;
132    }
133
134    void HUDNavigation::tick(float dt)
135    {
136        if (!Radar::getInstance().getFocus())
137        {
138            this->overlay_->hide();
139            return;
140        }
141        else
142        {
143            this->overlay_->show();
144        }
145
146        // set text
147        int dist = (int) getDist2Focus();
148        navText_->setCaption(convertToString(dist));
149        float textLength = convertToString(dist).size() * navText_->getCharHeight() * 0.3;
150
151        Ogre::Camera* navCam = SpaceShip::getLocalShip()->getCamera()->cam_;
152        Matrix4 transformationMatrix = navCam->getProjectionMatrix() * navCam->getViewMatrix();
153        // transform to screen coordinates
154        Vector3 pos = transformationMatrix * Radar::getInstance().getFocus()->getWorldPosition();
155
156        bool outOfView;
157        if (pos.z > 1.0)
158        {
159            // z > 1.0 means that the object is behind the camera
160            outOfView = true;
161            // we have to switch all coordinates (if you don't know why,
162            // try linear algebra lectures, because I can't explain..)
163            pos.x = -pos.x;
164            pos.y = -pos.y;
165        }
166        else
167            outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0;
168
169        if (outOfView)
170        {
171            // object is not in view
172            aimMarker_->hide();
173
174            if (!wasOutOfView_)
175            {
176                navMarker_->setMaterialName("Orxonox/NavArrows");
177                wasOutOfView_ = true;
178            }
179
180            if (pos.x < pos.y)
181            {
182                if (pos.y > -pos.x)
183                {
184                    // up
185                    float position = pos.x / pos.y + 1.0;
186                    navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 0.0);
187                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
188                    navText_->setLeft((position - textLength) * 0.5);
189                    navText_->setTop(navMarker_->getHeight());
190                }
191                else
192                {
193                    // left
194                    float position = pos.y / pos.x + 1.0;
195                    navMarker_->setPosition(0.0, (position - navMarker_->getWidth()) * 0.5);
196                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
197                    navText_->setLeft(navMarker_->getWidth() + 0.01);
198                    navText_->setTop((position - navText_->getCharHeight()) * 0.5);
199                }
200            }
201            else
202            {
203                if (pos.y < -pos.x)
204                {
205                    // down
206                    float position = -pos.x / pos.y + 1.0;
207                    navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 1.0 - navMarker_->getHeight());
208                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
209                    navText_->setLeft((position - textLength) * 0.5);
210                    navText_->setTop(1.0 - navMarker_->getHeight() - navText_->getCharHeight());
211                }
212                else
213                {
214                    // right
215                    float position = -pos.y / pos.x + 1.0;
216                    navMarker_->setPosition(1.0 - navMarker_->getWidth(), (position - navMarker_->getHeight()) * 0.5);
217                    navMarker_->setUV(0.5, 0.5, 1.0, 1.0);
218                    navText_->setLeft(1.0 - navMarker_->getWidth() - textLength - 0.01);
219                    navText_->setTop((position - navText_->getCharHeight()) * 0.5);
220                }
221            }
222        }
223        else
224        {
225            // object is in view
226
227            Vector3 aimpos = transformationMatrix * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(),
228                    Projectile::getSpeed(), Radar::getInstance().getFocus()->getWorldPosition(), Radar::getInstance().getFocus()->getOrientedVelocity());
229
230            if (wasOutOfView_)
231            {
232                navMarker_->setMaterialName("Orxonox/NavTDC");
233                wasOutOfView_ = false;
234            }
235
236            // object is in view
237            navMarker_->setUV(0.0, 0.0, 1.0, 1.0);
238            navMarker_->setLeft((pos.x + 1.0 - navMarker_->getWidth()) * 0.5);
239            navMarker_->setTop((-pos.y + 1.0 - navMarker_->getHeight()) * 0.5);
240
241            aimMarker_->show();
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        if (Radar::getInstance().getFocus())
253            return (Radar::getInstance().getFocus()->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length();
254        else
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.