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

107
Widget.cc
View File

@@ -13,16 +13,22 @@ Widget::Widget(int width, int height, std::string name)
bg_(SDL_Color{0, 0, 0, 0}), fg_(SDL_Color{255, 255, 255, 255}),
padLeft_(0), padTop_(0), padRight_(0), padBottom_(0),
alignHoriz_(ALIGN_LEFT), alignVert_(ALIGN_TOP),
alignContHoriz_(ALIGN_LEFT), alignContVert_(ALIGN_TOP),
parent_(nullptr),
renderTop_(0), renderLeft_(0),
renderTexValid_(false), renderAttribsValid_(false),
renderTex_(nullptr), vaID_(0)
{
if (width_ == WRAP_CONTENT)
if ((width_ == WRAP_CONTENT) ||
(width_ == MATCH_PARENT) ||
(width_ == WRAP_CONTENT_FILL))
realWidth_ = 0;
else
realWidth_ = width_;
if (height_ == WRAP_CONTENT)
if ((height_ == WRAP_CONTENT) ||
(height_ == MATCH_PARENT) ||
(height_ == WRAP_CONTENT_FILL))
realHeight_ = 0;
else
realHeight_ = height_;
@@ -38,18 +44,33 @@ void Widget::setSize(int width, int height)
if ((width_ != width) || (height_ != height)) {
width_ = width;
height_ = height;
if (width_ != WRAP_CONTENT)
if (realWidth_ != width_) {
realWidth_ = width_;
}
if (height_ != WRAP_CONTENT)
if (realHeight_ != height_) {
realHeight_ = height_;
}
if (width_ == MATCH_PARENT) {
if (parent_)
realWidth_ = parent_->realWidth_ - parent_->padLeft_ - parent_->padRight_;
else
realWidth_ = 0;
} else if ((width_ == WRAP_CONTENT) ||
(width_ == WRAP_CONTENT_FILL))
realWidth_ = 0;
else
realWidth_ = width_;
layout();
if (height_ == MATCH_PARENT) {
if (parent_)
realHeight_ = parent_->realHeight_ - parent_->padTop_ - parent_->padBottom_;
else
realHeight_ = 0;
} else if ((height_ == WRAP_CONTENT) ||
(height_ == WRAP_CONTENT_FILL))
realHeight_ = 0;
else
realHeight_ = height_;
if (parent_)
parent_->layout();
else
layout();
}
}
@@ -102,9 +123,32 @@ void Widget::setAlignment(int horiz, int vert) {
}
}
void Widget::setContainerAlignment(int horiz, int vert) {
if ((alignContHoriz_ != horiz) ||
(alignContVert_ != vert)) {
alignContHoriz_ = horiz;
alignContVert_ = vert;
if (parent_)
parent_->layout();
}
}
void Widget::setParent(Widget *parent)
{
parent_ = parent;
if (width_ == MATCH_PARENT) {
if (parent_ && (parent_->realWidth_ > 0))
realWidth_ = parent_->realWidth_ - parent_->padLeft_ - parent_->padRight_;
else
realWidth_ = 0;
}
if (height_ == MATCH_PARENT) {
if (parent_ && (parent_->realHeight_ > 0))
realHeight_ = parent_->realHeight_ - parent_->padTop_ - parent_->padBottom_;
else
realHeight_ = 0;
}
}
void Widget::setGLRenderPos(int left, int top)
@@ -205,11 +249,47 @@ SDL_Rect Widget::calcContentRect(SDL_Surface *dst, SDL_Rect *dstRect, SDL_Rect c
return rect;
}
std::tuple<int, int> Widget::baseLayout(Widget *caller)
{
int width = realWidth_, height = realHeight_;
if ((width_ == MATCH_PARENT) && parent_) {
if ((parent_->getRealWidth() <= 0) && (parent_ != caller))
parent_->layout();
width = parent_->getRealWidth() - parent_->getLeftPadding() - parent_->getRightPadding();
} else if ((width_ == WRAP_CONTENT_FILL) && parent_ && (parent_->getRealWidth() > 0)) {
width = parent_->getRealWidth() - parent_->getLeftPadding() - parent_->getRightPadding();
} else if ((width_ == WRAP_CONTENT) || (width_ == WRAP_CONTENT_FILL)) {
width = 0;
}
if ((height_ == MATCH_PARENT) && parent_) {
if ((parent_->getRealHeight() <= 0) && (parent_ != caller))
parent_->layout();
height = parent_->getRealHeight() - parent_->getTopPadding() - parent_->getBottomPadding();
} else if ((height_ == WRAP_CONTENT_FILL) && parent_ && (parent_->getRealHeight() > 0)) {
height = parent_->getRealHeight() - parent_->getTopPadding() - parent_->getBottomPadding();
} else if ((height_ == WRAP_CONTENT) || (height_ == WRAP_CONTENT_FILL)) {
height = 0;
}
printf("baseLayout: %s: %d, %d\n", name_.c_str(), width, height);
return std::make_tuple(width, height);
}
void Widget::invalidateGL()
{
renderTexValid_ = false;
if (parent_)
parent_->invalidateGL();
}
void Widget::renderToGL() const
{
const size_t NUM_TRIANGLES = 6;
if (!renderTexValid_) {
printf("Widget::renderToGL: redraw texture for %s\n", name_.c_str());
uint32_t rmask, gmask, bmask, amask;
int bpp;
if (!SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_ABGR8888,
@@ -235,7 +315,8 @@ void Widget::renderToGL() const
renderTex_->bind();
if (!renderAttribsValid_) {
int t = renderTop_, l = renderLeft_, b = renderTop_+realHeight_,
printf("Widget::renderToGL: regen attribs for %s\n", name_.c_str());
short int t = renderTop_, l = renderLeft_, b = renderTop_+realHeight_,
r = renderLeft_+realWidth_;
std::vector<VertexAttribs> vertexAttribs{
{{l, t}, {0, 0}},