From 83864fef26356455485dec256d58a7e0a1f95ef2 Mon Sep 17 00:00:00 2001 From: skyjake Date: Tue, 20 Aug 2013 13:22:59 +0300 Subject: [PATCH] Fixed|UI|Client|ChoiceWidget: Several improvements to ChoiceWidget - The font of the selected item defined in the Style. - Fixed addition of items into the widget. - Popup can open to the right. --- .../client/data/defaultstyle.pack/fonts.dei | 14 ++++-- .../client/include/ui/widgets/choicewidget.h | 3 ++ .../client/src/ui/widgets/choicewidget.cpp | 43 ++++++++++++++++++- .../client/src/ui/widgets/popupwidget.cpp | 12 ++++++ 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/doomsday/client/data/defaultstyle.pack/fonts.dei b/doomsday/client/data/defaultstyle.pack/fonts.dei index a3b382750f..314bbbb60b 100644 --- a/doomsday/client/data/defaultstyle.pack/fonts.dei +++ b/doomsday/client/data/defaultstyle.pack/fonts.dei @@ -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 { @@ -82,3 +78,13 @@ group separator { weight: bold } } + +group choice { + font selected inherits default { + weight: bold + } +} + +group log { + font normal inherits default {} +} diff --git a/doomsday/client/include/ui/widgets/choicewidget.h b/doomsday/client/include/ui/widgets/choicewidget.h index 108557c2f3..5783b5f7ac 100644 --- a/doomsday/client/include/ui/widgets/choicewidget.h +++ b/doomsday/client/include/ui/widgets/choicewidget.h @@ -66,6 +66,9 @@ class ChoiceWidget : public ButtonWidget ui::Item const &selectedItem() const; +public slots: + void openPopup(); + signals: void selectionChanged(unsigned int pos); diff --git a/doomsday/client/src/ui/widgets/choicewidget.cpp b/doomsday/client/src/ui/widgets/choicewidget.cpp index a97366266b..64be61b7b4 100644 --- a/doomsday/client/src/ui/widgets/choicewidget.cpp +++ b/doomsday/client/src/ui/widgets/choicewidget.cpp @@ -18,6 +18,7 @@ #include "ui/widgets/choicewidget.h" #include "ui/widgets/popupmenuwidget.h" +#include "ui/signalaction.h" using namespace de; using namespace ui; @@ -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; @@ -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 @@ -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) @@ -89,6 +109,7 @@ DENG2_OBSERVES(ContextWidgetOrganizer, WidgetCreation) selected = 0; updateButtonWithSelection(); + return; } if(id <= selected) @@ -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); @@ -149,6 +183,7 @@ void ChoiceWidget::setSelected(Context::Pos pos) { d->selected = pos; d->updateButtonWithSelection(); + d->updateItemHighlight(); } } @@ -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(); diff --git a/doomsday/client/src/ui/widgets/popupwidget.cpp b/doomsday/client/src/ui/widgets/popupwidget.cpp index 15b47d2ab9..9067ab7385 100644 --- a/doomsday/client/src/ui/widgets/popupwidget.cpp +++ b/doomsday/client/src/ui/widgets/popupwidget.cpp @@ -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; }