252 lines
9.6 KiB
C++
252 lines
9.6 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
// Presentation-only automatic checkpoint. It neither replaces a user save nor
|
|
// marks the game clean. The next state is written before the in-memory action
|
|
// commits.
|
|
fs::path App::opening_progress_path(const Catalog &catalog) const {
|
|
const auto id = catalog.manifest().value("id", std::string("none"));
|
|
return (config_.userdata.empty() ? root_ / "userdata" : config_.userdata) /
|
|
"ui-demo" / content::sha256(id) / "opening-progress.json";
|
|
}
|
|
void App::opening_write(const Catalog &catalog, const Json &state,
|
|
const Json &layout) {
|
|
const auto path = opening_progress_path(catalog);
|
|
const Json data = {{"schema", "openbmh-ui-opening@1"},
|
|
{"world", catalog.manifest().value("id", "")},
|
|
{"manifest", content::sha256(catalog.manifest().dump())},
|
|
{"state", state},
|
|
{"desktop", layout}};
|
|
try {
|
|
fs::create_directories(path.parent_path());
|
|
editor::write_atomic(path, data.dump(2));
|
|
} catch (const std::exception &) {
|
|
throw editor::Error("Die Saisonvorbereitung konnte nicht gesichert werden. "
|
|
"Bitte Schreibrechte prüfen und erneut versuchen.");
|
|
}
|
|
}
|
|
bool App::opening_pending() const {
|
|
if (!source_)
|
|
return false;
|
|
const auto state = snap().state;
|
|
if(state.value("career",Json::object()).value("ended",false))return false;
|
|
const auto &flow = state.at("opening_flow");
|
|
return flow.at("created").get<bool>() && flow.at("phase") != "done";
|
|
}
|
|
void App::opening_restore_startup() {
|
|
if (!gui_.available())
|
|
return;
|
|
if (!config_.state.empty()) {
|
|
// Explicit loads supersede an older unfinished preparation of this world.
|
|
if (source_ &&
|
|
(opening_pending() || fs::exists(opening_progress_path(*world_))))
|
|
opening_write(*world_, snap().state,
|
|
valid_desktop(desktop_) ? desktop_ : default_desktop());
|
|
if (opening_pending()) {
|
|
scene_ = "opening";
|
|
menu_.clear();
|
|
}
|
|
return;
|
|
}
|
|
if (scene_ != "main" && scene_ != "desktop" && scene_ != "opening")
|
|
return;
|
|
auto catalog = world_;
|
|
try {
|
|
if (!catalog) {
|
|
auto *m = mod(active_mod_);
|
|
if (!m || m->type != "Modern" || !m->error.empty())
|
|
return;
|
|
catalog = Catalog::open(m->file);
|
|
}
|
|
const auto path = opening_progress_path(*catalog);
|
|
if (!fs::exists(path))
|
|
return;
|
|
if (fs::file_size(path) > 32 * 1024 * 1024)
|
|
throw editor::Error("opening_progress_too_large");
|
|
const auto data = Json::parse(editor::read_file(path));
|
|
if (data.at("schema") != "openbmh-ui-opening@1" ||
|
|
data.at("world") != catalog->manifest().at("id") ||
|
|
data.at("manifest") != content::sha256(catalog->manifest().dump()) ||
|
|
!valid_desktop(data.at("desktop")))
|
|
throw editor::Error("invalid_opening_progress");
|
|
auto demo = std::make_unique<DemoSource>(catalog);
|
|
auto checkpoint = data.at("state");
|
|
checkpoint["desktop"] = data.at("desktop");
|
|
const auto layout = demo->load_data(std::move(checkpoint), false);
|
|
if(demo->snapshot().state.value("career",Json::object()).value("ended",false))return;
|
|
const auto flow = demo->snapshot().state.at("opening_flow");
|
|
if (!flow.at("created").get<bool>() || flow.at("phase") == "done")
|
|
return;
|
|
publish_world(catalog, std::move(demo));
|
|
desktop_ = saved_desktop_ = layout;
|
|
scene_ = "opening";
|
|
menu_.clear();
|
|
opening_due_ = 0;
|
|
} catch (const std::exception &) {
|
|
message("Die gesicherte Saisonvorbereitung konnte nicht geladen werden. "
|
|
"Du kannst eine neue Partie beginnen oder einen Spielstand laden.");
|
|
}
|
|
}
|
|
bool App::opening_change(const std::string &kind) {
|
|
if(engine_mode_) {
|
|
if(!engine_opening_active())return false;
|
|
auto next=engine_opening_state_.at("opening_flow");const auto error=advance_opening_presentation(next,kind);if(!error.empty())return false;
|
|
engine_opening_state_["opening_flow"]=std::move(next);opening_due_=0;refresh();return true;
|
|
}
|
|
if (!source_)
|
|
return false;
|
|
const auto before = snap();
|
|
auto next = before.state;
|
|
const Json action = {{"kind", kind}};
|
|
const auto error = edit_demo_opening(next, action, *world_);
|
|
if (!error.empty())
|
|
return false;
|
|
try {
|
|
opening_write(*world_, next,
|
|
valid_desktop(desktop_) ? desktop_ : default_desktop());
|
|
} catch (const std::exception &failure) {
|
|
opening_io_blocked_ = true;
|
|
opening_due_ = 0;
|
|
message(failure.what());
|
|
refresh();
|
|
return false;
|
|
}
|
|
const auto applied =
|
|
source_->apply({before.revision, "opening.flow", action});
|
|
if (!applied.ok) {
|
|
message("Die Vorbereitung hat sich geändert. Bitte erneut versuchen.");
|
|
return false;
|
|
}
|
|
opening_due_ = 0;
|
|
opening_io_blocked_ = false;
|
|
refresh();
|
|
return true;
|
|
}
|
|
bool App::opening_action(const std::string &action, Rml::Element *) {
|
|
if ((!engine_mode_&&scene_ != "opening") || (engine_mode_&&!engine_opening_active()) || !menu_.empty() || !popup_.empty())
|
|
return false;
|
|
if (action == "close")
|
|
return true;
|
|
std::string kind;
|
|
if (action == "opening-next")
|
|
kind = "next";
|
|
else if (action == "opening-play")
|
|
kind = opening_io_blocked_ &&
|
|
(engine_mode_?engine_opening_state_:snap().state).at("opening_flow").at("playing").get<bool>()
|
|
? "reveal"
|
|
: "toggle";
|
|
else if (action == "opening-all")
|
|
kind = "skip";
|
|
else if (action == "opening-finish") {
|
|
const auto phase =
|
|
(engine_mode_?engine_opening_state_:snap().state).at("opening_flow").at("phase").get<std::string>();
|
|
if (phase == "done") {
|
|
scene_ = "desktop";
|
|
opening_due_ = 0;
|
|
refresh();
|
|
return true;
|
|
}
|
|
kind = "finish";
|
|
} else
|
|
return false;
|
|
if (!opening_change(kind))
|
|
return true;
|
|
const auto phase =
|
|
(engine_mode_?engine_opening_state_:snap().state).at("opening_flow").at("phase").get<std::string>();
|
|
if (phase == "done") {
|
|
scene_ = "desktop";
|
|
focus_after_render_.clear();
|
|
} else
|
|
focus_after_render_ = phase == "overview" ? "opening-finish"
|
|
: phase == "draw" ? "opening-play"
|
|
: "opening-next";
|
|
return true;
|
|
}
|
|
bool App::opening_event(Rml::Event &event) {
|
|
if (scene_ != "opening" || !menu_.empty() || !popup_.empty())
|
|
return false;
|
|
if ((event.GetType() == "keydown" &&
|
|
event.GetParameter<int>("key_identifier", 0) == Rml::Input::KI_ESCAPE) ||
|
|
event.GetType() == "contextmenu" ||
|
|
(event.GetType() == "mouseup" &&
|
|
event.GetParameter<int>("button", 0) == 1)) {
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
void App::opening_tick() {
|
|
if ((!engine_mode_&&(scene_ != "opening" || !source_)) || (engine_mode_&&!engine_opening_active()) || !menu_.empty() || !popup_.empty() ||
|
|
opening_io_blocked_)
|
|
return;
|
|
const auto flow = (engine_mode_?engine_opening_state_:snap().state).at("opening_flow");
|
|
if (flow.at("phase") != "draw" || !flow.at("playing").get<bool>()) {
|
|
opening_due_ = 0;
|
|
return;
|
|
}
|
|
if (!(SDL_GetWindowFlags(video_.window) & SDL_WINDOW_INPUT_FOCUS)) {
|
|
opening_change("pause");
|
|
return;
|
|
}
|
|
const auto now = SDL_GetTicks64();
|
|
if (!opening_due_)
|
|
opening_due_ = now + 900;
|
|
if (now < opening_due_)
|
|
return;
|
|
if (opening_change("reveal")) {
|
|
if ((engine_mode_?engine_opening_state_:snap().state).at("opening_flow").at("phase") == "overview")
|
|
focus_after_render_ = "opening-finish";
|
|
opening_due_ = SDL_GetTicks64() + 900;
|
|
}
|
|
}
|
|
void App::handle_event(SDL_Event &event) {
|
|
if (event.type == SDL_QUIT) {
|
|
if(engine_creation_busy_||engine_files_busy_){message("Die Partie wird noch vorbereitet. Bitte kurz warten.");return;}
|
|
if (engine_mode_) { engine_quit(); return; }
|
|
if (front_busy_) {
|
|
front_feedback_ = "Bitte den laufenden Import abwarten.";
|
|
dirty_ = true;
|
|
} else if (source_ && scene_ != "main" && scene_ != "mods") {
|
|
quit_closes_app_ = true;
|
|
dispatch("context-quit", nullptr);
|
|
} else
|
|
closed_ = true;
|
|
return;
|
|
}
|
|
if (event.type == SDL_WINDOWEVENT) {
|
|
if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
|
|
desktop_cancel_drag();
|
|
if (scene_ == "opening" && source_ && !opening_io_blocked_ &&
|
|
(engine_mode_?engine_opening_state_:snap().state).at("opening_flow").at("phase") == "draw" &&
|
|
(engine_mode_?engine_opening_state_:snap().state).at("opening_flow").at("playing").get<bool>())
|
|
opening_change("pause");
|
|
}
|
|
resize();
|
|
if(event.window.event==SDL_WINDOWEVENT_SIZE_CHANGED || event.window.event==SDL_WINDOWEVENT_RESIZED || event.window.event==SDL_WINDOWEVENT_DISPLAY_CHANGED){
|
|
// resize() already uses the drawable size. The SDL2 Rml backend would
|
|
// replace it with logical event dimensions on SIZE_CHANGED.
|
|
dirty_=true;return;
|
|
}
|
|
}
|
|
int w, h, pw, ph;
|
|
SDL_GetWindowSize(video_.window, &w, &h);
|
|
video_.drawable_size(pw, ph);
|
|
if (w > 0 && h > 0) {
|
|
if (event.type == SDL_MOUSEMOTION) {
|
|
event.motion.x = int(double(event.motion.x) * pw / w);
|
|
event.motion.y = int(double(event.motion.y) * ph / h);
|
|
}
|
|
if (event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP) {
|
|
event.button.x = int(double(event.button.x) * pw / w);
|
|
event.button.y = int(double(event.button.y) * ph / h);
|
|
context_->ProcessMouseMove(event.button.x, event.button.y,
|
|
RmlSDL::GetKeyModifierState());
|
|
}
|
|
}
|
|
RmlSDL::InputEventHandler(context_, video_.window, event);
|
|
dirty_ = true;
|
|
}
|
|
void App::pump_events() {
|
|
SDL_Event event;
|
|
while (SDL_PollEvent(&event))
|
|
handle_event(event);
|
|
}
|