diff --git a/src/GuiComponent.cpp b/src/GuiComponent.cpp index e0ebbce6e..0ad40bbed 100644 --- a/src/GuiComponent.cpp +++ b/src/GuiComponent.cpp @@ -1,6 +1,7 @@ #include "GuiComponent.h" #include "Window.h" #include "Log.h" +#include "Renderer.h" GuiComponent::GuiComponent(Window* window) : mWindow(window), mParent(NULL) { @@ -34,6 +35,15 @@ void GuiComponent::update(int deltaTime) } void GuiComponent::render() +{ + Renderer::translate(mOffset); + + onRender(); + + Renderer::translate(-mOffset); +} + +void GuiComponent::onRender() { for(unsigned int i = 0; i < getChildCount(); i++) { @@ -57,16 +67,7 @@ void GuiComponent::deinit() } } -void GuiComponent::setParent(GuiComponent* parent) -{ - mParent = parent; -} - -GuiComponent* GuiComponent::getParent() -{ - return mParent; -} - +//Offset stuff. Vector2i GuiComponent::getGlobalOffset() { if(mParent) @@ -91,6 +92,7 @@ void GuiComponent::setOffset(int x, int y) mOffset.y = y; } +//Children stuff. void GuiComponent::addChild(GuiComponent* cmp) { mChildren.push_back(cmp); @@ -134,3 +136,13 @@ GuiComponent* GuiComponent::getChild(unsigned int i) { return mChildren.at(i); } + +void GuiComponent::setParent(GuiComponent* parent) +{ + mParent = parent; +} + +GuiComponent* GuiComponent::getParent() +{ + return mParent; +} diff --git a/src/GuiComponent.h b/src/GuiComponent.h index d4bf6f69b..5ab217295 100644 --- a/src/GuiComponent.h +++ b/src/GuiComponent.h @@ -12,11 +12,21 @@ public: GuiComponent(Window* window); virtual ~GuiComponent(); + //Called when input is received. //Return true if the input is consumed, false if it should continue to be passed to other children. virtual bool input(InputConfig* config, Input input); + + //Called when time passes. Default implementation also calls update(deltaTime) on children - so you should probably call GuiComponent::update(deltaTime) at some point. virtual void update(int deltaTime); + + //Called when it's time to render. Translates the OpenGL matrix, calls onRender() (which renders children), then un-translates the OpenGL matrix. + //You probably don't need to override this, but instead want the protected method onRender. virtual void render(); + + //Called when the Renderer initializes. Passes to children. virtual void init(); + + //Called when the Renderer deinitializes. Passes to children. virtual void deinit(); Vector2i getGlobalOffset(); @@ -34,6 +44,9 @@ public: GuiComponent* getChild(unsigned int i); protected: + //Default implementation just renders children - you should probably always call GuiComponent::onRender at some point in your custom onRender. + virtual void onRender(); + Window* mWindow; GuiComponent* mParent; Vector2i mOffset; diff --git a/src/Renderer.h b/src/Renderer.h index 7cbce0b39..c75294256 100644 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -3,6 +3,7 @@ #include #include +#include "Vector2.h" #include "platform.h" #include GLHEADER //#include "Font.h" @@ -28,8 +29,12 @@ namespace Renderer Font* getDefaultFont(FontSize size); void buildGLColorArray(GLubyte* ptr, unsigned int color, unsigned int vertCount); - //drawing commands + //graphics commands void swapBuffers(); + + void translatef(float x, float y); + void translate(Vector2i offset); + void drawRect(int x, int y, int w, int h, unsigned int color); void drawText(std::string text, int x, int y, unsigned int color, Font* font); void drawCenteredText(std::string text, int xOffset, int y, unsigned int color, Font* font); diff --git a/src/Renderer_draw_gl.cpp b/src/Renderer_draw_gl.cpp index ee2058d30..9b7120e64 100644 --- a/src/Renderer_draw_gl.cpp +++ b/src/Renderer_draw_gl.cpp @@ -26,6 +26,16 @@ namespace Renderer { } } + void translatef(float x, float y) + { + glTranslatef(x, y, 0); + } + + void translate(Vector2i offset) + { + translatef((float)offset.x, (float)offset.y); + } + void drawRect(int x, int y, int w, int h, unsigned int color) { #ifdef USE_OPENGL_ES diff --git a/src/Renderer_init_sdlgl.cpp b/src/Renderer_init_sdlgl.cpp index 8480d8eb0..f38b89b1b 100644 --- a/src/Renderer_init_sdlgl.cpp +++ b/src/Renderer_init_sdlgl.cpp @@ -88,7 +88,7 @@ namespace Renderer LOG(LogInfo) << "Created surface successfully."; //hide mouse cursor - initialCursorState = SDL_ShowCursor(0); + initialCursorState = SDL_ShowCursor(0) == 1; return true; } diff --git a/src/components/ImageComponent.cpp b/src/components/ImageComponent.cpp index 92a3889da..19644f95b 100644 --- a/src/components/ImageComponent.cpp +++ b/src/components/ImageComponent.cpp @@ -261,7 +261,7 @@ void ImageComponent::setFlipY(bool flip) mFlipY = flip; } -void ImageComponent::render() +void ImageComponent::onRender() { if(mTextureID && getOpacity() > 0) { @@ -274,14 +274,16 @@ void ImageComponent::render() float yCount = ((float)mResizeHeight/mHeight); Renderer::buildGLColorArray(colors, 0xFFFFFF00 | (getOpacity()), 6); - buildImageArray(getOffset().x, getOffset().y, points, texs, xCount, yCount); + buildImageArray(0, 0, points, texs, xCount, yCount); }else{ Renderer::buildGLColorArray(colors, 0xFFFFFF00 | (getOpacity()), 6); - buildImageArray(getOffset().x, getOffset().y, points, texs); + buildImageArray(0, 0, points, texs); } drawImageArray(points, texs, colors, 6); } + + GuiComponent::onRender(); } void ImageComponent::buildImageArray(int posX, int posY, GLfloat* points, GLfloat* texs, float px, float py) diff --git a/src/components/ImageComponent.h b/src/components/ImageComponent.h index 1c31a0320..476b4bfd3 100644 --- a/src/components/ImageComponent.h +++ b/src/components/ImageComponent.h @@ -31,8 +31,6 @@ public: bool hasImage(); - void render(); - //Image textures will be deleted on renderer deinitialization, and recreated on reinitialization (if mPath is not empty). void init(); void deinit(); @@ -40,6 +38,9 @@ public: unsigned char getOpacity(); void setOpacity(unsigned char opacity); +protected: + void onRender(); + private: unsigned int mResizeWidth, mResizeHeight; float mOriginX, mOriginY; diff --git a/src/components/TextListComponent.h b/src/components/TextListComponent.h index d8ea537ee..9999cfae6 100644 --- a/src/components/TextListComponent.h +++ b/src/components/TextListComponent.h @@ -21,7 +21,6 @@ public: bool input(InputConfig* config, Input input); void update(int deltaTime); - void render(); void addObject(std::string name, T obj, unsigned int color = 0xFF0000); void clear(); @@ -44,6 +43,9 @@ public: void setFont(Font* f); +protected: + void onRender(); + private: static const int SCROLLDELAY = 507; static const int SCROLLTIME = 200; @@ -96,9 +98,9 @@ TextListComponent::~TextListComponent() } template -void TextListComponent::render() +void TextListComponent::onRender() { - const int cutoff = getOffset().y; + const int cutoff = 0; const int entrySize = mFont->getHeight() + 5; int startEntry = 0; @@ -133,18 +135,20 @@ void TextListComponent::render() //draw selector bar if(mSelection == i) { - Renderer::drawRect(getOffset().x, y, Renderer::getScreenWidth(), mFont->getHeight(), mSelectorColor); + Renderer::drawRect(0, y, Renderer::getScreenWidth(), mFont->getHeight(), mSelectorColor); } ListRow row = mRowVector.at((unsigned int)i); if(mDrawCentered) - Renderer::drawCenteredText(row.name, getOffset().x + mTextOffsetX, y, (mSelection == i && mSelectedTextColorOverride != 0) ? mSelectedTextColorOverride : row.color, mFont); + Renderer::drawCenteredText(row.name, mTextOffsetX, y, (mSelection == i && mSelectedTextColorOverride != 0) ? mSelectedTextColorOverride : row.color, mFont); else - Renderer::drawText(row.name, getOffset().x + mTextOffsetX, y, (mSelection == i && mSelectedTextColorOverride != 0) ? mSelectedTextColorOverride : row.color, mFont); + Renderer::drawText(row.name, mTextOffsetX, y, (mSelection == i && mSelectedTextColorOverride != 0) ? mSelectedTextColorOverride : row.color, mFont); y += entrySize; } + + GuiComponent::onRender(); } template @@ -189,7 +193,7 @@ bool TextListComponent::input(InputConfig* config, Input input) } } - return false; + return GuiComponent::input(config, input); } template @@ -228,6 +232,8 @@ void TextListComponent::update(int deltaTime) } } } + + GuiComponent::update(deltaTime); } template diff --git a/src/components/ThemeComponent.h b/src/components/ThemeComponent.h index d79372586..8b711c8ec 100644 --- a/src/components/ThemeComponent.h +++ b/src/components/ThemeComponent.h @@ -9,7 +9,7 @@ #include "../AudioManager.h" #include "../Font.h" -//This class loads an XML-defined list of Guis. +//This class loads an XML-defined list of GuiComponents. class ThemeComponent : public GuiComponent { public: