libsegl/glmenu/menumenu.cpp

172 lines
4.3 KiB
C++

#include "menumenu.h"
namespace segl {
MenuMenu::MenuMenu() {
itempos = 0;
centermenu = false;
centerScreenX = false;
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;
}
void MenuMenu::setPaintBackground(bool _pbg) {
paintbackground = _pbg;
}
void MenuMenu::addMenuItem(MenuItem* mi) {
menuitems.push_back(mi);
}
void MenuMenu::up() {
do {
if(itempos==0)
itempos = menuitems.size()-1;
else
itempos--;
} while(!menuitems[itempos]->isSelectable());
}
void MenuMenu::down() {
do {
if((unsigned int)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();
}
void MenuMenu::charInput(char c) {
menuitems[itempos]->charInput(c);
}
void MenuMenu::resetItemPos() {
dohighlight = true;
for(itempos = 0; (unsigned int)itempos<menuitems.size()&&!menuitems[itempos]->isSelectable(); itempos++);
if((unsigned int)itempos==menuitems.size()) {
itempos = 0;
dohighlight = false;
}
}
void MenuMenu::render() {
Punkt2D pos = menupos;
pos.y += offset;
if(centerScreenX) {
SDL_Surface *screen = SDL_GetVideoSurface();
pos.x = screen->w/2;
}
int clen=0, vlen=0, cvlen=0, height=0, fullvalwidth=0, maxlen=0, extravalwidth=0;
for(unsigned int i=0; i<menuitems.size(); i++) {
fontengine.setSize(basefontsize+menuitems[i]->getFontSizeAdd());
if(menuitems[i]->hasValue()) {
// cvlen = std::max(cvlen, fontengine.getTextWidthbyInt(menuitems[i]->getMaxValueLen()));
cvlen = std::max(cvlen, fontengine.getTextWidth(menuitems[i]->getCaption()));
vlen = std::max(vlen, fontengine.getTextWidthbyInt(menuitems[i]->getMaxValueLen()) + fontengine.getTextWidth(menuitems[i]->getCaption()) + valuewidth);
extravalwidth = std::max(extravalwidth, fontengine.getTextWidthbyInt(menuitems[i]->getMaxValueLen()));
// if(menuitems[i]->getMaxValueLen()==9)
// std::cout << "Moep: " << menuitems[i]->getMaxValueLen() << " * " << fontengine.getTextWidthbyInt(1) << " + " << fontengine.getTextWidthbyInt(menuitems[i]->getMaxValueLen()) << " + " << valuewidth << " und das erg: " << fontengine.getTextWidthbyInt(menuitems[i]->getMaxValueLen()) + fontengine.getTextWidth(menuitems[i]->getCaption()) + valuewidth<< std::endl;
} else {
clen = std::max(clen, fontengine.getTextWidth(menuitems[i]->getCaption()));
}
// std::cout << "moep" << extravalwidth << std::endl;
height += basefontsize + menuitems[i]->getFontSizeAdd() + offset;
}
// fullvalwidth = clen + vlen + valuewidth;
maxlen = std::max(clen, cvlen+extravalwidth+(cvlen>0?valuewidth:0));
glDisable(GL_DEPTH_TEST);
if(paintbackground&&true) {
// geht noch nicht ganz
glDisable(GL_TEXTURE_2D);
// glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
glEnable(GL_BLEND);
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;
}
bgarea.x = (Sint16)pos.x-maxlen/2;
bgarea.w = maxlen;
//offset
// bgarea.x += offset;
// bgarea.y += offset;
bgarea.w += offset*2;
bgarea.h += offset;
//Wenn schatten dann hier...
// bgarea.x += offset;
// bgarea.y += offset;
bgarea.x -= offset/2;
bgarea.y -= offset/2;
glColor4f(0.1f, 0.1f, 0.1f, 0.2f);
GLFontEngine::paintSDLRect(bgarea);
bgarea.x -= offset/2;
bgarea.y -= offset/2;
glColor4f(0.2f, 0.2f, 0.2f, 0.3f);
GLFontEngine::paintSDLRect(bgarea);
glDisable(GL_BLEND);
}
for(unsigned int i=0; i<menuitems.size(); i++) {
menuitems[i]->render(pos, centermenu, basefontsize, maxwidth, valuewidth, ((unsigned int)itempos==i)&&dohighlight, clen, cvlen, cvlen+extravalwidth+valuewidth);
pos.y += basefontsize + menuitems[i]->getFontSizeAdd() + offset;
}
glEnable(GL_DEPTH_TEST);
}
} // namespace segl