/
savelistwidget.cpp
199 lines (172 loc) · 6.26 KB
/
savelistwidget.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
/** @file savelistwidget.cpp List showing the available saves of a game.
*
* @authors Copyright (c) 2016-2017 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/home/savelistwidget.h"
#include "ui/home/gamepanelbuttonwidget.h"
#include "ui/savelistdata.h"
#include <doomsday/game.h>
#include <doomsday/GameStateFolder>
#include <de/CallbackAction>
#include <de/DocumentPopupWidget>
#include <de/FileSystem>
using namespace de;
DENG_GUI_PIMPL(SaveListWidget)
, DENG2_OBSERVES(ChildWidgetOrganizer, WidgetUpdate)
{
/**
* Handles mouse button doubleclicks on the save items.
*/
struct DoubleClickHandler : public GuiWidget::IEventHandler
{
SaveListWidget::Impl *d;
DoubleClickHandler(SaveListWidget::Impl *d) : d(d) {}
bool handleEvent(GuiWidget &button, Event const &event)
{
if (event.type() == Event::MouseButton)
{
MouseEvent const &mouse = event.as<MouseEvent>();
// Check for right-clicking.
if (mouse.button() == MouseEvent::Right)
{
switch (button.handleMouseClick(event, MouseEvent::Right))
{
case MouseClickStarted:
return true;
case MouseClickAborted:
return true;
case MouseClickFinished:
saveRightClicked(button);
return true;
default:
return false; // Ignore.
}
}
if (button.hitTest(mouse) && mouse.state() == MouseEvent::DoubleClick)
{
emit d->self().doubleClicked(d->self().items().find(
*d->self().organizer().findItemForWidget(button)));
return true;
}
}
return false;
}
void saveRightClicked(GuiWidget &saveButton)
{
auto const &saveItem = d->self().organizer()
.findItemForWidget(saveButton)->as<SaveListData::SaveItem>();
if (GameStateFolder const *saved = FS::tryLocate<GameStateFolder>(saveItem.savePath()))
{
auto *docPop = new DocumentPopupWidget;
docPop->setDeleteAfterDismissed(true);
docPop->setCloseButtonVisible(true);
docPop->setAnchorAndOpeningDirection(saveButton.rule(), ui::Right);
docPop->document().setText(saved->metadata().asStyledText());
saveButton.add(docPop);
docPop->open();
}
}
};
GamePanelButtonWidget &owner;
ui::DataPos selected = ui::Data::InvalidPos;
Impl(Public *i, GamePanelButtonWidget &owner) : Base(i), owner(owner)
{
self().organizer().audienceForWidgetUpdate() += this;
}
void widgetUpdatedForItem(GuiWidget &widget, ui::Item const &item)
{
auto &button = widget.as<ButtonWidget>();
button.setTextAlignment(ui::AlignRight);
button.setAlignment(ui::AlignLeft);
button.setTextLineAlignment(ui::AlignLeft);
button.setSizePolicy(ui::Filled, ui::Expand);
button.setText(item.label());
button.margins().set("dialog.gap");
button.set(Background(Vector4f()));
button.setActionFn([this, &button] () {
toggleSelectedItem(button);
emit owner.mouseActivity();
});
button.addEventHandler(new DoubleClickHandler(this));
auto const &saveItem = item.as<SaveListData::SaveItem>();
button.setImage(style().images().image(Game::logoImageForId(saveItem.gameId())));
button.setOverrideImageSize(style().fonts().font("default").height().value() * 1.4f);
}
void toggleSelectedItem(ButtonWidget &button)
{
auto const buttonItemPos = self().items().find(*self().organizer().findItemForWidget(button));
if (selected == buttonItemPos)
{
// Unselect.
selected = ui::Data::InvalidPos;
updateItemHighlights(nullptr);
}
else
{
selected = buttonItemPos;
updateItemHighlights(&button);
}
emit self().selectionChanged(selected);
}
void updateItemHighlights(ButtonWidget *selectedButton)
{
for (auto *w : self().childWidgets())
{
if (auto *bw = w->maybeAs<ButtonWidget>())
{
if (selectedButton == bw)
{
bw->useInfoStyle();
bw->set(Background(style().colors().colorf("inverted.background")));
}
else
{
bw->useNormalStyle();
bw->set(Background());
}
}
}
}
};
SaveListWidget::SaveListWidget(GamePanelButtonWidget &owner)
: d(new Impl(this, owner))
{
setGridSize(1, ui::Filled, 0, ui::Expand);
enableScrolling(false);
enablePageKeys(false);
}
ui::DataPos SaveListWidget::selectedPos() const
{
return d->selected;
}
void SaveListWidget::setSelectedPos(ui::DataPos pos)
{
if (d->selected != pos)
{
d->selected = pos;
d->updateItemHighlights(&itemWidget<ButtonWidget>(items().at(pos)));
emit selectionChanged(d->selected);
}
}
void SaveListWidget::clearSelection()
{
if (d->selected != ui::Data::InvalidPos)
{
d->selected = ui::Data::InvalidPos;
d->updateItemHighlights(nullptr);
emit selectionChanged(d->selected);
}
}