Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8676 was 8417, checked in by snellen, 19 years ago

helicopter is scripted!

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