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 | * Fabian 'x3n' Landau |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "OrxonoxStableHeaders.h" |
---|
30 | |
---|
31 | #include "InGameConsole.h" |
---|
32 | |
---|
33 | #include <string> |
---|
34 | #include <OgreOverlay.h> |
---|
35 | #include <OgreOverlayElement.h> |
---|
36 | #include <OgreOverlayManager.h> |
---|
37 | #include <OgreOverlayContainer.h> |
---|
38 | #include <OgreStringConverter.h> |
---|
39 | |
---|
40 | #include "core/Debug.h" |
---|
41 | #include "core/CoreIncludes.h" |
---|
42 | #include "core/ConfigValueIncludes.h" |
---|
43 | #include "core/ConsoleCommand.h" |
---|
44 | #include "GraphicsEngine.h" |
---|
45 | |
---|
46 | #define LINES 20 |
---|
47 | |
---|
48 | namespace orxonox |
---|
49 | { |
---|
50 | using namespace Ogre; |
---|
51 | |
---|
52 | float InGameConsole::REL_WIDTH = 0.8; |
---|
53 | float InGameConsole::REL_HEIGHT = 0.4; |
---|
54 | float InGameConsole::BLINK = 0.25; |
---|
55 | |
---|
56 | /** |
---|
57 | @brief Constructor: Creates and initializes the InGameConsole. |
---|
58 | */ |
---|
59 | InGameConsole::InGameConsole() |
---|
60 | { |
---|
61 | RegisterObject(InGameConsole); |
---|
62 | |
---|
63 | this->active_ = false; |
---|
64 | this->cursor_ = 0.0; |
---|
65 | |
---|
66 | this->init(); |
---|
67 | this->setConfigValues(); |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | @brief Destructor: Destroys the TextAreas. |
---|
72 | */ |
---|
73 | InGameConsole::~InGameConsole(void) |
---|
74 | { |
---|
75 | for (int i = 0; i < LINES; i++) |
---|
76 | delete this->consoleOverlayTextAreas_[i]; |
---|
77 | |
---|
78 | delete this->consoleOverlayTextAreas_; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | @brief Returns a reference to the only existing instance of InGameConsole. |
---|
83 | */ |
---|
84 | InGameConsole& InGameConsole::getInstance() |
---|
85 | { |
---|
86 | static InGameConsole instance; |
---|
87 | return instance; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | @brief Sets the config values, describing the size of the console. |
---|
92 | */ |
---|
93 | void InGameConsole::setConfigValues() |
---|
94 | { |
---|
95 | SetConfigValue(REL_WIDTH, 0.8); |
---|
96 | SetConfigValue(REL_HEIGHT, 0.4); |
---|
97 | SetConfigValue(BLINK, 0.25); |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | @brief Called if all output-lines have to be redrawn. |
---|
102 | */ |
---|
103 | void InGameConsole::linesChanged() |
---|
104 | { |
---|
105 | std::list<std::string>::const_iterator it = Shell::getInstance().getNewestLineIterator(); |
---|
106 | for (int i = 1; i < LINES && it != Shell::getInstance().getEndIterator(); i++) |
---|
107 | { |
---|
108 | this->consoleOverlayTextAreas_[i]->setCaption(*it); |
---|
109 | ++it; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | @brief Called if only the last output-line has changed. |
---|
115 | */ |
---|
116 | void InGameConsole::onlyLastLineChanged() |
---|
117 | { |
---|
118 | if (LINES > 1) |
---|
119 | this->consoleOverlayTextAreas_[1]->setCaption(*Shell::getInstance().getNewestLineIterator()); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | @brief Called if a new output-line was added. |
---|
124 | */ |
---|
125 | void InGameConsole::lineAdded() |
---|
126 | { |
---|
127 | for (int i = LINES - 1; i > 1; i--) |
---|
128 | this->consoleOverlayTextAreas_[i]->setCaption(this->consoleOverlayTextAreas_[i - 1]->getCaption()); |
---|
129 | |
---|
130 | if (LINES > 1) |
---|
131 | this->consoleOverlayTextAreas_[1]->setCaption(*Shell::getInstance().getNewestLineIterator()); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | @brief Called if the text in the input-line has changed. |
---|
136 | */ |
---|
137 | void InGameConsole::inputChanged() |
---|
138 | { |
---|
139 | if (LINES > 0) |
---|
140 | this->consoleOverlayTextAreas_[0]->setCaption(Shell::getInstance().getInput()); |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | @brief Called if the position of the cursor in the input-line has changed. |
---|
145 | */ |
---|
146 | void InGameConsole::cursorChanged() |
---|
147 | { |
---|
148 | std::string input = Shell::getInstance().getInput(); |
---|
149 | input.insert(Shell::getInstance().getCursorPosition(), 1, '|'); |
---|
150 | if (LINES > 0) |
---|
151 | this->consoleOverlayTextAreas_[0]->setCaption(input); |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | @brief Called if the console gets closed. |
---|
156 | */ |
---|
157 | void InGameConsole::exit() |
---|
158 | { |
---|
159 | this->deactivate(); |
---|
160 | CommandExecutor::execute("set InputMode 2"); |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | @brief Called once by constructor, initializes the InGameConsole. |
---|
165 | */ |
---|
166 | void InGameConsole::init() |
---|
167 | { |
---|
168 | // for the beginning, don't scroll |
---|
169 | this->scroll_ = 0; |
---|
170 | this->scrollTimer_ = 0; |
---|
171 | this->cursor_ = 0; |
---|
172 | |
---|
173 | // create overlay and elements |
---|
174 | this->om_ = &Ogre::OverlayManager::getSingleton(); |
---|
175 | |
---|
176 | // create a container |
---|
177 | this->consoleOverlayContainer_ = static_cast<OverlayContainer*>(this->om_->createOverlayElement("Panel", "container")); |
---|
178 | this->consoleOverlayContainer_->setMetricsMode(Ogre::GMM_RELATIVE); |
---|
179 | this->consoleOverlayContainer_->setPosition((1 - InGameConsole::REL_WIDTH) / 2, 0); |
---|
180 | this->consoleOverlayContainer_->setDimensions(InGameConsole::REL_WIDTH, InGameConsole::REL_HEIGHT); |
---|
181 | |
---|
182 | // create BorderPanel |
---|
183 | this->consoleOverlayBorder_ = static_cast<BorderPanelOverlayElement*>(this->om_->createOverlayElement("BorderPanel", "borderPanel")); |
---|
184 | this->consoleOverlayBorder_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
185 | this->consoleOverlayBorder_->setMaterialName("ConsoleCenter"); |
---|
186 | // set parameters for border |
---|
187 | this->consoleOverlayBorder_->setBorderSize(16, 16, 0, 16); |
---|
188 | this->consoleOverlayBorder_->setBorderMaterialName("ConsoleBorder"); |
---|
189 | this->consoleOverlayBorder_->setLeftBorderUV(0.0, 0.49, 0.5, 0.51); |
---|
190 | this->consoleOverlayBorder_->setRightBorderUV(0.5, 0.49, 1.0, 0.5); |
---|
191 | this->consoleOverlayBorder_->setBottomBorderUV(0.49, 0.5, 0.51, 1.0); |
---|
192 | this->consoleOverlayBorder_->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0); |
---|
193 | this->consoleOverlayBorder_->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0); |
---|
194 | |
---|
195 | // create the text lines |
---|
196 | this->consoleOverlayTextAreas_ = new TextAreaOverlayElement*[LINES]; |
---|
197 | for (int i = 0; i < LINES; i++) |
---|
198 | { |
---|
199 | this->consoleOverlayTextAreas_[i] = static_cast<TextAreaOverlayElement*>(this->om_->createOverlayElement("TextArea", "textArea" + Ogre::StringConverter::toString(i))); |
---|
200 | this->consoleOverlayTextAreas_[i]->setMetricsMode(Ogre::GMM_PIXELS); |
---|
201 | this->consoleOverlayTextAreas_[i]->setFontName("Console"); |
---|
202 | this->consoleOverlayTextAreas_[i]->setCharHeight(20); |
---|
203 | this->consoleOverlayTextAreas_[i]->setParameter("colour_top", "0.21 0.69 0.21"); |
---|
204 | this->consoleOverlayTextAreas_[i]->setLeft(8); |
---|
205 | this->consoleOverlayTextAreas_[i]->setCaption(""); |
---|
206 | } |
---|
207 | |
---|
208 | // create noise |
---|
209 | this->consoleOverlayNoise_ = static_cast<PanelOverlayElement*>(this->om_->createOverlayElement("Panel", "noise")); |
---|
210 | this->consoleOverlayNoise_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
211 | this->consoleOverlayNoise_->setPosition(5,0); |
---|
212 | this->consoleOverlayNoise_->setMaterialName("ConsoleNoise"); |
---|
213 | |
---|
214 | this->consoleOverlay_ = this->om_->create("Console"); |
---|
215 | this->consoleOverlay_->add2D(this->consoleOverlayContainer_); |
---|
216 | this->consoleOverlayContainer_->addChild(this->consoleOverlayBorder_); |
---|
217 | //comment following line to disable noise |
---|
218 | this->consoleOverlayContainer_->addChild(this->consoleOverlayNoise_); |
---|
219 | for (int i = 0; i < LINES; i++) |
---|
220 | this->consoleOverlayContainer_->addChild(this->consoleOverlayTextAreas_[i]); |
---|
221 | |
---|
222 | this->resize(); |
---|
223 | |
---|
224 | // move overlay "above" the top edge of the screen |
---|
225 | // we take -1.2 because the border mkes the panel bigger |
---|
226 | this->consoleOverlayContainer_->setTop(-1.2 * InGameConsole::REL_HEIGHT); |
---|
227 | // show overlay |
---|
228 | this->consoleOverlay_->show(); |
---|
229 | |
---|
230 | COUT(3) << "Info: InGameConsole initialized" << std::endl; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | @brief Used to control the actual scrolling and the cursor. |
---|
235 | */ |
---|
236 | void InGameConsole::tick(float dt) |
---|
237 | { |
---|
238 | this->scrollTimer_ += dt; |
---|
239 | if (this->scrollTimer_ >= 0.01) |
---|
240 | { |
---|
241 | float top = this->consoleOverlayContainer_->getTop(); |
---|
242 | this->scrollTimer_ = 0; |
---|
243 | if (this->scroll_ != 0) |
---|
244 | { |
---|
245 | // scroll |
---|
246 | top = top + 0.02 * this->scroll_; |
---|
247 | this->consoleOverlayContainer_->setTop(top); |
---|
248 | } |
---|
249 | if (top <= -1.2 * InGameConsole::REL_HEIGHT) |
---|
250 | { |
---|
251 | // window has completely scrolled up |
---|
252 | this->scroll_ = 0; |
---|
253 | this->consoleOverlay_->hide(); |
---|
254 | this->active_ = false; |
---|
255 | } |
---|
256 | if (top >= 0) |
---|
257 | { |
---|
258 | // window has completely scrolled down |
---|
259 | this->scroll_ = 0; |
---|
260 | this->consoleOverlayContainer_->setTop(0); |
---|
261 | this->active_ = true; |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | this->cursor_ += dt; |
---|
266 | if (this->cursor_ >= 2 * InGameConsole::BLINK) |
---|
267 | this->cursor_ = 0; |
---|
268 | // print(convert2UTF(this->ib_->get())); |
---|
269 | |
---|
270 | // this creates a flickering effect |
---|
271 | this->consoleOverlayNoise_->setTiling(1, rand() % 5 + 1); |
---|
272 | } |
---|
273 | |
---|
274 | /** |
---|
275 | @brief Resizes the console elements. Call if window size changes. |
---|
276 | */ |
---|
277 | void InGameConsole::resize() |
---|
278 | { |
---|
279 | this->windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
280 | this->windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
281 | this->consoleOverlayBorder_->setWidth((int) this->windowW_* InGameConsole::REL_WIDTH); |
---|
282 | this->consoleOverlayBorder_->setHeight((int) this->windowH_ * InGameConsole::REL_HEIGHT); |
---|
283 | this->consoleOverlayNoise_->setWidth((int) this->windowW_ * InGameConsole::REL_WIDTH - 10); |
---|
284 | this->consoleOverlayNoise_->setHeight((int) this->windowH_ * InGameConsole::REL_HEIGHT - 5); |
---|
285 | // now adjust the text lines... |
---|
286 | for (int i = 0; i < LINES; i++) |
---|
287 | { |
---|
288 | this->consoleOverlayTextAreas_[i]->setWidth((int) this->windowW_ * InGameConsole::REL_WIDTH); |
---|
289 | this->consoleOverlayTextAreas_[i]->setTop((int) this->windowH_ * InGameConsole::REL_HEIGHT - 24 - 16*i); |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | /** |
---|
294 | @brief Shows the InGameConsole. |
---|
295 | */ |
---|
296 | void InGameConsole::activate() |
---|
297 | { |
---|
298 | this->consoleOverlay_->show(); |
---|
299 | // just in case window size has changed... |
---|
300 | this->resize(); |
---|
301 | // scroll down |
---|
302 | this->scroll_ = 1; |
---|
303 | // the rest is done by tick |
---|
304 | } |
---|
305 | |
---|
306 | /** |
---|
307 | @brief Hides the InGameConsole. |
---|
308 | */ |
---|
309 | void InGameConsole::deactivate() |
---|
310 | { |
---|
311 | // scroll up |
---|
312 | this->scroll_ = -1; |
---|
313 | // the rest is done by tick |
---|
314 | } |
---|
315 | |
---|
316 | /** |
---|
317 | @brief Activates the console. |
---|
318 | */ |
---|
319 | void InGameConsole::openConsole() |
---|
320 | { |
---|
321 | InGameConsole::getInstance().activate(); |
---|
322 | } |
---|
323 | |
---|
324 | /** |
---|
325 | @brief Deactivates the console. |
---|
326 | */ |
---|
327 | void InGameConsole::closeConsole() |
---|
328 | { |
---|
329 | InGameConsole::getInstance().deactivate(); |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | @brief Prints string to bottom line. |
---|
334 | @param s String to be printed |
---|
335 | */ |
---|
336 | void InGameConsole::print(Ogre::UTFString s) |
---|
337 | { |
---|
338 | if (this->cursor_ > InGameConsole::BLINK) |
---|
339 | this->consoleOverlayTextAreas_[0]->setCaption(">" + s); |
---|
340 | else |
---|
341 | this->consoleOverlayTextAreas_[0]->setCaption(">" + s + "_"); |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | @brief Shifts all lines up and clears the bottom line. |
---|
346 | */ |
---|
347 | void InGameConsole::newline() |
---|
348 | { |
---|
349 | Ogre::UTFString line; |
---|
350 | for (int i = LINES - 1; i >= 1; i--) |
---|
351 | { |
---|
352 | line = this->consoleOverlayTextAreas_[i - 1]->getCaption(); |
---|
353 | // don't copy the cursor... |
---|
354 | int l = line.length(); |
---|
355 | if (!line.empty() && line.substr(l-1) == "_") |
---|
356 | line.erase(l-1); |
---|
357 | this->consoleOverlayTextAreas_[i]->setCaption(line); |
---|
358 | } |
---|
359 | this->consoleOverlayTextAreas_[0]->setCaption(">"); |
---|
360 | } |
---|
361 | |
---|
362 | /** |
---|
363 | @brief Converts a string into an Ogre::UTFString. |
---|
364 | @param s The string to convert |
---|
365 | @return The converted string |
---|
366 | */ |
---|
367 | Ogre::UTFString InGameConsole::convert2UTF(std::string s) |
---|
368 | { |
---|
369 | Ogre::UTFString utf; |
---|
370 | Ogre::UTFString::code_point cp; |
---|
371 | for (unsigned int i = 0; i < s.size(); ++i) |
---|
372 | { |
---|
373 | cp = s[i]; |
---|
374 | cp &= 0xFF; |
---|
375 | utf.append(1, cp); |
---|
376 | } |
---|
377 | return utf; |
---|
378 | } |
---|
379 | } |
---|