Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/gui/gl/glgui_table.cc @ 8647

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

correct Values of Boxes

File size: 9.0 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_table.h"
19#include "debug.h"
20
21#include "glgui_handler.h"
22
23namespace OrxGui
24{
25  /**
26   * @brief standard constructor
27   */
28  GLGuiTable::GLGuiTable (unsigned int rows, unsigned int columns)
29  {
30    this->init();
31
32    this->setRowCount(rows);
33    this->setColumnCount(columns);
34  }
35
36
37  /**
38   * @brief standard deconstructor
39   */
40  GLGuiTable::~GLGuiTable()
41  {}
42
43  /**
44   * @brief initializes the GUI-element
45   */
46  void GLGuiTable::init()
47  {
48    this->setClassID(CL_GLGUI_TABLE, "GLGuiTable");
49
50    this->setFocusable(true);
51    this->setClickable(true);
52  }
53
54
55  void GLGuiTable::setRowCount(unsigned int rowCount)
56  {
57
58    unsigned int currentRowCount = this->rowCount();
59
60    this->_rowHeights.resize(rowCount, 20);
61
62    this->_entries.resize(rowCount);
63    for (unsigned int i = currentRowCount; i < this->rowCount(); ++i)
64    {
65      this->_entries[i].resize(columnCount(), LimitedWidthText("fonts/final_frontier.ttf", 20));
66      for (unsigned int j = 0; j < columnCount(); ++j)
67        this->applyTextSettings(i, j, &this->_entries[i][j]);
68    }
69
70    assert(this->checkIntegrity());
71    this->repositionText(currentRowCount, 0);
72
73
74    this->resize();
75
76    if (!this->isVisible())
77      this->hiding();
78  }
79
80  void GLGuiTable::setColumnCount(unsigned int columnCount)
81  {
82    unsigned int i;
83    unsigned int currentColumnCount = this->columnCount();
84
85    this->_columnWidths.resize(columnCount, 100);
86
87    // setup Headers.
88    this->_headers.resize(columnCount, LimitedWidthText("fonts/final_frontier.ttf", 20));
89    for (i = currentColumnCount; i < columnCount; ++i)
90      this->applyTextSettings(0, i, &this->_headers[i]);
91
92    for (i = 0; i < rowCount(); i++)
93    {
94      this->_entries[i].resize(columnCount, LimitedWidthText("fonts/final_frontier.ttf", 20));
95      for (unsigned int j = currentColumnCount; j < columnCount; ++j)
96        this->applyTextSettings(i, j, &this->_entries[i][j]);
97
98    }
99    assert(this->checkIntegrity());
100
101    this->repositionText(0, currentColumnCount);
102
103    this->resize();
104    if (!this->isVisible())
105      this->hiding();
106  }
107
108
109
110
111  void GLGuiTable::setHeader(unsigned int number, const std::string& headerName)
112  {
113    if (number >= this->columnCount())
114      this->setColumnCount(number + 1);
115    this->_headers[number].setText(headerName);
116  }
117
118  void GLGuiTable::setHeader(const std::vector<std::string>& headerNames)
119  {
120    for (unsigned int i = 0; i < headerNames.size(); ++i)
121      this->setHeader(i, headerNames[i]);
122  }
123
124  void GLGuiTable::setEntry(unsigned int row, unsigned int column, const std::string& name)
125  {
126    if (column >= this->columnCount() )
127      this->setColumnCount(column + 1);
128    if (row >= this->rowCount() )
129      this->setRowCount(row + 1);
130    this->_entries[row][column].setText(name);
131  }
132
133  void GLGuiTable::setColumnWidth(unsigned int column, float size)
134  {
135    this->_headers[column].setLineWidth(size);
136    for (unsigned int i = 0; i < this->rowCount(); ++i)
137      this->_entries[i][column].setLineWidth(size);
138  }
139
140
141
142  /// TODO.
143  void GLGuiTable::setRowHeight(unsigned int row, unsigned int size)
144{}
145
146
147
148
149  /**
150   * removes the focus from this Widget.
151   */
152  void GLGuiTable::removedFocus()
153  {
154    GLGuiWidget::removedFocus();
155  }
156
157
158  /**
159   * @brief Processes an Event.
160   * @param event The event to be processed
161   * @return true if the event was catched.
162   */
163  bool GLGuiTable::processEvent(const Event& event)
164  {
165    if (event.type == EV_MOUSE_MOTION)
166    {
167      unsigned int row, column;
168      this->pixelPositionToElement((OrxGui::GLGuiHandler::getInstance()->cursorPositionRel(this)), &row, &column);
169      return true;
170    }
171
172    return false;
173  }
174
175
176  /**
177   * @brief Resizes the Widget to the new Size-constraints.
178   */
179  void GLGuiTable::resize()
180  {
181    Vector2D size;
182    unsigned int i;
183    for(i = 0; i < this->columnCount(); ++i)
184      size.x += this->_columnWidths[i];
185
186    for(i = 0; i < this->rowCount(); ++i)
187      size.y += this->_rowHeights[i];
188
189    this->setSize2D(size);
190    this->setSize2D( size + Vector2D(borderLeft() + borderRight(), borderTop() + borderBottom()));
191
192    GLGuiWidget::resize();
193  }
194
195  void GLGuiTable::updateFrontColor()
196  {
197    for (unsigned int i = 0; i < this->columnCount(); ++i)
198    {
199      this->_headers[i].setColor(this->foregroundColor());
200      for (unsigned int j = 0; j < this->rowCount(); ++j)
201        this->_entries[j][i].setColor(this->foregroundColor());
202    }
203
204  }
205
206  void GLGuiTable::hiding()
207  {
208    for (unsigned int i = 0; i < this->columnCount(); ++i)
209    {
210      this->_headers[i].setVisibility(false);
211      for (unsigned int j = 0; j < this->rowCount(); ++j)
212        this->_entries[j][i].setVisibility(false);
213    }
214  }
215
216  void GLGuiTable::showing()
217  {
218    for (unsigned int i = 0; i < this->columnCount(); ++i)
219    {
220      this->_headers[i].setVisibility(true);
221      for (unsigned int j = 0; j < this->rowCount(); ++j)
222        this->_entries[j][i].setVisibility(true);
223    }
224  }
225
226
227  /**
228   * @brief draws the GLGuiTable
229   */
230  void GLGuiTable::draw() const
231  {
232    this->beginDraw();
233    GLGuiWidget::draw();
234
235    float columnPos = 0.0f;
236    float rowPos = 0.0f;
237
238    unsigned int i;
239    glColor4fv(&this->foregroundColor()[0]);
240    glLineWidth(1.5);
241    glBegin(GL_LINES);
242
243    glVertex2f(0, 0);
244    glVertex2f(0, this->getSizeY2D());
245    for (i = 0; i < this->columnCount(); ++i)
246    {
247      columnPos +=this->_columnWidths[i];
248      glVertex2f(columnPos, 0);
249      glVertex2f(columnPos, this->getSizeY2D());
250    }
251
252    glVertex2f(0, 0);
253    glVertex2f(this->getSizeX2D(), 0);
254    for (i = 0; i < this->rowCount(); ++i)
255    {
256      rowPos +=this->_rowHeights[i];
257      glVertex2f(0, rowPos);
258      glVertex2f(this->getSizeX2D(), rowPos);
259    }
260
261
262    glEnd();
263
264    this->endDraw();
265  }
266
267
268  void GLGuiTable::pixelPositionToElement(const Vector2D& pixelPosition, unsigned int* row, unsigned int* column)
269  {
270    *row = 0;
271    *column = 0;
272    float columnPos = 0.0f;
273    float rowPos = 0.0f;
274
275    for (*row = 0; *row < this->rowCount(); (*row)++)
276    {
277      if (pixelPosition.y > rowPos  && pixelPosition.< (rowPos += _rowHeights[*row]))
278        break;
279    }
280    for (*column = 0; *column < this->columnCount(); (*column)++)
281    {
282      if (pixelPosition.x > columnPos && pixelPosition.< (columnPos += _columnWidths[*column]))
283        break;
284    }
285
286    PRINTF(3)("Mouse Over row:%d Column:%d\n", *row, *column);
287  }
288
289
290  void GLGuiTable::repositionText(unsigned int fromLeft, unsigned int fromTop)
291  {
292    float columnPos = 0.0f;
293    float rowPos = 0.0f;
294    unsigned int i, j;
295    for (i = 0; i< fromLeft; ++i)
296      columnPos+=this->_columnWidths[i];
297    for (i = 0; i < fromTop; ++i)
298      rowPos += this->_rowHeights[i];
299
300    for (i = fromLeft; i < this->columnCount(); ++i)
301    {
302      this->_headers[i].setRelCoor2D(columnPos, 0);
303
304      // not required, but a good idea :)
305      this->_headers[i].setLineWidth(_columnWidths[i]);
306
307      float rowPosI = rowPos;
308      for (j = fromTop; j < this->rowCount(); ++j)
309      {
310        this->_entries[j][i].setRelCoor2D(columnPos, rowPosI);
311        this->_entries[j][i].setLineWidth(_columnWidths[i]);
312        rowPosI += _rowHeights[j];
313      }
314      columnPos+=this->_columnWidths[i];
315    }
316  }
317
318  /**
319   * @brief applies the GLGuiNotifiers Settings to a single Text of the GLGuiNotifier.
320   * @param text the Text to apply the settings to.
321   */
322  void GLGuiTable::applyTextSettings(unsigned int row, unsigned int column, LimitedWidthText* text)
323  {
324    text->setSize(this->textSize());
325    text->setLineWidth( this->_columnWidths[column] );
326    text->setFont("fonts/final_frontier.ttf", (int)this->textSize());
327    text->setColor(this->foregroundColor());
328
329    text->setColor(this->foregroundColor());
330    if (text->getParent2D() != this)
331      text->setParent2D(this);
332  }
333
334
335  void GLGuiTable::debug() const
336  {
337    PRINT(0)("Table: Size = %dx%d\n", this->rowCount(), this->columnCount());
338
339    for (unsigned int i = 0; i < this->rowCount(); ++i)
340      PRINT(0)("line %d: columns %d\n", i, this->_entries[i].size());
341  }
342
343
344  bool GLGuiTable::checkIntegrity() const
345  {
346    bool retVal = true;
347    if (rowCount() != this->_entries.size())
348    {
349      PRINTF(1)("ROW COUNT WRONG (is %d should be %d)\n", rowCount(), this->_entries.size());
350      retVal = false;
351    }
352    if (columnCount() != this->_headers.size())
353    {
354      PRINTF(1)("COLUMN COUNT WRONG (is %d should be %d)\n", columnCount(), this->_headers.size());
355      retVal = false;
356    }
357    for (unsigned int i = 0; i < this->rowCount(); ++i)
358      if (this->_entries[i].size() != this->columnCount())
359      {
360        PRINTF(1)("COLUMN-COUNT OF ROW %d WRONG (is %d should be %d)\n", i, this->_entries[i].size(), this->columnCount());
361        retVal = false;
362      }
363    return retVal;
364  }
365
366}
Note: See TracBrowser for help on using the repository browser.