[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 | |
---|
| 28 | #include <OgreOverlayManager.h> |
---|
[1399] | 29 | #include <OgrePanelOverlayElement.h> |
---|
| 30 | #include <OgreTextAreaOverlayElement.h> |
---|
[1395] | 31 | #include <OgreStringConverter.h> |
---|
[1393] | 32 | #include <GraphicsEngine.h> |
---|
| 33 | #include "objects/SpaceShip.h" |
---|
[1399] | 34 | #include "objects/CameraHandler.h" |
---|
[1393] | 35 | #include "HUD.h" |
---|
| 36 | #include "Navigation.h" |
---|
| 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
| 40 | using namespace Ogre; |
---|
| 41 | |
---|
| 42 | Navigation::Navigation(OverlayContainer* container){ |
---|
| 43 | container_ = container; |
---|
| 44 | focus_ = NULL; |
---|
| 45 | init(); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | Navigation::Navigation(OverlayContainer* container, RadarObject* focus){ |
---|
| 49 | container_ = container; |
---|
| 50 | focus_ = focus; |
---|
| 51 | init(); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | void Navigation::init(){ |
---|
| 55 | om = &OverlayManager::getSingleton(); |
---|
[1400] | 56 | navCam_ = NULL; |
---|
[1394] | 57 | // create nav text |
---|
| 58 | navText_ = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "navText")); |
---|
| 59 | navText_->show(); |
---|
[1399] | 60 | navText_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
| 61 | navText_->setDimensions(0.001, 0.001); |
---|
[1394] | 62 | navText_->setPosition(0.02, 0.02); |
---|
| 63 | navText_->setFontName("Console"); |
---|
[1399] | 64 | navText_->setCharHeight(20); |
---|
[1396] | 65 | navText_->setCaption(""); |
---|
[1394] | 66 | container_->addChild(navText_); |
---|
| 67 | |
---|
| 68 | |
---|
[1393] | 69 | // create nav marker ... |
---|
| 70 | navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker")); |
---|
| 71 | navMarker_->setMetricsMode(GMM_PIXELS); |
---|
| 72 | navMarker_->hide(); |
---|
[1399] | 73 | navText_->hide(); |
---|
[1393] | 74 | container_->addChild(navMarker_); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void Navigation::update(){ |
---|
| 78 | if(focus_ == NULL) return; |
---|
[1400] | 79 | navCamPos_ = SpaceShip::getLocalShip()->getPosition(); |
---|
| 80 | currentDir_ = SpaceShip::getLocalShip()->getDir(); |
---|
| 81 | currentOrth_ = SpaceShip::getLocalShip()->getOrth(); |
---|
| 82 | |
---|
[1393] | 83 | windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
| 84 | windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
[1399] | 85 | updateMarker(); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | void Navigation::updateMarker(){ |
---|
| 89 | // set text |
---|
| 90 | int dist = (float)(getDist2Focus()/100); |
---|
| 91 | navText_->setCaption(Ogre::StringConverter::toString(dist)); |
---|
| 92 | |
---|
[1400] | 93 | if(navCam_ == NULL) navCam_ = SpaceShip::getLocalShip()->getCamera()->cam_; |
---|
[1399] | 94 | Vector3 pos = focus_->pos_; |
---|
| 95 | // transform to screen coordinates |
---|
[1400] | 96 | pos = navCam_->getProjectionMatrix()*navCam_->getViewMatrix()*pos; |
---|
[1399] | 97 | float xPosRel = 0.5*pos.x+0.5; |
---|
| 98 | float yPosRel = 1-(0.5*pos.y+0.5); |
---|
| 99 | int xPos = xPosRel*windowW_; |
---|
| 100 | int yPos = yPosRel*windowH_; |
---|
| 101 | // is object in view? |
---|
| 102 | bool outOfView = (xPosRel<0 || xPosRel>1 || yPosRel<0 || yPosRel>1); |
---|
| 103 | // if object is behind us, it is out of view anyway: |
---|
[1400] | 104 | if(!outOfView && RadarOverlayElement::calcRadius(navCamPos_, currentDir_, currentOrth_, focus_)>3.14/2) outOfView = true; |
---|
[1399] | 105 | |
---|
| 106 | if(outOfView){ |
---|
| 107 | // NO! |
---|
| 108 | navMarker_->setMaterialName("Orxonox/NavArrows"); |
---|
| 109 | navMarker_->setDimensions(16,16); |
---|
| 110 | float phiUpRight = atan((float)(windowW_)/(float)(windowH_)); |
---|
| 111 | // from the angle we find out where to draw the marker |
---|
| 112 | // and which of the four arrows to take |
---|
[1400] | 113 | float phiNav = RadarOverlayElement::calcPhi(navCamPos_, currentDir_, currentOrth_, focus_); |
---|
| 114 | bool right = RadarOverlayElement::calcRight(navCamPos_, currentDir_, currentOrth_, focus_); |
---|
| 115 | if(right){ |
---|
| 116 | if(phiNav<phiUpRight){ |
---|
[1399] | 117 | // arrow up |
---|
[1400] | 118 | navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, 0); |
---|
[1399] | 119 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
---|
| 120 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 121 | navText_->setTop(navMarker_->getHeight()); |
---|
| 122 | } |
---|
[1400] | 123 | else if(phiNav>3.14-phiUpRight){ |
---|
[1399] | 124 | // arrow down |
---|
[1400] | 125 | navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, windowH_-16); |
---|
[1399] | 126 | navMarker_->setUV(0.0, 0.5, 0.5, 1.0); |
---|
| 127 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 128 | navText_->setTop(navMarker_->getTop()-navMarker_->getHeight()); |
---|
| 129 | } |
---|
| 130 | else { |
---|
| 131 | // arrow right |
---|
[1400] | 132 | navMarker_->setPosition(windowW_-16, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); |
---|
[1399] | 133 | navMarker_->setUV(0.5, 0.5, 1.0, 1.0); |
---|
| 134 | navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth()); |
---|
| 135 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 136 | } |
---|
[1393] | 137 | } |
---|
[1399] | 138 | else{ |
---|
[1400] | 139 | if(phiNav<phiUpRight) { |
---|
[1399] | 140 | // arrow up |
---|
[1400] | 141 | navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0); |
---|
[1399] | 142 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
---|
| 143 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 144 | navText_->setTop(navMarker_->getHeight()); |
---|
| 145 | } |
---|
[1400] | 146 | else if(phiNav>3.14-phiUpRight) { |
---|
[1399] | 147 | // arrow down |
---|
[1400] | 148 | navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16); |
---|
[1399] | 149 | navMarker_->setUV(0.0, 0.5, 0.5, 1.0); |
---|
| 150 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 151 | navText_->setTop(navMarker_->getTop()-navMarker_->getHeight()); |
---|
| 152 | } |
---|
| 153 | else { |
---|
| 154 | // arrow left |
---|
[1400] | 155 | navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); |
---|
[1399] | 156 | navMarker_->setUV(0.0, 0.0, 0.5, 0.5); |
---|
| 157 | navText_->setLeft(navMarker_->getWidth()); |
---|
| 158 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 159 | } |
---|
[1393] | 160 | } |
---|
| 161 | } |
---|
| 162 | else{ |
---|
[1399] | 163 | // YES! |
---|
| 164 | navMarker_->setMaterialName("Orxonox/NavTDC"); |
---|
| 165 | navMarker_->setDimensions(24,24); |
---|
| 166 | navMarker_->setUV(0.0,0.0,1.0,1.0); |
---|
| 167 | navMarker_->setPosition(xPos-navMarker_->getWidth()/2, yPos-navMarker_->getHeight()/2); |
---|
| 168 | navText_->setPosition(xPos+navMarker_->getWidth()/2, yPos+navMarker_->getHeight()/2); |
---|
[1393] | 169 | } |
---|
[1399] | 170 | } |
---|
[1394] | 171 | |
---|
[1393] | 172 | void Navigation::cycleFocus(){ |
---|
| 173 | if(focus_ == NULL){ |
---|
| 174 | focus_ = HUD::getSingleton().getFirstRadarObject(); |
---|
| 175 | } |
---|
| 176 | else{ |
---|
| 177 | focus_->panel_->setMaterialName("Orxonox/RedDot"); |
---|
[1399] | 178 | if(focus_ != NULL) focus_ = focus_->next; |
---|
[1393] | 179 | } |
---|
| 180 | |
---|
| 181 | if(focus_ == NULL){ |
---|
| 182 | navMarker_->hide(); |
---|
[1399] | 183 | navText_->hide(); |
---|
[1393] | 184 | } |
---|
| 185 | else{ |
---|
| 186 | navMarker_->show(); |
---|
[1399] | 187 | navText_->show(); |
---|
[1393] | 188 | focus_->panel_->setMaterialName("Orxonox/WhiteDot"); |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | float Navigation::getDist2Focus(){ |
---|
| 193 | if(focus_ == NULL) return(0.0); |
---|
[1400] | 194 | return((focus_->pos_-SpaceShip::getLocalShip()->getPosition()).length()); |
---|
[1393] | 195 | } |
---|
| 196 | } |
---|