Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1614 was 1614, checked in by rgrieder, 16 years ago
  • Dots on the Radar actually disappear now when a Ship gets destroyed…
  • svn save to keep History of HUDText when renaming AND moving
  • Property svn:eol-style set to native
File size: 11.0 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 "core/ConsoleCommand.h"
38#include "objects/SpaceShip.h"
39#include "objects/Projectile.h"
40#include "objects/CameraHandler.h"
41#include "Radar.h"
42
43namespace orxonox
44{
45    CreateFactory(HUDNavigation);
46
47    //HUDNavigation* HUDNavigation::instance_s = 0;
48
49    using namespace Ogre;
50
51    HUDNavigation::HUDNavigation()
52      : container_(0)
53      , navMarker_(0)
54      , aimMarker_(0)
55      , navText_(0)
56    {
57        RegisterObject(HUDNavigation);
58       
59        /*assert(instance_s == 0); // singleton class
60        HUDNavigation::instance_s = this;*/
61    }
62
63    HUDNavigation::~HUDNavigation()
64    {
65        if (this->isInitialized())
66        {
67            if (this->navMarker_)
68                OverlayManager::getSingleton().destroyOverlayElement(this->navMarker_);
69            if (this->navText_)
70                OverlayManager::getSingleton().destroyOverlayElement(this->navText_);
71            if (this->aimMarker_)
72                OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_);
73            if (this->container_)
74                OverlayManager::getSingleton().destroyOverlayElement(this->container_);
75        }
76
77        //HUDNavigation::instance_s = 0;
78    }
79
80    void HUDNavigation::XMLPort(Element& xmlElement, XMLPort::Mode mode)
81    {
82        OrxonoxOverlay::XMLPort(xmlElement, mode);
83
84        if (mode == XMLPort::LoadObject)
85        {
86            // create container because we cannot add a Text element to an Overlay
87            container_ = static_cast<OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_navContainer"));
88
89            // create nav text
90            navText_ = static_cast<TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("TextArea", getName() + "_navText"));
91            navText_->setCharHeight(0.05f);
92            navText_->setFontName("Monofur");
93
94            // create nav marker
95            navMarker_ = static_cast<PanelOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_navMarker"));
96            navMarker_->setMaterialName("Orxonox/NavArrows");
97            navMarkerSize_ = 0.05; //default
98            wasOutOfView_ = true; // just to ensure the material is changed right the first time..
99
100            // create aim marker
101            aimMarker_ = static_cast<PanelOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_aimMarker"));
102            aimMarker_->setMaterialName("Orxonox/NavCrosshair");
103            aimMarkerSize_ = 0.04; // default
104           
105            container_->addChild(navMarker_);
106            container_->addChild(aimMarker_);
107            container_->addChild(navText_);
108            container_->show();
109
110            overlay_->add2D(container_);
111        }
112
113        XMLPortParam(HUDNavigation, "font", setFont, getFont, xmlElement, mode);
114        XMLPortParam(HUDNavigation, "textsize", setTextSize, getTextSize, xmlElement, mode);
115        XMLPortParam(HUDNavigation, "navmarkersize", setNavMarkerSize, getNavMarkerSize, xmlElement, mode);
116        XMLPortParam(HUDNavigation, "aimmarkersize", setAimMarkerSize, getAimMarkerSize, xmlElement, mode);
117
118        if (mode == XMLPort::LoadObject)
119        {
120            this->sizeChanged();
121        }
122    }
123
124    void HUDNavigation::setNavMarkerSize(float size)
125    {
126        this->navMarkerSize_ = size;
127    }
128
129    float HUDNavigation::getNavMarkerSize() const
130    {
131        return this->navMarkerSize_;
132    }
133
134    void HUDNavigation::setAimMarkerSize(float size)
135    {
136        this->aimMarkerSize_ = size;
137    }
138
139    float HUDNavigation::getAimMarkerSize() const
140    {
141        return this->aimMarkerSize_;
142    }
143
144    void HUDNavigation::setFont(const std::string& font)
145    {
146        if (this->navText_ && font != "")
147            this->navText_->setFontName(font);
148    }
149
150    std::string HUDNavigation::getFont() const
151    {
152        if (this->navText_)
153            return this->navText_->getFontName();
154        else
155            return "";
156    }
157
158    void HUDNavigation::setTextSize(float size)
159    {
160        if (this->navText_ && size >= 0.0f)
161            this->navText_->setCharHeight(size);
162    }
163
164    float HUDNavigation::getTextSize() const
165    {
166        if (this->navText_)
167            return this->navText_->getCharHeight();
168        else
169            return 0.0f;
170    }
171
172    void HUDNavigation::tick(float dt)
173    {
174        if (!Radar::getInstance().getFocus())
175        {
176            this->overlay_->hide();
177            return;
178        }
179        else
180        {
181            this->overlay_->show();
182        }
183
184        // set text
185        int dist = (int) getDist2Focus();
186        navText_->setCaption(convertToString(dist));
187        float textLength = convertToString(dist).size() * navText_->getCharHeight() * 0.3;
188
189        Ogre::Camera* navCam = SpaceShip::getLocalShip()->getCamera()->cam_;
190        Matrix4 transformationMatrix = navCam->getProjectionMatrix() * navCam->getViewMatrix();
191        // transform to screen coordinates
192        Vector3 pos = transformationMatrix * Radar::getInstance().getFocus()->getWorldPosition();
193
194        bool outOfView;
195        if (pos.z > 1.0)
196        {
197            // z > 1.0 means that the object is behind the camera
198            outOfView = true;
199            // we have to switch all coordinates (if you don't know why,
200            // try linear algebra lectures, because I can't explain..)
201            pos.x = -pos.x;
202            pos.y = -pos.y;
203        }
204        else
205            outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0;
206
207        if (outOfView)
208        {
209            // object is not in view
210            aimMarker_->hide();
211
212            if (!wasOutOfView_)
213            {
214                navMarker_->setMaterialName("Orxonox/NavArrows");
215                wasOutOfView_ = true;
216            }
217
218            if (pos.x < pos.y)
219            {
220                if (pos.y > -pos.x)
221                {
222                    // up
223                    float position = pos.x / pos.y + 1.0;
224                    navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 0.0);
225                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
226                    navText_->setLeft((position - textLength) * 0.5);
227                    navText_->setTop(navMarker_->getHeight());
228                }
229                else
230                {
231                    // left
232                    float position = pos.y / pos.x + 1.0;
233                    navMarker_->setPosition(0.0, (position - navMarker_->getWidth()) * 0.5);
234                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
235                    navText_->setLeft(navMarker_->getWidth() + 0.01);
236                    navText_->setTop((position - navText_->getCharHeight()) * 0.5);
237                }
238            }
239            else
240            {
241                if (pos.y < -pos.x)
242                {
243                    // down
244                    float position = -pos.x / pos.y + 1.0;
245                    navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 1.0 - navMarker_->getHeight());
246                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
247                    navText_->setLeft((position - textLength) * 0.5);
248                    navText_->setTop(1.0 - navMarker_->getHeight() - navText_->getCharHeight());
249                }
250                else
251                {
252                    // right
253                    float position = -pos.y / pos.x + 1.0;
254                    navMarker_->setPosition(1.0 - navMarker_->getWidth(), (position - navMarker_->getHeight()) * 0.5);
255                    navMarker_->setUV(0.5, 0.5, 1.0, 1.0);
256                    navText_->setLeft(1.0 - navMarker_->getWidth() - textLength - 0.01);
257                    navText_->setTop((position - navText_->getCharHeight()) * 0.5);
258                }
259            }
260        }
261        else
262        {
263            // object is in view
264
265            Vector3 aimpos = transformationMatrix * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(),
266                    Projectile::getSpeed(), Radar::getInstance().getFocus()->getWorldPosition(), Radar::getInstance().getFocus()->getOrientedVelocity());
267
268            if (wasOutOfView_)
269            {
270                navMarker_->setMaterialName("Orxonox/NavTDC");
271                wasOutOfView_ = false;
272            }
273
274            // object is in view
275            navMarker_->setUV(0.0, 0.0, 1.0, 1.0);
276            navMarker_->setLeft((pos.x + 1.0 - navMarker_->getWidth()) * 0.5);
277            navMarker_->setTop((-pos.y + 1.0 - navMarker_->getHeight()) * 0.5);
278
279            aimMarker_->show();
280            aimMarker_->setLeft((aimpos.x + 1.0 - aimMarker_->getWidth()) * 0.5);
281            aimMarker_->setTop((-aimpos.y + 1.0 - aimMarker_->getHeight()) * 0.5);
282
283            navText_->setLeft((pos.x + 1.0 + navMarker_->getWidth()) * 0.5);
284            navText_->setTop((-pos.y + 1.0 + navMarker_->getHeight()) * 0.5);
285        }
286    }
287
288    float HUDNavigation::getDist2Focus() const
289    {
290        if (Radar::getInstance().getFocus())
291            return (Radar::getInstance().getFocus()->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length();
292        else
293            return 0;
294    }
295
296    /**
297    @brief Overridden method of OrxonoxOverlay. Usually the entire overlay
298           scales with scale(). Here we obviously have to adjust this.
299    */
300    void HUDNavigation::sizeChanged()
301    {
302        // use size to compensate for apspect ratio if enabled.
303        float xScale = this->getSize().x;
304        float yScale = this->getSize().y;
305        if (this->navMarker_)
306            navMarker_->setDimensions(navMarkerSize_ * xScale, navMarkerSize_ * yScale);
307        if (this->aimMarker_)
308            aimMarker_->setDimensions(aimMarkerSize_ * xScale, aimMarkerSize_ * yScale);
309        if (this->navText_)
310            navText_->setCharHeight(navText_->getCharHeight() * yScale);
311    }
312
313    /*static*/ /*HUDNavigation& HUDNavigation::getInstance()
314    {
315        assert(instance_s);
316        return *instance_s;
317    }*/
318}
Note: See TracBrowser for help on using the repository browser.