/
popupmenuwidget.cpp
477 lines (413 loc) · 14.7 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/** @file popupmenuwidget.cpp
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/PopupMenuWidget"
#include "de/MenuWidget"
#include "de/ToggleWidget"
#include "de/GuiRootWidget"
#include "de/ChildWidgetOrganizer"
#include "de/AtlasProceduralImage"
#include "de/ui/Item"
#include "de/App"
#include <de/IndirectRule>
#include <de/OperatorRule>
namespace de {
static String const VAR_SHOW_ANNOTATIONS("ui.showAnnotations");
DENG_GUI_PIMPL(PopupMenuWidget)
, DENG2_OBSERVES(ButtonWidget, StateChange)
, DENG2_OBSERVES(ButtonWidget, Triggered)
, DENG2_OBSERVES(ChildWidgetOrganizer, WidgetCreation)
, DENG2_OBSERVES(ChildWidgetOrganizer, WidgetUpdate)
, DENG2_OBSERVES(Variable, Change)
{
class HeadingOverlayImage : public ProceduralImage
{
public:
HeadingOverlayImage(GuiWidget &owner)
: _owner(owner)
, _id(Id::None)
{
if(_owner.hasRoot())
{
// We can set this up right away.
alloc();
}
}
GuiRootWidget &root()
{
return _owner.root();
}
void alloc()
{
_id = root().solidWhitePixel();
setSize(Vector2f(1, 1)); //root().atlas().imageRect(_id).size());
}
void glInit()
{
alloc();
}
void glDeinit()
{
_id = Id::None;
}
void glMakeGeometry(DefaultVertexBuf::Builder &verts, Rectanglef const &rect)
{
if(!_id.isNone())
{
Rectanglef visible = rect;
visible.setWidth(_owner.rule().width().value());
verts.makeQuad(visible, color(), root().atlas().imageRectf(_id));
}
}
private:
GuiWidget &_owner;
Id _id;
};
bool infoStyle = false;
ButtonWidget *hover;
int oldScrollY;
Rule const *widestItem;
IndirectRule *maxItemWidth;
Instance(Public *i)
: Base(i)
, hover(0)
, oldScrollY(0)
, widestItem(0)
, maxItemWidth(0)
{
maxItemWidth = new IndirectRule;
App::config(VAR_SHOW_ANNOTATIONS).audienceForChange() += this;
}
~Instance()
{
App::config(VAR_SHOW_ANNOTATIONS).audienceForChange() -= this;
releaseRef(maxItemWidth);
releaseRef(widestItem);
}
void addToMaxWidth(GuiWidget &widget)
{
maxInto(widestItem, widget.rule().width());
maxItemWidth->setSource(*widestItem);
}
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(item.semantics().testFlag(ui::Item::Annotation)? "label.altaccent" :
"label.accent");
lab.setMaximumTextWidth(*maxItemWidth);
lab.rule().setInput(Rule::Width, *maxItemWidth);
return;
}
if(LabelWidget *lab = widget.maybeAs<LabelWidget>())
{
lab->margins().set("popup.menu.margin");
addToMaxWidth(widget);
}
// 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>())
{
addToMaxWidth(widget);
setButtonColors(*b);
b->setSizePolicy(ui::Expand, ui::Expand);
if(!b->is<ToggleWidget>())
{
b->setTextGap("dialog.gap");
b->setOverrideImageSize(style().fonts().font("default").height().valuei());
}
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::Annotation))
{
if(!App::config().getb(VAR_SHOW_ANNOTATIONS))
{
widget.hide();
}
widget.margins().set("halfunit").setLeft("popup.menu.margin");
widget.setFont("separator.annotation");
}
else 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");
widget.as<LabelWidget>().setOverlayImage(0);
}
else
{
widget.margins().set("halfunit").setLeft("popup.menu.margin");
widget.setFont("separator.label");
/*
LabelWidget &lab = widget.as<LabelWidget>();
//lab.setAlignment(ui::AlignCenter);
HeadingOverlayImage *img = new HeadingOverlayImage(widget);
img->setColor(style().colors().colorf("accent"));
lab.setOverlayImage(img, ui::AlignBottomLeft);
*/
}
}
}
void setButtonColors(ButtonWidget &button)
{
bool const hovering = (hover == &button);
button.setTextColor(!hovering ^ infoStyle? "text" : "inverted.text");
button.setHoverTextColor(!hovering ^ infoStyle? "inverted.text" : "text",
ButtonWidget::ReplaceColor);
}
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 columns.
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)));
}
}
}
bool hasButtonsWithImages() const
{
foreach(Widget *child, self.menu().childWidgets())
{
if(ButtonWidget *button = child->maybeAs<ButtonWidget>())
{
// Menu item images are expected to be on the left side.
if(button->hasImage() && button->textAlignment() == ui::AlignRight)
return true;
}
}
return false;
}
/**
* Adjusts the left margins of clickable items so that icons are aligned by their
* text, with the possible icon hanging on the left. If there are no items with
* icons, no extra padding is applied.
*/
void updateItemMargins()
{
bool const useExtraPadding = hasButtonsWithImages();
auto const &padding = style().rules().rule("popup.menu.paddedmargin");
auto const &none = style().rules().rule("popup.menu.margin");
foreach(Widget *child, self.menu().childWidgets())
{
GuiWidget &widget = child->as<GuiWidget>();
// Pad annotations with the full amount.
if(LabelWidget *label = widget.maybeAs<LabelWidget>())
{
ui::Item const *item = self.menu().organizer().findItemForWidget(widget);
if(item->semantics().testFlag(ui::Item::Annotation))
{
if(useExtraPadding)
{
label->setMaximumTextWidth(*maxItemWidth - padding);
widget.margins().setLeft(padding);
}
else
{
label->setMaximumTextWidth(*maxItemWidth);
widget.margins().setLeft(none);
}
}
}
// Pad buttons according to their image size.
if(ButtonWidget *button = widget.maybeAs<ButtonWidget>())
{
if(useExtraPadding)
{
Rule const *padRule = holdRef(padding);
if(button->hasImage() && button->textAlignment() == ui::AlignRight)
{
LabelWidget::ContentLayout layout;
button->contentLayout(layout);
sumInto(padRule, -Const(layout.image.width()) -
style().rules().rule(button->textGap()));
}
widget.margins().setLeft(*padRule);
releaseRef(padRule);
}
else
{
widget.margins().setLeft(none);
}
}
}
}
void buttonStateChanged(ButtonWidget &button, ButtonWidget::State state)
{
button.setImageColor(style().colors().colorf
((state != ButtonWidget::Up) ^ infoStyle? "inverted.text" :
"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();
}
// Clip the highlight to the main popup area.
return hi & self.rule().recti();
}
void buttonActionTriggered(ButtonWidget &)
{
// The popup menu is closed when an action is triggered.
self.close();
}
void updateIfScrolled()
{
// If the menu is scrolled, we need to update some things.
int scrollY = self.menu().scrollPositionY().valuei();
if(scrollY == oldScrollY)
{
return;
}
oldScrollY = scrollY;
//qDebug() << "menu scrolling" << scrollY;
// Resend the mouse position so the buttons realize they've moved.
root().dispatchLatestMousePosition();
self.requestGeometry();
}
void variableValueChanged(Variable &, Value const &newValue)
{
bool changed = false;
// Update widgets of annotation items.
self.items().forAll([this, &newValue, &changed] (ui::Item const &item) {
if(item.semantics().testFlag(ui::Item::Annotation)) {
self.menu().itemWidget<GuiWidget>(item).show(newValue.isTrue());
changed = true;
}
return LoopContinue;
});
if(changed)
{
self.menu().updateLayout();
}
}
void updateButtonColors()
{
for(Widget *w : self.menu().childWidgets())
{
if(ButtonWidget *btn = w->maybeAs<ButtonWidget>())
{
setButtonColors(*btn);
}
}
}
};
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::useInfoStyle(bool yes)
{
PopupWidget::useInfoStyle(yes);
d->infoStyle = yes;
d->updateButtonColors();
}
void PopupMenuWidget::update()
{
PopupWidget::update();
d->updateIfScrolled();
}
void PopupMenuWidget::glMakeGeometry(DefaultVertexBuf::Builder &verts)
{
PopupWidget::glMakeGeometry(verts);
if(d->hover && d->hover->isEnabled())
{
verts.makeQuad(d->highlightRect(),
d->hover->state() == ButtonWidget::Hover?
style().colors().colorf(!d->infoStyle? "inverted.background" : "background") :
style().colors().colorf(!d->infoStyle? "accent" : "inverted.accent"),
root().atlas().imageRectf(root().solidWhitePixel()).middle());
}
}
void PopupMenuWidget::preparePanelForOpening()
{
// Redo the layout.
menu().updateLayout();
d->updateItemHitRules();
d->updateItemMargins();
// Make sure the menu doesn't go beyond the top of the view.
if(openingDirection() == ui::Up)
{
menu().rule().setInput(Rule::Height,
OperatorRule::minimum(menu().contentRule().height() + menu().margins().height(),
anchorY() - menu().margins().top()));
}
PopupWidget::preparePanelForOpening();
}
void PopupMenuWidget::panelClosing()
{
PopupWidget::panelClosing();
if(d->hover)
{
auto &btn = *d->hover;
d->hover = 0;
d->setButtonColors(btn);
btn.setImageColor(style().colors().colorf(!d->infoStyle? "text" : "inverted.text"));
requestGeometry();
}
menu().dismissPopups();
}
} // namespace de