Showing the changes to interface/src/Menu.cpp (1 of 7 files). Show all
Unified · Split
| @@ -1,199 +1,17 @@ | ||
| 1 | 1 | |
| 2 | 2 | #include <algorithm> |
| 3 | 3 | #include "InterfaceConfig.h" |
| 4 | +#include "MenuRow.h" | |
| 5 | +#include "MenuColumn.h" | |
| 4 | 6 | #include "Menu.h" |
| 5 | 7 | #include "Util.h" |
| 6 | 8 | |
| 7 | −int menuHeight = 30; | |
| 9 | + | |
| 8 | 10 | int lineHeight = 30; |
| 11 | +int menuHeight = 30; | |
| 9 | 12 | int yOffset = 8; // under windows we have 8 vertical pixels offset. In 2D an object with y=8, the object is displayed at y=0 |
| 10 | 13 | // change the value in the other platforms (if required). |
| 11 | 14 | |
| 12 | −MenuRow::MenuRow() | |
| 13 | −{ | |
| 14 | −} | |
| 15 | −MenuRow::MenuRow(char * columnName, PFNRowCallback callback) | |
| 16 | −{ | |
| 17 | − int length = std::min(MAX_COLUMN_NAME - 5,(int) strlen(columnName)); | |
| 18 | − strncpy(this->rowName, columnName, length); | |
| 19 | − memcpy(this->rowName + length, " \0", 5); | |
| 20 | − this->callback = callback; | |
| 21 | − rowWidth = 0; | |
| 22 | −} | |
| 23 | − | |
| 24 | −MenuRow::~MenuRow() | |
| 25 | −{ | |
| 26 | −} | |
| 27 | − | |
| 28 | −void MenuRow::call() { | |
| 29 | − callback(-2); | |
| 30 | −} | |
| 31 | − | |
| 32 | −char * MenuRow::getName() { | |
| 33 | − int length = (int) strlen(this->rowName) - 4; | |
| 34 | − int currentValue = callback(-1); | |
| 35 | − if (currentValue == 0) { | |
| 36 | − memcpy(this->rowName + length, " OFF\0", 5); | |
| 37 | − } else if (currentValue == 1) { | |
| 38 | − memcpy(this->rowName + length, " ON \0", 5); | |
| 39 | − } else { | |
| 40 | − memcpy(this->rowName + length, " \0", 5); | |
| 41 | − } | |
| 42 | − return this->rowName; | |
| 43 | −} | |
| 44 | − | |
| 45 | −int MenuRow::getWidth(float scale, int mono, int leftPosition){ | |
| 46 | − if (rowWidth == 0) { | |
| 47 | − rowWidth = widthText( scale, mono, this->rowName); | |
| 48 | − } | |
| 49 | − return rowWidth; | |
| 50 | −} | |
| 51 | − | |
| 52 | −int MenuRow::getWidth(){ | |
| 53 | − return rowWidth; | |
| 54 | −} | |
| 55 | − | |
| 56 | − | |
| 57 | −MenuColumn::MenuColumn() | |
| 58 | −{ | |
| 59 | −} | |
| 60 | −MenuColumn::MenuColumn(char * columnName) | |
| 61 | −{ | |
| 62 | − int length = std::min(MAX_COLUMN_NAME - 1,(int) strlen(columnName)); | |
| 63 | − strncpy(this->columnName, columnName, length); | |
| 64 | − this->columnName[length] = '\0'; | |
| 65 | − columnWidth = 0; | |
| 66 | − leftPosition = 0; | |
| 67 | − xLeftMouseOver = 0; | |
| 68 | − xRightMouseOver = 0; | |
| 69 | − yTopMouseOver = 0; | |
| 70 | − yBottomMouseOver = 0; | |
| 71 | −} | |
| 72 | − | |
| 73 | −MenuColumn::~MenuColumn() | |
| 74 | −{ | |
| 75 | −} | |
| 76 | − | |
| 77 | −void MenuColumn::mouseClickRow(int iRowIndex) { | |
| 78 | − rows[iRowIndex].call(); | |
| 79 | −} | |
| 80 | − | |
| 81 | −bool MenuColumn::mouseClick(int x, int y, int xLeft) { | |
| 82 | − int xRight = xLeft + 200; | |
| 83 | − int yTop = menuHeight; | |
| 84 | − int yBottom = menuHeight; | |
| 85 | − int columnWidth; | |
| 86 | − bool bRet = false; | |
| 87 | − for (unsigned int i = 0; i < rows.size(); ++i) { | |
| 88 | − columnWidth = rows[i].getWidth(); | |
| 89 | − yTop = yBottom + lineHeight; | |
| 90 | − if (x > xLeft && x < xRight && y > yBottom && y < yTop) { | |
| 91 | − mouseClickRow(i); | |
| 92 | − bRet = true; | |
| 93 | − break; | |
| 94 | − } | |
| 95 | − yBottom = yTop; | |
| 96 | − } | |
| 97 | − return bRet; | |
| 98 | −} | |
| 99 | − | |
| 100 | −void MenuColumn::setMouseOver(int xLeft, int xRight, int yTop, int yBottom) { | |
| 101 | − xLeftMouseOver = xLeft; | |
| 102 | − xRightMouseOver = xRight; | |
| 103 | − yTopMouseOver = yTop; | |
| 104 | − yBottomMouseOver = yBottom; | |
| 105 | −} | |
| 106 | − | |
| 107 | −bool MenuColumn::mouseOver(int x, int y, int xLeft) { | |
| 108 | − int xRight = xLeft + 100; | |
| 109 | − int yTop = menuHeight; | |
| 110 | − int yBottom = menuHeight; | |
| 111 | − int columnWidth; | |
| 112 | − bool bRet = false; | |
| 113 | − for (unsigned int i = 0; i < rows.size(); ++i) { | |
| 114 | − columnWidth = rows[i].getWidth(); | |
| 115 | − yTop = yBottom + lineHeight ; | |
| 116 | − if (x > xLeft && x < xRight && y > yBottom && y < yTop) { | |
| 117 | − setMouseOver(xLeft, xRight, yBottom, yTop); | |
| 118 | − bRet = true; | |
| 119 | − break; | |
| 120 | − } | |
| 121 | − yBottom = yTop; | |
| 122 | − } | |
| 123 | − if (!bRet) { | |
| 124 | − setMouseOver(0, 0, 0, 0); | |
| 125 | − } | |
| 126 | − return bRet; | |
| 127 | −} | |
| 128 | − | |
| 129 | −char * MenuColumn::getName() { | |
| 130 | − return this->columnName; | |
| 131 | −} | |
| 132 | − | |
| 133 | −int MenuColumn::getWidth(float scale, int mono, int leftPosition){ | |
| 134 | − if (columnWidth == 0) { | |
| 135 | − columnWidth = widthText( scale, mono, this->columnName); | |
| 136 | − this->leftPosition = leftPosition; | |
| 137 | − } | |
| 138 | − return columnWidth; | |
| 139 | −} | |
| 140 | − | |
| 141 | −int MenuColumn::getWidth(){ | |
| 142 | − return columnWidth; | |
| 143 | −} | |
| 144 | − | |
| 145 | −int MenuColumn::getLeftPosition(){ | |
| 146 | − return leftPosition; | |
| 147 | −} | |
| 148 | − | |
| 149 | −int MenuColumn::addRow(char * rowName, PFNRowCallback callback){ | |
| 150 | − MenuRow * pRow; | |
| 151 | − pRow = new MenuRow(rowName, callback); | |
| 152 | − rows.push_back(*pRow); | |
| 153 | − delete pRow; | |
| 154 | − return 0; | |
| 155 | − | |
| 156 | −} | |
| 157 | − | |
| 158 | −void MenuColumn::render() { | |
| 159 | − int iRow = rows.size(); | |
| 160 | − if (iRow > 0) { | |
| 161 | − glColor3f(0.9,0.9,0.9); | |
| 162 | − glBegin(GL_QUADS); { | |
| 163 | − glVertex2f(leftPosition, yOffset + menuHeight); | |
| 164 | − glVertex2f(leftPosition+100, yOffset + menuHeight); | |
| 165 | − glVertex2f(leftPosition+100, yOffset + menuHeight + iRow*lineHeight); | |
| 166 | − glVertex2f(leftPosition , yOffset + menuHeight + iRow* lineHeight); | |
| 167 | − } | |
| 168 | − glEnd(); | |
| 169 | − } | |
| 170 | − float scale = 0.10; | |
| 171 | − int mono = 0; | |
| 172 | − int y = menuHeight + lineHeight / 2 ; | |
| 173 | − char * rowName; | |
| 174 | − int columnWidth; | |
| 175 | − for (unsigned int i = 0; i < rows.size(); ++i) | |
| 176 | − { | |
| 177 | − rowName = rows[i].getName(); | |
| 178 | − columnWidth = rows[i].getWidth(scale, mono, 0); | |
| 179 | − drawtext(leftPosition + SPACE_BEFORE_ROW_NAME, y+5 + yOffset, scale, 0, 1.0, mono, rowName, 0, 0, 0); | |
| 180 | − y += lineHeight; | |
| 181 | − } | |
| 182 | − renderMouseOver(); | |
| 183 | −} | |
| 184 | − | |
| 185 | −void MenuColumn::renderMouseOver() { | |
| 186 | − if (xLeftMouseOver != 0 || yTopMouseOver != 0 || xRightMouseOver != 0 ||& yBottomMouseOver != 0){ | |
| 187 | − glColor4f(0,0,0,0.1); | |
| 188 | − glBegin(GL_QUADS); { | |
| 189 | − glVertex2f(xLeftMouseOver, yOffset + yTopMouseOver); | |
| 190 | − glVertex2f(xRightMouseOver, yOffset + yTopMouseOver); | |
| 191 | − glVertex2f(xRightMouseOver, yOffset + yBottomMouseOver); | |
| 192 | − glVertex2f(xLeftMouseOver , yOffset + yBottomMouseOver); | |
| 193 | − } | |
| 194 | − glEnd(); | |
| 195 | − } | |
| 196 | −} | |
| 197 | 15 | |
| 198 | 16 | Menu::Menu(){ |
| 199 | 17 | iCurrentColumn = -1; |
| @@ -251,7 +69,7 @@ bool Menu::mouseClick(int x, int y) { · 51 unmodified lines | ||
| 251 | 69 | break; |
| 252 | 70 | } |
| 253 | 71 | if (iCurrentColumn == i) { |
| 254 | − bRet = columns[i].mouseClick(x, y, xLeft); | |
| 72 | + bRet = columns[i].mouseClick(x, y, xLeft, menuHeight, lineHeight); | |
| 255 | 73 | if (bRet) { |
| 256 | 74 | iCurrentColumn = -1; |
| 257 | 75 | } |
| @@ -281,7 +99,7 @@ bool Menu::mouseOver(int x, int y) { · 23 unmodified lines | ||
| 281 | 99 | } |
| 282 | 100 | break; |
| 283 | 101 | } else if (iCurrentColumn == i) { |
| 284 | − columns[i].mouseOver(x, y, xLeft); | |
| 102 | + columns[i].mouseOver(x, y, xLeft, menuHeight, lineHeight); | |
| 285 | 103 | } |
| 286 | 104 | xLeft = xRight; |
| 287 | 105 | } |
| @@ -314,7 +132,7 @@ void Menu::render(int screenwidth, int screenheight) { · 26 unmodified lines | ||
| 314 | 132 | drawtext(x,18 + yOffset, scale, 0, 1.0, mono, columnName, 0, 0, 0); |
| 315 | 133 | x += columnWidth + SPACE_BETWEEN_COLUMNS; |
| 316 | 134 | if (iCurrentColumn == i) { |
| 317 | − columns[i].render(); | |
| 135 | + columns[i].render(yOffset, menuHeight, lineHeight); | |
| 318 | 136 | } |
| 319 | 137 | } |
| 320 | 138 | renderMouseOver(); |