| 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->resize(); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * @brief sets the Text of the InputLine |
|---|
| 59 | * @param text The new Text. |
|---|
| 60 | */ |
|---|
| 61 | void GLGuiInputLine::setText(const std::string& text) |
|---|
| 62 | { |
|---|
| 63 | this->text.setText(text); |
|---|
| 64 | this->resize(); |
|---|
| 65 | |
|---|
| 66 | emit(this->textChanged(this->getText())); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * @brief appends text to the InputLine |
|---|
| 71 | * @param appendText the Text to append |
|---|
| 72 | */ |
|---|
| 73 | void GLGuiInputLine::append(const std::string& appendText) |
|---|
| 74 | { |
|---|
| 75 | this->text.append(appendText); |
|---|
| 76 | this->resize(); |
|---|
| 77 | emit(this->textChanged(this->text.getText())); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | * @brief appends a Character to the InputLine |
|---|
| 83 | * @param character the Character to append. |
|---|
| 84 | */ |
|---|
| 85 | void GLGuiInputLine::appendCharacter(char character) |
|---|
| 86 | { |
|---|
| 87 | this->text.appendCharacter(character); |
|---|
| 88 | this->resize(); |
|---|
| 89 | emit(this->textChanged(this->text.getText())); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * @brief Removes Characters from the InputLine |
|---|
| 95 | * @param chars The count of characters to remove |
|---|
| 96 | */ |
|---|
| 97 | void GLGuiInputLine::removeCharacters(unsigned int chars) |
|---|
| 98 | { |
|---|
| 99 | this->text.removeCharacters(chars); |
|---|
| 100 | this->resize(); |
|---|
| 101 | emit(this->textChanged(this->text.getText())); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * removes the focus from this Widget. |
|---|
| 107 | */ |
|---|
| 108 | void GLGuiInputLine::removedFocus() |
|---|
| 109 | { |
|---|
| 110 | GLGuiWidget::removedFocus(); |
|---|
| 111 | this->pressedKey = 0; |
|---|
| 112 | this->pressedKeyName = 0; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * Processes an Event. |
|---|
| 118 | * @param event The event to be processed |
|---|
| 119 | * @return true if the event was catched. |
|---|
| 120 | */ |
|---|
| 121 | bool GLGuiInputLine::processEvent(const Event& event) |
|---|
| 122 | { |
|---|
| 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 | { |
|---|
| 136 | this->delayNext = GLGuiInputLine::repeatDelay; |
|---|
| 137 | |
|---|
| 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 | |
|---|
| 155 | return false; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | /** |
|---|
| 160 | * @brief Resizes the Widget to the new Size-constraints. |
|---|
| 161 | */ |
|---|
| 162 | void GLGuiInputLine::resize() |
|---|
| 163 | { |
|---|
| 164 | this->setSize2D( this->text.getSize2D() + Vector2D(8, 8)); |
|---|
| 165 | GLGuiWidget::resize(); |
|---|
| 166 | this->frontRect().setTopLeft(borderSize(), borderSize()); |
|---|
| 167 | this->frontRect().setSize(this->getSize2D() - Vector2D(borderSize(), borderSize())); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * ticks the InputLine |
|---|
| 173 | * @param dt the time passed. |
|---|
| 174 | */ |
|---|
| 175 | void GLGuiInputLine::tick(float dt) |
|---|
| 176 | { |
|---|
| 177 | if (this->delayNext > 0.0) |
|---|
| 178 | this->delayNext -= dt; |
|---|
| 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 | |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | /** |
|---|
| 199 | * @brief draws the GLGuiInputLine |
|---|
| 200 | */ |
|---|
| 201 | void GLGuiInputLine::draw() const |
|---|
| 202 | { |
|---|
| 203 | this->beginDraw(); |
|---|
| 204 | GLGuiWidget::draw(); |
|---|
| 205 | |
|---|
| 206 | this->frontMaterial().select(); |
|---|
| 207 | GLGuiWidget::drawRect(this->frontRect()); |
|---|
| 208 | |
|---|
| 209 | this->endDraw(); |
|---|
| 210 | } |
|---|
| 211 | } |
|---|