| 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 | * standard constructor |
|---|
| 24 | */ |
|---|
| 25 | GLGuiInputLine::GLGuiInputLine () |
|---|
| 26 | { |
|---|
| 27 | this->init(); |
|---|
| 28 | |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * standard deconstructor |
|---|
| 34 | */ |
|---|
| 35 | GLGuiInputLine::~GLGuiInputLine() |
|---|
| 36 | {} |
|---|
| 37 | |
|---|
| 38 | float GLGuiInputLine::repeatDelay = 0.3f; |
|---|
| 39 | float GLGuiInputLine::repeatRate = 0.01f; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * initializes the GUI-element |
|---|
| 44 | */ |
|---|
| 45 | void GLGuiInputLine::init() |
|---|
| 46 | { |
|---|
| 47 | this->setClassID(CL_GLGUI_INPUTLINE, "GLGuiInputLine"); |
|---|
| 48 | |
|---|
| 49 | this->setFocusable(true); |
|---|
| 50 | |
|---|
| 51 | this->text.setParent2D(this); |
|---|
| 52 | this->text.setRelCoor2D(4,4); |
|---|
| 53 | this->text.setFont("fonts/final_frontier.ttf", 20); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void GLGuiInputLine::setText(const std::string& text) |
|---|
| 57 | { |
|---|
| 58 | this->text.setText(text); |
|---|
| 59 | this->resize(); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | void GLGuiInputLine::append(const std::string& appendText) |
|---|
| 63 | { |
|---|
| 64 | this->text.append(appendText); |
|---|
| 65 | this->resize(); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | void GLGuiInputLine::appendCharacter(char character) |
|---|
| 69 | { |
|---|
| 70 | this->text.appendCharacter(character); |
|---|
| 71 | this->resize(); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | void GLGuiInputLine::removeCharacters(unsigned int chars) |
|---|
| 76 | { |
|---|
| 77 | this->text.removeCharacters(chars); |
|---|
| 78 | this->resize(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | void GLGuiInputLine::removedFocus() |
|---|
| 83 | { |
|---|
| 84 | GLGuiWidget::removedFocus(); |
|---|
| 85 | this->pressedKey = 0; |
|---|
| 86 | this->pressedKeyName = 0; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | bool GLGuiInputLine::processEvent(const Event& event) |
|---|
| 91 | { |
|---|
| 92 | if (event.bPressed) |
|---|
| 93 | { |
|---|
| 94 | if (event.type == SDLK_BACKSPACE) |
|---|
| 95 | { |
|---|
| 96 | this->delayNext = GLGuiInputLine::repeatDelay; |
|---|
| 97 | this->pressedKey = SDLK_BACKSPACE; |
|---|
| 98 | this->pressedKeyName = SDLK_BACKSPACE; |
|---|
| 99 | this->removeCharacters(1); |
|---|
| 100 | return true; |
|---|
| 101 | } |
|---|
| 102 | // any other keyboard key |
|---|
| 103 | else if (likely(event.type < 127)) |
|---|
| 104 | { |
|---|
| 105 | this->delayNext = GLGuiInputLine::repeatDelay; |
|---|
| 106 | |
|---|
| 107 | this->appendCharacter(event.x); |
|---|
| 108 | this->pressedKey = event.type; |
|---|
| 109 | this->pressedKeyName = event.x; |
|---|
| 110 | return true; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | else // if(!event.bPressed) |
|---|
| 114 | { |
|---|
| 115 | if (this->pressedKey == event.type) |
|---|
| 116 | { |
|---|
| 117 | this->pressedKeyName = 0; |
|---|
| 118 | this->pressedKey = 0; |
|---|
| 119 | this->delayNext = 0.0; |
|---|
| 120 | return true; |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | return false; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | void GLGuiInputLine::resize() |
|---|
| 129 | { |
|---|
| 130 | this->setSize2D( this->text.getSize2D() + Vector2D(8, 8)); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | void GLGuiInputLine::tick(float dt) |
|---|
| 135 | { |
|---|
| 136 | if (this->delayNext > 0.0) |
|---|
| 137 | this->delayNext -= dt; |
|---|
| 138 | else if (this->pressedKey != SDLK_FIRST ) |
|---|
| 139 | { |
|---|
| 140 | this->delayNext = GLGuiInputLine::repeatRate; |
|---|
| 141 | switch (this->pressedKey) |
|---|
| 142 | { |
|---|
| 143 | case SDLK_BACKSPACE: |
|---|
| 144 | this->removeCharacters(1); |
|---|
| 145 | break; |
|---|
| 146 | default: |
|---|
| 147 | { |
|---|
| 148 | if (likely(this->pressedKey < 127)) |
|---|
| 149 | this->appendCharacter(this->pressedKeyName); |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * draws the GLGuiInputLine |
|---|
| 159 | */ |
|---|
| 160 | void GLGuiInputLine::draw() const |
|---|
| 161 | { |
|---|
| 162 | this->startDraw(); |
|---|
| 163 | GLGuiWidget::draw(); |
|---|
| 164 | |
|---|
| 165 | this->frontMaterial().select(); |
|---|
| 166 | glBegin(GL_QUADS); |
|---|
| 167 | |
|---|
| 168 | glTexCoord2i(0,0); glVertex2d(3, 3); |
|---|
| 169 | glTexCoord2i(0,1); glVertex2d(3, this->getSizeY2D() - 3); |
|---|
| 170 | glTexCoord2i(1,1); glVertex2d(this->getSizeX2D() - 3, this->getSizeY2D() -3); |
|---|
| 171 | glTexCoord2i(1,0); glVertex2d(this->getSizeX2D() - 3, 3); |
|---|
| 172 | |
|---|
| 173 | glEnd(); |
|---|
| 174 | this->endDraw(); |
|---|
| 175 | } |
|---|
| 176 | } |
|---|