[4744] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[5360] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[5360] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GUI |
---|
[1853] | 17 | |
---|
[7892] | 18 | #include "glgui_inputline.h" |
---|
[1853] | 19 | |
---|
[7779] | 20 | namespace OrxGui |
---|
[3365] | 21 | { |
---|
[7779] | 22 | /** |
---|
[8035] | 23 | * @brief standard constructor |
---|
[8448] | 24 | */ |
---|
[7892] | 25 | GLGuiInputLine::GLGuiInputLine () |
---|
[7779] | 26 | { |
---|
| 27 | this->init(); |
---|
| 28 | } |
---|
[1853] | 29 | |
---|
| 30 | |
---|
[7779] | 31 | /** |
---|
[8035] | 32 | * @brief standard deconstructor |
---|
| 33 | */ |
---|
[7892] | 34 | GLGuiInputLine::~GLGuiInputLine() |
---|
[7894] | 35 | {} |
---|
[5360] | 36 | |
---|
[7895] | 37 | float GLGuiInputLine::repeatDelay = 0.3f; |
---|
| 38 | float GLGuiInputLine::repeatRate = 0.01f; |
---|
[7894] | 39 | |
---|
| 40 | |
---|
[7779] | 41 | /** |
---|
[8035] | 42 | * @brief initializes the GUI-element |
---|
[7779] | 43 | */ |
---|
[7892] | 44 | void GLGuiInputLine::init() |
---|
[7779] | 45 | { |
---|
[7892] | 46 | this->setClassID(CL_GLGUI_INPUTLINE, "GLGuiInputLine"); |
---|
[7893] | 47 | |
---|
| 48 | this->setFocusable(true); |
---|
[8717] | 49 | this->setClickable(true); |
---|
| 50 | this->setSelectable(true); |
---|
[7893] | 51 | |
---|
[8518] | 52 | this->_clearOnEnter = false; |
---|
[8448] | 53 | this->_text.setParent2D(this); |
---|
| 54 | this->_text.setRelCoor2D(4,4); |
---|
| 55 | this->_text.setFont("fonts/final_frontier.ttf", 20); |
---|
[8619] | 56 | this->_text.setLineWidth(400); |
---|
| 57 | this->_text.setDotsPosition(LimitedWidthText::Begin); |
---|
| 58 | this->_text.setColor(foregroundColor()); |
---|
[8448] | 59 | this->_text.setVisibility(false); |
---|
[8035] | 60 | this->resize(); |
---|
[8116] | 61 | |
---|
[8619] | 62 | this->delayNext = 0.0; |
---|
| 63 | this->pressedKey = SDLK_FIRST; |
---|
| 64 | this->pressedKeyName = SDLK_FIRST; |
---|
| 65 | |
---|
[7779] | 66 | } |
---|
[5360] | 67 | |
---|
[8035] | 68 | |
---|
| 69 | /** |
---|
| 70 | * @brief sets the Text of the InputLine |
---|
| 71 | * @param text The new Text. |
---|
| 72 | */ |
---|
[7892] | 73 | void GLGuiInputLine::setText(const std::string& text) |
---|
| 74 | { |
---|
[8448] | 75 | this->_text.setText(text); |
---|
| 76 | this->changedText(); |
---|
[7892] | 77 | } |
---|
| 78 | |
---|
[8035] | 79 | /** |
---|
| 80 | * @brief appends text to the InputLine |
---|
| 81 | * @param appendText the Text to append |
---|
| 82 | */ |
---|
[7892] | 83 | void GLGuiInputLine::append(const std::string& appendText) |
---|
| 84 | { |
---|
[8448] | 85 | this->_text.append(appendText); |
---|
| 86 | this->changedText(); |
---|
[7893] | 87 | } |
---|
[7892] | 88 | |
---|
[8035] | 89 | |
---|
| 90 | /** |
---|
| 91 | * @brief appends a Character to the InputLine |
---|
| 92 | * @param character the Character to append. |
---|
| 93 | */ |
---|
[7893] | 94 | void GLGuiInputLine::appendCharacter(char character) |
---|
| 95 | { |
---|
[8448] | 96 | this->_text.appendCharacter(character); |
---|
| 97 | this->changedText(); |
---|
[7892] | 98 | } |
---|
| 99 | |
---|
[7893] | 100 | |
---|
[8035] | 101 | /** |
---|
| 102 | * @brief Removes Characters from the InputLine |
---|
| 103 | * @param chars The count of characters to remove |
---|
| 104 | */ |
---|
[7892] | 105 | void GLGuiInputLine::removeCharacters(unsigned int chars) |
---|
| 106 | { |
---|
[8448] | 107 | this->_text.removeCharacters(chars); |
---|
| 108 | this->changedText(); |
---|
| 109 | } |
---|
| 110 | |
---|
[8518] | 111 | void GLGuiInputLine::clear() |
---|
| 112 | { |
---|
| 113 | this->_text.clear(); |
---|
| 114 | this->changedText(); |
---|
| 115 | } |
---|
| 116 | |
---|
[8448] | 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 | { |
---|
[7894] | 124 | this->resize(); |
---|
[8448] | 125 | this->setFrontColor(Color(1,1,1,1), true); |
---|
[9406] | 126 | this->textChanged.emit(this->_text.text()); |
---|
[7892] | 127 | } |
---|
| 128 | |
---|
[7896] | 129 | |
---|
[8035] | 130 | /** |
---|
| 131 | * removes the focus from this Widget. |
---|
| 132 | */ |
---|
[7896] | 133 | void GLGuiInputLine::removedFocus() |
---|
| 134 | { |
---|
| 135 | GLGuiWidget::removedFocus(); |
---|
| 136 | this->pressedKey = 0; |
---|
| 137 | this->pressedKeyName = 0; |
---|
| 138 | } |
---|
| 139 | |
---|
[8518] | 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 | { |
---|
[9406] | 148 | this->enterPushed.emit(this->_text.text()); |
---|
[8518] | 149 | if (this->_clearOnEnter) |
---|
| 150 | this->clear(); |
---|
| 151 | } |
---|
[7896] | 152 | |
---|
[8518] | 153 | |
---|
[8035] | 154 | /** |
---|
[8448] | 155 | * @brief Processes an Event. |
---|
[8035] | 156 | * @param event The event to be processed |
---|
| 157 | * @return true if the event was catched. |
---|
| 158 | */ |
---|
[7893] | 159 | bool GLGuiInputLine::processEvent(const Event& event) |
---|
| 160 | { |
---|
[7894] | 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 | } |
---|
[8518] | 171 | else if(event.type == SDLK_RETURN || event.type == SDLK_KP_ENTER) |
---|
| 172 | { |
---|
| 173 | this->pushEnter(); |
---|
| 174 | return true; |
---|
| 175 | } |
---|
[7894] | 176 | // any other keyboard key |
---|
| 177 | else if (likely(event.type < 127)) |
---|
| 178 | { |
---|
[7895] | 179 | this->delayNext = GLGuiInputLine::repeatDelay; |
---|
| 180 | |
---|
[7894] | 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 | } |
---|
[7893] | 197 | return false; |
---|
| 198 | } |
---|
[7892] | 199 | |
---|
[7893] | 200 | |
---|
[8035] | 201 | /** |
---|
| 202 | * @brief Resizes the Widget to the new Size-constraints. |
---|
| 203 | */ |
---|
[7894] | 204 | void GLGuiInputLine::resize() |
---|
| 205 | { |
---|
[8619] | 206 | this->_text.setRelCoor2D(borderLeft(), borderTop()); |
---|
[8448] | 207 | this->setSize2D( this->_text.getSize2D() + Vector2D(borderLeft() + borderRight(), borderTop() + borderBottom())); |
---|
[8035] | 208 | GLGuiWidget::resize(); |
---|
[8619] | 209 | /* this->frontRect().setTopLeft(borderLeft(), borderTop()); |
---|
| 210 | this->frontRect().setSize(this->getSize2D() - Vector2D(borderLeft() + borderRight(), borderTop() + borderBottom()));*/ |
---|
[7894] | 211 | } |
---|
| 212 | |
---|
[8448] | 213 | void GLGuiInputLine::updateFrontColor() |
---|
| 214 | { |
---|
[8619] | 215 | this->_text.setColor(foregroundColor()); |
---|
[8448] | 216 | } |
---|
[7894] | 217 | |
---|
[8116] | 218 | void GLGuiInputLine::hiding() |
---|
| 219 | { |
---|
[8448] | 220 | this->_text.setVisibility(false); |
---|
[8116] | 221 | } |
---|
| 222 | |
---|
| 223 | void GLGuiInputLine::showing() |
---|
| 224 | { |
---|
[8448] | 225 | this->_text.setVisibility(true); |
---|
[8116] | 226 | } |
---|
| 227 | |
---|
[8035] | 228 | /** |
---|
[8448] | 229 | * @brief ticks the InputLine |
---|
[8035] | 230 | * @param dt the time passed. |
---|
| 231 | */ |
---|
[7894] | 232 | void GLGuiInputLine::tick(float dt) |
---|
| 233 | { |
---|
[8448] | 234 | GLGuiWidget::tick(dt); |
---|
[7894] | 235 | if (this->delayNext > 0.0) |
---|
| 236 | this->delayNext -= dt; |
---|
[8619] | 237 | else if (this->pressedKey != SDLK_FIRST ) |
---|
[7895] | 238 | { |
---|
| 239 | this->delayNext = GLGuiInputLine::repeatRate; |
---|
| 240 | switch (this->pressedKey) |
---|
| 241 | { |
---|
| 242 | case SDLK_BACKSPACE: |
---|
| 243 | this->removeCharacters(1); |
---|
| 244 | break; |
---|
| 245 | default: |
---|
[8619] | 246 | { |
---|
| 247 | if (likely(this->pressedKey < 127)) |
---|
| 248 | this->appendCharacter(this->pressedKeyName); |
---|
| 249 | } |
---|
[7895] | 250 | } |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | |
---|
[7894] | 254 | } |
---|
| 255 | |
---|
[7779] | 256 | /** |
---|
[8035] | 257 | * @brief draws the GLGuiInputLine |
---|
[7779] | 258 | */ |
---|
[7892] | 259 | void GLGuiInputLine::draw() const |
---|
[7779] | 260 | { |
---|
[8035] | 261 | this->beginDraw(); |
---|
[7892] | 262 | GLGuiWidget::draw(); |
---|
| 263 | |
---|
[8619] | 264 | // this->frontMaterial().select(); |
---|
| 265 | // GLGuiWidget::drawRect(this->frontRect()); |
---|
[7892] | 266 | |
---|
| 267 | this->endDraw(); |
---|
[7779] | 268 | } |
---|
[5360] | 269 | } |
---|