libsegl/glmenu/menumenu.cpp

123 lines
2.7 KiB
C++
Raw Normal View History

#include "menumenu.h"
MenuMenu::MenuMenu() {
itempos = 0;
centermenu = false;
centerScreenX = false;
2008-03-23 18:00:55 +01:00
dohighlight = true;
paintbackground = true;
basefontsize = 20;
maxwidth = 0;
valuewidth = 100;
offset = 20;
}
void MenuMenu::setPos(const Punkt2D &p) {
menupos = p;
}
void MenuMenu::setCenter(bool _c) {
centermenu = _c;
}
void MenuMenu::setCenterScreenX(bool _c) {
centerScreenX = _c;
}
void MenuMenu::setBaseFontSize(int _bfs) {
if(_bfs>0)
basefontsize = _bfs;
}
void MenuMenu::setMaxWidth(int _mw) {
if(_mw>0)
maxwidth = _mw;
}
2008-03-23 18:00:55 +01:00
void MenuMenu::setPaintBackground(bool _pbg) {
paintbackground = _pbg;
}
void MenuMenu::addMenuItem(MenuItem* mi) {
menuitems.push_back(mi);
}
void MenuMenu::up() {
2008-03-23 18:00:55 +01:00
do {
if(itempos==0)
itempos = menuitems.size()-1;
else
itempos--;
} while(!menuitems[itempos]->isSelectable());
}
void MenuMenu::down() {
2008-03-23 18:00:55 +01:00
do {
if(itempos==menuitems.size()-1)
itempos = 0;
else
itempos++;
} while(!menuitems[itempos]->isSelectable());
}
void MenuMenu::left() {
menuitems[itempos]->left();
}
void MenuMenu::right() {
menuitems[itempos]->right();
}
void MenuMenu::select() {
menuitems[itempos]->select();
}
2008-03-23 18:00:55 +01:00
void MenuMenu::charInput(char c) {
menuitems[itempos]->charInput(c);
}
void MenuMenu::resetItemPos() {
dohighlight = true;
for(itempos = 0; itempos<menuitems.size()&&!menuitems[itempos]->isSelectable(); itempos++);
if(itempos==menuitems.size()) {
itempos = 0;
dohighlight = false;
}
}
void MenuMenu::render() {
Punkt2D pos = menupos;
if(centerScreenX) {
SDL_Surface *screen = SDL_GetVideoSurface();
pos.x = screen->w/2;
}
2008-03-23 18:00:55 +01:00
int clen=0, vlen=0, height=0;
for(unsigned int i=0; i<menuitems.size(); i++) {
fontengine.setSize(basefontsize+menuitems[i]->getFontSizeAdd());
clen = std::max(clen, fontengine.getTextWidth(menuitems[i]->getCaption()));
vlen = std::max(vlen, fontengine.getTextWidth(menuitems[i]->getValue()));
height += basefontsize + menuitems[i]->getFontSizeAdd() + offset;
}
std::cout << "clen: " << clen << " und " << (int) paintbackground << std::endl;
if(paintbackground&&false) {
// geht noch nicht ganz
glDisable(GL_TEXTURE_2D);
glColor3f(0.4f, 0.4f, 0.4f);
SDL_Rect bgarea = { pos.x-(clen/2)-((vlen>0)?(valuewidth/2+vlen/2):0),
pos.y,
clen+((vlen>0)?vlen/2+valuewidth/2:0),
height };
std::cout << bgarea.x << " " << bgarea.y << " " << bgarea.w << " " << bgarea.h << std::endl;
GLFontEngine::paintSDLRect(bgarea);
}
for(unsigned int i=0; i<menuitems.size(); i++) {
2008-03-23 18:00:55 +01:00
menuitems[i]->render(pos, centermenu, basefontsize, maxwidth, valuewidth, (itempos==i)&&dohighlight, clen, vlen);
pos.y += basefontsize + menuitems[i]->getFontSizeAdd() + offset;
}
}