Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added game_menu.cc

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    // 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
151
152  void GLGuiHandler::process(const Event &event)
153  {
154    switch (event.type)
155    {
156      case  EV_MOUSE_BUTTON_LEFT:
157        if (GLGuiWidget::mouseFocused() != NULL && event.bPressed)
158        {
159          // if clickable select the Widget.
160          if (GLGuiWidget::mouseFocused()->clickable())
161          {
162            Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
163            GLGuiWidget::mouseFocused()->select();
164            GLGuiWidget::mouseFocused()->click(cursorPos - GLGuiWidget::mouseFocused()->getAbsCoor2D());
165          }
166        }
167        else if (GLGuiWidget::selected() != NULL && !event.bPressed)
168        {
169          if (GLGuiWidget::selected()->clickable())
170          {
171            Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
172            GLGuiWidget::selected()->release(cursorPos - GLGuiWidget::selected()->getAbsCoor2D());
173          }
174        }
175
176        break;
177      case EV_LEAVE_STATE:
178        if (GLGuiWidget::selected() != NULL)
179          GLGuiWidget::selected()->unselect();
180
181        if (GLGuiWidget::mouseFocused() != NULL)
182          GLGuiWidget::mouseFocused()->breakMouseFocus();
183        break;
184
185      case EV_VIDEO_RESIZE:
186        if (this->_cursor != NULL)
187          this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h));
188        break;
189      case SDLK_TAB:
190        if (event.bPressed)
191          this->selectNext();
192        break;
193    }
194
195    if (GLGuiWidget::selected() != NULL)
196    {
197      GLGuiWidget::selected()->processEvent(event);
198    }
199
200
201
202  }
203
204
205  Vector2D GLGuiHandler::cursorPositionOverFocusedWidget() const
206  {
207    return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(0,0);
208  }
209
210  const Vector2D& GLGuiHandler::cursorPositionAbs() const
211  {
212    if (this->_cursor)
213      return this->_cursor->getAbsCoor2D();
214    else
215      return Vector2D::nullVector();
216  }
217
218  Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const
219  {
220    assert (widget != NULL);
221    if (this->_cursor)
222      return  this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D();
223    else
224      return Vector2D::nullVector();
225  }
226
227
228  void GLGuiHandler::checkFocus()
229  {
230   // CHECK THE COLLISIONS.
231    const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET);
232
233    if (objects != NULL && this->_cursor != NULL)
234    {
235      for (std::list<BaseObject*>::const_iterator it = objects->begin(); it != objects->end(); it++)
236      {
237        GLGuiWidget* widget = dynamic_cast<GLGuiWidget*>(*it);
238
239        if (widget->isVisible() &&
240            widget->focusable() &&
241            widget->focusOverWidget(this->_cursor))
242        {
243          // receiving Focus
244          if (GLGuiWidget::mouseFocused() != widget)
245          {
246            widget->giveMouseFocus();
247          }
248          return ;
249        }
250      }
251      if (GLGuiWidget::mouseFocused() != NULL)
252        GLGuiWidget::mouseFocused()->breakMouseFocus();
253    }
254  }
255
256  void GLGuiHandler::draw()
257  {
258    //    GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP);
259  }
260
261
262  void GLGuiHandler::tick(float dt)
263  {
264    // do not change if we already clicked into a Widget.
265    // if (GLGuiWidget::selected() != NULL && GLGuiWidget::selected()->pushed())
266    //      return ;
267
268    this->checkFocus();
269  }
270}
Note: See TracBrowser for help on using the repository browser.