Changeset 1502 for code/trunk/src/orxonox/console
- Timestamp:
- Jun 1, 2008, 3:54:20 PM (17 years ago)
- Location:
- code/trunk/src/orxonox/console
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/orxonox/console/InGameConsole.cc
r1362 r1502 23 23 * Felix Schulthess 24 24 * Co-authors: 25 * ...25 * Fabian 'x3n' Landau 26 26 * 27 27 */ … … 40 40 #include "core/Debug.h" 41 41 #include "core/CoreIncludes.h" 42 #include "core/ConfigValueIncludes.h" 42 43 #include "core/ConsoleCommand.h" 43 44 #include "core/InputManager.h" 45 #include "util/Math.h" 44 46 #include "GraphicsEngine.h" 45 47 46 #define LINES 20 48 #define LINES 30 49 #define CHAR_WIDTH1 7.78 //34 // fix this please - determine the char-width dynamically 50 #define CHAR_WIDTH2 8.28 // fix this please - determine the char-width dynamically 51 #define CHAR_WIDTH3 7.80 //78 // fix this please - determine the char-width dynamically 47 52 48 53 namespace orxonox 49 54 { 55 SetConsoleCommand(InGameConsole, openConsole, true); 56 SetConsoleCommand(InGameConsole, closeConsole, true); 57 50 58 using namespace Ogre; 51 59 52 const float REL_WIDTH = 0.8; 53 const float REL_HEIGHT = 0.4; 54 const float BLINK = 0.25; 55 56 InGameConsole::InGameConsole(InputBuffer* ib) : 57 windowW(0), windowH(0), 58 scroll(0), scrollTimer(0.0f), 59 cursor(0.0f), 60 active(false), 61 ib_(ib), 62 om(0), 63 consoleOverlay(0), 64 consoleOverlayContainer(0), 65 consoleOverlayNoise(0), 66 consoleOverlayBorder(0), 67 consoleOverlayTextAreas(0) 68 { 69 //RegisterObject(InGameConsole); 70 init(); 71 } 72 73 InGameConsole::~InGameConsole(void){ 74 for(int i=0; i<LINES; i++) delete consoleOverlayTextAreas[i]; 75 delete consoleOverlayTextAreas; 76 } 77 78 void InGameConsole::listen(){ 79 if(!active) activate(); 80 print(convert2UTF(this->ib_->get())); 81 } 82 83 void InGameConsole::execute(){ 84 newline(); 85 if (!CommandExecutor::execute(this->ib_->get())){ 86 print("Error"); 87 newline(); 88 } 89 this->ib_->clear(); 90 } 91 92 void InGameConsole::hintandcomplete(){ 93 print(CommandExecutor::hint(this->ib_->get())); 94 newline(); 95 this->ib_->set(CommandExecutor::complete(this->ib_->get())); 96 print(convert2UTF(this->ib_->get())); 97 } 98 99 void InGameConsole::clear(){ 100 this->ib_->clear(); 101 } 102 103 void InGameConsole::removeLast(){ 104 this->ib_->removeLast(); 105 } 106 107 void InGameConsole::exit(){ 108 clear(); 109 deactivate(); 110 InputManager::setInputState(InputManager::IS_NORMAL); 111 } 112 113 /** 114 @brief called once by constructor 115 */ 116 void InGameConsole::init(){ 60 float InGameConsole::REL_WIDTH = 0.8; 61 float InGameConsole::REL_HEIGHT = 0.4; 62 float InGameConsole::BLINK = 0.5; 63 64 /** 65 @brief Constructor: Creates and initializes the InGameConsole. 66 */ 67 InGameConsole::InGameConsole() : 68 om_(0), consoleOverlay_(0), consoleOverlayContainer_(0), 69 consoleOverlayNoise_(0), consoleOverlayBorder_(0), consoleOverlayTextAreas_(0) 70 { 71 RegisterObject(InGameConsole); 72 73 this->active_ = false; 74 this->cursor_ = 0.0; 75 this->cursorSymbol_ = '|'; 76 this->inputWindowStart_ = 0; 77 this->numLinesShifted_ = LINES - 1; 78 79 this->init(); 80 this->setConfigValues(); 81 82 Shell::getInstance().addOutputLevel(true); 83 } 84 85 /** 86 @brief Destructor: Destroys the TextAreas. 87 */ 88 InGameConsole::~InGameConsole(void) 89 { 90 /*for (int i = 0; i < LINES; i++) 91 if (this->consoleOverlayTextAreas_[i]) 92 om_->destroyOverlayElement(this->consoleOverlayTextAreas_[i]); 93 94 if (this->consoleOverlayTextAreas_) 95 delete[] this->consoleOverlayTextAreas_;*/ 96 } 97 98 /** 99 @brief Returns a reference to the only existing instance of InGameConsole. 100 */ 101 InGameConsole& InGameConsole::getInstance() 102 { 103 static InGameConsole instance; 104 return instance; 105 } 106 107 /** 108 @brief Sets the config values, describing the size of the console. 109 */ 110 void InGameConsole::setConfigValues() 111 { 112 SetConfigValue(REL_WIDTH, 0.8); 113 SetConfigValue(REL_HEIGHT, 0.4); 114 SetConfigValue(BLINK, 0.5); 115 } 116 117 /** 118 @brief Called if all output-lines have to be redrawn. 119 */ 120 void InGameConsole::linesChanged() 121 { 122 std::list<std::string>::const_iterator it = Shell::getInstance().getNewestLineIterator(); 123 int max = 0; 124 for (int i = 1; i < LINES; ++i) 125 { 126 if (it != Shell::getInstance().getEndIterator()) 127 { 128 ++it; 129 max = i; 130 } 131 else 132 break; 133 } 134 135 for (int i = LINES - 1; i > max; --i) 136 this->print("", i, true); 137 138 for (int i = max; i >= 1; --i) 139 { 140 --it; 141 this->print(*it, i, true); 142 } 143 } 144 145 /** 146 @brief Called if only the last output-line has changed. 147 */ 148 void InGameConsole::onlyLastLineChanged() 149 { 150 if (LINES > 1) 151 this->print(*Shell::getInstance().getNewestLineIterator(), 1); 152 } 153 154 /** 155 @brief Called if a new output-line was added. 156 */ 157 void InGameConsole::lineAdded() 158 { 159 this->numLinesShifted_ = 0; 160 this->shiftLines(); 161 this->onlyLastLineChanged(); 162 } 163 164 /** 165 @brief Called if the text in the input-line has changed. 166 */ 167 void InGameConsole::inputChanged() 168 { 169 if (LINES > 0) 170 this->print(Shell::getInstance().getInput(), 0); 171 172 if (Shell::getInstance().getInput() == "" || Shell::getInstance().getInput().size() == 0) 173 this->inputWindowStart_ = 0; 174 } 175 176 /** 177 @brief Called if the position of the cursor in the input-line has changed. 178 */ 179 void InGameConsole::cursorChanged() 180 { 181 /*std::string input = Shell::getInstance().getInput(); 182 input.insert(Shell::getInstance().getCursorPosition(), 1, this->cursorSymbol_); 183 if (LINES > 0) 184 this->print(input, 0);*/ 185 this->setCursorPosition(Shell::getInstance().getCursorPosition() - inputWindowStart_); 186 } 187 188 /** 189 @brief Called if the console gets closed. 190 */ 191 void InGameConsole::exit() 192 { 193 this->deactivate(); 194 } 195 196 /** 197 @brief Called once by constructor, initializes the InGameConsole. 198 */ 199 void InGameConsole::init() 200 { 117 201 // for the beginning, don't scroll 118 scroll= 0;119 scrollTimer= 0;120 cursor= 0;202 this->scroll_ = 0; 203 this->scrollTimer_ = 0; 204 this->cursor_ = 0; 121 205 122 206 // create overlay and elements 123 om= &Ogre::OverlayManager::getSingleton();207 this->om_ = &Ogre::OverlayManager::getSingleton(); 124 208 125 209 // create a container 126 consoleOverlayContainer = static_cast<OverlayContainer*>(om->createOverlayElement("Panel", "container"));127 consoleOverlayContainer->setMetricsMode(Ogre::GMM_RELATIVE);128 consoleOverlayContainer->setPosition((1-REL_WIDTH)/2, 0);129 consoleOverlayContainer->setDimensions(REL_WIDTH,REL_HEIGHT);210 this->consoleOverlayContainer_ = static_cast<OverlayContainer*>(this->om_->createOverlayElement("Panel", "InGameConsoleContainer")); 211 this->consoleOverlayContainer_->setMetricsMode(Ogre::GMM_RELATIVE); 212 this->consoleOverlayContainer_->setPosition((1 - InGameConsole::REL_WIDTH) / 2, 0); 213 this->consoleOverlayContainer_->setDimensions(InGameConsole::REL_WIDTH, InGameConsole::REL_HEIGHT); 130 214 131 215 // create BorderPanel 132 consoleOverlayBorder = static_cast<BorderPanelOverlayElement*>(om->createOverlayElement("BorderPanel", "borderPanel"));133 consoleOverlayBorder->setMetricsMode(Ogre::GMM_PIXELS);134 consoleOverlayBorder->setMaterialName("ConsoleCenter");216 this->consoleOverlayBorder_ = static_cast<BorderPanelOverlayElement*>(this->om_->createOverlayElement("BorderPanel", "InGameConsoleBorderPanel")); 217 this->consoleOverlayBorder_->setMetricsMode(Ogre::GMM_PIXELS); 218 this->consoleOverlayBorder_->setMaterialName("ConsoleCenter"); 135 219 // set parameters for border 136 consoleOverlayBorder->setBorderSize(16, 16, 0, 16);137 consoleOverlayBorder->setBorderMaterialName("ConsoleBorder");138 consoleOverlayBorder->setLeftBorderUV(0.0, 0.49, 0.5, 0.51);139 consoleOverlayBorder->setRightBorderUV(0.5, 0.49, 1.0, 0.5);140 consoleOverlayBorder->setBottomBorderUV(0.49, 0.5, 0.51, 1.0);141 consoleOverlayBorder->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0);142 consoleOverlayBorder->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0);220 this->consoleOverlayBorder_->setBorderSize(16, 16, 0, 16); 221 this->consoleOverlayBorder_->setBorderMaterialName("ConsoleBorder"); 222 this->consoleOverlayBorder_->setLeftBorderUV(0.0, 0.49, 0.5, 0.51); 223 this->consoleOverlayBorder_->setRightBorderUV(0.5, 0.49, 1.0, 0.5); 224 this->consoleOverlayBorder_->setBottomBorderUV(0.49, 0.5, 0.51, 1.0); 225 this->consoleOverlayBorder_->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0); 226 this->consoleOverlayBorder_->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0); 143 227 144 228 // create the text lines 145 consoleOverlayTextAreas = new TextAreaOverlayElement*[LINES]; 146 for(int i = 0; i<LINES; i++){ 147 consoleOverlayTextAreas[i] = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "textArea"+Ogre::StringConverter::toString(i))); 148 consoleOverlayTextAreas[i]->setMetricsMode(Ogre::GMM_PIXELS); 149 consoleOverlayTextAreas[i]->setFontName("Console"); 150 consoleOverlayTextAreas[i]->setCharHeight(20); 151 consoleOverlayTextAreas[i]->setParameter("colour_top", "0.21 0.69 0.21"); 152 consoleOverlayTextAreas[i]->setLeft(8); 153 consoleOverlayTextAreas[i]->setCaption(""); 229 this->consoleOverlayTextAreas_ = new TextAreaOverlayElement*[LINES]; 230 for (int i = 0; i < LINES; i++) 231 { 232 this->consoleOverlayTextAreas_[i] = static_cast<TextAreaOverlayElement*>(this->om_->createOverlayElement("TextArea", "InGameConsoleTextArea" + Ogre::StringConverter::toString(i))); 233 this->consoleOverlayTextAreas_[i]->setMetricsMode(Ogre::GMM_PIXELS); 234 this->consoleOverlayTextAreas_[i]->setFontName("Console"); 235 this->consoleOverlayTextAreas_[i]->setCharHeight(18); 236 this->consoleOverlayTextAreas_[i]->setParameter("colour_top", "0.21 0.69 0.21"); 237 this->consoleOverlayTextAreas_[i]->setLeft(8); 238 this->consoleOverlayTextAreas_[i]->setCaption(""); 154 239 } 155 240 156 241 // create noise 157 consoleOverlayNoise = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "noise")); 158 consoleOverlayNoise->setMetricsMode(Ogre::GMM_PIXELS); 159 consoleOverlayNoise->setPosition(5,0); 160 consoleOverlayNoise->setMaterialName("ConsoleNoise"); 161 162 consoleOverlay = om->create("Console"); 163 consoleOverlay->add2D(consoleOverlayContainer); 164 consoleOverlayContainer->addChild(consoleOverlayBorder); 165 //comment following line to disable noise 166 consoleOverlayContainer->addChild(consoleOverlayNoise); 167 for(int i = 0; i<LINES; i++) consoleOverlayContainer->addChild(consoleOverlayTextAreas[i]); 168 resize(); 242 this->consoleOverlayNoise_ = static_cast<PanelOverlayElement*>(this->om_->createOverlayElement("Panel", "InGameConsoleNoise")); 243 this->consoleOverlayNoise_->setMetricsMode(Ogre::GMM_PIXELS); 244 this->consoleOverlayNoise_->setPosition(5,0); 245 this->consoleOverlayNoise_->setMaterialName("ConsoleNoise"); 246 247 // create cursor 248 this->consoleOverlayCursor_ = static_cast<PanelOverlayElement*>(this->om_->createOverlayElement("Panel", "InGameConsoleCursor")); 249 this->consoleOverlayCursor_->setMetricsMode(Ogre::GMM_PIXELS); 250 this->consoleOverlayCursor_->setPosition(5,219); 251 this->consoleOverlayCursor_->setDimensions(1, 14); 252 this->consoleOverlayCursor_->setMaterialName("Orxonox/GreenDot"); 253 254 this->consoleOverlay_ = this->om_->create("InGameConsoleConsole"); 255 this->consoleOverlay_->add2D(this->consoleOverlayContainer_); 256 this->consoleOverlayContainer_->addChild(this->consoleOverlayBorder_); 257 this->consoleOverlayContainer_->addChild(this->consoleOverlayCursor_); 258 //comment following line to disable noise 259 this->consoleOverlayContainer_->addChild(this->consoleOverlayNoise_); 260 for (int i = 0; i < LINES; i++) 261 this->consoleOverlayContainer_->addChild(this->consoleOverlayTextAreas_[i]); 262 263 this->resize(); 169 264 170 265 // move overlay "above" the top edge of the screen 171 266 // we take -1.2 because the border mkes the panel bigger 172 consoleOverlayContainer->setTop(-1.2*REL_HEIGHT);267 this->consoleOverlayContainer_->setTop(-1.2 * InGameConsole::REL_HEIGHT); 173 268 // show overlay 174 consoleOverlay->show(); 175 176 COUT(3) << "Info: InGameConsole initialized" << std::endl; 177 } 178 179 /** 180 @brief used to control the actual scrolling and cursor 181 */ 182 void InGameConsole::tick(float dt){ 183 scrollTimer += dt; 184 if(scrollTimer >= 0.01){ 185 float top = consoleOverlayContainer->getTop(); 186 scrollTimer = 0; 187 if(scroll!=0){ 269 this->consoleOverlay_->show(); 270 271 COUT(4) << "Info: InGameConsole initialized" << std::endl; 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 286 // now adjust the text lines... 287 this->desiredTextWidth_ = (int) (this->windowW_ * InGameConsole::REL_WIDTH) - 12; 288 289 if (LINES > 0) 290 this->maxCharsPerLine_ = max((unsigned int)10, (unsigned int) ((float)this->desiredTextWidth_ / CHAR_WIDTH3)); 291 else 292 this->maxCharsPerLine_ = 10; 293 294 for (int i = 0; i < LINES; i++) 295 { 296 this->consoleOverlayTextAreas_[i]->setWidth(this->desiredTextWidth_); 297 this->consoleOverlayTextAreas_[i]->setTop((int) this->windowH_ * InGameConsole::REL_HEIGHT - 24 - 14*i); 298 } 299 300 this->linesChanged(); 301 } 302 303 /** 304 @brief Used to control the actual scrolling and the cursor. 305 */ 306 void InGameConsole::tick(float dt) 307 { 308 this->scrollTimer_ += dt; 309 if (this->scrollTimer_ >= 0.01) 310 { 311 float top = this->consoleOverlayContainer_->getTop(); 312 float timePassed = scrollTimer_; 313 this->scrollTimer_ = 0; 314 if (this->scroll_ != 0) 315 { 188 316 // scroll 189 top = top + 0.02*scroll;190 consoleOverlayContainer->setTop(top);317 top = top + timePassed * this->scroll_; 318 this->consoleOverlayContainer_->setTop(top); 191 319 } 192 if(top <= -1.2*REL_HEIGHT){ 320 if (top <= -1.2 * InGameConsole::REL_HEIGHT) 321 { 193 322 // window has completely scrolled up 194 scroll = 0; 195 consoleOverlay->hide(); 196 active = false; 323 this->scroll_ = 0; 324 this->consoleOverlay_->hide(); 325 this->active_ = false; 326 Shell::getInstance().unregisterListener(this); 197 327 } 198 if(top >= 0){ 328 if (top >= 0) 329 { 199 330 // window has completely scrolled down 200 scroll= 0;201 consoleOverlayContainer->setTop(0);202 active= true;331 this->scroll_ = 0; 332 this->consoleOverlayContainer_->setTop(0); 333 this->active_ = true; 203 334 } 204 335 } 205 336 206 cursor += dt; 207 if(cursor >= 2*BLINK) cursor = 0; 208 print(convert2UTF(this->ib_->get())); 209 210 // this creates a flickering effect 211 consoleOverlayNoise->setTiling(1, rand()%5+1); 212 } 213 214 /** 215 @brief resizes the console elements. call if window size changes 216 */ 217 void InGameConsole::resize(){ 218 windowW = GraphicsEngine::getSingleton().getWindowWidth(); 219 windowH = GraphicsEngine::getSingleton().getWindowHeight(); 220 consoleOverlayBorder->setWidth((int) windowW*REL_WIDTH); 221 consoleOverlayBorder->setHeight((int) windowH*REL_HEIGHT); 222 consoleOverlayNoise->setWidth((int) windowW*REL_WIDTH - 10); 223 consoleOverlayNoise->setHeight((int) windowH*REL_HEIGHT - 5); 224 // now adjust the text lines... 225 for(int i = 0; i<LINES; i++){ 226 consoleOverlayTextAreas[i]->setWidth(windowW*REL_WIDTH); 227 consoleOverlayTextAreas[i]->setTop((int)windowH*REL_HEIGHT - 24 - 16*i); 228 } 229 } 230 231 /** 232 @brief shows console 233 */ 234 void InGameConsole::activate(){ 235 consoleOverlay->show(); 337 this->cursor_ += dt; 338 if (this->cursor_ >= InGameConsole::BLINK) 339 { 340 this->cursor_ = 0; 341 bShowCursor_ = !bShowCursor_; 342 if (bShowCursor_) 343 this->consoleOverlayCursor_->show(); 344 else 345 this->consoleOverlayCursor_->hide(); 346 } 347 348 /*if (this->cursor_ >= 2 * InGameConsole::BLINK) 349 this->cursor_ = 0; 350 351 if (this->cursor_ >= InGameConsole::BLINK && this->cursorSymbol_ == '|') 352 { 353 this->cursorSymbol_ = ' '; 354 this->cursorChanged(); 355 } 356 else if (this->cursor_ < InGameConsole::BLINK && this->cursorSymbol_ == ' ') 357 { 358 this->cursorSymbol_ = '|'; 359 this->cursorChanged(); 360 }*/ 361 362 // this creates a flickering effect 363 this->consoleOverlayNoise_->setTiling(1, rand() % 5 + 1); 364 } 365 366 /** 367 @brief Shows the InGameConsole. 368 */ 369 void InGameConsole::activate() 370 { 371 InputManager::setInputState(InputManager::IS_CONSOLE); 372 Shell::getInstance().registerListener(this); 373 this->linesChanged(); 374 375 this->consoleOverlay_->show(); 236 376 // just in case window size has changed... 237 resize();377 this->resize(); 238 378 // scroll down 239 scroll= 1;379 this->scroll_ = 1; 240 380 // the rest is done by tick 241 381 } 242 382 243 383 /** 244 @brief hides console 245 */ 246 void InGameConsole::deactivate(){ 384 @brief Hides the InGameConsole. 385 */ 386 void InGameConsole::deactivate() 387 { 247 388 // scroll up 248 scroll= -1;389 this->scroll_ = -1; 249 390 // the rest is done by tick 250 } 251 252 /** 253 @brief prints string to bottom line 254 @param s string to be printed 255 */ 256 void InGameConsole::print(Ogre::UTFString s){ 257 if(cursor>BLINK) consoleOverlayTextAreas[0]->setCaption(">" + s); 258 else consoleOverlayTextAreas[0]->setCaption(">" + s + "_"); 259 } 260 261 /** 262 @brief shifts all lines up and clears the bottom line 263 */ 264 void InGameConsole::newline(){ 265 Ogre::UTFString line; 266 for(int i = LINES-1; i>=1; i--){ 267 line = consoleOverlayTextAreas[i-1]->getCaption(); 268 // don't copy the cursor... 269 int l = line.length(); 270 if(!line.empty() && line.substr(l-1) == "_") line.erase(l-1); 271 consoleOverlayTextAreas[i]->setCaption(line); 272 } 273 consoleOverlayTextAreas[0]->setCaption(">"); 274 } 275 276 Ogre::UTFString InGameConsole::convert2UTF(std::string s){ 391 InputManager::setInputState(InputManager::IS_NORMAL); 392 } 393 394 /** 395 @brief Activates the console. 396 */ 397 void InGameConsole::openConsole() 398 { 399 InGameConsole::getInstance().activate(); 400 } 401 402 /** 403 @brief Deactivates the console. 404 */ 405 void InGameConsole::closeConsole() 406 { 407 InGameConsole::getInstance().deactivate(); 408 } 409 410 /** 411 @brief Shifts all output lines one line up 412 */ 413 void InGameConsole::shiftLines() 414 { 415 for (unsigned int i = LINES - 1; i > 1; --i) 416 { 417 this->consoleOverlayTextAreas_[i]->setCaption(this->consoleOverlayTextAreas_[i - 1]->getCaption()); 418 this->consoleOverlayTextAreas_[i]->setColourTop(this->consoleOverlayTextAreas_[i - 1]->getColourTop()); 419 this->consoleOverlayTextAreas_[i]->setColourBottom(this->consoleOverlayTextAreas_[i - 1]->getColourBottom()); 420 } 421 } 422 423 void InGameConsole::colourLine(int colourcode, int index) 424 { 425 if (colourcode == -1) 426 { 427 this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.90, 0.90, 0.90, 1.00)); 428 this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 1.00, 1.00, 1.00)); 429 } 430 else if (colourcode == 1) 431 { 432 this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.95, 0.25, 0.25, 1.00)); 433 this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.50, 0.50, 1.00)); 434 } 435 else if (colourcode == 2) 436 { 437 this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.95, 0.50, 0.20, 1.00)); 438 this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.70, 0.50, 1.00)); 439 } 440 else if (colourcode == 3) 441 { 442 this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.50, 0.50, 0.95, 1.00)); 443 this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.80, 1.00, 1.00)); 444 } 445 else if (colourcode == 4) 446 { 447 this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.65, 0.48, 0.44, 1.00)); 448 this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.90, 0.90, 1.00)); 449 } 450 else if (colourcode == 5) 451 { 452 this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.40, 0.20, 0.40, 1.00)); 453 this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.60, 0.80, 1.00)); 454 } 455 else 456 { 457 this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.21, 0.69, 0.21, 1.00)); 458 this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 1.00, 0.80, 1.00)); 459 } 460 } 461 462 void InGameConsole::setCursorPosition(int pos) 463 { 464 static std::string char1 = "bdefgilpqtzCEGIJKNOPQT5[}äü"; 465 static std::string char2 = "Z4"; 466 467 if (pos > (int)maxCharsPerLine_) 468 pos = maxCharsPerLine_; 469 else if (pos < 0) 470 pos = 0; 471 472 float width = 0; 473 for (int i = 0; i < pos; ++i) 474 { 475 if (char1.find(displayedText_[i]) != std::string::npos) 476 width += CHAR_WIDTH1; 477 else if (char2.find(displayedText_[i]) != std::string::npos) 478 width += CHAR_WIDTH2; 479 else 480 width += CHAR_WIDTH3; 481 } 482 this->consoleOverlayCursor_->setPosition(width + 5, this->windowH_ * InGameConsole::REL_HEIGHT - 20); 483 } 484 485 /** 486 @brief Prints string to bottom line. 487 @param s String to be printed 488 */ 489 void InGameConsole::print(const std::string& text, int index, bool alwaysShift) 490 { 491 char level = 0; 492 if (text.size() > 0) 493 level = text[0]; 494 495 std::string output = text; 496 497 if (level >= -1 && level <= 5) 498 output.erase(0, 1); 499 500 if (LINES > index) 501 { 502 this->colourLine(level, index); 503 504 if (index > 0) 505 { 506 unsigned int linesUsed = 1; 507 while (output.size() > this->maxCharsPerLine_) 508 { 509 ++linesUsed; 510 this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output.substr(0, this->maxCharsPerLine_))); 511 output.erase(0, this->maxCharsPerLine_); 512 output.insert(0, 1, ' '); 513 if (linesUsed > numLinesShifted_ || alwaysShift) 514 this->shiftLines(); 515 this->colourLine(level, index); 516 } 517 this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output)); 518 this->displayedText_ = output; 519 this->numLinesShifted_ = linesUsed; 520 } 521 else 522 { 523 if (output.size() > this->maxCharsPerLine_) 524 { 525 if (Shell::getInstance().getInputBuffer().getCursorPosition() < this->inputWindowStart_) 526 this->inputWindowStart_ = Shell::getInstance().getInputBuffer().getCursorPosition(); 527 else if (Shell::getInstance().getInputBuffer().getCursorPosition() >= (this->inputWindowStart_ + this->maxCharsPerLine_ - 1)) 528 this->inputWindowStart_ = Shell::getInstance().getInputBuffer().getCursorPosition() - this->maxCharsPerLine_ + 1; 529 530 output = output.substr(this->inputWindowStart_, this->maxCharsPerLine_); 531 } 532 else 533 this->inputWindowStart_ = 0; 534 this->displayedText_ = output; 535 this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output)); 536 } 537 } 538 } 539 540 /** 541 @brief Converts a string into an Ogre::UTFString. 542 @param s The string to convert 543 @return The converted string 544 */ 545 Ogre::UTFString InGameConsole::convert2UTF(std::string s) 546 { 277 547 Ogre::UTFString utf; 278 int i;279 548 Ogre::UTFString::code_point cp; 280 for (i=0; i<(int)s.size(); ++i){ 549 for (unsigned int i = 0; i < s.size(); ++i) 550 { 281 551 cp = s[i]; 282 552 cp &= 0xFF; -
code/trunk/src/orxonox/console/InGameConsole.h
r1214 r1502 23 23 * Felix Schulthess 24 24 * Co-authors: 25 * ...25 * Fabian 'x3n' Landau 26 26 * 27 27 */ … … 37 37 38 38 #include "core/Tickable.h" 39 #include "core/ InputBuffer.h"39 #include "core/Shell.h" 40 40 41 41 42 42 namespace orxonox 43 43 { 44 class _OrxonoxExport InGameConsole : public InputBufferListener44 class _OrxonoxExport InGameConsole : public TickableReal, public ShellListener 45 45 { 46 46 public: 47 InGameConsole(InputBuffer* ib); 48 ~InGameConsole(); 49 void listen(); 50 void execute(); 51 void hintandcomplete(); 52 void clear(); 53 void removeLast(); 54 void exit(); 55 void init(); 47 static InGameConsole& getInstance(); 48 49 void setConfigValues(); 56 50 void tick(float dt); 51 57 52 void activate(); 58 53 void deactivate(); 54 void resize(); 55 56 static void openConsole(); 57 static void closeConsole(); 59 58 60 59 private: 61 void resize(); 62 void print(Ogre::UTFString s); 63 void newline(); 64 Ogre::UTFString convert2UTF(std::string s); 60 InGameConsole(); 61 InGameConsole(const InGameConsole& other); 62 ~InGameConsole(); 65 63 66 int windowW; 67 int windowH; 68 int scroll; 69 float scrollTimer; 70 float cursor; 71 bool active; 72 InputBuffer* ib_; 73 Ogre::OverlayManager* om; 74 Ogre::Overlay* consoleOverlay; 75 Ogre::OverlayContainer* consoleOverlayContainer; 76 Ogre::PanelOverlayElement* consoleOverlayNoise; 77 Ogre::BorderPanelOverlayElement* consoleOverlayBorder; 78 Ogre::TextAreaOverlayElement** consoleOverlayTextAreas; 64 virtual void linesChanged(); 65 virtual void onlyLastLineChanged(); 66 virtual void lineAdded(); 67 virtual void inputChanged(); 68 virtual void cursorChanged(); 69 virtual void exit(); 70 71 void init(); 72 void shiftLines(); 73 void colourLine(int colourcode, int index); 74 void setCursorPosition(int pos); 75 void print(const std::string& text, int index, bool alwaysShift = false); 76 static Ogre::UTFString convert2UTF(std::string s); 77 78 static float REL_WIDTH; 79 static float REL_HEIGHT; 80 static float BLINK; 81 82 int windowW_; 83 int windowH_; 84 int desiredTextWidth_; 85 unsigned int maxCharsPerLine_; 86 unsigned int numLinesShifted_; 87 int scroll_; 88 float scrollTimer_; 89 float cursor_; 90 unsigned int inputWindowStart_; 91 char cursorSymbol_; 92 bool active_; 93 bool bShowCursor_; 94 std::string displayedText_; 95 Ogre::OverlayManager* om_; 96 Ogre::Overlay* consoleOverlay_; 97 Ogre::OverlayContainer* consoleOverlayContainer_; 98 Ogre::PanelOverlayElement* consoleOverlayNoise_; 99 Ogre::PanelOverlayElement* consoleOverlayCursor_; 100 Ogre::BorderPanelOverlayElement* consoleOverlayBorder_; 101 Ogre::TextAreaOverlayElement** consoleOverlayTextAreas_; 79 102 }; 80 103 }
Note: See TracChangeset
for help on using the changeset viewer.