Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

Centre text in badges, tabs and keys by the font's cap height

Rafael Abud Ferreira5a648edfb0ac
diff --git a/src/app/cli_main.cpp b/src/app/cli_main.cpp
index 3cd9636..eb8a8bc 100644
--- a/src/app/cli_main.cpp
+++ b/src/app/cli_main.cpp
@@ -545,6 +545,42 @@ int main(int argc, char** argv) {
}
int seconds = pos.size() > 1 ? std::atoi(pos[1]) : 30;
rc = cmdStream(am, pos[0], seconds > 0 ? seconds : 30, pos.size() > 2 ? pos[2] : nullptr, res, opts, region);
+ } else if (cmd == "ui-badges" && argi < argc) {
+ // Offline: cards with every badge, to check their layout.
+ ui::Fonts fonts;
+ if (!fonts.load("assets/fonts")) return 1;
+ ui::ImageCache images([] {});
+ ui::AppUi app(fonts, images);
+ ui::Canvas canvas(1920, 1080);
+ std::vector<ui::GameTile> owned, buy;
+ const char* platforms[] = {"XS", "ONE", "360"};
+ for (int i = 0; i < 6; ++i) {
+ ui::GameTile t;
+ t.productId = "OWNED" + std::to_string(i);
+ t.titleId = t.productId;
+ t.name = "Owned game " + std::to_string(i);
+ t.platform = platforms[i % 3];
+ owned.push_back(t);
+ t.productId = "BUY" + std::to_string(i);
+ t.titleId = t.productId;
+ t.name = "Game to buy " + std::to_string(i);
+ t.playable = false;
+ t.purchasable = true;
+ buy.push_back(t);
+ }
+ app.setOwned(owned, buy, true);
+ app.setPrices({{"BUY0", {"R$ 56,98", "R$ 284,90"}}, {"BUY1", {"R$ 199,99", ""}}, {"BUY2", {"GR\xC3\x81TIS", ""}},
+ {"BUY3", {"R$ 1.299,90", ""}}});
+ app.showHome();
+ ui::NavInput r1;
+ r1.r1 = true;
+ app.handle(r1);
+ app.render(canvas, 1000);
+ app.render(canvas, 2000);
+ std::string path = argv[argi];
+ stbi_write_png(path.c_str(), canvas.width(), canvas.height(), 4, canvas.data(), canvas.width() * 4);
+ std::printf("wrote %s\n", path.c_str());
+ return 0;
} else if (cmd == "ui-preview" && argi < argc) {
rc = cmdUiPreview(am, argv[argi]);
} else if (cmd == "logout") {
diff --git a/src/ui/app_ui.cpp b/src/ui/app_ui.cpp
index 868f25f..b37f6c3 100644
--- a/src/ui/app_ui.cpp
+++ b/src/ui/app_ui.cpp
@@ -921,14 +921,14 @@ void AppUi::drawTabs(Canvas& c) {
auto shoulder = [&](const char* name, int cx) {
Rect r{cx - 22, kY + 8, 44, 28};
c.fillRect(r, rgba(255, 255, 255, 40), 8);
- fonts_.bold.draw(c, name, cx - fonts_.bold.measure(name, 16) / 2, kY + 13, 16, kGray);
+ fonts_.bold.draw(c, name, cx - fonts_.bold.measure(name, 16) / 2, fonts_.bold.centeredY(r.y, r.h, 16), 16, kGray);
};
shoulder("L1", x - 40);
for (int i = 0; i < 2; ++i) {
bool on = static_cast<int>(tab_) == i;
Rect r{x, kY, widths[i], kH};
if (on) c.fillRect(r, kWhite, kH / 2);
- fonts_.semibold.draw(c, labels[i], x + kPad, kY + 10, kPx, on ? kBg : kGray);
+ fonts_.semibold.draw(c, labels[i], x + kPad, fonts_.semibold.centeredY(kY, kH, kPx), kPx, on ? kBg : kGray);
x += widths[i] + kGap;
}
shoulder("R1", x - kGap + 40);
@@ -944,9 +944,15 @@ const char* platformLabel(const std::string& code) {
}
// A shopping bag centred on (cx, cy), about 22x24 px: a game to buy.
-void drawBag(Canvas& c, int cx, int cy, Color color) {
- c.fillRect({cx - 11, cy - 5, 22, 17}, color, 3);
- c.strokeArc(static_cast<float>(cx), static_cast<float>(cy - 5), 6, 2.5f, 3.1416f, 3.1416f, color);
+// A shopping bag (body + handle) centred on (cx, cy); `size` scales it
+// (1 = 22 x 23 px).
+void drawBag(Canvas& c, int cx, int cy, Color color, float size = 1.0f) {
+ float w = 22 * size, h = 17 * size, r = 6 * size;
+ float bodyTop = cy - (h + r) / 2 + r;
+ c.fillRect({static_cast<int>(std::lround(cx - w / 2)), static_cast<int>(std::lround(bodyTop)),
+ static_cast<int>(std::lround(w)), static_cast<int>(std::lround(h))},
+ color, std::max(1, static_cast<int>(3 * size)));
+ c.strokeArc(static_cast<float>(cx), bodyTop, r, std::max(1.5f, 2.5f * size), 3.1416f, 3.1416f, color);
}
} // namespace
@@ -966,18 +972,19 @@ void AppUi::drawCard(Canvas& c, const GameTile& t, int x, int y, bool focused, b
c.fillRect({x, y, kCard, kCard}, rgba(0, 0, 0, 70), 10);
auto price = prices_.find(t.productId);
std::string label = price != prices_.end() ? price->second.first : tr(Str::BuyBadge);
- int w = fonts_.bold.measure(label, 13) + 46;
+ int w = fonts_.bold.measure(label, 13) + 42;
Rect badge{x + 10, y + kCard - 34, w, 24};
c.fillRect(badge, rgba(16, 124, 16, 235), 4);
- drawBag(c, badge.x + 16, badge.y + 11, kWhite);
- fonts_.bold.draw(c, label, badge.x + 34, badge.y + 4, 13, kWhite);
+ drawBag(c, badge.x + 15, badge.y + badge.h / 2, kWhite, 0.62f);
+ fonts_.bold.draw(c, label, badge.x + 30, fonts_.bold.centeredY(badge.y, badge.h, 13), 13, kWhite);
} else if (!t.playable) {
c.fillRect({x, y, kCard, kCard}, rgba(0, 0, 0, 150), 10);
drawLock(c, x + kCard - 34, y + 30);
} else if (gamePassBadge) {
Rect badge{x + 10, y + kCard - 34, 96, 24};
c.fillRect(badge, rgba(0, 0, 0, 220), 4);
- fonts_.bold.draw(c, "GAME PASS", badge.x + (96 - fonts_.bold.measure("GAME PASS", 13)) / 2, badge.y + 4, 13,
+ fonts_.bold.draw(c, "GAME PASS", badge.x + (96 - fonts_.bold.measure("GAME PASS", 13)) / 2,
+ fonts_.bold.centeredY(badge.y, badge.h, 13), 13,
kWhite);
}
// Bottom-right: the console it was made for; one style and width for all.
@@ -985,7 +992,8 @@ void AppUi::drawCard(Canvas& c, const GameTile& t, int x, int y, bool focused, b
constexpr int kBadgeW = 96;
Rect badge{x + kCard - 10 - kBadgeW, y + kCard - 34, kBadgeW, 24};
c.fillRect(badge, rgba(0, 0, 0, 220), 4);
- fonts_.bold.draw(c, p, badge.x + (kBadgeW - fonts_.bold.measure(p, 13)) / 2, badge.y + 4, 13, kWhite);
+ fonts_.bold.draw(c, p, badge.x + (kBadgeW - fonts_.bold.measure(p, 13)) / 2,
+ fonts_.bold.centeredY(badge.y, badge.h, 13), 13, kWhite);
}
if (focused) c.strokeRect({x - 7, y - 7, kCard + 14, kCard + 14}, kWhite, 4, 16);
}
@@ -1108,7 +1116,7 @@ void AppUi::drawSearch(Canvas& c, uint64_t nowMs) {
: std::string(1, static_cast<char>(std::toupper(kKeys[r * kKeyCols + k])));
int px = r == kKeyRows ? 22 : 28;
int w = fonts_.semibold.measure(label, px);
- fonts_.semibold.draw(c, label, key.x + (key.w - w) / 2, key.y + (kKeyH - px) / 2 - 4, px,
+ fonts_.semibold.draw(c, label, key.x + (key.w - w) / 2, fonts_.semibold.centeredY(key.y, kKeyH, px), px,
focused ? kBg : kWhite);
}
}
diff --git a/src/ui/font.cpp b/src/ui/font.cpp
index 76669d8..42529aa 100644
--- a/src/ui/font.cpp
+++ b/src/ui/font.cpp
@@ -36,6 +36,15 @@ bool Font::load(const std::string& path) {
return true;
}
+int Font::centeredY(int boxY, int boxH, int px) const {
+ // Cap height from the "H" glyph's box, in font units.
+ int x0, y0, x1, y1;
+ float capH = stbtt_GetCodepointBox(&impl_->info, 'H', &x0, &y0, &x1, &y1) ? y1 * scale(px) : 0.7f * px;
+ float ascent = impl_->ascent * scale(px); // line top to baseline
+ // Cap top = y + ascent - capH; centre it: (boxH - capH) / 2 below boxY.
+ return static_cast<int>(std::lround(boxY + (boxH - capH) / 2.0f - (ascent - capH)));
+}
+
float Font::scale(int px) const { return stbtt_ScaleForPixelHeight(&impl_->info, static_cast<float>(px)); }
int Font::lineHeight(int px) const {
diff --git a/src/ui/font.h b/src/ui/font.h
index b6844e0..0e72d25 100644
--- a/src/ui/font.h
+++ b/src/ui/font.h
@@ -25,6 +25,9 @@ public:
bool load(const std::string& path);
int lineHeight(int px) const;
+ // The `y` to draw at so that capital letters sit centred in a box of
+ // height `boxH` starting at `boxY` (badges, keys, buttons).
+ int centeredY(int boxY, int boxH, int px) const;
int measure(std::string_view text, int px) const;
// Returns the x after the last glyph.
int draw(Canvas& c, std::string_view text, int x, int y, int px, Color color) const;