Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

widgets are selectable now

File size: 5.1 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 <cassert>
27
28
29/// TAKE THIS OUT OF HERE.
30#include "graphics_engine.h"
31
32namespace OrxGui
33{
34
35  /**
36   * standard constructor
37   */
38  GLGuiHandler::GLGuiHandler ()
39  {
40    this->setClassID(CL_GLGUI_HANDLER, "GLGuiHandler");
41    this->setName("GLGuiHandler");
42
43
44    EventHandler::getInstance()->withUNICODE(ES_MENU, true );
45
46    this->_cursor = NULL;
47    for (unsigned int i = 0; i < EV_NUMBER; i++)
48    {
49      this->subscribeEvent(ES_ALL, i);
50    }
51  }
52
53  /**
54   *  the singleton reference to this class
55   */
56  GLGuiHandler* GLGuiHandler::singletonRef = NULL;
57
58  /**
59     @brief standard deconstructor
60   */
61  GLGuiHandler::~GLGuiHandler ()
62  {
63    GLGuiHandler::singletonRef = NULL;
64  }
65
66  void GLGuiHandler::activateCursor()
67  {
68    if (this->_cursor == NULL)
69      this->_cursor = new GLGuiCursor();
70    this->_cursor->show();
71    this->_cursor->setMaxBorders(Vector2D(GraphicsEngine::getInstance()->getResolutionX(), GraphicsEngine::getInstance()->getResolutionY()));
72  }
73
74  void GLGuiHandler::deactivateCursor(bool deleteCursor)
75  {
76    if (this->_cursor)
77    {
78      if (deleteCursor)
79        delete this->_cursor;
80      this->_cursor = NULL;
81    }
82  }
83
84
85  void GLGuiHandler::activate()
86  {
87    EventHandler::getInstance()->pushState(ES_MENU);
88
89
90
91  }
92
93  void GLGuiHandler::deactivate()
94  {
95    EventHandler::getInstance()->popState();
96
97
98  }
99
100
101  void GLGuiHandler::process(const Event &event)
102  {
103    switch (event.type)
104    {
105      case  EV_MOUSE_BUTTON_LEFT:
106        if (GLGuiWidget::mouseFocused() != NULL && event.bPressed)
107        {
108          // if clickable select the Widget.
109          if (GLGuiWidget::mouseFocused()->clickable())
110          {
111            Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
112            GLGuiWidget::mouseFocused()->select();
113            GLGuiWidget::mouseFocused()->click(cursorPos - GLGuiWidget::mouseFocused()->getAbsCoor2D());
114          }
115        }
116        else if (GLGuiWidget::selected() != NULL && !event.bPressed)
117        {
118          if (GLGuiWidget::selected()->clickable())
119          {
120            Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
121            GLGuiWidget::selected()->release(cursorPos - GLGuiWidget::selected()->getAbsCoor2D());
122          }
123        }
124
125        break;
126      case EV_LEAVE_STATE:
127        if (GLGuiWidget::selected() != NULL)
128          GLGuiWidget::selected()->unselect();
129
130        if (GLGuiWidget::mouseFocused() != NULL)
131          GLGuiWidget::mouseFocused()->breakMouseFocus();
132        break;
133
134      case EV_VIDEO_RESIZE:
135        if (this->_cursor != NULL)
136          this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h));
137        break;
138    }
139
140
141
142    if (GLGuiWidget::selected() != NULL)
143    {
144      GLGuiWidget::selected()->processEvent(event);
145    }
146
147
148
149  }
150
151
152  Vector2D GLGuiHandler::cursorPositionOverFocusedWidget() const
153  {
154    return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(0,0);
155  }
156
157  const Vector2D& GLGuiHandler::cursorPositionAbs() const
158  {
159    if (this->_cursor)
160      return this->_cursor->getAbsCoor2D();
161    else
162      return Vector2D::nullVector();
163  }
164
165  Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const
166  {
167    assert (widget != NULL);
168    if (this->_cursor)
169      return  this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D();
170    else
171      return Vector2D::nullVector();
172  }
173
174
175  void GLGuiHandler::checkFocus()
176  {
177   // CHECK THE COLLISIONS.
178    const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET);
179
180    if (objects != NULL && this->_cursor != NULL)
181    {
182      for (std::list<BaseObject*>::const_iterator it = objects->begin(); it != objects->end(); it++)
183      {
184        GLGuiWidget* widget = dynamic_cast<GLGuiWidget*>(*it);
185
186        if (widget->isVisible() &&
187            widget->focusable() &&
188            widget->focusOverWidget(this->_cursor))
189        {
190          // receiving Focus
191          if (GLGuiWidget::mouseFocused() != widget)
192          {
193            widget->giveMouseFocus();
194          }
195          return ;
196        }
197      }
198      if (GLGuiWidget::mouseFocused() != NULL)
199        GLGuiWidget::mouseFocused()->breakMouseFocus();
200    }
201  }
202
203  void GLGuiHandler::draw()
204  {
205    //    GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP);
206  }
207
208
209  void GLGuiHandler::tick(float dt)
210  {
211    // do not change if we already clicked into a Widget.
212    if (GLGuiWidget::selected() != NULL && GLGuiWidget::selected()->pushed())
213      return ;
214
215    this->checkFocus();
216  }
217}
Note: See TracBrowser for help on using the repository browser.