| 1 | /* | 
|---|
| 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 |    Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 7 |    it under the terms of the GNU General Public License as published by | 
|---|
| 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 |    any later version. | 
|---|
| 10 |  | 
|---|
| 11 |    ### File Specific: | 
|---|
| 12 |    main-programmer: Benjamin Grauer | 
|---|
| 13 |    co-programmer: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GUI | 
|---|
| 17 |  | 
|---|
| 18 | #include "glgui_inputline.h" | 
|---|
| 19 |  | 
|---|
| 20 | namespace OrxGui | 
|---|
| 21 | { | 
|---|
| 22 |   /** | 
|---|
| 23 |    * @brief standard constructor | 
|---|
| 24 |    */ | 
|---|
| 25 |   GLGuiInputLine::GLGuiInputLine () | 
|---|
| 26 |   { | 
|---|
| 27 |     this->init(); | 
|---|
| 28 |   } | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 |   /** | 
|---|
| 32 |    * @brief standard deconstructor | 
|---|
| 33 |    */ | 
|---|
| 34 |   GLGuiInputLine::~GLGuiInputLine() | 
|---|
| 35 |   {} | 
|---|
| 36 |  | 
|---|
| 37 |   float GLGuiInputLine::repeatDelay = 0.3f; | 
|---|
| 38 |   float GLGuiInputLine::repeatRate  = 0.01f; | 
|---|
| 39 |  | 
|---|
| 40 |  | 
|---|
| 41 |   /** | 
|---|
| 42 |    * @brief initializes the GUI-element | 
|---|
| 43 |    */ | 
|---|
| 44 |   void GLGuiInputLine::init() | 
|---|
| 45 |   { | 
|---|
| 46 |     this->setClassID(CL_GLGUI_INPUTLINE, "GLGuiInputLine"); | 
|---|
| 47 |  | 
|---|
| 48 |     this->setFocusable(true); | 
|---|
| 49 |     this->setClickable(true); | 
|---|
| 50 |     this->setSelectable(true); | 
|---|
| 51 |  | 
|---|
| 52 |     this->_clearOnEnter = false; | 
|---|
| 53 |     this->_text.setParent2D(this); | 
|---|
| 54 |     this->_text.setRelCoor2D(4,4); | 
|---|
| 55 |     this->_text.setFont("fonts/final_frontier.ttf", 20); | 
|---|
| 56 |     this->_text.setLineWidth(400); | 
|---|
| 57 |     this->_text.setDotsPosition(LimitedWidthText::Begin); | 
|---|
| 58 |     this->_text.setColor(foregroundColor()); | 
|---|
| 59 |     this->_text.setVisibility(false); | 
|---|
| 60 |     this->resize(); | 
|---|
| 61 |  | 
|---|
| 62 |     this->delayNext = 0.0; | 
|---|
| 63 |     this->pressedKey = SDLK_FIRST; | 
|---|
| 64 |     this->pressedKeyName = SDLK_FIRST; | 
|---|
| 65 |  | 
|---|
| 66 |   } | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 |   /** | 
|---|
| 70 |    * @brief sets the Text of the InputLine | 
|---|
| 71 |    * @param text The new Text. | 
|---|
| 72 |    */ | 
|---|
| 73 |   void GLGuiInputLine::setText(const std::string& text) | 
|---|
| 74 |   { | 
|---|
| 75 |     this->_text.setText(text); | 
|---|
| 76 |     this->changedText(); | 
|---|
| 77 |   } | 
|---|
| 78 |  | 
|---|
| 79 |   /** | 
|---|
| 80 |    * @brief appends text to the InputLine | 
|---|
| 81 |    * @param appendText the Text to append | 
|---|
| 82 |    */ | 
|---|
| 83 |   void GLGuiInputLine::append(const std::string& appendText) | 
|---|
| 84 |   { | 
|---|
| 85 |     this->_text.append(appendText); | 
|---|
| 86 |     this->changedText(); | 
|---|
| 87 |   } | 
|---|
| 88 |  | 
|---|
| 89 |  | 
|---|
| 90 |   /** | 
|---|
| 91 |    * @brief appends a Character to the InputLine | 
|---|
| 92 |    * @param character the Character to append. | 
|---|
| 93 |    */ | 
|---|
| 94 |   void GLGuiInputLine::appendCharacter(char character) | 
|---|
| 95 |   { | 
|---|
| 96 |     this->_text.appendCharacter(character); | 
|---|
| 97 |     this->changedText(); | 
|---|
| 98 |   } | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 |   /** | 
|---|
| 102 |    * @brief Removes Characters from the InputLine | 
|---|
| 103 |    * @param chars The count of characters to remove | 
|---|
| 104 |    */ | 
|---|
| 105 |   void GLGuiInputLine::removeCharacters(unsigned int chars) | 
|---|
| 106 |   { | 
|---|
| 107 |     this->_text.removeCharacters(chars); | 
|---|
| 108 |     this->changedText(); | 
|---|
| 109 |   } | 
|---|
| 110 |  | 
|---|
| 111 |   void GLGuiInputLine::clear() | 
|---|
| 112 |   { | 
|---|
| 113 |     this->_text.clear(); | 
|---|
| 114 |     this->changedText(); | 
|---|
| 115 |   } | 
|---|
| 116 |  | 
|---|
| 117 |   /** | 
|---|
| 118 |    * @brief If the Text has been changed this function is called. | 
|---|
| 119 |    * | 
|---|
| 120 |    * This Function also emits the Signal textChanged. | 
|---|
| 121 |    */ | 
|---|
| 122 |   void GLGuiInputLine::changedText() | 
|---|
| 123 |   { | 
|---|
| 124 |     this->resize(); | 
|---|
| 125 |     this->setFrontColor(Color(1,1,1,1), true); | 
|---|
| 126 |     emit(this->textChanged(this->_text.text())); | 
|---|
| 127 |   } | 
|---|
| 128 |  | 
|---|
| 129 |  | 
|---|
| 130 |   /** | 
|---|
| 131 |    * removes the focus from this Widget. | 
|---|
| 132 |    */ | 
|---|
| 133 |   void GLGuiInputLine::removedFocus() | 
|---|
| 134 |   { | 
|---|
| 135 |     GLGuiWidget::removedFocus(); | 
|---|
| 136 |     this->pressedKey = 0; | 
|---|
| 137 |     this->pressedKeyName = 0; | 
|---|
| 138 |   } | 
|---|
| 139 |  | 
|---|
| 140 |   /** | 
|---|
| 141 |    * @brief handles the EnterPush-Event. | 
|---|
| 142 |    * | 
|---|
| 143 |    * Emmits th EnterPushed Signal with the current Line, | 
|---|
| 144 |    * and if _clearOnEnter is pushed clears the Line. | 
|---|
| 145 |    */ | 
|---|
| 146 |   void GLGuiInputLine::pushEnter() | 
|---|
| 147 |   { | 
|---|
| 148 |     emit(this->enterPushed(this->_text.text())); | 
|---|
| 149 |     if (this->_clearOnEnter) | 
|---|
| 150 |       this->clear(); | 
|---|
| 151 |   } | 
|---|
| 152 |  | 
|---|
| 153 |  | 
|---|
| 154 |   /** | 
|---|
| 155 |    * @brief Processes an Event. | 
|---|
| 156 |    * @param event The event to be processed | 
|---|
| 157 |    * @return true if the event was catched. | 
|---|
| 158 |    */ | 
|---|
| 159 |   bool GLGuiInputLine::processEvent(const Event& event) | 
|---|
| 160 |   { | 
|---|
| 161 |     if (event.bPressed) | 
|---|
| 162 |     { | 
|---|
| 163 |       if (event.type == SDLK_BACKSPACE) | 
|---|
| 164 |       { | 
|---|
| 165 |         this->delayNext = GLGuiInputLine::repeatDelay; | 
|---|
| 166 |         this->pressedKey = SDLK_BACKSPACE; | 
|---|
| 167 |         this->pressedKeyName = SDLK_BACKSPACE; | 
|---|
| 168 |         this->removeCharacters(1); | 
|---|
| 169 |         return true; | 
|---|
| 170 |       } | 
|---|
| 171 |       else if(event.type == SDLK_RETURN || event.type == SDLK_KP_ENTER) | 
|---|
| 172 |       { | 
|---|
| 173 |         this->pushEnter(); | 
|---|
| 174 |         return true; | 
|---|
| 175 |       } | 
|---|
| 176 |       // any other keyboard key | 
|---|
| 177 |       else if (likely(event.type < 127)) | 
|---|
| 178 |       { | 
|---|
| 179 |         this->delayNext = GLGuiInputLine::repeatDelay; | 
|---|
| 180 |  | 
|---|
| 181 |         this->appendCharacter(event.x); | 
|---|
| 182 |         this->pressedKey = event.type; | 
|---|
| 183 |         this->pressedKeyName = event.x; | 
|---|
| 184 |         return true; | 
|---|
| 185 |       } | 
|---|
| 186 |     } | 
|---|
| 187 |     else // if(!event.bPressed) | 
|---|
| 188 |     { | 
|---|
| 189 |       if (this->pressedKey == event.type) | 
|---|
| 190 |       { | 
|---|
| 191 |         this->pressedKeyName = 0; | 
|---|
| 192 |         this->pressedKey = 0; | 
|---|
| 193 |         this->delayNext = 0.0; | 
|---|
| 194 |         return true; | 
|---|
| 195 |       } | 
|---|
| 196 |     } | 
|---|
| 197 |     return false; | 
|---|
| 198 |   } | 
|---|
| 199 |  | 
|---|
| 200 |  | 
|---|
| 201 |   /** | 
|---|
| 202 |    * @brief Resizes the Widget to the new Size-constraints. | 
|---|
| 203 |    */ | 
|---|
| 204 |   void GLGuiInputLine::resize() | 
|---|
| 205 |   { | 
|---|
| 206 |     this->_text.setRelCoor2D(borderLeft(), borderTop()); | 
|---|
| 207 |     this->setSize2D( this->_text.getSize2D() + Vector2D(borderLeft() + borderRight(), borderTop() + borderBottom())); | 
|---|
| 208 |     GLGuiWidget::resize(); | 
|---|
| 209 |     /*    this->frontRect().setTopLeft(borderLeft(), borderTop()); | 
|---|
| 210 |         this->frontRect().setSize(this->getSize2D() - Vector2D(borderLeft() + borderRight(), borderTop() + borderBottom()));*/ | 
|---|
| 211 |   } | 
|---|
| 212 |  | 
|---|
| 213 |   void GLGuiInputLine::updateFrontColor() | 
|---|
| 214 |   { | 
|---|
| 215 |     this->_text.setColor(foregroundColor()); | 
|---|
| 216 |   } | 
|---|
| 217 |  | 
|---|
| 218 |   void GLGuiInputLine::hiding() | 
|---|
| 219 |   { | 
|---|
| 220 |     this->_text.setVisibility(false); | 
|---|
| 221 |   } | 
|---|
| 222 |  | 
|---|
| 223 |   void GLGuiInputLine::showing() | 
|---|
| 224 |   { | 
|---|
| 225 |     this->_text.setVisibility(true); | 
|---|
| 226 |   } | 
|---|
| 227 |  | 
|---|
| 228 |   /** | 
|---|
| 229 |    * @brief ticks the InputLine | 
|---|
| 230 |    * @param dt the time passed. | 
|---|
| 231 |    */ | 
|---|
| 232 |   void GLGuiInputLine::tick(float dt) | 
|---|
| 233 |   { | 
|---|
| 234 |     GLGuiWidget::tick(dt); | 
|---|
| 235 |     if (this->delayNext > 0.0) | 
|---|
| 236 |       this->delayNext -= dt; | 
|---|
| 237 |     else if (this->pressedKey != SDLK_FIRST ) | 
|---|
| 238 |     { | 
|---|
| 239 |       this->delayNext = GLGuiInputLine::repeatRate; | 
|---|
| 240 |       switch (this->pressedKey) | 
|---|
| 241 |       { | 
|---|
| 242 |         case SDLK_BACKSPACE: | 
|---|
| 243 |           this->removeCharacters(1); | 
|---|
| 244 |           break; | 
|---|
| 245 |         default: | 
|---|
| 246 |           { | 
|---|
| 247 |             if (likely(this->pressedKey < 127)) | 
|---|
| 248 |               this->appendCharacter(this->pressedKeyName); | 
|---|
| 249 |           } | 
|---|
| 250 |       } | 
|---|
| 251 |     } | 
|---|
| 252 |  | 
|---|
| 253 |  | 
|---|
| 254 |   } | 
|---|
| 255 |  | 
|---|
| 256 |   /** | 
|---|
| 257 |    * @brief draws the GLGuiInputLine | 
|---|
| 258 |    */ | 
|---|
| 259 |   void GLGuiInputLine::draw() const | 
|---|
| 260 |   { | 
|---|
| 261 |     this->beginDraw(); | 
|---|
| 262 |     GLGuiWidget::draw(); | 
|---|
| 263 |  | 
|---|
| 264 |     //     this->frontMaterial().select(); | 
|---|
| 265 |     //     GLGuiWidget::drawRect(this->frontRect()); | 
|---|
| 266 |  | 
|---|
| 267 |     this->endDraw(); | 
|---|
| 268 |   } | 
|---|
| 269 | } | 
|---|