Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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