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