// SPDX-License-Identifier: GPL-3.0-only // Input and navigation share the same paths for human and scripted events. void App::context_menu(const std::string &id, int x, int y) { init_desktop(); context_id_ = id; context_x_ = x; context_y_ = y; context_open_ = true; context_dirty_ = true; dirty_ = true; } void App::render_context() { auto *root = element("desktop-context"); root->SetProperty("display", context_open_ ? "block" : "none"); if (!context_open_ || !context_dirty_) return; const auto item=[&](const std::string &action,const std::string &label,const std::string &symbol,const std::string &extra=std::string()){ return btn(action,glyph(symbol,19./scale_)+""+esc(label)+""+((extra.find("aria-checked=\"true\"")!=std::string::npos||extra.find("data-checked=\"true\"")!=std::string::npos)?""+glyph("check",14./scale_)+"":""),"context-action",extra); }; std::string body=item("context-undo","Rückgängig","undo",undo_.empty()?"disabled":""); if(!context_id_.empty()){ const auto &o=desktop_.at("objects").at(context_id_); const bool pinned=o.value("pinned",true); body+=item("context-pin",pinned?"Fixierung lösen":"Fixieren","lock"); if(o.value("widget",false)){ body+="
Widgetgröße
"; for(const auto &d:widget_definitions())if(d.id==context_id_)for(const auto &[w,h]:d.sizes){ const bool selected=o.at("w")==w*248-24 && o.at("h")==h*248-24; body+=item("context-size",std::to_string(w)+" × "+std::to_string(h),"grid","data-id=\""+std::to_string(w)+"x"+std::to_string(h)+"\" "+(pinned?"disabled":"")+(selected?" data-checked=\"true\"":"")); } body+=item("context-remove","Widget entfernen","trash",pinned?"disabled":""); } }else{ bool pinned=true;for(const auto &[id,o]:desktop_.at("objects").items())if(o.at("visible")==true&&o.at("pinned")==false)pinned=false; body+=item("context-pin-all","Alles fixieren","lock",std::string("role=\"menuitemcheckbox\" aria-checked=\"")+(pinned?"true":"false")+"\""); body+=item("context-arrange","Am Raster anordnen","grid"); body+=item("context-grid",desktop_.value("grid",true)?"Raster deaktivieren":"Raster aktivieren","grid",std::string("role=\"menuitemcheckbox\" aria-checked=\"")+(desktop_.value("grid",true)?"true":"false")+"\""); body+=item("context-widgets","Widget hinzufügen","plus")+item("context-view-save","Ansicht speichern","save")+item("context-view-load","Ansicht laden","folder")+item("context-background","Hintergrundbild ändern","image")+item("context-quit","Spiel beenden","power"); } root->SetInnerRML(body); context_->Update(); const auto size = context_->GetDimensions(); root->SetProperty( "left", scalar(std::clamp(context_x_, 8, std::max(8, size.x - 278))) + "px"); root->SetProperty( "top", scalar(std::clamp( context_y_, 8, std::max(8, size.y - int(root->GetBox().GetSize(Rml::BoxArea::Border).y) - 8))) + "px"); Rml::ElementList buttons;root->QuerySelectorAll(buttons,"button");for(auto *button:buttons)if(!button->HasAttribute("disabled")){button->Focus(true);break;} context_dirty_ = false; } void App::drag_event(Rml::Event &event) { const auto type = event.GetType(); if (type == "mousedown") suppress_click_ = false; const float x = event.GetParameter("mouse_x", 0), y = event.GetParameter("mouse_y", 0); if (type == "mousedown" && event.GetParameter("button", 0) == 0 && scene_ == "desktop" && menu_.empty() && popup_.empty()) { auto *target = event.GetTargetElement(); bool interactive = false; for (auto *n = target; n && n != doc_; n = n->GetParentNode()) { if (n->HasAttribute("data-interactive")) interactive = true; if (n->HasAttribute("data-object")) { if (interactive) return; const auto id = n->GetAttribute("data-object", ""); const auto &o = desktop_["objects"].at(id); drag_ = {true, false, id, x, y, o.value("x", 0.0), o.value("y", 0.0)}; drag_.pinned=o.value("pinned",true);drag_undo_size_=undo_.size(); return; } } } if (!drag_.active) return; if (type == "mousemove") { if (!drag_.moved && std::hypot(x - drag_.x, y - drag_.y) < 6) return; if(drag_.pinned){drag_.moved=true;return;} if (!drag_.moved) { desktop_checkpoint(desktop_); desktop_raise(drag_.id); drag_.moved = true; } auto &o = desktop_["objects"][drag_.id]; if (o.value("pinned", true)) return; const double left = std::clamp(drag_.left + (x - drag_.x) / scale_, 0.0, 3840.0 - o.value("w", 224)); const double top = std::clamp(drag_.top + (y - drag_.y) / scale_, 224.0, double(desktop_height()) - o.value("h", 224)); o["x"] = left; o["y"] = top; if (auto *n = element("object-" + drag_.id)) { n->SetProperty("left", scalar(left) + "dp"); n->SetProperty("top", scalar(top) + "dp"); } dirty_ = true; } if (type == "mouseup") { if(drag_.pinned){suppress_click_=drag_.moved;drag_.active=false;return;} if (drag_.moved) { suppress_click_ = true; auto &o = desktop_["objects"][drag_.id]; auto proposed = undo_.back(); const int x = o.value("x", 64), y = o.value("y", 224); const auto &trash=desktop_.at("objects").at("trash"); const auto px=(event.GetParameter("mouse_x",0)-canvas_left_)/scale_,py=(event.GetParameter("mouse_y",0)-canvas_top_)/scale_; const bool remove=drag_.id!="trash"&&o.value("widget",false)&&trash.at("visible")==true&&px>=trash.at("x").get()&&px<=trash.at("x").get()+trash.at("w").get()&&py>=trash.at("y").get()&&py<=trash.at("y").get()+trash.at("h").get(); if(remove){proposed["objects"][drag_.id]["visible"]=false;desktop_=std::move(proposed);} else if (place_desktop_object(proposed, drag_.id, x, y)) {desktop_ = std::move(proposed);desktop_raise(drag_.id);} else {desktop_ = undo_.back();if(undo_.size()>drag_undo_size_){undo_.pop_back();if(!undo_themes_.empty())undo_themes_.pop_back();}} refresh(); } drag_.active = false; } } void App::ProcessEvent(Rml::Event &event) try { const auto type = event.GetType(); if (notebook_home_event(event)) return; if (tactics_page_event(event)) return; if (engine_mode_) { engine_event(event); return; } // Native widgets can emit intermediate selected values while their options // are being constructed. Opening a squad menu must stay read-only. if (setting_menu_markup_ && (menu_ == "lineup" || menu_ == "transfer" || menu_ == "news" || menu_ == "settings" || menu_=="widgets" || menu_=="save" || menu_=="load" || menu_=="view-save" || menu_=="view-load") && (type == "change" || type == "input")) return; if (setting_menu_markup_ && (scene_ == "manager" || scene_ == "newgame") && (type == "change" || type == "input")) return; if (type == "keydown") keyboard_navigation_ = true; else if (type == "mousedown") keyboard_navigation_ = false; if (type == "blur" && popup_.empty()) bank_event(event); if (type == "blur") { if (auto *select = dynamic_cast( event.GetTargetElement()); select && select->IsSelectBoxVisible()) { select->CancelSelectBox(); dirty_ = true; } return; } for (auto *n = event.GetTargetElement(); n && n != doc_; n = n->GetParentNode()) if (n->GetId() == "native-ticket-layer") return; if(front_event(event))return; if (live_event(event)) return; if(national_event(event))return; if (career_event(event)) return; if (entry_event(event)) return; if (opening_event(event)) return; if (amount_event(event)) return; if (balance_simulation_event(event)) return; if (popup_.empty() && settings_event(event)) return; if (popup_.empty() && (files_event(event) || widget_event(event))) return; if ((menu_ == "lineup" || menu_ == "transfer" || scene_ == "manager") && (type == "change" || type == "input")) { if (auto *select = dynamic_cast(event.GetTargetElement()); select && select->IsSelectBoxVisible()) { pending_selects_.insert(select->GetId()); dirty_ = true; return; } } if (popup_.empty() && amount_trigger_event(event)) return; if (popup_.empty() && report_detail_kind_.empty() && (lineup_event(event) || transfer_event(event))) return; if (popup_.empty() && report_detail_kind_.empty() && (career_report_event(event) || rankings_event(event))) return; if (report_detail_kind_.empty() && stadium_event(event)) return; if (popup_.empty() && contracts_event(event)) return; if (popup_.empty() && (advertising_event(event) || toto_event(event))) return; if (popup_.empty() && (statistics_event(event) || news_event(event))) return; if (popup_.empty() && (bank_event(event) || finance_event(event))) return; if (!planning_dialog_.empty() && popup_.empty()) { const bool key_event = type == "keydown"; const auto key = key_event ? Rml::Input::KeyIdentifier(event.GetParameter("key_identifier", 0)) : Rml::Input::KI_UNKNOWN; if ((key_event && key == Rml::Input::KI_ESCAPE) || (type == "mouseup" && event.GetParameter("button", 0) == 1)) { planning_action("planning-close", nullptr); event.StopImmediatePropagation(); return; } bool inside = false; for (auto *node = event.GetTargetElement(); node && node != doc_; node = node->GetParentNode()) if (node->GetId() == "planning-layer") inside = true; if (!inside && (type == "click" || type == "dblclick" || type == "mousedown" || type == "mouseup" || (key_event && key != Rml::Input::KI_TAB))) { event.StopImmediatePropagation(); return; } } if (!report_detail_kind_.empty() && popup_.empty()) { const bool keyboard = type == "keydown"; const auto key = keyboard ? Rml::Input::KeyIdentifier(event.GetParameter("key_identifier", 0)) : Rml::Input::KI_UNKNOWN; if ((keyboard && key == Rml::Input::KI_ESCAPE) || (type == "mouseup" && event.GetParameter("button", 0) == 1)) { report_detail_action("report-detail-close", nullptr); event.StopImmediatePropagation(); return; } bool inside = false; for (auto *n = event.GetTargetElement(); n && n != doc_; n = n->GetParentNode()) if (n->GetId() == "report-detail-layer") inside = true; if (!inside && (type == "click" || type == "dblclick" || type == "mousedown" || type == "mouseup" || (keyboard && key != Rml::Input::KI_TAB))) { event.StopImmediatePropagation(); return; } } else if (report_detail_kind_.empty() && planning_dialog_.empty() && popup_.empty() && report_picker_event(event)) return; if (menu_ == "almanac" && popup_.empty() && report_picker_.empty()) { const auto key = type == "keydown" ? Rml::Input::KeyIdentifier(event.GetParameter("key_identifier", 0)) : Rml::Input::KI_UNKNOWN; if (key == Rml::Input::KI_ESCAPE || (type == "mouseup" && event.GetParameter("button", 0) == 1)) { almanac_action("alm-back", nullptr); event.StopImmediatePropagation(); return; } if (key == Rml::Input::KI_TAB && context_->GetFocusElement() && context_->GetFocusElement()->GetId() == "alm-heading") { auto *next = event.GetParameter("shift_key", 0) ? element("alm-back") : !almanac_match_.empty() ? element("alm-tab-lineups") : doc_->QuerySelector("#alm-day-list .alm-match-row"); if (!next) next = element("alm-back"); if (next) next->Focus(true); event.StopImmediatePropagation(); return; } if (type == "keydown" && !almanac_match_.empty() && event.GetTargetElement()->GetId().starts_with("alm-tab-") && (key == Rml::Input::KI_LEFT || key == Rml::Input::KI_RIGHT || key == Rml::Input::KI_HOME || key == Rml::Input::KI_END)) { almanac_tab_ = key == Rml::Input::KI_HOME ? "lineups" : key == Rml::Input::KI_END ? "events" : almanac_tab_ == "lineups" ? "events" : "lineups"; focus_after_render_ = "alm-tab-" + almanac_tab_; refresh(); event.StopImmediatePropagation(); return; } } if (menu_ == "friendlies" && planning_dialog_.empty() && popup_.empty() && snap().state.at("calendar").at("friendly_stage") == 1 && ((type == "keydown" && event.GetParameter("key_identifier", 0) == Rml::Input::KI_ESCAPE) || (type == "mouseup" && event.GetParameter("button", 0) == 1))) { planning_action("friendly-back", nullptr); event.StopImmediatePropagation(); return; } if (type == "mouseup" && event.GetParameter("button", 0) == 1) { if (scene_ == "desktop" && !menu_.empty() && popup_.empty()) { menu_.clear(); quit_after_save_=false; quit_closes_app_=false; reset_settings(); refresh(); } else if (scene_ == "desktop" && menu_.empty() && popup_.empty()) { std::string id; for (auto *n = event.GetTargetElement(); n && n != doc_; n = n->GetParentNode()) if (n->HasAttribute("data-object")) { id = n->GetAttribute("data-object", ""); break; } context_menu(id, event.GetParameter("mouse_x", 0), event.GetParameter("mouse_y", 0)); } event.StopPropagation(); return; } if (type == "mousedown" && event.GetParameter("button", 0) == 0 && context_open_) { bool inside = false; for (auto *n = event.GetTargetElement(); n && n != doc_; n = n->GetParentNode()) if (n->GetId() == "desktop-context") inside = true; if (!inside) { context_open_ = false; dirty_ = true; event.StopPropagation(); return; } } if (type == "mousedown" || type == "mousemove" || type == "mouseup") { drag_event(event); return; } auto *target = event.GetTargetElement(); if (type == "contextmenu") { if (scene_ != "desktop" || !menu_.empty() || !popup_.empty()) return; std::string id; for (auto *n = target; n && n != doc_; n = n->GetParentNode()) if (n->HasAttribute("data-object")) { id = n->GetAttribute("data-object", ""); break; } context_menu(id, event.GetParameter("mouse_x", 0), event.GetParameter("mouse_y", 0)); event.StopPropagation(); return; } if (type == "keydown") { const auto key = Rml::Input::KeyIdentifier(event.GetParameter("key_identifier", 0)); Rml::ElementFormControlSelect *select = nullptr; for (auto *n = target; n && n != doc_; n = n->GetParentNode()) if ((select = dynamic_cast(n))) break; if (select && !select->HasAttribute("disabled")) { const bool open = select->IsSelectBoxVisible(); if (open && key == Rml::Input::KI_TAB) { select->CancelSelectBox(); dirty_ = true; } if ((!open && (key == Rml::Input::KI_DOWN || key == Rml::Input::KI_UP)) || key == Rml::Input::KI_RETURN || key == Rml::Input::KI_SPACE) { if (open) select->HideSelectBox(); else { select->ShowSelectBox(); if (key == Rml::Input::KI_UP) select->SetSelection(select->GetNumOptions() - 1); } event.StopImmediatePropagation(); dirty_ = true; return; } if (open && (key == Rml::Input::KI_HOME || key == Rml::Input::KI_END)) { select->SetSelection(key == Rml::Input::KI_HOME ? 0 : select->GetNumOptions() - 1); event.StopImmediatePropagation(); dirty_ = true; return; } } if (key == Rml::Input::KI_RETURN || key == Rml::Input::KI_SPACE) { for (auto *n = target; n && n != doc_; n = n->GetParentNode()) if (n->GetTagName() == "button") { if (!n->HasAttribute("disabled")) n->DispatchEvent("click", {}); event.StopImmediatePropagation(); return; } } if (key == Rml::Input::KI_ESCAPE) { Rml::ElementList selects; doc_->QuerySelectorAll(selects, "select"); for (auto *node : selects) if (auto *select = dynamic_cast(node); select && select->IsSelectBoxVisible()) { select->CancelSelectBox(); select->Focus(true); event.StopImmediatePropagation(); dirty_ = true; return; } if (context_open_) { context_open_ = false; dirty_ = true; } else if (!popup_.empty()) { popup_.clear(); confirm_ = {}; text_accept_ = {}; quit_after_save_ = false; dirty_ = true; } else if (!menu_.empty()) { menu_.clear(); quit_after_save_=false; quit_closes_app_=false; reset_settings(); refresh(); } else if (scene_ == "manager") { scene_ = "newgame"; refresh(); } else if (scene_ == "newgame" || scene_ == "mods") { scene_ = "main"; refresh(); } event.StopPropagation(); return; } if (key == Rml::Input::KI_Z && event.GetParameter("ctrl_key", 0) && scene_ == "desktop" && menu_.empty() && !undo_.empty()) { desktop_ = undo_.back(); undo_.pop_back(); refresh(); event.StopPropagation(); return; } if (key == Rml::Input::KI_TAB && (!menu_.empty() || !popup_.empty() || scene_ != "desktop")) { Rml::ElementList controls; auto *root = !popup_.empty() ? element("popup") : !planning_dialog_.empty() ? element("planning-layer") : !report_detail_kind_.empty() ? element("report-detail-layer") : !career_view_.value("dialog", "").empty() ? element("career-history-layer") : !stadium_view_.value("dialog", "").empty() ? element("stadium-modal") : element("canvas"); root->QuerySelectorAll( controls, !popup_.empty() || !report_detail_kind_.empty() || !planning_dialog_.empty() || !career_view_.value("dialog", "").empty() || !stadium_view_.value("dialog", "").empty() ? "button,input,select" : ".window button,.window input,.window select"); controls.erase(std::remove_if(controls.begin(), controls.end(), [this](auto *n) { return !n->IsVisible(true) || n->HasAttribute("disabled") || n->template GetAttribute("tabindex", 0) < 0 || (menu_ == "scorers" && n->GetId().starts_with("player-stat-tab-") && n->template GetAttribute("aria-selected", "false") != "true"); }), controls.end()); const auto current = context_->GetFocusElement(); const auto i = std::find(controls.begin(), controls.end(), current); if (!controls.empty()) { const int old = i == controls.end() ? -1 : int(i - controls.begin()); const bool backwards = event.GetParameter("shift_key", 0) != 0; const auto next = (old + (backwards ? -1 : 1) + int(controls.size())) % int(controls.size()); controls[size_t(next)]->Focus(true); if (controls[size_t(next)]->GetId().starts_with("report-detail-player-") || controls[size_t(next)]->GetId().starts_with("report-club-") || controls[size_t(next)]->GetId().starts_with("friendly-opponent-") || controls[size_t(next)]->GetId().starts_with("alm-match-") || controls[size_t(next)]->GetId().starts_with("contract-player-") || controls[size_t(next)]->GetId().starts_with("lineup-name-") || controls[size_t(next)]->GetId().starts_with("lineup-detail-")) controls[size_t(next)]->ScrollIntoView( Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest)); event.StopImmediatePropagation(); } return; } } if (type == "change") { if (target->HasAttribute("data-input")) input(target); else change(target); return; } if (type == "input") { input(target); return; } if (type != "click" && type != "dblclick") return; if (suppress_click_) { suppress_click_ = false; event.StopPropagation(); return; } if (type == "dblclick") return; for (auto *n = target; n && n != doc_; n = n->GetParentNode()) if (n->HasAttribute("data-action")) { if (!n->HasAttribute("disabled")) dispatch(n->GetAttribute("data-action", ""), n); return; } } catch (const std::exception &e) { message(e.what()); } void App::dispatch(const std::string &action, Rml::Element *target) { const auto id = target ? target->GetAttribute("data-id", "") : std::string(); if(front_action(action,target))return; if (live_action(action, target)) return; if(national_action(action,target))return; if (career_action(action,target)) return; if (entry_action(action, target)) return; if (opening_action(action, target)) return; if (settings_action(action,target)) return; if (files_action(action,target) || widget_action(action,target)) return; if (lineup_action(action, target) || transfer_action(action, target)) return; if (career_report_action(action, target) || rankings_action(action, target)) return; if (statistics_action(action, target) || dispatch_news(action, target)) return; if (stadium_action(action, target)) return; if (environment_action(action, target) || advertising_action(action, target) || toto_action(action, target)) return; if (amount_action(action, target) || bank_action(action, target) || finance_action(action, target) || contracts_action(action, target)) return; if (almanac_action(action, target) || planning_action(action, target) || indoor_action(action, target)) return; if (report_detail_action(action, target)) { close_report_picker(false); return; } if (report_picker_action(action, target)) return; if (action == "report-sort" || action == "report-reset" || action == "report-round") { Json change; if (action == "report-sort") change = {{"kind", "sort"}, {"view", menu_}, {"key", id}}; else if (action == "report-reset") change = {{"kind", "reset"}}; else { if (menu_ == "other" && snap().state.value("opening_created", false) && snap().state.at("reports").value("competition", 0) == 3) return; change = {{"kind", "round"}, {"step", std::stoi(id)}, {"other", menu_ == "other"}}; } if (apply("reports", change)) { close_report_picker(false); focus_after_render_ = target->GetId(); refresh(); } return; } if (action == "popup-close") { popup_.clear(); confirm_ = {}; text_accept_ = {}; quit_after_save_ = false; dirty_ = true; return; } if (action == "popup-confirm") { auto fn = std::move(confirm_); auto text_fn = std::move(text_accept_); const auto text = field("popup-text"); popup_.clear(); confirm_ = {}; text_accept_ = {}; if (text_fn) text_fn(text); else if (fn) fn(); dirty_ = true; return; } if (action == "exit") { closed_ = true; return; } if (action == "quit-without-save") { closed_ = true; return; } if (action.starts_with("context-")) { context_open_ = false; dirty_ = true; init_desktop(); if (action == "context-quit") { if (!source_->dirty() && desktop_ == saved_desktop_) { confirmation("Spiel beenden?", [this] { closed_ = true; }); return; } confirmation("Deine Änderungen wurden noch nicht gespeichert. Vor dem " "Beenden speichern?", [this] { quit_after_save_ = true; menu_ = "save"; refresh(); }); popup_accept_ = "Speichern"; popup_extra_ = btn("quit-without-save", "Nicht speichern"); return; } if (action == "context-widgets" || action == "context-view-save" || action == "context-view-load" || action == "context-background") { menu_ = action.substr(8); refresh(); return; } if (action == "context-undo") { if (!undo_.empty()) { desktop_ = undo_.back(); undo_.pop_back(); refresh(); } return; } focus_after_render_=context_id_.empty()?"desktop-identity":"object-"+context_id_; auto before = desktop_; bool ok = true; if (action == "context-pin") desktop_["objects"][context_id_]["pinned"] = !desktop_["objects"][context_id_].value("pinned", true); else if (action == "context-pin-all") { bool all = true; for (const auto &[key, o] : desktop_["objects"].items()) { (void)key; if (o.value("visible", false) && !o.value("pinned", true)) all = false; } for (auto &[key, o] : desktop_["objects"].items()) { (void)key; if (o.value("visible", false)) o["pinned"] = !all; } } else if (action == "context-grid") desktop_["grid"] = !desktop_.value("grid", true); else if (action == "context-arrange") ok = arrange_desktop(desktop_); else if (action == "context-remove") desktop_["objects"][context_id_]["visible"] = false; else if (action == "context-size") { const auto split = id.find('x'); ok = split != id.npos && resize_desktop_widget(desktop_, context_id_, std::stoi(id.substr(0, split)), std::stoi(id.substr(split + 1))); } if (ok && desktop_ != before) { desktop_checkpoint(before); refresh(); } else if (!ok) message("Kein freier Platz. Fixierte Widgets zuerst lösen oder andere " "Elemente entfernen."); return; } if (action == "close") { quit_after_save_=false; quit_closes_app_=false; menu_.clear(); reset_settings(); if (scene_ != "desktop") scene_ = "main"; refresh(); return; } if (action == "mods") { collect_mods(); scene_ = "mods"; refresh(); return; } if (action == "settings") { settings_category_ = "display"; reset_settings(); menu_ = "settings"; refresh(); return; } if (action == "load") { if(auto *active=mod(active_mod_);active&&active->type=="Modern"&&(!world_||world_->source()!=fs::absolute(active->file).lexically_normal()))load_world(active->file); if (!source_) { if (auto *m = mod(active_mod_); m && m->type == "Modern") load_world(m->file); else { message("Bitte zuerst eine Modern-Mod aktivieren."); return; } } menu_ = "load"; refresh(); return; } if (action == "new") { if (auto *m = mod(active_mod_); m && m->type == "Classic") { try { const auto contents = content::open(m->file); launch::ModEntry entry{m->name, "Classic", contents->content_id, {}, m->file.parent_path()}; launch::ModLibrary library(front_library_root()); const auto user = library.user_directory(entry); SDL_HideWindow(video_.window); const int code = launch::run_child(executable_, {"--run-mod", editor::utf8(m->file), "--save-directory", editor::utf8(user)}, [] {}); SDL_ShowWindow(video_.window); if (code) message("Classic wurde mit Fehlercode " + std::to_string(code) + " beendet."); refresh(); } catch (const std::exception &e) { SDL_ShowWindow(video_.window); message(e.what()); } return; } if (!gui_.available()) { message("Die GUI-Ressourcen für Modern fehlen. Bitte das vollständige " "Modern-UI-Paket verwenden."); return; } entry_begin(); return; } if (action == "desktop-context") { context_menu({}, 0, 0); return; } if (action.starts_with("open:")) { const auto route = action.substr(5); if(source_&&!demo_career_menu_available(snap().state,route)){message(snap().state.at("career").value("ended",false)?"Diese Partie ist beendet. Bitte ein neues Spiel beginnen oder einen Spielstand laden.":"Dieses Vereinsmenü ist für die Nationalmannschaft nicht verfügbar.");return;} if (route == "kickoff") return; if (route == "newplayer") { scene_="managerhub";menu_.clear();career_resign_open_=career_game_end_=false;focus_after_render_="career-resign";refresh(); return; } static const std::set available = { "tables", "strengths", "camp", "settings", "save", "load", "training", "tactics", "calendar", "league", "other", "postpone", "friendlies", "indoor", "almanac", "bank", "balance", "contracts", "environment", "advertising", "toto", "lineup", "transfer", "achievements", "scorers", "fairplay", "highscore", "stadium", "statistics", "news"}; if (!available.contains(route)) { message("Diese native Ansicht wird im nächsten Menüpaket übertragen."); return; } if (route == "bank") { bank_view_ = "entrance"; bank_hint_.clear(); } menu_ = route; report_picker_.clear(); report_detail_kind_.clear(); planning_dialog_.clear(); if (route == "settings") { settings_category_ = "display"; reset_settings(); } refresh(); return; } if (action == "training-auto" || action == "training-auto-all" || action == "training-ball") { Json change; if (action == "training-auto-all") change = {{"kind", "auto-all"}}; else if (action == "training-auto") change = {{"kind", "auto"}, {"index", std::stoi(id)}}; else { const auto group = target->GetAttribute("data-group", ""); change = {{"kind", "ball"}, {"group", group}, {"index", std::stoi(id)}, {"value", target->GetAttribute("data-value", 1)}}; if (group == "player") change["player"] = target->GetAttribute("data-player", ""); } if (apply("training", change)) { focus_after_render_ = target->GetId(); refresh(); } return; } if (action == "calendar-select") { if (apply("calendar", {{"kind", "select"}, {"value", std::stoi(id)}})) { focus_after_render_ = target->GetId(); refresh(); } return; } if (action == "tactic-effort-set") { int value = std::clamp(std::stoi(id), 0, 11); if (snap().state.at("tactics").at("values")[7] == value) value = std::max(0, value - 1); if (apply("tactics", {{"kind", "effort"}, {"value", value}})) { last_tactic_ = 7; focus_after_render_ = "tactic-effort-" + id; refresh(); } return; } if (action == "tactic-cycle") { const int i = std::stoi(id); if (i < 0 || i > 6) return; if (apply("tactics", {{"kind", "cycle"}, {"index", i}})) { last_tactic_ = i; focus_after_render_ = "tactic-cycle-" + id; refresh(); } return; } if (action == "ticket") { if (ticket_) { ticket_->open(target, device_ratio_); dirty_ = true; } return; } if (action == "camp-region") { selections_["camp-region"] = id; refresh(); return; } if (action == "camp-select") { apply("camp.select", id); refresh(); return; } if (action == "camp-remember") { if (apply("camp.remember", {})) message("Trainingslager für die Demo vorgemerkt."); return; } if (action == "squad-tactics") { menu_ = "tactics"; refresh(); return; } } void App::change(Rml::Element *target) { if (entry_change(target)) return; if(front_change(target)||files_change(target) || widget_change(target) || settings_change(target))return; const auto kind = target->GetAttribute("data-change", ""); auto *control = dynamic_cast(target); if (!control) return; const auto value = control->GetValue(); if (auto *select = dynamic_cast(target); select && select->IsSelectBoxVisible()) { pending_selects_.insert(target->GetId()); return; } if (kind == "calendar-country" || kind == "calendar-league") { std::string league = value; if (kind == "calendar-country") { const auto current = snap().state.at("calendar").at("league").get(); const auto *competition = world_->find("competitions", current); if (competition && competition->value("nation", "") == value) return; const auto leagues = world_->leagues(value); if (leagues.empty()) return; league = leagues.front()->value("id", ""); } if (snap().state.at("calendar").at("league") == league) return; if (apply("calendar", {{"kind", "league"}, {"value", league}})) { focus_after_render_ = target->GetId(); refresh(); } return; } if (kind == "training-focus") { const auto player = target->GetAttribute("data-player", ""); const int focus = std::stoi(value); if (snap().state.at("training").at("players").at(player).at("focus") == focus) return; if (apply("training", {{"kind", "focus"}, {"player", player}, {"value", focus}})) { focus_after_render_ = target->GetId(); refresh(); } return; } } void App::input(Rml::Element *target) { if (entry_input(target)) return; if(front_change(target)||files_change(target) || widget_change(target) || settings_change(target))return; const auto kind = target->GetAttribute("data-input", ""); auto *control = dynamic_cast(target); if (!control) return; const auto value = control->GetValue(); if (kind == "tactic-effort") { const int value = std::stoi(control->GetValue()); if (apply("tactics", {{"kind", "effort"}, {"value", value}})) { last_tactic_ = 7; element("tactic-effort-value") ->SetInnerRML(std::string(size_t(value), '!')); element("tactic-hint") ->SetInnerRML("Einsatz: " + std::to_string(value) + " / 11"); dirty_ = true; } return; } if (kind == "camp-focus") { const auto i = std::stoi(target->GetAttribute("data-id", "0")); if (i < 0 || i > 2) return; auto focus = snap().state.at("camp").at("focus"); focus[size_t(i)] = std::stoi(value); if (apply("camp.focus", focus)) { element("camp-value-" + std::to_string(i))->SetInnerRML(value + " / 15"); const int rest = 15 - (focus[0].get() + focus[1].get() + focus[2].get()) / 3; element("camp-value-3")->SetInnerRML(std::to_string(rest) + " / 15"); if (auto *c = dynamic_cast(element("camp-focus-3"))) c->SetValue(std::to_string(rest)); dirty_ = true; } } } void App::script() { std::ifstream stream(config_.commands); if (!stream) throw editor::Error("UI-Skript kann nicht gelesen werden."); const auto locate = [&](const Json &command) { if (command.contains("selector")) { context_->Update(); auto *node = doc_->QuerySelector(command.at("selector").get()); if (!node || !node->IsVisible(true)) throw editor::Error("UI-Skript: Element nicht sichtbar."); const auto point = script_click_point(*context_, *node); if (!point) throw editor::Error("UI-Skript: Elementmitte ist nicht sichtbar anklickbar oder die Transformation ist singulär."); return *point; } return Rml::Vector2i{command.at("x").get(), command.at("y").get()}; }; std::string line; size_t index = 0; while (std::getline(stream, line)) { ++index; if (line.empty()) continue; try { const auto command = Json::parse(line); const auto type = command.value("type", std::string()); if (type == "engine-wait") { const auto until = SDL_GetTicks64() + command.value("timeout_ms", uint64_t(300000)); while ((engine_busy_ || engine_creation_busy_ || engine_files_busy_ || engine_live_busy_) && SDL_GetTicks64() < until) { pump_events(); live_tick(); frame(); SDL_Delay(10); } if (engine_busy_ || engine_creation_busy_ || engine_files_busy_) throw editor::Error("Engine-Vorgang hat das QA-Zeitlimit überschritten."); } else if(type=="window-resize-probe"){ // QA-only synthetic event: no OS window or mouse coordinate is changed. int w,h,pw,ph;SDL_GetWindowSize(video_.window,&w,&h);video_.drawable_size(pw,ph); const auto before=context_->GetDimensions();SDL_Event probe{};probe.type=SDL_WINDOWEVENT;probe.window.windowID=SDL_GetWindowID(video_.window);probe.window.event=SDL_WINDOWEVENT_SIZE_CHANGED;probe.window.data1=std::max(1,w/2);probe.window.data2=std::max(1,h/2);handle_event(probe); const auto after=context_->GetDimensions();const bool ok=after.x==pw&&after.y==ph; editor::write_atomic(editor::path(command.at("path").get()),Json{{"qa_only",true},{"logical",{w,h}},{"drawable",{pw,ph}},{"context_before",{before.x,before.y}},{"synthetic_event",{probe.window.data1,probe.window.data2}},{"context_after",{after.x,after.y}},{"passed",ok}}.dump(2)); if(!ok)throw editor::Error("QA resize probe: context dimensions were overwritten by logical event data."); }else if(type=="front-wait"){ const auto end=SDL_GetTicks64()+command.value("timeout_ms",uint64_t(300000)); while(front_busy_&&SDL_GetTicks64()Update(); auto *node = doc_->QuerySelector(command.at("selector").get()); return !node || !node->IsVisible(true) || node->HasAttribute("disabled"); }()) { // An optional step whose element is absent, hidden or disabled is skipped. } else if (type == "click" || type == "right") { const auto point = locate(command); const int button = type == "right" ? 1 : 0; context_->ProcessMouseMove(point.x, point.y, 0); context_->ProcessMouseButtonDown(button, 0); context_->ProcessMouseButtonUp(button, 0); } else if(type=="pointer-down"||type=="pointer-move"||type=="pointer-up"){ const auto point=locate(command);context_->ProcessMouseMove(point.x,point.y,0); const int button=command.value("button",0); if(type=="pointer-down")context_->ProcessMouseButtonDown(button,0); if(type=="pointer-up")context_->ProcessMouseButtonUp(button,0); } else if (type == "drag") { const auto point = locate(command), end = Rml::Vector2i{command.at("to_x"), command.at("to_y")}; context_->ProcessMouseMove(point.x, point.y, 0); context_->ProcessMouseButtonDown(0, 0); for (int i = 1; i <= 12; ++i) context_->ProcessMouseMove(point.x + (end.x - point.x) * i / 12, point.y + (end.y - point.y) * i / 12, 0); context_->ProcessMouseButtonUp(0, 0); } else if (type == "wheel") { const auto point = locate(command); const float delta = command.at("delta").get(); if (!std::isfinite(delta) || std::abs(delta) > 1000) throw editor::Error("UI-Skript: ungültiger Mausradweg."); context_->ProcessMouseMove(point.x, point.y, 0); context_->ProcessMouseWheel(delta, 0); } else if (type == "text") context_->ProcessTextInput(command.value("value", std::string())); else if (type == "key") { const std::map keys = { {"ESC", Rml::Input::KI_ESCAPE}, {"ENTER", Rml::Input::KI_RETURN}, {"NUMENTER", Rml::Input::KI_NUMPADENTER}, {"TAB", Rml::Input::KI_TAB}, {"SPACE", Rml::Input::KI_SPACE}, {"HOME", Rml::Input::KI_HOME}, {"END", Rml::Input::KI_END}, {"LEFT", Rml::Input::KI_LEFT}, {"RIGHT", Rml::Input::KI_RIGHT}, {"UP", Rml::Input::KI_UP}, {"DOWN", Rml::Input::KI_DOWN}, {"S", Rml::Input::KI_S}, {"O", Rml::Input::KI_O}, {"A", Rml::Input::KI_A}, {"Z", Rml::Input::KI_Z}, {"V", Rml::Input::KI_V}, {"P", Rml::Input::KI_P}, {"BACKSPACE", Rml::Input::KI_BACK}, {"DELETE", Rml::Input::KI_DELETE}, {"F4", Rml::Input::KI_F4}, {"F10", Rml::Input::KI_F10}, {"CONTEXT", Rml::Input::KI_APPS}, {"0", Rml::Input::KI_0}, {"1", Rml::Input::KI_1}, {"2", Rml::Input::KI_2}, {"3", Rml::Input::KI_3}, {"4", Rml::Input::KI_4}, {"5", Rml::Input::KI_5}, {"6", Rml::Input::KI_6}, {"7", Rml::Input::KI_7}, {"8", Rml::Input::KI_8}, {"9", Rml::Input::KI_9}}; const auto name = command.at("value").get(); if (!keys.contains(name)) throw editor::Error("Unbekannte UI-Skripttaste."); const int mods = (command.value("ctrl", false) ? Rml::Input::KM_CTRL : 0) | (command.value("shift", false) ? Rml::Input::KM_SHIFT : 0); context_->ProcessKeyDown(keys.at(name), mods); context_->ProcessKeyUp(keys.at(name), mods); } else if (type == "wait") { const int milliseconds = command.at("milliseconds").get(); if (milliseconds < 0 || milliseconds > 30000) throw editor::Error("UI-Wartezeit muss zwischen 0 und 30000 ms liegen."); const auto until = SDL_GetTicks64() + uint64_t(milliseconds); while (!closed_ && SDL_GetTicks64() < until) { pump_events(); front_poll(); opening_tick(); live_tick(); frame(); SDL_Delay(5); } } else if (type == "demo-start") { // QA: begin a demo career with `managers` human managers (1-4), each at a club of // its own, so the match day can be looked at with several matches at once if (engine_mode_ || !source_) throw editor::Error("demo-start braucht die UI-Demo."); const int wanted = std::clamp(command.value("managers", 1), 1, 4); Json managers = Json::array(); const int step = std::max(1, command.value("step", 2)); int skip = 0; for (const auto *club : world_->clubs()) { if (int(managers.size()) >= wanted) break; const auto id = club->value("id", std::string()); if (!world_->manageable(id) || skip++ % step) continue; managers.push_back({{"name", "Manager " + std::to_string(managers.size() + 1)}, {"club", id}, {"portrait", int(managers.size())}, {"portrait_style", "photo"}, {"strength_choice", 1}}); } const auto result = source_->apply({source_->snapshot().revision, "start", {{"setup", {{"duration", 5}, {"difficulty", 3}, {"players", managers.size()}, {"mode", 3}, {"league", true}, {"world", true}}}, {"managers", managers}}}); if (!result.ok) throw editor::Error("demo-start: " + result.error); scene_ = "desktop"; menu_.clear(); refresh(); } else if (type == "screenshot") screenshot(editor::path(command.at("path").get())); else if (type == "checkpoint") { if (engine_mode_) throw editor::Error("Der UI-Demo-Checkpoint speichert keine Enginekarriere. Bitte die OBS-Speicheraktion verwenden."); init_desktop(); source_->save(editor::path(command.at("path").get()), desktop_); } else if (type == "snapshot") editor::write_atomic( editor::path(command.at("path").get()), Json{{"scene", scene_}, {"menu", menu_}, {"live_view", live_snapshot()}, {"engine_view", engine_snapshot()}, {"report_picker", report_picker_}, {"planning_dialog", planning_dialog_}, {"bank_view", bank_view_}, {"bank_property", bank_property_}, {"bank_stock", bank_stock_}, {"balance_future", balance_future_}, {"advertising_view", {{"selected", advertising_selected_}, {"offer", advertising_offer_}}}, {"toto_view", {{"column", toto_column_}, {"dialog", toto_dialog_}}}, {"lineup_view", {{"pitch", lineup_pitch_}, {"data", lineup_data_}, {"page", lineup_page_}, {"side", lineup_side_}, {"selected", lineup_selected_}, {"drag", lineup_drag_}}}, {"transfer_view", transfer_view_}, {"transfer_dialog", transfer_dialog_}, {"transfer_draft", transfer_draft_}, {"career_view", career_view_}, {"rankings_view", rankings_view_}, {"stadium_view", stadium_view_}, {"select_menu", select_menu_ ? select_menu_->snapshot() : Json()}, {"career_context",source_?demo_career_context(snap().state,*world_):Json()}, {"national_view",{{"dialog",national_dialog_},{"menu",national_menu_},{"portrait",national_portrait_},{"revision",national_revision_}}}, {"manager_hub",{{"resign_open",career_resign_open_},{"game_end",career_game_end_},{"owner",field("career-resign-owner")}}}, {"entry_view", entry_snapshot()}, {"front_view",front_snapshot_view()}, {"files_view",{{"mode",files_mode_},{"selected",files_selected_},{"name",files_name_},{"feedback",files_feedback_}}}, {"widget_view",{{"query",widget_query_},{"notice",widget_notice_}}}, {"ui_preferences",ui_preferences_}, {"settings_draft",settings_draft_}, {"settings_category",settings_category_}, {"settings_query",settings_query_}, {"settings_notice",settings_notice_}, {"statistics_view", statistics_view_}, {"statistics_trophy", statistics_trophy_}, {"statistics_scrolls", statistics_scrolls_}, {"news_view", news_view_}, {"news_dialog", news_dialog_}, {"news_undo", news_undo_}, {"news_scroll", element("news-feed") ? element("news-feed")->GetScrollTop() : news_scroll_}, {"focus", context_->GetFocusElement() ? context_->GetFocusElement()->GetId() : ""}, {"contracts_view", {{"dialog", contracts_dialog_}, {"player", contracts_player_}, {"tab", contracts_tab_}, {"draft", contract_draft_}}}, {"amount_editor", amount_ ? Json{{"title", amount_->request.title}, {"raw", amount_->raw}, {"review", amount_->review}, {"error", amount_error()}} : Json()}, {"display_metrics",[&](){int w,h,pw,ph;SDL_GetWindowSize(video_.window,&w,&h);video_.drawable_size(pw,ph);const auto d=context_->GetDimensions();return Json{{"logical",{w,h}},{"drawable",{pw,ph}},{"context",{d.x,d.y}}};}()}, {"source_revision", source_ ? Json(snap().revision) : Json()}, {"game_dirty", engine_mode_ ? engine_dirty_ : source_ && source_->dirty()}, {"desktop_dirty", desktop_ != saved_desktop_}, {"almanac_view", {{"year", almanac_year_}, {"date", almanac_day_}, {"id", almanac_match_}, {"tab", almanac_tab_}}}, {"report_detail", {{"kind", report_detail_kind_}, {"club", report_detail_club_}, {"player", report_detail_player_}}}, {"desktop", desktop_}, {"demo", source_ ? snap().state : Json()}} .dump(2)); else if(type=="metrics"){ context_->Update();Json rows=Json::array(); for(const auto &selector:command.at("selectors")){Rml::ElementList nodes;doc_->QuerySelectorAll(nodes,selector.get());for(auto *node:nodes){auto at=node->GetAbsoluteOffset(Rml::BoxArea::Border),size=node->GetBox().GetSize(Rml::BoxArea::Border);rows.push_back({{"selector",selector},{"id",node->GetId()},{"x",at.x},{"y",at.y},{"width",size.x},{"height",size.y},{"scroll_top",node->GetScrollTop()},{"scroll_height",node->GetScrollHeight()},{"client_height",node->GetClientHeight()},{"font_size",node->GetComputedValues().font_size()},{"rml",node->GetInnerRML()},{"visible",node->IsVisible(true)}});}} editor::write_atomic(editor::path(command.at("path").get()),rows.dump(2)); } else if (type == "text-audit") { // Readability check: every visible text with the size it is drawn at (in screen // pixels) and whether its box is too narrow for it. context_->Update(); Json rows = Json::array(); const auto dimensions = context_->GetDimensions(); std::function walk = [&](Rml::Element *node) { if (!node->IsVisible(true)) return; std::string text; for (int i = 0; i < node->GetNumChildren(); ++i) if (auto *part = dynamic_cast(node->GetChild(i))) text += part->GetText(); const auto first = text.find_first_not_of(" \t\r\n"), last = text.find_last_not_of(" \t\r\n"); if (first != std::string::npos) { const auto box = node->GetAbsoluteOffset(Rml::BoxArea::Border); const auto size = node->GetBox().GetSize(Rml::BoxArea::Border); // only what is on the screen counts if (box.x + size.x > 0 && box.y + size.y > 0 && box.x < float(dimensions.x) && box.y < float(dimensions.y)) rows.push_back({{"px", node->GetComputedValues().font_size()}, {"text", text.substr(first, std::min(last - first + 1, 70))}, {"id", node->GetId()}, {"class", node->GetClassNames()}, {"tag", node->GetTagName()}, {"overflow", node->GetScrollWidth() > node->GetClientWidth() + 1.5f}}); } for (int i = 0; i < node->GetNumChildren(); ++i) walk(node->GetChild(i)); }; // with a menu window open only the window and its dialogs count, not the desk beneath Rml::ElementList windows; doc_->QuerySelectorAll(windows, ".window"); if (windows.empty()) walk(doc_); else { for (auto *window : windows) walk(window); if (auto *popup = element("popup")) walk(popup); } editor::write_atomic(editor::path(command.at("path").get()), Json{{"scene", scene_}, {"menu", menu_}, {"scale", scale_}, {"width", dimensions.x}, {"height", dimensions.y}, {"texts", rows}}.dump(1)); } else if (type == "dom") editor::write_atomic( editor::path(command.at("path").get()), doc_->GetInnerRML()); else if (type == "assert") { if (command.contains("scene") && scene_ != command.at("scene").get()) throw editor::Error("UI-Skript: falsche Szene."); if (command.contains("menu") && menu_ != command.at("menu").get()) throw editor::Error("UI-Skript: falsches Menü."); if (command.contains("selector")) { auto *node = doc_->QuerySelector(command.at("selector").get()); const bool present = node && node->IsVisible(true); if (present != command.value("visible", true)) throw editor::Error("UI-Skript: falsche Sichtbarkeit."); if (command.contains("disabled") && (!node || node->HasAttribute("disabled") != command.at("disabled").get())) throw editor::Error("UI-Skript: falscher Sperrstatus."); } if (command.contains("ticket") && snap().state.at("ticket") != command.at("ticket")) throw editor::Error("UI-Skript: falscher Eintrittspreis."); } else throw editor::Error("Unbekannter UI-Skriptbefehl: " + type); frame(); } catch (const std::exception &e) { throw editor::Error("UI-Skript Zeile " + std::to_string(index) + ": " + e.what()); } } }