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 | * ... |
---|
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/ConsoleCommand.h" |
---|
43 | #include "GraphicsEngine.h" |
---|
44 | |
---|
45 | #define LINES 20 |
---|
46 | |
---|
47 | namespace orxonox |
---|
48 | { |
---|
49 | using namespace Ogre; |
---|
50 | |
---|
51 | const float REL_WIDTH = 0.8; |
---|
52 | const float REL_HEIGHT = 0.4; |
---|
53 | const float BLINK = 0.25; |
---|
54 | |
---|
55 | InGameConsole::InGameConsole(InputBuffer* ib){ |
---|
56 | //RegisterObject(InGameConsole); |
---|
57 | ib_ = ib; |
---|
58 | active = false; |
---|
59 | cursor = 0.0; |
---|
60 | init(); |
---|
61 | } |
---|
62 | |
---|
63 | InGameConsole::~InGameConsole(void){ |
---|
64 | for(int i=0; i<LINES; i++) delete consoleOverlayTextAreas[i]; |
---|
65 | delete consoleOverlayTextAreas; |
---|
66 | } |
---|
67 | |
---|
68 | void InGameConsole::listen(){ |
---|
69 | if(!active) activate(); |
---|
70 | print(convert2UTF(this->ib_->get())); |
---|
71 | } |
---|
72 | |
---|
73 | void InGameConsole::execute(){ |
---|
74 | newline(); |
---|
75 | if (!CommandExecutor::execute(this->ib_->get())){ |
---|
76 | print("Error"); |
---|
77 | newline(); |
---|
78 | } |
---|
79 | this->ib_->clear(); |
---|
80 | } |
---|
81 | |
---|
82 | void InGameConsole::hintandcomplete(){ |
---|
83 | print(CommandExecutor::hint(this->ib_->get())); |
---|
84 | newline(); |
---|
85 | this->ib_->set(CommandExecutor::complete(this->ib_->get())); |
---|
86 | print(convert2UTF(this->ib_->get())); |
---|
87 | } |
---|
88 | |
---|
89 | void InGameConsole::clear(){ |
---|
90 | this->ib_->clear(); |
---|
91 | } |
---|
92 | |
---|
93 | void InGameConsole::removeLast(){ |
---|
94 | this->ib_->removeLast(); |
---|
95 | } |
---|
96 | |
---|
97 | void InGameConsole::exit(){ |
---|
98 | clear(); |
---|
99 | deactivate(); |
---|
100 | CommandExecutor::execute("setInputMode 2"); |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | @brief called once by constructor |
---|
105 | */ |
---|
106 | void InGameConsole::init(){ |
---|
107 | // for the beginning, don't scroll |
---|
108 | scroll = 0; |
---|
109 | scrollTimer = 0; |
---|
110 | cursor = 0; |
---|
111 | |
---|
112 | // create overlay and elements |
---|
113 | om = &Ogre::OverlayManager::getSingleton(); |
---|
114 | |
---|
115 | // create a container |
---|
116 | consoleOverlayContainer = static_cast<OverlayContainer*>(om->createOverlayElement("Panel", "container")); |
---|
117 | consoleOverlayContainer->setMetricsMode(Ogre::GMM_RELATIVE); |
---|
118 | consoleOverlayContainer->setPosition((1-REL_WIDTH)/2, 0); |
---|
119 | consoleOverlayContainer->setDimensions(REL_WIDTH, REL_HEIGHT); |
---|
120 | |
---|
121 | // create BorderPanel |
---|
122 | consoleOverlayBorder = static_cast<BorderPanelOverlayElement*>(om->createOverlayElement("BorderPanel", "borderPanel")); |
---|
123 | consoleOverlayBorder->setMetricsMode(Ogre::GMM_PIXELS); |
---|
124 | consoleOverlayBorder->setMaterialName("ConsoleCenter"); |
---|
125 | // set parameters for border |
---|
126 | consoleOverlayBorder->setBorderSize(16, 16, 0, 16); |
---|
127 | consoleOverlayBorder->setBorderMaterialName("ConsoleBorder"); |
---|
128 | consoleOverlayBorder->setLeftBorderUV(0.0, 0.49, 0.5, 0.51); |
---|
129 | consoleOverlayBorder->setRightBorderUV(0.5, 0.49, 1.0, 0.5); |
---|
130 | consoleOverlayBorder->setBottomBorderUV(0.49, 0.5, 0.51, 1.0); |
---|
131 | consoleOverlayBorder->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0); |
---|
132 | consoleOverlayBorder->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0); |
---|
133 | |
---|
134 | // create the text lines |
---|
135 | consoleOverlayTextAreas = new TextAreaOverlayElement*[LINES]; |
---|
136 | for(int i = 0; i<LINES; i++){ |
---|
137 | consoleOverlayTextAreas[i] = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "textArea"+Ogre::StringConverter::toString(i))); |
---|
138 | consoleOverlayTextAreas[i]->setMetricsMode(Ogre::GMM_PIXELS); |
---|
139 | consoleOverlayTextAreas[i]->setFontName("Console"); |
---|
140 | consoleOverlayTextAreas[i]->setCharHeight(20); |
---|
141 | consoleOverlayTextAreas[i]->setParameter("colour_top", "0.21 0.69 0.21"); |
---|
142 | consoleOverlayTextAreas[i]->setLeft(8); |
---|
143 | consoleOverlayTextAreas[i]->setCaption(""); |
---|
144 | } |
---|
145 | |
---|
146 | // create noise |
---|
147 | consoleOverlayNoise = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "noise")); |
---|
148 | consoleOverlayNoise->setMetricsMode(Ogre::GMM_PIXELS); |
---|
149 | consoleOverlayNoise->setPosition(5,0); |
---|
150 | consoleOverlayNoise->setMaterialName("ConsoleNoise"); |
---|
151 | |
---|
152 | consoleOverlay = om->create("Console"); |
---|
153 | consoleOverlay->add2D(consoleOverlayContainer); |
---|
154 | consoleOverlayContainer->addChild(consoleOverlayBorder); |
---|
155 | //comment following line to disable noise |
---|
156 | consoleOverlayContainer->addChild(consoleOverlayNoise); |
---|
157 | for(int i = 0; i<LINES; i++) consoleOverlayContainer->addChild(consoleOverlayTextAreas[i]); |
---|
158 | resize(); |
---|
159 | |
---|
160 | // move overlay "above" the top edge of the screen |
---|
161 | // we take -1.2 because the border mkes the panel bigger |
---|
162 | consoleOverlayContainer->setTop(-1.2*REL_HEIGHT); |
---|
163 | // show overlay |
---|
164 | consoleOverlay->show(); |
---|
165 | |
---|
166 | COUT(3) << "Info: InGameConsole initialized" << std::endl; |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | @brief used to control the actual scrolling and cursor |
---|
171 | */ |
---|
172 | void InGameConsole::tick(float dt){ |
---|
173 | scrollTimer += dt; |
---|
174 | if(scrollTimer >= 0.01){ |
---|
175 | float top = consoleOverlayContainer->getTop(); |
---|
176 | scrollTimer = 0; |
---|
177 | if(scroll!=0){ |
---|
178 | // scroll |
---|
179 | top = top + 0.02*scroll; |
---|
180 | consoleOverlayContainer->setTop(top); |
---|
181 | } |
---|
182 | if(top <= -1.2*REL_HEIGHT){ |
---|
183 | // window has completely scrolled up |
---|
184 | scroll = 0; |
---|
185 | consoleOverlay->hide(); |
---|
186 | active = false; |
---|
187 | } |
---|
188 | if(top >= 0){ |
---|
189 | // window has completely scrolled down |
---|
190 | scroll = 0; |
---|
191 | consoleOverlayContainer->setTop(0); |
---|
192 | active = true; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | cursor += dt; |
---|
197 | if(cursor >= 2*BLINK) cursor = 0; |
---|
198 | print(convert2UTF(this->ib_->get())); |
---|
199 | |
---|
200 | // this creates a flickering effect |
---|
201 | consoleOverlayNoise->setTiling(1, rand()%5+1); |
---|
202 | } |
---|
203 | |
---|
204 | /** |
---|
205 | @brief resizes the console elements. call if window size changes |
---|
206 | */ |
---|
207 | void InGameConsole::resize(){ |
---|
208 | windowW = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
209 | windowH = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
210 | consoleOverlayBorder->setWidth((int) windowW*REL_WIDTH); |
---|
211 | consoleOverlayBorder->setHeight((int) windowH*REL_HEIGHT); |
---|
212 | consoleOverlayNoise->setWidth((int) windowW*REL_WIDTH - 10); |
---|
213 | consoleOverlayNoise->setHeight((int) windowH*REL_HEIGHT - 5); |
---|
214 | // now adjust the text lines... |
---|
215 | for(int i = 0; i<LINES; i++){ |
---|
216 | consoleOverlayTextAreas[i]->setWidth(windowW*REL_WIDTH); |
---|
217 | consoleOverlayTextAreas[i]->setTop((int)windowH*REL_HEIGHT - 24 - 16*i); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | @brief shows console |
---|
223 | */ |
---|
224 | void InGameConsole::activate(){ |
---|
225 | consoleOverlay->show(); |
---|
226 | // just in case window size has changed... |
---|
227 | resize(); |
---|
228 | // scroll down |
---|
229 | scroll = 1; |
---|
230 | // the rest is done by tick |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | @brief hides console |
---|
235 | */ |
---|
236 | void InGameConsole::deactivate(){ |
---|
237 | // scroll up |
---|
238 | scroll = -1; |
---|
239 | // the rest is done by tick |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | @brief prints string to bottom line |
---|
244 | @param s string to be printed |
---|
245 | */ |
---|
246 | void InGameConsole::print(Ogre::UTFString s){ |
---|
247 | if(cursor>BLINK) consoleOverlayTextAreas[0]->setCaption(">" + s); |
---|
248 | else consoleOverlayTextAreas[0]->setCaption(">" + s + "_"); |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | @brief shifts all lines up and clears the bottom line |
---|
253 | */ |
---|
254 | void InGameConsole::newline(){ |
---|
255 | Ogre::UTFString line; |
---|
256 | for(int i = LINES-1; i>=1; i--){ |
---|
257 | line = consoleOverlayTextAreas[i-1]->getCaption(); |
---|
258 | // don't copy the cursor... |
---|
259 | int l = line.length(); |
---|
260 | if(!line.empty() && line.substr(l-1) == "_") line.erase(l-1); |
---|
261 | consoleOverlayTextAreas[i]->setCaption(line); |
---|
262 | } |
---|
263 | consoleOverlayTextAreas[0]->setCaption(">"); |
---|
264 | } |
---|
265 | |
---|
266 | Ogre::UTFString InGameConsole::convert2UTF(std::string s){ |
---|
267 | Ogre::UTFString utf; |
---|
268 | int i; |
---|
269 | Ogre::UTFString::code_point cp; |
---|
270 | for (i=0; i<(int)s.size(); ++i){ |
---|
271 | cp = s[i]; |
---|
272 | cp &= 0xFF; |
---|
273 | utf.append(1, cp); |
---|
274 | } |
---|
275 | return utf; |
---|
276 | } |
---|
277 | } |
---|