Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

resizing should work, but strangely it does not

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