Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

gui: slider work

File size: 3.5 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
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    GLGuiWidget::resize();
132    this->frontRect().setTopLeft(1,1);
133    this->frontRect().setSize(this->getSize2D() - Vector2D(2,2));
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   * @brief draws the GLGuiInputLine
162   */
163  void GLGuiInputLine::draw() const
164  {
165    this->beginDraw();
166    GLGuiWidget::draw();
167
168    this->frontMaterial().select();
169    GLGuiWidget::drawRect(this->frontRect());
170
171    this->endDraw();
172  }
173}
Note: See TracBrowser for help on using the repository browser.