Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6849 was 6849, checked in by scurcio, 14 years ago

update hudnavigation /addObject, removeObject

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