| 1 | /* | 
|---|
| 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 |  *                    > www.orxonox.net < | 
|---|
| 4 |  * | 
|---|
| 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 |  * | 
|---|
| 22 |  *   Author: | 
|---|
| 23 |  *      Felix Schulthess | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "OrxonoxStableHeaders.h" | 
|---|
| 30 | #include "Navigation.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include <OgreOverlayManager.h> | 
|---|
| 33 | #include <OgreStringConverter.h> | 
|---|
| 34 |  | 
|---|
| 35 | #include "GraphicsEngine.h" | 
|---|
| 36 | // TODO: remove the SpaceShip and CameraHandler dependencies | 
|---|
| 37 | #include "objects/SpaceShip.h" | 
|---|
| 38 | #include "objects/CameraHandler.h" | 
|---|
| 39 | #include "RadarObject.h" | 
|---|
| 40 | #include "RadarOverlayElement.h" | 
|---|
| 41 | #include "HUD.h" | 
|---|
| 42 | #include "core/Debug.h" | 
|---|
| 43 |  | 
|---|
| 44 | namespace orxonox | 
|---|
| 45 | { | 
|---|
| 46 |     using namespace Ogre; | 
|---|
| 47 |  | 
|---|
| 48 |     Navigation::Navigation(OverlayContainer* container){ | 
|---|
| 49 |         container_ = container; | 
|---|
| 50 |         focus_ = NULL; | 
|---|
| 51 |         init(); | 
|---|
| 52 |     } | 
|---|
| 53 |  | 
|---|
| 54 |     Navigation::Navigation(OverlayContainer* container, RadarObject* focus){ | 
|---|
| 55 |         container_ = container; | 
|---|
| 56 |         focus_ = focus; | 
|---|
| 57 |         init(); | 
|---|
| 58 |     } | 
|---|
| 59 |  | 
|---|
| 60 |     void Navigation::init(){ | 
|---|
| 61 |         om = &OverlayManager::getSingleton(); | 
|---|
| 62 |         navCam_ = NULL; | 
|---|
| 63 |         // create nav text | 
|---|
| 64 |         navText_ = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "navText")); | 
|---|
| 65 |         navText_->show(); | 
|---|
| 66 |         navText_->setMetricsMode(Ogre::GMM_PIXELS); | 
|---|
| 67 |         navText_->setDimensions(0.001, 0.001); | 
|---|
| 68 |         navText_->setPosition(0.02, 0.02); | 
|---|
| 69 |         navText_->setFontName("Console"); | 
|---|
| 70 |         navText_->setCharHeight(20); | 
|---|
| 71 |         navText_->setCaption(""); | 
|---|
| 72 |         container_->addChild(navText_); | 
|---|
| 73 |  | 
|---|
| 74 |  | 
|---|
| 75 |         // create nav marker ... | 
|---|
| 76 |         navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker")); | 
|---|
| 77 |         navMarker_->setMetricsMode(GMM_PIXELS); | 
|---|
| 78 |         navMarker_->hide(); | 
|---|
| 79 |         navText_->hide(); | 
|---|
| 80 |         container_->addChild(navMarker_); | 
|---|
| 81 |     } | 
|---|
| 82 |  | 
|---|
| 83 |     void Navigation::update(){ | 
|---|
| 84 |         if(focus_ == NULL) return; | 
|---|
| 85 |         navCamPos_ = SpaceShip::getLocalShip()->getPosition(); | 
|---|
| 86 |         currentDir_ = SpaceShip::getLocalShip()->getDir(); | 
|---|
| 87 |         currentOrth_ = SpaceShip::getLocalShip()->getOrth(); | 
|---|
| 88 |  | 
|---|
| 89 |         windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); | 
|---|
| 90 |         windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); | 
|---|
| 91 |         updateMarker(); | 
|---|
| 92 |     } | 
|---|
| 93 |  | 
|---|
| 94 |     void Navigation::updateMarker(){ | 
|---|
| 95 |         // set text | 
|---|
| 96 |         int dist = (int) getDist2Focus()/100; | 
|---|
| 97 |         navText_->setCaption(Ogre::StringConverter::toString(dist)); | 
|---|
| 98 |  | 
|---|
| 99 |         if(navCam_ == NULL) navCam_ = SpaceShip::getLocalShip()->getCamera()->cam_; | 
|---|
| 100 |         Vector3 pos = focus_->getPosition(); | 
|---|
| 101 |         // transform to screen coordinates | 
|---|
| 102 |         pos = navCam_->getProjectionMatrix()*navCam_->getViewMatrix()*pos; | 
|---|
| 103 |         float xPosRel = 0.5*pos.x+0.5; | 
|---|
| 104 |         float yPosRel = 1-(0.5*pos.y+0.5); | 
|---|
| 105 |         int xPos = (int) (xPosRel*windowW_); | 
|---|
| 106 |         int yPos = (int) (yPosRel*windowH_); | 
|---|
| 107 |         int xFromCenter = xPos-windowW_/2; | 
|---|
| 108 |         int yFromCenter = yPos-windowH_/2; | 
|---|
| 109 |         // is object in view? | 
|---|
| 110 |         float radius = RadarOverlayElement::calcRadius(navCamPos_, currentDir_, currentOrth_, focus_); | 
|---|
| 111 |         bool isRight = (currentDir_.crossProduct(currentOrth_)).dotProduct(focus_->getPosition() - navCamPos_)>0; | 
|---|
| 112 |         bool isAbove = currentOrth_.dotProduct(focus_->getPosition() - navCamPos_)>0; | 
|---|
| 113 |         bool outOfView = (xPosRel<0 || xPosRel>1 || yPosRel<0 || yPosRel>1); | 
|---|
| 114 |         // if object is behind us, it is out of view anyway: | 
|---|
| 115 |         if(!outOfView && radius>3.14/2) outOfView = true; | 
|---|
| 116 |  | 
|---|
| 117 |         if(outOfView){ | 
|---|
| 118 |             // object is not in view | 
|---|
| 119 |             navMarker_->setMaterialName("Orxonox/NavArrows"); | 
|---|
| 120 |             navMarker_->setDimensions(16,16); | 
|---|
| 121 |             float phiUpperCorner = atan((float)(windowW_)/(float)(windowH_)); | 
|---|
| 122 |             // from the angle we find out on which edge to draw the marker | 
|---|
| 123 |             // and which of the four arrows to take | 
|---|
| 124 |             float phiNav = atan((float) xFromCenter / (float) yFromCenter); | 
|---|
| 125 |  | 
|---|
| 126 |             if(isAbove && isRight){ | 
|---|
| 127 |                 // top right quadrant | 
|---|
| 128 |                 if(-phiNav<phiUpperCorner){ | 
|---|
| 129 |                     //COUT(3) << "arrow up\n"; | 
|---|
| 130 |                     navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0); | 
|---|
| 131 |                     navMarker_->setUV(0.5, 0.0, 1.0, 0.5); | 
|---|
| 132 |                     navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); | 
|---|
| 133 |                     navText_->setTop(navMarker_->getHeight()); | 
|---|
| 134 |                 } | 
|---|
| 135 |                 else { | 
|---|
| 136 |                     //COUT(3) << "arrow right\n"; | 
|---|
| 137 |                     navMarker_->setPosition(windowW_-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); | 
|---|
| 138 |                     navMarker_->setUV(0.5, 0.5, 1.0, 1.0); | 
|---|
| 139 |                     navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth()); | 
|---|
| 140 |                     navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); | 
|---|
| 141 |                 } | 
|---|
| 142 |             } | 
|---|
| 143 |             if(!isAbove && isRight){ | 
|---|
| 144 |                 // bottom right quadrant | 
|---|
| 145 |                 if(phiNav<phiUpperCorner) { | 
|---|
| 146 |                     //COUT(3) << "arrow down\n"; | 
|---|
| 147 |                     navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16); | 
|---|
| 148 |                     navMarker_->setUV(0.0, 0.5, 0.5, 1.0); | 
|---|
| 149 |                     navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); | 
|---|
| 150 |                     navText_->setTop(navMarker_->getTop()-navMarker_->getHeight()); | 
|---|
| 151 |                 } | 
|---|
| 152 |                 else { | 
|---|
| 153 |                     //COUT(3) << "arrow right\n"; | 
|---|
| 154 |                     navMarker_->setPosition(windowW_-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); | 
|---|
| 155 |                     navMarker_->setUV(0.5, 0.5, 1.0, 1.0); | 
|---|
| 156 |                     navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth()); | 
|---|
| 157 |                     navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); | 
|---|
| 158 |                 } | 
|---|
| 159 |             } | 
|---|
| 160 |             if(isAbove && !isRight){ | 
|---|
| 161 |                 // top left quadrant | 
|---|
| 162 |                 if(phiNav<phiUpperCorner){ | 
|---|
| 163 |                     //COUT(3) << "arrow up\n"; | 
|---|
| 164 |                     navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0); | 
|---|
| 165 |                     navMarker_->setUV(0.5, 0.0, 1.0, 0.5); | 
|---|
| 166 |                     navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); | 
|---|
| 167 |                     navText_->setTop(navMarker_->getHeight()); | 
|---|
| 168 |                 } | 
|---|
| 169 |                 else { | 
|---|
| 170 |                     //COUT(3) << "arrow left\n"; | 
|---|
| 171 |                     navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); | 
|---|
| 172 |                     navMarker_->setUV(0.0, 0.0, 0.5, 0.5); | 
|---|
| 173 |                     navText_->setLeft(navMarker_->getWidth()); | 
|---|
| 174 |                     navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); | 
|---|
| 175 |                 } | 
|---|
| 176 |             } | 
|---|
| 177 |             if(!isAbove && !isRight){ | 
|---|
| 178 |                 // bottom left quadrant | 
|---|
| 179 |                 if(phiNav>-phiUpperCorner) { | 
|---|
| 180 |                     //COUT(3) << "arrow down\n"; | 
|---|
| 181 |                     navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16); | 
|---|
| 182 |                     navMarker_->setUV(0.0, 0.5, 0.5, 1.0); | 
|---|
| 183 |                     navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); | 
|---|
| 184 |                     navText_->setTop(navMarker_->getTop()-navMarker_->getHeight()); | 
|---|
| 185 |                 } | 
|---|
| 186 |                 else { | 
|---|
| 187 |                     //COUT(3) << "arrow left\n"; | 
|---|
| 188 |                     navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); | 
|---|
| 189 |                     navMarker_->setUV(0.0, 0.0, 0.5, 0.5); | 
|---|
| 190 |                     navText_->setLeft(navMarker_->getWidth()); | 
|---|
| 191 |                     navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); | 
|---|
| 192 |                 } | 
|---|
| 193 |             } | 
|---|
| 194 |         } | 
|---|
| 195 |         else{ | 
|---|
| 196 |             // object is in view | 
|---|
| 197 |             navMarker_->setMaterialName("Orxonox/NavTDC"); | 
|---|
| 198 |             navMarker_->setDimensions(24,24); | 
|---|
| 199 |             navMarker_->setUV(0.0, 0.0, 1.0, 1.0); | 
|---|
| 200 |             navMarker_->setPosition(xPos-navMarker_->getWidth()/2, yPos-navMarker_->getHeight()/2); | 
|---|
| 201 |             navText_->setPosition(xPos+navMarker_->getWidth()/2, yPos+navMarker_->getHeight()/2); | 
|---|
| 202 |         } | 
|---|
| 203 |     } | 
|---|
| 204 |  | 
|---|
| 205 |     void Navigation::cycleFocus(){ | 
|---|
| 206 |         if(focus_ == NULL){ | 
|---|
| 207 |             it_ = HUD::getSingleton().roSet.begin(); | 
|---|
| 208 |             focus_ = *it_; | 
|---|
| 209 |             ++it_; | 
|---|
| 210 |         } | 
|---|
| 211 |         else{ | 
|---|
| 212 |             focus_->resetColour(); | 
|---|
| 213 |             if(it_ != HUD::getSingleton().roSet.end()){ | 
|---|
| 214 |                 focus_ = *it_; | 
|---|
| 215 |                 ++it_; | 
|---|
| 216 |             } | 
|---|
| 217 |             else focus_ = NULL; | 
|---|
| 218 |         } | 
|---|
| 219 |         if(focus_ == NULL){ | 
|---|
| 220 |             navMarker_->hide(); | 
|---|
| 221 |             navText_->hide(); | 
|---|
| 222 |         } | 
|---|
| 223 |         else{ | 
|---|
| 224 |             navMarker_->show(); | 
|---|
| 225 |             navText_->show(); | 
|---|
| 226 |             focus_->setColour(RadarObject::WHITE); | 
|---|
| 227 |         } | 
|---|
| 228 |     } | 
|---|
| 229 |  | 
|---|
| 230 |     float Navigation::getDist2Focus(){ | 
|---|
| 231 |         if(focus_ == NULL) return(0.0); | 
|---|
| 232 |         return((focus_->getPosition()-SpaceShip::getLocalShip()->getPosition()).length()); | 
|---|
| 233 |     } | 
|---|
| 234 | } | 
|---|