Port native Training and Tactics from the HTML reference (by Codex)
This commit is contained in:
1 parent
c737c17870
commit
79bf38aa78
18 files changed
+952
-12
No files matched your search
@@ -1,5 +1,5 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
add_library(bmh_modern_ui_core STATIC catalog.cpp model.cpp gui_assets.cpp desktop.cpp)
|
||||
add_library(bmh_modern_ui_core STATIC catalog.cpp model.cpp gui_assets.cpp desktop.cpp demo_sports.cpp)
|
||||
target_include_directories(bmh_modern_ui_core PUBLIC ../..)
|
||||
target_link_libraries(bmh_modern_ui_core PUBLIC bmh_asset_io)
|
||||
add_library(bmh_modern_ui STATIC view.cpp ticket_popover.cpp)
|
||||
@@ -14,7 +14,7 @@ endif()
|
||||
set(modern_assets "${CMAKE_BINARY_DIR}/assets/modern")
|
||||
FetchContent_GetProperties(rmlui)
|
||||
file(MAKE_DIRECTORY "${modern_assets}")
|
||||
foreach(asset modern.rml theme.rcss ticket.rcss)
|
||||
foreach(asset modern.rml theme.rcss ticket.rcss sports.rcss)
|
||||
configure_file("${CMAKE_SOURCE_DIR}/assets/modern/${asset}" "${modern_assets}/${asset}" COPYONLY)
|
||||
endforeach()
|
||||
file(GLOB modern_icons CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/assets/modern/icons/*.svg")
|
||||
|
||||
@@ -29,13 +29,13 @@ inline bool center_selects(Rml::ElementDocument &document) {
|
||||
bool changed = false;
|
||||
Rml::ElementList controls;
|
||||
document.QuerySelectorAll(controls, "select");
|
||||
const auto set_pixels = [&](Rml::Element *element, const char *name,
|
||||
const auto set_pixels = [&](Rml::Element *element, Rml::PropertyId name,
|
||||
float value) {
|
||||
const auto *old = element->GetLocalProperty(name);
|
||||
if (old && old->unit == Rml::Unit::PX &&
|
||||
std::abs(old->Get<float>() - value) < .02f)
|
||||
return;
|
||||
element->SetProperty(name, std::to_string(value) + "px");
|
||||
element->SetProperty(name, Rml::Property(value, Rml::Unit::PX));
|
||||
changed = true;
|
||||
};
|
||||
for (auto *select : controls) {
|
||||
@@ -48,8 +48,8 @@ inline bool center_selects(Rml::ElementDocument &document) {
|
||||
continue;
|
||||
// WidgetDropDown fixes both offsets at the content-box top. A flex
|
||||
// selectvalue loses its text in pinned RmlUI; center its line instead.
|
||||
set_pixels(caption, "line-height", height);
|
||||
set_pixels(arrow, "margin-top",
|
||||
set_pixels(caption, Rml::PropertyId::LineHeight, height);
|
||||
set_pixels(arrow, Rml::PropertyId::MarginTop,
|
||||
std::max(0.f, (height -
|
||||
arrow->GetBox().GetSize(Rml::BoxArea::Border).y) /
|
||||
2));
|
||||
|
||||
@@ -131,6 +131,10 @@ void App::drag_event(Rml::Event &event) {
|
||||
}
|
||||
void App::ProcessEvent(Rml::Event &event) try {
|
||||
const auto type = event.GetType();
|
||||
if (type == "keydown")
|
||||
keyboard_navigation_ = true;
|
||||
else if (type == "mousedown")
|
||||
keyboard_navigation_ = false;
|
||||
for (auto *n = event.GetTargetElement(); n && n != doc_;
|
||||
n = n->GetParentNode())
|
||||
if (n->GetId() == "native-ticket-layer")
|
||||
@@ -267,7 +271,10 @@ void App::ProcessEvent(Rml::Event &event) try {
|
||||
}
|
||||
}
|
||||
if (type == "change") {
|
||||
change(target);
|
||||
if (target->HasAttribute("data-input"))
|
||||
input(target);
|
||||
else
|
||||
change(target);
|
||||
return;
|
||||
}
|
||||
if (type == "input") {
|
||||
@@ -879,7 +886,8 @@ void App::dispatch(const std::string &action, Rml::Element *target) {
|
||||
return;
|
||||
}
|
||||
static const std::set<std::string> available = {
|
||||
"tables", "strengths", "camp", "settings", "save", "load"};
|
||||
"tables", "strengths", "camp", "settings",
|
||||
"save", "load", "training", "tactics"};
|
||||
if (!available.contains(route)) {
|
||||
message("Diese native Ansicht wird im nächsten Menüpaket übertragen.");
|
||||
return;
|
||||
@@ -892,6 +900,39 @@ void App::dispatch(const std::string &action, Rml::Element *target) {
|
||||
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<Rml::String>("data-group", "");
|
||||
change = {{"kind", "ball"},
|
||||
{"group", group},
|
||||
{"index", std::stoi(id)},
|
||||
{"value", target->GetAttribute<int>("data-value", 1)}};
|
||||
if (group == "player")
|
||||
change["player"] = target->GetAttribute<Rml::String>("data-player", "");
|
||||
}
|
||||
if (apply("training", change)) {
|
||||
focus_after_render_ = target->GetId();
|
||||
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_);
|
||||
@@ -1061,6 +1102,19 @@ void App::change(Rml::Element *target) {
|
||||
pending_selects_.insert(target->GetId());
|
||||
return;
|
||||
}
|
||||
if (kind == "training-focus") {
|
||||
const auto player = target->GetAttribute<Rml::String>("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;
|
||||
}
|
||||
if ((kind == "manager-country" || kind == "table-country") &&
|
||||
value == country_)
|
||||
return;
|
||||
@@ -1097,6 +1151,18 @@ void App::input(Rml::Element *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 == "sound-volume") {
|
||||
const int n = std::stoi(value);
|
||||
draft_preferences_["volume"] = n;
|
||||
|
||||
@@ -0,0 +1,264 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/demo_sports.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
bool integers(const Json &a, size_t size, int low, int high) {
|
||||
if (!a.is_array() || a.size() != size)
|
||||
return false;
|
||||
return std::all_of(a.begin(), a.end(), [&](const auto &v) {
|
||||
return v.is_number_integer() && v >= low && v <= high;
|
||||
});
|
||||
}
|
||||
int sum(const Json &a, size_t begin, size_t end) {
|
||||
int n = 0;
|
||||
for (size_t i = begin; i < end; ++i)
|
||||
n += a.at(i).template get<int>();
|
||||
return n;
|
||||
}
|
||||
constexpr std::array<int, 8> tactic_limits = {3, 3, 3, 3, 1, 1, 1, 11};
|
||||
} // namespace
|
||||
Json demo_training(const Catalog &world, const std::string &club) {
|
||||
Json players = Json::object();
|
||||
size_t i = 0;
|
||||
for (auto *player : world.squad(club)) {
|
||||
players[player->value("id", "")] = {{"intensity", i % 5 == 0 ? 1 : 2},
|
||||
{"focus", int(i % 8)}};
|
||||
++i;
|
||||
}
|
||||
return {{"club", club},
|
||||
{"areas", {2, 2, 1, 1, 2, 2, 2, 2, 3}},
|
||||
{"types", {2, 2, 2, 2}},
|
||||
{"autos", {false, false, false, false}},
|
||||
{"players", players}};
|
||||
}
|
||||
bool valid_demo_training(const Json &value, const Catalog &world,
|
||||
const std::string &club) {
|
||||
try {
|
||||
if (!value.is_object() || value.at("club") != club ||
|
||||
!integers(value.at("areas"), 9, 1, 3) ||
|
||||
!integers(value.at("types"), 4, 1, 3))
|
||||
return false;
|
||||
if (sum(value["types"], 0, 4) > 8 || sum(value["areas"], 0, 2) > 4 ||
|
||||
sum(value["areas"], 2, 9) > 18)
|
||||
return false;
|
||||
const auto &autos = value.at("autos");
|
||||
if (!autos.is_array() || autos.size() != 4)
|
||||
return false;
|
||||
for (const auto &v : autos)
|
||||
if (!v.is_boolean())
|
||||
return false;
|
||||
const auto &players = value.at("players");
|
||||
const auto squad = world.squad(club);
|
||||
if (!players.is_object() || players.size() != squad.size())
|
||||
return false;
|
||||
for (auto *p : squad) {
|
||||
const auto &v = players.at(p->value("id", ""));
|
||||
if (!v.at("intensity").is_number_integer() || v["intensity"] < 0 ||
|
||||
v["intensity"] > 3 || !v.at("focus").is_number_integer() ||
|
||||
v["focus"] < 0 || v["focus"] > 7)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (const Json::exception &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
std::string edit_demo_training(Json &state, const Json &action) {
|
||||
for (const auto *key : {"index", "value"})
|
||||
if (action.contains(key) && (!action[key].is_number_integer() ||
|
||||
action[key] < 0 || action[key] > 100))
|
||||
return "invalid_training_value";
|
||||
auto next = state;
|
||||
const auto kind = action.at("kind").get<std::string>();
|
||||
if (kind == "auto" || kind == "auto-all") {
|
||||
const int group = kind == "auto-all" ? -1 : action.at("index").get<int>();
|
||||
if (group < -1 || group > 3)
|
||||
return "invalid_training_group";
|
||||
const bool enabled =
|
||||
kind == "auto-all"
|
||||
? !std::all_of(next["autos"].begin(), next["autos"].end(),
|
||||
[](const auto &v) { return v == true; })
|
||||
: !next["autos"][size_t(group)].get<bool>();
|
||||
for (int i = 0; i < 4; ++i)
|
||||
if (group == -1 || group == i) {
|
||||
next["autos"][size_t(i)] = enabled;
|
||||
if (!enabled)
|
||||
continue;
|
||||
if (i == 0)
|
||||
next["types"] = {2, 2, 2, 2};
|
||||
if (i == 1) {
|
||||
next["areas"][0] = 2;
|
||||
next["areas"][1] = 2;
|
||||
}
|
||||
if (i == 2)
|
||||
for (size_t j = 2; j < 9; ++j)
|
||||
next["areas"][j] = 2;
|
||||
if (i == 3)
|
||||
for (auto &[id, p] : next["players"].items()) {
|
||||
(void)id;
|
||||
p["intensity"] = 2;
|
||||
}
|
||||
}
|
||||
} else if (kind == "focus") {
|
||||
const auto player = action.at("player").get<std::string>();
|
||||
const int value = action.at("value").get<int>();
|
||||
if (!next["players"].contains(player) || value < 0 || value > 7)
|
||||
return "invalid_training_player";
|
||||
if (next["autos"][3] == true)
|
||||
return "training_automatic";
|
||||
next["players"][player]["focus"] = value;
|
||||
} else if (kind == "ball") {
|
||||
const auto group = action.at("group").get<std::string>();
|
||||
const int value = action.at("value").get<int>();
|
||||
if (value < 1 || value > 3)
|
||||
return "invalid_training_value";
|
||||
if (group == "player") {
|
||||
const auto player = action.at("player").get<std::string>();
|
||||
if (!next["players"].contains(player))
|
||||
return "invalid_training_player";
|
||||
if (next["autos"][3] == true)
|
||||
return "training_automatic";
|
||||
auto &v = next["players"][player]["intensity"];
|
||||
v = v == value ? 0 : value;
|
||||
} else {
|
||||
const int index = action.at("index").get<int>();
|
||||
const bool area = group == "area";
|
||||
if ((!area && group != "type") || index < 0 || index >= (area ? 9 : 4))
|
||||
return "invalid_training_group";
|
||||
if (next["autos"][size_t(area ? (index < 2 ? 1 : 2) : 0)] == true)
|
||||
return "training_automatic";
|
||||
auto &list = next[area ? "areas" : "types"];
|
||||
list[size_t(index)] = value;
|
||||
if (sum(next["types"], 0, 4) > 8 || sum(next["areas"], 0, 2) > 4 ||
|
||||
sum(next["areas"], 2, 9) > 18)
|
||||
return "training_preview_budget";
|
||||
}
|
||||
} else
|
||||
return "unknown_training_action";
|
||||
state = std::move(next);
|
||||
return {};
|
||||
}
|
||||
Json demo_tactics() { return {{"values", {2, 1, 1, 0, 1, 0, 1, 6}}}; }
|
||||
bool valid_demo_tactics(const Json &value) {
|
||||
try {
|
||||
const auto &v = value.at("values");
|
||||
if (!integers(v, 8, 0, 11))
|
||||
return false;
|
||||
for (size_t i = 0; i < 8; ++i)
|
||||
if (v[i] > tactic_limits[i])
|
||||
return false;
|
||||
return true;
|
||||
} catch (const Json::exception &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
std::string edit_demo_tactics(Json &state, const Json &action) {
|
||||
for (const auto *key : {"index", "value"})
|
||||
if (action.contains(key) && (!action[key].is_number_integer() ||
|
||||
action[key] < 0 || action[key] > 100))
|
||||
return "invalid_tactic_value";
|
||||
const auto kind = action.at("kind").get<std::string>();
|
||||
const int index = kind == "effort" ? 7 : action.at("index").get<int>();
|
||||
if (index < 0 || index > 7)
|
||||
return "invalid_tactic_index";
|
||||
if (kind == "cycle")
|
||||
state["values"][size_t(index)] =
|
||||
(state["values"][size_t(index)].get<int>() + 1) %
|
||||
(tactic_limits[size_t(index)] + 1);
|
||||
else if (kind == "effort") {
|
||||
const int v = action.at("value").get<int>();
|
||||
if (v < 0 || v > 11)
|
||||
return "invalid_tactic_value";
|
||||
state["values"][7] = v;
|
||||
} else
|
||||
return "unknown_tactic_action";
|
||||
return {};
|
||||
}
|
||||
std::string tactics_diagram(const Json &state, const std::string &theme) {
|
||||
const auto values = state.at("values").get<std::array<int, 8>>();
|
||||
const std::string line = theme == "silver" ? "#536f66" : "#dfe8bf";
|
||||
const auto field_line = theme == "silver" ? "#536f66" : "#d7edbf",
|
||||
man_line = theme == "silver" ? "#536f66" : "#c9e6ae",
|
||||
curve_line = theme == "silver" ? "#536f66" : "#d4e7ba";
|
||||
const std::string defence = theme == "silver" ? "#85600c" : "#ffdd6b";
|
||||
std::ostringstream out;
|
||||
out.imbue(std::locale::classic());
|
||||
out << "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 295 "
|
||||
"206'><defs><marker id='arrow' markerWidth='5' markerHeight='5' "
|
||||
"refX='4' refY='2.5' orient='auto'><path d='M0 0L5 2.5L0 5Z' fill='"
|
||||
<< line << "'/></marker></defs>";
|
||||
const auto field = [&](double x, double y, double w, double h) {
|
||||
out << "<g fill='none' stroke='" << field_line
|
||||
<< "' stroke-width='.65'><path d='M" << x << ' ' << y << 'h' << w << 'v'
|
||||
<< h << 'h' << -w << "Z M" << x + w / 2 << ' ' << y << 'v' << h << " M"
|
||||
<< x << ' ' << y + h * .25 << 'h' << w * .15 << 'v' << h * .5 << 'h'
|
||||
<< -w * .15 << " M" << x + w << ' ' << y + h * .25 << 'h' << -w * .15
|
||||
<< 'v' << h * .5 << 'h' << w * .15 << "'/><circle cx='" << x + w / 2
|
||||
<< "' cy='" << y + h / 2 << "' r='" << h * .14 << "'/></g>";
|
||||
};
|
||||
const auto dot = [&](double x, double y, bool opponent = false) {
|
||||
out << "<circle cx='" << x << "' cy='" << y << "' r='2.4' fill='"
|
||||
<< (opponent ? "#e46145" : defence)
|
||||
<< "' stroke='#809344' stroke-width='.4'/>";
|
||||
};
|
||||
const auto arrow = [&](double x, double y, double dx, double dy,
|
||||
bool dashed = false) {
|
||||
out << "<path d='M" << x << ' ' << y << 'l' << dx << ' ' << dy
|
||||
<< "' fill='none' stroke='" << line << "' stroke-width='.85' "
|
||||
<< (dashed ? "stroke-dasharray='2 1.3'" : "")
|
||||
<< " marker-end='url(#arrow)'/>";
|
||||
};
|
||||
field(19, 9, 104, 62);
|
||||
field(19, 82, 104, 62);
|
||||
field(137, 10, 58, 37);
|
||||
field(205, 10, 58, 37);
|
||||
const std::array<std::pair<int, int>, 10> formation = {{{5, 1},
|
||||
{5, 3},
|
||||
{5, 5},
|
||||
{5, 7},
|
||||
{10, 1},
|
||||
{10, 3},
|
||||
{10, 5},
|
||||
{10, 7},
|
||||
{16, 3},
|
||||
{16, 5}}};
|
||||
for (const auto &[x, y] : formation) {
|
||||
const double px = 25 + x / 18.0 * 82, py = 16 + y / 8.0 * 49;
|
||||
arrow(px, py, std::array<int, 4>{-16, -7, 7, 16}[size_t(values[0])], 0);
|
||||
dot(px, py);
|
||||
}
|
||||
for (int k = 0; k < 2; ++k) {
|
||||
const int x = 137 + k * 68;
|
||||
dot(x + 12, 27);
|
||||
dot(x + 25, 20);
|
||||
dot(x + 25, 37);
|
||||
dot(x + 43, 28, true);
|
||||
if (values[size_t(5 + k)] == 0)
|
||||
out << "<path d='M" << x + 3 << " 12l52 32 M" << x + 55
|
||||
<< " 12l-52 32' stroke='" << line << "' stroke-width='1.6'/>";
|
||||
}
|
||||
dot(29, 111);
|
||||
dot(76, 94);
|
||||
dot(76, 119);
|
||||
dot(101, 118);
|
||||
dot(111, 120, true);
|
||||
arrow(29, 111, 10 + values[3] * 10, -values[3] * 5, true);
|
||||
arrow(76, 94, 10 + values[2] * 8, 12 - values[2] * 5);
|
||||
arrow(76, 119, 10 + values[1] * 8, -8 + values[1] * 4, true);
|
||||
for (int x : {181, 222})
|
||||
out << "<g fill='none' stroke='" << man_line
|
||||
<< "' stroke-width='.8'><circle cx='" << x
|
||||
<< "' cy='65' r='2'/><path d='M" << x
|
||||
<< " 67l-2 7 5 4 m-5-4 -4 4 m5-8 -5 2 m5-2 5 2'/></g>";
|
||||
out << "<path d='M188 72Q201 " << (values[4] ? 45 : 72)
|
||||
<< " 215 72' fill='none' stroke='" << curve_line
|
||||
<< "' stroke-width='.9' stroke-dasharray='1 1.5' "
|
||||
"marker-end='url(#arrow)'/></svg>";
|
||||
return out.str();
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
@@ -0,0 +1,15 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#pragma once
|
||||
#include "modern/ui/catalog.hpp"
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
// UI fixtures from the HTML reference; these never run a training/match rule.
|
||||
Json demo_training(const Catalog &, const std::string &club);
|
||||
bool valid_demo_training(const Json &, const Catalog &,
|
||||
const std::string &club);
|
||||
std::string edit_demo_training(Json &, const Json &action);
|
||||
Json demo_tactics();
|
||||
bool valid_demo_tactics(const Json &);
|
||||
std::string edit_demo_tactics(Json &, const Json &action);
|
||||
std::string tactics_diagram(const Json &, const std::string &theme);
|
||||
} // namespace bmh::modern_ui
|
||||
+25
-1
@@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/model.hpp"
|
||||
#include "modern/ui/demo_sports.hpp"
|
||||
#include "modern/ui/desktop.hpp"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
@@ -73,6 +74,8 @@ DemoSource::DemoSource(std::shared_ptr<const Catalog> catalog)
|
||||
{"preferences",
|
||||
{{"theme", "midnight"}, {"volume", 80}, {"effects", true}}},
|
||||
{"edits", Json::object()}};
|
||||
state_["training"] = demo_training(*catalog_, club);
|
||||
state_["tactics"] = demo_tactics();
|
||||
saved_ = state_;
|
||||
if (!valid_date(state_.at("date")))
|
||||
throw editor::Error("invalid_demo_date");
|
||||
@@ -106,6 +109,19 @@ ActionResult DemoSource::apply(const Action &action) {
|
||||
return {false, revision_, "invalid_manager"};
|
||||
}
|
||||
next["managers"] = action.value;
|
||||
const auto first = action.value.at(0).at("club").get<std::string>();
|
||||
if (next.at("training").at("club") != first) {
|
||||
next["training"] = demo_training(*catalog_, first);
|
||||
next["tactics"] = demo_tactics();
|
||||
}
|
||||
} else if (action.kind == "training") {
|
||||
const auto error = edit_demo_training(next["training"], action.value);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
} else if (action.kind == "tactics") {
|
||||
const auto error = edit_demo_tactics(next["tactics"], action.value);
|
||||
if (!error.empty())
|
||||
return {false, revision_, error};
|
||||
} else if (action.kind == "camp.select") {
|
||||
if (!camp_id(action.value))
|
||||
return {false, revision_, "camp_unknown"};
|
||||
@@ -147,7 +163,7 @@ void DemoSource::save(const fs::path &file, const Json &desktop) {
|
||||
saved_ = state_;
|
||||
}
|
||||
Json DemoSource::load(const fs::path &file) {
|
||||
const auto data = Json::parse(editor::read_file(file));
|
||||
auto data = Json::parse(editor::read_file(file));
|
||||
if (data.value("schema", "") != "openbmh-ui-demo@1" ||
|
||||
data.value("world", "") != catalog_->manifest().value("id", ""))
|
||||
throw editor::Error("wrong_demo_world");
|
||||
@@ -155,6 +171,14 @@ Json DemoSource::load(const fs::path &file) {
|
||||
const auto result = validation.apply({0, "managers", data.at("managers")});
|
||||
if (!result.ok)
|
||||
throw editor::Error(result.error);
|
||||
const auto club = data.at("managers").at(0).at("club").get<std::string>();
|
||||
if (!data.contains("training"))
|
||||
data["training"] = demo_training(*catalog_, club);
|
||||
if (!data.contains("tactics"))
|
||||
data["tactics"] = demo_tactics();
|
||||
if (!valid_demo_training(data.at("training"), *catalog_, club) ||
|
||||
!valid_demo_tactics(data.at("tactics")))
|
||||
throw editor::Error("invalid_demo_sports");
|
||||
if (!data.at("ticket").is_number_integer() || data.at("ticket") < 0 ||
|
||||
data.at("ticket") > 25)
|
||||
throw editor::Error("invalid_demo_ticket");
|
||||
|
||||
@@ -963,6 +963,10 @@ std::string App::render_menu(const std::string &key) {
|
||||
std::string body;
|
||||
if (key == "tables" || key == "strengths")
|
||||
body = render_table(key == "strengths", false);
|
||||
else if (key == "training")
|
||||
body = render_training();
|
||||
else if (key == "tactics")
|
||||
body = render_tactics();
|
||||
else if (key == "camp")
|
||||
body = render_camp();
|
||||
else if (key == "settings")
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
std::string App::training_balls(const std::string &group, int index, int value,
|
||||
bool automatic, const std::string &player,
|
||||
int size, int gap, bool overtraining) {
|
||||
std::string body =
|
||||
"<div class=\"training-balls\" style=\"gap:" + std::to_string(gap) +
|
||||
"dp;\">";
|
||||
for (int i = 1; i <= 3; ++i)
|
||||
body += btn(
|
||||
"training-ball", img("training.ball", size, size, false, false, 128),
|
||||
std::string("training-ball ") +
|
||||
(i <= value ? "is-selected" : "is-empty") +
|
||||
(overtraining && i <= value ? " overtraining" : ""),
|
||||
"id=\"training-ball-" + group + "-" + std::to_string(index) + "-" +
|
||||
std::to_string(i) + "\" data-group=\"" + group + "\" data-id=\"" +
|
||||
std::to_string(index) + "\" data-player=\"" + esc(player) +
|
||||
"\" data-value=\"" + std::to_string(i) + "\" style=\"width:" +
|
||||
std::to_string(size) + "dp;height:" + std::to_string(size) +
|
||||
"dp;\" " + (automatic ? "disabled" : ""));
|
||||
return body + "</div>";
|
||||
}
|
||||
std::string App::render_training() {
|
||||
const auto s = snap();
|
||||
const auto &t = s.state.at("training"), &automatic = t.at("autos");
|
||||
const std::array<const char *, 9> skills = {
|
||||
"Kondition", "Technik", "Eckball", "Freistoß", "Zweikampf",
|
||||
"Pass", "Schuss", "Schnelligkeit", "Teamplay"};
|
||||
const std::vector<std::pair<std::string, std::string>> focuses = {
|
||||
{"0", "Allgemein"}, {"1", "Eckball"}, {"2", "Freistoß"},
|
||||
{"3", "Zweikampf"}, {"4", "Pass"}, {"5", "Schuss"},
|
||||
{"6", "Schnelligkeit"}, {"7", "Teamplay"}};
|
||||
int skill_total = 0, type_total = 0;
|
||||
for (size_t i = 2; i < 9; ++i)
|
||||
skill_total += t["areas"][i].get<int>();
|
||||
for (const auto &v : t["types"])
|
||||
type_total += v.get<int>();
|
||||
std::string body = "<div class=\"training-skills\">";
|
||||
for (size_t i = 0; i < 9; ++i)
|
||||
body +=
|
||||
"<div class=\"training-skill\"><div style=\"width:86dp;\">" +
|
||||
img("training.skill." + std::to_string(i), 80, 80, false, false, 160) +
|
||||
"</div><span style=\"width:677dp;\">" + skills[i] +
|
||||
"</span><span style=\"width:35dp;\">:</span><div "
|
||||
"style=\"width:220dp;\">" +
|
||||
training_balls("area", int(i), t["areas"][i].get<int>(),
|
||||
automatic[i < 2 ? 1 : 2].get<bool>(), {}, 56, 10,
|
||||
i >= 2 && skill_total > 14) +
|
||||
"</div></div>";
|
||||
body += "</div><div class=\"training-pitch\">";
|
||||
for (int i = 0; i < 10; ++i)
|
||||
body += "<div style=\"position:absolute;left:0;right:0;top:" +
|
||||
std::to_string(i * 43) + "dp;height:43dp;background-color:" +
|
||||
std::string(i % 2 ? "#306b4c" : "#2d6447") + ";\"></div>";
|
||||
body += "<div class=\"training-midline\"></div><div "
|
||||
"class=\"training-circle\"></div><div class=\"training-box "
|
||||
"left-box\"></div><div class=\"training-box right-box\"></div>";
|
||||
for (int i = 0; i < 4; ++i)
|
||||
body += "<div class=\"training-type-cell\" style=\"left:" +
|
||||
std::to_string(30 + (i % 2) * 277) +
|
||||
"dp;top:" + std::to_string(26 + (i / 2) * 176) + "dp;\">" +
|
||||
training_balls("type", i, t["types"][size_t(i)].get<int>(),
|
||||
automatic[0].get<bool>(), {}, 48, 5) +
|
||||
"</div>";
|
||||
body += "</div>";
|
||||
const std::array<const char *, 4> auto_names = {
|
||||
"AUTO · Arten", "AUTO · Kondition", "AUTO · Fertigkeiten",
|
||||
"AUTO · Spieler"};
|
||||
const std::array<std::array<int, 4>, 4> auto_boxes = {
|
||||
{{0, 1456, 570, 76},
|
||||
{604, 1040, 440, 76},
|
||||
{604, 1136, 440, 76},
|
||||
{2312, 1803, 300, 61}}};
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
const auto a = auto_boxes[i];
|
||||
body += btn(
|
||||
"training-auto", auto_names[i],
|
||||
std::string(i == 3 ? "training-auto auto-players " : "training-auto ") +
|
||||
(automatic[i].get<bool>() ? "primary" : ""),
|
||||
"id=\"training-auto-" + std::to_string(i) + "\" data-id=\"" +
|
||||
std::to_string(i) + "\" style=\"position:absolute;" +
|
||||
rect(a[0], a[1], a[2], a[3]) + "\"");
|
||||
}
|
||||
const bool all_auto = std::all_of(automatic.begin(), automatic.end(),
|
||||
[](const auto &v) { return v == true; });
|
||||
body += btn("training-auto-all", "Automatisch",
|
||||
std::string("training-auto ") + (all_auto ? "primary" : ""),
|
||||
"id=\"training-auto-all\" style=\"position:absolute;" +
|
||||
rect(604, 1232, 440, 76) + "\"");
|
||||
body += "<p class=\"training-budget\">Arten: " + std::to_string(type_total) +
|
||||
" / 8 Bälle ·<br/>Kondition/Technik: " +
|
||||
std::to_string(t["areas"][0].get<int>() + t["areas"][1].get<int>()) +
|
||||
" / 4 ·<br/>Fertigkeiten: " + std::to_string(skill_total) + " / 18" +
|
||||
(skill_total > 14 ? " · hohe<br/>Belastung" : "") +
|
||||
"</p><div id=\"training-roster\" class=\"training-roster\"><div "
|
||||
"class=\"training-row "
|
||||
"training-header\">";
|
||||
const std::array<const char *, 5> headings = {"Pos.", "Spieler", "Stärke",
|
||||
"Schwerpunkt", "Intensität"};
|
||||
const std::array<int, 5> widths = {90, 300, 95, 510, 497};
|
||||
for (size_t i = 0; i < 5; ++i)
|
||||
body += "<div style=\"width:" + std::to_string(widths[i]) + "dp;\">" +
|
||||
headings[i] + "</div>";
|
||||
body += "</div>";
|
||||
const auto players = world_->squad(t.value("club", ""));
|
||||
for (size_t i = 0; i < players.size(); ++i) {
|
||||
const auto *p = players[i];
|
||||
const auto id = p->value("id", "");
|
||||
const auto &row = t["players"].at(id);
|
||||
const auto position = p->value("positions", Json::array());
|
||||
const auto pos = position.empty() ? "" : position[0].get<std::string>();
|
||||
body +=
|
||||
"<div class=\"training-row " + std::string(i % 2 ? "striped" : "") +
|
||||
"\"><div class=\"training-position\" style=\"width:90dp;\">" +
|
||||
std::string(pos == "gk" ? "TW"
|
||||
: pos == "def" ? "ABW"
|
||||
: pos == "mid" ? "MIT"
|
||||
: pos == "fwd" ? "STU"
|
||||
: pos) +
|
||||
"</div><div class=\"training-name\" style=\"width:300dp;\" title=\"" +
|
||||
esc(world_->name(*p)) + "\">" + esc(world_->name(*p)) +
|
||||
"</div><div style=\"width:95dp;\">" +
|
||||
std::to_string(
|
||||
p->value("attributes", Json::object()).value("strength", 0)) +
|
||||
"</div><div style=\"width:510dp;\">";
|
||||
auto selector =
|
||||
select("training-focus-" + std::to_string(i), focuses,
|
||||
std::to_string(row.value("focus", 0)), "training-focus");
|
||||
const auto end = selector.find('>');
|
||||
selector.insert(end, " data-player=\"" + esc(id) + "\" " +
|
||||
(automatic[3] == true ? "disabled" : ""));
|
||||
body += selector + "</div><div style=\"width:497dp;\">" +
|
||||
training_balls("player", int(i), row.value("intensity", 0),
|
||||
automatic[3].get<bool>(), id, 50, 9) +
|
||||
"</div></div>";
|
||||
}
|
||||
return body + "</div>";
|
||||
}
|
||||
std::string App::render_tactics() {
|
||||
const auto t = snap().state.at("tactics");
|
||||
const std::array<const char *, 8> names = {
|
||||
"Defensiv / offensiv", "Pass-/Schussbild unten",
|
||||
"Pass-/Schussbild Mitte", "Pass-/Schussbild links",
|
||||
"Flache / hohe Bälle", "Flügelbild links",
|
||||
"Flügelbild rechts", "Einsatz"};
|
||||
const std::array<int, 8> limits = {3, 3, 3, 3, 1, 1, 1, 11};
|
||||
const std::array<std::array<int, 4>, 7> areas = {{{19, 9, 104, 62},
|
||||
{61, 108, 50, 34},
|
||||
{63, 84, 54, 30},
|
||||
{26, 95, 24, 24},
|
||||
{168, 53, 71, 34},
|
||||
{137, 10, 58, 37},
|
||||
{205, 10, 58, 37}}};
|
||||
std::string body =
|
||||
"<div class=\"tactics-board\"><div class=\"tactics-surface\"></div>" +
|
||||
picture(renderer_.diagram("tactics", tactics_diagram(t, theme_)), 2300,
|
||||
1864);
|
||||
for (size_t i = 0; i < 7; ++i) {
|
||||
const auto a = areas[i];
|
||||
body += btn("tactic-cycle", "", "tactic-hotspot",
|
||||
"id=\"tactic-cycle-" + std::to_string(i) + "\" data-id=\"" +
|
||||
std::to_string(i) + "\" aria-label=\"" + esc(names[i]) +
|
||||
"\" style=\"position:absolute;" +
|
||||
rect(a[0] / 295.0 * 2300, a[1] / 206.0 * 1864,
|
||||
a[2] / 295.0 * 2300, a[3] / 206.0 * 1864) +
|
||||
"z-index:" + std::to_string(8 - i) + ";\"");
|
||||
}
|
||||
const int effort = t["values"][7].get<int>();
|
||||
body +=
|
||||
"<div class=\"tactic-effort\"><div "
|
||||
"class=\"tactic-effort-label\"><span>EINSATZ</span><span "
|
||||
"id=\"tactic-effort-value\" class=\"tactic-effort-marks\">" +
|
||||
std::string(size_t(effort), '!') +
|
||||
"</span></div><input type=\"range\" id=\"tactic-effort\" "
|
||||
"data-input=\"tactic-effort\" min=\"0\" max=\"11\" step=\"1\" value=\"" +
|
||||
std::to_string(effort) +
|
||||
"\"/></div><p id=\"tactic-hint\" class=\"tactic-hint\">" +
|
||||
esc(names[size_t(last_tactic_)]) + ": " +
|
||||
std::to_string(t["values"][size_t(last_tactic_)].get<int>()) + " / " +
|
||||
std::to_string(limits[size_t(last_tactic_)]) + "</p></div>";
|
||||
return body;
|
||||
}
|
||||
+76
-2
@@ -5,6 +5,7 @@
|
||||
#include "launch/rml_path.hpp"
|
||||
#include "launch/rml_video.hpp"
|
||||
#include "modern/ui/controls.hpp"
|
||||
#include "modern/ui/demo_sports.hpp"
|
||||
#include "modern/ui/desktop.hpp"
|
||||
#include "modern/ui/gui_assets.hpp"
|
||||
#include "modern/ui/model.hpp"
|
||||
@@ -24,6 +25,7 @@
|
||||
#include <functional>
|
||||
#include <iomanip>
|
||||
#include <nfd.hpp>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
@@ -129,11 +131,29 @@ class Renderer final : public rml::Renderer {
|
||||
};
|
||||
std::map<std::string, Entry> entries_;
|
||||
std::map<std::string, std::string> keys_;
|
||||
std::map<std::string, std::string> loaded_sources_;
|
||||
size_t serial_ = 0;
|
||||
|
||||
public:
|
||||
Renderer(rml::Video &video, GuiAssets &gui, fs::path assets)
|
||||
: rml::Renderer(video), gui_(gui), assets_(std::move(assets)) {}
|
||||
Picture diagram(const std::string &slot, const std::string &svg,
|
||||
int pixels = 2400) {
|
||||
const auto token = "modern-dynamic-" + slot + ".png",
|
||||
digest = content::sha256(svg);
|
||||
auto found = entries_.find(token);
|
||||
if (found == entries_.end() || found->second.key != digest) {
|
||||
auto raster = editor::images::decode(svg, ".svg", pixels);
|
||||
if (const auto loaded = loaded_sources_.find(token);
|
||||
loaded != loaded_sources_.end()) {
|
||||
Rml::ReleaseTexture(loaded->second, this);
|
||||
loaded_sources_.erase(loaded);
|
||||
}
|
||||
entries_[token] = {digest, false, pixels, std::move(raster)};
|
||||
}
|
||||
const auto &r = entries_.at(token).raster;
|
||||
return {token, r.width, r.height};
|
||||
}
|
||||
Picture local_image(const fs::path &file, int pixels) {
|
||||
const auto identity =
|
||||
editor::utf8(file) + ":" +
|
||||
@@ -155,6 +175,7 @@ public:
|
||||
Rml::ReleaseTextures(this);
|
||||
entries_.clear();
|
||||
keys_.clear();
|
||||
loaded_sources_.clear();
|
||||
world_ = std::move(value);
|
||||
}
|
||||
Picture image(const std::string &key, bool world = false, int pixels = 512) {
|
||||
@@ -239,6 +260,7 @@ public:
|
||||
auto found = entries_.find(token);
|
||||
if (found == entries_.end())
|
||||
return rml::Renderer::LoadTexture(dimensions, source);
|
||||
loaded_sources_[token] = source;
|
||||
auto pixels = found->second.raster.pixels;
|
||||
for (size_t i = 0; i < pixels.size(); i += 4)
|
||||
for (int c = 0; c < 3; ++c)
|
||||
@@ -278,6 +300,9 @@ class App final : public Rml::EventListener {
|
||||
bool focus_ticket_ = false;
|
||||
std::set<std::string> pending_selects_;
|
||||
std::string focus_after_render_;
|
||||
bool keyboard_navigation_ = false;
|
||||
std::string last_rendered_menu_;
|
||||
std::optional<float> training_scroll_;
|
||||
float device_ratio_ = 1.f;
|
||||
Json desktop_ = Json::object(), saved_desktop_ = Json::object(),
|
||||
options_ = Json::object();
|
||||
@@ -305,6 +330,7 @@ class App final : public Rml::EventListener {
|
||||
std::string context_id_;
|
||||
int context_x_ = 0, context_y_ = 0;
|
||||
int opening_page_ = 0, draw_revealed_ = 0;
|
||||
int last_tactic_ = 0;
|
||||
bool draw_playing_ = true;
|
||||
uint64_t draw_at_ = 0;
|
||||
std::string settings_category_ = "display";
|
||||
@@ -336,7 +362,10 @@ class App final : public Rml::EventListener {
|
||||
}
|
||||
std::string img(const std::string &key, double w, double h,
|
||||
bool world = false, bool cover = false, int pixels = 512) {
|
||||
const auto p = renderer_.image(key, world, pixels);
|
||||
return picture(renderer_.image(key, world, pixels), w, h, cover);
|
||||
}
|
||||
std::string picture(const Picture &p, double w, double h,
|
||||
bool cover = false) {
|
||||
if (p.token.empty())
|
||||
return "<div style=\"width:" + scalar(w) + "dp;height:" + scalar(h) +
|
||||
"dp;\"></div>";
|
||||
@@ -398,6 +427,14 @@ class App final : public Rml::EventListener {
|
||||
bool apply(const std::string &kind, const Json &value) {
|
||||
const auto r = source_->apply({source_->snapshot().revision, kind, value});
|
||||
if (!r.ok) {
|
||||
if (kind == "training" && r.error == "training_preview_budget") {
|
||||
const int limit = value.value("group", "") == "type" ? 8
|
||||
: value.value("index", 0) < 2 ? 4
|
||||
: 18;
|
||||
message("Für diesen Trainingsbereich stehen höchstens " +
|
||||
std::to_string(limit) + " Bälle zur Verfügung.");
|
||||
return false;
|
||||
}
|
||||
message("Die Eingabe konnte nicht übernommen werden: " + r.error);
|
||||
return false;
|
||||
}
|
||||
@@ -559,6 +596,11 @@ class App final : public Rml::EventListener {
|
||||
std::string widget(const std::string &, const Json &);
|
||||
std::string render_table(bool strengths = false, bool indoor = false);
|
||||
std::string render_camp();
|
||||
std::string render_training();
|
||||
std::string render_tactics();
|
||||
std::string training_balls(const std::string &, int, int, bool,
|
||||
const std::string &player = {}, int size = 56,
|
||||
int gap = 10, bool overtraining = false);
|
||||
std::string render_settings();
|
||||
std::string render_save(bool save);
|
||||
std::string render_desktop_tools(const std::string &);
|
||||
@@ -605,6 +647,10 @@ class App final : public Rml::EventListener {
|
||||
dirty_ = true;
|
||||
}
|
||||
void render() {
|
||||
training_scroll_.reset();
|
||||
if (menu_ == "training" && last_rendered_menu_ == "training")
|
||||
if (auto *roster = element("training-roster"))
|
||||
training_scroll_ = roster->GetScrollTop();
|
||||
if (ticket_)
|
||||
ticket_->close(false);
|
||||
context_open_ = false;
|
||||
@@ -638,6 +684,7 @@ class App final : public Rml::EventListener {
|
||||
? "saturate(0.13) brightness(0.56)"
|
||||
: "saturate(0.34) brightness(0.70)");
|
||||
needs_render_ = false;
|
||||
last_rendered_menu_ = menu_;
|
||||
dirty_ = true;
|
||||
if (focus_ticket_) {
|
||||
if (auto *n = element("desktop-ticket"))
|
||||
@@ -646,7 +693,7 @@ class App final : public Rml::EventListener {
|
||||
}
|
||||
if (!focus_after_render_.empty()) {
|
||||
if (auto *node = element(focus_after_render_))
|
||||
node->Focus(true);
|
||||
node->Focus(keyboard_navigation_);
|
||||
focus_after_render_.clear();
|
||||
}
|
||||
}
|
||||
@@ -691,6 +738,11 @@ class App final : public Rml::EventListener {
|
||||
context_->Update();
|
||||
if (center_selects(*doc_))
|
||||
context_->Update();
|
||||
if (training_scroll_) {
|
||||
if (auto *roster = element("training-roster"))
|
||||
roster->SetScrollTop(*training_scroll_);
|
||||
training_scroll_.reset();
|
||||
}
|
||||
renderer_.Clear();
|
||||
renderer_.BeginFrame();
|
||||
context_->Render();
|
||||
@@ -750,6 +802,27 @@ class App final : public Rml::EventListener {
|
||||
bold = Rml::LoadFontFace(rml::asset_path(assets_ / "LatoLatin-Bold.ttf"),
|
||||
"Modern", Rml::Style::FontStyle::Normal,
|
||||
Rml::Style::FontWeight::Bold);
|
||||
std::vector<fs::path> chalk_fonts;
|
||||
#ifdef _WIN32
|
||||
if (const char *windir = std::getenv("WINDIR")) {
|
||||
chalk_fonts.push_back(fs::path(windir) / "Fonts" / "comic.ttf");
|
||||
chalk_fonts.push_back(fs::path(windir) / "Fonts" / "times.ttf");
|
||||
}
|
||||
#elif defined(__APPLE__)
|
||||
chalk_fonts = {"/System/Library/Fonts/Supplemental/Comic Sans MS.ttf",
|
||||
"/System/Library/Fonts/Supplemental/Apple Chancery.ttf",
|
||||
"/System/Library/Fonts/Times.ttc"};
|
||||
#else
|
||||
chalk_fonts = {
|
||||
"/usr/share/fonts/truetype/liberation2/LiberationSerif-Regular.ttf",
|
||||
"/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf"};
|
||||
#endif
|
||||
chalk_fonts.push_back(assets_ / "LatoLatin-Regular.ttf");
|
||||
for (const auto &font : chalk_fonts)
|
||||
if (fs::exists(font) && Rml::LoadFontFace(rml::asset_path(font), "Chalk",
|
||||
Rml::Style::FontStyle::Normal,
|
||||
Rml::Style::FontWeight::Normal))
|
||||
break;
|
||||
if (!regular || !bold)
|
||||
throw std::runtime_error(
|
||||
"GUI-Schriften fehlen. Bitte das vollständige Paket entpacken.");
|
||||
@@ -906,6 +979,7 @@ public:
|
||||
#include "modern/ui/controls.inc"
|
||||
#include "modern/ui/desktop_screens.inc"
|
||||
#include "modern/ui/screens.inc"
|
||||
#include "modern/ui/sports_screens.inc"
|
||||
} // namespace
|
||||
bool is_modern_world(const fs::path &file) {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user