Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/gui/gl/glgui_inputline.cc @ 8528

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

valgrind-sweep

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