Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9406 was 9240, checked in by bensch, 18 years ago

orxonox/trunk: better signal handler.
patched by chrigi

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