Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/gui/gl/glgui_handler.cc @ 8671

Last change on this file since 8671 was 8671, checked in by bensch, 19 years ago

selecting with keyboard

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