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
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
[1410]29#include "OrxonoxStableHeaders.h"
[1601]30#include "HUDNavigation.h"
[1410]31
[1393]32#include <OgreOverlayManager.h>
[1614]33#include <OgreTextAreaOverlayElement.h>
34#include <OgrePanelOverlayElement.h>
[1410]35
[1614]36#include "util/Math.h"
[1615]37#include "util/String.h"
[1616]38#include "util/Convert.h"
[1604]39#include "core/ConsoleCommand.h"
[1616]40#include "core/CoreIncludes.h"
41#include "core/XMLPort.h"
[1819]42#include "objects/Radar.h"
[1399]43#include "objects/CameraHandler.h"
[1393]44
45namespace orxonox
46{
[1601]47    CreateFactory(HUDNavigation);
[1590]48
[1601]49    HUDNavigation::HUDNavigation()
[1615]50        : navMarker_(0)
51        , aimMarker_(0)
52        , navText_(0)
[1580]53    {
[1601]54        RegisterObject(HUDNavigation);
[1393]55    }
56
[1601]57    HUDNavigation::~HUDNavigation()
[1564]58    {
[1615]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_);
[1564]65    }
66
[1601]67    void HUDNavigation::XMLPort(Element& xmlElement, XMLPort::Mode mode)
[1580]68    {
[1747]69        SUPER(HUDNavigation, XMLPort, xmlElement, mode);
[1394]70
[1590]71        if (mode == XMLPort::LoadObject)
72        {
73            // create nav text
[1615]74            navText_ = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton()
75                .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberStr()));
[1590]76
77            // create nav marker
[1615]78            navMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
79                .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberStr()));
[1590]80            navMarker_->setMaterialName("Orxonox/NavArrows");
[1604]81            wasOutOfView_ = true; // just to ensure the material is changed right the first time..
[1590]82
83            // create aim marker
[1615]84            aimMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
85                .createOverlayElement("Panel", "HUDNavigation_aimMarker_" + getUniqueNumberStr()));
[1590]86            aimMarker_->setMaterialName("Orxonox/NavCrosshair");
[1747]87
[1615]88            background_->addChild(navMarker_);
89            background_->addChild(aimMarker_);
90            background_->addChild(navText_);
[1590]91
[1615]92            // hide at first
[1625]93            this->setVisible(false);
[1590]94        }
95
[1627]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);
[1410]102    }
[1393]103
[1601]104    void HUDNavigation::setFont(const std::string& font)
[1590]105    {
106        if (this->navText_ && font != "")
107            this->navText_->setFontName(font);
108    }
109
[1615]110    const std::string& HUDNavigation::getFont() const
[1590]111    {
112        if (this->navText_)
113            return this->navText_->getFontName();
114        else
[1615]115            return blankString;
[1590]116    }
117
[1601]118    void HUDNavigation::setTextSize(float size)
[1590]119    {
120        if (this->navText_ && size >= 0.0f)
121            this->navText_->setCharHeight(size);
122    }
123
[1601]124    float HUDNavigation::getTextSize() const
[1590]125    {
126        if (this->navText_)
127            return this->navText_->getCharHeight();
128        else
129            return 0.0f;
130    }
131
[1601]132    void HUDNavigation::tick(float dt)
[1590]133    {
[1613]134        if (!Radar::getInstance().getFocus())
135        {
136            this->overlay_->hide();
[1564]137            return;
[1613]138        }
139        else
140        {
141            this->overlay_->show();
142        }
[1400]143
[1399]144        // set text
[1613]145        int dist = (int) getDist2Focus();
[1590]146        navText_->setCaption(convertToString(dist));
147        float textLength = convertToString(dist).size() * navText_->getCharHeight() * 0.3;
[1399]148
[1916]149/*
[1564]150        Ogre::Camera* navCam = SpaceShip::getLocalShip()->getCamera()->cam_;
[1590]151        Matrix4 transformationMatrix = navCam->getProjectionMatrix() * navCam->getViewMatrix();
[1916]152*/
[1399]153        // transform to screen coordinates
[1916]154        Vector3 pos = /*transformationMatrix * */Radar::getInstance().getFocus()->getWorldPosition();
[1564]155
[1590]156        bool outOfView;
157        if (pos.z > 1.0)
158        {
159            // z > 1.0 means that the object is behind the camera
[1580]160            outOfView = true;
[1590]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;
[1399]168
[1580]169        if (outOfView)
170        {
[1411]171            // object is not in view
[1566]172            aimMarker_->hide();
[1411]173
[1590]174            if (!wasOutOfView_)
[1580]175            {
[1590]176                navMarker_->setMaterialName("Orxonox/NavArrows");
177                wasOutOfView_ = true;
[1411]178            }
[1590]179
180            if (pos.x < pos.y)
[1580]181            {
[1590]182                if (pos.y > -pos.x)
[1580]183                {
[1590]184                    // up
185                    float position = pos.x / pos.y + 1.0;
186                    navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 0.0);
[1399]187                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
[1590]188                    navText_->setLeft((position - textLength) * 0.5);
[1399]189                    navText_->setTop(navMarker_->getHeight());
190                }
[1580]191                else
192                {
[1590]193                    // left
194                    float position = pos.y / pos.x + 1.0;
195                    navMarker_->setPosition(0.0, (position - navMarker_->getWidth()) * 0.5);
[1411]196                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
[1590]197                    navText_->setLeft(navMarker_->getWidth() + 0.01);
198                    navText_->setTop((position - navText_->getCharHeight()) * 0.5);
[1411]199                }
200            }
[1590]201            else
[1580]202            {
[1590]203                if (pos.y < -pos.x)
[1580]204                {
[1590]205                    // down
206                    float position = -pos.x / pos.y + 1.0;
207                    navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 1.0 - navMarker_->getHeight());
[1399]208                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
[1590]209                    navText_->setLeft((position - textLength) * 0.5);
210                    navText_->setTop(1.0 - navMarker_->getHeight() - navText_->getCharHeight());
[1399]211                }
[1580]212                else
213                {
[1590]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);
[1399]220                }
[1393]221            }
222        }
[1580]223        else
224        {
[1411]225            // object is in view
[1916]226/*
[1590]227            Vector3 aimpos = transformationMatrix * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(),
[1613]228                    Projectile::getSpeed(), Radar::getInstance().getFocus()->getWorldPosition(), Radar::getInstance().getFocus()->getOrientedVelocity());
[1916]229*/
[1590]230            if (wasOutOfView_)
231            {
232                navMarker_->setMaterialName("Orxonox/NavTDC");
233                wasOutOfView_ = false;
234            }
235
236            // object is in view
[1459]237            navMarker_->setUV(0.0, 0.0, 1.0, 1.0);
[1590]238            navMarker_->setLeft((pos.x + 1.0 - navMarker_->getWidth()) * 0.5);
239            navMarker_->setTop((-pos.y + 1.0 - navMarker_->getHeight()) * 0.5);
[1566]240
241            aimMarker_->show();
[1916]242/*
[1590]243            aimMarker_->setLeft((aimpos.x + 1.0 - aimMarker_->getWidth()) * 0.5);
244            aimMarker_->setTop((-aimpos.y + 1.0 - aimMarker_->getHeight()) * 0.5);
[1916]245*/
[1590]246            navText_->setLeft((pos.x + 1.0 + navMarker_->getWidth()) * 0.5);
247            navText_->setTop((-pos.y + 1.0 + navMarker_->getHeight()) * 0.5);
[1393]248        }
[1410]249    }
[1394]250
[1601]251    float HUDNavigation::getDist2Focus() const
[1580]252    {
[1916]253/*
[1613]254        if (Radar::getInstance().getFocus())
255            return (Radar::getInstance().getFocus()->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length();
[1580]256        else
[1916]257*/
[1580]258            return 0;
[1410]259    }
[1590]260
[1604]261    /**
262    @brief Overridden method of OrxonoxOverlay. Usually the entire overlay
263           scales with scale(). Here we obviously have to adjust this.
264    */
[1601]265    void HUDNavigation::sizeChanged()
[1590]266    {
[1604]267        // use size to compensate for apspect ratio if enabled.
[1622]268        float xScale = this->getActualSize().x;
269        float yScale = this->getActualSize().y;
[1595]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);
[1590]276    }
[1393]277}
Note: See TracBrowser for help on using the repository browser.