Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/orxonox/hud/Navigation.cc @ 1410

Last change on this file since 1410 was 1410, checked in by rgrieder, 16 years ago
  • converted tabs to spaces in HUD
  • adjusted msvc project
File size: 7.7 KB
RevLine 
[1393]1/*
[1394]2*   ORXONOX - the hotnavText_ 3D action shooter ever to exist
[1393]3*
4*
5*   License notice:
6*
7*   This program is free software; you can redistribute it and/or
8*   modify it under the terms of the GNU General Public License
9*   as published by the Free Software Foundation; either version 2
10*   of the License, or (at your option) any later version.
11*
12*   This program is distributed in the hope that it will be useful,
13*   but WITHOUT ANY WARRANTY; without even the implied warranty of
14*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*   GNU General Public License for more details.
16*
17*   You should have received a copy of the GNU General Public License
18*   along with this program; if not, write to the Free Software
19*   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20*
21*   Author:
22*      Felix Schulthess
23*   Co-authors:
24*      ...
25*
26*/
27
[1410]28#include "OrxonoxStableHeaders.h"
29#include "Navigation.h"
30
[1393]31#include <OgreOverlayManager.h>
[1395]32#include <OgreStringConverter.h>
[1410]33
34#include "GraphicsEngine.h"
35// TODO: remove the SpaceShip and CameraHandler dependencies
[1393]36#include "objects/SpaceShip.h"
[1399]37#include "objects/CameraHandler.h"
[1410]38#include "RadarObject.h"
39#include "RadarOverlayElement.h"
[1393]40#include "HUD.h"
41
42namespace orxonox
43{
44    using namespace Ogre;
45
46    Navigation::Navigation(OverlayContainer* container){
47        container_ = container;
48        focus_ = NULL;
49        init();
50    }
51
52    Navigation::Navigation(OverlayContainer* container, RadarObject* focus){
53        container_ = container;
54        focus_ = focus;
55        init();
56    }
57
58    void Navigation::init(){
[1410]59        om = &OverlayManager::getSingleton();
60        navCam_ = NULL;
[1394]61        // create nav text
62        navText_ = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "navText"));
63        navText_->show();
[1399]64        navText_->setMetricsMode(Ogre::GMM_PIXELS);
65        navText_->setDimensions(0.001, 0.001);
[1394]66        navText_->setPosition(0.02, 0.02);
67        navText_->setFontName("Console");
[1399]68        navText_->setCharHeight(20);
[1396]69        navText_->setCaption("");
[1394]70        container_->addChild(navText_);
71
72
[1393]73        // create nav marker ...
74        navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker"));
75        navMarker_->setMetricsMode(GMM_PIXELS);
76        navMarker_->hide();
[1399]77        navText_->hide();
[1393]78        container_->addChild(navMarker_);
[1410]79    }
[1393]80
[1410]81    void Navigation::update(){
[1393]82        if(focus_ == NULL) return;
[1400]83        navCamPos_ = SpaceShip::getLocalShip()->getPosition();
84        currentDir_ = SpaceShip::getLocalShip()->getDir();
[1410]85        currentOrth_ = SpaceShip::getLocalShip()->getOrth();
[1400]86
[1393]87        windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
88        windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
[1399]89        updateMarker();
90    }
91
92    void Navigation::updateMarker(){
93        // set text
94        int dist = (float)(getDist2Focus()/100);
95        navText_->setCaption(Ogre::StringConverter::toString(dist));
96
[1400]97        if(navCam_ == NULL) navCam_ = SpaceShip::getLocalShip()->getCamera()->cam_;
[1399]98        Vector3 pos = focus_->pos_;
99        // transform to screen coordinates
[1400]100        pos = navCam_->getProjectionMatrix()*navCam_->getViewMatrix()*pos;
[1399]101        float xPosRel = 0.5*pos.x+0.5;
102        float yPosRel = 1-(0.5*pos.y+0.5);
103        int xPos = xPosRel*windowW_;
104        int yPos = yPosRel*windowH_;
105        // is object in view?
106        bool outOfView = (xPosRel<0 || xPosRel>1 || yPosRel<0 || yPosRel>1);
107        // if object is behind us, it is out of view anyway:
[1400]108        if(!outOfView && RadarOverlayElement::calcRadius(navCamPos_, currentDir_, currentOrth_, focus_)>3.14/2) outOfView = true;
[1399]109
110        if(outOfView){
111            // NO!
112            navMarker_->setMaterialName("Orxonox/NavArrows");
113            navMarker_->setDimensions(16,16);
114            float phiUpRight = atan((float)(windowW_)/(float)(windowH_));
115            // from the angle we find out where to draw the marker
116            // and which of the four arrows to take
[1400]117            float phiNav = RadarOverlayElement::calcPhi(navCamPos_, currentDir_, currentOrth_, focus_);
118            bool right = RadarOverlayElement::calcRight(navCamPos_, currentDir_, currentOrth_, focus_);
119            if(right){
120                if(phiNav<phiUpRight){
[1399]121                    // arrow up
[1400]122                    navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, 0);
[1399]123                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
124                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
125                    navText_->setTop(navMarker_->getHeight());
126                }
[1400]127                else if(phiNav>3.14-phiUpRight){
[1399]128                    // arrow down
[1400]129                    navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, windowH_-16);
[1399]130                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
131                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
132                    navText_->setTop(navMarker_->getTop()-navMarker_->getHeight());
133                }
134                else {
135                    // arrow right
[1400]136                    navMarker_->setPosition(windowW_-16, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
[1399]137                    navMarker_->setUV(0.5, 0.5, 1.0, 1.0);
138                    navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth());
139                    navText_->setTop(navMarker_->getTop()+navMarker_->getHeight());
140                }
[1393]141            }
[1399]142            else{
[1400]143                if(phiNav<phiUpRight) {
[1399]144                    // arrow up
[1400]145                    navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0);
[1399]146                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
147                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
148                    navText_->setTop(navMarker_->getHeight());
149                }
[1400]150                else if(phiNav>3.14-phiUpRight) {
[1399]151                    // arrow down
[1400]152                    navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16);
[1399]153                    navMarker_->setUV(0.0, 0.5, 0.5, 1.0);
154                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
155                    navText_->setTop(navMarker_->getTop()-navMarker_->getHeight());
156                }
157                else {
158                    // arrow left
[1400]159                    navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
[1399]160                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
161                    navText_->setLeft(navMarker_->getWidth());
162                    navText_->setTop(navMarker_->getTop()+navMarker_->getHeight());
163                }
[1393]164            }
165        }
166        else{
[1399]167            // YES!
168            navMarker_->setMaterialName("Orxonox/NavTDC");
169            navMarker_->setDimensions(24,24);
170            navMarker_->setUV(0.0,0.0,1.0,1.0);
171            navMarker_->setPosition(xPos-navMarker_->getWidth()/2, yPos-navMarker_->getHeight()/2);
172            navText_->setPosition(xPos+navMarker_->getWidth()/2, yPos+navMarker_->getHeight()/2);
[1393]173        }
[1410]174    }
[1394]175
[1393]176    void Navigation::cycleFocus(){
[1410]177        if(focus_ == NULL){
[1393]178            focus_ = HUD::getSingleton().getFirstRadarObject();
[1410]179        }
[1393]180        else{
181            focus_->panel_->setMaterialName("Orxonox/RedDot");
[1399]182            if(focus_ != NULL) focus_ = focus_->next;
[1393]183        }
184
185        if(focus_ == NULL){
186            navMarker_->hide();
[1399]187            navText_->hide();
[1393]188        }
189        else{
190            navMarker_->show();
[1399]191            navText_->show();
[1393]192            focus_->panel_->setMaterialName("Orxonox/WhiteDot");
193        }
[1410]194    }
[1393]195
[1410]196    float Navigation::getDist2Focus(){
197        if(focus_ == NULL) return(0.0);
198        return((focus_->pos_-SpaceShip::getLocalShip()->getPosition()).length());
199    }
[1393]200}
Note: See TracBrowser for help on using the repository browser.