Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/gui/gl/glgui_handler.cc @ 8711

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

merged the script_engine back here

File size: 4.6 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#include <cassert>
26
27#include <cassert>
28
29
30/// TAKE THIS OUT OF HERE.
31#include "graphics_engine.h"
32
33namespace OrxGui
34{
35
36  /**
37   * standard constructor
38   */
39  GLGuiHandler::GLGuiHandler ()
40  {
41    this->setClassID(CL_GLGUI_HANDLER, "GLGuiHandler");
42    this->setName("GLGuiHandler");
43
44
45    EventHandler::getInstance()->withUNICODE(ES_MENU, true );
46
47    this->_cursor = NULL;
48    for (unsigned int i = 0; i < EV_NUMBER; i++)
49    {
50      this->subscribeEvent(ES_ALL, i);
51    }
52  }
53
54  /**
55   *  the singleton reference to this class
56   */
57  GLGuiHandler* GLGuiHandler::singletonRef = NULL;
58
59  /**
60     @brief standard deconstructor
61   */
62  GLGuiHandler::~GLGuiHandler ()
63  {
64    GLGuiHandler::singletonRef = NULL;
65  }
66
67  void GLGuiHandler::activateCursor()
68  {
69    if (this->_cursor == NULL)
70      this->_cursor = new GLGuiCursor();
71    this->_cursor->show();
72    this->_cursor->setMaxBorders(Vector2D(GraphicsEngine::getInstance()->getResolutionX(), GraphicsEngine::getInstance()->getResolutionY()));
73  }
74
75  void GLGuiHandler::deactivateCursor(bool deleteCursor)
76  {
77    if (this->_cursor)
78    {
79      if (deleteCursor)
80        delete this->_cursor;
81      this->_cursor = NULL;
82    }
83  }
84
85
86  void GLGuiHandler::activate()
87  {
88    EventHandler::getInstance()->pushState(ES_MENU);
89
90
91
92  }
93
94  void GLGuiHandler::deactivate()
95  {
96    EventHandler::getInstance()->popState();
97
98
99  }
100
101
102  void GLGuiHandler::process(const Event &event)
103  {
104    switch (event.type)
105    {
106      case  EV_MOUSE_BUTTON_LEFT:
107        if (GLGuiWidget::focused() != NULL)
108        {
109          if (event.bPressed)
110          {
111            if (GLGuiWidget::focused()->clickable())
112            {
113              Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
114              GLGuiWidget::focused()->click(cursorPos - GLGuiWidget::focused()->getAbsCoor2D());
115            }
116          }
117          else
118          {
119            if (GLGuiWidget::focused()->clickable())
120            {
121              Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
122              GLGuiWidget::focused()->release(cursorPos - GLGuiWidget::focused()->getAbsCoor2D());
123            }
124          }
125        }
126        break;
127      case EV_LEAVE_STATE:
128        if (GLGuiWidget::focused() != NULL)
129          GLGuiWidget::focused()->breakFocus();
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::focused())
141    {
142      GLGuiWidget::focused()->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::focused() != widget)
196          {
197            widget->giveFocus();
198          }
199          return ;
200        }
201      }
202      if (GLGuiWidget::focused() != NULL)
203        GLGuiWidget::focused()->breakFocus();
204    }
205  }
206}
Note: See TracBrowser for help on using the repository browser.