Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/modules/overlays/hud/HUDNavigation.cc @ 5813

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

Moved Radar Management from GSLevel to Scene.

  • Property svn:eol-style set to native
File size: 9.8 KB
RevLine 
[1505]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
[1454]4 *
[1505]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 *
[1454]22 *   Author:
23 *      Felix Schulthess
24 *   Co-authors:
[1590]25 *      Reto Grieder
[1454]26 *
27 */
[1393]28
[1601]29#include "HUDNavigation.h"
[1410]30
[3196]31#include <string>
[1393]32#include <OgreOverlayManager.h>
[1614]33#include <OgreTextAreaOverlayElement.h>
34#include <OgrePanelOverlayElement.h>
[1410]35
[1614]36#include "util/Math.h"
[3280]37#include "util/StringUtils.h"
[1616]38#include "util/Convert.h"
39#include "core/CoreIncludes.h"
40#include "core/XMLPort.h"
[5813]41#include "Scene.h"
[5735]42#include "Radar.h"
[1393]43
44namespace orxonox
45{
[1601]46    CreateFactory(HUDNavigation);
[1590]47
[2087]48    HUDNavigation::HUDNavigation(BaseObject* creator)
49        : OrxonoxOverlay(creator)
[1580]50    {
[1601]51        RegisterObject(HUDNavigation);
[2087]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);
[1393]79    }
80
[1601]81    HUDNavigation::~HUDNavigation()
[1564]82    {
[2087]83        if (this->isInitialized())
84        {
[1615]85            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navMarker_);
86            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navText_);
87            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_);
[2087]88        }
[1564]89    }
90
[1601]91    void HUDNavigation::XMLPort(Element& xmlElement, XMLPort::Mode mode)
[1580]92    {
[1747]93        SUPER(HUDNavigation, XMLPort, xmlElement, mode);
[1394]94
[2087]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);
[1410]99    }
[1393]100
[1601]101    void HUDNavigation::setFont(const std::string& font)
[1590]102    {
103        if (this->navText_ && font != "")
104            this->navText_->setFontName(font);
105    }
106
[1615]107    const std::string& HUDNavigation::getFont() const
[1590]108    {
109        if (this->navText_)
110            return this->navText_->getFontName();
111        else
[2087]112            return BLANKSTRING;
[1590]113    }
114
[1601]115    void HUDNavigation::setTextSize(float size)
[1590]116    {
117        if (this->navText_ && size >= 0.0f)
118            this->navText_->setCharHeight(size);
119    }
120
[1601]121    float HUDNavigation::getTextSize() const
[1590]122    {
123        if (this->navText_)
124            return this->navText_->getCharHeight();
125        else
126            return 0.0f;
127    }
128
[1601]129    void HUDNavigation::tick(float dt)
[1590]130    {
[2662]131        SUPER(HUDNavigation, tick, dt);
132
[5813]133        // Get radar
134        Radar* radar = this->getOwner()->getScene()->getRadar();
135
136        if (!radar->getFocus())
[1613]137        {
138            this->overlay_->hide();
[1564]139            return;
[1613]140        }
141        else
142        {
143            this->overlay_->show();
144        }
[1400]145
[1399]146        // set text
[3301]147        int dist = static_cast<int>(getDist2Focus());
[3280]148        navText_->setCaption(multi_cast<std::string>(dist));
149        float textLength = multi_cast<std::string>(dist).size() * navText_->getCharHeight() * 0.3;
[1399]150
[2087]151/*
[1564]152        Ogre::Camera* navCam = SpaceShip::getLocalShip()->getCamera()->cam_;
[1590]153        Matrix4 transformationMatrix = navCam->getProjectionMatrix() * navCam->getViewMatrix();
[2087]154*/
[1399]155        // transform to screen coordinates
[5813]156        Vector3 pos = /*transformationMatrix * */radar->getFocus()->getRVWorldPosition();
[1564]157
[1590]158        bool outOfView;
159        if (pos.z > 1.0)
160        {
161            // z > 1.0 means that the object is behind the camera
[1580]162            outOfView = true;
[1590]163            // we have to switch all coordinates (if you don't know why,
164            // try linear algebra lectures, because I can't explain..)
165            pos.x = -pos.x;
166            pos.y = -pos.y;
167        }
168        else
169            outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0;
[1399]170
[1580]171        if (outOfView)
172        {
[1411]173            // object is not in view
[1566]174            aimMarker_->hide();
[1411]175
[1590]176            if (!wasOutOfView_)
[1580]177            {
[1590]178                navMarker_->setMaterialName("Orxonox/NavArrows");
179                wasOutOfView_ = true;
[1411]180            }
[1590]181
182            if (pos.x < pos.y)
[1580]183            {
[1590]184                if (pos.y > -pos.x)
[1580]185                {
[1590]186                    // up
187                    float position = pos.x / pos.y + 1.0;
188                    navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 0.0);
[1399]189                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
[1590]190                    navText_->setLeft((position - textLength) * 0.5);
[1399]191                    navText_->setTop(navMarker_->getHeight());
192                }
[1580]193                else
194                {
[1590]195                    // left
196                    float position = pos.y / pos.x + 1.0;
197                    navMarker_->setPosition(0.0, (position - navMarker_->getWidth()) * 0.5);
[1411]198                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
[1590]199                    navText_->setLeft(navMarker_->getWidth() + 0.01);
200                    navText_->setTop((position - navText_->getCharHeight()) * 0.5);
[1411]201                }
202            }
[1590]203            else
[1580]204            {
[1590]205                if (pos.y < -pos.x)
[1580]206                {
[1590]207                    // down
208                    float position = -pos.x / pos.y + 1.0;
209                    navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 1.0 - navMarker_->getHeight());
[1399]210                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
[1590]211                    navText_->setLeft((position - textLength) * 0.5);
212                    navText_->setTop(1.0 - navMarker_->getHeight() - navText_->getCharHeight());
[1399]213                }
[1580]214                else
215                {
[1590]216                    // right
217                    float position = -pos.y / pos.x + 1.0;
218                    navMarker_->setPosition(1.0 - navMarker_->getWidth(), (position - navMarker_->getHeight()) * 0.5);
219                    navMarker_->setUV(0.5, 0.5, 1.0, 1.0);
220                    navText_->setLeft(1.0 - navMarker_->getWidth() - textLength - 0.01);
221                    navText_->setTop((position - navText_->getCharHeight()) * 0.5);
[1399]222                }
[1393]223            }
224        }
[1580]225        else
226        {
[1411]227            // object is in view
[2087]228/*
[1590]229            Vector3 aimpos = transformationMatrix * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(),
[2662]230                    Projectile::getSpeed(), Radar::getInstance().getFocus()->getRVWorldPosition(), Radar::getInstance().getFocus()->getRVOrientedVelocity());
[2087]231*/
[1590]232            if (wasOutOfView_)
233            {
234                navMarker_->setMaterialName("Orxonox/NavTDC");
235                wasOutOfView_ = false;
236            }
237
238            // object is in view
[1459]239            navMarker_->setUV(0.0, 0.0, 1.0, 1.0);
[1590]240            navMarker_->setLeft((pos.x + 1.0 - navMarker_->getWidth()) * 0.5);
241            navMarker_->setTop((-pos.y + 1.0 - navMarker_->getHeight()) * 0.5);
[1566]242
243            aimMarker_->show();
[2087]244/*
[1590]245            aimMarker_->setLeft((aimpos.x + 1.0 - aimMarker_->getWidth()) * 0.5);
246            aimMarker_->setTop((-aimpos.y + 1.0 - aimMarker_->getHeight()) * 0.5);
[2087]247*/
[1590]248            navText_->setLeft((pos.x + 1.0 + navMarker_->getWidth()) * 0.5);
249            navText_->setTop((-pos.y + 1.0 + navMarker_->getHeight()) * 0.5);
[1393]250        }
[1410]251    }
[1394]252
[1601]253    float HUDNavigation::getDist2Focus() const
[1580]254    {
[2087]255/*
[1613]256        if (Radar::getInstance().getFocus())
[2662]257            return (Radar::getInstance().getFocus()->getRVWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length();
[1580]258        else
[2087]259*/
[1580]260            return 0;
[1410]261    }
[1590]262
[1604]263    /**
264    @brief Overridden method of OrxonoxOverlay. Usually the entire overlay
265           scales with scale(). Here we obviously have to adjust this.
266    */
[1601]267    void HUDNavigation::sizeChanged()
[1590]268    {
[1604]269        // use size to compensate for apspect ratio if enabled.
[1622]270        float xScale = this->getActualSize().x;
271        float yScale = this->getActualSize().y;
[1595]272        if (this->navMarker_)
273            navMarker_->setDimensions(navMarkerSize_ * xScale, navMarkerSize_ * yScale);
274        if (this->aimMarker_)
275            aimMarker_->setDimensions(aimMarkerSize_ * xScale, aimMarkerSize_ * yScale);
276        if (this->navText_)
277            navText_->setCharHeight(navText_->getCharHeight() * yScale);
[1590]278    }
[1393]279}
Note: See TracBrowser for help on using the repository browser.