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 public:
89  GridItemTray(GuiObject *boss, int x, int y, int w, int h, int entryID, GridWidget *grid);
90  void enableLoadButton(bool canLoad) { _loadButton->setEnabled(canLoad); }
91 
92  void reflowLayout() override;
93 
94  void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
95  void handleMouseDown(int x, int y, int button, int clickCount) override;
96  void handleMouseUp(int x, int y, int button, int clickCount) override;
97  void handleMouseWheel(int x, int y, int direction) override;
98  void handleMouseMoved(int x, int y, int button) override;
99 };
100 
101 
102 /* GridWidget */
103 class GridWidget : public ContainerWidget, public CommandSender {
104  friend class GridItemWidget;
105 public:
106  typedef bool (*FilterMatcher)(void *arg, int idx, const Common::U32String &item, const Common::U32String &token);
107 
108 protected:
112  Common::HashMap<int, Graphics::AlphaType> _platformIconsAlpha;
113  Common::HashMap<int, Graphics::AlphaType> _languageIconsAlpha;
115  Common::SharedPtr<Graphics::ManagedSurface> _disabledIconOverlay;
116  // Images are mapped by filename -> surface.
118 
119  Common::Array<GridItemInfo> _dataEntryList;
120  Common::Array<GridItemInfo> _headerEntryList;
121  Common::Array<GridItemInfo *> _sortedEntryList;
122  Common::Array<GridItemInfo *> _visibleEntryList;
123 
124  Common::String _groupingAttribute;
126  Common::Array<bool> _groupExpanded;
127  Common::U32String _groupHeaderPrefix;
128  Common::U32String _groupHeaderSuffix;
129  Common::Array<Common::U32String> _groupHeaders;
130  Common::StringMap _metadataNames;
132 
134 
135  ScrollBarWidget *_scrollBar;
136 
137  int _scrollBarWidth;
138  int _scrollWindowHeight;
139  int _scrollWindowWidth;
140  int _scrollSpeed;
141  float _scrollPos;
142  int _innerHeight;
143  int _innerWidth;
144  int _thumbnailHeight;
145  int _thumbnailWidth;
146  int _flagIconHeight;
147  int _flagIconWidth;
148  int _platformIconHeight;
149  int _platformIconWidth;
150  int _extraIconHeight;
151  int _extraIconWidth;
152  int _minGridXSpacing;
153  int _minGridYSpacing;
154  int _rows;
155  int _itemsPerRow;
156  int _firstVisibleItem;
157  int _lastVisibleItem;
158  bool _isGridInvalid;
159 
160  int _scrollWindowPaddingX;
161  int _scrollWindowPaddingY;
162  int _gridHeaderHeight;
163  int _gridHeaderWidth;
164  int _trayHeight;
165 
166  bool _multiSelectEnabled;
167 
168  FilterMatcher _filterMatcher;
169  void *_filterMatcherArg;
170 
171  // Drag to scroll
172  bool _isMouseDown;
173  bool _isDragging;
174  bool _selectionPending;
175  int _dragStartY, _dragLastY;
176  uint32 _mouseDownTime;
177  static const int kDragThreshold = 5;
178 
179  FluidScroller *_fluidScroller;
180 
181 public:
182  int _gridItemHeight;
183  int _gridItemWidth;
184  int _gridXSpacing;
185  int _gridYSpacing;
186  int _thumbnailMargin;
187 
188  bool _isTitlesVisible;
189 
190  GridItemInfo *_selectedEntry;
191 
192  Common::U32String _filter;
193 
194  GridWidget(GuiObject *boss, const Common::String &name);
195  ~GridWidget();
196 
197  Common::SharedPtr<Graphics::ManagedSurface> filenameToSurface(const Common::String &name);
198  Common::SharedPtr<Graphics::ManagedSurface> languageToSurface(Common::Language languageCode, Graphics::AlphaType &alphaType);
199  Common::SharedPtr<Graphics::ManagedSurface> platformToSurface(Common::Platform platformCode, Graphics::AlphaType &alphaType);
200  Common::SharedPtr<Graphics::ManagedSurface> demoToSurface(const Common::String &extraString, Graphics::AlphaType &alphaType);
202 
204  bool calcVisibleEntries();
205  void setEntryList(Common::Array<GridItemInfo> *list);
206  void setAttributeValues(const Common::Array<Common::U32String> &attrs);
207  void setMetadataNames(const Common::StringMap &metadata);
208  void setTitlesVisible(bool vis);
209  void markGridAsInvalid() { _isGridInvalid = true; }
210  void setGroupHeaderFormat(const Common::U32String &prefix, const Common::U32String &suffix);
211 
212  void groupEntries();
213  void sortGroups();
214  bool groupExpanded(int groupID) { return _groupExpanded[groupID]; }
215  void toggleGroup(int groupID);
216  void loadClosedGroups(const Common::U32String &groupName);
217  void saveClosedGroups(const Common::U32String &groupName);
218 
219  void reloadThumbnails();
220  void loadFlagIcons();
221  void loadPlatformIcons();
222  void loadExtraIcons();
223 
224  void destroyItems();
225  void calcInnerHeight();
226  void calcEntrySizes();
227  void updateGrid();
228  void move(int x, int y);
229  void scrollToEntry(int id, bool forceToTop);
230  void assignEntriesToItems();
231 
232  int getItemPos(int item);
233  int getNewSel(int index);
234  int getVisualPos(int entryID) const;
235  void selectVisualRange(int startPos, int endPos);
236  float getScrollPos() const { return _scrollPos; }
237  int getSelected() const { return ((_selectedEntry == nullptr) ? -1 : _selectedEntry->entryID); }
238  int getThumbnailHeight() const { return _thumbnailHeight; }
239  int getThumbnailWidth() const { return _thumbnailWidth; }
240 
241  void handleMouseWheel(int x, int y, int direction) override;
242  void handleMouseDown(int x, int y, int button, int clickCount) override;
243  void handleMouseUp(int x, int y, int button, int clickCount) override;
244  void handleMouseMoved(int x, int y, int button) override;
245  void handleTickle() override;
246  void applyScrollPos(); // Updates the grid's visual elements to match current scroll position
247  void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
248  void reflowLayout() override;
249 
250  bool wantsFocus() override { return true; }
251 
252  bool handleKeyDown(Common::KeyState state) override;
253  bool handleKeyUp(Common::KeyState state) override;
254  void openTrayAtSelected();
255  void scrollBarRecalc();
256 
257  void setSelected(int id);
258  void setFilter(const Common::U32String &filter);
259  void setFilterMatcher(FilterMatcher matcher, void *arg) { _filterMatcher = matcher; _filterMatcherArg = arg; }
260 
261  // Multi-selection methods
262  void setMultiSelectEnabled(bool enabled) { _multiSelectEnabled = enabled; }
263  bool isMultiSelectEnabled() const { return _multiSelectEnabled; }
264  Common::Array<bool> _selectedItems;
265  int _lastSelectedEntryID = -1;
266  bool isItemSelected(int entryID) const;
267  void markSelectedItem(int entryID, bool state);
268  void clearSelection();
269  const Common::Array<bool> &getSelectedItems() const { return _selectedItems; }
270 };
271 
272 /* GridItemWidget */
274 protected:
276  Graphics::AlphaType _thumbAlpha;
277 
278  GridItemInfo *_activeEntry;
279  GridWidget *_grid;
280  bool _isHighlighted;
281 
282 public:
283  GridItemWidget(GridWidget *boss);
284 
285  void move(int x, int y);
286  void update();
287  void updateThumb();
288  void setActiveEntry(GridItemInfo &entry);
289 
290  void drawWidget() override;
291 
292  void handleMouseWheel(int x, int y, int direction) override;
293  void handleMouseEntered(int button) override;
294  void handleMouseLeft(int button) override;
295  void handleMouseDown(int x, int y, int button, int clickCount) override;
296  void handleMouseUp(int x, int y, int button, int clickCount) override;
297  void handleMouseMoved(int x, int y, int button) override;
298  void doSelection();
299 };
300 
301 } // End of namespace GUI
302 
303 #endif
Definition: grid.h:51
Definition: str.h:59
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
Definition: grid.h:273
Definition: array.h:52
Definition: grid.h:103
Definition: widget.h:307
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:517
FilterMatcher _filterMatcher
Flag for multi-selection.
Definition: grid.h:168
Definition: object.h:40
Platform
Definition: platform.h:93
Language
Definition: language.h:45