// SPDX-License-Identifier: GPL-3.0-only DemoFiles App::files_store() const { return DemoFiles(user_dir(), files_mode_.starts_with("view-") ? DemoFileKind::desktop : DemoFileKind::career, world_); } bool App::files_bound() const { return source_ && files_source_ == source_.get() && files_world_ == world_ && files_revision_ == snap().revision && files_desktop_base_ == desktop_ && files_theme_base_ == theme_; } void App::reset_files() { files_mode_.clear(); files_selected_.clear(); files_name_.clear(); files_feedback_.clear(); files_rows_.clear(); files_entry_.reset(); files_world_.reset(); files_source_ = nullptr; files_desktop_base_ = Json(); files_theme_base_.clear(); files_revision_ = 0; files_scroll_ = 0; files_restore_scroll_ = false; files_reveal_selected_ = false; } void App::files_begin(const std::string &mode) { reset_files(); files_mode_ = mode; files_world_ = world_; files_source_ = source_.get(); files_desktop_base_ = desktop_; files_theme_base_ = theme_; if (source_) files_revision_ = snap().revision; const bool layouts = mode.starts_with("view-"); files_selected_ = mode == "view-load" ? "standard" : ""; try { files_name_ = files_store().free_name(layouts ? "Meine Ansicht" : "Spielstand 1"); files_reload(); if (mode == "load" && !files_rows_.empty()) files_select(files_rows_.front().id); } catch (const std::exception &e) { files_feedback_ = e.what(); } } void App::files_reload(const std::string &selected) { files_rows_ = files_store().list(); if (!selected.empty()) files_selected_ = selected; files_entry_.reset(); if (files_selected_.empty() || files_selected_ == "standard") return; const auto found = std::find_if(files_rows_.begin(), files_rows_.end(), [&](const auto &row) { return row.id == files_selected_; }); if (found == files_rows_.end()) { files_selected_.clear(); return; } files_entry_ = *found; if (!found->valid()) return; try { files_entry_ = files_store().read(found->id, found->token); } catch (const std::exception &e) { files_entry_->error = e.what(); } } void App::files_select(const std::string &selected) { files_selected_ = selected; files_feedback_.clear(); files_reload(); focus_after_render_ = selected.empty() ? "files-new" : selected == "standard" ? "files-standard" : ""; for (size_t i = 0; i < files_rows_.size(); ++i) if (files_rows_[i].id == selected) focus_after_render_ = "files-entry-" + std::to_string(i); files_reveal_selected_ = true; refresh(); } void App::files_before_render() { if (files_capturing_) return; const bool relevant = menu_ == "save" || menu_ == "load" || menu_ == "view-save" || menu_ == "view-load"; if (!relevant) { if (!files_mode_.empty()) reset_files(); return; } if (files_mode_ != menu_) { files_begin(menu_); return; } if (auto *list = element("files-list")) { files_scroll_ = list->GetScrollTop(); files_restore_scroll_ = true; } } void App::files_after_render() { if (files_capturing_ || files_mode_ != menu_) return; context_->Update(); if (files_restore_scroll_) if (auto *list = element("files-list")) list->SetScrollTop(files_scroll_); files_restore_scroll_ = false; if (files_reveal_selected_) { Rml::Element *row = nullptr; if (files_selected_.empty()) row = element("files-new"); else if (files_selected_ == "standard") row = element("files-standard"); else for (size_t i = 0; i < files_rows_.size(); ++i) if (files_rows_[i].id == files_selected_) row = element("files-entry-" + std::to_string(i)); if (row) row->ScrollIntoView( Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest)); if (auto *list = element("files-list")) files_scroll_ = list->GetScrollTop(); files_reveal_selected_ = false; } } std::string App::files_capture_preview() { const auto old_menu = menu_, old_scene = scene_; files_capturing_ = true; menu_.clear(); scene_ = "desktop"; refresh(); try { frame(); int width = 0, height = 0; auto pixels = video_.capture(width, height); using Surface = std::unique_ptr; Surface original(SDL_CreateRGBSurfaceWithFormatFrom(pixels.data(), width, height, 32, width * 4, SDL_PIXELFORMAT_RGBA32), SDL_FreeSurface); Surface thumb(SDL_CreateRGBSurfaceWithFormat(0, 1280, 720, 32, SDL_PIXELFORMAT_RGBA32), SDL_FreeSurface); if (!original || !thumb) throw editor::Error( "Die Desktop-Vorschau konnte nicht aufgenommen werden."); SDL_Rect crop{int(std::lround(canvas_left_)), int(std::lround(canvas_top_)), int(std::lround(3840 * scale_)), int(std::lround(2160 * scale_))}; crop.w = std::min(crop.w, width - crop.x); crop.h = std::min(crop.h, height - crop.y); if (SDL_SoftStretchLinear(original.get(), &crop, thumb.get(), nullptr) != 0) throw editor::Error("Die Desktop-Vorschau konnte nicht skaliert werden."); editor::images::Raster image{1280, 720, std::vector(1280 * 720 * 4)}; for (int y = 0; y < 720; ++y) std::memcpy(image.pixels.data() + y * 1280 * 4, static_cast(thumb->pixels) + y * thumb->pitch, 1280 * 4); auto png = editor::images::encode_png(image); menu_ = old_menu; scene_ = old_scene; files_capturing_ = false; refresh(); return png; } catch (...) { menu_ = old_menu; scene_ = old_scene; files_capturing_ = false; refresh(); throw; } } void App::files_save(const std::string &name, const std::string &id, const std::string &token) { if (!files_bound()) throw editor::Error("Die aktive Karriere wurde inzwischen geändert. Bitte " "das Fenster neu öffnen."); const bool layouts = files_mode_.starts_with("view-"); const auto revision = snap().revision; Json payload = layouts ? Json{{"layout", desktop_}, {"theme", theme_}} : snap().state; if (!layouts) payload["desktop"] = desktop_; // Validate before the capture and before publishing any file. auto store = files_store(); store.validate(payload); const auto png = files_capture_preview(); if (!files_bound() || snap().revision != revision) throw editor::Error("Die Karriere wurde während der Aufnahme geändert."); const auto saved = store.put(name, payload, png, id, token); files_selected_ = saved.id; files_feedback_ = layouts ? "Ansicht mit Vorschau gespeichert." : "Spielstand gespeichert."; if (!layouts) { if (!source_->mark_saved(revision)) throw editor::Error( "Gespeichert; die inzwischen geänderte Karriere bleibt ungesichert."); saved_desktop_ = desktop_; } files_saved_success(!layouts); refresh(); } void App::files_load() { if (!files_bound()) throw editor::Error("Die aktive Karriere wurde inzwischen geändert. Bitte " "das Fenster neu öffnen."); const bool layouts = files_mode_.starts_with("view-"); if (layouts) { Json layout, preferences = ui_preferences_; std::string theme = theme_; if (files_selected_ == "standard") layout = default_desktop(); else { if (!files_entry_ || !files_entry_->valid()) return; const auto record = files_store().read(files_entry_->id, files_entry_->token); layout = record.data.at("layout"); theme = record.data.at("theme").get(); } // Persist the global theme successfully before publishing either layout // or theme. Other global display and sound preferences remain untouched. preferences["theme"] = theme; save_ui_preferences(ui_preferences_file(), preferences); desktop_checkpoint(desktop_); desktop_ = std::move(layout); theme_ = theme; ui_preferences_ = std::move(preferences); ui_preferences_loaded_ = true; menu_.clear(); reset_files(); refresh(); return; } if (!files_entry_ || !files_entry_->valid()) return; const auto record = files_store().read(files_entry_->id, files_entry_->token); auto next = std::make_unique(world_); auto layout = next->load_data(record.data); if (!files_bound()) throw editor::Error("Die aktive Karriere wurde inzwischen geändert."); const auto selected_id = files_entry_->id, selected_name = files_entry_->name; if (next->snapshot().state.at("opening_flow").at("phase") != "done" || fs::exists(opening_progress_path(*world_))) opening_write(*world_, next->snapshot().state, layout); ticket_.reset(); source_ = std::move(next); opening_due_ = 0; opening_io_blocked_ = false; desktop_ = std::move(layout); saved_desktop_ = desktop_; reset_files(); files_loaded_success(); menu_ = "load"; files_begin("load"); files_select(selected_id); files_feedback_ = "Spielstand „" + selected_name + "“ geladen."; focus_after_render_ = "files-confirm"; refresh(); } bool App::files_action(const std::string &action, Rml::Element *target) { if (!action.starts_with("files-")) return false; const auto id = target ? target->GetAttribute("data-id", "") : std::string(); try { if (action == "files-close") { quit_after_save_ = false; menu_.clear(); reset_files(); if (scene_ != "desktop") scene_ = "main"; refresh(); return true; } if (action == "files-select") { files_select(id); return true; } if (action == "files-new") { files_select(""); focus_after_render_ = "files-name"; return true; } if (action == "files-standard") { files_select("standard"); return true; } if (action == "files-load") { files_load(); return true; } if (!files_bound()) throw editor::Error("Die aktive Karriere wurde inzwischen geändert. " "Bitte das Fenster neu öffnen."); auto store = files_store(); if (action == "files-save") { const auto name = files_entry_ ? files_entry_->name : DemoFiles::clean_name(files_name_); auto chosen = files_entry_; if (!chosen) for (const auto &row : files_rows_) { bool duplicate = false; try { duplicate = DemoFiles::name_key(row.name, files_mode_.starts_with("view-")) == DemoFiles::name_key(name, files_mode_.starts_with("view-")); } catch (...) { } if (duplicate) { chosen = row; break; } } if (chosen) { const auto row = *chosen; confirmation( (files_mode_.starts_with("view-") ? "Ansicht" : "Spielstand") + std::string(" „") + row.name + "“ durch den aktuellen Stand ersetzen?", [this, name, row] { files_save(name, row.id, row.token); }); popup_accept_ = "Überschreiben"; } else files_save(name, "", ""); return true; } if (action == "files-rename" && files_entry_ && files_entry_->valid()) { const auto row = *files_entry_; text_dialog(files_mode_.starts_with("view-") ? "Ansicht umbenennen" : "Spielstand umbenennen", row.name, [this, row](const std::string &name) { if (!files_bound()) throw editor::Error( "Die aktive Karriere wurde inzwischen geändert."); const auto renamed = files_store().rename(row.id, row.token, name); files_select(renamed.id); files_feedback_ = "Bezeichnung geändert."; refresh(); }); popup_accept_ = "Umbenennen"; return true; } if (action == "files-delete" && files_entry_ && !files_entry_->token.empty()) { const auto row = *files_entry_; confirmation("„" + row.name + "“ mit Vorschau wirklich löschen?", [this, row] { if (!files_bound()) throw editor::Error("Die aktive Karriere wurde inzwischen geändert."); files_store().remove(row.id, row.token); files_selected_ = files_mode_ == "view-load" ? "standard" : ""; files_reload(); files_feedback_ = "Datei gelöscht."; refresh(); }); popup_accept_ = "Löschen"; return true; } if (action == "files-import") { fs::path selected = config_.file_import; if (selected.empty()) { NFD::Guard guard; NFD::UniquePath chosen; const nfdfilteritem_t filters[] = {{"OpenBMH UI-Demodatei", "json"}}; const auto result = NFD::OpenDialog(chosen, filters, 1); if (result == NFD_CANCEL) return true; if (result != NFD_OKAY) throw editor::Error("Die Dateiauswahl konnte nicht geöffnet werden."); selected = editor::path(chosen.get()); } const auto imported = store.import_file(selected); files_select(imported.id); files_feedback_ = "Datei importiert; die aktive Karriere bleibt unverändert."; refresh(); return true; } if (action == "files-export" && files_entry_ && files_entry_->valid()) { const auto row = *files_entry_; fs::path destination = config_.file_export; if (destination.empty()) { NFD::Guard guard; NFD::UniquePath chosen; const nfdfilteritem_t filters[] = {{"OpenBMH UI-Demodatei", "json"}}; const auto result = NFD::SaveDialog(chosen, filters, 1, nullptr, "openbmh-ui-demo.json"); if (result == NFD_CANCEL) return true; if (result != NFD_OKAY) throw editor::Error("Die Dateiauswahl konnte nicht geöffnet werden."); destination = editor::path(chosen.get()); } const auto export_record = [this, row, destination] { if (!files_bound()) throw editor::Error("Die aktive Karriere wurde inzwischen geändert."); files_store().export_file(row.id, row.token, destination, true); files_feedback_ = "Datei exportiert."; refresh(); }; if (fs::exists(destination)) { confirmation("Vorhandene Exportdatei ersetzen?", export_record); popup_accept_ = "Überschreiben"; } else { store.export_file(row.id, row.token, destination); files_feedback_ = "Datei exportiert."; refresh(); } return true; } } catch (const std::exception &e) { message("Dateiaktion nicht möglich: " + std::string(e.what())); } return true; } bool App::files_change(Rml::Element *target) { if (!target || target->GetId() != "files-name") return false; const auto *input = dynamic_cast(target); if (input) files_name_ = input->GetValue(); if (auto *button = element("files-confirm")) { bool valid = true; try { DemoFiles::clean_name(files_name_); } catch (...) { valid = false; } if (valid && files_mode_.starts_with("view-") && DemoFiles::name_key(files_name_, true) == "standard") valid = false; if (valid) button->RemoveAttribute("disabled"); else button->SetAttribute("disabled", "disabled"); } return true; } bool App::files_event(Rml::Event &event) { if (files_mode_.empty() || !popup_.empty()) return false; if (event.GetType() == "input" || event.GetType() == "change") return files_change(event.GetTargetElement()); if (event.GetType() != "keydown") return false; const auto key = event.GetParameter("key_identifier", 0); auto *target = event.GetTargetElement(); if (key == Rml::Input::KI_ESCAPE) { event.StopPropagation(); return files_action("files-close", target); } if (target && target->GetId() == "files-name" && (key == Rml::Input::KI_RETURN || key == Rml::Input::KI_NUMPADENTER)) { event.StopPropagation(); return files_action("files-save", target); } if (!target || !target->HasAttribute("data-file-row")) return false; if (key != Rml::Input::KI_UP && key != Rml::Input::KI_DOWN && key != Rml::Input::KI_HOME && key != Rml::Input::KI_END) return false; std::vector> ids; if (files_mode_ == "save" || files_mode_ == "view-save") ids.emplace_back("", "files-new"); if (files_mode_ == "view-load") ids.emplace_back("standard", "files-standard"); for (size_t i = 0; i < files_rows_.size(); ++i) ids.emplace_back(files_rows_[i].id, "files-entry-" + std::to_string(i)); if (ids.empty()) return false; size_t index = 0; for (size_t i = 0; i < ids.size(); ++i) if (ids[i].first == files_selected_) index = i; if (key == Rml::Input::KI_HOME) index = 0; else if (key == Rml::Input::KI_END) index = ids.size() - 1; else if (key == Rml::Input::KI_DOWN) index = std::min(index + 1, ids.size() - 1); else if (index) --index; files_select(ids[index].first); focus_after_render_ = ids[index].second; event.StopPropagation(); return true; }