Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

HUDNavigation constructor corrected

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