Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/modules/overlays/hud/HUDNavigation.cc @ 6273

Last change on this file since 6273 was 6273, checked in by scheusso, 14 years ago

radar working again (also focus incl. cycleNavigationFocus)

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