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

95 lines
6.9 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
// The pages of the notebook: what all of them are built from. A page is 1244 x 720 design
// units (1 unit = 2.25 dp) under the heading of the sheet; its blocks are styled in pages.rcss.
// The menus that are laid out as a page of the notebook. Every other career menu is still a
// sheet of the old format, shown scaled inside the book.
bool App::page_native() const {
static const std::set<std::string> native{"achievements", "highscore", "balance", "contracts", "environment", "tables", "league", "other", "strengths", "fairplay", "indoor", "scorers"};
return notebook_page() && native.contains(menu_);
}
// A length of the page design in dp.
std::string App::pg(double units) { return scalar(units * 2.25) + "dp"; }
// Inline styles written in design units: every "<number>px" becomes dp.
std::string App::pg_css(std::string_view css) {
std::string out;
for (size_t i = 0; i < css.size();) {
const bool starts = (std::isdigit(static_cast<unsigned char>(css[i])) || (css[i] == '.' && i + 1 < css.size() && std::isdigit(static_cast<unsigned char>(css[i + 1])))) &&
(i == 0 || !(std::isalnum(static_cast<unsigned char>(css[i - 1])) || css[i - 1] == '#' || css[i - 1] == '.'));
if (!starts) { out += css[i++]; continue; }
size_t end = i;
while (end < css.size() && (std::isdigit(static_cast<unsigned char>(css[end])) || css[end] == '.')) ++end;
if (css.substr(end, 2) == "px") { out += pg(std::stod(std::string(css.substr(i, end - i)))); i = end + 2; }
else { out.append(css.substr(i, end - i)); i = end; }
}
return out;
}
std::string App::pg_upper(const std::string &text) { return notebook_upper(text); }
std::string App::pg_kicker(const std::string &text, const std::string &css) {
return "<span class=\"pg-k\"" + (css.empty() ? std::string() : " style=\"" + pg_css(css) + "\"") + ">" + esc(pg_upper(text)) + "</span>";
}
std::string App::pg_stat(const std::string &label, const std::string &value, const std::string &sub) {
return "<div class=\"pg-stat\">" + pg_kicker(label) + "<strong>" + value + "</strong>" + (sub.empty() ? std::string() : "<small>" + sub + "</small>") + "</div>";
}
std::string App::pg_bar(double percent, const std::string &kind) {
return "<div class=\"pg-bar\"><div class=\"" + kind + "\" style=\"width:" + scalar(std::clamp(percent, 0.0, 100.0)) + "%;\"></div></div>";
}
std::string App::pg_dots(int on, int of) {
std::string out = "<span class=\"pg-dots\">";
for (int i = 0; i < of; ++i) out += i < on ? "<span class=\"on\"></span>" : "<span></span>";
return out + "</span>";
}
// The four lines of a team as the page writes them.
std::string App::pg_pos(const std::string &code) {
const std::string key = code == "TW" || code == "T" ? "TW" : code == "ABW" || code == "A" || code == "AB" ? "ABW" : code == "MIT" || code == "M" || code == "MF" ? "MIT" : code == "STU" || code == "S" || code == "ST" ? "STU" : "";
return "<span class=\"pg-pos " + key + "\">" + esc(key.empty() ? code : key) + "</span>";
}
// The last matches as marks: S won, U drawn, N lost, anything else not played.
std::string App::pg_form(const std::string &marks) {
std::string out = "<span class=\"pg-form\" title=\"" + esc(marks) + "\">";
for (const char c : marks) out += c == 'S' || c == 'U' || c == 'N' ? std::string("<span class=\"") + c + "\">" + c + "</span>" : std::string("<span class=\"X\"></span>");
return out + "</span>";
}
std::string App::pg_status(const std::string &text) { return text.empty() ? std::string() : "<span class=\"pg-status\">" + esc(text) + "</span>"; }
std::string App::pg_button(const std::string &action, const std::string &label, const std::string &css, const std::string &attrs) {
// "primary" is the page's own filled button, not the one of the old windows
std::string classes = css;
if (const auto at = classes.find("primary"); at != std::string::npos) classes.replace(at, 7, "pg-go");
return btn(action, label, "pg-btn " + classes, attrs);
}
// A cell of a table head (a button when the column sorts) and of a row; a width of 0 takes
// the room that is left. `align` is "", "r" or "c".
std::string App::pg_th(const std::string &label, double width, const std::string &align, const std::string &sort_action, const std::string &attrs, int sorted) {
const std::string css = "pg-th " + align + (width > 0 ? "" : " flex"), style = width > 0 ? " style=\"width:" + pg(width) + ";\"" : "";
const std::string text = "<span>" + esc(pg_upper(label)) + "</span>" + (sorted ? std::string("<span class=\"pg-sort ") + (sorted > 0 ? "up" : "down") + "\"></span>" : std::string());
if (sort_action.empty()) return "<span class=\"" + css + "\"" + style + ">" + text + "</span>";
return btn(sort_action, "<span class=\"pg-th-text\">" + text + "</span>", css, attrs + style);
}
std::string App::pg_td(const std::string &html, double width, const std::string &align, bool cell) {
return "<div class=\"pg-td " + align + (cell ? " cell" : "") + (width > 0 ? "" : " flex") + "\"" + (width > 0 ? " style=\"width:" + pg(width) + ";\"" : "") + ">" + html + "</div>";
}
// A dialog: a note on the dimmed book. `actions` are its buttons, right-aligned.
std::string App::pg_dialog(const std::string &title, const std::string &sub, const std::string &body, const std::string &actions, double width, const std::string &attrs) {
return "<div class=\"pg-ov\" " + attrs + "><div class=\"pg-dlg\" role=\"dialog\" aria-modal=\"true\" style=\"width:" + pg(width) + ";\">" +
(title.empty() ? std::string() : "<div><h2>" + esc(title) + "</h2>" + (sub.empty() ? std::string() : "<span class=\"pg-dlg-sub\">" + sub + "</span>") + "</div>") + body +
(actions.empty() ? std::string() : "<div class=\"pg-dlg-act\">" + actions + "</div>") + "</div></div>";
}
// A club as a link to its card: crest and name (the name first when `reverse`).
std::string App::pg_club(const std::string &id, double size, bool reverse) {
const auto *c = world_->find("clubs", id);
const auto live = engine_mode_ ? engine_report_team(id) : Json();
if ((engine_mode_ && live.is_null()) || (!engine_mode_ && !c)) return "<span>—</span>";
const auto name = esc(engine_mode_ ? live.at("name").get<std::string>() : world_->name(*c));
return btn("club-detail", crest(c, size) + "<span title=\"" + name + "\">" + name + "</span>", std::string("pg-link") + (reverse ? " reverse" : ""),
"id=\"report-club-" + esc(id) + "\" data-id=\"" + esc(id) + "\"");
}
// The heading of the sheet is written by the book before the page is; the page may have
// changed its title or added a hint since.
void App::notebook_finish(std::string &markup) {
for (const auto &[token, value] : {std::pair<std::string, std::string>{"\x02T\x02", esc(page_title_)}, {"\x02H\x02", page_hint_.empty() ? std::string() : " · " + esc(pg_upper(page_hint_))}})
for (size_t at = markup.find(token); at != std::string::npos; at = markup.find(token, at)) markup.replace(at, token.size(), value);
}