Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/gui/gl/glgui_inputline.cc @ 8494

Last change on this file since 8494 was 8448, checked in by bensch, 19 years ago

merged gui back to the trunk
merged with command
merge -r8377:HEAD https://svn.orxonox.net/orxonox/branches/gui .

File size: 5.2 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   * @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->_text.setColor(this->frontColor());
54    this->_text.setVisibility(false);
55    this->resize();
56
57  }
58
59
60  /**
61   * @brief sets the Text of the InputLine
62   * @param text The new Text.
63   */
64  void GLGuiInputLine::setText(const std::string& text)
65  {
66    this->_text.setText(text);
67    this->changedText();
68  }
69
70  /**
71   * @brief appends text to the InputLine
72   * @param appendText the Text to append
73   */
74  void GLGuiInputLine::append(const std::string& appendText)
75  {
76    this->_text.append(appendText);
77    this->changedText();
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->changedText();
89  }
90
91
92  /**
93   * @brief Removes Characters from the InputLine
94   * @param chars The count of characters to remove
95   */
96  void GLGuiInputLine::removeCharacters(unsigned int chars)
97  {
98    this->_text.removeCharacters(chars);
99    this->changedText();
100  }
101
102  /**
103   * @brief If the Text has been changed this function is called.
104   *
105   * This Function also emits the Signal textChanged.
106   */
107  void GLGuiInputLine::changedText()
108  {
109    this->resize();
110    this->setFrontColor(Color(1,1,1,1), true);
111    this->setFrontColor(Color(0,1,0,1));
112    emit(this->textChanged(this->_text.getText()));
113  }
114
115
116  /**
117   * removes the focus from this Widget.
118   */
119  void GLGuiInputLine::removedFocus()
120  {
121    GLGuiWidget::removedFocus();
122    this->pressedKey = 0;
123    this->pressedKeyName = 0;
124  }
125
126
127  /**
128   * @brief Processes an Event.
129   * @param event The event to be processed
130   * @return true if the event was catched.
131   */
132  bool GLGuiInputLine::processEvent(const Event& event)
133  {
134    if (event.bPressed)
135    {
136      if (event.type == SDLK_BACKSPACE)
137      {
138        this->delayNext = GLGuiInputLine::repeatDelay;
139        this->pressedKey = SDLK_BACKSPACE;
140        this->pressedKeyName = SDLK_BACKSPACE;
141        this->removeCharacters(1);
142        return true;
143      }
144      // any other keyboard key
145      else if (likely(event.type < 127))
146      {
147        this->delayNext = GLGuiInputLine::repeatDelay;
148
149        this->appendCharacter(event.x);
150        this->pressedKey = event.type;
151        this->pressedKeyName = event.x;
152        return true;
153      }
154    }
155    else // if(!event.bPressed)
156    {
157      if (this->pressedKey == event.type)
158      {
159        this->pressedKeyName = 0;
160        this->pressedKey = 0;
161        this->delayNext = 0.0;
162        return true;
163      }
164    }
165    return false;
166  }
167
168
169  /**
170   * @brief Resizes the Widget to the new Size-constraints.
171   */
172  void GLGuiInputLine::resize()
173  {
174    this->_text.setRelCoor2D(this->borderLeft(), this->borderTop());
175    this->setSize2D( this->_text.getSize2D() + Vector2D(borderLeft() + borderRight(), borderTop() + borderBottom()));
176    GLGuiWidget::resize();
177/*    this->frontRect().setTopLeft(borderLeft(), borderTop());
178    this->frontRect().setSize(this->getSize2D() - Vector2D(borderLeft() + borderRight(), borderTop() + borderBottom()));*/
179  }
180
181  void GLGuiInputLine::updateFrontColor()
182  {
183    this->_text.setColor(this->frontColor());
184  }
185
186  void GLGuiInputLine::hiding()
187  {
188    this->_text.setVisibility(false);
189  }
190
191  void GLGuiInputLine::showing()
192  {
193    this->_text.setVisibility(true);
194  }
195
196  /**
197   * @brief ticks the InputLine
198   * @param dt the time passed.
199   */
200  void GLGuiInputLine::tick(float dt)
201  {
202    GLGuiWidget::tick(dt);
203    if (this->delayNext > 0.0)
204      this->delayNext -= dt;
205        else if (this->pressedKey != SDLK_FIRST )
206    {
207      this->delayNext = GLGuiInputLine::repeatRate;
208      switch (this->pressedKey)
209      {
210        case SDLK_BACKSPACE:
211          this->removeCharacters(1);
212          break;
213        default:
214        {
215          if (likely(this->pressedKey < 127))
216            this->appendCharacter(this->pressedKeyName);
217        }
218      }
219    }
220
221
222  }
223
224  /**
225   * @brief draws the GLGuiInputLine
226   */
227  void GLGuiInputLine::draw() const
228  {
229    this->beginDraw();
230    GLGuiWidget::draw();
231
232//     this->frontMaterial().select();
233//     GLGuiWidget::drawRect(this->frontRect());
234
235    this->endDraw();
236  }
237}
Note: See TracBrowser for help on using the repository browser.