Load GUI from XML

This commit is contained in:
2015-03-10 00:48:25 +01:00
parent 9d7dd452c7
commit 884fd8bb52
21 changed files with 1019 additions and 119 deletions

49
main.cc
View File

@@ -27,6 +27,9 @@
#include "Overlay.hh"
#include "TextWidget.hh"
#include "LinearLayout.hh"
#include "GUILoader.hh"
#include "FontProvider.hh"
#include "ImageProvider.hh"
using namespace gl;
@@ -125,7 +128,7 @@ int main(int argc, char *argv[])
glm::vec3(0.0f, 0.0f, 0),
glm::vec3(0, 1, 0));
Font font{"DejaVuSans.ttf", 12};
//Font font{"DejaVuSans.ttf", 12};
// sf::Font font;
// if (!font.loadFromFile("DejaVuSans.ttf")) {
@@ -212,34 +215,55 @@ int main(int argc, char *argv[])
bool close = false;
TTF_Font * font = FontProvider::getInstance().getFont("DejaVuSans.ttf", 12);
unsigned fpsTime = 0, fpsCount = 0;
TextWidget fpsText{font, "0 FPS"};
TextWidget fpsText{font, "0 FPS", WRAP_CONTENT, WRAP_CONTENT, "fpsText"};
fpsText.setBackgroundColor(SDL_Color{0, 255, 0, 128});
fpsText.setPadding(8, 8, 8, 8);
auto laber = make_unique<TextWidget>(font, "Dies ist ein längerer\nText, der umgebrochen werden sollte. Bla laber schwafl, laber bullshit bingo",
200, WRAP_CONTENT);
204, WRAP_CONTENT, "laber");
laber->setBackgroundColor(SDL_Color{255, 0, 0, 128});
laber->setPadding(4, 0, 4, 0);
SDLSurfaceUPtr buttonImg(IMG_Load("textures/button_100x30.png"));
if (!buttonImg)
throw SDLException();
auto button = make_unique<TextWidget>(font, "Do stuff!", 100, 30);
auto button = make_unique<TextWidget>(font, "Do stuff!", 100, 30, "button");
button->setPadding(6, 6, 6, 6);
button->setBackground(buttonImg.get());
button->setForegroundColor(SDL_Color{0, 0, 0, 255});
button->setAlignment(ALIGN_CENTER, ALIGN_CENTER);
auto button2 = make_unique<TextWidget>(font, "Cancel", 100, 30, "button2");
button2->setPadding(6, 6, 6, 6);
button2->setBackground(buttonImg.get());
button2->setForegroundColor(SDL_Color{0, 0, 0, 255});
button2->setAlignment(ALIGN_CENTER, ALIGN_CENTER);
auto layout = make_unique<LinearLayout>();
auto layout2 = make_unique<LinearLayout>(DIR_HORIZONTAL, MATCH_PARENT, WRAP_CONTENT, "layout2");
layout2->setBackgroundColor(SDL_Color{0, 255, 0, 128});
layout2->setChildPadding(4);
layout2->addChild(std::move(button2));
layout2->addChild(std::move(button));
auto layout = make_unique<LinearLayout>(DIR_VERTICAL, WRAP_CONTENT, WRAP_CONTENT, "layout");
layout->setBackgroundColor(SDL_Color{0, 255, 255, 128});
layout->addChild(std::move(laber));
layout->addChild(std::move(button));
layout->addChild(std::move(layout2));
layout->setChildPadding(4);
layout->setPadding(4, 4, 4, 4);
layout->setGLRenderPos(200, 200);
auto gui = loadGUIFromFile("layouts/test.xml");
if (!gui)
throw Exception{"GUI load failed"};
View& guiRoot = dynamic_cast<View&>(*gui);
gui->setGLRenderPos(200, 500);
TextWidget& xmlButton = dynamic_cast<TextWidget&>(guiRoot.getPath("layout2/button"));
int countdown = 5;
xmlButton.setText(std::to_string(countdown));
while (!close) {
SDL_Event event;
@@ -368,6 +392,13 @@ int main(int argc, char *argv[])
fpsCount = 0;
fpsTime = 0;
if(countdown > 0) {
--countdown;
if (countdown > 0)
xmlButton.setText(std::to_string(countdown));
else
xmlButton.setText("Do stuff!");
}
} else {
++fpsCount;
fpsTime += elapsed;
@@ -380,6 +411,7 @@ int main(int argc, char *argv[])
fpsText.renderToGL();
layout->renderToGL();
gui->renderToGL();
glDisable(GL_BLEND);
SDL_GL_SwapWindow(window);
@@ -393,6 +425,9 @@ int main(int argc, char *argv[])
SDL_DestroyWindow(window);
FontProvider::getInstance().cleanup();
ImageProvider::getInstance().cleanup();
// Clean up
TTF_Quit();
IMG_Quit();