Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

nochma test?

  • Property svn:eol-style set to native
File size: 13.8 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        if (this->isInitialized())
94        {
95            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navMarker_);
96            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navText_);
97//            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_);
98        }
99    }
100
101    void HUDNavigation::XMLPort(Element& xmlElement, XMLPort::Mode mode)
102    {
103        SUPER(HUDNavigation, XMLPort, xmlElement, mode);
104
105        XMLPortParam(HUDNavigation, "font",     setFont,     getFont,     xmlElement, mode);
106        XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlElement, mode);
107        XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlElement, mode);
108//        XMLPortParam(HUDNavigation, "aimMarkerSize", setAimMarkerSize, getAimMarkerSize, xmlElement, mode);
109    }
110
111    void HUDNavigation::setFont(const std::string& font)
112    {
113        fontName_ = font;
114        if(!activeObjectList_.empty()) 
115        {
116          for(tempRadarViewable = activeObjectList_.begin(); tempRadarViewable!=activeObjectList_.end(); ++tempRadarViewable)
117           {
118              if (tempRadarViewable->second.second && !fontName_.empty())
119                tempRadarViewable->second.second->setFontName(fontName_);
120           }
121        }
122    }
123
124    const std::string& HUDNavigation::getFont() const
125    {
126        return fontName_;
127    }
128
129    void HUDNavigation::setTextSize(float size)
130    {
131      textSize_ = size;
132      if(!activeObjectList_.empty()) 
133        {
134          for(tempRadarViewable = activeObjectList_.begin(); tempRadarViewable!=activeObjectList_.end(); ++tempRadarViewable)
135            {
136              if (tempRadarViewable->second.second && size >= 0.0f)
137                tempRadarViewable->second.second->setCharHeight(size);
138            }
139        }   
140    }
141
142    float HUDNavigation::getTextSize() const
143    {
144      return textSize_;
145    }
146
147    void HUDNavigation::tick(float dt)
148    {
149        SUPER(HUDNavigation, tick, dt);
150       
151//      updateActiveObjectList(activeObjectList_);
152
153        // Get radar
154//         Radar* radar = this->getOwner()->getScene()->getRadar();
155//
156//         if (!radar->getFocus())
157//         {
158//             this->overlay_->hide();
159//             return;
160//         }
161//         else
162//         {
163//             this->overlay_->show();
164//         }
165
166        // set text
167//         int dist = static_cast<int>(getDist2Focus());
168//         navText_->setCaption(multi_cast<std::string>(dist));
169//         float textLength = multi_cast<std::string>(dist).size() * navText_->getCharHeight() * 0.3f;
170
171      if(!activeObjectList_.empty()) {
172      for(tempRadarViewable = activeObjectList_.begin(); tempRadarViewable!=activeObjectList_.end(); ++tempRadarViewable)
173        {
174       
175        //get Distance to HumanController and save it in the TextAreaOverlayElement.
176        int dist = (int)(tempRadarViewable->first->getRVWorldPosition() - HumanController::getLocalControllerEntityAsPawn()->getWorldPosition()).length();
177        tempRadarViewable->second.second->setCaption(multi_cast<std::string>(dist));
178        float textLength = multi_cast<std::string>(dist).size() * tempRadarViewable->second.second->getCharHeight() * 0.3f;
179         
180        orxonox::Camera* cam = CameraManager::getInstance().getActiveCamera();
181        if (!cam)
182            return;
183        const Matrix4& transform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix();
184        // transform to screen coordinates
185        Vector3 pos = transform * tempRadarViewable->first->getRVWorldPosition();
186
187        bool outOfView;
188        if (pos.z > 1.0)
189        {
190            // z > 1.0 means that the object is behind the camera
191            outOfView = true;
192            // we have to switch all coordinates (if you don't know why,
193            // try linear algebra lectures, because I can't explain..)
194            pos.x = -pos.x;
195            pos.y = -pos.y;
196        }
197        else
198            outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0;
199
200        if (outOfView)
201        {
202            // object is not in view
203//            aimMarker_->hide();
204
205            if (!wasOutOfView_)
206            {
207                tempRadarViewable->second.first->setMaterialName("Orxonox/NavArrows");
208                wasOutOfView_ = true;
209            }
210
211            if (pos.x < pos.y)
212            {
213                if (pos.y > -pos.x)
214                {
215                    // up
216                    float position = pos.x / pos.y + 1.0f;
217                    tempRadarViewable->second.first->setPosition((position - tempRadarViewable->second.first->getWidth()) * 0.5f, 0.0f);
218                    tempRadarViewable->second.first->setUV(0.5f, 0.0f, 1.0f, 0.5f);
219                    tempRadarViewable->second.second->setLeft((position - textLength) * 0.5f);
220                    tempRadarViewable->second.second->setTop(tempRadarViewable->second.first->getHeight());
221                }
222                else
223                {
224                    // left
225                    float position = pos.y / pos.x + 1.0f;
226                    tempRadarViewable->second.first->setPosition(0.0f, (position - tempRadarViewable->second.first->getWidth()) * 0.5f);
227                    tempRadarViewable->second.first->setUV(0.0f, 0.0f, 0.5f, 0.5f);
228                    tempRadarViewable->second.second->setLeft(tempRadarViewable->second.first->getWidth() + 0.01f);
229                    tempRadarViewable->second.second->setTop((position - tempRadarViewable->second.second->getCharHeight()) * 0.5f);
230                }
231            }
232            else
233            {
234                if (pos.y < -pos.x)
235                {
236                    // down
237                    float position = -pos.x / pos.y + 1.0f;
238                    tempRadarViewable->second.first->setPosition((position - tempRadarViewable->second.first->getWidth()) * 0.5f, 1.0f - navMarker_->getHeight());
239                    tempRadarViewable->second.first->setUV(0.0f, 0.5f, 0.5f, 1.0f);
240                    tempRadarViewable->second.second->setLeft((position - textLength) * 0.5f);
241                    tempRadarViewable->second.second->setTop(1.0f - tempRadarViewable->second.first->getHeight() - tempRadarViewable->second.second->getCharHeight());
242                }
243                else
244                {
245                    // right
246                    float position = -pos.y / pos.x + 1.0f;
247                    tempRadarViewable->second.first->setPosition(1.0f - tempRadarViewable->second.first->getWidth(), (position - tempRadarViewable->second.first->getHeight()) * 0.5f);
248                    tempRadarViewable->second.first->setUV(0.5f, 0.5f, 1.0f, 1.0f);
249                    tempRadarViewable->second.second->setLeft(1.0f - tempRadarViewable->second.first->getWidth() - textLength - 0.01f);
250                    tempRadarViewable->second.second->setTop((position - tempRadarViewable->second.second->getCharHeight()) * 0.5f);
251                }
252            }
253        }
254        else
255        {
256         
257         
258            // object is in view
259/*
260            Vector3 aimpos = transform * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(),
261                    Projectile::getSpeed(), Radar::getInstance().getFocus()->getRVWorldPosition(), Radar::getInstance().getFocus()->getRVOrientedVelocity());
262*/
263            if (wasOutOfView_)
264            {
265                tempRadarViewable->second.first->setMaterialName("Orxonox/NavTDC");
266                wasOutOfView_ = false;
267            }
268
269            // object is in view
270            tempRadarViewable->second.first->setUV(0.0f, 0.0f, 1.0f, 1.0f);
271            tempRadarViewable->second.first->setLeft((pos.x + 1.0f - tempRadarViewable->second.first->getWidth()) * 0.5f);
272            tempRadarViewable->second.first->setTop((-pos.y + 1.0f - tempRadarViewable->second.first->getHeight()) * 0.5f);
273
274/*
275            aimMarker_->show();
276            aimMarker_->setLeft((aimpos.x + 1.0f - aimMarker_->getWidth()) * 0.5f);
277            aimMarker_->setTop((-aimpos.y + 1.0f - aimMarker_->getHeight()) * 0.5f);
278*/
279            tempRadarViewable->second.second->setLeft((pos.x + 1.0f + tempRadarViewable->second.first->getWidth()) * 0.5f);
280            tempRadarViewable->second.second->setTop((-pos.y + 1.0f + tempRadarViewable->second.first->getHeight()) * 0.5f);
281        }
282      }
283    }
284    }
285   
286   
287    float HUDNavigation::getDist2Focus() const{
288     
289        Radar* radar = this->getOwner()->getScene()->getRadar();
290        if (radar->getFocus() && HumanController::getLocalControllerEntityAsPawn())
291            return (radar->getFocus()->getRVWorldPosition() - HumanController::getLocalControllerEntityAsPawn()->getWorldPosition()).length();
292        else
293            return 0;
294    }
295
296    /**
297    @brief Overridden method of OrxonoxOverlay. Usually the entire overlay
298           scales with scale(). Here we obviously have to adjust this.
299    */
300    void HUDNavigation::sizeChanged(){
301            // use size to compensate for aspect ratio if enabled.
302        float xScale = this->getActualSize().x;
303        float yScale = this->getActualSize().y;
304        if (this->navMarker_)
305            navMarker_->setDimensions(navMarkerSize_ * xScale, navMarkerSize_ * yScale);
306//         if (this->aimMarker_)
307//             aimMarker_->setDimensions(aimMarkerSize_ * xScale, aimMarkerSize_ * yScale);
308        if (this->navText_)
309            navText_->setCharHeight(navText_->getCharHeight() * yScale);
310    }
311   
312   
313    void HUDNavigation::addObject(RadarViewable* object){
314            if (object == dynamic_cast<RadarViewable*>(this->getOwner()))
315            return;
316         
317        assert(object);
318         
319        // Make sure the object hasn't been added yet
320        assert( this->activeObjectList_.find(object) == this->activeObjectList_.end() );
321
322        // Create everything needed to display the object on the radar and add it to the map
323       
324         // create nav marker
325        Ogre::PanelOverlayElement* panel = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
326            .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString()));
327           
328        panel->setMaterialName("Orxonox/NavArrows");
329         
330        Ogre::TextAreaOverlayElement* text = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton()
331             .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString()));
332             
333        int dist = (int)(object->getRVWorldPosition() - HumanController::getLocalControllerEntityAsPawn()->getWorldPosition()).length();
334            activeObjectList_[object].second->setCaption(multi_cast<std::string>(dist));
335       
336        activeObjectList_[object] = std::make_pair (panel, text) ;     
337        activeObjectList_[object].first->show();
338        activeObjectList_[object].second->show();
339//      background_->addChild(activeObjectList_[object].first);
340//      background_->addChild(activeObjectList_[object].second);
341    }
342   
343  void HUDNavigation::removeObject(RadarViewable* viewable){
344          assert(activeObjectList_.find(viewable)!=activeObjectList_.end());
345          activeObjectList_.erase(viewable);
346  }
347
348//      void updateActiveObjectList(map activeObjectList_){}
349//
350//      void HUDNavigation::objectChanged(RadarViewable* viewable){}
351//
352//     
353//         float HUDNavigation::getRadarSensitivity(){}
354//         void HUDNavigation::radarTick(float dt){}
355       
356
357}
358
359
Note: See TracBrowser for help on using the repository browser.