From f542e933b209e3b4e76ccf95fb1f60efb9d9c77c Mon Sep 17 00:00:00 2001 From: seba Date: Sun, 23 Mar 2008 18:00:55 +0100 Subject: [PATCH] Arbeit in Afrika Menus --- glfontengine.cpp | 4 +- glmenu/menuitem.cpp | 40 +++++++++- glmenu/menuitem.h | 15 +++- glmenu/menuitems.cpp | 175 ++++++++++++++++++++++++++++++++++++++++- glmenu/menuitems.h | 78 ++++++++++++++++++ glmenu/menumanager.cpp | 17 ++++ glmenu/menumanager.h | 5 ++ glmenu/menumenu.cpp | 64 ++++++++++++--- glmenu/menumenu.h | 7 ++ glsdlscreen.cpp | 25 +++++- glsdlscreen.h | 4 + gltexture.cpp | 11 ++- 12 files changed, 426 insertions(+), 19 deletions(-) diff --git a/glfontengine.cpp b/glfontengine.cpp index d00cf7a..f1f7afb 100644 --- a/glfontengine.cpp +++ b/glfontengine.cpp @@ -95,7 +95,7 @@ bool GLFontEngine::fontSelect(std::string fontstr) { else { //fallbackfont - ersten aus der liste font = (++fontpool.begin())->second; - std::cout << "font: " << font << std::endl; +// std::cout << "font: " << font << std::endl; } 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) { + glEnable(GL_TEXTURE_2D); + SDL_Rect m = { x, y, getTextWidth(str), fsize}; if(center) diff --git a/glmenu/menuitem.cpp b/glmenu/menuitem.cpp index 831e681..e61b173 100644 --- a/glmenu/menuitem.cpp +++ b/glmenu/menuitem.cpp @@ -5,14 +5,31 @@ MenuItem::MenuItem(std::string c) { fontsizeadd = 0; fontname = "menufont"; usevalue = false; + selectable = true; + grey = false; + novaluecenter = false; 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); if(highlight) fontengine.setColor(1.0f, 0.0f, 0.0f); + else if(grey) + fontengine.setColor(0.8f, 0.8f, 0.8f); else fontengine.setColor(1.0f, 1.0f, 1.0f); @@ -21,18 +38,33 @@ void MenuItem::render(Punkt2D pos, bool center, int basefontsize, int maxwidth, } else { // center und position ggf. überarbeiten.. - Punkt2D tmp; - tmp = pos - valuewidth/2 - fontengine.getTextWidth(caption)/2; + Punkt2D tmp = pos; + + tmp.x = pos.x - valuewidth/2 - fontengine.getTextWidth(caption)/2; 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); +// std::cout << "Value: " << value << std::endl; } } +bool MenuItem::isSelectable() { + return (!grey)&&selectable; +} + +void MenuItem::setFontSizeAdd(int fsa) { + fontsizeadd = fsa; +} + int MenuItem::getFontSizeAdd() { return fontsizeadd; } +void MenuItem::greyItem(bool _grey) { + grey = _grey; +} + MenuItem::~MenuItem() { diff --git a/glmenu/menuitem.h b/glmenu/menuitem.h index 85e7c4e..36021de 100644 --- a/glmenu/menuitem.h +++ b/glmenu/menuitem.h @@ -13,18 +13,31 @@ class MenuItem { GLFontEngine fontengine; std::string fontname; int fontsizeadd; + bool grey; bool usevalue; + bool selectable; + bool novaluecenter; public: MenuItem(std::string); virtual ~MenuItem(); + std::string getValue(); + std::string getCaption(); + void setCaption(std::string str); + virtual void left() { }; virtual void right() { }; virtual void select() { }; + virtual void charInput(char c) { }; + + 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); + virtual void render(Punkt2D pos, bool center, int basefontsize, int maxwidth, int valuewidth, bool highlight, int caplen, int vallen); }; #endif diff --git a/glmenu/menuitems.cpp b/glmenu/menuitems.cpp index b288ffb..d23d136 100644 --- a/glmenu/menuitems.cpp +++ b/glmenu/menuitems.cpp @@ -1,9 +1,182 @@ #include "menuitems.h" +// MISendSDLEvent + MISendSDLEvent::MISendSDLEvent(std::string str, SDL_Event event) : MenuItem(str) { - sendevent = event; + resetEvent(event); } void MISendSDLEvent::select() { 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; t0) { + value = value.substr(0, value.length()-1); + } + } else if(c>=32) { + if(!maxlen || value.length() +#include #include #include "menuitem.h" @@ -11,7 +13,83 @@ class MISendSDLEvent : public MenuItem { MISendSDLEvent(std::string str, SDL_Event event); 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 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 diff --git a/glmenu/menumanager.cpp b/glmenu/menumanager.cpp index 3551ab7..0bc5442 100644 --- a/glmenu/menumanager.cpp +++ b/glmenu/menumanager.cpp @@ -14,12 +14,17 @@ bool MenuManager::changeMenu(MenuMenu *mm) { for(unsigned int i=0; iresetItemPos(); return true; } } return false; } +const MenuMenu* MenuManager::getMenu() { + return aktuell; +} + void MenuManager::render() { glEnable(GL_TEXTURE_2D); GLFontEngine::prepare2DbyPushingMatrix(); @@ -52,3 +57,15 @@ void MenuManager::select() { if(aktuell) aktuell->select(); } + +void MenuManager::charInput(char c) { + if(aktuell) + aktuell->charInput(c); +} + +void MenuManager::reset() { + if(aktuell) { + aktuell->resetItemPos(); + } + +} diff --git a/glmenu/menumanager.h b/glmenu/menumanager.h index 5c3fff9..814efd9 100644 --- a/glmenu/menumanager.h +++ b/glmenu/menumanager.h @@ -17,6 +17,7 @@ class MenuManager { void addMenu(MenuMenu*); bool changeMenu(MenuMenu*); + const MenuMenu* getMenu(); void render(); void up(); @@ -24,6 +25,10 @@ class MenuManager { void left(); void right(); void select(); + void charInput(char c); + + + void reset(); }; #endif diff --git a/glmenu/menumenu.cpp b/glmenu/menumenu.cpp index 34c3867..38e68f6 100644 --- a/glmenu/menumenu.cpp +++ b/glmenu/menumenu.cpp @@ -4,6 +4,8 @@ MenuMenu::MenuMenu() { itempos = 0; centermenu = false; centerScreenX = false; + dohighlight = true; + paintbackground = true; basefontsize = 20; maxwidth = 0; valuewidth = 100; @@ -32,22 +34,30 @@ void MenuMenu::setMaxWidth(int _mw) { maxwidth = _mw; } +void MenuMenu::setPaintBackground(bool _pbg) { + paintbackground = _pbg; +} + void MenuMenu::addMenuItem(MenuItem* mi) { menuitems.push_back(mi); } void MenuMenu::up() { - if(itempos==0) - itempos = menuitems.size()-1; - else - itempos--; + do { + if(itempos==0) + itempos = menuitems.size()-1; + else + itempos--; + } while(!menuitems[itempos]->isSelectable()); } void MenuMenu::down() { - if(itempos==menuitems.size()-1) - itempos = 0; - else - itempos++; + do { + if(itempos==menuitems.size()-1) + itempos = 0; + else + itempos++; + } while(!menuitems[itempos]->isSelectable()); } void MenuMenu::left() { @@ -62,15 +72,51 @@ void MenuMenu::select() { menuitems[itempos]->select(); } +void MenuMenu::charInput(char c) { + menuitems[itempos]->charInput(c); +} + +void MenuMenu::resetItemPos() { + dohighlight = true; + for(itempos = 0; itemposisSelectable(); 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; } + + int clen=0, vlen=0, height=0; + for(unsigned int i=0; igetFontSizeAdd()); + + 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; irender(pos, centermenu, basefontsize, maxwidth, valuewidth, itempos==i); + menuitems[i]->render(pos, centermenu, basefontsize, maxwidth, valuewidth, (itempos==i)&&dohighlight, clen, vlen); pos.y += basefontsize + menuitems[i]->getFontSizeAdd() + offset; } } diff --git a/glmenu/menumenu.h b/glmenu/menumenu.h index 1d94417..de7a0ff 100644 --- a/glmenu/menumenu.h +++ b/glmenu/menumenu.h @@ -14,10 +14,13 @@ class MenuMenu { Punkt2D menupos; bool centermenu; bool centerScreenX; + bool dohighlight; + bool paintbackground; int basefontsize; int offset; int maxwidth; int valuewidth; + GLFontEngine fontengine; public: MenuMenu(); @@ -26,12 +29,16 @@ class MenuMenu { void setCenterScreenX(bool); void setBaseFontSize(int); void setMaxWidth(int); + void setPaintBackground(bool); void up(); void down(); void left(); void right(); void select(); + void charInput(char c); + + void resetItemPos(); void addMenuItem(MenuItem*); diff --git a/glsdlscreen.cpp b/glsdlscreen.cpp index c48670f..7568957 100644 --- a/glsdlscreen.cpp +++ b/glsdlscreen.cpp @@ -32,6 +32,9 @@ int GLSDLScreen::getFlags() { videoflags |= SDL_HWPALETTE; } + if(fullscreen) + videoflags |= SDL_FULLSCREEN; + return videoflags; } void GLSDLScreen::enableOpenGL(bool w) { @@ -68,6 +71,13 @@ void GLSDLScreen::setVideoMode(int _width, int _height, int _bpp) { 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() { return SDL_VideoModeOK(width, height, bpp, getFlags()); } @@ -111,10 +121,14 @@ bool GLSDLScreen::apply() { extraglparam(); } - glMatrixMode(GL_MODELVIEW); + // Texturen neuladen, eigentlich nur für Windows. Aber egal. GLTexture::reloadAll(); + glMatrixMode(GL_TEXTURE); + glLoadIdentity(); + + glMatrixMode(GL_MODELVIEW); } return true; @@ -127,3 +141,12 @@ int GLSDLScreen::getWidth() { int GLSDLScreen::getHeight() { return height; } + +void GLSDLScreen::clearScreen() { + glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity(); +} + +void GLSDLScreen::renderScreen() { + SDL_GL_SwapBuffers(); +} diff --git a/glsdlscreen.h b/glsdlscreen.h index dca61f5..7d02fda 100644 --- a/glsdlscreen.h +++ b/glsdlscreen.h @@ -27,6 +27,7 @@ class GLSDLScreen { void enableResizable(bool); void enableFullscreen(bool); 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 enableGLSetup(bool); @@ -37,6 +38,9 @@ class GLSDLScreen { int getWidth(); int getHeight(); + + void clearScreen(); + void renderScreen(); }; #endif diff --git a/gltexture.cpp b/gltexture.cpp index a519f4f..336900b 100644 --- a/gltexture.cpp +++ b/gltexture.cpp @@ -9,6 +9,7 @@ GLTexture::GLTexture(std::string fname, GLint _minfilter, GLint _magfilter, GLin init(); setParameter(_minfilter, _magfilter, _wraps, _wrapt); loadImage(fname); + filename = fname; alltextures.push_back(this); } @@ -128,17 +129,22 @@ bool GLTexture::setParameter(GLint _minfilter, GLint _magfilter, GLint _wraps, G if(!loaded) return true; + loaded = false; + texconverted = false; + if(keepsurface&&tex!=0) { unloadTexture(); return loadLocalSurface(); } else if(filename!="") { - return loadImage("filename"); + std::cout << "Filename: " << filename << std::endl; + return loadImage(filename); } else { // keine datei, kein surface unloadTexture(); std::cerr << "Couldn't reload GLTexture " << this << "- No surface, no file!" << std::endl; return false; } + std::cout << "Texture reloaded" << std::endl; } int GLTexture::getW() { @@ -154,8 +160,9 @@ bool GLTexture::isLoaded() { } void GLTexture::reloadAll() { - for(unsigned int i=0; isetParameter(); + } } GLTexture::~GLTexture() {