coastline 0.0.4
Loading...
Searching...
No Matches
setupwizard.h
Go to the documentation of this file.
1#pragma once
2
9
10#include <array>
11#include <cstdint>
12#include <filesystem>
13#include <functional>
14#include <future>
15#include <map>
16#include <memory>
17#include <optional>
18#include <string>
19#include <thread>
20#include <utility>
21#include <vector>
22
23#include "filebrowser.h"
24#include "textgrid.h"
25
26namespace coastline {
27
29enum class WizardCartridge {
33};
34
36inline constexpr int WIZARD_CARTRIDGES = 3;
37
46
54
56using WizardProgress = std::function<bool(const std::string &step, double fraction)>;
57
89
103
106 std::function<bool(const WizardSearchResult &, const std::string &, double)>;
107
117 const WizardServices &services,
118 const WizardSearchProgress &progress);
119
142
165
176
183
221
245
250char AcceleratorOf(const std::string &label);
251
254public:
260 explicit SetupWizard(WizardServices services);
261
265 SetupWizard(const SetupWizard &) = delete;
268
274 void Handle(const WizardEvent &event);
275
277 void Update();
278
285 void Draw(TextGrid &grid, double seconds) const;
286
289
292 bool Checking() const;
293
295 WizardEnd End() const;
296
298 bool Portable() const;
299
302
305
307 bool BrowserOpen() const;
308
311
313 int BrowserSelection() const;
314
317
323
329
332
335
338
339private:
340 static constexpr std::string_view REPOSITORY_URL = "https://github.com/Tatsh/coastline";
341 bool m_repositoryHovered = false;
342 bool m_repositoryPressed = false;
344 struct TextField {
345 std::u32string text;
346 std::size_t caret = 0;
347 std::size_t scroll = 0;
349 bool valid = false;
351 std::string reason;
352 std::u32string checked;
353 std::uintmax_t size = 0;
355 bool checkedOnce = false;
356 };
358 struct Browser {
360 std::filesystem::path directory;
362 int selected = 0;
363 int top = 0;
364 bool allFiles = false;
365 std::string typed;
366 std::uint64_t typedAt = 0;
367 std::uint64_t clickedAt = 0;
368 int clicked = -1;
369 bool dragging = false;
370 };
372 struct Message {
373 std::vector<std::string> lines;
374 bool question = false;
375 };
376 struct ImportState;
378 struct CheckResult {
379 bool valid = false;
380 std::optional<WizardCartridge> release;
381 std::string reason;
382 };
383
384 std::vector<WizardControl> ScreenControls() const;
385 std::vector<WizardControl> BrowserControls() const;
386 std::vector<WizardControl> MessageControls() const;
387 std::optional<WizardControlId> &FocusSlot();
388 void FocusFirst();
389 void MoveFocus(bool backward);
390 void MoveFocusRow(bool upward);
391 void Activate(WizardControlId id);
392 void PressDefault();
393 void Escape();
394 void SetScreen(WizardScreen screen);
395 void OpenBrowser(WizardControlId target);
396 void ChangeDirectory(const std::filesystem::path &directory);
397 void OpenSelected();
398 void ChooseDirectory();
399 void ShowMessage(std::vector<std::string> lines, bool question);
400 void StartImport();
401 void StartSearch();
402 void ApplySearchResult();
403 void SetText(WizardControlId id, const std::filesystem::path &path);
404 void ClearText(WizardControlId id);
405 void Validate(WizardControlId id, bool force);
406 void RetireCheck(WizardControlId id);
407 void ApplyChecks();
408 void EditText(TextField &field, const WizardEvent &event, WizardControlId id);
409 void HandleListKey(WizardKey key);
410 void HandleListText(const std::string &text, std::uint64_t milliseconds);
411 void HandleMouse(const WizardEvent &event);
412 void SetSelection(int selected);
413 bool NextEnabled() const;
414 TextField &Field(WizardControlId id);
415 const TextField &Field(WizardControlId id) const;
416 std::array<bool, WIZARD_CARTRIDGES> Detected() const;
417
418 WizardServices m_services;
421 TextField m_disc;
422 std::array<TextField, WIZARD_CARTRIDGES> m_cartridges;
423 TextField m_directory;
424 WizardSearchResult m_searchResult;
425 bool m_searchComplete = false;
426 std::optional<Browser> m_browser;
427 std::optional<Message> m_message;
428 std::optional<WizardControlId> m_screenFocus;
429 std::optional<WizardControlId> m_browserFocus;
430 std::optional<WizardControlId> m_messageFocus;
431 std::filesystem::path m_lastDirectory;
432 std::shared_ptr<ImportState> m_import;
433 std::thread m_thread;
434 // A check reads a whole image, and a disc check decodes an ISZ or PDI image. The checks run
435 // on threads. A check replaced by a newer path waits in m_retiredChecks until it finishes,
436 // as the destruction of its future waits for it.
437 std::map<WizardControlId, std::future<CheckResult>> m_checks;
438 std::vector<std::future<CheckResult>> m_retiredChecks;
439 std::string m_importStep;
440 double m_importFraction = 0.0;
441 std::optional<std::string> m_importFailure;
442 std::optional<WizardControlId> m_selecting;
443};
444
445} // namespace coastline
const WizardSearchResult & SearchResult() const
~SetupWizard()
Cancel active search or import work and wait for the worker thread.
WizardEnd End() const
std::optional< WizardControlId > Focus() const
bool BrowserOpen() const
SetupWizard(WizardServices services)
Create the initial setup screen without importing assets.
void Handle(const WizardEvent &event)
Act on an event of the keyboard or the mouse.
std::string SelectedText() const
void Update()
Take the progress of the import, and move to the next screen when it ends.
const std::vector< BrowserEntry > & BrowserEntries() const
SetupWizard & operator=(const SetupWizard &)=delete
Prevent assignment of active search and import threads.
std::string Text(WizardControlId id) const
SetupWizard(const SetupWizard &)=delete
Prevent copying active search and import threads.
std::string Reason(WizardControlId id) const
void Draw(TextGrid &grid, double seconds) const
Draw the wizard.
std::vector< WizardControl > Controls() const
int BrowserSelection() const
std::vector< std::string > MessageLines() const
WizardScreen Screen() const
const std::filesystem::path & LastDirectory() const
The cells of the screen and the place of the text cursor.
Definition textgrid.h:99
The listing of a directory for the file browser of the setup wizard.
Definition assetimport.h:11
constexpr int WIZARD_CARTRIDGES
The number of supported ROM releases.
Definition setupwizard.h:36
char AcceleratorOf(const std::string &label)
@ Jpn
Definition controllerpak.h:107
@ Usa
Definition controllerpak.h:105
@ Eur
Definition controllerpak.h:106
std::function< bool(const WizardSearchResult &, const std::string &, double)> WizardSearchProgress
Report search progress and return false to cancel.
Definition setupwizard.h:105
WizardControlId
A control of a screen or a dialog of the wizard.
Definition setupwizard.h:185
@ Back
Definition setupwizard.h:207
@ Rom3Browse
Definition setupwizard.h:194
@ BrowserOk
Definition setupwizard.h:214
@ SearchClear
Definition setupwizard.h:203
@ Next
Definition setupwizard.h:208
@ MessageOk
Definition setupwizard.h:217
@ BrowserList
Definition setupwizard.h:212
@ MessageYes
Definition setupwizard.h:218
@ MessageNo
Definition setupwizard.h:219
@ Rom2Browse
Definition setupwizard.h:193
@ SearchBrowse
Definition setupwizard.h:202
@ Rom3Clear
Definition setupwizard.h:197
@ DiscBrowse
Definition setupwizard.h:187
@ DiscText
Definition setupwizard.h:186
@ Rom3Text
Definition setupwizard.h:191
@ StartSearch
Definition setupwizard.h:204
@ RetrySearch
Definition setupwizard.h:205
@ Rom1Clear
Definition setupwizard.h:195
@ BrowserCancel
Definition setupwizard.h:216
@ DiscClear
Definition setupwizard.h:188
@ BrowserUseDirectory
Definition setupwizard.h:215
@ SearchText
Definition setupwizard.h:201
@ Rom1Browse
Definition setupwizard.h:192
@ Finish
Definition setupwizard.h:211
@ Rom2Text
Definition setupwizard.h:190
@ Rom1Text
Definition setupwizard.h:189
@ SearchAssets
Definition setupwizard.h:198
@ Rom2Clear
Definition setupwizard.h:196
@ Cancel
Definition setupwizard.h:210
@ BrowserFilter
Definition setupwizard.h:213
@ SpecifyPaths
Definition setupwizard.h:199
@ UseFound
Definition setupwizard.h:206
@ Portable
Definition setupwizard.h:200
WizardSearchResult SearchWizardDirectory(const std::filesystem::path &directory, const WizardServices &services, const WizardSearchProgress &progress)
Search recursively, including hidden directories, for validated disc and ROM images.
std::function< bool(const std::string &step, double fraction)> WizardProgress
Reports the step of an import and its share done from 0 to 1, and returns false to cancel.
Definition setupwizard.h:56
WizardEnd
How the wizard ended.
Definition setupwizard.h:178
@ Quit
Definition setupwizard.h:180
@ Running
Definition setupwizard.h:179
@ Finished
Definition setupwizard.h:181
std::optional< WizardCartridge > BaseCartridge(const std::array< bool, WIZARD_CARTRIDGES > &checked)
Choose the base release for the default pack.
WizardScreen
A screen of the wizard, in order.
Definition setupwizard.h:167
@ Search
Definition setupwizard.h:172
@ SearchDirectory
Definition setupwizard.h:171
@ SourceSelection
Definition setupwizard.h:169
@ Import
Definition setupwizard.h:173
@ Files
Definition setupwizard.h:170
@ Reimport
Definition setupwizard.h:168
@ Done
Definition setupwizard.h:174
WizardCartridge
A supported ROM release.
Definition setupwizard.h:29
WizardKey
A key of the keyboard the wizard reads.
Definition setupwizard.h:121
@ Escape
Definition setupwizard.h:125
@ Down
Definition setupwizard.h:127
@ Up
Definition setupwizard.h:126
@ WordLeft
Definition setupwizard.h:139
@ Tab
Definition setupwizard.h:122
@ DeleteNextWord
Definition setupwizard.h:138
@ SelectAll
Definition setupwizard.h:136
@ DeleteWord
Definition setupwizard.h:137
@ End
Definition setupwizard.h:133
@ Home
Definition setupwizard.h:132
@ Right
Definition setupwizard.h:129
@ Left
Definition setupwizard.h:128
@ PageUp
Definition setupwizard.h:130
@ Backspace
Definition setupwizard.h:134
@ Space
Definition setupwizard.h:124
@ WordRight
Definition setupwizard.h:140
@ PageDown
Definition setupwizard.h:131
@ Enter
Definition setupwizard.h:123
@ Delete
Definition setupwizard.h:135
A control positioned on the grid.
Definition setupwizard.h:223
std::string label
The label, with & before the letter Alt chooses it by.
Definition setupwizard.h:239
int column
Definition setupwizard.h:234
Kind kind
Definition setupwizard.h:233
int row
Definition setupwizard.h:235
bool checked
Definition setupwizard.h:242
bool bad
Definition setupwizard.h:243
bool enabled
Definition setupwizard.h:241
Kind
The kind of control.
Definition setupwizard.h:225
@ Choice
Definition setupwizard.h:230
@ List
Definition setupwizard.h:229
@ CheckBox
Definition setupwizard.h:227
@ Button
Definition setupwizard.h:226
@ TextBox
Definition setupwizard.h:228
int width
Definition setupwizard.h:236
std::string value
Definition setupwizard.h:240
int height
Definition setupwizard.h:237
An event of the keyboard or the mouse.
Definition setupwizard.h:144
Type type
Definition setupwizard.h:155
std::string text
Definition setupwizard.h:158
bool shift
Definition setupwizard.h:157
int x
Definition setupwizard.h:160
char letter
Definition setupwizard.h:159
Type
The kind of event.
Definition setupwizard.h:146
@ MouseUp
Definition setupwizard.h:151
@ Key
Definition setupwizard.h:147
@ Text
Definition setupwizard.h:148
@ Wheel
Definition setupwizard.h:153
@ MouseDown
Definition setupwizard.h:150
@ Accelerator
Definition setupwizard.h:149
@ MouseMove
Definition setupwizard.h:152
WizardKey key
Definition setupwizard.h:156
int wheel
Definition setupwizard.h:162
int y
Definition setupwizard.h:161
std::uint64_t milliseconds
Definition setupwizard.h:163
The files an import of the wizard reads.
Definition setupwizard.h:48
std::filesystem::path disc
Definition setupwizard.h:49
bool portable
Definition setupwizard.h:50
std::vector< std::pair< WizardCartridge, std::filesystem::path > > cartridges
The ROM images by release, with the base of the default pack first.
Definition setupwizard.h:52
The images found during a directory search, indexed by detected release.
Definition setupwizard.h:91
std::optional< std::string > failure
The search failure reason, or std::nullopt without a reported failure.
Definition setupwizard.h:101
std::size_t examined
The number of candidate paths processed, including paths skipped without validation.
Definition setupwizard.h:97
std::filesystem::path disc
The first validated disc image, or empty until a disc is found.
Definition setupwizard.h:93
std::array< std::filesystem::path, WIZARD_CARTRIDGES > roms
The first validated ROM of each WizardCartridge region, or empty for absent regions.
Definition setupwizard.h:95
std::size_t candidates
The total candidate image and archive paths, assigned after traversal finishes.
Definition setupwizard.h:99
The routines the wizard calls to check and import its files.
Definition setupwizard.h:59
std::string buildRevision
Definition setupwizard.h:83
std::string buildDate
Definition setupwizard.h:84
std::filesystem::path disc
Definition setupwizard.h:78
std::function< std::optional< WizardCartridge >(const std::filesystem::path &)> identifyCartridge
The release of a ROM image by its SHA-1, or nothing when it is not Top Gear Rally.
Definition setupwizard.h:63
std::function< bool(const std::filesystem::path &, const std::filesystem::path &)> preferCartridge
Whether a newly found ROM should replace one already selected for the same slot.
Definition setupwizard.h:66
std::string reimportReason
Definition setupwizard.h:85
bool portableAvailable
Definition setupwizard.h:86
bool portable
Definition setupwizard.h:87
std::function< void(const std::filesystem::path &, const std::function< bool(WizardCartridge, const std::filesystem::path &)> &, const WizardProgress &)> scanArchive
Search ZIP entries and report validated ROM images cached outside the archive.
Definition setupwizard.h:71
std::filesystem::path home
Definition setupwizard.h:76
std::function< void(const std::string &)> openUrl
Open an absolute UTF-8 URL in the default browser, or remain empty to disable links.
Definition setupwizard.h:75
std::function< std::optional< std::string >(const std::filesystem::path &)> checkDisc
The reason a disc image is not Boss Rally, or nothing when it is.
Definition setupwizard.h:61
std::filesystem::path lastDirectory
Definition setupwizard.h:77
std::filesystem::path searchDirectory
Definition setupwizard.h:82
std::array< std::filesystem::path, WIZARD_CARTRIDGES > roms
Previously entered ROM paths in their original slots, including empty slots.
Definition setupwizard.h:80
A text-mode screen of 80 by 25 cells, each a character of code page 437 with a foreground and a backg...