Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

selectables

File size: 6.2 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 "debug.h"
27
28#include <cassert>
29
30
31/// TAKE THIS OUT OF HERE.
32#include "graphics_engine.h"
33
34namespace OrxGui
35{
36
37  /**
38   * standard constructor
39   */
40  GLGuiHandler::GLGuiHandler ()
41  {
42    this->setClassID(CL_GLGUI_HANDLER, "GLGuiHandler");
43    this->setName("GLGuiHandler");
44
45
46    EventHandler::getInstance()->withUNICODE(ES_MENU, true );
47
48    this->_cursor = NULL;
49    for (unsigned int i = 0; i < EV_NUMBER; i++)
50    {
51      this->subscribeEvent(ES_ALL, i);
52    }
53  }
54
55  /**
56   *  the singleton reference to this class
57   */
58  GLGuiHandler* GLGuiHandler::singletonRef = NULL;
59
60  /**
61     @brief standard deconstructor
62   */
63  GLGuiHandler::~GLGuiHandler ()
64  {
65    GLGuiHandler::singletonRef = NULL;
66  }
67
68  void GLGuiHandler::activateCursor()
69  {
70    if (this->_cursor == NULL)
71      this->_cursor = new GLGuiCursor();
72    this->_cursor->show();
73    this->_cursor->setMaxBorders(Vector2D(GraphicsEngine::getInstance()->getResolutionX(), GraphicsEngine::getInstance()->getResolutionY()));
74  }
75
76  void GLGuiHandler::deactivateCursor(bool deleteCursor)
77  {
78    if (this->_cursor)
79    {
80      if (deleteCursor)
81        delete this->_cursor;
82      this->_cursor = NULL;
83    }
84  }
85
86
87  void GLGuiHandler::activate()
88  {
89    EventHandler::getInstance()->pushState(ES_MENU);
90
91
92
93  }
94
95  void GLGuiHandler::deactivate()
96  {
97    EventHandler::getInstance()->popState();
98
99
100  }
101
102  void GLGuiHandler::selectNext()
103  {
104    printf("searching next Widget\n");
105    // retrieve Objects.
106    const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET);
107
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
137        if (dynamic_cast<GLGuiWidget*>(*it)->selectable())
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
152
153  void GLGuiHandler::process(const Event &event)
154  {
155    switch (event.type)
156    {
157      case  EV_MOUSE_BUTTON_LEFT:
158        if (GLGuiWidget::mouseFocused() != NULL && event.bPressed)
159        {
160          // if clickable select the Widget.
161          if (GLGuiWidget::mouseFocused()->clickable())
162          {
163            Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
164            GLGuiWidget::mouseFocused()->select();
165            GLGuiWidget::mouseFocused()->click(cursorPos - GLGuiWidget::mouseFocused()->getAbsCoor2D());
166          }
167        }
168        else if (GLGuiWidget::selected() != NULL && !event.bPressed)
169        {
170          if (GLGuiWidget::selected()->clickable())
171          {
172            Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
173            GLGuiWidget::selected()->release(cursorPos - GLGuiWidget::selected()->getAbsCoor2D());
174          }
175        }
176
177        break;
178      case EV_LEAVE_STATE:
179        if (GLGuiWidget::selected() != NULL)
180          GLGuiWidget::selected()->unselect();
181
182        if (GLGuiWidget::mouseFocused() != NULL)
183          GLGuiWidget::mouseFocused()->breakMouseFocus();
184        break;
185
186      case EV_VIDEO_RESIZE:
187        if (this->_cursor != NULL)
188          this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h));
189        break;
190      case SDLK_TAB:
191        if (event.bPressed)
192          this->selectNext();
193        break;
194    }
195
196    if (GLGuiWidget::selected() != NULL)
197    {
198      GLGuiWidget::selected()->processEvent(event);
199    }
200
201
202
203  }
204
205
206  Vector2D GLGuiHandler::cursorPositionOverFocusedWidget() const
207  {
208    return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(0,0);
209  }
210
211  const Vector2D& GLGuiHandler::cursorPositionAbs() const
212  {
213    if (this->_cursor)
214      return this->_cursor->getAbsCoor2D();
215    else
216      return Vector2D::nullVector();
217  }
218
219  Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const
220  {
221    assert (widget != NULL);
222    if (this->_cursor)
223      return  this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D();
224    else
225      return Vector2D::nullVector();
226  }
227
228
229  void GLGuiHandler::checkFocus()
230  {
231   // CHECK THE COLLISIONS.
232    const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET);
233
234    if (objects != NULL && this->_cursor != NULL)
235    {
236      for (std::list<BaseObject*>::const_iterator it = objects->begin(); it != objects->end(); it++)
237      {
238        GLGuiWidget* widget = dynamic_cast<GLGuiWidget*>(*it);
239
240        if (widget->isVisible() &&
241            widget->focusable() &&
242            widget->focusOverWidget(this->_cursor))
243        {
244          // receiving Focus
245          if (GLGuiWidget::mouseFocused() != widget)
246          {
247            widget->giveMouseFocus();
248          }
249          return ;
250        }
251      }
252      if (GLGuiWidget::mouseFocused() != NULL)
253        GLGuiWidget::mouseFocused()->breakMouseFocus();
254    }
255  }
256
257  void GLGuiHandler::draw()
258  {
259    //    GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP);
260  }
261
262
263  void GLGuiHandler::tick(float dt)
264  {
265    // do not change if we already clicked into a Widget.
266    // if (GLGuiWidget::selected() != NULL && GLGuiWidget::selected()->pushed())
267    //      return ;
268
269    this->checkFocus();
270  }
271}
Note: See TracBrowser for help on using the repository browser.