ScummVM API documentation
FileMenu.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 /*
23  * This code is based on the CRAB engine
24  *
25  * Copyright (c) Arvind Raja Yadav
26  *
27  * Licensed under MIT
28  *
29  */
30 
31 #ifndef CRAB_FILEMENU_H
32 #define CRAB_FILEMENU_H
33 
34 #include "common/savefile.h"
35 #include "crab/crab.h"
36 #include "crab/ui/FileData.h"
37 #include "crab/ui/ImageData.h"
38 #include "crab/ui/PageMenu.h"
39 #include "crab/ui/TextData.h"
40 
41 namespace Crab {
42 
43 namespace pyrodactyl {
44 namespace ui {
45 // Used for menus that are responsible for reading multiple files from disk
46 template<typename FileType>
47 class FileMenu {
48 protected:
49  // The background of the menu
50  ImageData _bg;
51 
52  // The collection of buttons
53  PageButtonMenu _menu;
54 
55  // The final filename that is selected
56  Common::String _selected;
57 
58  // The extension and directory used by this menu
59  Common::String _extension;
60  Common::Path _directory;
61 
62  // The save information for each slot
63  Common::Array<FileType> _slotInfo;
64  TextData tdB[DATA_BUTTON_TOTAL];
65 
66  // The titles for loc_name, difficulty, time_played and player_name
67  HoverInfo hov[DATA_HOVER_TOTAL];
68  TextData tdH[DATA_HOVER_TOTAL];
69 
70  // The preview picture details
71  struct {
72  // We load only the current preview image instead of all of them
73  pyrodactyl::image::Image _preview;
74 
75  // Fallback path if there is no preview image or if we fail to load it
76  Common::Path _noPreviewPath;
77 
78  // Position of image
79  Element _pos;
80 
81  // Is the image loaded
82  bool _loaded;
83  } _img;
84 
85  // Are we hovering over a button right now?
86  bool _hover;
87 
88  // The previously hover button
89  int _prevHover;
90 
91 public:
92  FileMenu() {
93  _img._loaded = false;
94  _hover = false;
95  _prevHover = -1;
96  }
97 
98  ~FileMenu() {
99  if (_img._loaded)
100  _img._preview.deleteImage();
101  }
102  void reset() {
103  if (_img._loaded)
104  _img._preview.deleteImage();
105  _img._loaded = false;
106  _hover = false;
107  }
108 
109  Common::String selectedPath() {
110  return _selected;
111  }
112 
113  void selectedPath(const Common::String &val) {
114  _selected = val;
115  }
116 
117  void scanDir() {
118  Common::String res = "CRAB_*";
119  res += g_engine->_filePath->_saveExt;
121 
122  _slotInfo.clear();
123  _menu.clear();
124 
125  uint countSlot = 0, countMenu = 0;
126  for (const Common::String& save : saves) {
127  _slotInfo.push_back(FileType(save));
128  _menu.add(countSlot, countMenu);
129  }
130 
131  _menu.assignPaths();
132  }
133 
134  void load(rapidxml::xml_node<char> *node) {
135  if (nodeValid("bg", node))
136  _bg.load(node->first_node("bg"));
137 
138  if (nodeValid("menu", node))
139  _menu.load(node->first_node("menu"));
140 
141  if (nodeValid("preview", node)) {
142  auto prnode = node->first_node("preview");
143  _img._pos.load(prnode);
144  loadPath(_img._noPreviewPath, "path", prnode);
145  }
146 
147  if (nodeValid("offset", node)) {
148  rapidxml::xml_node<char> *offnode = node->first_node("offset");
149 
150  // Stuff displayed on the slot button
151  tdB[DATA_SAVENAME].load(offnode->first_node("save_name"));
152  tdB[DATA_LASTMODIFIED].load(offnode->first_node("last_modified"));
153 
154  // Stuff displayed when you hover over a slot button
155  tdH[DATA_LOCNAME].load(offnode->first_node("loc_name"));
156  tdH[DATA_DIFFICULTY].load(offnode->first_node("difficulty"));
157  tdH[DATA_TIMEPLAYED].load(offnode->first_node("time_played"));
158  tdH[DATA_PLAYERNAME].load(offnode->first_node("player_name"));
159 
160  // Titles for the stuff displayed when you hover over a slot button
161  hov[DATA_LOCNAME].load(offnode->first_node("loc_name_title"));
162  hov[DATA_DIFFICULTY].load(offnode->first_node("difficulty_title"));
163  hov[DATA_TIMEPLAYED].load(offnode->first_node("time_played_title"));
164  hov[DATA_PLAYERNAME].load(offnode->first_node("player_name_title"));
165  }
166 
167  _extension = g_engine->_filePath->_saveExt;
168  _directory = g_engine->_filePath->_appdata.join(g_engine->_filePath->_saveDir);
169  scanDir();
170  }
171 
172  bool handleEvents(const Common::Event &event) {
173  int choice = _menu.handleEvents(event);
174  if (choice >= 0) {
175  _menu.reset();
176  _selected = _slotInfo[_menu.index() + choice]._path;
177  reset();
178  return true;
179  }
180 
181  return false;
182  }
183 
184  void draw() {
185  _bg.draw();
186  _menu.draw();
187  for (auto i = _menu.index(), count = 0u; i < _menu.indexPlusOne() && i < _slotInfo.size(); i++, count++) {
188  auto base_x = _menu.baseX(count), base_y = _menu.baseY(count);
189  tdB[DATA_SAVENAME].draw(_slotInfo[i]._name, base_x, base_y);
190  tdB[DATA_LASTMODIFIED].draw(_slotInfo[i]._lastModified, base_x, base_y);
191  }
192 
193  drawHover();
194  }
195 
196  void drawHover() {
197  if (_menu.hoverIndex() >= 0) {
198  int i = _menu.hoverIndex();
199 
200  if (!_img._loaded || _prevHover != i) {
201  _img._loaded = true;
202  _prevHover = i;
203  if (!_img._preview.load(Common::Path(_slotInfo[i]._preview)))
204  _img._preview.load(Common::Path(_img._noPreviewPath));
205  }
206 
207  _hover = true;
208  _img._preview.draw(_img._pos.x, _img._pos.y);
209 
210  tdH[DATA_LOCNAME].draw(_slotInfo[i]._locName);
211  tdH[DATA_DIFFICULTY].draw(_slotInfo[i]._diff);
212  tdH[DATA_TIMEPLAYED].draw(_slotInfo[i]._time);
213  tdH[DATA_PLAYERNAME].draw(_slotInfo[i]._charName);
214 
215  for (int num = 0; num < DATA_HOVER_TOTAL; ++num)
216  hov[num].draw();
217  } else if (_hover)
218  reset();
219  }
220 
221  bool empty() {
222  scanDir();
223  return _slotInfo.empty();
224  }
225 
226  bool selectNewestFile() {
227  if (_slotInfo.size() > 0) {
228  _selected = _slotInfo[0]._path;
229  return true;
230  }
231 
232  return false;
233  }
234 
235  void setUI() {
236  _bg.setUI();
237  _menu.setUI();
238  scanDir();
239  _img._pos.setUI();
240 
241  for (int i = 0; i < DATA_BUTTON_TOTAL; ++i)
242  tdB[i].setUI();
243 
244  for (int i = 0; i < DATA_HOVER_TOTAL; ++i) {
245  tdH[i].setUI();
246  hov[i].setUI();
247  }
248  }
249 };
250 
251 } // End of namespace ui
252 } // End of namespace pyrodactyl
253 
254 } // End of namespace Crab
255 
256 #endif // CRAB_FILEMENU_H
Definition: str.h:59
void clear()
Definition: array.h:320
Definition: path.h:52
Engine * g_engine
bool empty() const
Definition: array.h:351
void push_back(const T &element)
Definition: array.h:180
Definition: events.h:199
size_type size() const
Definition: array.h:315
Definition: moveeffect.h:37
Common::SaveFileManager * getSaveFileManager()
Definition: engine.h:620
virtual StringArray listSavefiles(const String &pattern)=0