From eb646ade8450e2db0be4fcaaaa01defd207a4237 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 10 Apr 2013 22:39:01 +0200 Subject: [PATCH] #19171 split of menu.cpp in menuColumn.cpp and menuRow.cpp --- interface/src/Menu.cpp | 196 ++--------------------------------- interface/src/Menu.h | 42 -------- interface/src/MenuColumn.cpp | 150 +++++++++++++++++++++++++++ interface/src/MenuColumn.h | 28 +++++ interface/src/MenuRow.cpp | 53 ++++++++++ interface/src/MenuRow.h | 23 ++++ interface/src/main.cpp | 2 + 7 files changed, 263 insertions(+), 231 deletions(-) create mode 100644 interface/src/MenuColumn.cpp create mode 100644 interface/src/MenuColumn.h create mode 100644 interface/src/MenuRow.cpp create mode 100644 interface/src/MenuRow.h diff --git a/interface/src/Menu.cpp b/interface/src/Menu.cpp index 160b59fae7..730b2e27c1 100644 --- a/interface/src/Menu.cpp +++ b/interface/src/Menu.cpp @@ -1,199 +1,17 @@ #include #include "InterfaceConfig.h" +#include "MenuRow.h" +#include "MenuColumn.h" #include "Menu.h" #include "Util.h" -int menuHeight = 30; + int lineHeight = 30; +int menuHeight = 30; 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 // change the value in the other platforms (if required). -MenuRow::MenuRow() -{ -} -MenuRow::MenuRow(char * columnName, PFNRowCallback callback) -{ - int length = std::min(MAX_COLUMN_NAME - 5,(int) strlen(columnName)); - strncpy(this->rowName, columnName, length); - memcpy(this->rowName + length, " \0", 5); - this->callback = callback; - rowWidth = 0; -} - -MenuRow::~MenuRow() -{ -} - -void MenuRow::call() { - callback(-2); -} - -char * MenuRow::getName() { - int length = (int) strlen(this->rowName) - 4; - int currentValue = callback(-1); - if (currentValue == 0) { - memcpy(this->rowName + length, " OFF\0", 5); - } else if (currentValue == 1) { - memcpy(this->rowName + length, " ON \0", 5); - } else { - memcpy(this->rowName + length, " \0", 5); - } - return this->rowName; -} - -int MenuRow::getWidth(float scale, int mono, int leftPosition){ - if (rowWidth == 0) { - rowWidth = widthText( scale, mono, this->rowName); - } - return rowWidth; -} - -int MenuRow::getWidth(){ - return rowWidth; -} - - -MenuColumn::MenuColumn() -{ -} -MenuColumn::MenuColumn(char * columnName) -{ - int length = std::min(MAX_COLUMN_NAME - 1,(int) strlen(columnName)); - strncpy(this->columnName, columnName, length); - this->columnName[length] = '\0'; - columnWidth = 0; - leftPosition = 0; - xLeftMouseOver = 0; - xRightMouseOver = 0; - yTopMouseOver = 0; - yBottomMouseOver = 0; -} - -MenuColumn::~MenuColumn() -{ -} - -void MenuColumn::mouseClickRow(int iRowIndex) { - rows[iRowIndex].call(); -} - -bool MenuColumn::mouseClick(int x, int y, int xLeft) { - int xRight = xLeft + 200; - int yTop = menuHeight; - int yBottom = menuHeight; - int columnWidth; - bool bRet = false; - for (unsigned int i = 0; i < rows.size(); ++i) { - columnWidth = rows[i].getWidth(); - yTop = yBottom + lineHeight; - if (x > xLeft && x < xRight && y > yBottom && y < yTop) { - mouseClickRow(i); - bRet = true; - break; - } - yBottom = yTop; - } - return bRet; -} - -void MenuColumn::setMouseOver(int xLeft, int xRight, int yTop, int yBottom) { - xLeftMouseOver = xLeft; - xRightMouseOver = xRight; - yTopMouseOver = yTop; - yBottomMouseOver = yBottom; -} - -bool MenuColumn::mouseOver(int x, int y, int xLeft) { - int xRight = xLeft + 100; - int yTop = menuHeight; - int yBottom = menuHeight; - int columnWidth; - bool bRet = false; - for (unsigned int i = 0; i < rows.size(); ++i) { - columnWidth = rows[i].getWidth(); - yTop = yBottom + lineHeight ; - if (x > xLeft && x < xRight && y > yBottom && y < yTop) { - setMouseOver(xLeft, xRight, yBottom, yTop); - bRet = true; - break; - } - yBottom = yTop; - } - if (!bRet) { - setMouseOver(0, 0, 0, 0); - } - return bRet; -} - -char * MenuColumn::getName() { - return this->columnName; -} - -int MenuColumn::getWidth(float scale, int mono, int leftPosition){ - if (columnWidth == 0) { - columnWidth = widthText( scale, mono, this->columnName); - this->leftPosition = leftPosition; - } - return columnWidth; -} - -int MenuColumn::getWidth(){ - return columnWidth; -} - -int MenuColumn::getLeftPosition(){ - return leftPosition; -} - -int MenuColumn::addRow(char * rowName, PFNRowCallback callback){ - MenuRow * pRow; - pRow = new MenuRow(rowName, callback); - rows.push_back(*pRow); - delete pRow; - return 0; - -} - -void MenuColumn::render() { - int iRow = rows.size(); - if (iRow > 0) { - glColor3f(0.9,0.9,0.9); - glBegin(GL_QUADS); { - glVertex2f(leftPosition, yOffset + menuHeight); - glVertex2f(leftPosition+100, yOffset + menuHeight); - glVertex2f(leftPosition+100, yOffset + menuHeight + iRow*lineHeight); - glVertex2f(leftPosition , yOffset + menuHeight + iRow* lineHeight); - } - glEnd(); - } - float scale = 0.10; - int mono = 0; - int y = menuHeight + lineHeight / 2 ; - char * rowName; - int columnWidth; - for (unsigned int i = 0; i < rows.size(); ++i) - { - rowName = rows[i].getName(); - columnWidth = rows[i].getWidth(scale, mono, 0); - drawtext(leftPosition + SPACE_BEFORE_ROW_NAME, y+5 + yOffset, scale, 0, 1.0, mono, rowName, 0, 0, 0); - y += lineHeight; - } - renderMouseOver(); -} - -void MenuColumn::renderMouseOver() { - if (xLeftMouseOver != 0 || yTopMouseOver != 0 || xRightMouseOver != 0 ||& yBottomMouseOver != 0){ - glColor4f(0,0,0,0.1); - glBegin(GL_QUADS); { - glVertex2f(xLeftMouseOver, yOffset + yTopMouseOver); - glVertex2f(xRightMouseOver, yOffset + yTopMouseOver); - glVertex2f(xRightMouseOver, yOffset + yBottomMouseOver); - glVertex2f(xLeftMouseOver , yOffset + yBottomMouseOver); - } - glEnd(); - } -} Menu::Menu(){ iCurrentColumn = -1; @@ -251,7 +69,7 @@ bool Menu::mouseClick(int x, int y) { break; } if (iCurrentColumn == i) { - bRet = columns[i].mouseClick(x, y, xLeft); + bRet = columns[i].mouseClick(x, y, xLeft, menuHeight, lineHeight); if (bRet) { iCurrentColumn = -1; } @@ -281,7 +99,7 @@ bool Menu::mouseOver(int x, int y) { } break; } else if (iCurrentColumn == i) { - columns[i].mouseOver(x, y, xLeft); + columns[i].mouseOver(x, y, xLeft, menuHeight, lineHeight); } xLeft = xRight; } @@ -314,7 +132,7 @@ void Menu::render(int screenwidth, int screenheight) { drawtext(x,18 + yOffset, scale, 0, 1.0, mono, columnName, 0, 0, 0); x += columnWidth + SPACE_BETWEEN_COLUMNS; if (iCurrentColumn == i) { - columns[i].render(); + columns[i].render(yOffset, menuHeight, lineHeight); } } renderMouseOver(); diff --git a/interface/src/Menu.h b/interface/src/Menu.h index 7b8eb9bad6..c5b6b995f3 100644 --- a/interface/src/Menu.h +++ b/interface/src/Menu.h @@ -7,48 +7,6 @@ typedef int(CALLBACK * PFNRowCallback)(int); -class MenuRow { - public: - MenuRow(); - MenuRow(char * rowName, PFNRowCallback); - ~MenuRow(); - void call(); - char * getName(); - int getWidth(float scale, int mono, int leftPosition); - int getWidth(); -private: - char rowName[MAX_COLUMN_NAME]; - int rowWidth; - PFNRowCallback callback; -}; - - -class MenuColumn { - public: - MenuColumn(); - MenuColumn(char * columnName); - ~MenuColumn(); - void mouseClickRow(int iColumnIndex); - bool mouseClick(int x, int y, int xLeft); - void setMouseOver(int xLeft, int xRight, int yTop, int yBottom); - bool mouseOver(int x, int y, int xLeft); - char * getName(); - int getWidth(float scale, int mono, int leftPosition); - int getWidth(); - int getLeftPosition(); - void render(); - void MenuColumn::renderMouseOver(); - int addRow(char * rowName, PFNRowCallback callback); -private: - char columnName[MAX_COLUMN_NAME]; - int columnWidth; - int leftPosition; - std::vector rows; - int xLeftMouseOver; - int xRightMouseOver; - int yTopMouseOver; - int yBottomMouseOver; -}; class Menu { public: diff --git a/interface/src/MenuColumn.cpp b/interface/src/MenuColumn.cpp new file mode 100644 index 0000000000..bd60021a21 --- /dev/null +++ b/interface/src/MenuColumn.cpp @@ -0,0 +1,150 @@ + +#include +#include "InterfaceConfig.h" +#include "MenuRow.h" +#include "MenuColumn.h" +#include "Menu.h" +#include "Util.h" + + + +MenuColumn::MenuColumn() +{ +} +MenuColumn::MenuColumn(char * columnName) +{ + int length = std::min(MAX_COLUMN_NAME - 1,(int) strlen(columnName)); + strncpy(this->columnName, columnName, length); + this->columnName[length] = '\0'; + columnWidth = 0; + leftPosition = 0; + xLeftMouseOver = 0; + xRightMouseOver = 0; + yTopMouseOver = 0; + yBottomMouseOver = 0; +} + +MenuColumn::~MenuColumn() +{ +} + +void MenuColumn::mouseClickRow(int iRowIndex) { + rows[iRowIndex].call(); +} + +bool MenuColumn::mouseClick(int x, int y, int xLeft, int menuHeight, int lineHeight) { + int xRight = xLeft + 200; + int yTop = menuHeight; + int yBottom = menuHeight; + int columnWidth; + bool bRet = false; + for (unsigned int i = 0; i < rows.size(); ++i) { + columnWidth = rows[i].getWidth(); + yTop = yBottom + lineHeight; + if (x > xLeft && x < xRight && y > yBottom && y < yTop) { + mouseClickRow(i); + bRet = true; + break; + } + yBottom = yTop; + } + return bRet; +} + +void MenuColumn::setMouseOver(int xLeft, int xRight, int yTop, int yBottom) { + xLeftMouseOver = xLeft; + xRightMouseOver = xRight; + yTopMouseOver = yTop; + yBottomMouseOver = yBottom; +} + +bool MenuColumn::mouseOver(int x, int y, int xLeft, int menuHeight, int lineHeight) { + int xRight = xLeft + 100; + int yTop = menuHeight; + int yBottom = menuHeight; + int columnWidth; + bool bRet = false; + for (unsigned int i = 0; i < rows.size(); ++i) { + columnWidth = rows[i].getWidth(); + yTop = yBottom + lineHeight ; + if (x > xLeft && x < xRight && y > yBottom && y < yTop) { + setMouseOver(xLeft, xRight, yBottom, yTop); + bRet = true; + break; + } + yBottom = yTop; + } + if (!bRet) { + setMouseOver(0, 0, 0, 0); + } + return bRet; +} + +char * MenuColumn::getName() { + return this->columnName; +} + +int MenuColumn::getWidth(float scale, int mono, int leftPosition){ + if (columnWidth == 0) { + columnWidth = widthText( scale, mono, this->columnName); + this->leftPosition = leftPosition; + } + return columnWidth; +} + +int MenuColumn::getWidth(){ + return columnWidth; +} + +int MenuColumn::getLeftPosition(){ + return leftPosition; +} + +int MenuColumn::addRow(char * rowName, PFNRowCallback callback){ + MenuRow * pRow; + pRow = new MenuRow(rowName, callback); + rows.push_back(*pRow); + delete pRow; + return 0; + +} + +void MenuColumn::render(int yOffset, int menuHeight, int lineHeight) { + int iRow = rows.size(); + if (iRow > 0) { + glColor3f(0.9,0.9,0.9); + glBegin(GL_QUADS); { + glVertex2f(leftPosition, yOffset + menuHeight); + glVertex2f(leftPosition+100, yOffset + menuHeight); + glVertex2f(leftPosition+100, yOffset + menuHeight + iRow*lineHeight); + glVertex2f(leftPosition , yOffset + menuHeight + iRow* lineHeight); + } + glEnd(); + } + float scale = 0.10; + int mono = 0; + int y = menuHeight + lineHeight / 2 ; + char * rowName; + int columnWidth; + for (unsigned int i = 0; i < rows.size(); ++i) + { + rowName = rows[i].getName(); + columnWidth = rows[i].getWidth(scale, mono, 0); + drawtext(leftPosition + SPACE_BEFORE_ROW_NAME, y+5 + yOffset, scale, 0, 1.0, mono, rowName, 0, 0, 0); + y += lineHeight; + } + renderMouseOver(yOffset); +} + +void MenuColumn::renderMouseOver(int yOffset) { + if (xLeftMouseOver != 0 || yTopMouseOver != 0 || xRightMouseOver != 0 ||& yBottomMouseOver != 0){ + glColor4f(0,0,0,0.1); + glBegin(GL_QUADS); { + glVertex2f(xLeftMouseOver, yOffset + yTopMouseOver); + glVertex2f(xRightMouseOver, yOffset + yTopMouseOver); + glVertex2f(xRightMouseOver, yOffset + yBottomMouseOver); + glVertex2f(xLeftMouseOver , yOffset + yBottomMouseOver); + } + glEnd(); + } +} diff --git a/interface/src/MenuColumn.h b/interface/src/MenuColumn.h new file mode 100644 index 0000000000..be4033326b --- /dev/null +++ b/interface/src/MenuColumn.h @@ -0,0 +1,28 @@ + +class MenuColumn { + public: + MenuColumn(); + MenuColumn(char * columnName); + ~MenuColumn(); + void mouseClickRow(int iColumnIndex); + bool mouseClick(int x, int y, int xLeft, int menuHeight, int lineHeight); + void setMouseOver(int xLeft, int xRight, int yTop, int yBottom); + bool mouseOver(int x, int y, int xLeft, int menuHeight, int lineHeight); + char * getName(); + int getWidth(float scale, int mono, int leftPosition); + int getWidth(); + int getLeftPosition(); + void render(int yOffset, int menuHeight, int lineHeight); + void MenuColumn::renderMouseOver(int yOffset); + int addRow(char * rowName, PFNRowCallback callback); +private: + char columnName[MAX_COLUMN_NAME]; + int columnWidth; + int leftPosition; + std::vector rows; + int xLeftMouseOver; + int xRightMouseOver; + int yTopMouseOver; + int yBottomMouseOver; +}; + diff --git a/interface/src/MenuRow.cpp b/interface/src/MenuRow.cpp new file mode 100644 index 0000000000..e8efd29008 --- /dev/null +++ b/interface/src/MenuRow.cpp @@ -0,0 +1,53 @@ + +#include +#include "InterfaceConfig.h" +#include "MenuRow.h" +#include "MenuColumn.h" +#include "Menu.h" +#include "Util.h" + + +MenuRow::MenuRow() +{ +} +MenuRow::MenuRow(char * columnName, PFNRowCallback callback) +{ + int length = std::min(MAX_COLUMN_NAME - 5,(int) strlen(columnName)); + strncpy(this->rowName, columnName, length); + memcpy(this->rowName + length, " \0", 5); + this->callback = callback; + rowWidth = 0; +} + +MenuRow::~MenuRow() +{ +} + +void MenuRow::call() { + callback(-2); +} + +char * MenuRow::getName() { + int length = (int) strlen(this->rowName) - 4; + int currentValue = callback(-1); + if (currentValue == 0) { + memcpy(this->rowName + length, " OFF\0", 5); + } else if (currentValue == 1) { + memcpy(this->rowName + length, " ON \0", 5); + } else { + memcpy(this->rowName + length, " \0", 5); + } + return this->rowName; +} + +int MenuRow::getWidth(float scale, int mono, int leftPosition){ + if (rowWidth == 0) { + rowWidth = widthText( scale, mono, this->rowName); + } + return rowWidth; +} + +int MenuRow::getWidth(){ + return rowWidth; +} + diff --git a/interface/src/MenuRow.h b/interface/src/MenuRow.h new file mode 100644 index 0000000000..57deeb300d --- /dev/null +++ b/interface/src/MenuRow.h @@ -0,0 +1,23 @@ + +#include + +#define MAX_COLUMN_NAME 50 +#define SPACE_BETWEEN_COLUMNS 20 +#define SPACE_BEFORE_ROW_NAME 10 +typedef int(CALLBACK * PFNRowCallback)(int); + +class MenuRow { + public: + MenuRow(); + MenuRow(char * rowName, PFNRowCallback); + ~MenuRow(); + void call(); + char * getName(); + int getWidth(float scale, int mono, int leftPosition); + int getWidth(); +private: + char rowName[MAX_COLUMN_NAME]; + int rowWidth; + PFNRowCallback callback; +}; + diff --git a/interface/src/main.cpp b/interface/src/main.cpp index b1e69d749c..d3697ce930 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -57,6 +57,8 @@ #include "FieldOfView.h" #include "Stars.h" +#include "MenuRow.h" +#include "MenuColumn.h" #include "Menu.h" #include "Head.h" #include "Hand.h"