/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GUI #include "glgui_table.h" #include "debug.h" #include "glgui_handler.h" namespace OrxGui { ObjectListDefinition(GLGuiTable); /** * @brief standard constructor */ GLGuiTable::GLGuiTable (unsigned int rows, unsigned int columns) : _selected(-1, -1), _focused(-1, -1) { this->init(); this->setRowCount(rows); this->setColumnCount(columns); } /** * @brief standard deconstructor */ GLGuiTable::~GLGuiTable() {} /** * @brief initializes the GUI-element */ void GLGuiTable::init() { this->registerObject(this, GLGuiTable::_objectList); this->setBorderTop(10); this->setFocusable(true); this->setClickable(true); } void GLGuiTable::setRowCount(unsigned int rowCount) { unsigned int currentRowCount = this->rowCount(); this->_rowHeights.resize(rowCount, 20); this->_entries.resize(rowCount); for (unsigned int i = currentRowCount; i < this->rowCount(); ++i) { this->_entries[i].resize(columnCount(), LimitedWidthText("fonts/final_frontier.ttf", 20)); for (unsigned int j = 0; j < columnCount(); ++j) this->applyTextSettings(i, j, &this->_entries[i][j]); } assert(this->checkIntegrity()); this->repositionText(currentRowCount, 0); this->resize(); if (!this->isVisible()) this->hiding(); } void GLGuiTable::setColumnCount(unsigned int columnCount) { unsigned int i; unsigned int currentColumnCount = this->columnCount(); this->_columnWidths.resize(columnCount, 100); // setup Headers. this->_headers.resize(columnCount, LimitedWidthText("fonts/final_frontier.ttf", 20)); for (i = currentColumnCount; i < columnCount; ++i) this->applyTextSettings(0, i, &this->_headers[i]); for (i = 0; i < rowCount(); i++) { this->_entries[i].resize(columnCount, LimitedWidthText("fonts/final_frontier.ttf", 20)); for (unsigned int j = currentColumnCount; j < columnCount; ++j) this->applyTextSettings(i, j, &this->_entries[i][j]); } assert(this->checkIntegrity()); this->repositionText(0, currentColumnCount); this->resize(); if (!this->isVisible()) this->hiding(); } void GLGuiTable::setHeader(unsigned int number, const std::string& headerName) { if (number >= this->columnCount()) this->setColumnCount(number + 1); this->_headers[number].setText(headerName); } void GLGuiTable::setHeader(const std::vector& headerNames) { for (unsigned int i = 0; i < headerNames.size(); ++i) this->setHeader(i, headerNames[i]); } void GLGuiTable::setEntry(unsigned int row, unsigned int column, const std::string& name) { if (column >= this->columnCount() ) this->setColumnCount(column + 1); if (row >= this->rowCount() ) this->setRowCount(row + 1); this->_entries[row][column].setText(name); } void GLGuiTable::setColumnWidth(unsigned int column, float size) { this->_headers[column].setLineWidth(size); for (unsigned int i = 0; i < this->rowCount(); ++i) this->_entries[i][column].setLineWidth(size); } /// TODO. void GLGuiTable::setRowHeight(unsigned int row, unsigned int size) {} /** * removes the focus from this Widget. */ void GLGuiTable::removedFocus() { GLGuiWidget::removedFocus(); } /** * @brief Processes an Event. * @param event The event to be processed * @return true if the event was catched. */ bool GLGuiTable::processEvent(const Event& event) { if (event.type == EV_MOUSE_MOTION) { this->pixelPositionToElement((OrxGui::GLGuiHandler::getInstance()->cursorPositionRel(this)), &this->_focused.row, &this->_focused.column); return true; } return false; } /** * @brief Resizes the Widget to the new Size-constraints. */ void GLGuiTable::resize() { Vector2D size; unsigned int i; for(i = 0; i < this->columnCount(); ++i) size.x += this->_columnWidths[i]; for(i = 0; i < this->rowCount(); ++i) size.y += this->_rowHeights[i]; this->setSize2D(size); this->setSize2D( size + Vector2D(borderLeft() + borderRight(), borderTop() + borderBottom())); GLGuiWidget::resize(); } void GLGuiTable::updateFrontColor() { for (unsigned int i = 0; i < this->columnCount(); ++i) { this->_headers[i].setColor(this->foregroundColor()); for (unsigned int j = 0; j < this->rowCount(); ++j) this->_entries[j][i].setColor(this->foregroundColor()); } } void GLGuiTable::hiding() { for (unsigned int i = 0; i < this->columnCount(); ++i) { this->_headers[i].setVisibility(false); for (unsigned int j = 0; j < this->rowCount(); ++j) this->_entries[j][i].setVisibility(false); } } void GLGuiTable::showing() { for (unsigned int i = 0; i < this->columnCount(); ++i) { this->_headers[i].setVisibility(true); for (unsigned int j = 0; j < this->rowCount(); ++j) this->_entries[j][i].setVisibility(true); } } /** * @brief draws the GLGuiTable */ void GLGuiTable::draw() const { this->beginDraw(); GLGuiWidget::draw(); float columnPos = this->borderLeft(); float rowPos = this->borderTop(); unsigned int i; glColor4fv(&this->foregroundColor()[0]); glLineWidth(1.5); glBegin(GL_LINES); glVertex2f(columnPos, this->borderTop()); glVertex2f(columnPos, this->getSizeY2D()); for (i = 0; i < this->columnCount(); ++i) { columnPos +=this->_columnWidths[i]; glVertex2f(columnPos, this->borderTop()); glVertex2f(columnPos, this->getSizeY2D()); } glVertex2f(this->borderLeft(), rowPos); glVertex2f(this->getSizeX2D(), rowPos); for (i = 0; i < this->rowCount(); ++i) { rowPos +=this->_rowHeights[i]; glVertex2f(this->borderLeft(), rowPos); glVertex2f(this->getSizeX2D(), rowPos); } glEnd(); this->endDraw(); } void GLGuiTable::pixelPositionToElement(const Vector2D& pixelPosition, int* row, int* column) { *row = 0; *column = 0; float columnPos = this->borderLeft(); float rowPos = this->borderTop(); for (*row = 0; *row < (int)this->rowCount(); (*row)++) { if (pixelPosition.y > rowPos && pixelPosition.y < (rowPos += _rowHeights[*row])) break; } for (*column = 0; *column < (int)this->columnCount(); (*column)++) { if (pixelPosition.x > columnPos && pixelPosition.x < (columnPos += _columnWidths[*column])) break; } PRINTF(3)("Mouse Over row:%d Column:%d\n", *row, *column); } void GLGuiTable::repositionText(unsigned int fromLeft, unsigned int fromTop) { float columnPos = this->borderLeft(); float rowPos = this->borderTop(); unsigned int i, j; for (i = 0; i< fromLeft; ++i) columnPos+=this->_columnWidths[i]; for (i = 0; i < fromTop; ++i) rowPos += this->_rowHeights[i]; for (i = fromLeft; i < this->columnCount(); ++i) { this->_headers[i].setRelCoor2D(columnPos, rowPos); // not required, but a good idea :) this->_headers[i].setLineWidth(_columnWidths[i]); float rowPosI = rowPos; for (j = fromTop; j < this->rowCount(); ++j) { this->_entries[j][i].setRelCoor2D(columnPos, rowPosI); this->_entries[j][i].setLineWidth(_columnWidths[i]); rowPosI += _rowHeights[j]; } columnPos+=this->_columnWidths[i]; } } /** * @brief applies the GLGuiNotifiers Settings to a single Text of the GLGuiNotifier. * @param text the Text to apply the settings to. */ void GLGuiTable::applyTextSettings(unsigned int row, unsigned int column, LimitedWidthText* text) { text->setSize(this->textSize()); text->setLineWidth( this->_columnWidths[column] ); text->setFont("fonts/final_frontier.ttf", (int)this->textSize()); text->setColor(this->foregroundColor()); text->setColor(this->foregroundColor()); if (text->getParent2D() != this) text->setParent2D(this); } void GLGuiTable::debug() const { PRINT(0)("Table: Size = %dx%d\n", this->rowCount(), this->columnCount()); for (unsigned int i = 0; i < this->rowCount(); ++i) PRINT(0)("line %d: columns %d\n", i, this->_entries[i].size()); } bool GLGuiTable::checkIntegrity() const { bool retVal = true; if (rowCount() != this->_entries.size()) { PRINTF(1)("ROW COUNT WRONG (is %d should be %d)\n", rowCount(), this->_entries.size()); retVal = false; } if (columnCount() != this->_headers.size()) { PRINTF(1)("COLUMN COUNT WRONG (is %d should be %d)\n", columnCount(), this->_headers.size()); retVal = false; } for (unsigned int i = 0; i < this->rowCount(); ++i) if (this->_entries[i].size() != this->columnCount()) { PRINTF(1)("COLUMN-COUNT OF ROW %d WRONG (is %d should be %d)\n", i, this->_entries[i].size(), this->columnCount()); retVal = false; } return retVal; } }