// SPDX-License-Identifier: GPL-3.0-only void App::cancel_lineup_drag(bool refresh_view) { lineup_drag_ = Json(); lineup_drag_world_.reset(); lineup_drag_revision_ = 0; if (auto *ghost = element("lineup-ghost")) ghost->SetProperty("display", "none"); if (refresh_view) refresh(); } bool App::lineup_action(const std::string &action, Rml::Element *target) { if (!action.starts_with("lineup-") || menu_ != "lineup") return false; if (!source_ || !target || !popup_.empty() || amount_ || !report_detail_kind_.empty()) return true; if (action == "lineup-surface") { const auto surface = target ? target->GetAttribute("data-id", "") : ""; if (apply("career", {{"kind", "surface"}, {"value", surface}})) { lineup_selected_.clear(); focus_after_render_ = "lineup-surface-" + surface; refresh(); } return true; } if (lineup_ignore_click_ && action == "lineup-player") { lineup_ignore_click_ = false; return true; } const auto id = target->GetAttribute("data-id", ""); const auto s = snap(); if (!s.world || !valid_demo_lineup(s.state, *s.world, lineup_managed(s))) return true; const auto &l = demo_lineup_visible(s.state); const int field_count = demo_career_field_count(s.state); const auto parse_index = [&](int maximum) -> int { return id.size() == 1 && id[0] >= '0' && id[0] <= '0' + maximum ? id[0] - '0' : -1; }; const auto commit = [&](const Json &payload, const std::string &focus) { const auto result = source_->apply({s.revision, "lineup", payload}); if (!result.ok) { message("Die Aufstellung konnte nicht übernommen werden. Bitte die " "Ansicht erneut öffnen."); return false; } focus_after_render_ = focus; refresh(); return true; }; const auto player_focus = [&](const std::string &p) { const int slot = lineup_slot(l, p); return std::string(lineup_pitch_ && slot < field_count ? "lineup-player-" : "lineup-name-") + p; }; if (action == "lineup-player") { if (lineup_slot(l, id) < 0) return true; if (lineup_selected_ == id) lineup_selected_.clear(); else if (!lineup_selected_.empty()) { const auto selected = lineup_selected_; lineup_selected_.clear(); const int destination = lineup_slot(l, selected); const auto focus = std::string(lineup_pitch_ && destination < field_count ? "lineup-player-" : "lineup-name-") + id; commit({{"kind", "swap"}, {"a", selected}, {"b", id}}, focus); return true; } else lineup_selected_ = id; focus_after_render_ = player_focus(id); refresh(); return true; } if (action == "lineup-data") { lineup_data_ = 1 - lineup_data_; focus_after_render_ = "lineup-data"; refresh(); return true; } if (action == "lineup-view") { cancel_lineup_drag(false); lineup_pitch_ = !lineup_pitch_; lineup_reset_scroll_ = true; focus_after_render_ = "lineup-view"; refresh(); return true; } if (action == "lineup-side") { if (id == "formations" || id == "orders" || id == "takers") { lineup_side_ = id; focus_after_render_ = "lineup-side-" + id; refresh(); } return true; } if (action == "lineup-page") { const int n = parse_index(1); if (n >= 0) { lineup_page_ = n; focus_after_render_ = n ? "lineup-page-prev" : "lineup-page-next"; refresh(); } return true; } if (action == "lineup-formation") { const int n = parse_index(7); if (n >= 0) { lineup_selected_.clear(); commit({{"kind", "formation"}, {"value", n}}, "lineup-formation-" + id); } return true; } if (action == "lineup-save") { const int n = parse_index(3); if (n >= 0) commit({{"kind", "custom-save"}, {"index", n}}, "lineup-save-" + id); return true; } if (action == "lineup-weight") { const int n = parse_index(2); if (n >= 0) commit({{"kind", "weight"}, {"index", n}}, "lineup-weight-" + id); return true; } if (action == "lineup-auto") { commit({{"kind", "takers-auto"}}, "lineup-auto"); return true; } return true; } bool App::lineup_event(Rml::Event &event) { if (menu_ != "lineup" || !source_ || !popup_.empty() || amount_ || !report_detail_kind_.empty()) return false; const auto type = event.GetType(); auto *target = event.GetTargetElement(); const auto ancestor = [&](const std::string &attribute) -> Rml::Element * { for (auto *n = target; n && n != doc_; n = n->GetParentNode()) if (n->HasAttribute(attribute)) return n; return nullptr; }; auto *player_node = ancestor("data-squad-player"); auto *pitch = element("lineup-pitch"); const auto s = snap(); if (!s.world || !valid_demo_lineup(s.state, *s.world, lineup_managed(s))) { cancel_lineup_drag(false); return false; } const auto &l = demo_lineup_visible(s.state); const int field_count = demo_career_field_count(s.state); const auto stop = [&]() { event.StopImmediatePropagation(); }; const auto commit = [&](const Json &payload, uint64_t revision) { const auto result = source_->apply({revision, "lineup", payload}); if (!result.ok) message("Die Aufstellung hat sich geändert. Bitte den Spieler erneut " "auswählen."); return result.ok; }; const auto point = [&](float x, float y) -> Json { if (!pitch) return Json(); const auto origin = pitch->GetAbsoluteOffset(Rml::BoxArea::Border), size = pitch->GetBox().GetSize(Rml::BoxArea::Border); if (size.x <= 0 || size.y <= 0) return Json(); return Json::array( {std::clamp( int(std::round(((x - origin.x) / size.x * 100 - 8) / 84 * 18)), 0, 18), std::clamp( int(std::round(((y - origin.y) / size.y * 100 - 10) / 80 * 8)), 0, 8)}); }; const auto in_pitch = [&](float x, float y) { if (!pitch) return false; const auto origin = pitch->GetAbsoluteOffset(Rml::BoxArea::Border), size = pitch->GetBox().GetSize(Rml::BoxArea::Border); return x >= origin.x && y >= origin.y && x <= origin.x + size.x && y <= origin.y + size.y; }; const float mx = event.GetParameter("mouse_x", 0), my = event.GetParameter("mouse_y", 0); if (type == "keydown") { lineup_ignore_click_ = false; const auto key = Rml::Input::KeyIdentifier(event.GetParameter("key_identifier", 0)); if (key == Rml::Input::KI_ESCAPE) { for (auto *n = target; n && n != doc_; n = n->GetParentNode()) if (auto *select = dynamic_cast(n); select && select->IsSelectBoxVisible()) return false; if (!lineup_drag_.is_null()) { cancel_lineup_drag(true); stop(); return true; } if (!lineup_selected_.empty()) { lineup_selected_.clear(); refresh(); stop(); return true; } return false; } if (pitch && player_node && player_node->IsClassSet("lineup-player") && (key == Rml::Input::KI_LEFT || key == Rml::Input::KI_RIGHT || key == Rml::Input::KI_UP || key == Rml::Input::KI_DOWN)) { const auto id = player_node->GetAttribute("data-squad-player", ""); const int slot = lineup_slot(l, id); if (slot >= 0 && slot < field_count) { auto p = l.at("points")[size_t(slot)]; p[0] = std::clamp(p[0].get() + (key == Rml::Input::KI_RIGHT ? 1 : key == Rml::Input::KI_LEFT ? -1 : 0), 0, 18); p[1] = std::clamp(p[1].get() + (key == Rml::Input::KI_DOWN ? 1 : key == Rml::Input::KI_UP ? -1 : 0), 0, 8); lineup_selected_ = id; commit({{"kind", "move"}, {"player", id}, {"point", p}}, s.revision); focus_after_render_ = "lineup-player-" + id; refresh(); stop(); return true; } } } if ((type == "change" || type == "input") && target) { if (auto *control = dynamic_cast(target)) { if (target->HasAttribute("data-lineup-taker")) { const auto index = target->GetAttribute("data-lineup-taker", ""); if (index.size() != 1 || index[0] < '0' || index[0] > '2') return true; const auto value = control->GetValue(); const int i = index[0] - '0'; if (l.at("takers")[size_t(i)] != value) { commit({{"kind", "taker"}, {"index", i}, {"player", value}}, s.revision); focus_after_render_ = target->GetId(); refresh(); } stop(); return true; } if (target->HasAttribute("data-lineup-order")) { const auto index = target->GetAttribute("data-lineup-order", ""); const auto id = target->GetAttribute("data-player", ""); const auto text = control->GetValue(); // RmlUI ranges format whole steps as "%f" (for example "22.000000"). // Accept that complete representation without rounding fractional // input. const auto dot = text.find('.'); const auto whole = text.substr(0, dot); const bool whole_fraction = dot == std::string::npos || (dot + 1 < text.size() && std::all_of(text.begin() + dot + 1, text.end(), [](char c) { return c == '0'; })); if (index.size() != 1 || index[0] < '0' || index[0] > '3' || whole.empty() || whole.size() > 2 || !whole_fraction || std::any_of(whole.begin(), whole.end(), [](char c) { return c < '0' || c > '9'; })) return true; const int value = std::stoi(whole), i = index[0] - '0'; if (value < 2 || value > 40 || lineup_slot(l, id) < 0 || lineup_slot(l, id) >= field_count) return true; if (l.at("orders").at(id)[size_t(i)] != value && commit({{"kind", "order"}, {"player", id}, {"index", i}, {"value", value}}, s.revision)) { if (auto *output = element("lineup-order-value-" + index)) output->SetInnerRML(std::to_string(value)); if (auto *zone = element("lineup-zone")) { const auto now = snap(); zone->SetInnerRML( picture(renderer_.diagram( "lineup-zone", lineup_zone(demo_lineup_visible(now.state), id), lineup_pitch_raster_px), lineup_art_width_dp, lineup_art_height_dp)); } dirty_ = true; } stop(); return true; } } } if (type == "mousedown" && event.GetParameter("button", 0) == 0) { lineup_ignore_click_ = false; if (pitch && player_node) { const auto id = player_node->GetAttribute("data-squad-player", ""); const int slot = lineup_slot(l, id); if (slot >= 0) { lineup_drag_ = {{"player", id}, {"club", lineup_managed(s)}, {"source_slot", slot}, {"start_x", mx}, {"start_y", my}, {"ghost", point(mx, my)}, {"moved", false}}; lineup_drag_revision_ = s.revision; lineup_drag_world_ = s.world; } } return false; } if (type == "mousemove" && lineup_drag_.is_object()) { if (!lineup_drag_.value("moved", false) && std::hypot(mx - lineup_drag_.at("start_x").get(), my - lineup_drag_.at("start_y").get()) < 5) return false; lineup_drag_["moved"] = true; const auto p = point(mx, my); lineup_drag_["ghost"] = p; if (p.is_array()) if (auto *ghost = element("lineup-ghost")) { ghost->SetProperty("display", "block"); ghost->SetProperty("left", scalar((8 + p[0].get() / 18.0 * 84) * lineup_art_width_dp / 100 - lineup_player_anchor_x_dp) + "dp"); ghost->SetProperty("top", scalar((10 + p[1].get() / 8.0 * 80) * lineup_art_height_dp / 100 - lineup_player_anchor_y_dp) + "dp"); dirty_ = true; } stop(); return true; } if (type == "mouseup" && lineup_drag_.is_object()) { const auto drag = lineup_drag_; const auto revision = lineup_drag_revision_; const auto world = lineup_drag_world_; const bool moved = drag.value("moved", false); cancel_lineup_drag(false); if (!moved) return false; lineup_ignore_click_ = true; const auto id = drag.at("player").get(); if (s.world != world || s.revision != revision || lineup_managed(s) != drag.at("club").get() || lineup_slot(l, id) != drag.at("source_slot").get()) { message("Die Aufstellung hat sich geändert. Bitte den Spieler erneut " "auswählen."); stop(); return true; } // Resolve the physical drop target independently of the active mouse // element: Rml may continue dispatching mouseup to the drag origin. std::string drop; for (auto *n = context_->GetElementAtPoint({mx, my}); n && n != doc_; n = n->GetParentNode()) { if (n->HasAttribute("data-squad-player")) { drop = n->GetAttribute("data-squad-player", ""); break; } } const int drop_slot = lineup_slot(l, drop); if (drop_slot >= 0 && drop != id) { commit({{"kind", "swap"}, {"a", id}, {"b", drop}}, revision); lineup_selected_.clear(); focus_after_render_ = std::string(drop_slot < field_count ? "lineup-player-" : "lineup-name-") + id; } else if (drop_slot < 0 && in_pitch(mx, my)) { const int slot = lineup_slot(l, id); const auto p = point(mx, my); if (slot < field_count) { lineup_selected_ = id; commit({{"kind", "move"}, {"player", id}, {"point", p}}, revision); } else { const auto &points = l.at("points"); const auto hit = std::find(points.begin(), points.end(), p); const int n = int(hit - points.begin()); if (n < int(std::min(size_t(field_count), l.at("order").size()))) { commit({{"kind", "swap"}, {"a", id}, {"b", l.at("order")[size_t(n)]}}, revision); lineup_selected_.clear(); } else lineup_selected_ = id; } focus_after_render_ = "lineup-player-" + id; } refresh(); stop(); return true; } if (type == "click" && lineup_ignore_click_) { lineup_ignore_click_ = false; stop(); return true; } if (type == "click" && pitch && !player_node && !lineup_selected_.empty()) { bool inside = false; for (auto *n = target; n && n != doc_; n = n->GetParentNode()) if (n == pitch) inside = true; if (inside) { const int slot = lineup_slot(l, lineup_selected_); if (slot >= 0 && slot < field_count) { const auto id = lineup_selected_; commit({{"kind", "move"}, {"player", id}, {"point", point(mx, my)}}, s.revision); focus_after_render_ = "lineup-player-" + id; refresh(); } else message("Einen Ersatzspieler auf einen Spieler der Startelf ziehen " "oder beide nacheinander anklicken."); stop(); return true; } } return false; }