Bind native desk and training camps to Modern OBS careers (by Codex)

This commit is contained in:
Codex committed 2026-10-27 22:00:10 +01:00
1 parent 1fb6499b15
commit 85a1fe5a95
19 files changed
+733 -46

No files matched your search

+147
View File
@@ -0,0 +1,147 @@
// SPDX-License-Identifier: GPL-3.0-only
void App::engine_start(std::string label, std::function<EngineReply()> job, bool mutating) {
if (engine_busy_) return;
engine_base_dirty_ = engine_dirty_;
if (mutating) { engine_dirty_ = true; engine_fresh_ = false; }
engine_operation_ = std::move(label); engine_busy_ = true;
engine_job_ = std::async(std::launch::async, std::move(job)); refresh();
}
void App::engine_poll() {
if (!engine_busy_ || engine_job_.wait_for(std::chrono::milliseconds(0)) != std::future_status::ready) return;
engine_busy_ = false;
try {
auto result = engine_job_.get();
validate_engine_desk(result.bundle);
engine_fresh_ = true;
if (result.catalog) { world_ = std::move(result.catalog); bind_world(world_); }
engine_link_ = std::move(result.connection); engine_data_ = std::move(result.bundle);
engine_response_ = std::move(result.response);
if (engine_response_.value("messageType", "") == "command_result") {
engine_dirty_ = true; engine_notice_ = "Trainingslager gebucht · Änderungen noch nicht gespeichert.";
} else if (engine_response_.value("messageType", "") == "error") {
engine_dirty_ = engine_base_dirty_;
engine_notice_ = "Buchung abgewiesen: " + engine_response_.value("code", "unknown_error");
} else if (engine_response_.value("messageType", "") == "saved") {
engine_dirty_ = false; engine_notice_ = "Spielstand als neue OBS-Datei gespeichert.";
} else engine_notice_ = "Spielstand geladen · Weitere Menüs werden schrittweise angebunden.";
const auto &f = engine_data_.at("camps").at("data").at("focus");
engine_focus_ = {{"condition", f.at("condition")}, {"technique", f.at("technique")}, {"play", f.at("play")}};
if (engine_camp_.empty() && !engine_data_.at("camps").at("data").at("camps").empty())
engine_camp_ = engine_data_.at("camps").at("data").at("camps").front().at("id");
} catch (const std::exception &e) { engine_notice_ = e.what(); }
refresh();
}
void App::engine_frame() {
engine_poll();
if (needs_render_) engine_render();
render_popup(); context_->Update();
if (popup_.empty() && !focus_after_render_.empty()) {
if (auto *node = element(focus_after_render_)) node->Focus(keyboard_navigation_);
focus_after_render_.clear();
}
renderer_.Clear(); renderer_.BeginFrame(); context_->Render(); renderer_.EndFrame(); video_.present(); dirty_ = false;
}
void App::engine_save() {
if (engine_busy_ || !engine_link_) return;
auto output = config_.file_export;
if (output.empty()) {
NFD::Guard guard; NFD::UniquePath chosen;
const nfdfilteritem_t filters[] = {{"OpenBMH-Spielstand", "obs"}};
const auto result = NFD::SaveDialog(chosen, filters, 1, nullptr, "openbmh-career.obs");
if (result == NFD_CANCEL) return;
if (result != NFD_OKAY) throw editor::Error("Die Dateiauswahl konnte nicht geöffnet werden.");
output = editor::path(chosen.get());
}
auto link = engine_link_;
engine_start("Spielstand wird gespeichert …", [link, output] { auto response = link->save_copy(output); return EngineReply{link, link->snapshot(), response, nullptr}; });
}
void App::engine_quit() {
if (engine_busy_) { engine_notice_ = "Bitte den laufenden Vorgang abwarten."; dirty_ = true; return; }
if (engine_dirty_) confirmation("Ungespeicherte Änderungen verwerfen und beenden? Über die Diskette kannst du zuvor eine neue OBS-Datei speichern.", [this] { closed_ = true; });
else closed_ = true;
}
void App::engine_event(Rml::Event &event) {
const auto type = event.GetType(); auto *target = event.GetTargetElement();
if (type == "keydown") keyboard_navigation_ = true;
if (type == "mousedown") keyboard_navigation_ = false;
Rml::ElementFormControl *range = nullptr;
for (auto *node = target; node && node != doc_; node = node->GetParentNode())
if (node->GetId().starts_with("camp-focus-") && !node->HasAttribute("disabled")) {
range = dynamic_cast<Rml::ElementFormControl *>(node); break;
}
if (popup_.empty() && !engine_busy_ && range && type == "mouseup") {
focus_after_render_ = range->GetId(); dirty_ = true;
}
if (popup_.empty() && !engine_busy_ && range && type == "keydown") {
const auto key = Rml::Input::KeyIdentifier(event.GetParameter<int>("key_identifier", 0));
if (key == Rml::Input::KI_HOME || key == Rml::Input::KI_END || key == Rml::Input::KI_LEFT || key == Rml::Input::KI_RIGHT || key == Rml::Input::KI_UP || key == Rml::Input::KI_DOWN) {
const int index = range->GetAttribute<int>("data-id", -1);
if (index >= 0 && index < 3) {
const int value = key == Rml::Input::KI_HOME ? 0 : key == Rml::Input::KI_END ? 15 : std::clamp(std::stoi(range->GetValue()) + (key == Rml::Input::KI_LEFT || key == Rml::Input::KI_DOWN ? -1 : 1), 0, 15);
range->SetValue(std::to_string(value));
engine_focus_[std::array<const char *, 3>{"condition", "technique", "play"}[size_t(index)]] = value;
element("camp-value-" + std::to_string(index))->SetInnerRML(std::to_string(value) + " / 15");
element("camp-value-3")->SetInnerRML("—"); element("camp-focus-3")->SetProperty("visibility", "hidden");
dirty_ = true; event.StopImmediatePropagation(); return;
}
}
}
if (type == "keydown" && event.GetParameter<int>("key_identifier", 0) == Rml::Input::KI_ESCAPE) {
if (!popup_.empty()) { popup_.clear(); confirm_ = {}; dirty_ = true; }
else if (!engine_busy_ && !menu_.empty()) { menu_.clear(); refresh(); }
else engine_quit();
event.StopImmediatePropagation(); return;
}
if (type == "keydown" && event.GetParameter<int>("key_identifier", 0) == Rml::Input::KI_TAB) {
auto *root = !popup_.empty() ? element("popup") : !menu_.empty() ? doc_->QuerySelector(".window") : element("canvas"); Rml::ElementList controls;
if (!root) return;
root->QuerySelectorAll(controls, "button,input");
controls.erase(std::remove_if(controls.begin(), controls.end(), [](auto *node) { return !node->IsVisible(true) || node->HasAttribute("disabled"); }), controls.end());
if (!controls.empty()) {
const auto found = std::find(controls.begin(), controls.end(), context_->GetFocusElement());
const int index = found == controls.end() ? -1 : int(found - controls.begin());
controls[size_t((index + int(controls.size()) + (event.GetParameter<int>("shift_key", 0) ? -1 : 1)) % int(controls.size()))]->Focus(true);
}
event.StopImmediatePropagation(); return;
}
if (engine_busy_) return;
if ((type == "input" || type == "change") && menu_ == "camp" && popup_.empty() && target->GetId().starts_with("camp-focus-")) {
const int index = target->GetAttribute<int>("data-id", -1);
if (index < 0 || index > 2) return;
const int value = std::stoi(dynamic_cast<Rml::ElementFormControl *>(target)->GetValue());
if (value < 0 || value > 15) return;
engine_focus_[std::array<const char *, 3>{"condition", "technique", "play"}[size_t(index)]] = value;
element("camp-value-" + std::to_string(index))->SetInnerRML(std::to_string(value) + " / 15");
element("camp-value-3")->SetInnerRML("—");
element("camp-focus-3")->SetProperty("visibility", "hidden"); dirty_ = true; return;
}
if (type != "click") return;
for (auto *node = target; node && node != doc_; node = node->GetParentNode()) {
if (!node->HasAttribute("data-action") || node->HasAttribute("disabled")) continue;
const auto action = node->GetAttribute<Rml::String>("data-action", ""), id = node->GetAttribute<Rml::String>("data-id", "");
if (!popup_.empty()) {
if (action == "popup-close") { popup_.clear(); confirm_ = {}; dirty_ = true; }
if (action == "popup-confirm") { auto callback = std::move(confirm_); popup_.clear(); dirty_ = true; if (callback) callback(); }
return;
}
if (action == "close") { menu_.clear(); refresh(); }
else if (action == "engine-open" && id == "save") engine_save();
else if (action == "engine-open" && id == "camp") { menu_ = "camp"; refresh(); }
else if (action == "camp-region") { selections_["camp-region"] = id; refresh(); }
else if (action == "camp-select") { engine_camp_ = id; refresh(); }
else if (action == "camp-remember" && engine_fresh_) {
const auto command = engine_camp_command(engine_data_, engine_camp_, engine_focus_, editor::new_id("request:ui-"));
const auto &camps = engine_data_.at("camps").at("data").at("camps");
const auto chosen = std::find_if(camps.begin(), camps.end(), [&](const auto &camp) { return camp.at("id") == engine_camp_; });
confirmation(engine_text(chosen->at("name")) + " für " + engine_price(chosen->at("quote").at("total")) + " verbindlich buchen?", [this, command] {
auto link = engine_link_;
engine_start("Trainingslager wird gebucht …", [link, command] { auto response = link->command(command); return EngineReply{link, link->snapshot(), response, nullptr}; }, true);
});
}
return;
}
}
Json App::engine_snapshot() const {
return {{"mode", engine_mode_}, {"busy", engine_busy_}, {"dirty", engine_dirty_}, {"fresh", engine_fresh_}, {"bundle", engine_data_},
{"response", engine_response_}, {"focus", engine_focus_}, {"selected_camp", engine_camp_}, {"notice", engine_notice_}};
}