Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/gui/gl/glgui_widget.cc @ 8583

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

gui: using style.

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_widget.h"
19
20#include "glgui_cursor.h"
21
22#include "material.h"
23
24#include "debug.h"
25
26namespace OrxGui
27{
28
29  /**
30   * @brief standard constructor
31  */
32  GLGuiWidget::GLGuiWidget (GLGuiWidget* parent)
33  {
34    this->init();
35
36    this->setParentWidget(parent);
37  }
38
39
40  /**
41   * @brief standard deconstructor
42   */
43  GLGuiWidget::~GLGuiWidget()
44  {
45    if (this == GLGuiWidget::_focused)
46      GLGuiWidget::_focused = NULL;
47
48    if (this->_toFrontColor)
49      delete this->_toFrontColor;
50  }
51
52  GLGuiWidget* GLGuiWidget::_selected = NULL;
53  GLGuiWidget* GLGuiWidget::_focused = NULL;
54  GLGuiWidget* GLGuiWidget::_inputGrabber = NULL;
55
56
57
58  /**
59   * initializes the GUI-element
60   */
61  void GLGuiWidget::init()
62  {
63    this->setClassID(CL_GLGUI_WIDGET, "GLGuiWidget");
64
65    this->_focusable = false;
66    this->_clickable = false;
67    this->_pushed = false;
68    this->_state = OrxGui::Normal;
69
70    this->setVisibility(GLGUI_WIDGET_DEFAULT_VISIBLE);
71
72/*    this->_backMat.setDiffuseColor(Color(1.0, 0.5, 0.4, 1.0));
73    this->_backMat.setDiffuseMap("gui_element_background.png");
74    this->_frontColor = Color(1.0, 0.0, 0.0);*/
75    this->_toFrontColor = NULL;
76  }
77
78
79  void GLGuiWidget::setParentWidget(GLGuiWidget* parent)
80  {
81    this->_parent = parent;
82
83    if (parent != NULL)
84      parent->addChild2D(this);
85  }
86
87  /** @brief gives focus to this widget */
88  void GLGuiWidget::giveFocus()
89  {
90    if (GLGuiWidget::focused() != NULL)
91      GLGuiWidget::focused()->breakFocus();
92    GLGuiWidget::_focused = this;
93    this->receivedFocus();
94  };
95
96  void GLGuiWidget::breakFocus()
97  {
98    if (GLGuiWidget::_focused == this)
99    {
100      GLGuiWidget::_focused = NULL;
101      this->_pushed = false;
102      this->removedFocus();
103    }
104  };
105
106
107  bool GLGuiWidget::focusOverWidget(const Vector2D& position) const
108  {
109    return (this->getAbsCoor2D().x < position.x && this->getAbsCoor2D().x + this->getSizeX2D() > position.x &&
110            this->getAbsCoor2D().y < position.y && this->getAbsCoor2D().y + this->getSizeY2D() > position.y);
111  }
112
113  bool GLGuiWidget::focusOverWidget(const GLGuiCursor* const cursor) const
114  {
115    return this->focusOverWidget(cursor->getAbsCoor2D());
116  }
117
118  void GLGuiWidget::setFrontColor(const Color& frontColor, bool instantaniously)
119  {
120    if (instantaniously)
121    {
122      this->style().setForegroundColor(frontColor);
123      if (this->_toFrontColor != NULL)
124      {
125        delete this->_toFrontColor;
126        this->_toFrontColor = NULL;
127      }
128    }
129    else if (!this->_toFrontColor)
130      this->_toFrontColor = new Color(frontColor);
131    else
132      *this->_toFrontColor = frontColor;
133    //this->_frontColor = frontColor;
134    //this->updateFrontColor();
135  };
136
137
138  void GLGuiWidget::resize()
139  {
140    this->backRect().setSize(this->getSize2D());
141    if (this->parent() != NULL)
142      this->parent()->resize();
143  }
144
145
146  void GLGuiWidget::click(const Vector2D& pos)
147  {
148    assert (!this->_pushed);
149    this->_pushed = true;
150
151    this->clicking(pos);
152  }
153
154  void GLGuiWidget::release(const Vector2D& pos)
155  {
156    if (this->_pushed)
157    {
158      this->releasing(pos);
159      this->_pushed = false;
160    }
161  }
162
163
164  void GLGuiWidget::clicking(const Vector2D& pos)
165  {
166    this->setFrontColor(Color(0, 0, 1));
167
168  }
169
170  void GLGuiWidget::releasing(const Vector2D& pos)
171  {
172    this->setFrontColor(Color(0,1,0));
173
174  }
175
176  void GLGuiWidget::receivedFocus()
177  {
178    this->setFrontColor(Color(0, 1, 0));
179  }
180
181  void GLGuiWidget::removedFocus()
182  {
183    this->setFrontColor(Color(1, 0, 0));
184
185  }
186
187  void GLGuiWidget::destroyed()
188  {}
189  ;
190
191
192  void GLGuiWidget::setWidgetSize(const Vector2D& size)
193  {
194    this->setSize2D(size);
195    this->resize();
196
197  }
198
199
200  void GLGuiWidget::setWidgetSize(float x, float y)
201  {
202    this->setWidgetSize(Vector2D(x, y));
203  }
204
205
206
207  void GLGuiWidget::connect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver, Slot executor)
208  {
209    sender->connect(signal, receiver, executor);
210  }
211
212  void GLGuiWidget::connect(Signal& signal, BaseObject* receiver, Slot executor)
213  {
214    signal.push_back(SignalConnector(receiver, executor));
215  }
216
217
218  void GLGuiWidget::show()
219  {
220    this->setVisibility(true);
221    this->showing();
222  }
223
224
225
226  void GLGuiWidget::hide()
227  {
228    this->setVisibility(false);
229    this->hiding();
230  }
231
232  void GLGuiWidget::tick(float dt)
233  {
234    if (this->_toFrontColor)
235    {
236      this->_style.foregroundColor(_state).slerp(*_toFrontColor, dt*3.0);
237      this->updateFrontColor();
238      if (this->style().foregroundColor(_state).dist(*_toFrontColor) < .1)
239      {
240        delete _toFrontColor;
241        _toFrontColor = NULL;
242      }
243    }
244  }
245
246
247  /**
248   * USE THIS FUNCTION ONLY FROM DERIVED CLASS
249   */
250  void GLGuiWidget::draw() const
251  {
252    this->background().select();
253    this->drawRect(this->backRect());
254    this->background().unselect();
255  }
256
257}
Note: See TracBrowser for help on using the repository browser.