Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hudelements/src/modules/overlays/hud/HUDNavigation.cc @ 6849

Last change on this file since 6849 was 6849, checked in by scurcio, 14 years ago

update hudnavigation /addObject, removeObject

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