/
popupmenuwidget.cpp
208 lines (178 loc) · 6.26 KB
/
popupmenuwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/** @file popupmenuwidget.cpp
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "ui/widgets/popupmenuwidget.h"
#include "ui/widgets/guirootwidget.h"
#include "ui/widgets/menuwidget.h"
#include "ui/widgets/contextwidgetorganizer.h"
#include "ui/widgets/item.h"
using namespace de;
DENG2_PIMPL(PopupMenuWidget),
DENG2_OBSERVES(ButtonWidget, StateChange),
DENG2_OBSERVES(ButtonWidget, Triggered),
DENG2_OBSERVES(ContextWidgetOrganizer, WidgetCreation),
DENG2_OBSERVES(ContextWidgetOrganizer, WidgetUpdate)
{
ButtonWidget *hover;
Rectanglei hoverHighlightRect;
Instance(Public *i) : Base(i), hover(0) {}
void widgetCreatedForItem(GuiWidget &widget, ui::Item const &item)
{
// Popup menu items' background is provided by the popup.
widget.set(Background());
if(item.semantics().testFlag(ui::Item::Separator))
{
LabelWidget &lab = widget.as<LabelWidget>();
lab.setTextColor("label.accent");
return;
}
// Customize buttons for use in the popup. We will observe the button
// state for highlighting and possibly close the popup when an action
// gets triggered.
if(ButtonWidget *b = widget.maybeAs<ButtonWidget>())
{
b->setSizePolicy(ui::Expand, ui::Expand);
b->margins().set("unit");
b->audienceForStateChange += this;
// Triggered actions close the menu.
if(item.semantics().testFlag(ui::Item::ActivationClosesPopup))
{
b->audienceForTriggered += this;
}
}
}
void widgetUpdatedForItem(GuiWidget &widget, ui::Item const &item)
{
if(item.semantics().testFlag(ui::Item::Separator))
{
// The label of a separator may change.
if(item.label().isEmpty())
{
widget.margins().set("");
widget.setFont("separator.empty");
}
else
{
widget.margins().set("halfunit");
widget.setFont("separator.label");
}
}
}
void updateItemHitRules()
{
GridLayout const &layout = self.menu().layout();
foreach(Widget *child, self.menu().childWidgets())
{
GuiWidget &widget = child->as<GuiWidget>();
if(self.menu().isWidgetPartOfMenu(widget))
{
Vector2i cell = layout.widgetPos(widget);
DENG2_ASSERT(cell.x >= 0 && cell.y >= 0);
// We want items to be hittable throughout the width of the menu,
// however restrict this to the item's column if there are multiple.
widget.hitRule()
.setInput(Rule::Left, (!cell.x? self.rule().left() :
layout.columnLeft(cell.x)))
.setInput(Rule::Right, (cell.x == layout.gridSize().x - 1? self.rule().right() :
layout.columnRight(cell.x)));
}
}
}
void buttonStateChanged(ButtonWidget &button, ButtonWidget::State state)
{
// Update button style.
if(state == ButtonWidget::Up)
{
button.setTextColor("text");
}
else
{
button.setTextColor("inverted.text");
}
// Position item highlight.
if(&button == hover && state == ButtonWidget::Up)
{
hover = 0;
self.requestGeometry();
return;
}
if(state == ButtonWidget::Hover || state == ButtonWidget::Down)
{
hover = &button;
self.requestGeometry();
}
}
Rectanglei highlightRect() const
{
Rectanglei hi;
if(hover)
{
hi.topLeft.x = hover->hitRule().left().valuei();
hi.topLeft.y = hover->hitRule().top().valuei();
hi.bottomRight.x = hover->hitRule().right().valuei();
hi.bottomRight.y = hover->hitRule().bottom().valuei();
}
return hi;
}
void buttonActionTriggered(ButtonWidget &)
{
// The popup menu is closed when an action is triggered.
self.close();
}
};
PopupMenuWidget::PopupMenuWidget(String const &name)
: PopupWidget(name), d(new Instance(this))
{
setContent(new MenuWidget(name.isEmpty()? "" : name + "-content"));
menu().setGridSize(1, ui::Expand, 0, ui::Expand);
menu().organizer().audienceForWidgetCreation += d;
menu().organizer().audienceForWidgetUpdate += d;
}
MenuWidget &PopupMenuWidget::menu() const
{
return static_cast<MenuWidget &>(content());
}
void PopupMenuWidget::glMakeGeometry(DefaultVertexBuf::Builder &verts)
{
PopupWidget::glMakeGeometry(verts);
if(d->hover)
{
verts.makeQuad(d->highlightRect(),
d->hover->state() == ButtonWidget::Hover?
style().colors().colorf("inverted.background") :
style().colors().colorf("accent"),
root().atlas().imageRectf(root().solidWhitePixel()).middle());
}
}
void PopupMenuWidget::preparePopupForOpening()
{
// Redo the layout.
menu().updateLayout();
d->updateItemHitRules();
PopupWidget::preparePopupForOpening();
}
void PopupMenuWidget::popupClosing()
{
PopupWidget::popupClosing();
if(d->hover)
{
d->hover->setTextColor("text");
d->hover = 0;
requestGeometry();
}
menu().dismissPopups();
}