Files
Dreierpack-Manager/src/modern/ui/tactics_page.inc
T
MichaelandClaude Fable 5.2 72e06aabdb Players wear shirt numbers the manager can change
Every player keeps a number from 1 to 99, unique within his squad. The
line-up shows it and changes it by a click; the tactics sketch and the
match sheet wear it too.

Engine: shirt_numbers.hpp, squad read player.shirtNumber, command
lineup.setNumber; saved in state/modern.json.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-17 16:58:34 +01:00

192 lines
17 KiB
C++

// 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 {"<path d=\"M" + scalar(ax) + " " + scalar(ay) + "Q" + scalar(cx) + " " + scalar(cy) + " " + scalar(bx) + " " + scalar(by) + "\" fill=\"none\" stroke=\"" + colour + "\" stroke-width=\"" + scalar(width) +
"\" stroke-linecap=\"round\"" + (dashed ? " stroke-dasharray=\"9 8\"" : "") + "/><path d=\"" + head(.45) + head(-.45) + "\" fill=\"none\" stroke=\"" + colour + "\" stroke-width=\"" + scalar(width) + "\" stroke-linecap=\"round\"/>",
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<double, 2> tactics_between(const std::array<std::array<double, 2>, 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<int, 8> limits = engine_mode_ ? t.at("limits").get<std::array<int, 8>>() : std::array<int, 8>{3, 3, 3, 3, 1, 1, 1, 11};
const std::array<int, 8> minima = engine_mode_ ? t.at("minima").get<std::array<int, 8>>() : std::array<int, 8>{0, 0, 0, 0, 0, 0, 0, 0};
std::array<int, 8> values{};
for (size_t i = 0; i < 8; ++i) values[i] = t["values"][i].get<int>();
// 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<const char *, 8> 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<std::array<double, 3>, 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 += "<span class=\"tk-text\" style=\"" + at(left, y - size * .82, box, size * 1.2) + "font-size:" + scalar(size * tactics_unit) + "dp;line-height:" + scalar(size * 1.2 * tactics_unit) + "dp;color:" + colour + ";text-align:" +
(std::string(anchor) == "middle" ? "center" : std::string(anchor) == "end" ? "right" : "left") + ";" + (bold ? "font-weight:bold;" : "") + (struck ? "text-decoration:line-through;opacity:.6;" : "") + "\">" + esc(text) + "</span>";
};
// the pitch
lines += "<g fill=\"none\" stroke=\"" + ink + "\" stroke-width=\"2.2\" stroke-linecap=\"round\"><path d=\"M40 42H860V558H40ZM450 42V558M40 170H170V430H40M860 170H730V430H860M40 240H88V360H40M860 240H812V360H860M28 262V338M872 262V338\"/>"
"<ellipse cx=\"450\" cy=\"300\" rx=\"62\" ry=\"60\"/><circle cx=\"450\" cy=\"300\" r=\"2.5\" fill=\"" + ink + "\"/></g>";
// the opponents as crosses
for (const auto &[x, y] : std::array<std::pair<int, int>, 5>{{{822, 300}, {700, 120}, {712, 240}, {712, 360}, {700, 480}}})
lines += "<path d=\"M" + std::to_string(x - 11) + " " + std::to_string(y - 11) + "l22 22M" + std::to_string(x + 11) + " " + std::to_string(y - 11) + "l-22 22\" stroke=\"" + red + "\" stroke-width=\"2.6\" stroke-linecap=\"round\" opacity=\".55\"/>";
write(770, 76, "Gegner", 20, red);
// play on the wings: the runs of the three forwards
const std::array<std::array<std::array<double, 2>, 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<double, 2> 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 += "<path d=\"M" + scalar(xd + 30) + " 48V552\" stroke=\"" + red + "\" stroke-width=\"2.6\" stroke-dasharray=\"11 9\" stroke-linecap=\"round\"/><path d=\"M" + scalar(xd + 30) + " 48l22 9-22 9\" fill=\"" + red +
"\" fill-opacity=\".35\" stroke=\"" + red + "\" stroke-width=\"2.2\" stroke-linejoin=\"round\"/>";
if (pressing)
for (size_t i = 8; i < 11; ++i)
lines += "<path d=\"M" + scalar(players[i][0] + 26) + " " + scalar(players[i][1] + 16) + "l12 7-12 7M" + scalar(players[i][0] + 38) + " " + scalar(players[i][1] + 16) + "l12 7-12 7\" fill=\"none\" stroke=\"" + red +
"\" stroke-width=\"2.6\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>";
// the two boxes above the pitch
const auto box = [&](size_t index, double x, const std::string &label, bool on) {
lines += "<path d=\"M" + scalar(x) + " 9h20v20h-20z\" fill=\"" + paper + "\" stroke=\"" + ink + "\" stroke-width=\"2.2\"/>";
if (on) lines += "<path d=\"M" + scalar(x + 3) + " 18l6 8 15-22\" fill=\"none\" stroke=\"" + red + "\" stroke-width=\"3.2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>";
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", "<span>!</span>", 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 += "<circle cx=\"" + scalar(x) + "\" cy=\"" + scalar(y) + "\" r=\"17\" fill=\"" + paper + "\" fill-opacity=\".85\" stroke=\"" + ink + "\" stroke-width=\"2.6\"/>";
over += "<span class=\"tk-text tk-number\" style=\"" + at(x - 25, y - 14, 50, 28) + "font-size:" + scalar(23 * tactics_unit) + "dp;line-height:" + scalar(28 * tactics_unit) + "dp;color:" + ink + ";\">" + engine_shirt_at(i) + "</span>";
if (side != 0) over += "<div class=\"tk-drag\" data-tactic-drag=\"wings\" data-side=\"" + std::to_string(int(side)) + "\" title=\"Nach außen oder innen ziehen\" style=\"" + at(x - 25, y - 25, 50, 50) + "\"></div>";
}
// 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 += "<circle cx=\"" + scalar(x) + "\" cy=\"" + scalar(y) + "\" r=\"8\" fill=\"" + paper + "\" stroke=\"" + colour + "\" stroke-width=\"2.4\"/><circle cx=\"" + scalar(x) + "\" cy=\"" + scalar(y) + "\" r=\"2.6\" fill=\"" + colour + "\"/>";
over += "<div class=\"tk-drag\" data-tactic-drag=\"" + std::string(kind) + "\" title=\"" + label + "\" style=\"" + at(x - 18, y - 18, 36, 36) + "\"></div>";
};
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 = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 900 610\"><g>" + lines + "</g><g opacity=\".32\" transform=\"translate(1.4 1) rotate(.22 450 305)\">" + lines + "</g></svg>";
const double sketch_w = 900 * tactics_unit, sketch_h = 610 * tactics_unit;
// --- the page ---
std::string out = "<div class=\"pg-page\"><div class=\"pg-body\"><div class=\"tk-sketch\" id=\"tactics-sketch\" role=\"group\" aria-label=\"Taktikskizze\" style=\"width:" + scalar(sketch_w) + "dp;height:" + scalar(sketch_h) + "dp;margin-top:" +
scalar((1620 - sketch_h) / 2) + "dp;\">" + picture(renderer_.diagram("tactics-sketch-" + theme_, svg, 2088), sketch_w, sketch_h) + over + "</div><div class=\"pg-col pg-grow\"><strong class=\"tk-title\">Meine Taktik</strong><div class=\"pg-col pg-grow\" style=\"gap:0dp;\">";
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;") + "<span class=\"tk-value" + std::string(toggle && !on ? " off" : "") + "\">" + esc(toggle ? (on ? "an" : "aus") : text_of(i)) + "</span>", "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 += "<div class=\"tk-row last\">" + pg_kicker("Einsatz", "width:122px;font-size:10px;") + "<span class=\"tk-value\" id=\"tactic-effort-value\" style=\"color:" + red + ";letter-spacing:" + pg(3) + ";\">" + std::string(size_t(values[7]), '!') +
"</span></div></div><div class=\"pg-surf\" style=\"" + pg_css("padding:10px 14px;") + "\">" + pg_kicker("Wirkung") + "<div id=\"tactic-hint\" class=\"tk-hint\"><strong>" +
esc(std::string(names[size_t(last_tactic_)]) + ": " + text_of(size_t(last_tactic_))) + "</strong><span>" + esc(tactic_effect_text(size_t(last_tactic_))) + "</span></div></div>";
if (engine_mode_)
out += "<span id=\"sports-note\" class=\"pg-mut tk-note\"></span><div class=\"pg-row\">" + pg_button("sports-reset", "Zurücksetzen", "", "id=\"sports-reset\"") + pg_button("sports-apply", "Anwenden", "primary", "id=\"sports-apply\"") + "</div>";
else out += "<span class=\"pg-mut tk-note\">Jede Änderung gilt sofort.</span>";
return out + "</div></div></div>";
}
// 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<float>("mouse_x", 0) - float(canvas_left_)) / float(scale_), (event.GetParameter<float>("mouse_y", 0) - float(canvas_top_)) / float(scale_)}; };
if (type == "mousedown" && event.GetParameter<int>("button", 0) == 0) {
for (auto *node = event.GetTargetElement(); node && node != doc_; node = node->GetParentNode()) {
const auto kind = node->GetAttribute<Rml::String>("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<int>("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<double>()) / tactics_unit, dy = (at.y - tactic_drag_.at("y").get<double>()) / tactics_unit;
const size_t index = tactic_drag_.at("index").get<size_t>();
const auto kind = tactic_drag_.at("kind").get<std::string>();
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<int>() : kind == "arc" ? -dy : dx;
const int value = std::clamp(tactic_drag_.at("from").get<int>() + 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;
}