Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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