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