Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

renamed newclassid to classid and newobjectlist to objectlist

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 <cassert>
26
27#include "debug.h"
28
29
30/// TAKE THIS OUT OF HERE.
31#include "graphics_engine.h"
32#include "loading/resource_manager.h"
33
34namespace OrxGui
35{
36  ObjectListDefinition(GLGuiHandler);
37  /**
38   * standard constructor
39   */
40  GLGuiHandler::GLGuiHandler ()
41  {
42    this->registerObject(this, GLGuiHandler::_objectList);
43    this->setName("GLGuiHandler");
44
45    this->_resolution = Vector2D(GraphicsEngine::getInstance()->getResolutionX(), GraphicsEngine::getInstance()->getResolutionY());
46
47    EventHandler::getInstance()->withUNICODE(ES_MENU, true );
48
49    this->_cursor = NULL;
50    for (unsigned int i = 0; i < EV_NUMBER; i++)
51    {
52      this->subscribeEvent(ES_GAME, i);
53      this->subscribeEvent(ES_GAME_MENU, i);
54      this->subscribeEvent(ES_MENU, 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  const Vector2D& GLGuiHandler::resolution()
93  {
94    if (this->_resolution == Vector2D::nullVector())
95      this->_resolution = Vector2D(GraphicsEngine::getInstance()->getResolutionX(), GraphicsEngine::getInstance()->getResolutionY());
96    return _resolution;
97  }
98
99
100  void GLGuiHandler::activate()
101  {
102    //EventHandler::getInstance()->pushState(ES_MENU);
103
104
105
106  }
107
108  void GLGuiHandler::deactivate()
109  {
110    //EventHandler::getInstance()->popState();
111
112
113  }
114
115  void GLGuiHandler::selectNext()
116  {
117    // retrieve Objects.
118    ObjectList<GLGuiWidget>::const_iterator it, currentIt;
119    currentIt = GLGuiWidget::objectList().end();
120
121    if (GLGuiWidget::selected() != NULL)
122    {
123      it = std::find(GLGuiWidget::objectList().begin(), GLGuiWidget::objectList().end(), GLGuiWidget::selected());
124      if (it != GLGuiWidget::objectList().end())
125      {
126        currentIt = it;
127        it++;
128      }
129    }
130    else
131    {
132      it = GLGuiWidget::objectList().begin();
133    }
134
135    bool cycledOnce = false;
136
137    for (; it != currentIt; ++it)
138    {
139      if (it == GLGuiWidget::objectList().end() && !cycledOnce)
140      {
141        it = GLGuiWidget::objectList().begin();
142        cycledOnce = true;
143      }
144
145      if ((*it)->selectable() && (*it)->isVisible())
146      {
147        (*it)->select();
148        return;
149      }
150    }
151
152  }
153
154  void GLGuiHandler::selectPrevious()
155  {
156    ObjectList<GLGuiWidget>::const_iterator it, currentIt;
157    currentIt = GLGuiWidget::objectList().begin();
158
159      if (GLGuiWidget::selected() != NULL)
160      {
161        it = std::find(GLGuiWidget::objectList().begin(), GLGuiWidget::objectList().end(), GLGuiWidget::selected());
162        if (it != GLGuiWidget::objectList().end())
163        {
164          currentIt = it;
165          it--;
166        }
167      }
168      else
169      {
170        it = GLGuiWidget::objectList().end();
171      }
172
173      bool cycledOnce = false;
174
175      for (; it != currentIt; --it)
176      {
177        if (it == GLGuiWidget::objectList().end() && !cycledOnce)
178        {
179          --it ;
180          cycledOnce = true;
181        }
182
183        if ((*it)->selectable() && (*it)->isVisible())
184        {
185          (*it)->select();
186          return;
187        }
188      }
189
190  }
191
192
193
194  void GLGuiHandler::process(const Event &event)
195  {
196    switch (event.type)
197    {
198      case EV_MOUSE_MOTION:
199      this->checkFocus();
200      break;
201
202      case  EV_MOUSE_BUTTON_LEFT:
203      if (GLGuiWidget::mouseFocused() != NULL && event.bPressed)
204      {
205        // if clickable select the Widget.
206        if (GLGuiWidget::mouseFocused()->clickable())
207        {
208          Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
209          GLGuiWidget::mouseFocused()->select();
210          GLGuiWidget::mouseFocused()->click(cursorPos - GLGuiWidget::mouseFocused()->getAbsCoor2D());
211        }
212      }
213      else if (GLGuiWidget::selected() != NULL && !event.bPressed)
214      {
215        if (GLGuiWidget::selected()->clickable())
216        {
217          Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
218          GLGuiWidget::selected()->release(cursorPos - GLGuiWidget::selected()->getAbsCoor2D());
219        }
220      }
221
222      break;
223      case EV_LEAVE_STATE:
224      if (GLGuiWidget::selected() != NULL)
225        GLGuiWidget::selected()->unselect();
226
227      if (GLGuiWidget::mouseFocused() != NULL)
228        GLGuiWidget::mouseFocused()->breakMouseFocus();
229      break;
230
231      case EV_VIDEO_RESIZE:
232      if (this->_cursor != NULL)
233        this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h));
234      this->_resolution = Vector2D(event.resize.w, event.resize.h);
235      break;
236
237      case SDLK_TAB:
238      if (event.bPressed)
239      {
240        if (EventHandler::getInstance()->isPressed(SDLK_LSHIFT) || EventHandler::getInstance()->isPressed(SDLK_RSHIFT))
241          this->selectPrevious();
242        else
243          this->selectNext();
244      }
245      break;
246    }
247
248
249    // Send the Event to the Widget below.
250    if (GLGuiWidget::selected() != NULL)
251    {
252      GLGuiWidget::selected()->processEvent(event);
253    }
254
255
256
257  }
258
259
260  const Vector2D& GLGuiHandler::cursorPositionOverFocusedWidget() const
261  {
262    return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D::nullVector();
263  }
264
265  const Vector2D& GLGuiHandler::cursorPositionAbs() const
266  {
267    if (this->_cursor)
268      return this->_cursor->getAbsCoor2D();
269    else
270      return Vector2D::nullVector();
271  }
272
273  Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const
274  {
275    assert (widget != NULL);
276    if (this->_cursor)
277      return  this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D();
278    else
279      return Vector2D::nullVector();
280  }
281
282
283  void GLGuiHandler::checkFocus()
284  {
285    // CHECK THE COLLISIONS.
286    if (this->_cursor != NULL)
287    {
288      for (ObjectList<GLGuiWidget>::const_iterator it = GLGuiWidget::objectList().begin();
289           it != GLGuiWidget::objectList().end();
290           it++)
291      {
292        GLGuiWidget* widget = (*it);
293
294        if (widget->isVisible() &&
295            widget->focusable() &&
296            widget->focusOverWidget(this->_cursor))
297        {
298          // receiving Focus
299          if (GLGuiWidget::mouseFocused() != widget)
300          {
301            widget->giveMouseFocus();
302          }
303          return ;
304        }
305      }
306      if (GLGuiWidget::mouseFocused() != NULL)
307        GLGuiWidget::mouseFocused()->breakMouseFocus();
308    }
309  }
310
311  void GLGuiHandler::draw()
312  {
313    //    GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP);
314  }
315
316
317  void GLGuiHandler::tick(float dt)
318  {
319    // do not change if we already clicked into a Widget.
320    // if (GLGuiWidget::selected() != NULL && GLGuiWidget::selected()->pushed())
321    //      return ;
322
323    this->checkFocus();
324  }
325}
Note: See TracBrowser for help on using the repository browser.