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
Line 
1/*
2*   ORXONOX - the hotnavText_ 3D action shooter ever to exist
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
28#include "OrxonoxStableHeaders.h"
29#include "Navigation.h"
30
31#include <OgreOverlayManager.h>
32#include <OgreStringConverter.h>
33
34#include "GraphicsEngine.h"
35// TODO: remove the SpaceShip and CameraHandler dependencies
36#include "objects/SpaceShip.h"
37#include "objects/CameraHandler.h"
38#include "RadarObject.h"
39#include "RadarOverlayElement.h"
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(){
59        om = &OverlayManager::getSingleton();
60        navCam_ = NULL;
61        // create nav text
62        navText_ = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "navText"));
63        navText_->show();
64        navText_->setMetricsMode(Ogre::GMM_PIXELS);
65        navText_->setDimensions(0.001, 0.001);
66        navText_->setPosition(0.02, 0.02);
67        navText_->setFontName("Console");
68        navText_->setCharHeight(20);
69        navText_->setCaption("");
70        container_->addChild(navText_);
71
72
73        // create nav marker ...
74        navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker"));
75        navMarker_->setMetricsMode(GMM_PIXELS);
76        navMarker_->hide();
77        navText_->hide();
78        container_->addChild(navMarker_);
79    }
80
81    void Navigation::update(){
82        if(focus_ == NULL) return;
83        navCamPos_ = SpaceShip::getLocalShip()->getPosition();
84        currentDir_ = SpaceShip::getLocalShip()->getDir();
85        currentOrth_ = SpaceShip::getLocalShip()->getOrth();
86
87        windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
88        windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
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
97        if(navCam_ == NULL) navCam_ = SpaceShip::getLocalShip()->getCamera()->cam_;
98        Vector3 pos = focus_->pos_;
99        // transform to screen coordinates
100        pos = navCam_->getProjectionMatrix()*navCam_->getViewMatrix()*pos;
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:
108        if(!outOfView && RadarOverlayElement::calcRadius(navCamPos_, currentDir_, currentOrth_, focus_)>3.14/2) outOfView = true;
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
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){
121                    // arrow up
122                    navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, 0);
123                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
124                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
125                    navText_->setTop(navMarker_->getHeight());
126                }
127                else if(phiNav>3.14-phiUpRight){
128                    // arrow down
129                    navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, windowH_-16);
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
136                    navMarker_->setPosition(windowW_-16, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
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                }
141            }
142            else{
143                if(phiNav<phiUpRight) {
144                    // arrow up
145                    navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0);
146                    navMarker_->setUV(0.5, 0.0, 1.0, 0.5);
147                    navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth());
148                    navText_->setTop(navMarker_->getHeight());
149                }
150                else if(phiNav>3.14-phiUpRight) {
151                    // arrow down
152                    navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16);
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
159                    navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);
160                    navMarker_->setUV(0.0, 0.0, 0.5, 0.5);
161                    navText_->setLeft(navMarker_->getWidth());
162                    navText_->setTop(navMarker_->getTop()+navMarker_->getHeight());
163                }
164            }
165        }
166        else{
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);
173        }
174    }
175
176    void Navigation::cycleFocus(){
177        if(focus_ == NULL){
178            focus_ = HUD::getSingleton().getFirstRadarObject();
179        }
180        else{
181            focus_->panel_->setMaterialName("Orxonox/RedDot");
182            if(focus_ != NULL) focus_ = focus_->next;
183        }
184
185        if(focus_ == NULL){
186            navMarker_->hide();
187            navText_->hide();
188        }
189        else{
190            navMarker_->show();
191            navText_->show();
192            focus_->panel_->setMaterialName("Orxonox/WhiteDot");
193        }
194    }
195
196    float Navigation::getDist2Focus(){
197        if(focus_ == NULL) return(0.0);
198        return((focus_->pos_-SpaceShip::getLocalShip()->getPosition()).length());
199    }
200}
Note: See TracBrowser for help on using the repository browser.