Skip to content

Commit

Permalink
Fixed|UI|Client|ChoiceWidget: Several improvements to ChoiceWidget
Browse files Browse the repository at this point in the history
- The font of the selected item defined in the Style.
- Fixed addition of items into the widget.
- Popup can open to the right.
  • Loading branch information
skyjake committed Aug 20, 2013
1 parent 8c3b8fc commit 83864fe
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
14 changes: 10 additions & 4 deletions doomsday/client/data/defaultstyle.pack/fonts.dei
Expand Up @@ -61,10 +61,6 @@ font title inherits default {
weight: bold
}

group log {
font normal inherits default {}
}

group editor {
font plaintext inherits default {}
font hint inherits default {
Expand All @@ -82,3 +78,13 @@ group separator {
weight: bold
}
}

group choice {
font selected inherits default {
weight: bold
}
}

group log {
font normal inherits default {}
}
3 changes: 3 additions & 0 deletions doomsday/client/include/ui/widgets/choicewidget.h
Expand Up @@ -66,6 +66,9 @@ class ChoiceWidget : public ButtonWidget

ui::Item const &selectedItem() const;

public slots:
void openPopup();

signals:
void selectionChanged(unsigned int pos);

Expand Down
43 changes: 42 additions & 1 deletion doomsday/client/src/ui/widgets/choicewidget.cpp
Expand Up @@ -18,6 +18,7 @@

#include "ui/widgets/choicewidget.h"
#include "ui/widgets/popupmenuwidget.h"
#include "ui/signalaction.h"

using namespace de;
using namespace ui;
Expand All @@ -27,6 +28,10 @@ DENG2_OBSERVES(Context, Addition),
DENG2_OBSERVES(Context, Removal),
DENG2_OBSERVES(ContextWidgetOrganizer, WidgetCreation)
{
/**
* Items in the choice's popup uses this as action to change the selected
* item.
*/
struct SelectAction : public de::Action
{
Instance *d;
Expand All @@ -39,6 +44,8 @@ DENG2_OBSERVES(ContextWidgetOrganizer, WidgetCreation)
Action::trigger();
d->selected = d->items().find(selItem);
d->updateButtonWithSelection();
d->updateItemHighlight();
d->choices->dismiss();
}

Action *duplicate() const
Expand All @@ -51,14 +58,27 @@ DENG2_OBSERVES(ContextWidgetOrganizer, WidgetCreation)
PopupMenuWidget *choices;
Context::Pos selected; ///< One item is always selected.

Instance(Public *i) : Base(i), selected(0)
Instance(Public *i) : Base(i), selected(Context::InvalidPos)
{
self.setFont("choice.selected");

choices = new PopupMenuWidget;
choices->setAnchorAndOpeningDirection(self.hitRule(), ui::Right);
choices->menu().items().audienceForAddition += this;
choices->menu().items().audienceForRemoval += this;
choices->menu().organizer().audienceForWidgetCreation += this;
self.add(choices);

self.setAction(new SignalAction(thisPublic, SLOT(openPopup())));

updateButtonWithSelection();

updateStyle();
}

void updateStyle()
{
// todo
}

void widgetCreatedForItem(GuiWidget &widget, ui::Item const &item)
Expand Down Expand Up @@ -89,6 +109,7 @@ DENG2_OBSERVES(ContextWidgetOrganizer, WidgetCreation)
selected = 0;

updateButtonWithSelection();
return;
}

if(id <= selected)
Expand All @@ -111,8 +132,21 @@ DENG2_OBSERVES(ContextWidgetOrganizer, WidgetCreation)
}
}

void updateItemHighlight()
{
// Highlight the currently selected item.
for(Context::Pos i = 0; i < items().size(); ++i)
{
if(GuiWidget *w = choices->menu().organizer().itemWidget(i))
{
w->setFont(i == selected? "choice.selected" : "default");
}
}
}

void updateButtonWithSelection()
{
// Update the main button.
if(isValidSelection())
{
ui::Item const &item = items().at(selected);
Expand Down Expand Up @@ -149,6 +183,7 @@ void ChoiceWidget::setSelected(Context::Pos pos)
{
d->selected = pos;
d->updateButtonWithSelection();
d->updateItemHighlight();
}
}

Expand All @@ -163,6 +198,12 @@ Item const &ChoiceWidget::selectedItem() const
return d->items().at(d->selected);
}

void ChoiceWidget::openPopup()
{
d->updateItemHighlight();
d->choices->open();
}

ui::Context &ChoiceWidget::items()
{
return d->choices->menu().items();
Expand Down
12 changes: 12 additions & 0 deletions doomsday/client/src/ui/widgets/popupwidget.cpp
Expand Up @@ -468,6 +468,18 @@ void PopupWidget::glMakeGeometry(DefaultVertexBuf::Builder &verts)
v.pos = anchorPos + Vector2i(-marker, marker); tri << v;
v.pos = anchorPos + Vector2i(-marker, -marker); tri << v;
}
else if(d->dir == ui::Right)
{
v.pos = anchorPos; tri << v;
v.pos = anchorPos + Vector2i(marker, -marker); tri << v;
v.pos = anchorPos + Vector2i(marker, marker); tri << v;
}
else
{
v.pos = anchorPos; tri << v;
v.pos = anchorPos + Vector2i(marker, marker); tri << v;
v.pos = anchorPos + Vector2i(-marker, marker); tri << v;
}

verts += tri;
}
Expand Down

0 comments on commit 83864fe

Please sign in to comment.