[1137] | 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 | |
---|
[1143] | 29 | #include "OrxonoxStableHeaders.h" |
---|
| 30 | |
---|
[1137] | 31 | #include "InGameConsole.h" |
---|
[1143] | 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" |
---|
[1223] | 43 | #include "core/InputManager.h" |
---|
[1143] | 44 | #include "GraphicsEngine.h" |
---|
| 45 | |
---|
[1137] | 46 | #define LINES 20 |
---|
| 47 | |
---|
| 48 | namespace orxonox |
---|
| 49 | { |
---|
| 50 | using namespace Ogre; |
---|
| 51 | |
---|
| 52 | const float REL_WIDTH = 0.8; |
---|
| 53 | const float REL_HEIGHT = 0.4; |
---|
[1139] | 54 | const float BLINK = 0.25; |
---|
[1137] | 55 | |
---|
[1263] | 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 | { |
---|
[1143] | 69 | //RegisterObject(InGameConsole); |
---|
[1137] | 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(); |
---|
[1214] | 80 | print(convert2UTF(this->ib_->get())); |
---|
[1137] | 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())); |
---|
[1214] | 96 | print(convert2UTF(this->ib_->get())); |
---|
[1137] | 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(); |
---|
[1223] | 110 | InputManager::setInputState(InputManager::IS_NORMAL); |
---|
[1137] | 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | @brief called once by constructor |
---|
| 115 | */ |
---|
| 116 | void InGameConsole::init(){ |
---|
| 117 | // for the beginning, don't scroll |
---|
| 118 | scroll = 0; |
---|
[1141] | 119 | scrollTimer = 0; |
---|
[1137] | 120 | cursor = 0; |
---|
[1214] | 121 | |
---|
[1137] | 122 | // create overlay and elements |
---|
| 123 | om = &Ogre::OverlayManager::getSingleton(); |
---|
| 124 | |
---|
| 125 | // 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); |
---|
| 130 | |
---|
| 131 | // create BorderPanel |
---|
| 132 | consoleOverlayBorder = static_cast<BorderPanelOverlayElement*>(om->createOverlayElement("BorderPanel", "borderPanel")); |
---|
| 133 | consoleOverlayBorder->setMetricsMode(Ogre::GMM_PIXELS); |
---|
| 134 | consoleOverlayBorder->setMaterialName("ConsoleCenter"); |
---|
| 135 | // 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); |
---|
| 143 | |
---|
| 144 | // 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(""); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | // 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 |
---|
[1144] | 166 | consoleOverlayContainer->addChild(consoleOverlayNoise); |
---|
[1137] | 167 | for(int i = 0; i<LINES; i++) consoleOverlayContainer->addChild(consoleOverlayTextAreas[i]); |
---|
| 168 | resize(); |
---|
| 169 | |
---|
| 170 | // move overlay "above" the top edge of the screen |
---|
| 171 | // we take -1.2 because the border mkes the panel bigger |
---|
| 172 | consoleOverlayContainer->setTop(-1.2*REL_HEIGHT); |
---|
| 173 | // 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){ |
---|
[1141] | 183 | scrollTimer += dt; |
---|
| 184 | if(scrollTimer >= 0.01){ |
---|
| 185 | float top = consoleOverlayContainer->getTop(); |
---|
| 186 | scrollTimer = 0; |
---|
| 187 | if(scroll!=0){ |
---|
| 188 | // scroll |
---|
| 189 | top = top + 0.02*scroll; |
---|
| 190 | consoleOverlayContainer->setTop(top); |
---|
| 191 | } |
---|
| 192 | if(top <= -1.2*REL_HEIGHT){ |
---|
| 193 | // window has completely scrolled up |
---|
| 194 | scroll = 0; |
---|
| 195 | consoleOverlay->hide(); |
---|
| 196 | active = false; |
---|
| 197 | } |
---|
| 198 | if(top >= 0){ |
---|
| 199 | // window has completely scrolled down |
---|
| 200 | scroll = 0; |
---|
| 201 | consoleOverlayContainer->setTop(0); |
---|
| 202 | active = true; |
---|
| 203 | } |
---|
[1137] | 204 | } |
---|
| 205 | |
---|
[1139] | 206 | cursor += dt; |
---|
| 207 | if(cursor >= 2*BLINK) cursor = 0; |
---|
[1214] | 208 | print(convert2UTF(this->ib_->get())); |
---|
[1137] | 209 | |
---|
| 210 | // this creates a flickering effect |
---|
[1144] | 211 | consoleOverlayNoise->setTiling(1, rand()%5+1); |
---|
[1137] | 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(); |
---|
| 236 | // just in case window size has changed... |
---|
| 237 | resize(); |
---|
| 238 | // scroll down |
---|
| 239 | scroll = 1; |
---|
| 240 | // the rest is done by tick |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | /** |
---|
| 244 | @brief hides console |
---|
| 245 | */ |
---|
| 246 | void InGameConsole::deactivate(){ |
---|
| 247 | // scroll up |
---|
| 248 | scroll = -1; |
---|
| 249 | // the rest is done by tick |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | /** |
---|
| 253 | @brief prints string to bottom line |
---|
| 254 | @param s string to be printed |
---|
| 255 | */ |
---|
[1214] | 256 | void InGameConsole::print(Ogre::UTFString s){ |
---|
[1139] | 257 | if(cursor>BLINK) consoleOverlayTextAreas[0]->setCaption(">" + s); |
---|
[1137] | 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(){ |
---|
[1214] | 265 | Ogre::UTFString line; |
---|
[1137] | 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(">"); |
---|
[1214] | 274 | } |
---|
| 275 | |
---|
| 276 | Ogre::UTFString InGameConsole::convert2UTF(std::string s){ |
---|
| 277 | Ogre::UTFString utf; |
---|
| 278 | int i; |
---|
| 279 | Ogre::UTFString::code_point cp; |
---|
| 280 | for (i=0; i<(int)s.size(); ++i){ |
---|
| 281 | cp = s[i]; |
---|
| 282 | cp &= 0xFF; |
---|
| 283 | utf.append(1, cp); |
---|
| 284 | } |
---|
| 285 | return utf; |
---|
[1137] | 286 | } |
---|
| 287 | } |
---|