Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

select works (more or less)

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