Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

select works (more or less)

File size: 4.7 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)
107        {
108          if (event.bPressed)
109          {
110            if (GLGuiWidget::mouseFocused()->clickable())
111            {
112              Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
113              GLGuiWidget::mouseFocused()->select();
114              GLGuiWidget::mouseFocused()->click(cursorPos - GLGuiWidget::mouseFocused()->getAbsCoor2D());
115            }
116          }
117          else
118          {
119            if (GLGuiWidget::mouseFocused()->clickable())
120            {
121              Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
122              GLGuiWidget::mouseFocused()->release(cursorPos - GLGuiWidget::mouseFocused()->getAbsCoor2D());
123            }
124          }
125        }
126        break;
127      case EV_LEAVE_STATE:
128        if (GLGuiWidget::mouseFocused() != NULL)
129          GLGuiWidget::mouseFocused()->breakMouseFocus();
130        break;
131
132      case EV_VIDEO_RESIZE:
133        if (this->_cursor != NULL)
134          this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h));
135        break;
136    }
137
138
139
140    if (GLGuiWidget::selected())
141    {
142      GLGuiWidget::selected()->processEvent(event);
143    }
144
145
146
147  }
148
149
150  Vector2D GLGuiHandler::cursorPositionOverFocusedWidget() const
151  {
152    return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(0,0);
153  }
154
155  const Vector2D& GLGuiHandler::cursorPositionAbs() const
156  {
157    if (this->_cursor)
158      return this->_cursor->getAbsCoor2D();
159    else
160      return Vector2D::nullVector();
161  }
162  Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const
163  {
164    assert (widget != NULL);
165    if (this->_cursor)
166      return  this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D();
167    else
168      return Vector2D::nullVector();
169  }
170
171
172  void GLGuiHandler::draw()
173  {
174    //    GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP);
175  }
176
177
178  void GLGuiHandler::tick(float dt)
179  {
180
181    // CHECK THE COLLISIONS.
182    const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET);
183
184    if (objects != NULL && this->_cursor != NULL)
185    {
186      for (std::list<BaseObject*>::const_iterator it = objects->begin(); it != objects->end(); it++)
187      {
188        GLGuiWidget* widget = dynamic_cast<GLGuiWidget*>(*it);
189
190        if (widget->isVisible() &&
191            widget->focusable() &&
192            widget->focusOverWidget(this->_cursor))
193        {
194          // receiving Focus
195          if (GLGuiWidget::mouseFocused() != widget)
196          {
197            widget->giveMouseFocus();
198          }
199          return ;
200        }
201      }
202      if (GLGuiWidget::mouseFocused() != NULL)
203        GLGuiWidget::mouseFocused()->breakMouseFocus();
204    }
205  }
206}
Note: See TracBrowser for help on using the repository browser.