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