libsegl/glmenu/menumenu.cpp

167 lines
4.3 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 18:48:58 +02:00
int clen=0, vlen=0, cvlen=0, height=0, fullvalwidth=0, maxlen=0, extravalwidth=0;
2008-03-23 18:00:55 +01:00
for(unsigned int i=0; i<menuitems.size(); i++) {
fontengine.setSize(basefontsize+menuitems[i]->getFontSizeAdd());
2008-03-31 13:00:13 +02:00
if(menuitems[i]->hasValue()) {
2008-03-31 18:48:58 +02:00
// 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;
2008-03-31 13:00:13 +02:00
} else {
clen = std::max(clen, fontengine.getTextWidth(menuitems[i]->getCaption()));
}
2008-03-31 18:48:58 +02:00
// std::cout << "moep" << extravalwidth << std::endl;
2008-03-23 18:00:55 +01:00
height += basefontsize + menuitems[i]->getFontSizeAdd() + offset;
}
2008-03-31 18:48:58 +02:00
// fullvalwidth = clen + vlen + valuewidth;
maxlen = std::max(clen, cvlen+extravalwidth+(cvlen>0?valuewidth:0));
2008-03-23 18:00:55 +01:00
glDisable(GL_DEPTH_TEST);
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 18:48:58 +02:00
// glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
2008-03-31 00:14:35 +02:00
glEnable(GL_BLEND);
2008-03-31 18:48:58 +02:00
2008-03-31 00:14:35 +02:00
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;
}
2008-03-31 18:48:58 +02:00
bgarea.x = pos.x-maxlen/2;
bgarea.w = maxlen;
2008-03-31 00:14:35 +02:00
2008-03-31 18:48:58 +02:00
//offset
// bgarea.x += offset;
// bgarea.y += offset;
bgarea.w += offset*2;
bgarea.h += offset;
2008-03-31 00:14:35 +02:00
2008-03-31 18:48:58 +02:00
//Wenn schatten dann hier...
// bgarea.x += offset;
// bgarea.y += offset;
bgarea.x -= offset/2;
bgarea.y -= offset/2;
2008-03-31 18:48:58 +02:00
glColor4f(0.1f, 0.1f, 0.1f, 0.2f);
GLFontEngine::paintSDLRect(bgarea);
bgarea.x -= offset/2;
bgarea.y -= offset/2;
2008-03-31 00:14:35 +02:00
2008-03-31 18:48:58 +02:00
glColor4f(0.2f, 0.2f, 0.2f, 0.3f);
2008-03-23 18:00:55 +01:00
GLFontEngine::paintSDLRect(bgarea);
2008-03-31 18:48:58 +02:00
glDisable(GL_BLEND);
2008-03-23 18:00:55 +01:00
}
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);
}