Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/gui/gl/glgui_handler.cc @ 8932

Last change on this file since 8932 was 8743, checked in by bensch, 18 years ago

gui: event-handler now supports retrieving key-states

File size: 7.5 KB
RevLine 
[4744]1/*
[3655]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:
[5366]12   main-programmer: Benjamin Grauer
[3655]13   co-programmer: ...
14*/
15
[5401]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GUI
[3655]17
[5366]18#include "glgui_handler.h"
[5388]19#include "event_handler.h"
[3655]20
[5406]21#include "glgui_mainwidget.h"
[7919]22#include "glgui_cursor.h"
[5406]23
[7919]24#include "class_list.h"
[8711]25#include <cassert>
[7919]26
[8717]27#include "debug.h"
28
[8450]29#include <cassert>
[7919]30
[8450]31
[7919]32/// TAKE THIS OUT OF HERE.
33#include "graphics_engine.h"
34
[7779]35namespace OrxGui
[3655]36{
37
[7779]38  /**
39   * standard constructor
40   */
41  GLGuiHandler::GLGuiHandler ()
42  {
43    this->setClassID(CL_GLGUI_HANDLER, "GLGuiHandler");
44    this->setName("GLGuiHandler");
[3655]45
[7868]46
[8448]47    EventHandler::getInstance()->withUNICODE(ES_MENU, true );
[7919]48
[8324]49    this->_cursor = NULL;
[7919]50    for (unsigned int i = 0; i < EV_NUMBER; i++)
51    {
[8312]52      this->subscribeEvent(ES_ALL, i);
[7919]53    }
[7779]54  }
[3655]55
[7779]56  /**
57   *  the singleton reference to this class
58   */
59  GLGuiHandler* GLGuiHandler::singletonRef = NULL;
[5388]60
[7779]61  /**
62     @brief standard deconstructor
63   */
64  GLGuiHandler::~GLGuiHandler ()
65  {
66    GLGuiHandler::singletonRef = NULL;
67  }
[5388]68
[7919]69  void GLGuiHandler::activateCursor()
70  {
[8324]71    if (this->_cursor == NULL)
72      this->_cursor = new GLGuiCursor();
73    this->_cursor->show();
74    this->_cursor->setMaxBorders(Vector2D(GraphicsEngine::getInstance()->getResolutionX(), GraphicsEngine::getInstance()->getResolutionY()));
[7919]75  }
76
77  void GLGuiHandler::deactivateCursor(bool deleteCursor)
78  {
[8324]79    if (this->_cursor)
[7919]80    {
81      if (deleteCursor)
[8324]82        delete this->_cursor;
83      this->_cursor = NULL;
[7919]84    }
85  }
86
87
[7779]88  void GLGuiHandler::activate()
89  {
90    EventHandler::getInstance()->pushState(ES_MENU);
[5388]91
[7919]92
93
[7779]94  }
[5388]95
[7779]96  void GLGuiHandler::deactivate()
97  {
98    EventHandler::getInstance()->popState();
[5388]99
[7919]100
[7779]101  }
[5388]102
[8717]103  void GLGuiHandler::selectNext()
104  {
105    // retrieve Objects.
106    const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET);
[5388]107
[8717]108    if (objects)
109    {
110      std::list<BaseObject*>::const_iterator it ;
111      std::list<BaseObject*>::const_iterator currentIt = objects->end();
112
113      if (GLGuiWidget::selected() != NULL)
114      {
115        it = std::find(objects->begin(), objects->end(), GLGuiWidget::selected());
116        if (it != objects->end())
117        {
118          currentIt = it;
119          it++;
120        }
121      }
122      else
123      {
124        it = objects->begin();
125      }
126
127      bool cycledOnce = false;
128
129      for (; it != currentIt; ++it)
130      {
131        if (it == objects->end() && !cycledOnce)
132        {
133          it = objects->begin();
134          cycledOnce = true;
135        }
136
[8742]137        if (dynamic_cast<GLGuiWidget*>(*it)->selectable() && dynamic_cast<GLGuiWidget*>(*it)->isVisible())
[8717]138        {
139          dynamic_cast<GLGuiWidget*>(*it)->select();
140          return;
141        }
142      }
143
144    }
145    else
146    {
147      PRINTF(0)("NO GUI-ELEMENTS EXISTING\n");
148    }
149  }
150
151  void GLGuiHandler::selectPrevious()
152  {
153    // retrieve Objects.
154    const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET);
155
156    if (objects)
157    {
158      std::list<BaseObject*>::const_iterator it ;
159      std::list<BaseObject*>::const_iterator currentIt = objects->begin();
160
161      if (GLGuiWidget::selected() != NULL)
162      {
163        it = std::find(objects->begin(), objects->end(), GLGuiWidget::selected());
164        if (it != objects->end())
165        {
166          currentIt = it;
167          it--;
168        }
169      }
170      else
171      {
172        it = objects->end();
173      }
174
175      bool cycledOnce = false;
176
177      for (; it != currentIt; --it)
178      {
179        if (it == objects->end() && !cycledOnce)
180        {
181          --it ;
182          cycledOnce = true;
183        }
184
[8742]185        if (dynamic_cast<GLGuiWidget*>(*it)->selectable() && dynamic_cast<GLGuiWidget*>(*it)->isVisible())
[8717]186        {
187          dynamic_cast<GLGuiWidget*>(*it)->select();
188          return;
189        }
190      }
191
192    }
193    else
194    {
195      PRINTF(0)("NO GUI-ELEMENTS EXISTING\n");
196    }
197  }
198
199
200
[7779]201  void GLGuiHandler::process(const Event &event)
202  {
[7919]203    switch (event.type)
204    {
205      case  EV_MOUSE_BUTTON_LEFT:
[8717]206        if (GLGuiWidget::mouseFocused() != NULL && event.bPressed)
[7919]207        {
[8717]208          // if clickable select the Widget.
209          if (GLGuiWidget::mouseFocused()->clickable())
[7919]210          {
[8717]211            Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
212            GLGuiWidget::mouseFocused()->select();
213            GLGuiWidget::mouseFocused()->click(cursorPos - GLGuiWidget::mouseFocused()->getAbsCoor2D());
[7919]214          }
[8717]215        }
216        else if (GLGuiWidget::selected() != NULL && !event.bPressed)
217        {
218          if (GLGuiWidget::selected()->clickable())
[7919]219          {
[8717]220            Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
221            GLGuiWidget::selected()->release(cursorPos - GLGuiWidget::selected()->getAbsCoor2D());
[7919]222          }
223        }
[8717]224
[7919]225        break;
226      case EV_LEAVE_STATE:
[8717]227        if (GLGuiWidget::selected() != NULL)
228          GLGuiWidget::selected()->unselect();
229
230        if (GLGuiWidget::mouseFocused() != NULL)
231          GLGuiWidget::mouseFocused()->breakMouseFocus();
[7919]232        break;
[5388]233
[7919]234      case EV_VIDEO_RESIZE:
[8324]235        if (this->_cursor != NULL)
236          this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h));
[7919]237        break;
[8717]238      case SDLK_TAB:
239        if (event.bPressed)
[8743]240        {
241          if (EventHandler::getInstance()->isPressed(SDLK_LSHIFT) || EventHandler::getInstance()->isPressed(SDLK_RSHIFT))
242            this->selectPrevious();
243          else
244            this->selectNext();
245        }
[8717]246        break;
[7919]247    }
[5388]248
[8717]249    if (GLGuiWidget::selected() != NULL)
[7919]250    {
[8717]251      GLGuiWidget::selected()->processEvent(event);
[7919]252    }
253
254
255
[7779]256  }
[5406]257
[8035]258
259  Vector2D GLGuiHandler::cursorPositionOverFocusedWidget() const
260  {
[8324]261    return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(0,0);
[8035]262  }
263
264  const Vector2D& GLGuiHandler::cursorPositionAbs() const
265  {
[8324]266    if (this->_cursor)
267      return this->_cursor->getAbsCoor2D();
[8035]268    else
269      return Vector2D::nullVector();
270  }
[8717]271
[8035]272  Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const
273  {
274    assert (widget != NULL);
[8324]275    if (this->_cursor)
276      return  this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D();
[8035]277    else
278      return Vector2D::nullVector();
279  }
280
281
[8717]282  void GLGuiHandler::checkFocus()
[7779]283  {
[8743]284    // CHECK THE COLLISIONS.
[7919]285    const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET);
286
[8324]287    if (objects != NULL && this->_cursor != NULL)
[7919]288    {
289      for (std::list<BaseObject*>::const_iterator it = objects->begin(); it != objects->end(); it++)
290      {
291        GLGuiWidget* widget = dynamic_cast<GLGuiWidget*>(*it);
292
293        if (widget->isVisible() &&
294            widget->focusable() &&
[8324]295            widget->focusOverWidget(this->_cursor))
[7919]296        {
297          // receiving Focus
[8717]298          if (GLGuiWidget::mouseFocused() != widget)
[7919]299          {
[8717]300            widget->giveMouseFocus();
[7919]301          }
302          return ;
303        }
304      }
[8717]305      if (GLGuiWidget::mouseFocused() != NULL)
306        GLGuiWidget::mouseFocused()->breakMouseFocus();
[7919]307    }
[7779]308  }
[8717]309
310  void GLGuiHandler::draw()
311  {
312    //    GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP);
313  }
314
315
316  void GLGuiHandler::tick(float dt)
317  {
318    // do not change if we already clicked into a Widget.
319    // if (GLGuiWidget::selected() != NULL && GLGuiWidget::selected()->pushed())
320    //      return ;
321
322    this->checkFocus();
323  }
[5406]324}
Note: See TracBrowser for help on using the repository browser.