Files
openglplayground/LinearLayout.cc
2015-03-09 01:11:13 +01:00

78 lines
1.8 KiB
C++

#include "LinearLayout.hh"
LinearLayout::LinearLayout(int direction, int width, int height, std::string name)
: View(width, height, std::move(name)), direction_(direction)
{
}
LinearLayout::~LinearLayout()
{
}
void LinearLayout::setChildPadding(int pad)
{
if (pad != padChildren_) {
padChildren_ = pad;
layout();
}
}
void LinearLayout::layout()
{
// TODO: Respect parent padding
if ((width_ == MATCH_PARENT) && parent_)
realWidth_ = parent_->getRealWidth();
if ((height_ == MATCH_PARENT) && parent_)
realHeight_ = parent_->getRealHeight();
if (direction_ == DIR_VERTICAL) {
int contentWidth = 0;
if (width_ == WRAP_CONTENT) {
// Determine content width
for(auto& ent : children_) {
auto& child = *(std::get<1>(ent));
assert(child.getHeight() != MATCH_PARENT);
if (child.getWidth() == MATCH_PARENT)
continue;
if (child.getWidth() == WRAP_CONTENT)
_layout(child);
contentWidth = std::max(contentWidth, child.getRealWidth());
}
assert(contentWidth > 0);
realWidth_ = contentWidth + padLeft_ + padRight_;
printf("Inner width: %d, outer width: %d\n", contentWidth, realWidth_);
}
int currentY = padTop_;
// Layout children
for(auto& ent : children_) {
auto& child = *(std::get<1>(ent));
if (child.getWidth() == MATCH_PARENT)
_layout(child);
SDL_Rect cr;
cr.x = padLeft_;
cr.y = currentY;
if ((realWidth_ - padLeft_ - padRight_) > child.getRealWidth())
cr.w = child.getRealWidth();
else
cr.w = realWidth_ - padLeft_ - padRight_;
cr.h = child.getRealHeight();
std::get<2>(ent) = cr;
currentY += child.getRealHeight() + padChildren_;
}
if (height_ == WRAP_CONTENT)
realHeight_ = currentY - padChildren_ + padBottom_;
}
invalidateGL();
}