Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the gui back to the trunk

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