- Splash: Summit's icon rises in over an amber glow, the name follows, then it fades into the app (about a second, no keys taken, off when motion is off) - install.sh: after installing icons, rebuild KDE's sycoca and send KIconLoader.iconChanged so a running Plasma shows the updated icon - New app icon (SVG + rendered PNG sizes, render.sh), used by the packages and the engine's window - Lutris scan, consoles (Remote Play via chiaki-ng frame tap, capture cards), Steam account switching, running-game tracking, window mode, artwork - Library UI: add dialog, profile editor, accounts, "now playing" screen - Engine: in-game overlay/HUD, overlay key, global key fixes Co-Authored-By: Claude Opus 5.5 <[email protected]>
237 lines
11 KiB
Python
237 lines
11 KiB
Python
"""Drawing for everything Summit puts over the picture: the overlay (framegen/menu.py), the performance stats and
|
|
messages (framegen/hud.py) and the watermark. Real fonts and anti-aliased rounded shapes with Pillow, handed to the
|
|
window as numpy images (it uploads them as textures and blends them over the game).
|
|
|
|
Colours are RGB and match the launcher's default theme (crates/summit-gui/ui/theme.slint, "Midnight"). Sizes are in
|
|
pixels of the image; callers scale them with the picture (`ui`, 1.0 at 1080p).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import functools
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
import numpy as np
|
|
|
|
try:
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
except ImportError as e: # pragma: no cover - a missing package, said plainly
|
|
raise RuntimeError("Summit's overlay needs the Pillow package: pip install Pillow (or run install.sh again, "
|
|
"which adds it to the engine's environment)") from e
|
|
|
|
BG = (13, 16, 23)
|
|
SURFACE = (22, 27, 37)
|
|
RAISED = (33, 40, 55)
|
|
HILITE = (52, 61, 82) # the picked choice of a row that isn't the selected one
|
|
BORDER = (44, 52, 69)
|
|
TEXT = (238, 241, 247)
|
|
DIM = (160, 169, 187)
|
|
FAINT = (104, 113, 132)
|
|
OFF = (72, 80, 98) # a choice that can't be picked
|
|
ACCENT = (255, 176, 46)
|
|
ACCENT2 = (255, 122, 61)
|
|
ON_ACCENT = (28, 19, 0)
|
|
GOOD = (74, 222, 128)
|
|
TIGHT = (250, 204, 21)
|
|
BAD = (248, 113, 113)
|
|
# how well the PC keeps up (io.MiniText.health)
|
|
HEALTH = {"good": GOOD, "tight": TIGHT, "behind": BAD, "off": FAINT, "": FAINT}
|
|
|
|
WEIGHTS = ("regular", "medium", "semibold", "bold")
|
|
SS = 4 # shapes are drawn this many times bigger, then shrunk: that's their anti-aliasing
|
|
|
|
# Looked for when fontconfig isn't there to ask (or finds nothing)
|
|
_FALLBACK_FONTS = {
|
|
"regular": ("/usr/share/fonts/noto/NotoSans-Regular.ttf", "/usr/share/fonts/truetype/noto/NotoSans-Regular.ttf",
|
|
"/usr/share/fonts/TTF/DejaVuSans.ttf", "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
|
|
"/usr/share/fonts/dejavu-sans-fonts/DejaVuSans.ttf", "/usr/share/fonts/liberation/LiberationSans-Regular.ttf"),
|
|
"bold": ("/usr/share/fonts/noto/NotoSans-Bold.ttf", "/usr/share/fonts/truetype/noto/NotoSans-Bold.ttf",
|
|
"/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
|
|
"/usr/share/fonts/dejavu-sans-fonts/DejaVuSans-Bold.ttf", "/usr/share/fonts/liberation/LiberationSans-Bold.ttf"),
|
|
}
|
|
_WINDOWS_FONTS = {"regular": "segoeui.ttf", "medium": "seguisb.ttf", "semibold": "seguisb.ttf", "bold": "segoeuib.ttf"}
|
|
|
|
|
|
@functools.lru_cache(maxsize=None)
|
|
def font_file(weight: str = "regular") -> str | None:
|
|
"""The file of the system's sans-serif font in this weight (SUMMIT_UI_FONT names another family), or None when
|
|
none is found: Pillow's own font is used then."""
|
|
family = os.environ.get("SUMMIT_UI_FONT", "").strip()
|
|
if sys.platform == "win32":
|
|
path = os.path.join(os.environ.get("WINDIR", r"C:\Windows"), "Fonts", _WINDOWS_FONTS[weight])
|
|
return path if os.path.isfile(path) else None
|
|
try:
|
|
out = subprocess.run(["fc-match", "-f", "%{file}", f"{family or 'sans-serif'}:{weight}"], capture_output=True,
|
|
text=True, timeout=3).stdout.strip()
|
|
if out and os.path.isfile(out) and out.lower().endswith((".ttf", ".otf", ".ttc")):
|
|
return out
|
|
except (OSError, subprocess.SubprocessError):
|
|
pass
|
|
heavy = "bold" if weight in ("semibold", "bold") else "regular"
|
|
return next((p for p in _FALLBACK_FONTS[heavy] if os.path.isfile(p)), None)
|
|
|
|
|
|
@functools.lru_cache(maxsize=256)
|
|
def font(size: int, weight: str = "regular"):
|
|
size = max(6, int(size))
|
|
path = font_file(weight)
|
|
if path:
|
|
try:
|
|
return ImageFont.truetype(path, size)
|
|
except OSError:
|
|
pass
|
|
return ImageFont.load_default(size)
|
|
|
|
|
|
def text_w(s: str, f) -> int:
|
|
return int(round(f.getlength(s))) if s else 0
|
|
|
|
|
|
def wrap(s: str, f, width: int, max_lines: int = 0) -> list[str]:
|
|
"""s as lines no wider than `width`; with max_lines, what doesn't fit ends in an ellipsis."""
|
|
lines, line = [], ""
|
|
for word in s.split():
|
|
trial = f"{line} {word}".strip()
|
|
if line and f.getlength(trial) > width:
|
|
lines.append(line)
|
|
line = word
|
|
else:
|
|
line = trial
|
|
if line:
|
|
lines.append(line)
|
|
if max_lines and len(lines) > max_lines:
|
|
lines = lines[:max_lines]
|
|
lines[-1] = fit(lines[-1] + " ...", f, width, force=True)
|
|
return lines
|
|
|
|
|
|
def fit(s: str, f, width: int, force: bool = False) -> str:
|
|
"""s, cut short with an ellipsis if it's wider than `width` (force: it already stands for cut text)."""
|
|
if f.getlength(s) <= width and not force:
|
|
return s
|
|
s = s.removesuffix(" ...").rstrip()
|
|
while s and f.getlength(s + "...") > width:
|
|
s = s[:-1].rstrip()
|
|
return s + "..."
|
|
|
|
|
|
@functools.lru_cache(maxsize=512)
|
|
def _rr_mask(w: int, h: int, r: int):
|
|
"""An anti-aliased rounded rectangle, as a mask."""
|
|
big = Image.new("L", (w * SS, h * SS), 0)
|
|
ImageDraw.Draw(big).rounded_rectangle((0, 0, w * SS - 1, h * SS - 1), max(0, min(r, w // 2, h // 2)) * SS, fill=255)
|
|
return big.reduce(SS)
|
|
|
|
|
|
class Canvas:
|
|
"""An opaque picture to draw a panel on. rgba() hands it over with rounded corners and a see-through
|
|
background."""
|
|
|
|
def __init__(self, w: int, h: int, bg=BG, radius: int = 0, border=None) -> None:
|
|
self.w, self.h, self.bg, self.radius = int(w), int(h), bg, int(radius)
|
|
self.img = Image.new("RGB", (self.w, self.h), bg)
|
|
self.d = ImageDraw.Draw(self.img)
|
|
if border is not None:
|
|
self.rrect(0, 0, self.w, self.h, self.radius, fill=bg, outline=border)
|
|
|
|
def rrect(self, x0: int, y0: int, x1: int, y1: int, r: int, fill=None, outline=None, width: int = 1) -> None:
|
|
"""A rounded rectangle from (x0, y0) up to, not including, (x1, y1)."""
|
|
x0, y0, x1, y1 = int(x0), int(y0), int(x1), int(y1)
|
|
w, h = x1 - x0, y1 - y0
|
|
if w <= 0 or h <= 0:
|
|
return
|
|
if outline is not None:
|
|
self.img.paste(outline, (x0, y0), _rr_mask(w, h, int(r)))
|
|
if fill is None:
|
|
fill = self.bg
|
|
x0, y0, w, h, r = x0 + width, y0 + width, w - 2 * width, h - 2 * width, r - width
|
|
if w <= 0 or h <= 0:
|
|
return
|
|
if fill is not None:
|
|
self.img.paste(fill, (x0, y0), _rr_mask(w, h, max(0, int(r))))
|
|
|
|
def circle(self, cx: int, cy: int, r: int, fill) -> None:
|
|
self.rrect(cx - r, cy - r, cx + r, cy + r, r, fill=fill)
|
|
|
|
def poly(self, points, fill) -> None:
|
|
"""An anti-aliased filled polygon; points are (x, y) pairs, fractions allowed."""
|
|
xs, ys = [p[0] for p in points], [p[1] for p in points]
|
|
x0, y0 = int(np.floor(min(xs))), int(np.floor(min(ys)))
|
|
w, h = int(np.ceil(max(xs))) - x0 + 1, int(np.ceil(max(ys))) - y0 + 1
|
|
big = Image.new("L", (w * SS, h * SS), 0)
|
|
ImageDraw.Draw(big).polygon([((x - x0) * SS, (y - y0) * SS) for x, y in points], fill=255)
|
|
self.img.paste(fill, (x0, y0), big.reduce(SS))
|
|
|
|
def chevron(self, cx: int, cy: int, size: int, direction: str, fill, weight: float = 0.3) -> None:
|
|
"""A small arrowhead (< > ^ v) centred on (cx, cy), `size` tall (or wide, for up and down)."""
|
|
a, t = size / 2, max(1.0, size * weight)
|
|
left = [(cx + a / 2, cy - a), (cx + a / 2 - t, cy - a), (cx - a / 2 - t / 2, cy), (cx + a / 2 - t, cy + a),
|
|
(cx + a / 2, cy + a), (cx - a / 2 + t / 2, cy)]
|
|
if direction == "right":
|
|
pts = [(2 * cx - x, y) for x, y in left]
|
|
elif direction == "up":
|
|
pts = [(cx + (y - cy), cy + (x - cx)) for x, y in left]
|
|
elif direction == "down":
|
|
pts = [(cx + (y - cy), cy - (x - cx)) for x, y in left]
|
|
else:
|
|
pts = left
|
|
self.poly(pts, fill)
|
|
|
|
def line(self, x0: int, y0: int, x1: int, y1: int, fill, width: int = 1) -> None:
|
|
self.d.line((x0, y0, x1, y1), fill=fill, width=width)
|
|
|
|
def text(self, x: int, y: int, s: str, f, fill=TEXT, anchor: str = "lm") -> int:
|
|
"""Draws s; anchor as Pillow's (left/middle/right + top/middle/baseline/bottom). Returns its width."""
|
|
if not s:
|
|
return 0
|
|
self.d.text((x, y), s, font=f, fill=fill, anchor=anchor)
|
|
return text_w(s, f)
|
|
|
|
def spaced(self, x: int, y: int, s: str, f, fill=TEXT, gap: float = 0.0) -> int:
|
|
"""s with extra room between its letters (a wordmark), left-middle at (x, y). Returns its width."""
|
|
x0 = x
|
|
for ch in s:
|
|
self.d.text((x, y), ch, font=f, fill=fill, anchor="lm")
|
|
x += f.getlength(ch) + gap
|
|
return int(round(x - gap - x0))
|
|
|
|
def logo(self, x: int, y: int, size: int) -> None:
|
|
"""Summit's peak mark (the launcher's LogoMark), size x size with its top-left at (x, y)."""
|
|
s = size / 24.0
|
|
at = lambda pts: [(x + px * s, y + py * s) for px, py in pts] # noqa: E731
|
|
self.poly(at([(9, 21), (16.5, 8.5), (23.5, 21)]), (150, 96, 44))
|
|
self.poly(at([(0.5, 21), (9.5, 3.5), (18.5, 21)]), ACCENT)
|
|
self.poly(at([(9.5, 3.5), (12.6, 9.6), (10.6, 8.6), (9.3, 10.6), (8, 8.8), (6.5, 9.4)]), (255, 250, 240))
|
|
|
|
def rgb(self) -> np.ndarray:
|
|
return np.asarray(self.img)
|
|
|
|
def rgba(self, opacity: float = 1.0, backgrounds=()) -> np.ndarray:
|
|
"""The picture as RGBA: outside the rounded corners it's clear, the background (and the other colours in
|
|
`backgrounds`, such as a card's) lets `1 - opacity` of the game through, and text and controls stay solid,
|
|
with the anti-aliased edges in between."""
|
|
rgb = np.asarray(self.img)
|
|
out = np.empty((self.h, self.w, 4), np.uint8)
|
|
out[..., :3] = rgb
|
|
if opacity >= 0.999:
|
|
alpha = np.full((self.h, self.w), 255, np.uint8)
|
|
else: # how far each pixel is from the nearest background colour, through small tables (fast)
|
|
steps = np.arange(256)
|
|
dist = None
|
|
for colour in (self.bg, *backgrounds):
|
|
d = None
|
|
for ch in range(3):
|
|
one = np.abs(steps - colour[ch]).astype(np.uint8)[rgb[..., ch]]
|
|
d = one if d is None else np.maximum(d, one)
|
|
dist = d if dist is None else np.minimum(dist, d)
|
|
alpha = ((opacity + (1.0 - opacity) * np.clip(steps / 60.0, 0.0, 1.0)) * 255.0).astype(np.uint8)[dist]
|
|
r = min(self.radius, self.w // 2, self.h // 2)
|
|
if r > 0: # the mask is solid but for its corners
|
|
mask = np.asarray(_rr_mask(self.w, self.h, self.radius))
|
|
for ys in (slice(0, r), slice(self.h - r, self.h)):
|
|
for xs in (slice(0, r), slice(self.w - r, self.w)):
|
|
alpha[ys, xs] = (alpha[ys, xs].astype(np.uint16) * mask[ys, xs] // 255).astype(np.uint8)
|
|
out[..., 3] = alpha
|
|
return out
|