Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

gui:better design

File size: 7.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    // retrieve Objects.
105    const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET);
106
107    if (objects)
108    {
109      std::list<BaseObject*>::const_iterator it ;
110      std::list<BaseObject*>::const_iterator currentIt = objects->end();
111
112      if (GLGuiWidget::selected() != NULL)
113      {
114        it = std::find(objects->begin(), objects->end(), GLGuiWidget::selected());
115        if (it != objects->end())
116        {
117          currentIt = it;
118          it++;
119        }
120      }
121      else
122      {
123        it = objects->begin();
124      }
125
126      bool cycledOnce = false;
127
128      for (; it != currentIt; ++it)
129      {
130        if (it == objects->end() && !cycledOnce)
131        {
132          it = objects->begin();
133          cycledOnce = true;
134        }
135
136        if (dynamic_cast<GLGuiWidget*>(*it)->selectable())
137        {
138          dynamic_cast<GLGuiWidget*>(*it)->select();
139          return;
140        }
141      }
142
143    }
144    else
145    {
146      PRINTF(0)("NO GUI-ELEMENTS EXISTING\n");
147    }
148  }
149
150  void GLGuiHandler::selectPrevious()
151  {
152    // retrieve Objects.
153    const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET);
154
155    if (objects)
156    {
157      std::list<BaseObject*>::const_iterator it ;
158      std::list<BaseObject*>::const_iterator currentIt = objects->begin();
159
160      if (GLGuiWidget::selected() != NULL)
161      {
162        it = std::find(objects->begin(), objects->end(), GLGuiWidget::selected());
163        if (it != objects->end())
164        {
165          currentIt = it;
166          it--;
167        }
168      }
169      else
170      {
171        it = objects->end();
172      }
173
174      bool cycledOnce = false;
175
176      for (; it != currentIt; --it)
177      {
178        if (it == objects->end() && !cycledOnce)
179        {
180          --it ;
181          cycledOnce = true;
182        }
183
184        if (dynamic_cast<GLGuiWidget*>(*it)->selectable())
185        {
186          dynamic_cast<GLGuiWidget*>(*it)->select();
187          return;
188        }
189      }
190
191    }
192    else
193    {
194      PRINTF(0)("NO GUI-ELEMENTS EXISTING\n");
195    }
196  }
197
198
199
200  void GLGuiHandler::process(const Event &event)
201  {
202    switch (event.type)
203    {
204      case  EV_MOUSE_BUTTON_LEFT:
205        if (GLGuiWidget::mouseFocused() != NULL && event.bPressed)
206        {
207          // if clickable select the Widget.
208          if (GLGuiWidget::mouseFocused()->clickable())
209          {
210            Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
211            GLGuiWidget::mouseFocused()->select();
212            GLGuiWidget::mouseFocused()->click(cursorPos - GLGuiWidget::mouseFocused()->getAbsCoor2D());
213          }
214        }
215        else if (GLGuiWidget::selected() != NULL && !event.bPressed)
216        {
217          if (GLGuiWidget::selected()->clickable())
218          {
219            Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
220            GLGuiWidget::selected()->release(cursorPos - GLGuiWidget::selected()->getAbsCoor2D());
221          }
222        }
223
224        break;
225      case EV_LEAVE_STATE:
226        if (GLGuiWidget::selected() != NULL)
227          GLGuiWidget::selected()->unselect();
228
229        if (GLGuiWidget::mouseFocused() != NULL)
230          GLGuiWidget::mouseFocused()->breakMouseFocus();
231        break;
232
233      case EV_VIDEO_RESIZE:
234        if (this->_cursor != NULL)
235          this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h));
236        break;
237      case SDLK_TAB:
238        if (event.bPressed)
239          this->selectNext();
240        break;
241    }
242
243    if (GLGuiWidget::selected() != NULL)
244    {
245      GLGuiWidget::selected()->processEvent(event);
246    }
247
248
249
250  }
251
252
253  Vector2D GLGuiHandler::cursorPositionOverFocusedWidget() const
254  {
255    return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(0,0);
256  }
257
258  const Vector2D& GLGuiHandler::cursorPositionAbs() const
259  {
260    if (this->_cursor)
261      return this->_cursor->getAbsCoor2D();
262    else
263      return Vector2D::nullVector();
264  }
265
266  Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const
267  {
268    assert (widget != NULL);
269    if (this->_cursor)
270      return  this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D();
271    else
272      return Vector2D::nullVector();
273  }
274
275
276  void GLGuiHandler::checkFocus()
277  {
278   // CHECK THE COLLISIONS.
279    const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET);
280
281    if (objects != NULL && this->_cursor != NULL)
282    {
283      for (std::list<BaseObject*>::const_iterator it = objects->begin(); it != objects->end(); it++)
284      {
285        GLGuiWidget* widget = dynamic_cast<GLGuiWidget*>(*it);
286
287        if (widget->isVisible() &&
288            widget->focusable() &&
289            widget->focusOverWidget(this->_cursor))
290        {
291          // receiving Focus
292          if (GLGuiWidget::mouseFocused() != widget)
293          {
294            widget->giveMouseFocus();
295          }
296          return ;
297        }
298      }
299      if (GLGuiWidget::mouseFocused() != NULL)
300        GLGuiWidget::mouseFocused()->breakMouseFocus();
301    }
302  }
303
304  void GLGuiHandler::draw()
305  {
306    //    GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP);
307  }
308
309
310  void GLGuiHandler::tick(float dt)
311  {
312    // do not change if we already clicked into a Widget.
313    // if (GLGuiWidget::selected() != NULL && GLGuiWidget::selected()->pushed())
314    //      return ;
315
316    this->checkFocus();
317  }
318}
Note: See TracBrowser for help on using the repository browser.