ScummVM API documentation
grid.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef GUI_WIDGETS_GRID_H
23 #define GUI_WIDGETS_GRID_H
24 
25 #include "gui/dialog.h"
26 #include "gui/widgets/scrollbar.h"
27 #include "common/str.h"
28 
29 #include "image/bmp.h"
30 #include "image/png.h"
31 #include "graphics/svg.h"
32 
33 namespace GUI {
34 
35 class ScrollBarWidget;
36 class GridItemWidget;
37 class GridWidget;
38 class FluidScroller;
39 
40 enum {
41  kPlayButtonCmd = 'PLAY',
42  kEditButtonCmd = 'EDIT',
43  kLoadButtonCmd = 'LOAD',
44  kOpenTrayCmd = 'OPTR',
45  kItemClicked = 'LBX1',
46  kItemDoubleClickedCmd = 'LBX2',
47  kItemSizeCmd = 'SIZE'
48 };
49 
50 /* GridItemInfo */
51 struct GridItemInfo {
52  bool isHeader, validEntry;
53  int entryID;
54  Common::String engineid;
55  Common::String gameid;
56  Common::String title;
57  Common::String description;
58  Common::String extra;
59  Common::String thumbPath;
60  // Generic attribute value, may be any piece of metadata
61  Common::String attribute;
62  Common::Language language;
63  Common::Platform platform;
64  bool canLoadGame;
65 
66  int32 x, y, w, h;
67 
68  GridItemInfo(int id, const Common::String &eid, const Common::String &gid, const Common::String &t,
69  const Common::String &d, const Common::String &e, Common::Language l, Common::Platform p, bool v, bool cl)
70  : entryID(id), gameid(gid), engineid(eid), title(t), description(d), extra(e), language(l), platform(p), validEntry(v), canLoadGame(cl), isHeader(false) {
71  thumbPath = Common::String::format("icons/%s-%s.png", engineid.c_str(), gameid.c_str());
72  }
73 
74  GridItemInfo(const Common::String &groupHeader, int groupID) : title(groupHeader), description(groupHeader),
75  isHeader(true), validEntry(true), entryID(groupID), language(Common::UNK_LANG), platform(Common::kPlatformUnknown) {
76  thumbPath = Common::String("");
77  }
78 };
79 
80 /* GridItemTray */
81 class GridItemTray: public Dialog, public CommandSender {
82  int _entryID;
83  GridWidget *_grid;
84  GuiObject *_boss;
85  PicButtonWidget *_playButton;
86  PicButtonWidget *_loadButton;
87  PicButtonWidget *_editButton;
88 
89  bool _mouseOutside;
90 public:
91  GridItemTray(GuiObject *boss, int x, int y, int w, int h, int entryID, GridWidget *grid);
92  void enableLoadButton(bool canLoad) { _loadButton->setEnabled(canLoad); }
93 
94  void reflowLayout() override;
95 
96  void receivedFocus(int x = -1, int y = -1) override;
97  void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
98  void handleMouseDown(int x, int y, int button, int clickCount) override;
99  void handleMouseUp(int x, int y, int button, int clickCount) override;
100  void handleMouseWheel(int x, int y, int direction) override;
101  void handleMouseMoved(int x, int y, int button) override;
102 };
103 
104 
105 /* GridWidget */
106 class GridWidget : public ContainerWidget, public CommandSender {
107  friend class GridItemWidget;
108 public:
109  typedef bool (*FilterMatcher)(void *arg, int idx, const Common::U32String &item, const Common::U32String &token);
110 
111 protected:
115  Common::HashMap<int, Graphics::AlphaType> _platformIconsAlpha;
116  Common::HashMap<int, Graphics::AlphaType> _languageIconsAlpha;
118  Common::SharedPtr<Graphics::ManagedSurface> _disabledIconOverlay;
119  // Images are mapped by filename -> surface.
121 
122  Common::Array<GridItemInfo> _dataEntryList;
123  Common::Array<GridItemInfo> _headerEntryList;
124  Common::Array<GridItemInfo *> _sortedEntryList;
125  Common::Array<GridItemInfo *> _visibleEntryList;
126 
127  Common::String _groupingAttribute;
129  Common::Array<bool> _groupExpanded;
130  Common::U32String _groupHeaderPrefix;
131  Common::U32String _groupHeaderSuffix;
132  Common::Array<Common::U32String> _groupHeaders;
133  Common::StringMap _metadataNames;
135 
137 
138  ScrollBarWidget *_scrollBar;
139 
140  int _scrollBarWidth;
141  int _scrollWindowHeight;
142  int _scrollWindowWidth;
143  int _scrollSpeed;
144  float _scrollPos;
145  int _innerHeight;
146  int _innerWidth;
147  int _thumbnailHeight;
148  int _thumbnailWidth;
149  int _flagIconHeight;
150  int _flagIconWidth;
151  int _platformIconHeight;
152  int _platformIconWidth;
153  int _extraIconHeight;
154  int _extraIconWidth;
155  int _minGridXSpacing;
156  int _minGridYSpacing;
157  int _rows;
158  int _itemsPerRow;
159  int _firstVisibleItem;
160  int _lastVisibleItem;
161  bool _isGridInvalid;
162 
163  int _scrollWindowPaddingX;
164  int _scrollWindowPaddingY;
165  int _gridHeaderHeight;
166  int _gridHeaderWidth;
167  int _trayHeight;
168 
169  bool _multiSelectEnabled;
170 
171  FilterMatcher _filterMatcher;
172  void *_filterMatcherArg;
173 
174  // Drag to scroll
175  bool _isMouseDown;
176  bool _isDragging;
177  bool _selectionPending;
178  int _dragStartY, _dragLastY;
179  uint32 _mouseDownTime;
180  static const int kDragThreshold = 5;
181 
182  FluidScroller *_fluidScroller;
183  bool _wasAnimating;
184  GridItemWidget *_highlightedItem = nullptr;
185 
186 public:
187  int _gridItemHeight;
188  int _gridItemWidth;
189  int _gridXSpacing;
190  int _gridYSpacing;
191  int _thumbnailMargin;
192 
193  bool _isTitlesVisible;
194 
195  GridItemInfo *_selectedEntry;
196 
197  Common::U32String _filter;
198 
199  GridWidget(GuiObject *boss, const Common::String &name);
200  ~GridWidget();
201 
202  Common::SharedPtr<Graphics::ManagedSurface> filenameToSurface(const Common::String &name);
203  Common::SharedPtr<Graphics::ManagedSurface> languageToSurface(Common::Language languageCode, Graphics::AlphaType &alphaType);
204  Common::SharedPtr<Graphics::ManagedSurface> platformToSurface(Common::Platform platformCode, Graphics::AlphaType &alphaType);
205  Common::SharedPtr<Graphics::ManagedSurface> demoToSurface(const Common::String &extraString, Graphics::AlphaType &alphaType);
207 
209  bool calcVisibleEntries();
210  void setEntryList(Common::Array<GridItemInfo> *list);
211  void setAttributeValues(const Common::Array<Common::U32String> &attrs);
212  void setMetadataNames(const Common::StringMap &metadata);
213  void setTitlesVisible(bool vis);
214  void markGridAsInvalid() { _isGridInvalid = true; }
215  void setGroupHeaderFormat(const Common::U32String &prefix, const Common::U32String &suffix);
216 
217  void groupEntries();
218  void sortGroups();
219  bool groupExpanded(int groupID) { return _groupExpanded[groupID]; }
220  void toggleGroup(int groupID);
221  void loadClosedGroups(const Common::U32String &groupName);
222  void saveClosedGroups(const Common::U32String &groupName);
223 
224  void reloadThumbnails();
225  void loadFlagIcons();
226  void loadPlatformIcons();
227  void loadExtraIcons();
228 
229  void destroyItems();
230  void calcInnerHeight();
231  void calcEntrySizes();
232  void updateGrid();
233  void move(int x, int y);
234  void scrollToEntry(int id, bool forceToTop);
235  void assignEntriesToItems();
236 
237  int getItemPos(int item);
238  int getNewSel(int index);
239  int getVisualPos(int entryID) const;
240  void selectVisualRange(int startPos, int endPos);
241  float getScrollPos() const { return _scrollPos; }
242  int getSelected() const { return ((_selectedEntry == nullptr) ? -1 : _selectedEntry->entryID); }
243  int getThumbnailHeight() const { return _thumbnailHeight; }
244  int getThumbnailWidth() const { return _thumbnailWidth; }
245 
246  void handleMouseWheel(int x, int y, int direction) override;
247  void handleMouseDown(int x, int y, int button, int clickCount) override;
248  void handleMouseUp(int x, int y, int button, int clickCount) override;
249  void handleMouseMoved(int x, int y, int button) override;
250  void handleTickle() override;
251  void applyScrollPos(); // Updates the grid's visual elements to match current scroll position
252  void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
253  void reflowLayout() override;
254 
255  bool wantsFocus() override { return true; }
256 
257  void lostFocusWidget() override;
258  bool handleKeyDown(Common::KeyState state) override;
259  bool handleKeyUp(Common::KeyState state) override;
260  void openTrayAtSelected();
261  void scrollBarRecalc();
262 
263  void setSelected(int id);
264  void setFilter(const Common::U32String &filter);
265  void setFilterMatcher(FilterMatcher matcher, void *arg) { _filterMatcher = matcher; _filterMatcherArg = arg; }
266 
267  // Multi-selection methods
268  void setMultiSelectEnabled(bool enabled) { _multiSelectEnabled = enabled; }
269  bool isMultiSelectEnabled() const { return _multiSelectEnabled; }
270  Common::Array<bool> _selectedItems;
271  int _lastSelectedEntryID = -1;
272  bool isItemSelected(int entryID) const;
273  void markSelectedItem(int entryID, bool state);
274  void clearSelection();
275  const Common::Array<bool> &getSelectedItems() const { return _selectedItems; }
276 };
277 
278 /* GridItemWidget */
280 protected:
282  Graphics::AlphaType _thumbAlpha;
283 
284  GridItemInfo *_activeEntry;
285  GridWidget *_grid;
286  bool _isHighlighted;
287 
288 public:
289  GridItemWidget(GridWidget *boss);
290 
291  void move(int x, int y);
292  void update();
293  void updateThumb();
294  void setActiveEntry(GridItemInfo &entry);
295 
296  void drawWidget() override;
297 
298  void handleMouseWheel(int x, int y, int direction) override;
299  void handleMouseEntered(int button) override;
300  void handleMouseLeft(int button) override;
301  void handleMouseDown(int x, int y, int button, int clickCount) override;
302  void handleMouseUp(int x, int y, int button, int clickCount) override;
303  void handleMouseMoved(int x, int y, int button) override;
304  void doSelection();
305 };
306 
307 } // End of namespace GUI
308 
309 #endif
Definition: grid.h:51
Definition: str.h:59
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
Definition: grid.h:279
Definition: array.h:52
Definition: grid.h:106
Definition: widget.h:309
Definition: FluidScroll.h:34
Definition: scrollbar.h:34
Definition: printman.h:30
Definition: object.h:60
Definition: grid.h:81
Definition: hashmap.h:85
Definition: ustr.h:57
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
Definition: dialog.h:49
Definition: keyboard.h:294
Definition: widget.h:519
FilterMatcher _filterMatcher
Flag for multi-selection.
Definition: grid.h:171
Definition: object.h:40
Platform
Definition: platform.h:93
Language
Definition: language.h:45