Skip to content

Commit

Permalink
Client|MenuWidget: Menu layout that expands in both directions
Browse files Browse the repository at this point in the history
Fixed or unlimited number of columns/rows.
  • Loading branch information
skyjake committed Jun 20, 2013
1 parent 8fe9d06 commit b95a96f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
15 changes: 13 additions & 2 deletions doomsday/client/include/ui/widgets/menuwidget.h
Expand Up @@ -25,8 +25,9 @@
/**
* Menu with an N-by-M grid of items (child widgets).
*
* One of the dimensions of the grid can be configured to be Unlimited, but
* then the children must manage their size on that axis by themselves.
* One of the dimensions of the grid can be configured to use ui::Expand
* policy, but then the child widgets must manage their size on that axis by
* themselves.
*/
class MenuWidget : public ScrollAreaWidget
{
Expand Down Expand Up @@ -73,6 +74,16 @@ class MenuWidget : public ScrollAreaWidget
*/
void updateLayout();

/**
* Constructs a rule for the width of a column, based on the widths of the
* items in the column.
*
* @param column Column index.
*
* @return Width rule with a reference count of one (given to the caller).
*/
de::Rule const *newColumnWidthRule(int column) const;

// Events.
void update();

Expand Down
39 changes: 32 additions & 7 deletions doomsday/client/src/ui/widgets/menuwidget.cpp
Expand Up @@ -69,12 +69,18 @@ DENG2_PIMPL(MenuWidget)
pos.x = ordinal % cols->valuei();
pos.y = ordinal / cols->valuei();
}
else
else if(rows->valuei() > 0)
{
DENG2_ASSERT(rowPolicy != Expand);
pos.x = ordinal % rows->valuei();
pos.y = ordinal / rows->valuei();
}
else
{
DENG2_ASSERT(cols->valuei() > 0);
pos.x = ordinal % cols->valuei();
pos.y = ordinal / cols->valuei();
}
return pos;
}

Expand Down Expand Up @@ -216,6 +222,8 @@ void MenuWidget::setGridSize(int columns, ui::SizePolicy columnPolicy,

d->colPolicy = columnPolicy;
d->rowPolicy = rowPolicy;

d->needLayout = true;
}

ButtonWidget *MenuWidget::addItem(String const &styledText, Action *action)
Expand Down Expand Up @@ -250,6 +258,8 @@ int MenuWidget::count() const

void MenuWidget::updateLayout()
{
if(!d->needLayout) return;

Rule const *baseVert = holdRef(&contentRule().top());

Vector2i gridSize = d->countGrid();
Expand All @@ -271,9 +281,16 @@ void MenuWidget::updateLayout()
.setInput(Rule::Left, previous? previous->rule().right() : *baseHoriz)
.setInput(Rule::Width, *d->colWidth);
}
else if(d->colPolicy == Fixed)
else //if(d->colPolicy == Fixed)
{
widget->rule().setInput(Rule::Left, contentRule().left() + *d->colWidth * Const(col));
if(col > 0)
{
widget->rule().setInput(Rule::Left, contentRule().left() + *d->colWidth * Const(col));
}
else
{
widget->rule().setInput(Rule::Left, contentRule().left());
}
}

if(d->rowPolicy == Filled)
Expand Down Expand Up @@ -318,7 +335,9 @@ void MenuWidget::updateLayout()
}
else
{
setContentWidth(*d->totalWidth());
Rule const *width = d->totalWidth();
setContentWidth(*width);
rule().setInput(Rule::Width, *width + *d->margin * 2);
}

if(d->rowPolicy != Expand)
Expand All @@ -338,10 +357,16 @@ void MenuWidget::updateLayout()
d->needLayout = false;
}

void MenuWidget::update()
Rule const *MenuWidget::newColumnWidthRule(int column) const
{
if(d->needLayout)
if(d->colPolicy != Filled)
{
updateLayout();
return holdRef(d->fullColumnWidth(column));
}
return holdRef(d->colWidth);
}

void MenuWidget::update()
{
updateLayout();
}

0 comments on commit b95a96f

Please sign in to comment.