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 
39 enum {
40  kPlayButtonCmd = 'PLAY',
41  kEditButtonCmd = 'EDIT',
42  kLoadButtonCmd = 'LOAD',
43  kOpenTrayCmd = 'OPTR',
44  kItemClicked = 'LBX1',
45  kItemDoubleClickedCmd = 'LBX2',
46  kItemSizeCmd = 'SIZE'
47 };
48 
49 /* GridItemInfo */
50 struct GridItemInfo {
51  bool isHeader, validEntry;
52  int entryID;
53  Common::String engineid;
54  Common::String gameid;
55  Common::String title;
56  Common::String description;
57  Common::String extra;
58  Common::String thumbPath;
59  // Generic attribute value, may be any piece of metadata
60  Common::String attribute;
61  Common::Language language;
62  Common::Platform platform;
63 
64  int32 x, y, w, h;
65 
66  GridItemInfo(int id, const Common::String &eid, const Common::String &gid, const Common::String &t,
67  const Common::String &d, const Common::String &e, Common::Language l, Common::Platform p, bool v)
68  : entryID(id), gameid(gid), engineid(eid), title(t), description(d), extra(e), language(l), platform(p), validEntry(v), isHeader(false) {
69  thumbPath = Common::String::format("icons/%s-%s.png", engineid.c_str(), gameid.c_str());
70  }
71 
72  GridItemInfo(const Common::String &groupHeader, int groupID) : title(groupHeader), description(groupHeader),
73  isHeader(true), validEntry(true), entryID(groupID), language(Common::UNK_LANG), platform(Common::kPlatformUnknown) {
74  thumbPath = Common::String("");
75  }
76 };
77 
78 /* GridItemTray */
79 class GridItemTray: public Dialog, public CommandSender {
80  int _entryID;
81  GridWidget *_grid;
82  GuiObject *_boss;
83  PicButtonWidget *_playButton;
84  PicButtonWidget *_loadButton;
85  PicButtonWidget *_editButton;
86 public:
87  GridItemTray(GuiObject *boss, int x, int y, int w, int h, int entryID, GridWidget *grid);
88 
89  void reflowLayout() override;
90 
91  void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
92  void handleMouseDown(int x, int y, int button, int clickCount) override;
93  void handleMouseUp(int x, int y, int button, int clickCount) override;
94  void handleMouseWheel(int x, int y, int direction) override;
95  void handleMouseMoved(int x, int y, int button) override;
96 };
97 
98 
99 /* GridWidget */
100 class GridWidget : public ContainerWidget, public CommandSender {
101 public:
102  typedef bool (*FilterMatcher)(void *arg, int idx, const Common::U32String &item, const Common::U32String &token);
103 
104 protected:
108  Common::HashMap<int, Graphics::AlphaType> _platformIconsAlpha;
109  Common::HashMap<int, Graphics::AlphaType> _languageIconsAlpha;
111  Graphics::ManagedSurface *_disabledIconOverlay;
112  // Images are mapped by filename -> surface.
114 
115  Common::Array<GridItemInfo> _dataEntryList;
116  Common::Array<GridItemInfo> _headerEntryList;
117  Common::Array<GridItemInfo *> _sortedEntryList;
118  Common::Array<GridItemInfo *> _visibleEntryList;
119 
120  Common::String _groupingAttribute;
122  Common::Array<bool> _groupExpanded;
123  Common::U32String _groupHeaderPrefix;
124  Common::U32String _groupHeaderSuffix;
125  Common::Array<Common::U32String> _groupHeaders;
126  Common::StringMap _metadataNames;
128 
130 
131  ScrollBarWidget *_scrollBar;
132 
133  int _scrollBarWidth;
134  int _scrollWindowHeight;
135  int _scrollWindowWidth;
136  int _scrollSpeed;
137  int _scrollPos;
138  int _innerHeight;
139  int _innerWidth;
140  int _thumbnailHeight;
141  int _thumbnailWidth;
142  int _flagIconHeight;
143  int _flagIconWidth;
144  int _platformIconHeight;
145  int _platformIconWidth;
146  int _extraIconHeight;
147  int _extraIconWidth;
148  int _minGridXSpacing;
149  int _minGridYSpacing;
150  int _rows;
151  int _itemsPerRow;
152  int _firstVisibleItem;
153  int _lastVisibleItem;
154  bool _isGridInvalid;
155 
156  int _scrollWindowPaddingX;
157  int _scrollWindowPaddingY;
158  int _gridHeaderHeight;
159  int _gridHeaderWidth;
160  int _trayHeight;
161 
162  bool _multiSelectEnabled;
163 
164  FilterMatcher _filterMatcher;
165  void *_filterMatcherArg;
166 
167 public:
168  int _gridItemHeight;
169  int _gridItemWidth;
170  int _gridXSpacing;
171  int _gridYSpacing;
172  int _thumbnailMargin;
173 
174  bool _isTitlesVisible;
175 
176  GridItemInfo *_selectedEntry;
177 
178  Common::U32String _filter;
179 
180  GridWidget(GuiObject *boss, const Common::String &name);
181  ~GridWidget();
182 
183  template<typename T>
184  void unloadSurfaces(Common::HashMap<T, const Graphics::ManagedSurface *> &surfaces);
185 
186  const Graphics::ManagedSurface *filenameToSurface(const Common::String &name);
187  const Graphics::ManagedSurface *languageToSurface(Common::Language languageCode, Graphics::AlphaType &alphaType);
188  const Graphics::ManagedSurface *platformToSurface(Common::Platform platformCode, Graphics::AlphaType &alphaType);
189  const Graphics::ManagedSurface *demoToSurface(const Common::String &extraString, Graphics::AlphaType &alphaType);
190  const Graphics::ManagedSurface *disabledThumbnail();
191 
193  bool calcVisibleEntries();
194  void setEntryList(Common::Array<GridItemInfo> *list);
195  void setAttributeValues(const Common::Array<Common::U32String> &attrs);
196  void setMetadataNames(const Common::StringMap &metadata);
197  void setTitlesVisible(bool vis);
198  void markGridAsInvalid() { _isGridInvalid = true; }
199  void setGroupHeaderFormat(const Common::U32String &prefix, const Common::U32String &suffix);
200 
201  void groupEntries();
202  void sortGroups();
203  bool groupExpanded(int groupID) { return _groupExpanded[groupID]; }
204  void toggleGroup(int groupID);
205  void loadClosedGroups(const Common::U32String &groupName);
206  void saveClosedGroups(const Common::U32String &groupName);
207 
208  void reloadThumbnails();
209  void loadFlagIcons();
210  void loadPlatformIcons();
211  void loadExtraIcons();
212 
213  void destroyItems();
214  void calcInnerHeight();
215  void calcEntrySizes();
216  void updateGrid();
217  void move(int x, int y);
218  void scrollToEntry(int id, bool forceToTop);
219  void assignEntriesToItems();
220 
221  int getItemPos(int item);
222  int getNewSel(int index);
223  int getVisualPos(int entryID) const;
224  void selectVisualRange(int startPos, int endPos);
225  int getScrollPos() const { return _scrollPos; }
226  int getSelected() const { return ((_selectedEntry == nullptr) ? -1 : _selectedEntry->entryID); }
227  int getThumbnailHeight() const { return _thumbnailHeight; }
228  int getThumbnailWidth() const { return _thumbnailWidth; }
229 
230  void handleMouseWheel(int x, int y, int direction) override;
231  void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
232  void reflowLayout() override;
233 
234  bool wantsFocus() override { return true; }
235 
236  bool handleKeyDown(Common::KeyState state) override;
237  bool handleKeyUp(Common::KeyState state) override;
238  void openTrayAtSelected();
239  void scrollBarRecalc();
240 
241  void setSelected(int id);
242  void setFilter(const Common::U32String &filter);
243  void setFilterMatcher(FilterMatcher matcher, void *arg) { _filterMatcher = matcher; _filterMatcherArg = arg; }
244 
245  // Multi-selection methods
246  void setMultiSelectEnabled(bool enabled) { _multiSelectEnabled = enabled; }
247  bool isMultiSelectEnabled() const { return _multiSelectEnabled; }
248  Common::Array<bool> _selectedItems;
249  int _lastSelectedEntryID = -1;
250  bool isItemSelected(int entryID) const;
251  void markSelectedItem(int entryID, bool state);
252  void clearSelection();
253  const Common::Array<bool> &getSelectedItems() const { return _selectedItems; }
254 };
255 
256 /* GridItemWidget */
258 protected:
259  Graphics::ManagedSurface _thumbGfx;
260  Graphics::AlphaType _thumbAlpha;
261 
262  GridItemInfo *_activeEntry;
263  GridWidget *_grid;
264  bool _isHighlighted;
265 
266 public:
267  GridItemWidget(GridWidget *boss);
268 
269  void move(int x, int y);
270  void update();
271  void updateThumb();
272  void setActiveEntry(GridItemInfo &entry);
273 
274  void drawWidget() override;
275 
276  void handleMouseWheel(int x, int y, int direction) override;
277  void handleMouseEntered(int button) override;
278  void handleMouseLeft(int button) override;
279  void handleMouseDown(int x, int y, int button, int clickCount) override;
280  void handleMouseMoved(int x, int y, int button) override;
281 };
282 
283 } // End of namespace GUI
284 
285 #endif
Definition: managed_surface.h:51
Definition: grid.h:50
Definition: str.h:59
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
Definition: grid.h:257
Definition: array.h:52
Definition: grid.h:100
Definition: widget.h:307
Definition: scrollbar.h:34
Definition: printman.h:30
Definition: object.h:60
Definition: grid.h:79
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:515
FilterMatcher _filterMatcher
Flag for multi-selection.
Definition: grid.h:164
Definition: object.h:40
Platform
Definition: platform.h:93
Language
Definition: language.h:45