[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 | /** |
---|
[8002] | 23 | * @brief standard constructor |
---|
[7779] | 24 | */ |
---|
[7892] | 25 | GLGuiInputLine::GLGuiInputLine () |
---|
[7779] | 26 | { |
---|
| 27 | this->init(); |
---|
| 28 | } |
---|
[1853] | 29 | |
---|
| 30 | |
---|
[7779] | 31 | /** |
---|
[8002] | 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 | /** |
---|
[8002] | 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); |
---|
| 49 | |
---|
[7892] | 50 | this->text.setParent2D(this); |
---|
[7894] | 51 | this->text.setRelCoor2D(4,4); |
---|
[7892] | 52 | this->text.setFont("fonts/final_frontier.ttf", 20); |
---|
[8012] | 53 | this->resize(); |
---|
[7779] | 54 | } |
---|
[5360] | 55 | |
---|
[8002] | 56 | |
---|
| 57 | /** |
---|
| 58 | * @brief sets the Text of the InputLine |
---|
| 59 | * @param text The new Text. |
---|
| 60 | */ |
---|
[7892] | 61 | void GLGuiInputLine::setText(const std::string& text) |
---|
| 62 | { |
---|
| 63 | this->text.setText(text); |
---|
[7894] | 64 | this->resize(); |
---|
[7998] | 65 | |
---|
| 66 | emit(this->textChanged(this->getText())); |
---|
[7892] | 67 | } |
---|
| 68 | |
---|
[8002] | 69 | /** |
---|
| 70 | * @brief appends text to the InputLine |
---|
| 71 | * @param appendText the Text to append |
---|
| 72 | */ |
---|
[7892] | 73 | void GLGuiInputLine::append(const std::string& appendText) |
---|
| 74 | { |
---|
| 75 | this->text.append(appendText); |
---|
[7894] | 76 | this->resize(); |
---|
[7998] | 77 | emit(this->textChanged(this->text.getText())); |
---|
[7893] | 78 | } |
---|
[7892] | 79 | |
---|
[8002] | 80 | |
---|
| 81 | /** |
---|
| 82 | * @brief appends a Character to the InputLine |
---|
| 83 | * @param character the Character to append. |
---|
| 84 | */ |
---|
[7893] | 85 | void GLGuiInputLine::appendCharacter(char character) |
---|
| 86 | { |
---|
| 87 | this->text.appendCharacter(character); |
---|
[7894] | 88 | this->resize(); |
---|
[7998] | 89 | emit(this->textChanged(this->text.getText())); |
---|
[7892] | 90 | } |
---|
| 91 | |
---|
[7893] | 92 | |
---|
[8002] | 93 | /** |
---|
| 94 | * @brief Removes Characters from the InputLine |
---|
| 95 | * @param chars The count of characters to remove |
---|
| 96 | */ |
---|
[7892] | 97 | void GLGuiInputLine::removeCharacters(unsigned int chars) |
---|
| 98 | { |
---|
| 99 | this->text.removeCharacters(chars); |
---|
[7894] | 100 | this->resize(); |
---|
[7998] | 101 | emit(this->textChanged(this->text.getText())); |
---|
[7892] | 102 | } |
---|
| 103 | |
---|
[7896] | 104 | |
---|
[8002] | 105 | /** |
---|
| 106 | * removes the focus from this Widget. |
---|
| 107 | */ |
---|
[7896] | 108 | void GLGuiInputLine::removedFocus() |
---|
| 109 | { |
---|
| 110 | GLGuiWidget::removedFocus(); |
---|
| 111 | this->pressedKey = 0; |
---|
| 112 | this->pressedKeyName = 0; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
[8002] | 116 | /** |
---|
| 117 | * Processes an Event. |
---|
| 118 | * @param event The event to be processed |
---|
| 119 | * @return true if the event was catched. |
---|
| 120 | */ |
---|
[7893] | 121 | bool GLGuiInputLine::processEvent(const Event& event) |
---|
| 122 | { |
---|
[7894] | 123 | if (event.bPressed) |
---|
| 124 | { |
---|
| 125 | if (event.type == SDLK_BACKSPACE) |
---|
| 126 | { |
---|
| 127 | this->delayNext = GLGuiInputLine::repeatDelay; |
---|
| 128 | this->pressedKey = SDLK_BACKSPACE; |
---|
| 129 | this->pressedKeyName = SDLK_BACKSPACE; |
---|
| 130 | this->removeCharacters(1); |
---|
| 131 | return true; |
---|
| 132 | } |
---|
| 133 | // any other keyboard key |
---|
| 134 | else if (likely(event.type < 127)) |
---|
| 135 | { |
---|
[7895] | 136 | this->delayNext = GLGuiInputLine::repeatDelay; |
---|
| 137 | |
---|
[7894] | 138 | this->appendCharacter(event.x); |
---|
| 139 | this->pressedKey = event.type; |
---|
| 140 | this->pressedKeyName = event.x; |
---|
| 141 | return true; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | else // if(!event.bPressed) |
---|
| 145 | { |
---|
| 146 | if (this->pressedKey == event.type) |
---|
| 147 | { |
---|
| 148 | this->pressedKeyName = 0; |
---|
| 149 | this->pressedKey = 0; |
---|
| 150 | this->delayNext = 0.0; |
---|
| 151 | return true; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
[7893] | 155 | return false; |
---|
| 156 | } |
---|
[7892] | 157 | |
---|
[7893] | 158 | |
---|
[8002] | 159 | /** |
---|
| 160 | * @brief Resizes the Widget to the new Size-constraints. |
---|
| 161 | */ |
---|
[7894] | 162 | void GLGuiInputLine::resize() |
---|
| 163 | { |
---|
| 164 | this->setSize2D( this->text.getSize2D() + Vector2D(8, 8)); |
---|
[7926] | 165 | GLGuiWidget::resize(); |
---|
[7936] | 166 | this->frontRect().setTopLeft(borderSize(), borderSize()); |
---|
| 167 | this->frontRect().setSize(this->getSize2D() - Vector2D(borderSize(), borderSize())); |
---|
[7894] | 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
[8002] | 171 | /** |
---|
| 172 | * ticks the InputLine |
---|
| 173 | * @param dt the time passed. |
---|
| 174 | */ |
---|
[7894] | 175 | void GLGuiInputLine::tick(float dt) |
---|
| 176 | { |
---|
| 177 | if (this->delayNext > 0.0) |
---|
| 178 | this->delayNext -= dt; |
---|
[7895] | 179 | else if (this->pressedKey != SDLK_FIRST ) |
---|
| 180 | { |
---|
| 181 | this->delayNext = GLGuiInputLine::repeatRate; |
---|
| 182 | switch (this->pressedKey) |
---|
| 183 | { |
---|
| 184 | case SDLK_BACKSPACE: |
---|
| 185 | this->removeCharacters(1); |
---|
| 186 | break; |
---|
| 187 | default: |
---|
| 188 | { |
---|
| 189 | if (likely(this->pressedKey < 127)) |
---|
| 190 | this->appendCharacter(this->pressedKeyName); |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | |
---|
[7894] | 196 | } |
---|
| 197 | |
---|
[7779] | 198 | /** |
---|
[7926] | 199 | * @brief draws the GLGuiInputLine |
---|
[7779] | 200 | */ |
---|
[7892] | 201 | void GLGuiInputLine::draw() const |
---|
[7779] | 202 | { |
---|
[7928] | 203 | this->beginDraw(); |
---|
[7892] | 204 | GLGuiWidget::draw(); |
---|
| 205 | |
---|
| 206 | this->frontMaterial().select(); |
---|
[7926] | 207 | GLGuiWidget::drawRect(this->frontRect()); |
---|
[7892] | 208 | |
---|
| 209 | this->endDraw(); |
---|
[7779] | 210 | } |
---|
[5360] | 211 | } |
---|