461 lines
15 KiB
C++
461 lines
15 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
void App::front_invalidate_world() {
|
|
ticket_.reset();
|
|
source_.reset();
|
|
world_.reset();
|
|
bind_world(nullptr);
|
|
reset_files();
|
|
reset_settings();
|
|
reset_news();
|
|
desktop_ = Json::object();
|
|
saved_desktop_ = Json::object();
|
|
working_desktop_initialized_ = false;
|
|
working_desktop_base_ = Json();
|
|
files_default_key_.clear();
|
|
files_default_picture_ = Picture();
|
|
files_default_pending_ = false;
|
|
undo_.clear();
|
|
undo_themes_.clear();
|
|
pending_selects_.clear();
|
|
amount_.reset();
|
|
}
|
|
void App::front_start(std::function<FrontResult()> work) {
|
|
if (front_busy_)
|
|
return;
|
|
front_busy_ = true;
|
|
front_dialog_.clear();
|
|
front_feedback_ = "Dateien werden verarbeitet …";
|
|
front_job_ = std::async(std::launch::async, std::move(work));
|
|
refresh();
|
|
}
|
|
void App::front_poll() {
|
|
if (!front_busy_ || !front_job_.valid())
|
|
return;
|
|
if (front_job_.wait_for(std::chrono::milliseconds(0)) !=
|
|
std::future_status::ready) {
|
|
if (front_progress_) {
|
|
std::lock_guard lock(front_progress_->mutex);
|
|
if (!front_progress_->text.empty() &&
|
|
front_progress_->text != front_feedback_) {
|
|
front_feedback_ = front_progress_->text;
|
|
if (auto *text = element("mod-feedback"))
|
|
text->SetInnerRML(esc(front_feedback_));
|
|
dirty_ = true;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
const auto previous = active_mod_;
|
|
front_busy_ = false;
|
|
try {
|
|
auto result = front_job_.get();
|
|
if (result.plan) {
|
|
front_plan_ = std::move(result.plan);
|
|
front_name_ = front_plan_->metadata.name;
|
|
front_dialog_ = "name";
|
|
front_feedback_.clear();
|
|
front_selection_token_ = front_snapshot_.selection_token;
|
|
front_validate_name();
|
|
focus_after_render_ = "front-mod-name";
|
|
front_select_name_ = true;
|
|
} else {
|
|
if (result.activate)
|
|
front_external_override_ = false;
|
|
selected_mod_ = result.selected;
|
|
collect_mods();
|
|
front_plan_.reset();
|
|
front_target_.reset();
|
|
front_feedback_ = result.feedback;
|
|
if ((!front_external_override_ && previous != active_mod_) ||
|
|
(world_ && !fs::exists(world_->source()))) {
|
|
front_external_override_ = false;
|
|
front_invalidate_world();
|
|
}
|
|
front_select(selected_mod_);
|
|
}
|
|
} catch (const std::exception &error) {
|
|
front_error_ = error.what();
|
|
front_dialog_ = "error";
|
|
front_plan_.reset();
|
|
front_target_.reset();
|
|
try {
|
|
collect_mods();
|
|
if (!front_external_override_ && previous != active_mod_)
|
|
front_invalidate_world();
|
|
} catch (const std::exception &) { /* The original error remains visible. */
|
|
}
|
|
}
|
|
front_progress_.reset();
|
|
refresh();
|
|
}
|
|
void App::front_validate_name() {
|
|
front_name_error_.clear();
|
|
try {
|
|
const auto clean = UiModLibrary::clean_name(front_name_),
|
|
key = UiModLibrary::name_key(clean);
|
|
for (const auto &entry : front_snapshot_.entries) {
|
|
if (front_dialog_ == "rename" && front_target_ &&
|
|
entry.id == front_target_->id)
|
|
continue;
|
|
std::string existing;
|
|
try {
|
|
existing = UiModLibrary::name_key(entry.name);
|
|
} catch (const std::exception &) {
|
|
continue;
|
|
}
|
|
if (existing == key)
|
|
throw editor::Error("Diese Mod-Bezeichnung wird bereits verwendet.");
|
|
}
|
|
} catch (const std::exception &error) {
|
|
front_name_error_ = error.what();
|
|
}
|
|
if (auto *error = element("front-name-error"))
|
|
error->SetInnerRML(esc(front_name_error_));
|
|
if (auto *button = element("front-name-confirm")) {
|
|
if (front_name_error_.empty() && !front_busy_)
|
|
button->RemoveAttribute("disabled");
|
|
else
|
|
button->SetAttribute("disabled", true);
|
|
}
|
|
dirty_ = true;
|
|
}
|
|
void App::front_select(const std::string &id) {
|
|
selected_mod_ = id;
|
|
front_reveal_selection_ = true;
|
|
for (size_t i = 0; i < front_snapshot_.entries.size(); ++i)
|
|
if (front_snapshot_.entries[i].id == id)
|
|
focus_after_render_ = "mod-entry-" + std::to_string(i);
|
|
refresh();
|
|
}
|
|
void App::front_after_render() {
|
|
if (scene_ != "mods")
|
|
return;
|
|
if (front_select_name_) {
|
|
if (auto *input = dynamic_cast<Rml::ElementFormControlInput *>(
|
|
element("front-mod-name")))
|
|
input->Select();
|
|
front_select_name_ = false;
|
|
}
|
|
if (auto *list = element("mod-list")) {
|
|
if (front_restore_scroll_) {
|
|
list->SetScrollTop(front_list_scroll_);
|
|
front_restore_scroll_ = false;
|
|
}
|
|
if (front_reveal_selection_) {
|
|
for (size_t i = 0; i < front_snapshot_.entries.size(); ++i)
|
|
if (front_snapshot_.entries[i].id == selected_mod_)
|
|
if (auto *row = element("mod-entry-" + std::to_string(i)))
|
|
row->ScrollIntoView(
|
|
Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest));
|
|
front_reveal_selection_ = false;
|
|
}
|
|
}
|
|
if (!front_dialog_.empty()) {
|
|
Rml::ElementList behind;
|
|
doc_->QuerySelectorAll(
|
|
behind, ".front-mod-content button,.front-mod-window>.titlebar button");
|
|
for (auto *control : behind) {
|
|
control->SetAttribute("disabled", true);
|
|
control->SetProperty("pointer-events", "none");
|
|
}
|
|
}
|
|
}
|
|
void App::front_pick(const std::string &kind) {
|
|
if (front_busy_)
|
|
return;
|
|
fs::path selected = config_.mod_import;
|
|
if (selected.empty()) {
|
|
NFD::Guard guard;
|
|
NFD::UniquePath chosen;
|
|
const nfdfilteritem_t filters[] = {{"OpenBMH-Weltpaket", "obw,zip"}};
|
|
const auto result = kind == "package" ? NFD::OpenDialog(chosen, filters, 1)
|
|
: NFD::PickFolder(chosen);
|
|
if (result == NFD_CANCEL)
|
|
return;
|
|
if (result != NFD_OKAY) {
|
|
front_error_ = "Die Dateiauswahl konnte nicht geöffnet werden.";
|
|
front_dialog_ = "error";
|
|
refresh();
|
|
return;
|
|
}
|
|
selected = editor::path(chosen.get());
|
|
}
|
|
const auto root = front_library_root();
|
|
front_start([root, selected] {
|
|
UiModLibrary library(root);
|
|
FrontResult result;
|
|
result.plan = library.inspect(selected);
|
|
return result;
|
|
});
|
|
}
|
|
bool App::front_change(Rml::Element *target) {
|
|
if (!target || target->GetId() != "front-mod-name" || front_busy_)
|
|
return false;
|
|
front_name_ = field("front-mod-name");
|
|
front_validate_name();
|
|
return true;
|
|
}
|
|
bool App::front_action(const std::string &action, Rml::Element *target) {
|
|
const bool front = scene_ == "main" || scene_ == "mods";
|
|
if (!front)
|
|
return false;
|
|
if (front_busy_) {
|
|
if (action != "")
|
|
return true;
|
|
return false;
|
|
}
|
|
const auto id =
|
|
target ? target->GetAttribute<Rml::String>("data-id", "") : std::string();
|
|
if (action == "load") {
|
|
if (auto *active = mod(active_mod_);
|
|
active && active->type == "Classic" && active->error.empty()) {
|
|
dispatch("new", target);
|
|
return true;
|
|
}
|
|
}
|
|
if (action == "mods") {
|
|
collect_mods();
|
|
front_tab_ = "installed";
|
|
front_dialog_.clear();
|
|
front_plan_.reset();
|
|
front_feedback_.clear();
|
|
front_list_scroll_ = 0;
|
|
if (!front_selected())
|
|
selected_mod_ = front_snapshot_.entries.empty()
|
|
? ""
|
|
: front_snapshot_.entries.front().id;
|
|
scene_ = "mods";
|
|
focus_after_render_ = "mod-installed";
|
|
refresh();
|
|
return true;
|
|
}
|
|
if (action == "front-home" || (action == "close" && scene_ == "mods")) {
|
|
scene_ = "main";
|
|
front_dialog_.clear();
|
|
front_plan_.reset();
|
|
front_target_.reset();
|
|
focus_after_render_ = "front-mods";
|
|
refresh();
|
|
return true;
|
|
}
|
|
if (action == "front-tab") {
|
|
if (auto *list = element("mod-list"))
|
|
front_list_scroll_ = list->GetScrollTop();
|
|
if (id == "installed" || id == "downloads") {
|
|
front_tab_ = id;
|
|
front_restore_scroll_ = true;
|
|
focus_after_render_ =
|
|
id == "installed" ? "mod-installed" : "mod-downloads";
|
|
refresh();
|
|
}
|
|
return true;
|
|
}
|
|
if (action == "mod-select") {
|
|
front_select(id);
|
|
return true;
|
|
}
|
|
if (action == "front-import") {
|
|
front_dialog_ = "choice";
|
|
front_return_focus_ = "front-import-button";
|
|
refresh();
|
|
return true;
|
|
}
|
|
if (action == "front-pick") {
|
|
front_pick(id);
|
|
return true;
|
|
}
|
|
if (action == "front-dismiss") {
|
|
front_dialog_.clear();
|
|
front_plan_.reset();
|
|
front_target_.reset();
|
|
front_name_error_.clear();
|
|
focus_after_render_ =
|
|
front_return_focus_.empty() ? "mod-installed" : front_return_focus_;
|
|
refresh();
|
|
return true;
|
|
}
|
|
if (action == "front-name-confirm") {
|
|
front_name_ = field("front-mod-name");
|
|
front_validate_name();
|
|
if (!front_name_error_.empty())
|
|
return true;
|
|
const auto name = UiModLibrary::clean_name(front_name_);
|
|
const auto root = front_library_root();
|
|
if (front_dialog_ == "name" && front_plan_) {
|
|
const auto plan = *front_plan_;
|
|
front_progress_ = std::make_shared<FrontProgress>();
|
|
const auto progress = front_progress_;
|
|
front_start([root, plan, name, progress] {
|
|
UiModLibrary library(root);
|
|
const auto row =
|
|
library.commit(plan, name, [progress](const std::string &text) {
|
|
std::lock_guard lock(progress->mutex);
|
|
progress->text = text;
|
|
});
|
|
return FrontResult{{},
|
|
row.id,
|
|
"Mod importiert. Du kannst sie jetzt aktivieren.",
|
|
false};
|
|
});
|
|
} else if (front_dialog_ == "rename" && front_target_) {
|
|
const auto row = *front_target_;
|
|
const auto token = front_selection_token_;
|
|
front_start([root, row, name, token] {
|
|
UiModLibrary library(root);
|
|
const auto renamed = library.rename(row, name, token);
|
|
return FrontResult{{}, renamed.id, "Mod umbenannt.", false};
|
|
});
|
|
}
|
|
return true;
|
|
}
|
|
if (action == "mod-rename") {
|
|
if (auto *entry = front_selected()) {
|
|
front_target_ = *entry;
|
|
front_selection_token_ = front_snapshot_.selection_token;
|
|
front_name_ = entry->name;
|
|
front_dialog_ = "rename";
|
|
front_return_focus_ = "mod-rename-button";
|
|
front_validate_name();
|
|
focus_after_render_ = "front-mod-name";
|
|
front_select_name_ = true;
|
|
refresh();
|
|
}
|
|
return true;
|
|
}
|
|
if (action == "mod-remove") {
|
|
if (auto *entry = front_selected()) {
|
|
front_target_ = *entry;
|
|
front_selection_token_ = front_snapshot_.selection_token;
|
|
front_dialog_ = "delete";
|
|
front_return_focus_ = "mod-remove-button";
|
|
refresh();
|
|
}
|
|
return true;
|
|
}
|
|
if (action == "front-delete-confirm" && front_target_) {
|
|
const auto entry = *front_target_;
|
|
const auto token = front_selection_token_;
|
|
const auto root = front_library_root();
|
|
front_start([root, entry, token] {
|
|
UiModLibrary library(root);
|
|
library.remove(entry, token);
|
|
return FrontResult{
|
|
{}, "", "Mod entfernt. Persönliche Daten wurden erhalten.", false};
|
|
});
|
|
return true;
|
|
}
|
|
if (action == "mod-activate") {
|
|
if (auto *entry = front_selected(); entry && entry->valid()) {
|
|
const auto row = *entry;
|
|
const auto token = front_snapshot_.selection_token;
|
|
const auto root = front_library_root();
|
|
front_start([root, row, token] {
|
|
UiModLibrary library(root);
|
|
library.activate(row, token);
|
|
return FrontResult{{}, row.id, "Mod aktiviert.", true};
|
|
});
|
|
}
|
|
return true;
|
|
}
|
|
if (action == "front-noop" || action == "network")
|
|
return true;
|
|
return false;
|
|
}
|
|
bool App::front_event(Rml::Event &event) {
|
|
if (scene_ != "main" && scene_ != "mods")
|
|
return false;
|
|
if (!popup_.empty() || !menu_.empty())
|
|
return false;
|
|
const auto type = event.GetType();
|
|
auto *target = event.GetTargetElement();
|
|
if ((type == "input" || type == "change") && front_change(target))
|
|
return true;
|
|
if (type != "keydown")
|
|
return false;
|
|
const auto key =
|
|
Rml::Input::KeyIdentifier(event.GetParameter<int>("key_identifier", 0));
|
|
if (front_busy_) {
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
if (key == Rml::Input::KI_ESCAPE) {
|
|
if (!front_dialog_.empty())
|
|
front_action("front-dismiss", nullptr);
|
|
else if (scene_ == "mods")
|
|
front_action("front-home", nullptr);
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
if (!front_dialog_.empty() && target->GetId() == "front-mod-name" &&
|
|
(key == Rml::Input::KI_RETURN || key == Rml::Input::KI_NUMPADENTER)) {
|
|
front_action("front-name-confirm", nullptr);
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
if (key == Rml::Input::KI_TAB) {
|
|
Rml::ElementList controls;
|
|
auto *region = scene_ == "main" ? doc_->QuerySelector(".main-options")
|
|
: !front_dialog_.empty()
|
|
? doc_->QuerySelector(".front-dialog")
|
|
: doc_->QuerySelector(".front-mod-window");
|
|
if (region)
|
|
region->QuerySelectorAll(controls, "button,input,select");
|
|
std::erase_if(controls, [](auto *node) {
|
|
return node->HasAttribute("disabled") || !node->IsVisible(true);
|
|
});
|
|
if (!controls.empty()) {
|
|
const auto it = std::find(controls.begin(), controls.end(),
|
|
context_->GetFocusElement());
|
|
const int pos = it == controls.end() ? -1 : int(it - controls.begin()),
|
|
n = int(controls.size());
|
|
const bool reverse = event.GetParameter<int>("shift_key", 0) != 0;
|
|
controls[size_t(pos < 0 ? (reverse ? n - 1 : 0)
|
|
: (pos + (reverse ? -1 : 1) + n) % n)]
|
|
->Focus(true);
|
|
}
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
if (front_dialog_.empty() &&
|
|
(key == Rml::Input::KI_UP || key == Rml::Input::KI_DOWN ||
|
|
key == Rml::Input::KI_HOME || key == Rml::Input::KI_END)) {
|
|
if (scene_ == "main") {
|
|
Rml::ElementList options;
|
|
doc_->QuerySelectorAll(options, ".main-options button");
|
|
std::erase_if(options,
|
|
[](auto *n) { return n->HasAttribute("disabled"); });
|
|
if (!options.empty()) {
|
|
const auto it = std::find(options.begin(), options.end(),
|
|
context_->GetFocusElement());
|
|
const int n = int(options.size()),
|
|
pos = it == options.end() ? 0 : int(it - options.begin());
|
|
const int index =
|
|
key == Rml::Input::KI_HOME ? 0
|
|
: key == Rml::Input::KI_END
|
|
? n - 1
|
|
: (pos + (key == Rml::Input::KI_UP ? -1 : 1) + n) % n;
|
|
options[size_t(index)]->Focus(true);
|
|
}
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
if (target->IsClassSet("front-mod-entry")) {
|
|
size_t index = 0;
|
|
for (size_t i = 0; i < front_snapshot_.entries.size(); ++i)
|
|
if (front_snapshot_.entries[i].id == selected_mod_)
|
|
index = i;
|
|
if (!front_snapshot_.entries.empty()) {
|
|
index = key == Rml::Input::KI_HOME ? 0
|
|
: key == Rml::Input::KI_END ? front_snapshot_.entries.size() - 1
|
|
: key == Rml::Input::KI_UP
|
|
? (index ? index - 1 : 0)
|
|
: std::min(index + 1, front_snapshot_.entries.size() - 1);
|
|
front_select(front_snapshot_.entries[index].id);
|
|
}
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|