libsegl/glmenu/menumenu.cpp

143 lines
3.1 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 {
2008-03-31 00:14:35 +02:00
if((unsigned int)itempos==menuitems.size()-1)
2008-03-23 18:00:55 +01:00
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;
2008-03-31 00:14:35 +02:00
for(itempos = 0; (unsigned int)itempos<menuitems.size()&&!menuitems[itempos]->isSelectable(); itempos++);
if((unsigned int)itempos==menuitems.size()) {
2008-03-23 18:00:55 +01:00
itempos = 0;
dohighlight = false;
}
}
void MenuMenu::render() {
Punkt2D pos = menupos;
2008-03-31 00:14:35 +02:00
pos.y += offset;
if(centerScreenX) {
SDL_Surface *screen = SDL_GetVideoSurface();
pos.x = screen->w/2;
}
2008-03-23 18:00:55 +01:00
2008-03-31 00:14:35 +02:00
int clen=0, vlen=0, height=0, fullvalwidth=0;
2008-03-23 18:00:55 +01:00
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;
}
2008-03-31 00:14:35 +02:00
fullvalwidth = clen + vlen + valuewidth;
2008-03-23 18:00:55 +01:00
2008-03-31 00:14:35 +02:00
if(paintbackground&&true) {
2008-03-23 18:00:55 +01:00
// geht noch nicht ganz
glDisable(GL_TEXTURE_2D);
2008-03-31 00:14:35 +02:00
glEnable(GL_BLEND);
glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
SDL_Rect bgarea = { 0, (int)pos.y, 0, height };
if(vlen>0&&fullvalwidth>=clen) {
bgarea.x = (int)pos.x-fullvalwidth/2;
bgarea.w = fullvalwidth;
} else {
bgarea.x = (int)pos.x-clen/2;
bgarea.w = clen;
}
std::cout << "clen: " << clen << " fvw: " << fullvalwidth << std::endl;
// SDL_Rect bgarea = { pos.x-(clen/2)-((vlen>0)?(valuewidth+vlen):0),
// pos.y,
// clen+((vlen>0)?vlen+valuewidth:0),
// height };
// std::cout << bgarea.x << " " << bgarea.y << " " << bgarea.w << " " << bgarea.h << std::endl;
bgarea.x -= offset;
bgarea.y -= offset;
bgarea.w += offset*2;
bgarea.h += offset;
2008-03-23 18:00:55 +01:00
GLFontEngine::paintSDLRect(bgarea);
}
for(unsigned int i=0; i<menuitems.size(); i++) {
2008-03-31 00:14:35 +02:00
menuitems[i]->render(pos, centermenu, basefontsize, maxwidth, valuewidth, ((unsigned int)itempos==i)&&dohighlight, clen, vlen);
pos.y += basefontsize + menuitems[i]->getFontSizeAdd() + offset;
}
}