Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/gui/gl_gui/glgui_inputline.cc @ 7896

Last change on this file since 7896 was 7896, checked in by bensch, 18 years ago

better dispachers for events

File size: 3.6 KB
Line 
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
20namespace 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    this->setClickable(true);
51
52    this->text.setParent2D(this);
53    this->text.setRelCoor2D(4,4);
54    this->text.setFont("fonts/final_frontier.ttf", 20);
55
56    this->setText("SUPERTEST");
57  }
58
59  void GLGuiInputLine::setText(const std::string& text)
60  {
61    this->text.setText(text);
62    this->resize();
63  }
64
65  void GLGuiInputLine::append(const std::string& appendText)
66  {
67    this->text.append(appendText);
68    this->resize();
69  }
70
71  void GLGuiInputLine::appendCharacter(char character)
72  {
73    this->text.appendCharacter(character);
74    this->resize();
75  }
76
77
78  void GLGuiInputLine::removeCharacters(unsigned int chars)
79  {
80    this->text.removeCharacters(chars);
81    this->resize();
82  }
83
84
85  void GLGuiInputLine::removedFocus()
86  {
87    GLGuiWidget::removedFocus();
88    this->pressedKey = 0;
89    this->pressedKeyName = 0;
90  }
91
92
93  bool GLGuiInputLine::processEvent(const Event& event)
94  {
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      {
108        this->delayNext = GLGuiInputLine::repeatDelay;
109
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
127    return false;
128  }
129
130
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;
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
158  }
159
160  /**
161   * draws the GLGuiInputLine
162   */
163  void GLGuiInputLine::draw() const
164  {
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();
178  }
179}
Note: See TracBrowser for help on using the repository browser.