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
RevLine 
[4744]1/*
[3655]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:
[5366]12   main-programmer: Benjamin Grauer
[3655]13   co-programmer: ...
14*/
15
[5401]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GUI
[3655]17
[5366]18#include "glgui_handler.h"
[5388]19#include "event_handler.h"
[3655]20
[5406]21#include "glgui_mainwidget.h"
[7919]22#include "glgui_cursor.h"
[5406]23
[7919]24#include "class_list.h"
25
[8450]26#include <cassert>
[7919]27
[8450]28
[7919]29/// TAKE THIS OUT OF HERE.
30#include "graphics_engine.h"
31
[7779]32namespace OrxGui
[3655]33{
34
[7779]35  /**
36   * standard constructor
37   */
38  GLGuiHandler::GLGuiHandler ()
39  {
40    this->setClassID(CL_GLGUI_HANDLER, "GLGuiHandler");
41    this->setName("GLGuiHandler");
[3655]42
[7868]43
[8448]44    EventHandler::getInstance()->withUNICODE(ES_MENU, true );
[7919]45
[8324]46    this->_cursor = NULL;
[7919]47    for (unsigned int i = 0; i < EV_NUMBER; i++)
48    {
[8312]49      this->subscribeEvent(ES_ALL, i);
[7919]50    }
[7779]51  }
[3655]52
[7779]53  /**
54   *  the singleton reference to this class
55   */
56  GLGuiHandler* GLGuiHandler::singletonRef = NULL;
[5388]57
[7779]58  /**
59     @brief standard deconstructor
60   */
61  GLGuiHandler::~GLGuiHandler ()
62  {
63    GLGuiHandler::singletonRef = NULL;
64  }
[5388]65
[7919]66  void GLGuiHandler::activateCursor()
67  {
[8324]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()));
[7919]72  }
73
74  void GLGuiHandler::deactivateCursor(bool deleteCursor)
75  {
[8324]76    if (this->_cursor)
[7919]77    {
78      if (deleteCursor)
[8324]79        delete this->_cursor;
80      this->_cursor = NULL;
[7919]81    }
82  }
83
84
[7779]85  void GLGuiHandler::activate()
86  {
87    EventHandler::getInstance()->pushState(ES_MENU);
[5388]88
[7919]89
90
[7779]91  }
[5388]92
[7779]93  void GLGuiHandler::deactivate()
94  {
95    EventHandler::getInstance()->popState();
[5388]96
[7919]97
[7779]98  }
[5388]99
100
[7779]101  void GLGuiHandler::process(const Event &event)
102  {
[7919]103    switch (event.type)
104    {
105      case  EV_MOUSE_BUTTON_LEFT:
[8664]106        if (GLGuiWidget::mouseFocused() != NULL && event.bPressed)
[7919]107        {
[8664]108          // if clickable select the Widget.
109          if (GLGuiWidget::mouseFocused()->clickable())
[7919]110          {
[8664]111            Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
112            GLGuiWidget::mouseFocused()->select();
[8667]113            GLGuiWidget::mouseFocused()->click(cursorPos - GLGuiWidget::mouseFocused()->getAbsCoor2D());
[7919]114          }
[8664]115        }
[8667]116        else if (GLGuiWidget::selected() != NULL && !event.bPressed)
[8664]117        {
118          if (GLGuiWidget::selected()->clickable())
[7919]119          {
[8664]120            Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y);
121            GLGuiWidget::selected()->release(cursorPos - GLGuiWidget::selected()->getAbsCoor2D());
[7919]122          }
123        }
[8664]124
[7919]125        break;
126      case EV_LEAVE_STATE:
[8664]127        if (GLGuiWidget::selected() != NULL)
128          GLGuiWidget::selected()->unselect();
129
[8652]130        if (GLGuiWidget::mouseFocused() != NULL)
131          GLGuiWidget::mouseFocused()->breakMouseFocus();
[7919]132        break;
[5388]133
[7919]134      case EV_VIDEO_RESIZE:
[8324]135        if (this->_cursor != NULL)
136          this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h));
[7919]137        break;
138    }
[5388]139
[7919]140
141
[8664]142    if (GLGuiWidget::selected() != NULL)
[7919]143    {
[8653]144      GLGuiWidget::selected()->processEvent(event);
[7919]145    }
146
147
148
[7779]149  }
[5406]150
[8035]151
152  Vector2D GLGuiHandler::cursorPositionOverFocusedWidget() const
153  {
[8324]154    return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(0,0);
[8035]155  }
156
157  const Vector2D& GLGuiHandler::cursorPositionAbs() const
158  {
[8324]159    if (this->_cursor)
160      return this->_cursor->getAbsCoor2D();
[8035]161    else
162      return Vector2D::nullVector();
163  }
[8667]164
[8035]165  Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const
166  {
167    assert (widget != NULL);
[8324]168    if (this->_cursor)
169      return  this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D();
[8035]170    else
171      return Vector2D::nullVector();
172  }
173
174
[8667]175  void GLGuiHandler::checkFocus()
[7779]176  {
[8667]177   // CHECK THE COLLISIONS.
[7919]178    const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET);
179
[8324]180    if (objects != NULL && this->_cursor != NULL)
[7919]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() &&
[8324]188            widget->focusOverWidget(this->_cursor))
[7919]189        {
190          // receiving Focus
[8652]191          if (GLGuiWidget::mouseFocused() != widget)
[7919]192          {
[8652]193            widget->giveMouseFocus();
[7919]194          }
195          return ;
196        }
197      }
[8652]198      if (GLGuiWidget::mouseFocused() != NULL)
199        GLGuiWidget::mouseFocused()->breakMouseFocus();
[7919]200    }
[7779]201  }
[8667]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  }
[5406]217}
Note: See TracBrowser for help on using the repository browser.