Files
Dreierpack-Manager/src/modern/ui/widgets_screens.inc
T

431 lines
21 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: GPL-3.0-only
std::string App::widget_heading(const std::string &name, bool link) {
return "<header class=\"desk-card-heading\"><span>" + esc(name) + "</span>" +
(link ? glyph("arrow-diagonal", 28) : "") + "</header>";
}
std::string App::widget_curve(const std::string &id, const Json &o, int w,
int h) {
const bool compare = id == "comparison-curve",
square = (w == 720 || h == 968);
const int height = square ? 600 : 330, range = height - 76;
const auto accent = theme_ == "silver" ? "#235e78"
: theme_ == "matchday" ? "#f1a99e"
: "#a5e2cf";
const auto other = theme_ == "silver" ? "#a04b30"
: theme_ == "matchday" ? "#91cce8"
: "#f2b286";
const auto muted = theme_ == "silver" ? "#586d80"
: theme_ == "matchday" ? "#b4b5b7"
: "#afbfcd";
std::string svg =
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1000\" height=\"" +
std::to_string(height) + "\" viewBox=\"0 0 1000 " +
std::to_string(height) + "\"><path d=\"M32 38h936M32 " +
scalar(38 + range / 2.) + "h936M32 " + std::to_string(38 + range) +
"h936\" stroke=\"" + muted +
"\" stroke-opacity=\"0.22\" fill=\"none\" stroke-width=\"2\"/>";
const auto series = [&](const Json &values, const char *color, bool dash) {
std::string path, dots;
for (size_t i = 0; i < values.size(); ++i) {
const auto x = 32 + i * 936. / (values.size() - 1),
y = 38 + (values[i].get<int>() - 1) / 17. * range;
path += (i ? "L" : "M") + scalar(x) + " " + scalar(y);
dots += "<circle cx=\"" + scalar(x) + "\" cy=\"" + scalar(y) +
"\" r=\"7\" fill=\"" + color + "\"/>";
}
return "<path d=\"" + path + "\" stroke=\"" + color +
"\" fill=\"none\" stroke-width=\"5\" stroke-linecap=\"round\" "
"stroke-linejoin=\"round\" " +
(dash ? "stroke-dasharray=\"18 12\"" : "") + "/>" + dots;
};
svg += series(
widget_data_.at(id == "opponent-curve" ? "opponent_curve" : "own_curve"),
accent, false);
if (compare)
svg += series(widget_data_.at("opponent_curve"), other, true);
svg += "</svg>";
const int ch = h - (compare ? 180 : 96);
std::string out =
"<div class=\"desk-curve-picture\" style=\"height:" + std::to_string(ch) +
"dp;\">" +
picture(renderer_.diagram("widget-" + id + "-" + std::to_string(w) + "-" +
std::to_string(h),
svg, 1600),
w - 42, ch) +
"<span class=\"curve-start\">Saisonstart</span><span "
"class=\"curve-end\">Heute</span></div>";
if (compare)
out +=
"<div class=\"desk-curve-legend\"><span><i></i><span>" +
esc(world_->name(*world_->find("clubs", widget_data_.at("own")))) +
"</span></span><span class=\"curve-opponent\"><i></i><span>" +
esc(world_->name(*world_->find("clubs", widget_data_.at("opponent")))) +
"</span></span></div>";
(void)o;
return out;
}
std::string App::widget(const std::string &id, const Json &o) {
const auto s = snap();
if (!widget_data_.is_object())
widget_data_ = demo_desktop_widgets(*world_, s.state);
const auto *own = world_->find("clubs", widget_data_.at("own")),
*opponent = world_->find("clubs", widget_data_.at("opponent"));
const int w = o.at("w"), h = o.at("h");
const bool wide = h == 224, compact = w == 472;
const auto heading = [&](const std::string &name, bool link = true) {
return widget_heading(name, link);
};
if(s.state.at("career").value("role","club")=="national"){
if(id=="crest"||id=="opponent-crest")return "<div class=\"desk-logo-surface\">"+img(id=="crest"?"career.national-de":"career.national-fr",std::min(w-48,h-138),std::min(w-48,h-138),false,false,768)+"</div><div class=\"desk-logo-caption\"><small>"+(id=="crest"?std::string("Dein Nationalteam"):"Nächster Gegner")+"</small><strong>"+(id=="crest"?std::string("Deutschland"):"Frankreich")+"</strong></div>";
if(id=="competition")return heading("Nächste Begegnung")+"<div class=\"desk-fixture\"><div>"+img("career.national-de",80,80,false,false,256)+"<span>Deutschland</span></div><div class=\"desk-fixture-centre\"><small>Fiktives Länderspiel</small><strong>19:00</strong><span>Heim · Samstag</span></div><div>"+img("career.national-fr",80,80,false,false,256)+"<span>Frankreich</span></div></div>";
}
if (id == "calendar")
return "<div class=\"desk-calendar-month\"><span>" +
esc(widget_data_.at("month").get<std::string>()) +
"</span></div><div class=\"desk-calendar-body\"><span>" +
esc(widget_data_.at("weekday").get<std::string>()) +
"</span><strong>" +
std::to_string(widget_data_.at("day").get<int>()) +
"</strong><small>" +
(widget_data_.at("matchday") == 0
? "Saisonvorbereitung"
: std::to_string(widget_data_.at("matchday").get<int>()) +
". Spieltag") +
"</small></div><div class=\"desk-calendar-foot\">" +
glyph("calendar", 30) +
"<span>Kalender öffnen</span><span class=\"calendar-arrow\">" +
glyph("arrow-diagonal", 26) + "</span></div>";
if (id == "account")
return heading("Vereinskasse") +
"<div class=\"desk-account-main\"><span>Guthaben</span><strong>" +
cash(s.state.at("cash")) +
"</strong></div><div "
"class=\"desk-account-bottom\"><div><span>Sparbuch</span><strong>" +
cash(s.state.at("deposit")) + "</strong></div>" +
btn("ticket",
glyph("ticket", 36) + "<span><span>Eintritt</span><strong>" +
std::to_string(s.state.at("ticket").get<int>()) + " " +
money_unit() + " &nbsp; ›</strong></span>",
"desk-ticket",
"id=\"account-ticket\" data-interactive=\"true\"") +
"</div>";
if (id == "competition")
return heading("Nächste Begegnung") + "<div class=\"desk-fixture\"><div>" +
crest(own, compact ? 68 : 80) + "<span>" +
esc(own ? own->value("short_name", world_->name(*own)) : "") +
"</span></div><div "
"class=\"desk-fixture-centre\"><small>Freundschaftsspiel</"
"small><strong>19:00</strong><span>Heim · "
"Samstag</span></div><div>" +
crest(opponent, compact ? 68 : 80) + "<span>" +
esc(opponent ? opponent->value("short_name", world_->name(*opponent))
: "") +
"</span></div></div>";
if (id == "portrait")
return img(std::string(manager(s).value("portrait_style",
std::string("photo")) == "painted"
? "manager-painted."
: "manager.") +
std::to_string(manager(s).value("portrait", 0)),
w, h, false, false, 768) +
"<div "
"class=\"desk-portrait-caption\"><small>Manager</small><strong>" +
esc(manager(s).value("name", "")) + "</strong></div>";
if (id == "crest" || id == "opponent-crest") {
const auto *c = id == "crest" ? own : opponent;
return "<div class=\"desk-logo-surface\">" +
crest(c, std::min(int((w - 48) * .78), int((h - 138) * .9))) +
"</div><div class=\"desk-logo-caption\"><small>" +
(id == "crest" ? std::string("Dein Verein") : "Nächster Gegner") +
"</small><strong>" + esc(c ? world_->name(*c) : "") +
"</strong></div>";
}
if (id == "warnings") {
const std::vector<std::vector<int>> groups =
wide ? std::vector<std::vector<int>>{{0}, {1}, {2}, {3, 4},
{5, 6}, {7}, {8}, {9}}
: std::vector<std::vector<int>>{{0}, {1}, {2}, {3}, {4},
{5}, {6}, {7}, {8}, {9}};
std::string out =
heading("Vereinsstatus", false) + "<div class=\"desk-warning-grid\">";
const int cols = wide ? 8 : 5,
cw = (w - (wide ? 56 : 52) - (wide ? 7 * 18 : 4 * 14)) / cols,
rh = wide ? h - 76 : (h - 120) / 2;
for (size_t i = 0; i < groups.size(); ++i) {
std::string label, key;
bool attention = false;
for (int n : groups[i]) {
const auto &v = widget_data_.at("warnings")[size_t(n)];
if (!label.empty()) {
label += " & ";
key += ",";
}
label += v.at("name").get<std::string>();
key += std::to_string(n);
attention = attention || v.at("attention") == true;
}
const auto &v = widget_data_.at("warnings")[size_t(groups[i][0])];
out += btn("widget-warning",
"<span class=\"status-lamp " +
std::string(attention ? "attention" : "okay") + "\">" +
glyph(v.at("icon"), h == 720 ? 42 : 28) + "</span>" +
(wide ? ""
: "<span class=\"warning-caption\">" + esc(label) +
"</span>"),
"desk-warning",
"data-id=\"" + key + "\" data-interactive=\"true\" title=\"" +
esc(label) + "\" aria-label=\"" + esc(label) + ": " +
(attention ? "Handlungsbedarf" : "In Ordnung") +
"\" style=\"left:" +
std::to_string((wide ? 28 : 26) +
(int(i) % cols) * (cw + (wide ? 18 : 14))) +
"dp;top:" + std::to_string(int(i) / cols * (rh + 20)) +
"dp;width:" + std::to_string(cw) +
"dp;height:" + std::to_string(rh) + "dp;\"");
}
return out + "</div>";
}
if (id == "fans")
return heading("Fan-Stimmung") + "<div class=\"desk-mood\">" +
glyph("heart", 36) + "<div><strong>" +
std::to_string(widget_data_.at("fans").get<int>()) +
" <small>%</small></strong><span class=\"mood-track\"><i "
"style=\"width:" +
std::to_string(widget_data_.at("fans").get<int>()) +
"%;\"></i></span></div><span>Zuversichtlich</span></div>";
if (id == "news")
return render_news_widget(o);
if (id == "own-curve" || id == "opponent-curve" || id == "comparison-curve")
return heading(id == "comparison-curve"
? "Tabellenplätze"
: world_->name(*(id == "own-curve" ? own : opponent))) +
widget_curve(id, o, w, h);
if (id == "own-summary" || id == "opponent-summary") {
bool self = id == "own-summary";
return heading(world_->name(*(self ? own : opponent))) +
"<div "
"class=\"desk-summary\"><div><small>Tabellenplatz</small><strong>" +
std::to_string(widget_data_.at(self ? "own_rank" : "opponent_rank")
.get<int>()) +
".</strong></div><div><small>Teamstärke</small><strong>" +
std::to_string(
widget_data_.at(self ? "own_strength" : "opponent_strength")
.get<int>()) +
"</strong></div></div>";
}
if (id == "results") {
std::string out =
heading("Die letzten sechs Spiele") + "<div class=\"desk-form\">";
for (size_t i = 0; i < 6; ++i)
out += "<div><span class=\"form-" +
widget_data_.at("form")[i].get<std::string>() +
"\" style=\"width:" + scalar(wide ? 50 : w * .08) +
"dp;height:" + scalar(wide ? 50 : w * .08) +
"dp;font-size:" + scalar(wide ? 26 : w * .04) + "dp;\"><span>" +
widget_data_.at("form")[i].get<std::string>() +
"</span></span><strong style=\"font-size:" +
scalar(wide ? 29 : w * .035) + "dp;\">" +
widget_data_.at("scores")[i].get<std::string>() +
"</strong></div>";
return out + "</div>" +
(wide ? ""
: "<p class=\"desk-card-note\">3 Siege · 2 Unentschieden · 1 "
"Niederlage</p>");
}
if (id == "duels")
return heading("Direkter Vergleich") + "<div class=\"desk-duels\"><div>" +
crest(own, 75) +
"<strong>2</strong><small>Siege</small></div><div><strong>1</"
"strong><small>Unentschieden</small></div><div>" +
crest(opponent, 75) +
"<strong>3</strong><small>Siege</small></div></div>";
if (id == "trash")
return "<div class=\"desktop-utility-icon\">" + glyph("trash", 100) +
"</div><strong>Papierkorb</strong>";
return {};
}
std::string App::desktop_dialog(const std::string &heading,
const std::string &body,
const std::string &footer, int width,
int height) {
return "<div class=\"fill desktop-modal-layer\" "
"style=\"z-index:100;\"><section class=\"window "
"desktop-window\" style=\"left:" +
std::to_string((3840 - width) / 2) +
"dp;top:" + std::to_string((2160 - height) / 2) +
"dp;width:" + std::to_string(width) +
"dp;height:" + std::to_string(height) +
"dp;\"><header class=\"desktop-window-titlebar\">" +
btn("close", "×", "desktop-window-close",
"id=\"window-close\" aria-label=\"Schließen\"") +
"<h1><span>" + esc(heading) +
"</span></h1></header><div class=\"desktop-window-content\" "
"style=\"width:" +
std::to_string(width - 104) +
"dp;height:" + std::to_string(height - 302) + "dp;\">" + body +
"</div><footer class=\"desktop-window-footer\">" + footer +
"</footer></section></div>";
}
std::string App::render_widget_gallery_items() {
std::string out;
int index = 0;
const auto query = settings_search_key(widget_query_);
for (const auto &d : widget_catalogue()) {
const auto id = d.at("id").get<std::string>();
if (!query.empty() &&
settings_search_key(d.at("label").get<std::string>() + " " +
d.at("description").get<std::string>() + " " +
d.at("group").get<std::string>())
.find(query) == std::string::npos)
continue;
auto preview = desktop_.at("objects").at(id);
const auto span = d.at("spans").at(d.at("size").get<std::string>());
const int w = span[0].get<int>() * 248 - 24,
h = span[1].get<int>() * 248 - 24;
preview["w"] = w;
preview["h"] = h;
const auto factor = std::min(620. / w, 300. / h);
std::string dimensions;
for (const auto &def : widget_definitions())
if (def.id == id) {
auto chosen = widget_choices_.contains(id)
? widget_choices_[id]
: std::pair<int, int>{span[0], span[1]};
for (const auto &[cols, rows] : def.sizes) {
const auto key = std::to_string(cols) + "x" + std::to_string(rows);
dimensions += "<option value=\"" + key + "\" " +
(chosen == std::pair{cols, rows} ? "selected" : "") +
">" + std::to_string(cols) + " × " +
std::to_string(rows) + "</option>";
}
}
auto preview_markup = widget(id, preview);
for (size_t at = 0;
(at = preview_markup.find("<button", at)) != std::string::npos;
at += 40)
preview_markup.insert(at + 7, " disabled tabindex=\"-1\"");
for (size_t at = 0;
(at = preview_markup.find(" id=\"", at)) != std::string::npos;) {
at += 5;
const auto prefix = "preview-" + id + "-";
preview_markup.insert(at, prefix);
at += prefix.size();
}
const bool present = desktop_.at("objects").at(id).at("visible");
out +=
"<article class=\"widget-gallery-item\" data-gallery=\"" + id +
"\" style=\"left:" + std::to_string(6 + index % 3 * 814) +
"dp;top:" + std::to_string(8 + index / 3 * 690) +
"dp;\"><div class=\"gallery-widget-preview\"><div class=\"desktop-card "
"desk-card gallery-preview-card\" data-widget=\"" +
id + "\" data-size=\"" + d.at("size").get<std::string>() +
"\" style=\"width:" + std::to_string(w) +
"dp;height:" + std::to_string(h) + "dp;left:" + scalar((732 - w) / 2.) +
"dp;top:" + scalar((320 - h) / 2.) + "dp;transform:scale(" +
scalar(factor) + ");\">" + preview_markup +
"</div></div><div class=\"gallery-description\"><small>" +
esc(d.at("group").get<std::string>()) + "</small><h2>" +
esc(d.at("label").get<std::string>()) + "</h2><p>" +
esc(d.at("description").get<std::string>()) +
"</p></div><div class=\"gallery-actions\"><select id=\"widget-size-" +
id + "\" data-widget-size=\"" + id + "\">" + dimensions + "</select>" +
btn("widget-add",
(present ? glyph("check", 28) : "") + "<span>" +
(present ? "Auf dem Desktop" : "Hinzufügen") + "</span>",
"widget-add-button",
"data-id=\"" + id + "\" " + (present ? "disabled" : "")) +
"</div></article>";
++index;
}
return index
? "<div class=\"widget-gallery-space\" "
"style=\"position:relative;height:" +
std::to_string(8 + ((index + 2) / 3 - 1) * 690 + 660 + 30) +
"dp;\">" + out + "</div>"
: "<p class=\"widget-gallery-empty\">Keine Widgets gefunden.</p>";
}
std::string App::render_widget_dialog(const std::string &key) {
init_desktop();
widget_data_ = demo_desktop_widgets(*world_, snap().state);
if (key == "widgets") {
widget_restore_scroll_ = true;
return desktop_dialog(
"Widget hinzufügen",
"<div class=\"modern-gallery-toolbar\"><p>Dein Verein. Auf einen "
"Blick.</p><label class=\"modern-search\">" +
glyph("plus", 30) +
"<input type=\"text\" id=\"widget-search\" maxlength=\"80\" "
"value=\"" +
esc(widget_query_) +
"\" placeholder=\"Widget suchen\"/></label></div><div "
"id=\"widget-gallery\" class=\"modern-widget-gallery\" "
"tabindex=\"0\">" +
render_widget_gallery_items() + "</div>",
"<p id=\"widget-feedback\">" + esc(widget_notice_) + "</p>" +
btn("close", "Fertig"));
}
if (key == "background")
return desktop_dialog(
"Hintergrundbild ändern",
"<p class=\"modern-intro\">Ein Ort. Dein Spieltag.</p>" +
btn("widget-background",
img("background", 1972, 820, false, true, 2000) +
"<span><strong>Stadion bei Nacht</strong><small>Auf die "
"aktuelle Desktopgestaltung abgestimmt</small></span><i>" +
glyph("check", 48) + "</i>",
"background-choice", "aria-pressed=\"true\"") +
"<p class=\"background-note\">Weitere Hintergründe folgen.</p>",
"<p>" + esc(widget_notice_) + "</p>" + btn("close", "Fertig"), 2080,
1680);
if (key == "warning") {
std::string label, description;
bool attention = false;
std::istringstream in(widget_warning_);
std::string bit;
while (std::getline(in, bit, ',')) {
const int i = std::stoi(bit);
if (i < 0 || i >= 10)
continue;
const auto &v = widget_data_.at("warnings")[size_t(i)];
if (!label.empty())
label += " & ";
label += v.at("name").get<std::string>();
if (v.at("attention") == true) {
attention = true;
description += v.at("description").get<std::string>() + " ";
}
}
return desktop_dialog("Vereinsstatus",
"<div class=\"desktop-message\"><div "
"class=\"desktop-warning-symbol\">" +
glyph(attention ? "alert" : "check", 80) +
"</div><h2>" + esc(label) + "</h2><p>" +
esc(attention
? description
: "Hier ist derzeit alles in Ordnung.") +
"</p></div>",
"<p></p>" + btn("close", "Fertig"), 1800, 1000);
}
if (key == "quit") {
const bool dirty = source_->dirty();
return desktop_dialog(
"Spiel beenden",
"<div class=\"desktop-message\"><div "
"class=\"desktop-warning-symbol\">" +
glyph("power", 80) + "</div><h2>" +
std::string(dirty ? "Noch nicht gespeichert" : "Spiel beenden?") +
"</h2><p>" +
(dirty ? "Deine Änderungen wurden noch nicht gespeichert. Möchtest "
"du vor dem Beenden speichern?"
: "Möchtest du das Spiel beenden und zum Hauptmenü "
"zurückkehren?") +
"</p></div>",
"<p></p>" + btn("desktop-quit-cancel", "Abbrechen") +
(dirty ? btn("desktop-quit-discard", "Nicht speichern") +
btn("desktop-quit-save", "Speichern", "primary")
: btn("desktop-quit-confirm", "Beenden", "primary")),
1800, 1000);
}
return {};
}