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