// SPDX-License-Identifier: GPL-3.0-only // Tactics as a pencil sketch on the page: the pitch with the own eleven, the runs of the // forwards, the passes and the goal kick drawn as arrows whose shape follows the settings. // Wide players and arrow heads are dragged, the two boxes and the exclamation marks are // clicked; the list at the right names every setting and steps it on a click. The sketch is // 900 x 610 units; the lines are one picture, texts and click areas lie over it. namespace { constexpr double tactics_unit = 2.32; // dp per unit of the sketch struct TacticsArrow { std::string svg; double mid_x, mid_y; }; // A pencil arrow from a to b, bent sideways by `bend` units, with an open head. TacticsArrow tactics_arrow(double ax, double ay, double bx, double by, const std::string &colour, double bend = 0, double width = 2.6, bool dashed = false) { const double dx = bx - ax, dy = by - ay, length = std::max(1.0, std::hypot(dx, dy)); bend = std::clamp(bend, -length * .4, length * .4); // a short arrow cannot arch far const double cx = (ax + bx) / 2 + dy / length * bend, cy = (ay + by) / 2 - dx / length * bend, angle = std::atan2(by - cy, bx - cx); const auto head = [&](double turn) { return "M" + scalar(bx) + " " + scalar(by) + "L" + scalar(bx - std::cos(angle + turn) * 14) + " " + scalar(by - std::sin(angle + turn) * 14); }; return {"", ax * .25 + cx * .5 + bx * .25, ay * .25 + cy * .5 + by * .25}; } double tactics_mix(double a, double b, double t) { return a + (b - a) * std::clamp(t, 0.0, 1.0); } // A point between three anchors: `level` runs from 0 (the first) over 1 to 2 (the last). std::array tactics_between(const std::array, 3> &p, double level) { const size_t from = level <= 1 ? 0 : 1; const double t = level <= 1 ? level : level - 1; return {tactics_mix(p[from][0], p[from + 1][0], t), tactics_mix(p[from][1], p[from + 1][1], t)}; } } // namespace std::string App::render_tactics() { const auto t = engine_mode_ ? engine_tactics_draft_ : snap().state.at("tactics"); const std::array limits = engine_mode_ ? t.at("limits").get>() : std::array{3, 3, 3, 3, 1, 1, 1, 11}; const std::array minima = engine_mode_ ? t.at("minima").get>() : std::array{0, 0, 0, 0, 0, 0, 0, 0}; std::array values{}; for (size_t i = 0; i < 8; ++i) values[i] = t["values"][i].get(); // every setting as a level from 0 to 2, whatever its number of steps const auto level = [&](size_t i) { return limits[i] > minima[i] ? double(values[i] - minima[i]) / (limits[i] - minima[i]) * 2 : 0.0; }; const auto text_of = [&](size_t i) { return tactic_value_text(i, values[i], limits[i]); }; static const std::array names{"Grundausrichtung", "Flügelspiel", "Steilpässe", "Abschlag", "Flach / Hoch", "Abseitsfalle", "Pressing", "Einsatz"}; const bool light = theme_ != "midnight"; const std::string ink = light ? "#2f3542" : "#e6e2d8", red = light ? "#a8452f" : "#e58a70", blue = light ? "#2b5f8f" : "#8fc0ea", grey = light ? "#5a6170" : "#aab0bb", faint = light ? "#b9b2a4" : "#5b606b", paper = light ? "#fffdf6" : "#2a2d34"; // --- where everything stands --- const double shift = (level(0) - 1) * 84, wings = level(1), wide_front = wings <= 1 ? tactics_mix(.72, 1, wings) : tactics_mix(1, 1.18, wings - 1), wide_back = 1 + (wide_front - 1) * .6; const double xd = 215 + shift * .7, xm = 390 + shift, xa = 570 + shift * 1.15; // the eleven: keeper, four at the back, three in midfield, three up front; the third value says to which side a player moves when dragged const std::array, 11> players{{{85, 300, 0}, {xd, 300 - 190 * wide_back, -1}, {xd, 235, 0}, {xd, 365, 0}, {xd, 300 + 190 * wide_back, 1}, {xm, 300 - 130 * wide_back, -1}, {xm, 300, 0}, {xm, 300 + 130 * wide_back, 1}, {xa, 300 - 170 * wide_front, -1}, {xa + 14, 300, 0}, {xa, 300 + 170 * wide_front, 1}}}; std::string lines, over; // the drawing, and what lies over it: texts and click areas const auto at = [&](double x, double y, double w, double h) { return "left:" + scalar(x * tactics_unit) + "dp;top:" + scalar(y * tactics_unit) + "dp;width:" + scalar(w * tactics_unit) + "dp;height:" + scalar(h * tactics_unit) + "dp;"; }; // a line of handwriting whose baseline is at y; `anchor` is start, middle or end const auto write = [&](double x, double y, const std::string &text, double size, const std::string &colour, const char *anchor = "start", bool bold = false, bool struck = false) { const double box = 420, left = std::string(anchor) == "middle" ? x - box / 2 : std::string(anchor) == "end" ? x - box : x; over += "" + esc(text) + ""; }; // the pitch lines += "" ""; // the opponents as crosses for (const auto &[x, y] : std::array, 5>{{{822, 300}, {700, 120}, {712, 240}, {712, 360}, {700, 480}}}) lines += ""; write(770, 76, "Gegner", 20, red); // play on the wings: the runs of the three forwards const std::array, 3>, 3> runs{{{{{xa + 95, 262}, {xa + 105, 180}, {xa + 120, players[8][1] - 4}}}, {{{xa + 110, 300}, {xa + 110, 300}, {xa + 110, 300}}}, {{{xa + 95, 338}, {xa + 105, 420}, {xa + 120, players[10][1] + 4}}}}}; for (size_t k = 0; k < 3; ++k) { const auto target = tactics_between(runs[k], wings); lines += tactics_arrow(players[8 + k][0] + 22, players[8 + k][1], std::min(835.0, target[0]), target[1], blue, wings > 1.5 && k != 1 ? (k ? 18 : -18) : 0).svg; } if (wings > 1.5) lines += tactics_arrow(std::min(835.0, xa + 124), players[8][1] + 8, std::min(820.0, xa + 150), 250, blue, -14, 2.6, true).svg + tactics_arrow(std::min(835.0, xa + 124), players[10][1] - 8, std::min(820.0, xa + 150), 350, blue, 14, 2.6, true).svg; write(players[8][0] - 20, players[8][1] - 30, text_of(1), 22, blue, "middle"); // passes: their length (through balls) and their arc (flat or high) const double through = level(2), arc = tactics_mix(0, 58, level(4) / 2); const auto pass_to = tactics_between({{{xm + 78, 232}, {xa - 22, 268}, {std::min(800.0, xa + 128), 246}}}, through); const auto pass = tactics_arrow(xm + 20, 292, pass_to[0], pass_to[1], ink, arc, 3); lines += pass.svg; if (through < .5) lines += tactics_arrow(pass_to[0] + 6, pass_to[1] + 6, pass_to[0] + 58, pass_to[1] + 52, ink, arc).svg + tactics_arrow(pass_to[0] + 62, pass_to[1] + 46, pass_to[0] + 112, pass_to[1] - 2, ink, arc).svg; else if (through < 1.5) lines += tactics_arrow(xm + 16, 314, xm + 60, 372, ink).svg; write(pass_to[0] > 690 ? pass_to[0] - 14 : pass_to[0] + 14, pass_to[1] - 20, text_of(2), 21, ink, pass_to[0] > 690 ? "end" : "start"); write(pass.mid_x, pass.mid_y - 16, values[4] > minima[4] ? "hoch" : "flach", 19, grey, "middle"); // the goal kick const double kick = level(3); const std::array kick_to{tactics_mix(xd - 24, xm + 50, kick - 1), tactics_mix(246, 150, kick - 1)}; lines += tactics_arrow(108, 296, kick_to[0], kick_to[1], ink, tactics_mix(0, 60, kick - 1), 3).svg; if (kick > .4 && kick < 1.6) lines += tactics_arrow(108, 296, xm + 30, 160, ink, 55, 2.6, true).svg; write(kick_to[0] - 6, kick_to[1] - 16, text_of(3), 20, ink, "middle"); // offside trap and pressing const bool trap = values[5] > minima[5], pressing = values[6] > minima[6]; if (trap) lines += ""; if (pressing) for (size_t i = 8; i < 11; ++i) lines += ""; // the two boxes above the pitch const auto box = [&](size_t index, double x, const std::string &label, bool on) { lines += ""; if (on) lines += ""; write(x + 30, 28, label, 25, ink, "start", true, !on); over += btn("tactic-cycle", "", "tk-hot", "id=\"tactic-box-" + std::to_string(index) + "\" data-id=\"" + std::to_string(index) + "\" aria-label=\"" + esc(label + (on ? " an" : " aus")) + "\" aria-pressed=\"" + (on ? "true" : "false") + "\" style=\"" + at(x - 8, 2, double(label.size()) * 12 + 44, 34) + "\""); }; box(5, 150, "Abseitsfalle", trap); box(6, 520, "Pressing", pressing); // attitude and effort under the pitch write(44, 592, "Ausrichtung:", 20, grey); write(150, 592, text_of(0), 26, ink, "start", true); write(560, 592, "Einsatz:", 26, ink, "start", true); const double mark_step = limits[7] > 6 ? 17 : 24; for (int n = 1; n <= limits[7]; ++n) over += btn("tactic-effort-set", "!", std::string("tk-mark") + (n <= values[7] ? " on" : ""), "id=\"tactic-effort-" + std::to_string(n) + "\" data-id=\"" + std::to_string(n) + "\" aria-label=\"Einsatz " + std::to_string(n) + " von " + std::to_string(limits[7]) + "\" " + (n < minima[7] ? "disabled " : "") + "style=\"" + at(646 + (n - 1) * mark_step, 556, mark_step, 50) + "color:" + (n <= values[7] ? red : faint) + ";\""); // the own eleven; the wide ones are dragged out and in for (size_t i = 0; i < 11; ++i) { const auto &[x, y, side] = players[i]; lines += ""; over += "" + std::to_string(i + 1) + ""; if (side != 0) over += "
"; } // the three knobs: head of the pass, its arc, head of the goal kick const auto knob = [&](const char *kind, double x, double y, const std::string &colour, const char *label) { lines += ""; over += "
"; }; knob("through", pass_to[0], pass_to[1], ink, "Pfeilspitze ziehen: Steilpässe"); knob("arc", pass.mid_x, pass.mid_y, grey, "Bogen ziehen: flach oder hoch"); knob("kick", kick_to[0], kick_to[1], ink, "Pfeilspitze ziehen: Abschlag"); // the pencil went over every line twice const std::string svg = "" + lines + "" + lines + ""; const double sketch_w = 900 * tactics_unit, sketch_h = 610 * tactics_unit; // --- the page --- std::string out = "
" + picture(renderer_.diagram("tactics-sketch-" + theme_, svg, 2088), sketch_w, sketch_h) + over + "
Meine Taktik
"; for (size_t i = 0; i < 7; ++i) { const bool toggle = i >= 5, on = values[i] > minima[i]; out += btn("tactic-cycle", pg_kicker(names[i], "width:122px;font-size:10px;") + "" + esc(toggle ? (on ? "an" : "aus") : text_of(i)) + "", "tk-row", "id=\"tactic-cycle-" + std::to_string(i) + "\" data-id=\"" + std::to_string(i) + "\" aria-label=\"" + esc(std::string(names[i]) + ": " + text_of(i)) + "\""); } out += "
" + pg_kicker("Einsatz", "width:122px;font-size:10px;") + "" + std::string(size_t(values[7]), '!') + "
" + pg_kicker("Wirkung") + "
" + esc(std::string(names[size_t(last_tactic_)]) + ": " + text_of(size_t(last_tactic_))) + "" + esc(tactic_effect_text(size_t(last_tactic_))) + "
"; if (engine_mode_) out += "
" + pg_button("sports-reset", "Zurücksetzen", "", "id=\"sports-reset\"") + pg_button("sports-apply", "Anwenden", "primary", "id=\"sports-apply\"") + "
"; else out += "Jede Änderung gilt sofort."; return out + "
"; } // Dragging on the sketch: a wide player out or in, the head of the pass along the pitch, its // arc up or down, the head of the goal kick along the pitch. Each step of a setting is a // stretch of the drag; the sketch is redrawn whenever the setting changes. bool App::tactics_page_event(Rml::Event &event) { if (menu_ != "tactics" || !engine_mode_ || engine_busy_ || !popup_.empty() || !engine_tactics_draft_.is_object()) return false; const auto type = event.GetType(); const auto mouse = [&] { return Rml::Vector2f{(event.GetParameter("mouse_x", 0) - float(canvas_left_)) / float(scale_), (event.GetParameter("mouse_y", 0) - float(canvas_top_)) / float(scale_)}; }; if (type == "mousedown" && event.GetParameter("button", 0) == 0) { for (auto *node = event.GetTargetElement(); node && node != doc_; node = node->GetParentNode()) { const auto kind = node->GetAttribute("data-tactic-drag", ""); if (kind.empty()) continue; const size_t index = kind == "wings" ? 1 : kind == "through" ? 2 : kind == "arc" ? 4 : 3; const auto at = mouse(); tactic_drag_ = {{"kind", kind}, {"index", index}, {"side", node->GetAttribute("data-side", 1)}, {"x", at.x}, {"y", at.y}, {"from", engine_tactics_draft_.at("values")[index]}}; event.StopImmediatePropagation(); return true; } return false; } if (!tactic_drag_.is_object()) return false; if (type == "mouseup") { tactic_drag_ = Json(); refresh(); return true; } if (type != "mousemove") return false; const auto at = mouse(); const double dx = (at.x - tactic_drag_.at("x").get()) / tactics_unit, dy = (at.y - tactic_drag_.at("y").get()) / tactics_unit; const size_t index = tactic_drag_.at("index").get(); const auto kind = tactic_drag_.at("kind").get(); const int lo = engine_tactics_draft_.at("minima")[index], hi = engine_tactics_draft_.at("limits")[index]; // how far the mouse travels for the whole range of the setting const double travel = kind == "wings" ? 56 : kind == "through" ? 140 : kind == "arc" ? 52 : 160; const double moved = kind == "wings" ? dy * tactic_drag_.at("side").get() : kind == "arc" ? -dy : dx; const int value = std::clamp(tactic_drag_.at("from").get() + int(std::lround(moved / travel * (hi - lo))), lo, hi); if (engine_tactics_draft_.at("values")[index] != value) { engine_tactics_draft_["values"][index] = value; last_tactic_ = int(index); refresh(); } return true; }