Arbeit in Afrika

Menus
This commit is contained in:
seba 2008-03-23 18:00:55 +01:00
parent cca746e2f8
commit f542e933b2
12 changed files with 427 additions and 20 deletions

View File

@ -95,7 +95,7 @@ bool GLFontEngine::fontSelect(std::string fontstr) {
else { else {
//fallbackfont - ersten aus der liste //fallbackfont - ersten aus der liste
font = (++fontpool.begin())->second; font = (++fontpool.begin())->second;
std::cout << "font: " << font << std::endl; // std::cout << "font: " << font << std::endl;
} }
return true; return true;
@ -158,6 +158,8 @@ void GLFontEngine::renderLine(std::string text, SDL_Rect pos) {
void GLFontEngine::renderLine(std::string str, int x, int y, bool center, SDL_Rect *rendered_to) { void GLFontEngine::renderLine(std::string str, int x, int y, bool center, SDL_Rect *rendered_to) {
glEnable(GL_TEXTURE_2D);
SDL_Rect m = { x, y, getTextWidth(str), fsize}; SDL_Rect m = { x, y, getTextWidth(str), fsize};
if(center) if(center)

View File

@ -5,14 +5,31 @@ MenuItem::MenuItem(std::string c) {
fontsizeadd = 0; fontsizeadd = 0;
fontname = "menufont"; fontname = "menufont";
usevalue = false; usevalue = false;
selectable = true;
grey = false;
novaluecenter = false;
fontengine.fontSelect(fontname); fontengine.fontSelect(fontname);
} }
void MenuItem::render(Punkt2D pos, bool center, int basefontsize, int maxwidth, int valuewidth, bool highlight) { std::string MenuItem::getValue() {
return value;
}
std::string MenuItem::getCaption() {
return caption;
}
void MenuItem::setCaption(std::string str) {
caption = str;
}
void MenuItem::render(Punkt2D pos, bool center, int basefontsize, int maxwidth, int valuewidth, bool highlight, int caplen, int vallen) {
fontengine.setSize(basefontsize+fontsizeadd); fontengine.setSize(basefontsize+fontsizeadd);
if(highlight) if(highlight)
fontengine.setColor(1.0f, 0.0f, 0.0f); fontengine.setColor(1.0f, 0.0f, 0.0f);
else if(grey)
fontengine.setColor(0.8f, 0.8f, 0.8f);
else else
fontengine.setColor(1.0f, 1.0f, 1.0f); fontengine.setColor(1.0f, 1.0f, 1.0f);
@ -21,18 +38,33 @@ void MenuItem::render(Punkt2D pos, bool center, int basefontsize, int maxwidth,
} else { } else {
// center und position ggf. überarbeiten.. // center und position ggf. überarbeiten..
Punkt2D tmp; Punkt2D tmp = pos;
tmp = pos - valuewidth/2 - fontengine.getTextWidth(caption)/2;
tmp.x = pos.x - valuewidth/2 - fontengine.getTextWidth(caption)/2;
fontengine.renderLine(caption, tmp.x, tmp.y, center); fontengine.renderLine(caption, tmp.x, tmp.y, center);
tmp = pos + valuewidth/2;
tmp.x = pos.x + valuewidth/2;
fontengine.renderLine(value, tmp.x, tmp.y, false); fontengine.renderLine(value, tmp.x, tmp.y, false);
// std::cout << "Value: " << value << std::endl;
} }
} }
bool MenuItem::isSelectable() {
return (!grey)&&selectable;
}
void MenuItem::setFontSizeAdd(int fsa) {
fontsizeadd = fsa;
}
int MenuItem::getFontSizeAdd() { int MenuItem::getFontSizeAdd() {
return fontsizeadd; return fontsizeadd;
} }
void MenuItem::greyItem(bool _grey) {
grey = _grey;
}
MenuItem::~MenuItem() { MenuItem::~MenuItem() {

View File

@ -13,18 +13,31 @@ class MenuItem {
GLFontEngine fontengine; GLFontEngine fontengine;
std::string fontname; std::string fontname;
int fontsizeadd; int fontsizeadd;
bool grey;
bool usevalue; bool usevalue;
bool selectable;
bool novaluecenter;
public: public:
MenuItem(std::string); MenuItem(std::string);
virtual ~MenuItem(); virtual ~MenuItem();
std::string getValue();
std::string getCaption();
void setCaption(std::string str);
virtual void left() { }; virtual void left() { };
virtual void right() { }; virtual void right() { };
virtual void select() { }; virtual void select() { };
int getFontSizeAdd(); virtual void charInput(char c) { };
virtual void render(Punkt2D pos, bool center, int basefontsize, int maxwidth, int valuewidth, bool highlight); void setFontSizeAdd(int fsa);
int getFontSizeAdd();
bool isSelectable();
void greyItem(bool _grey);
void noValueCenter(bool _vc);
virtual void render(Punkt2D pos, bool center, int basefontsize, int maxwidth, int valuewidth, bool highlight, int caplen, int vallen);
}; };
#endif #endif

View File

@ -1,9 +1,182 @@
#include "menuitems.h" #include "menuitems.h"
// MISendSDLEvent
MISendSDLEvent::MISendSDLEvent(std::string str, SDL_Event event) : MenuItem(str) { MISendSDLEvent::MISendSDLEvent(std::string str, SDL_Event event) : MenuItem(str) {
sendevent = event; resetEvent(event);
} }
void MISendSDLEvent::select() { void MISendSDLEvent::select() {
SDL_PushEvent(&sendevent); SDL_PushEvent(&sendevent);
} }
void MISendSDLEvent::resetEvent(SDL_Event event) {
sendevent = event;
}
// MITextLabel
MITextLabel::MITextLabel(std::string str) : MenuItem(str) {
selectable = false;
}
// MIValueLabel
MIValueLabel::MIValueLabel(std::string str) : MenuItem(str) {
selectable = false;
usevalue = true;
}
void MIValueLabel::setValue(std::string _val) {
value = _val;
}
// MIToggle
MIToggle::MIToggle(std::string str) : MenuItem(str) {
usevalue = true;
togglepos = 0;
}
void MIToggle::left() {
togglepos--;
if(togglepos<0)
togglepos = toggles.size()-1;
value = getValueString();
}
void MIToggle::right() {
togglepos++;
if(togglepos>=toggles.size())
togglepos = 0;
value = getValueString();
}
void MIToggle::select() {
right();
}
void MIToggle::addToggle(std::string str) {
toggles.push_back(str);
if(toggles.size()==1) {
value = getValueString();
}
}
int MIToggle::getValueInt() {
return togglepos;
}
void MIToggle::setValueInt(int val) {
if(val<0||val>=toggles.size())
return;
togglepos = val;
value = getValueString();
}
void MIToggle::setValueString(std::string str) {
for(unsigned int t=0; t<toggles.size(); t++) {
std::cout << str << " == " << toggles[t] << std::endl;
if(str==toggles[t]) {
togglepos = t;
value = getValueString();
return;
}
}
}
std::string MIToggle::getValueString() {
if(toggles.size()==0)
return "";
return toggles[togglepos];
}
// MIStringInput
MIStringInput::MIStringInput(std::string str, int _maxlen) : MenuItem(str) {
usevalue = true;
maxlen = _maxlen;
}
void MIStringInput::charInput(char c) {
// std::cout << "INPUT: " << c << ", maxlen" << maxlen << std::endl;
if(c=='\b') {
if(value.length()>0) {
value = value.substr(0, value.length()-1);
}
} else if(c>=32) {
if(!maxlen || value.length()<maxlen)
value +=c;
// std::cout << maxlen << " && " << value.length() << " < " << maxlen << std::endl;
}
}
void MIStringInput::setValue(std::string str) {
value = str;
}
std::string MIStringInput::getValue() {
return value;
}
// MICheckbox
MICheckBox::MICheckBox(std::string str) : MenuItem(str) {
usevalue = true;
}
bool MICheckBox::isChecked() {
return state;
}
void MICheckBox::setState(bool _state) {
state = _state;
updateValue();
}
void MICheckBox::select() {
right();
}
void MICheckBox::right() {
state = !state;
updateValue();
}
void MICheckBox::left() {
state = !state;
updateValue();
}
void MICheckBox::updateValue() {
value = state ? "An" : "Aus";
}
// MIEventOnToggle
MIEventOnToggle::MIEventOnToggle(std::string str, SDL_Event event) : MIToggle(str) {
resetEvent(event);
}
void MIEventOnToggle::sendEvent() {
SDL_PushEvent(&sendevent);
}
void MIEventOnToggle::select() {
MIToggle::select();
sendEvent();
}
void MIEventOnToggle::left() {
MIToggle::left();
sendEvent();
}
void MIEventOnToggle::right() {
MIToggle::right();
sendEvent();
}
void MIEventOnToggle::resetEvent(SDL_Event event) {
sendevent = event;
}

View File

@ -1,6 +1,8 @@
#ifndef __MENUITEMS_H #ifndef __MENUITEMS_H
#define __MENUITEMS_H #define __MENUITEMS_H
#include <string>
#include <vector>
#include <SDL.h> #include <SDL.h>
#include "menuitem.h" #include "menuitem.h"
@ -11,7 +13,83 @@ class MISendSDLEvent : public MenuItem {
MISendSDLEvent(std::string str, SDL_Event event); MISendSDLEvent(std::string str, SDL_Event event);
void select(); void select();
void resetEvent(SDL_Event event);
}; };
class MITextLabel : public MenuItem {
private:
public:
MITextLabel(std::string str);
};
class MIValueLabel : public MenuItem {
private:
public:
MIValueLabel(std::string str);
void setValue(std::string);
};
class MIToggle : public MenuItem {
private:
std::vector<std::string> toggles;
int togglepos;
public:
MIToggle(std::string);
void left();
void right();
void select();
void addToggle(std::string);
int getValueInt();
void setValueInt(int val);
std::string getValueString();
void setValueString(std::string str);
};
class MIStringInput : public MenuItem {
private:
int maxlen;
public:
MIStringInput(std::string str, int _maxlen=0);
void charInput(char c);
void setValue(std::string str);
std::string getValue();
};
class MICheckBox : public MenuItem {
private:
bool state;
void updateValue();
public:
MICheckBox(std::string str);
bool isChecked();
void setState(bool _state);
void select();
void right();
void left();
};
class MIEventOnToggle : public MIToggle {
private:
SDL_Event sendevent;
void sendEvent();
public:
MIEventOnToggle(std::string str, SDL_Event event);
void select();
void left();
void right();
void resetEvent(SDL_Event event);
};
#endif #endif

View File

@ -14,12 +14,17 @@ bool MenuManager::changeMenu(MenuMenu *mm) {
for(unsigned int i=0; i<menus.size(); i++) { for(unsigned int i=0; i<menus.size(); i++) {
if(menus[i]==mm) { if(menus[i]==mm) {
aktuell = mm; aktuell = mm;
aktuell->resetItemPos();
return true; return true;
} }
} }
return false; return false;
} }
const MenuMenu* MenuManager::getMenu() {
return aktuell;
}
void MenuManager::render() { void MenuManager::render() {
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
GLFontEngine::prepare2DbyPushingMatrix(); GLFontEngine::prepare2DbyPushingMatrix();
@ -52,3 +57,15 @@ void MenuManager::select() {
if(aktuell) if(aktuell)
aktuell->select(); aktuell->select();
} }
void MenuManager::charInput(char c) {
if(aktuell)
aktuell->charInput(c);
}
void MenuManager::reset() {
if(aktuell) {
aktuell->resetItemPos();
}
}

View File

@ -17,6 +17,7 @@ class MenuManager {
void addMenu(MenuMenu*); void addMenu(MenuMenu*);
bool changeMenu(MenuMenu*); bool changeMenu(MenuMenu*);
const MenuMenu* getMenu();
void render(); void render();
void up(); void up();
@ -24,6 +25,10 @@ class MenuManager {
void left(); void left();
void right(); void right();
void select(); void select();
void charInput(char c);
void reset();
}; };
#endif #endif

View File

@ -4,6 +4,8 @@ MenuMenu::MenuMenu() {
itempos = 0; itempos = 0;
centermenu = false; centermenu = false;
centerScreenX = false; centerScreenX = false;
dohighlight = true;
paintbackground = true;
basefontsize = 20; basefontsize = 20;
maxwidth = 0; maxwidth = 0;
valuewidth = 100; valuewidth = 100;
@ -32,22 +34,30 @@ void MenuMenu::setMaxWidth(int _mw) {
maxwidth = _mw; maxwidth = _mw;
} }
void MenuMenu::setPaintBackground(bool _pbg) {
paintbackground = _pbg;
}
void MenuMenu::addMenuItem(MenuItem* mi) { void MenuMenu::addMenuItem(MenuItem* mi) {
menuitems.push_back(mi); menuitems.push_back(mi);
} }
void MenuMenu::up() { void MenuMenu::up() {
if(itempos==0) do {
itempos = menuitems.size()-1; if(itempos==0)
else itempos = menuitems.size()-1;
itempos--; else
itempos--;
} while(!menuitems[itempos]->isSelectable());
} }
void MenuMenu::down() { void MenuMenu::down() {
if(itempos==menuitems.size()-1) do {
itempos = 0; if(itempos==menuitems.size()-1)
else itempos = 0;
itempos++; else
itempos++;
} while(!menuitems[itempos]->isSelectable());
} }
void MenuMenu::left() { void MenuMenu::left() {
@ -62,6 +72,19 @@ void MenuMenu::select() {
menuitems[itempos]->select(); menuitems[itempos]->select();
} }
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() { void MenuMenu::render() {
Punkt2D pos = menupos; Punkt2D pos = menupos;
if(centerScreenX) { if(centerScreenX) {
@ -69,8 +92,31 @@ void MenuMenu::render() {
pos.x = screen->w/2; pos.x = screen->w/2;
} }
int clen=0, vlen=0, height=0;
for(unsigned int i=0; i<menuitems.size(); i++) { for(unsigned int i=0; i<menuitems.size(); i++) {
menuitems[i]->render(pos, centermenu, basefontsize, maxwidth, valuewidth, itempos==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++) {
menuitems[i]->render(pos, centermenu, basefontsize, maxwidth, valuewidth, (itempos==i)&&dohighlight, clen, vlen);
pos.y += basefontsize + menuitems[i]->getFontSizeAdd() + offset; pos.y += basefontsize + menuitems[i]->getFontSizeAdd() + offset;
} }
} }

View File

@ -14,10 +14,13 @@ class MenuMenu {
Punkt2D menupos; Punkt2D menupos;
bool centermenu; bool centermenu;
bool centerScreenX; bool centerScreenX;
bool dohighlight;
bool paintbackground;
int basefontsize; int basefontsize;
int offset; int offset;
int maxwidth; int maxwidth;
int valuewidth; int valuewidth;
GLFontEngine fontengine;
public: public:
MenuMenu(); MenuMenu();
@ -26,12 +29,16 @@ class MenuMenu {
void setCenterScreenX(bool); void setCenterScreenX(bool);
void setBaseFontSize(int); void setBaseFontSize(int);
void setMaxWidth(int); void setMaxWidth(int);
void setPaintBackground(bool);
void up(); void up();
void down(); void down();
void left(); void left();
void right(); void right();
void select(); void select();
void charInput(char c);
void resetItemPos();
void addMenuItem(MenuItem*); void addMenuItem(MenuItem*);

View File

@ -32,6 +32,9 @@ int GLSDLScreen::getFlags() {
videoflags |= SDL_HWPALETTE; videoflags |= SDL_HWPALETTE;
} }
if(fullscreen)
videoflags |= SDL_FULLSCREEN;
return videoflags; return videoflags;
} }
void GLSDLScreen::enableOpenGL(bool w) { void GLSDLScreen::enableOpenGL(bool w) {
@ -68,6 +71,13 @@ void GLSDLScreen::setVideoMode(int _width, int _height, int _bpp) {
height = 1; height = 1;
} }
void GLSDLScreen::getVideoRes(std::string vidstr, int *_w, int *_h) {
if(_w)
*_w = atoi(vidstr.substr(0, vidstr.find("x")).c_str());
if(_h)
*_h = atoi(vidstr.substr(vidstr.find("x")+1, (vidstr.length()-vidstr.find("x")-1)).c_str());
}
bool GLSDLScreen::isOK() { bool GLSDLScreen::isOK() {
return SDL_VideoModeOK(width, height, bpp, getFlags()); return SDL_VideoModeOK(width, height, bpp, getFlags());
} }
@ -111,10 +121,14 @@ bool GLSDLScreen::apply() {
extraglparam(); extraglparam();
} }
glMatrixMode(GL_MODELVIEW);
// Texturen neuladen, eigentlich nur für Windows. Aber egal. // Texturen neuladen, eigentlich nur für Windows. Aber egal.
GLTexture::reloadAll(); GLTexture::reloadAll();
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
} }
return true; return true;
@ -127,3 +141,12 @@ int GLSDLScreen::getWidth() {
int GLSDLScreen::getHeight() { int GLSDLScreen::getHeight() {
return height; return height;
} }
void GLSDLScreen::clearScreen() {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
}
void GLSDLScreen::renderScreen() {
SDL_GL_SwapBuffers();
}

View File

@ -27,6 +27,7 @@ class GLSDLScreen {
void enableResizable(bool); void enableResizable(bool);
void enableFullscreen(bool); void enableFullscreen(bool);
void setVideoMode(int _width, int _height, int _bpp); void setVideoMode(int _width, int _height, int _bpp);
static void getVideoRes(std::string vidstr, int *w, int *h);
void setGLNearFar(float _znear, float _zfar); void setGLNearFar(float _znear, float _zfar);
void enableGLSetup(bool); void enableGLSetup(bool);
@ -37,6 +38,9 @@ class GLSDLScreen {
int getWidth(); int getWidth();
int getHeight(); int getHeight();
void clearScreen();
void renderScreen();
}; };
#endif #endif

View File

@ -9,6 +9,7 @@ GLTexture::GLTexture(std::string fname, GLint _minfilter, GLint _magfilter, GLin
init(); init();
setParameter(_minfilter, _magfilter, _wraps, _wrapt); setParameter(_minfilter, _magfilter, _wraps, _wrapt);
loadImage(fname); loadImage(fname);
filename = fname;
alltextures.push_back(this); alltextures.push_back(this);
} }
@ -128,17 +129,22 @@ bool GLTexture::setParameter(GLint _minfilter, GLint _magfilter, GLint _wraps, G
if(!loaded) if(!loaded)
return true; return true;
loaded = false;
texconverted = false;
if(keepsurface&&tex!=0) { if(keepsurface&&tex!=0) {
unloadTexture(); unloadTexture();
return loadLocalSurface(); return loadLocalSurface();
} else if(filename!="") { } else if(filename!="") {
return loadImage("filename"); std::cout << "Filename: " << filename << std::endl;
return loadImage(filename);
} else { } else {
// keine datei, kein surface // keine datei, kein surface
unloadTexture(); unloadTexture();
std::cerr << "Couldn't reload GLTexture " << this << "- No surface, no file!" << std::endl; std::cerr << "Couldn't reload GLTexture " << this << "- No surface, no file!" << std::endl;
return false; return false;
} }
std::cout << "Texture reloaded" << std::endl;
} }
int GLTexture::getW() { int GLTexture::getW() {
@ -154,8 +160,9 @@ bool GLTexture::isLoaded() {
} }
void GLTexture::reloadAll() { void GLTexture::reloadAll() {
for(unsigned int i=0; i<alltextures.size(); i++) for(unsigned int i=0; i<alltextures.size(); i++) {
alltextures[i]->setParameter(); alltextures[i]->setParameter();
}
} }
GLTexture::~GLTexture() { GLTexture::~GLTexture() {