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  GridItemWidget *_highlightedItem = nullptr;
184 
185 public:
186  int _gridItemHeight;
187  int _gridItemWidth;
188  int _gridXSpacing;
189  int _gridYSpacing;
190  int _thumbnailMargin;
191 
192  bool _isTitlesVisible;
193 
194  GridItemInfo *_selectedEntry;
195 
196  Common::U32String _filter;
197 
198  GridWidget(GuiObject *boss, const Common::String &name);
199  ~GridWidget();
200 
201  Common::SharedPtr<Graphics::ManagedSurface> filenameToSurface(const Common::String &name);
202  Common::SharedPtr<Graphics::ManagedSurface> languageToSurface(Common::Language languageCode, Graphics::AlphaType &alphaType);
203  Common::SharedPtr<Graphics::ManagedSurface> platformToSurface(Common::Platform platformCode, Graphics::AlphaType &alphaType);
204  Common::SharedPtr<Graphics::ManagedSurface> demoToSurface(const Common::String &extraString, Graphics::AlphaType &alphaType);
206 
208  bool calcVisibleEntries();
209  void setEntryList(Common::Array<GridItemInfo> *list);
210  void setAttributeValues(const Common::Array<Common::U32String> &attrs);
211  void setMetadataNames(const Common::StringMap &metadata);
212  void setTitlesVisible(bool vis);
213  void markGridAsInvalid() { _isGridInvalid = true; }
214  void setGroupHeaderFormat(const Common::U32String &prefix, const Common::U32String &suffix);
215 
216  void groupEntries();
217  void sortGroups();
218  bool groupExpanded(int groupID) { return _groupExpanded[groupID]; }
219  void toggleGroup(int groupID);
220  void loadClosedGroups(const Common::U32String &groupName);
221  void saveClosedGroups(const Common::U32String &groupName);
222 
223  void reloadThumbnails();
224  void loadFlagIcons();
225  void loadPlatformIcons();
226  void loadExtraIcons();
227 
228  void destroyItems();
229  void calcInnerHeight();
230  void calcEntrySizes();
231  void updateGrid();
232  void move(int x, int y);
233  void scrollToEntry(int id, bool forceToTop);
234  void assignEntriesToItems();
235 
236  int getItemPos(int item);
237  int getNewSel(int index);
238  int getVisualPos(int entryID) const;
239  void selectVisualRange(int startPos, int endPos);
240  float getScrollPos() const { return _scrollPos; }
241  int getSelected() const { return ((_selectedEntry == nullptr) ? -1 : _selectedEntry->entryID); }
242  int getThumbnailHeight() const { return _thumbnailHeight; }
243  int getThumbnailWidth() const { return _thumbnailWidth; }
244 
245  void handleMouseWheel(int x, int y, int direction) override;
246  void handleMouseDown(int x, int y, int button, int clickCount) override;
247  void handleMouseUp(int x, int y, int button, int clickCount) override;
248  void handleMouseMoved(int x, int y, int button) override;
249  void handleTickle() override;
250  void applyScrollPos(); // Updates the grid's visual elements to match current scroll position
251  void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
252  void reflowLayout() override;
253 
254  bool wantsFocus() override { return true; }
255 
256  void lostFocusWidget() override;
257  bool handleKeyDown(Common::KeyState state) override;
258  bool handleKeyUp(Common::KeyState state) override;
259  void openTrayAtSelected();
260  void scrollBarRecalc();
261 
262  void setSelected(int id);
263  void setFilter(const Common::U32String &filter);
264  void setFilterMatcher(FilterMatcher matcher, void *arg) { _filterMatcher = matcher; _filterMatcherArg = arg; }
265 
266  // Multi-selection methods
267  void setMultiSelectEnabled(bool enabled) { _multiSelectEnabled = enabled; }
268  bool isMultiSelectEnabled() const { return _multiSelectEnabled; }
269  Common::Array<bool> _selectedItems;
270  int _lastSelectedEntryID = -1;
271  bool isItemSelected(int entryID) const;
272  void markSelectedItem(int entryID, bool state);
273  void clearSelection();
274  const Common::Array<bool> &getSelectedItems() const { return _selectedItems; }
275 };
276 
277 /* GridItemWidget */
279 protected:
281  Graphics::AlphaType _thumbAlpha;
282 
283  GridItemInfo *_activeEntry;
284  GridWidget *_grid;
285  bool _isHighlighted;
286 
287 public:
288  GridItemWidget(GridWidget *boss);
289 
290  void move(int x, int y);
291  void update();
292  void updateThumb();
293  void setActiveEntry(GridItemInfo &entry);
294 
295  void drawWidget() override;
296 
297  void handleMouseWheel(int x, int y, int direction) override;
298  void handleMouseEntered(int button) override;
299  void handleMouseLeft(int button) override;
300  void handleMouseDown(int x, int y, int button, int clickCount) override;
301  void handleMouseUp(int x, int y, int button, int clickCount) override;
302  void handleMouseMoved(int x, int y, int button) override;
303  void doSelection();
304 };
305 
306 } // End of namespace GUI
307 
308 #endif
Definition: grid.h:51
Definition: str.h:59
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
Definition: grid.h:278
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