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