| 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 | |
|---|
| 23 | namespace OrxGui |
|---|
| 24 | { |
|---|
| 25 | ObjectListDefinition(GLGuiTable); |
|---|
| 26 | /** |
|---|
| 27 | * @brief standard constructor |
|---|
| 28 | */ |
|---|
| 29 | GLGuiTable::GLGuiTable (unsigned int rows, unsigned int columns) |
|---|
| 30 | : _selected(-1, -1), _focused(-1, -1) |
|---|
| 31 | { |
|---|
| 32 | this->init(); |
|---|
| 33 | |
|---|
| 34 | this->setRowCount(rows); |
|---|
| 35 | this->setColumnCount(columns); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * @brief standard deconstructor |
|---|
| 41 | */ |
|---|
| 42 | GLGuiTable::~GLGuiTable() |
|---|
| 43 | {} |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * @brief initializes the GUI-element |
|---|
| 47 | */ |
|---|
| 48 | void GLGuiTable::init() |
|---|
| 49 | { |
|---|
| 50 | this->registerObject(this, GLGuiTable::_objectList); |
|---|
| 51 | |
|---|
| 52 | this->setBorderTop(10); |
|---|
| 53 | |
|---|
| 54 | this->setFocusable(true); |
|---|
| 55 | this->setClickable(true); |
|---|
| 56 | |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | void GLGuiTable::setRowCount(unsigned int rowCount) |
|---|
| 61 | { |
|---|
| 62 | |
|---|
| 63 | unsigned int currentRowCount = this->rowCount(); |
|---|
| 64 | |
|---|
| 65 | this->_rowHeights.resize(rowCount, 20); |
|---|
| 66 | |
|---|
| 67 | this->_entries.resize(rowCount); |
|---|
| 68 | for (unsigned int i = currentRowCount; i < this->rowCount(); ++i) |
|---|
| 69 | { |
|---|
| 70 | this->_entries[i].resize(columnCount(), LimitedWidthText("fonts/final_frontier.ttf", 20)); |
|---|
| 71 | for (unsigned int j = 0; j < columnCount(); ++j) |
|---|
| 72 | this->applyTextSettings(i, j, &this->_entries[i][j]); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | assert(this->checkIntegrity()); |
|---|
| 76 | this->repositionText(currentRowCount, 0); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | this->resize(); |
|---|
| 80 | |
|---|
| 81 | if (!this->isVisible()) |
|---|
| 82 | this->hiding(); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | void GLGuiTable::setColumnCount(unsigned int columnCount) |
|---|
| 86 | { |
|---|
| 87 | unsigned int i; |
|---|
| 88 | unsigned int currentColumnCount = this->columnCount(); |
|---|
| 89 | |
|---|
| 90 | this->_columnWidths.resize(columnCount, 100); |
|---|
| 91 | |
|---|
| 92 | // setup Headers. |
|---|
| 93 | this->_headers.resize(columnCount, LimitedWidthText("fonts/final_frontier.ttf", 20)); |
|---|
| 94 | for (i = currentColumnCount; i < columnCount; ++i) |
|---|
| 95 | this->applyTextSettings(0, i, &this->_headers[i]); |
|---|
| 96 | |
|---|
| 97 | for (i = 0; i < rowCount(); i++) |
|---|
| 98 | { |
|---|
| 99 | this->_entries[i].resize(columnCount, LimitedWidthText("fonts/final_frontier.ttf", 20)); |
|---|
| 100 | for (unsigned int j = currentColumnCount; j < columnCount; ++j) |
|---|
| 101 | this->applyTextSettings(i, j, &this->_entries[i][j]); |
|---|
| 102 | |
|---|
| 103 | } |
|---|
| 104 | assert(this->checkIntegrity()); |
|---|
| 105 | |
|---|
| 106 | this->repositionText(0, currentColumnCount); |
|---|
| 107 | |
|---|
| 108 | this->resize(); |
|---|
| 109 | if (!this->isVisible()) |
|---|
| 110 | this->hiding(); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | void GLGuiTable::setHeader(unsigned int number, const std::string& headerName) |
|---|
| 117 | { |
|---|
| 118 | if (number >= this->columnCount()) |
|---|
| 119 | this->setColumnCount(number + 1); |
|---|
| 120 | this->_headers[number].setText(headerName); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 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 | } |
|---|
| 128 | |
|---|
| 129 | void GLGuiTable::setEntry(unsigned int row, unsigned int column, const std::string& name) |
|---|
| 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 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | /// TODO. |
|---|
| 148 | void GLGuiTable::setRowHeight(unsigned int row, unsigned int size) |
|---|
| 149 | {} |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * removes the focus from this Widget. |
|---|
| 156 | */ |
|---|
| 157 | void GLGuiTable::removedFocus() |
|---|
| 158 | { |
|---|
| 159 | GLGuiWidget::removedFocus(); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | /** |
|---|
| 164 | * @brief Processes an Event. |
|---|
| 165 | * @param event The event to be processed |
|---|
| 166 | * @return true if the event was catched. |
|---|
| 167 | */ |
|---|
| 168 | bool GLGuiTable::processEvent(const Event& event) |
|---|
| 169 | { |
|---|
| 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 | |
|---|
| 176 | return false; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | /** |
|---|
| 181 | * @brief Resizes the Widget to the new Size-constraints. |
|---|
| 182 | */ |
|---|
| 183 | void GLGuiTable::resize() |
|---|
| 184 | { |
|---|
| 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 | |
|---|
| 196 | GLGuiWidget::resize(); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | void GLGuiTable::updateFrontColor() |
|---|
| 200 | { |
|---|
| 201 | for (unsigned int i = 0; i < this->columnCount(); ++i) |
|---|
| 202 | { |
|---|
| 203 | this->_headers[i].setColor(this->foregroundColor()); |
|---|
| 204 | for (unsigned int j = 0; j < this->rowCount(); ++j) |
|---|
| 205 | this->_entries[j][i].setColor(this->foregroundColor()); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | void GLGuiTable::hiding() |
|---|
| 211 | { |
|---|
| 212 | for (unsigned int i = 0; i < this->columnCount(); ++i) |
|---|
| 213 | { |
|---|
| 214 | this->_headers[i].setVisibility(false); |
|---|
| 215 | for (unsigned int j = 0; j < this->rowCount(); ++j) |
|---|
| 216 | this->_entries[j][i].setVisibility(false); |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | void GLGuiTable::showing() |
|---|
| 221 | { |
|---|
| 222 | for (unsigned int i = 0; i < this->columnCount(); ++i) |
|---|
| 223 | { |
|---|
| 224 | this->_headers[i].setVisibility(true); |
|---|
| 225 | for (unsigned int j = 0; j < this->rowCount(); ++j) |
|---|
| 226 | this->_entries[j][i].setVisibility(true); |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | /** |
|---|
| 232 | * @brief draws the GLGuiTable |
|---|
| 233 | */ |
|---|
| 234 | void GLGuiTable::draw() const |
|---|
| 235 | { |
|---|
| 236 | this->beginDraw(); |
|---|
| 237 | GLGuiWidget::draw(); |
|---|
| 238 | |
|---|
| 239 | float columnPos = this->borderLeft(); |
|---|
| 240 | float rowPos = this->borderTop(); |
|---|
| 241 | |
|---|
| 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 | |
|---|
| 270 | this->endDraw(); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 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.y < (rowPos += _rowHeights[*row])) |
|---|
| 284 | break; |
|---|
| 285 | } |
|---|
| 286 | for (*column = 0; *column < (int)this->columnCount(); (*column)++) |
|---|
| 287 | { |
|---|
| 288 | if (pixelPosition.x > columnPos && pixelPosition.x < (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 | |
|---|
| 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()); |
|---|
| 331 | text->setLineWidth( this->_columnWidths[column] ); |
|---|
| 332 | text->setFont("fonts/final_frontier.ttf", (int)this->textSize()); |
|---|
| 333 | text->setColor(this->foregroundColor()); |
|---|
| 334 | |
|---|
| 335 | text->setColor(this->foregroundColor()); |
|---|
| 336 | if (text->getParent2D() != this) |
|---|
| 337 | text->setParent2D(this); |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 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()) |
|---|
| 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 | } |
|---|
| 369 | return retVal; |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | } |
|---|