[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 | |
---|
[8633] | 21 | #include "glgui_handler.h" |
---|
| 22 | |
---|
[7779] | 23 | namespace OrxGui |
---|
[3365] | 24 | { |
---|
[7779] | 25 | /** |
---|
[8035] | 26 | * @brief standard constructor |
---|
[8448] | 27 | */ |
---|
[8616] | 28 | GLGuiTable::GLGuiTable (unsigned int rows, unsigned int columns) |
---|
[7779] | 29 | { |
---|
| 30 | this->init(); |
---|
[8629] | 31 | |
---|
[8616] | 32 | this->setRowCount(rows); |
---|
| 33 | this->setColumnCount(columns); |
---|
[7779] | 34 | } |
---|
[1853] | 35 | |
---|
| 36 | |
---|
[7779] | 37 | /** |
---|
[8035] | 38 | * @brief standard deconstructor |
---|
| 39 | */ |
---|
[8614] | 40 | GLGuiTable::~GLGuiTable() |
---|
[7894] | 41 | {} |
---|
[5360] | 42 | |
---|
[7779] | 43 | /** |
---|
[8035] | 44 | * @brief initializes the GUI-element |
---|
[7779] | 45 | */ |
---|
[8614] | 46 | void GLGuiTable::init() |
---|
[7779] | 47 | { |
---|
[8614] | 48 | this->setClassID(CL_GLGUI_TABLE, "GLGuiTable"); |
---|
[7893] | 49 | |
---|
| 50 | this->setFocusable(true); |
---|
[8614] | 51 | this->setClickable(true); |
---|
[7779] | 52 | } |
---|
[5360] | 53 | |
---|
[8035] | 54 | |
---|
[8614] | 55 | void GLGuiTable::setRowCount(unsigned int rowCount) |
---|
[7892] | 56 | { |
---|
| 57 | |
---|
[8616] | 58 | unsigned int currentRowCount = this->rowCount(); |
---|
[8629] | 59 | |
---|
| 60 | this->_rowHeights.resize(rowCount, 20); |
---|
| 61 | |
---|
[8616] | 62 | this->_entries.resize(rowCount); |
---|
| 63 | for (unsigned int i = currentRowCount; i < this->rowCount(); ++i) |
---|
[8618] | 64 | { |
---|
[8616] | 65 | this->_entries[i].resize(columnCount(), LimitedWidthText("fonts/final_frontier.ttf", 20)); |
---|
[8618] | 66 | for (unsigned int j = 0; j < columnCount(); ++j) |
---|
| 67 | this->applyTextSettings(i, j, &this->_entries[i][j]); |
---|
| 68 | } |
---|
[8616] | 69 | |
---|
| 70 | assert(this->checkIntegrity()); |
---|
[8626] | 71 | this->repositionText(currentRowCount, 0); |
---|
[8616] | 72 | |
---|
[8629] | 73 | |
---|
| 74 | this->resize(); |
---|
| 75 | |
---|
[8614] | 76 | if (!this->isVisible()) |
---|
| 77 | this->hiding(); |
---|
[7893] | 78 | } |
---|
[7892] | 79 | |
---|
[8614] | 80 | void GLGuiTable::setColumnCount(unsigned int columnCount) |
---|
[7893] | 81 | { |
---|
[8618] | 82 | unsigned int i; |
---|
| 83 | unsigned int currentColumnCount = this->columnCount(); |
---|
| 84 | |
---|
[8626] | 85 | this->_columnWidths.resize(columnCount, 100); |
---|
| 86 | |
---|
[8618] | 87 | // setup Headers. |
---|
[8616] | 88 | this->_headers.resize(columnCount, LimitedWidthText("fonts/final_frontier.ttf", 20)); |
---|
[8618] | 89 | for (i = currentColumnCount; i < columnCount; ++i) |
---|
| 90 | this->applyTextSettings(0, i, &this->_headers[i]); |
---|
| 91 | |
---|
| 92 | for (i = 0; i < rowCount(); i++) |
---|
| 93 | { |
---|
[8616] | 94 | this->_entries[i].resize(columnCount, LimitedWidthText("fonts/final_frontier.ttf", 20)); |
---|
[8618] | 95 | for (unsigned int j = currentColumnCount; j < columnCount; ++j) |
---|
| 96 | this->applyTextSettings(i, j, &this->_entries[i][j]); |
---|
[8616] | 97 | |
---|
[8618] | 98 | } |
---|
[8616] | 99 | assert(this->checkIntegrity()); |
---|
| 100 | |
---|
[8626] | 101 | this->repositionText(0, currentColumnCount); |
---|
| 102 | |
---|
[8629] | 103 | this->resize(); |
---|
[8614] | 104 | if (!this->isVisible()) |
---|
| 105 | this->hiding(); |
---|
[7892] | 106 | } |
---|
| 107 | |
---|
[7893] | 108 | |
---|
[8618] | 109 | |
---|
| 110 | |
---|
[8615] | 111 | void GLGuiTable::setHeader(unsigned int number, const std::string& headerName) |
---|
| 112 | { |
---|
[8616] | 113 | if (number >= this->columnCount()) |
---|
[8615] | 114 | this->setColumnCount(number + 1); |
---|
| 115 | this->_headers[number].setText(headerName); |
---|
| 116 | } |
---|
[8448] | 117 | |
---|
[8615] | 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 | } |
---|
[8518] | 123 | |
---|
[8626] | 124 | void GLGuiTable::setEntry(unsigned int row, unsigned int column, const std::string& name) |
---|
[8615] | 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 | |
---|
[8626] | 140 | |
---|
| 141 | |
---|
[8616] | 142 | /// TODO. |
---|
[8615] | 143 | void GLGuiTable::setRowHeight(unsigned int row, unsigned int size) |
---|
[8629] | 144 | {} |
---|
[8615] | 145 | |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | |
---|
[8448] | 149 | /** |
---|
[8035] | 150 | * removes the focus from this Widget. |
---|
| 151 | */ |
---|
[8614] | 152 | void GLGuiTable::removedFocus() |
---|
[7896] | 153 | { |
---|
| 154 | GLGuiWidget::removedFocus(); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | |
---|
[8035] | 158 | /** |
---|
[8448] | 159 | * @brief Processes an Event. |
---|
[8035] | 160 | * @param event The event to be processed |
---|
| 161 | * @return true if the event was catched. |
---|
| 162 | */ |
---|
[8614] | 163 | bool GLGuiTable::processEvent(const Event& event) |
---|
[7893] | 164 | { |
---|
[8633] | 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 | |
---|
[7893] | 172 | return false; |
---|
| 173 | } |
---|
[7892] | 174 | |
---|
[7893] | 175 | |
---|
[8035] | 176 | /** |
---|
| 177 | * @brief Resizes the Widget to the new Size-constraints. |
---|
| 178 | */ |
---|
[8614] | 179 | void GLGuiTable::resize() |
---|
[7894] | 180 | { |
---|
[8629] | 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 | |
---|
[8035] | 192 | GLGuiWidget::resize(); |
---|
[7894] | 193 | } |
---|
| 194 | |
---|
[8614] | 195 | void GLGuiTable::updateFrontColor() |
---|
[8448] | 196 | { |
---|
[8616] | 197 | for (unsigned int i = 0; i < this->columnCount(); ++i) |
---|
[8614] | 198 | { |
---|
| 199 | this->_headers[i].setColor(this->foregroundColor()); |
---|
[8616] | 200 | for (unsigned int j = 0; j < this->rowCount(); ++j) |
---|
| 201 | this->_entries[j][i].setColor(this->foregroundColor()); |
---|
[8614] | 202 | } |
---|
[8629] | 203 | |
---|
[8448] | 204 | } |
---|
[7894] | 205 | |
---|
[8614] | 206 | void GLGuiTable::hiding() |
---|
[8116] | 207 | { |
---|
[8616] | 208 | for (unsigned int i = 0; i < this->columnCount(); ++i) |
---|
[8614] | 209 | { |
---|
| 210 | this->_headers[i].setVisibility(false); |
---|
[8616] | 211 | for (unsigned int j = 0; j < this->rowCount(); ++j) |
---|
| 212 | this->_entries[j][i].setVisibility(false); |
---|
[8614] | 213 | } |
---|
[8116] | 214 | } |
---|
| 215 | |
---|
[8614] | 216 | void GLGuiTable::showing() |
---|
[8116] | 217 | { |
---|
[8616] | 218 | for (unsigned int i = 0; i < this->columnCount(); ++i) |
---|
[8614] | 219 | { |
---|
| 220 | this->_headers[i].setVisibility(true); |
---|
[8616] | 221 | for (unsigned int j = 0; j < this->rowCount(); ++j) |
---|
| 222 | this->_entries[j][i].setVisibility(true); |
---|
[8614] | 223 | } |
---|
[8116] | 224 | } |
---|
| 225 | |
---|
[8616] | 226 | |
---|
[8035] | 227 | /** |
---|
[8614] | 228 | * @brief draws the GLGuiTable |
---|
[7779] | 229 | */ |
---|
[8614] | 230 | void GLGuiTable::draw() const |
---|
[7779] | 231 | { |
---|
[8035] | 232 | this->beginDraw(); |
---|
[7892] | 233 | GLGuiWidget::draw(); |
---|
| 234 | |
---|
[8629] | 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(); |
---|
[7892] | 263 | |
---|
| 264 | this->endDraw(); |
---|
[7779] | 265 | } |
---|
[8616] | 266 | |
---|
[8618] | 267 | |
---|
[8633] | 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 | |
---|
[8647] | 275 | for (*row = 0; *row < this->rowCount(); (*row)++) |
---|
[8633] | 276 | { |
---|
[8647] | 277 | if (pixelPosition.y > rowPos && pixelPosition.y < (rowPos += _rowHeights[*row])) |
---|
[8633] | 278 | break; |
---|
| 279 | } |
---|
[8647] | 280 | for (*column = 0; *column < this->columnCount(); (*column)++) |
---|
[8633] | 281 | { |
---|
[8647] | 282 | if (pixelPosition.x > columnPos && pixelPosition.x < (columnPos += _columnWidths[*column])) |
---|
[8633] | 283 | break; |
---|
| 284 | } |
---|
| 285 | |
---|
[8647] | 286 | PRINTF(3)("Mouse Over row:%d Column:%d\n", *row, *column); |
---|
[8633] | 287 | } |
---|
| 288 | |
---|
| 289 | |
---|
[8626] | 290 | void GLGuiTable::repositionText(unsigned int fromLeft, unsigned int fromTop) |
---|
| 291 | { |
---|
| 292 | float columnPos = 0.0f; |
---|
[8629] | 293 | float rowPos = 0.0f; |
---|
[8626] | 294 | unsigned int i, j; |
---|
| 295 | for (i = 0; i< fromLeft; ++i) |
---|
| 296 | columnPos+=this->_columnWidths[i]; |
---|
[8629] | 297 | for (i = 0; i < fromTop; ++i) |
---|
| 298 | rowPos += this->_rowHeights[i]; |
---|
[8626] | 299 | |
---|
| 300 | for (i = fromLeft; i < this->columnCount(); ++i) |
---|
| 301 | { |
---|
| 302 | this->_headers[i].setRelCoor2D(columnPos, 0); |
---|
| 303 | |
---|
[8627] | 304 | // not required, but a good idea :) |
---|
| 305 | this->_headers[i].setLineWidth(_columnWidths[i]); |
---|
| 306 | |
---|
[8629] | 307 | float rowPosI = rowPos; |
---|
[8626] | 308 | for (j = fromTop; j < this->rowCount(); ++j) |
---|
| 309 | { |
---|
[8629] | 310 | this->_entries[j][i].setRelCoor2D(columnPos, rowPosI); |
---|
[8627] | 311 | this->_entries[j][i].setLineWidth(_columnWidths[i]); |
---|
[8629] | 312 | rowPosI += _rowHeights[j]; |
---|
[8626] | 313 | } |
---|
| 314 | columnPos+=this->_columnWidths[i]; |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | |
---|
[8618] | 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()); |
---|
[8626] | 325 | text->setLineWidth( this->_columnWidths[column] ); |
---|
[8618] | 326 | text->setFont("fonts/final_frontier.ttf", (int)this->textSize()); |
---|
[8631] | 327 | text->setColor(this->foregroundColor()); |
---|
[8618] | 328 | |
---|
| 329 | text->setColor(this->foregroundColor()); |
---|
| 330 | if (text->getParent2D() != this) |
---|
| 331 | text->setParent2D(this); |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | |
---|
[8616] | 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()) |
---|
[8626] | 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 | } |
---|
[8616] | 363 | return retVal; |
---|
| 364 | } |
---|
| 365 | |
---|
[5360] | 366 | } |
---|