Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: merged the gui-branche back.
merged with command:
svn merge -r8520:HEAD https://svn.orxonox.net/orxonox/branches/gui
no conflicts

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