Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1916 was 1916, checked in by landauf, 16 years ago

removed WorldEntity, SpaceShip and several other objects
removed SpaceShip-related hacks in network and other places

  • 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/Radar.h"
43#include "objects/CameraHandler.h"
44
45namespace orxonox
46{
47    CreateFactory(HUDNavigation);
48
49    HUDNavigation::HUDNavigation()
50        : navMarker_(0)
51        , aimMarker_(0)
52        , navText_(0)
53    {
54        RegisterObject(HUDNavigation);
55    }
56
57    HUDNavigation::~HUDNavigation()
58    {
59        if (this->navMarker_)
60            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navMarker_);
61        if (this->navText_)
62            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navText_);
63        if (this->aimMarker_)
64            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_);
65    }
66
67    void HUDNavigation::XMLPort(Element& xmlElement, XMLPort::Mode mode)
68    {
69        SUPER(HUDNavigation, XMLPort, xmlElement, mode);
70
71        if (mode == XMLPort::LoadObject)
72        {
73            // create nav text
74            navText_ = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton()
75                .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberStr()));
76
77            // create nav marker
78            navMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
79                .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberStr()));
80            navMarker_->setMaterialName("Orxonox/NavArrows");
81            wasOutOfView_ = true; // just to ensure the material is changed right the first time..
82
83            // create aim marker
84            aimMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
85                .createOverlayElement("Panel", "HUDNavigation_aimMarker_" + getUniqueNumberStr()));
86            aimMarker_->setMaterialName("Orxonox/NavCrosshair");
87
88            background_->addChild(navMarker_);
89            background_->addChild(aimMarker_);
90            background_->addChild(navText_);
91
92            // hide at first
93            this->setVisible(false);
94        }
95
96        XMLPortParam(HUDNavigation, "font",     setFont,     getFont,     xmlElement, mode).defaultValues("Monofur");
97        XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlElement, mode).defaultValues(0.05f);
98        XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlElement, mode)
99            .defaultValues(0.05f);
100        XMLPortParam(HUDNavigation, "aimMarkerSize", setAimMarkerSize, getAimMarkerSize, xmlElement, mode)
101            .defaultValues(0.04f);
102    }
103
104    void HUDNavigation::setFont(const std::string& font)
105    {
106        if (this->navText_ && font != "")
107            this->navText_->setFontName(font);
108    }
109
110    const std::string& HUDNavigation::getFont() const
111    {
112        if (this->navText_)
113            return this->navText_->getFontName();
114        else
115            return blankString;
116    }
117
118    void HUDNavigation::setTextSize(float size)
119    {
120        if (this->navText_ && size >= 0.0f)
121            this->navText_->setCharHeight(size);
122    }
123
124    float HUDNavigation::getTextSize() const
125    {
126        if (this->navText_)
127            return this->navText_->getCharHeight();
128        else
129            return 0.0f;
130    }
131
132    void HUDNavigation::tick(float dt)
133    {
134        if (!Radar::getInstance().getFocus())
135        {
136            this->overlay_->hide();
137            return;
138        }
139        else
140        {
141            this->overlay_->show();
142        }
143
144        // set text
145        int dist = (int) getDist2Focus();
146        navText_->setCaption(convertToString(dist));
147        float textLength = convertToString(dist).size() * navText_->getCharHeight() * 0.3;
148
149/*
150        Ogre::Camera* navCam = SpaceShip::getLocalShip()->getCamera()->cam_;
151        Matrix4 transformationMatrix = navCam->getProjectionMatrix() * navCam->getViewMatrix();
152*/
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/*
243            aimMarker_->setLeft((aimpos.x + 1.0 - aimMarker_->getWidth()) * 0.5);
244            aimMarker_->setTop((-aimpos.y + 1.0 - aimMarker_->getHeight()) * 0.5);
245*/
246            navText_->setLeft((pos.x + 1.0 + navMarker_->getWidth()) * 0.5);
247            navText_->setTop((-pos.y + 1.0 + navMarker_->getHeight()) * 0.5);
248        }
249    }
250
251    float HUDNavigation::getDist2Focus() const
252    {
253/*
254        if (Radar::getInstance().getFocus())
255            return (Radar::getInstance().getFocus()->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length();
256        else
257*/
258            return 0;
259    }
260
261    /**
262    @brief Overridden method of OrxonoxOverlay. Usually the entire overlay
263           scales with scale(). Here we obviously have to adjust this.
264    */
265    void HUDNavigation::sizeChanged()
266    {
267        // use size to compensate for apspect ratio if enabled.
268        float xScale = this->getActualSize().x;
269        float yScale = this->getActualSize().y;
270        if (this->navMarker_)
271            navMarker_->setDimensions(navMarkerSize_ * xScale, navMarkerSize_ * yScale);
272        if (this->aimMarker_)
273            aimMarker_->setDimensions(aimMarkerSize_ * xScale, aimMarkerSize_ * yScale);
274        if (this->navText_)
275            navText_->setCharHeight(navText_->getCharHeight() * yScale);
276    }
277}
Note: See TracBrowser for help on using the repository browser.