| 1 | LibraryManager* libraryManager = nullptr; |
| 2 | |
| 3 | LibraryBrowser::LibraryBrowser(Emulator::Interface& emulator) : emulator(emulator) { |
| 4 | setMargin(5); |
| 5 | |
| 6 | informationType.setText({ |
| 7 | "Title:\n", |
| 8 | "Serial:" |
| 9 | }); |
| 10 | |
| 11 | for(auto& media : emulator.media) { |
| 12 | mediaMode.append(media.name); |
| 13 | } |
| 14 | |
| 15 | unsigned height = Font::size(program->normalFont, " ").height; |
| 16 | |
| 17 | append(folders, {~0, ~0}, 5); |
| 18 | append(informationLayout, {~0, 0}); |
| 19 | informationLayout.append(informationType, {0, height * 2}, 5); |
| 20 | informationLayout.append(information, {~0, height * 2}, 5); |
| 21 | informationLayout.append(mediaMode, {0, 0}); |
| 22 | |
| 23 | folders.onActivate = {&LibraryBrowser::onActivate, this}; |
| 24 | folders.onChange = {&LibraryBrowser::onChange, this}; |
| 25 | mediaMode.onChange = {&LibraryBrowser::setMode, this}; |
| 26 | } |
| 27 | |
| 28 | void LibraryBrowser::onActivate() { |
| 29 | if(folders.selected() == false) return; |
| 30 | if(libraryManager->loadButton.enabled() == false) return; |
| 31 | |
| 32 | unsigned selection = folders.selection(); |
| 33 | string pathname = {this->pathname, folders.text(selection, 0), typeSuffix}; |
| 34 | |
| 35 | libraryManager->loaded.append(folders.text(selection, 0)); |
| 36 | libraryManager->setInformation(false); |
| 37 | |
| 38 | if(libraryManager->slotLoad == false) { |
| 39 | utility->loadMedia(pathname); |
| 40 | } else { |
| 41 | libraryManager->loadPathname = pathname; |
| 42 | libraryManager->setModal(false); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void LibraryBrowser::onChange() { |
| 47 | if(folders.selected() == false) return information.setText(""); |
| 48 | |
| 49 | string manifest = {pathname, folders.text(folders.selection(), 0), typeSuffix, "manifest.bml"}; |
| 50 | auto document = Markup::Document(file::read(manifest)); |
| 51 | |
| 52 | information.setText({ |
| 53 | document["information/title"].text(), "\n", |
| 54 | document["information/serial"].text() |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | void LibraryBrowser::refresh() { |
| 59 | folders.reset(); |
| 60 | lstring pathnames = directory::ifolders(pathname, typeMask); |
| 61 | unsigned selection = 0; |
| 62 | for(auto& pathname : pathnames) { |
| 63 | folders.append(string{pathname}.rtrim<1>(typeSuffix)); |
| 64 | folders.setImage(selection++, 0, {resource::game, sizeof resource::game}); |
| 65 | } |
| 66 | folders.setSelection(0); |
| 67 | onChange(); |
| 68 | } |
| 69 | |
| 70 | void LibraryBrowser::setMode() { |
| 71 | config->library.mediaMode = mediaMode.selection(); |
| 72 | auto& media = emulator.media[mediaMode.selection()]; |
| 73 | |
| 74 | pathname = {utility->libraryPath(), media.name, "/"}; |
| 75 | type = media.type; |
| 76 | typeMask = {"*.", type}; |
| 77 | typeSuffix = {".", type, "/"}; |
| 78 | |
| 79 | refresh(); |
| 80 | folders.setFocused(); |
| 81 | libraryManager->synchronize(); |
| 82 | } |
| 83 | |
| 84 | LibraryImport::LibraryImport() { |
| 85 | setMargin(5); |
| 86 | information.setText({ |
| 87 | "higan manages games in a library. To play a game, you must first import the game.\n" |
| 88 | "After doing so, the game will appear inside your library, and can then be loaded and played." |
| 89 | }); |
| 90 | importButton.setText("Import Game ..."); |
| 91 | append(information, {~0, 0}, 5); |
| 92 | append(importButton, {0, 0}); |
| 93 | |
| 94 | importButton.onActivate = {&LibraryImport::onImportActivate, this}; |
| 95 | } |
| 96 | |
| 97 | void LibraryImport::onImportActivate() { |
| 98 | if(program->ananke.open() == false) { |
| 99 | MessageWindow().setText("ananke must be installed to use this feature").warning(); |
| 100 | return; |
| 101 | } |
| 102 | function<string ()> browse = program->ananke.sym("ananke_browse"); |
| 103 | if(!browse) return; |
| 104 | audio.clear(); //ananke's browser is modal |
| 105 | string pathname = browse(); |
| 106 | pathname.rtrim<1>("/"); |
| 107 | if(pathname.empty()) return; |
| 108 | |
| 109 | //after importing game, take user to the relevant game list to show the newly imported title |
| 110 | string type = extension(pathname); |
| 111 | for(signed bootable = 1; bootable >= 0; bootable--) { |
| 112 | unsigned selection = 0; |
| 113 | for(auto& browser : libraryManager->browsers) { |
| 114 | unsigned mode = 0; |
| 115 | for(auto& media : browser->emulator.media) { |
| 116 | if(type == media.type && media.bootable == bootable) { |
| 117 | browser->mediaMode.setSelection(mode); |
| 118 | libraryManager->libraryFrame.setSelection(selection); |
| 119 | libraryManager->onChange(); |
| 120 | |
| 121 | //find game in list and select it |
| 122 | string name = notdir(nall::basename(pathname)); |
| 123 | for(unsigned n = 0; n < browser->folders.rows(); n++) { |
| 124 | if(browser->folders.text(n, 0) == name) { |
| 125 | browser->folders.setSelection(n); |
| 126 | browser->onChange(); |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return; |
| 132 | } |
| 133 | mode++; |
| 134 | } |
| 135 | selection++; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | LibraryManager::LibraryManager() { |
| 141 | libraryManager = this; |
| 142 | |
| 143 | setTitle("Game Library"); |
| 144 | setGeometry({128, 128, 640, 680}); |
| 145 | windowManager->append(this, "LibraryManager"); |
| 146 | |
| 147 | layout.setMargin(5); |
| 148 | bootstrap(); |
| 149 | libraryFrame.append("Import"); |
| 150 | libraryFrame.setLayout(browsers.size(), libraryImport); |
| 151 | loadButton.setText("Load"); |
| 152 | |
| 153 | unsigned height = Font::size(program->normalFont, " ").height; |
| 154 | |
| 155 | append(layout); |
| 156 | layout.append(libraryFrame, {~0, ~0}, 5); |
| 157 | layout.append(informationLayout, {~0, 0}); |
| 158 | informationLayout.append(information, {~0, height * 3}, 5); |
| 159 | informationLayout.append(skipButton, {80, 0}, 5); |
| 160 | informationLayout.append(loadButton, {80, 0}); |
| 161 | |
| 162 | onClose = skipButton.onActivate = [&] { |
| 163 | setModal(false); |
| 164 | setVisible(false); |
| 165 | }; |
| 166 | |
| 167 | libraryFrame.onChange = {&LibraryManager::onChange, this}; |
| 168 | loadButton.onActivate = {&LibraryManager::onLoad, this}; |
| 169 | |
| 170 | //initial config value of -1 defaults to import tab on first launch of higan |
| 171 | if(config->library.selection < 0) config->library.selection = browsers.size(); |
| 172 | libraryFrame.setSelection(config->library.selection); |
| 173 | |
| 174 | if(libraryFrame.selection() < browsers.size()) { |
| 175 | browsers[libraryFrame.selection()]->mediaMode.setSelection(config->library.mediaMode); |
| 176 | browsers[libraryFrame.selection()]->setMode(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void LibraryManager::bootstrap() { |
| 181 | unsigned selection = 0; |
| 182 | for(auto& emulator : program->emulator) { |
| 183 | LibraryBrowser* browser = new LibraryBrowser(*emulator); |
| 184 | libraryFrame.append(emulator->information.name); |
| 185 | libraryFrame.setLayout(selection++, *browser); |
| 186 | browsers.append(browser); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | string LibraryManager::load(const string& type) { |
| 191 | requestedLoadType = type; |
| 192 | unsigned selection = 0; |
| 193 | for(auto& browser : browsers) { |
| 194 | unsigned mode = 0; |
| 195 | for(auto& media : browser->emulator.media) { |
| 196 | if(type == media.type && media.bootable == false) { |
| 197 | libraryFrame.setSelection(selection); |
| 198 | browser->mediaMode.setSelection(mode); |
| 199 | browser->setMode(); |
| 200 | |
| 201 | slotLoad = true; |
| 202 | loadPathname = ""; |
| 203 | show(); |
| 204 | setModal(); |
| 205 | slotLoad = false; |
| 206 | browser->mediaMode.setSelection(config->library.mediaMode = 0); |
| 207 | return loadPathname; |
| 208 | } |
| 209 | mode++; |
| 210 | } |
| 211 | selection++; |
| 212 | } |
| 213 | return ""; //should never occur |
| 214 | } |
| 215 | |
| 216 | void LibraryManager::onChange() { |
| 217 | unsigned selection = libraryFrame.selection(); |
| 218 | config->library.selection = selection; |
| 219 | if(selection < browsers.size()) { |
| 220 | browsers[selection]->setMode(); |
| 221 | } else { |
| 222 | loadButton.setEnabled(false); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void LibraryManager::onLoad() { |
| 227 | unsigned selection = libraryFrame.selection(); |
| 228 | if(selection < browsers.size()) browsers[selection]->onActivate(); |
| 229 | } |
| 230 | |
| 231 | void LibraryManager::setInformation(bool load) { |
| 232 | string text; |
| 233 | if(loaded.size() == 0) { |
| 234 | text = {" \nPlease select a game to load ...\n "}; |
| 235 | } else if(loaded.size() == 1 && load == false) { |
| 236 | text = {" \n", loaded[0], "\n "}; |
| 237 | } else if(loaded.size() == 1 && load == true) { |
| 238 | text = {loaded[0], " \nPlease select a slot game to load ...\n "}; |
| 239 | } else if(loaded.size() == 2 && load == false) { |
| 240 | text = {loaded[0], "\n", loaded[1], "\n "}; |
| 241 | } else if(loaded.size() == 2 && load == true) { |
| 242 | text = {loaded[0], "\n", loaded[1], "\nPlease select a slot game to load ..."}; |
| 243 | } else if(loaded.size() == 3) { |
| 244 | text = {loaded[0], "\n", loaded[1], "\n", loaded[2]}; |
| 245 | } |
| 246 | information.setText(text); |
| 247 | } |
| 248 | |
| 249 | void LibraryManager::show() { |
| 250 | if(slotLoad == false) { |
| 251 | loaded.reset(); |
| 252 | requestedLoadType.reset(); |
| 253 | skipButton.setText("Cancel"); |
| 254 | } else { |
| 255 | skipButton.setText("Skip"); |
| 256 | } |
| 257 | |
| 258 | setInformation(true); |
| 259 | setVisible(); |
| 260 | setFocused(); |
| 261 | onChange(); |
| 262 | } |
| 263 | |
| 264 | //set library to show a specific media type, and then show the library |
| 265 | void LibraryManager::show(const string& type) { |
| 266 | unsigned selection = 0; |
| 267 | for(auto& browser : browsers) { |
| 268 | unsigned mode = 0; |
| 269 | for(auto& media : browser->emulator.media) { |
| 270 | if(media.bootable && media.type == type) { |
| 271 | libraryFrame.setSelection(selection); |
| 272 | browser->mediaMode.setSelection(mode); |
| 273 | browser->setMode(); |
| 274 | return show(); |
| 275 | } |
| 276 | mode++; |
| 277 | } |
| 278 | selection++; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void LibraryManager::synchronize() { |
| 283 | if(libraryFrame.selection() < browsers.size()) { |
| 284 | auto& emulator = browsers[libraryFrame.selection()]->emulator; |
| 285 | auto& media = emulator.media[browsers[libraryFrame.selection()]->mediaMode.selection()]; |
| 286 | |
| 287 | if(requestedLoadType.empty()) { |
| 288 | loadButton.setEnabled(media.bootable); |
| 289 | } else { |
| 290 | bool enabled = (requestedLoadType == media.type); |
| 291 | //allow Super Game Boy to load Game Boy Color games |
| 292 | if(requestedLoadType == "gb" && loaded.size() == 1 && media.type == "gbc") enabled = true; |
| 293 | loadButton.setEnabled(enabled); |
| 294 | } |
| 295 | } else { |
| 296 | loadButton.setEnabled(false); |
| 297 | } |
| 298 | } |