Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/gui/gl/glgui_table.cc @ 9357

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

merged the gui back

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