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 | * Yuning Chai |
---|
24 | * Co-authors: |
---|
25 | * Felix Schulthess |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "OrxonoxStableHeaders.h" |
---|
30 | #include "HUD.h" |
---|
31 | |
---|
32 | #include <string> |
---|
33 | #include <set> |
---|
34 | #include <OgreOverlay.h> |
---|
35 | #include <OgreOverlayContainer.h> |
---|
36 | #include <OgreOverlayManager.h> |
---|
37 | #include <OgreStringConverter.h> |
---|
38 | |
---|
39 | #include "core/Debug.h" |
---|
40 | #include "core/ConsoleCommand.h" |
---|
41 | #include "objects/SpaceShip.h" |
---|
42 | #include "GraphicsEngine.h" |
---|
43 | #include "BarOverlayElement.h" |
---|
44 | #include "RadarObject.h" |
---|
45 | #include "RadarOverlayElement.h" |
---|
46 | #include "Navigation.h" |
---|
47 | #include "OverlayElementFactories.h" |
---|
48 | |
---|
49 | namespace orxonox |
---|
50 | { |
---|
51 | SetConsoleCommandShortcut(HUD, cycleNavigationFocus).setAccessLevel(AccessLevel::User); |
---|
52 | SetConsoleCommandShortcut(HUD, toggleFPS).setAccessLevel(AccessLevel::User); |
---|
53 | SetConsoleCommandShortcut(HUD, toggleRenderTime).setAccessLevel(AccessLevel::User); |
---|
54 | |
---|
55 | using namespace Ogre; |
---|
56 | |
---|
57 | HUD::HUD(){ |
---|
58 | om = &Ogre::OverlayManager::getSingleton(); |
---|
59 | sm = GraphicsEngine::getSingleton().getSceneManager(); |
---|
60 | showFPS = true; |
---|
61 | showRenderTime = true; |
---|
62 | |
---|
63 | // create Factories |
---|
64 | BarOverlayElementFactory *barOverlayElementFactory = new BarOverlayElementFactory(); |
---|
65 | om->addOverlayElementFactory(barOverlayElementFactory); |
---|
66 | RadarOverlayElementFactory *radarOverlayElementFactory = new RadarOverlayElementFactory(); |
---|
67 | om->addOverlayElementFactory(radarOverlayElementFactory); |
---|
68 | |
---|
69 | orxonoxHUD = om->create("Orxonox/HUD"); |
---|
70 | container = static_cast<Ogre::OverlayContainer*>(om->createOverlayElement("Panel", "Orxonox/HUD/container")); |
---|
71 | |
---|
72 | // creating text to display fps |
---|
73 | fpsText = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "fpsText")); |
---|
74 | fpsText->show(); |
---|
75 | fpsText->setMetricsMode(Ogre::GMM_PIXELS); |
---|
76 | fpsText->setDimensions(0.001, 0.001); |
---|
77 | fpsText->setPosition(10, 10); |
---|
78 | fpsText->setFontName("Console"); |
---|
79 | fpsText->setCharHeight(20); |
---|
80 | fpsText->setCaption("init"); |
---|
81 | |
---|
82 | // creating text to display render time ratio |
---|
83 | rTRText = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "rTRText")); |
---|
84 | rTRText->show(); |
---|
85 | rTRText->setMetricsMode(Ogre::GMM_PIXELS); |
---|
86 | rTRText->setDimensions(0.001, 0.001); |
---|
87 | rTRText->setPosition(10, 30); |
---|
88 | rTRText->setFontName("Console"); |
---|
89 | rTRText->setCharHeight(20); |
---|
90 | rTRText->setCaption("init"); |
---|
91 | |
---|
92 | // create energy bar |
---|
93 | energyBar = static_cast<BarOverlayElement*>(om->createOverlayElement("Bar", "energyBar")); |
---|
94 | energyBar->show(); |
---|
95 | // create speedo bar |
---|
96 | speedoBar = static_cast<BarOverlayElement*>(om->createOverlayElement("Bar", "speedoBar")); |
---|
97 | speedoBar->show(); |
---|
98 | // create radar |
---|
99 | radar = static_cast<RadarOverlayElement*>(om->createOverlayElement("Radar", "radar")); |
---|
100 | radar->show(); |
---|
101 | |
---|
102 | // create Navigation |
---|
103 | nav = new Navigation(container); |
---|
104 | |
---|
105 | // set up screen-wide container |
---|
106 | container->show(); |
---|
107 | |
---|
108 | orxonoxHUD->add2D(container); |
---|
109 | orxonoxHUD->show(); |
---|
110 | container->setLeft(0.0); |
---|
111 | container->setTop(0.0); |
---|
112 | container->setWidth(1.0); |
---|
113 | container->setHeight(1.0); |
---|
114 | container->setMetricsMode(Ogre::GMM_RELATIVE); |
---|
115 | container->addChild(fpsText); |
---|
116 | container->addChild(rTRText); |
---|
117 | |
---|
118 | energyBar->init(0.01, 0.94, 0.4, container); |
---|
119 | energyBar->setValue(1); |
---|
120 | |
---|
121 | speedoBar->init(0.01, 0.90, 0.4, container); |
---|
122 | |
---|
123 | radar->init(0.5, 0.9, 0.2, container); |
---|
124 | SceneNode* node; |
---|
125 | node = sm->getRootSceneNode()->createChildSceneNode("tomato1", Vector3(2000.0, 0.0, 0.0)); |
---|
126 | addRadarObject(node); |
---|
127 | node = sm->getRootSceneNode()->createChildSceneNode("tomato2", Vector3(0.0, 2000.0, 0.0)); |
---|
128 | addRadarObject(node); |
---|
129 | node = sm->getRootSceneNode()->createChildSceneNode("tomato3", Vector3(0.0, 0.0, 2000.0)); |
---|
130 | addRadarObject(node); |
---|
131 | node = sm->getRootSceneNode()->createChildSceneNode("station", Vector3(10000.0,16000.0,0.0)); |
---|
132 | addRadarObject(node, 3); |
---|
133 | } |
---|
134 | |
---|
135 | HUD::~HUD(){ |
---|
136 | //todo: clean up objects |
---|
137 | } |
---|
138 | |
---|
139 | void HUD::tick(float dt) |
---|
140 | { |
---|
141 | energyBar->resize(); |
---|
142 | |
---|
143 | if(!SpaceShip::getLocalShip()) |
---|
144 | return; |
---|
145 | float v = SpaceShip::getLocalShip()->getVelocity().length(); |
---|
146 | float vmax = SpaceShip::getLocalShip()->getMaxSpeed(); |
---|
147 | speedoBar->setValue(v/vmax); |
---|
148 | speedoBar->resize(); |
---|
149 | |
---|
150 | radar->resize(); |
---|
151 | radar->update(); |
---|
152 | |
---|
153 | nav->update(); |
---|
154 | |
---|
155 | setFPS(); |
---|
156 | } |
---|
157 | |
---|
158 | void HUD::setRenderTimeRatio(float ratio) |
---|
159 | { |
---|
160 | if(showRenderTime){ |
---|
161 | rTRText->setCaption("Render time ratio: " + Ogre::StringConverter::toString(ratio)); |
---|
162 | } |
---|
163 | else{ |
---|
164 | rTRText->setCaption(""); |
---|
165 | return; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | void HUD::setFPS(){ |
---|
170 | if(showFPS){ |
---|
171 | float fps = GraphicsEngine::getSingleton().getAverageFPS(); |
---|
172 | fpsText->setCaption("FPS: " + Ogre::StringConverter::toString(fps)); |
---|
173 | } |
---|
174 | else{ |
---|
175 | fpsText->setCaption(""); |
---|
176 | return; |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | void HUD::addRadarObject(SceneNode* node, int colour){ |
---|
181 | RadarObject* obj = new RadarObject(container, node, colour); |
---|
182 | roSet.insert(obj); |
---|
183 | // // check if this is the first RadarObject to create |
---|
184 | // if(firstRadarObject == NULL){ |
---|
185 | // firstRadarObject = new RadarObject(container, node, colour); |
---|
186 | // lastRadarObject = firstRadarObject; |
---|
187 | // } |
---|
188 | // else{ // if not, append to list |
---|
189 | // lastRadarObject->next = new RadarObject(container, node, colour); |
---|
190 | // lastRadarObject = lastRadarObject->next; |
---|
191 | // } |
---|
192 | } |
---|
193 | |
---|
194 | void HUD::removeRadarObject(Ogre::SceneNode* node){ |
---|
195 | COUT(3) << "blabla" << std::endl; |
---|
196 | for(std::set<RadarObject*>::iterator it=roSet.begin(); it!=roSet.end(); it++){ |
---|
197 | if((*it)->getNode() == node) { |
---|
198 | delete (*it); |
---|
199 | roSet.erase(it); |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | /*static*/ HUD& HUD::getSingleton(){ |
---|
205 | static HUD theInstance; |
---|
206 | return theInstance; |
---|
207 | } |
---|
208 | |
---|
209 | /*static*/ void HUD::setEnergy(float value){ |
---|
210 | HUD::getSingleton().energyBar->setValue(value); |
---|
211 | } |
---|
212 | |
---|
213 | /*static*/ void HUD::cycleNavigationFocus(){ |
---|
214 | HUD::getSingleton().nav->cycleFocus(); |
---|
215 | } |
---|
216 | |
---|
217 | /*static*/ void HUD::toggleFPS(){ |
---|
218 | HUD::getSingleton().showFPS = !HUD::getSingleton().showFPS; |
---|
219 | } |
---|
220 | |
---|
221 | /*static*/ void HUD::toggleRenderTime(){ |
---|
222 | HUD::getSingleton().showRenderTime = !HUD::getSingleton().showRenderTime; |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | |
---|