Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hudelements/src/modules/overlays/hud/HUDNavigation.cc @ 6873

Last change on this file since 6873 was 6873, checked in by sfluecki, 14 years ago

für reto

  • Property svn:eol-style set to native
File size: 14.6 KB
Line 
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 *      Reto Grieder
26 *
27 */
28
29#include "HUDNavigation.h"
30#include <utility>
31
32#include <string>
33#include <OgreCamera.h>
34#include <OgreOverlayManager.h>
35#include <OgreTextAreaOverlayElement.h>
36#include <OgrePanelOverlayElement.h>
37#include "util/Math.h"
38#include "util/Convert.h"
39#include "core/CoreIncludes.h"
40#include "core/XMLPort.h"
41#include "CameraManager.h"
42#include "Scene.h"
43#include "Radar.h"
44#include "graphics/Camera.h"
45#include "controllers/HumanController.h"
46#include "worldentities/pawns/Pawn.h"
47#include "worldentities/WorldEntity.h"
48
49namespace orxonox
50{
51    CreateFactory(HUDNavigation);
52
53    HUDNavigation::HUDNavigation(BaseObject* creator)
54        : OrxonoxOverlay(creator)
55    {
56        RegisterObject(HUDNavigation);
57       
58//         // create nav text
59//         navText_ = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton()
60//             .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString()));
61//
62//         // create nav marker
63//         navMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
64//             .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString()));
65//         navMarker_->setMaterialName("Orxonox/NavArrows");
66
67/*
68        // create aim marker
69        aimMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
70            .createOverlayElement("Panel", "HUDNavigation_aimMarker_" + getUniqueNumberString()));
71        aimMarker_->setMaterialName("Orxonox/NavCrosshair");
72        this->wasOutOfView_ = true; // Ensure the material is changed right the first time..
73
74
75*/
76/*
77        background_->addChild(navMarker_);*/
78//        background_->addChild(aimMarker_);
79//         background_->addChild(navText_);
80
81        // hide at first
82//         this->setVisible(false);
83       
84       
85        setFont("Monofur");
86        setTextSize(0.05f);
87        setNavMarkerSize(0.05f);
88//         setAimMarkerSize(0.04f);
89    }
90
91    HUDNavigation::~HUDNavigation()
92    {
93     
94        if (this->isInitialized())
95        {
96          if(!activeObjectList_.empty()) 
97           {
98            for(tempRadarViewable = activeObjectList_.begin(); tempRadarViewable!=activeObjectList_.end(); ++tempRadarViewable)
99            {
100              Ogre::OverlayManager::getSingleton().destroyOverlayElement(tempRadarViewable->second.first);
101              Ogre::OverlayManager::getSingleton().destroyOverlayElement(tempRadarViewable->second.second);
102//            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_);
103            }
104          }
105        }
106    }
107
108    void HUDNavigation::XMLPort(Element& xmlElement, XMLPort::Mode mode)
109    {
110        SUPER(HUDNavigation, XMLPort, xmlElement, mode);
111
112        XMLPortParam(HUDNavigation, "font",     setFont,     getFont,     xmlElement, mode);
113        XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlElement, mode);
114        XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlElement, mode);
115//        XMLPortParam(HUDNavigation, "aimMarkerSize", setAimMarkerSize, getAimMarkerSize, xmlElement, mode);
116    }
117
118    void HUDNavigation::setFont(const std::string& font)
119    {
120        fontName_ = font;
121        if(!activeObjectList_.empty()) 
122        {
123          for(tempRadarViewable = activeObjectList_.begin(); tempRadarViewable!=activeObjectList_.end(); ++tempRadarViewable)
124           {
125              if (tempRadarViewable->second.second && !fontName_.empty())
126                tempRadarViewable->second.second->setFontName(fontName_);
127           }
128        }
129    }
130
131    const std::string& HUDNavigation::getFont() const
132    {
133        return fontName_;
134    }
135
136    void HUDNavigation::setTextSize(float size)
137    {
138      textSize_ = size;
139      if(!activeObjectList_.empty()) 
140        {
141          for(tempRadarViewable = activeObjectList_.begin(); tempRadarViewable!=activeObjectList_.end(); ++tempRadarViewable)
142            {
143              if (tempRadarViewable->second.second && size >= 0.0f)
144                tempRadarViewable->second.second->setCharHeight(size);
145            }
146        }   
147    }
148
149    float HUDNavigation::getTextSize() const
150    {
151      return textSize_;
152    }
153
154    void HUDNavigation::tick(float dt)
155    {
156        SUPER(HUDNavigation, tick, dt);
157       
158//      updateActiveObjectList(activeObjectList_);
159
160        // Get radar
161//         Radar* radar = this->getOwner()->getScene()->getRadar();
162//
163//         if (!radar->getFocus())
164//         {
165//             this->overlay_->hide();
166//             return;
167//         }
168//         else
169//         {
170//             this->overlay_->show();
171//         }
172
173        // set text
174//         int dist = static_cast<int>(getDist2Focus());
175//         navText_->setCaption(multi_cast<std::string>(dist));
176//         float textLength = multi_cast<std::string>(dist).size() * navText_->getCharHeight() * 0.3f;
177
178      if(!activeObjectList_.empty()) {
179      for(tempRadarViewable = activeObjectList_.begin(); tempRadarViewable!=activeObjectList_.end(); ++tempRadarViewable)
180        {
181          COUT(0) << "check 174" << std::endl;
182       
183        //get Distance to HumanController and save it in the TextAreaOverlayElement.
184        int dist = (int)(tempRadarViewable->first->getRVWorldPosition() - HumanController::getLocalControllerEntityAsPawn()->getWorldPosition()).length();
185        tempRadarViewable->second.second->setCaption(multi_cast<std::string>(dist));
186        float textLength = multi_cast<std::string>(dist).size() * tempRadarViewable->second.second->getCharHeight() * 0.3f;
187         
188         COUT(0) << "check 181" << std::endl;
189     
190        orxonox::Camera* cam = CameraManager::getInstance().getActiveCamera();
191        if (!cam)
192            return;
193        const Matrix4& transform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix();
194        // transform to screen coordinates
195        Vector3 pos = transform * tempRadarViewable->first->getRVWorldPosition();
196
197         COUT(0) << "check 190" << std::endl;
198         
199        bool outOfView;
200        if (pos.z > 1.0)
201        {
202            // z > 1.0 means that the object is behind the camera
203            outOfView = true;
204            // we have to switch all coordinates (if you don't know why,
205            // try linear algebra lectures, because I can't explain..)
206            pos.x = -pos.x;
207            pos.y = -pos.y;
208        }
209        else
210            outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0;
211
212        if (outOfView)
213        {
214            // object is not in view
215//            aimMarker_->hide();
216
217            if (!wasOutOfView_)
218            {
219                tempRadarViewable->second.first->setMaterialName("Orxonox/NavArrows");
220                wasOutOfView_ = true;
221            }
222           
223             COUT(0) << "check 174" << std::endl;
224
225            if (pos.x < pos.y)
226            {
227                if (pos.y > -pos.x)
228                {
229                    // up
230                    float position = pos.x / pos.y + 1.0f;
231                    tempRadarViewable->second.first->setPosition((position - tempRadarViewable->second.first->getWidth()) * 0.5f, 0.0f);
232                    tempRadarViewable->second.first->setUV(0.5f, 0.0f, 1.0f, 0.5f);
233                    tempRadarViewable->second.second->setLeft((position - textLength) * 0.5f);
234                    tempRadarViewable->second.second->setTop(tempRadarViewable->second.first->getHeight());
235                }
236                else
237                {
238                    // left
239                    float position = pos.y / pos.x + 1.0f;
240                    tempRadarViewable->second.first->setPosition(0.0f, (position - tempRadarViewable->second.first->getWidth()) * 0.5f);
241                    tempRadarViewable->second.first->setUV(0.0f, 0.0f, 0.5f, 0.5f);
242                    tempRadarViewable->second.second->setLeft(tempRadarViewable->second.first->getWidth() + 0.01f);
243                    tempRadarViewable->second.second->setTop((position - tempRadarViewable->second.second->getCharHeight()) * 0.5f);
244                }
245            }
246            else
247            {
248               COUT(0) << "check 241" << std::endl;
249                if (pos.y < -pos.x)
250                {
251                    // down
252                    float position = -pos.x / pos.y + 1.0f;
253                    tempRadarViewable->second.first->setPosition((position - tempRadarViewable->second.first->getWidth()) * 0.5f, 1.0f - navMarker_->getHeight());
254                    tempRadarViewable->second.first->setUV(0.0f, 0.5f, 0.5f, 1.0f);
255                    tempRadarViewable->second.second->setLeft((position - textLength) * 0.5f);
256                    tempRadarViewable->second.second->setTop(1.0f - tempRadarViewable->second.first->getHeight() - tempRadarViewable->second.second->getCharHeight());
257                }
258                else
259                {
260                    // right
261                    float position = -pos.y / pos.x + 1.0f;
262                    tempRadarViewable->second.first->setPosition(1.0f - tempRadarViewable->second.first->getWidth(), (position - tempRadarViewable->second.first->getHeight()) * 0.5f);
263                    tempRadarViewable->second.first->setUV(0.5f, 0.5f, 1.0f, 1.0f);
264                    tempRadarViewable->second.second->setLeft(1.0f - tempRadarViewable->second.first->getWidth() - textLength - 0.01f);
265                    tempRadarViewable->second.second->setTop((position - tempRadarViewable->second.second->getCharHeight()) * 0.5f);
266                }
267            }
268        }
269        else
270        {
271           COUT(0) << "check 264" << std::endl;
272         
273            // object is in view
274/*
275            Vector3 aimpos = transform * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(),
276                    Projectile::getSpeed(), Radar::getInstance().getFocus()->getRVWorldPosition(), Radar::getInstance().getFocus()->getRVOrientedVelocity());
277*/
278            if (wasOutOfView_)
279            {
280                tempRadarViewable->second.first->setMaterialName("Orxonox/NavTDC");
281                wasOutOfView_ = false;
282            }
283
284            // object is in view
285            tempRadarViewable->second.first->setUV(0.0f, 0.0f, 1.0f, 1.0f);
286            tempRadarViewable->second.first->setLeft((pos.x + 1.0f - tempRadarViewable->second.first->getWidth()) * 0.5f);
287            tempRadarViewable->second.first->setTop((-pos.y + 1.0f - tempRadarViewable->second.first->getHeight()) * 0.5f);
288
289/*
290            aimMarker_->show();
291            aimMarker_->setLeft((aimpos.x + 1.0f - aimMarker_->getWidth()) * 0.5f);
292            aimMarker_->setTop((-aimpos.y + 1.0f - aimMarker_->getHeight()) * 0.5f);
293*/
294            tempRadarViewable->second.second->setLeft((pos.x + 1.0f + tempRadarViewable->second.first->getWidth()) * 0.5f);
295            tempRadarViewable->second.second->setTop((-pos.y + 1.0f + tempRadarViewable->second.first->getHeight()) * 0.5f);
296        }
297      }
298    }
299    }
300   
301   
302    float HUDNavigation::getDist2Focus() const{
303     
304        Radar* radar = this->getOwner()->getScene()->getRadar();
305        if (radar->getFocus() && HumanController::getLocalControllerEntityAsPawn())
306            return (radar->getFocus()->getRVWorldPosition() - HumanController::getLocalControllerEntityAsPawn()->getWorldPosition()).length();
307        else
308            return 0;
309    }
310
311    /**
312    @brief Overridden method of OrxonoxOverlay. Usually the entire overlay
313           scales with scale(). Here we obviously have to adjust this.
314    */
315    void HUDNavigation::sizeChanged(){
316            // use size to compensate for aspect ratio if enabled.
317        float xScale = this->getActualSize().x;
318        float yScale = this->getActualSize().y;
319       
320         if(!activeObjectList_.empty()) 
321        {
322          for(tempRadarViewable = activeObjectList_.begin(); tempRadarViewable!=activeObjectList_.end(); ++tempRadarViewable)
323          {
324
325             if (tempRadarViewable->second.first)
326              tempRadarViewable->second.first->setDimensions(navMarkerSize_ * xScale, navMarkerSize_ * yScale);
327//            if (this->aimMarker_)
328//            aimMarker_->setDimensions(aimMarkerSize_ * xScale, aimMarkerSize_ * yScale);
329             if (tempRadarViewable->second.second)
330              tempRadarViewable->second.second->setCharHeight(tempRadarViewable->second.second->getCharHeight() * yScale);
331          }
332        }
333    }
334   
335   
336    void HUDNavigation::addObject(RadarViewable* object){
337            if (object == dynamic_cast<RadarViewable*>(this->getOwner()))
338            return;
339         
340        assert(object);
341         
342        // Make sure the object hasn't been added yet
343        assert( this->activeObjectList_.find(object) == this->activeObjectList_.end() );
344
345        // Create everything needed to display the object on the radar and add it to the map
346       
347         // create nav marker
348        Ogre::PanelOverlayElement* panel = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
349            .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString()));
350           
351        panel->setMaterialName("Orxonox/NavArrows");
352         
353        Ogre::TextAreaOverlayElement* text = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton()
354             .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString()));
355             
356        int dist = (int)(object->getRVWorldPosition() - HumanController::getLocalControllerEntityAsPawn()->getWorldPosition()).length();
357            activeObjectList_[object].second->setCaption(multi_cast<std::string>(dist));
358       
359        activeObjectList_[object] = std::make_pair (panel, text) ;     
360        activeObjectList_[object].first->show();
361        activeObjectList_[object].second->show();
362//      background_->addChild(activeObjectList_[object].first);
363//      background_->addChild(activeObjectList_[object].second);
364        COUT(0) << "check 357" << std::endl;
365    }
366   
367  void HUDNavigation::removeObject(RadarViewable* viewable){
368          assert(activeObjectList_.find(viewable)!=activeObjectList_.end());
369          activeObjectList_.erase(viewable);
370  }
371
372//      void updateActiveObjectList(map activeObjectList_){}
373//
374//      void HUDNavigation::objectChanged(RadarViewable* viewable){}
375//
376//     
377//         float HUDNavigation::getRadarSensitivity(){}
378//         void HUDNavigation::radarTick(float dt){}
379       
380
381}
382
383
Note: See TracBrowser for help on using the repository browser.