| 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 <OgreOverlayManager.h> |
|---|
| 29 | #include <OgrePanelOverlayElement.h> |
|---|
| 30 | #include <OgreTextAreaOverlayElement.h> |
|---|
| 31 | #include <OgreStringConverter.h> |
|---|
| 32 | #include <GraphicsEngine.h> |
|---|
| 33 | #include "objects/SpaceShip.h" |
|---|
| 34 | #include "objects/CameraHandler.h" |
|---|
| 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(); |
|---|
| 56 | navCam_ = NULL; |
|---|
| 57 | // create nav text |
|---|
| 58 | navText_ = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "navText")); |
|---|
| 59 | navText_->show(); |
|---|
| 60 | navText_->setMetricsMode(Ogre::GMM_PIXELS); |
|---|
| 61 | navText_->setDimensions(0.001, 0.001); |
|---|
| 62 | navText_->setPosition(0.02, 0.02); |
|---|
| 63 | navText_->setFontName("Console"); |
|---|
| 64 | navText_->setCharHeight(20); |
|---|
| 65 | navText_->setCaption(""); |
|---|
| 66 | container_->addChild(navText_); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | // create nav marker ... |
|---|
| 70 | navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker")); |
|---|
| 71 | navMarker_->setMetricsMode(GMM_PIXELS); |
|---|
| 72 | navMarker_->hide(); |
|---|
| 73 | navText_->hide(); |
|---|
| 74 | container_->addChild(navMarker_); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void Navigation::update(){ |
|---|
| 78 | if(focus_ == NULL) return; |
|---|
| 79 | navCamPos_ = SpaceShip::getLocalShip()->getPosition(); |
|---|
| 80 | currentDir_ = SpaceShip::getLocalShip()->getDir(); |
|---|
| 81 | currentOrth_ = SpaceShip::getLocalShip()->getOrth(); |
|---|
| 82 | |
|---|
| 83 | windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
|---|
| 84 | windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
|---|
| 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 | |
|---|
| 93 | if(navCam_ == NULL) navCam_ = SpaceShip::getLocalShip()->getCamera()->cam_; |
|---|
| 94 | Vector3 pos = focus_->pos_; |
|---|
| 95 | // transform to screen coordinates |
|---|
| 96 | pos = navCam_->getProjectionMatrix()*navCam_->getViewMatrix()*pos; |
|---|
| 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: |
|---|
| 104 | if(!outOfView && RadarOverlayElement::calcRadius(navCamPos_, currentDir_, currentOrth_, focus_)>3.14/2) outOfView = true; |
|---|
| 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 |
|---|
| 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){ |
|---|
| 117 | // arrow up |
|---|
| 118 | navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, 0); |
|---|
| 119 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
|---|
| 120 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
|---|
| 121 | navText_->setTop(navMarker_->getHeight()); |
|---|
| 122 | } |
|---|
| 123 | else if(phiNav>3.14-phiUpRight){ |
|---|
| 124 | // arrow down |
|---|
| 125 | navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, windowH_-16); |
|---|
| 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 |
|---|
| 132 | navMarker_->setPosition(windowW_-16, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); |
|---|
| 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 | } |
|---|
| 137 | } |
|---|
| 138 | else{ |
|---|
| 139 | if(phiNav<phiUpRight) { |
|---|
| 140 | // arrow up |
|---|
| 141 | navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0); |
|---|
| 142 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
|---|
| 143 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
|---|
| 144 | navText_->setTop(navMarker_->getHeight()); |
|---|
| 145 | } |
|---|
| 146 | else if(phiNav>3.14-phiUpRight) { |
|---|
| 147 | // arrow down |
|---|
| 148 | navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16); |
|---|
| 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 |
|---|
| 155 | navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); |
|---|
| 156 | navMarker_->setUV(0.0, 0.0, 0.5, 0.5); |
|---|
| 157 | navText_->setLeft(navMarker_->getWidth()); |
|---|
| 158 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | else{ |
|---|
| 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); |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | void Navigation::cycleFocus(){ |
|---|
| 173 | if(focus_ == NULL){ |
|---|
| 174 | focus_ = HUD::getSingleton().getFirstRadarObject(); |
|---|
| 175 | } |
|---|
| 176 | else{ |
|---|
| 177 | focus_->panel_->setMaterialName("Orxonox/RedDot"); |
|---|
| 178 | if(focus_ != NULL) focus_ = focus_->next; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | if(focus_ == NULL){ |
|---|
| 182 | navMarker_->hide(); |
|---|
| 183 | navText_->hide(); |
|---|
| 184 | } |
|---|
| 185 | else{ |
|---|
| 186 | navMarker_->show(); |
|---|
| 187 | navText_->show(); |
|---|
| 188 | focus_->panel_->setMaterialName("Orxonox/WhiteDot"); |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | float Navigation::getDist2Focus(){ |
|---|
| 193 | if(focus_ == NULL) return(0.0); |
|---|
| 194 | return((focus_->pos_-SpaceShip::getLocalShip()->getPosition()).length()); |
|---|
| 195 | } |
|---|
| 196 | } |
|---|