Menu: mouse control, opacity and size; stats overlay: opacity, size, corner, per-graph toggles
This commit is contained in:
1 parent
06bdf44674
commit
9192c98912
6 files changed
+349
-38
No files matched your search
@@ -2,6 +2,15 @@
|
||||
|
||||
## 0.3.8
|
||||
|
||||
**Menu and stats overlay**
|
||||
- **See-through menu and stats.** New Display settings: **Menu opacity** and **Stats overlay opacity** (30% to 100%).
|
||||
Text and graph lines stay solid; only the background lets the game show through.
|
||||
- **Mouse in the in-video menu.** Right-click the video to open it. Left-click a setting for the next choice,
|
||||
right-click for the one before, click a section to open it, **< Back** and **Close** buttons, the wheel moves the
|
||||
highlight, hovering highlights a row, and clicking outside closes it. The keys still work.
|
||||
- **More controls:** Menu size, Stats overlay size, Stats overlay corner, and on/off for each graph (frametime,
|
||||
pipeline, delay, graphics card).
|
||||
|
||||
**Stats overlay**
|
||||
- **A Graphs style, like MangoHud's.** Press S to cycle Off, Minimal, Detailed, Graphs (or pick it in Settings). Live
|
||||
scrolling graphs of the time between frames on screen (with 1% lows), the pipeline's time per tick against its
|
||||
|
||||
+169
-32
@@ -381,10 +381,35 @@ class _Panel:
|
||||
def __init__(self, GL, hdr: bool = False) -> None:
|
||||
self.GL, self.w, self.h, self.hdr = GL, 0, 0, hdr
|
||||
self.tex, self.fbo = GL.glGenTextures(1), GL.glGenFramebuffers(1)
|
||||
self.blend = False # drawn see-through (update(opacity < 1)), by a shader, instead of copied
|
||||
|
||||
def update(self, img: np.ndarray) -> None:
|
||||
@staticmethod
|
||||
def with_alpha(img: np.ndarray, bgs, opacity: float) -> np.ndarray:
|
||||
"""img as BGRA: pixels at (or near) a background colour of `bgs` get alpha `opacity`, text and lines stay solid,
|
||||
and the anti-aliased edges in between get alpha in between."""
|
||||
d = np.min([np.abs(img.astype(np.int16) - np.array(b, np.int16)).max(axis=2) for b in bgs], axis=0)
|
||||
cover = np.clip(d / 70.0, 0.0, 1.0)
|
||||
alpha = (opacity + (1.0 - opacity) * cover) * 255.0
|
||||
return np.dstack([img, alpha.astype(np.uint8)])
|
||||
|
||||
def update(self, img: np.ndarray, bgs=None, opacity: float = 1.0) -> None:
|
||||
GL = self.GL
|
||||
h, w = img.shape[:2]
|
||||
blend = bool(bgs) and opacity < 0.999 and not self.hdr
|
||||
if blend != self.blend: # the texture's format changes: make it again
|
||||
self.blend, self.w, self.h = blend, 0, 0
|
||||
if blend:
|
||||
rgba = np.ascontiguousarray(self.with_alpha(img, bgs, opacity))
|
||||
GL.glBindTexture(GL.GL_TEXTURE_2D, self.tex)
|
||||
GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1)
|
||||
if (w, h) != (self.w, self.h):
|
||||
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST)
|
||||
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST)
|
||||
GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA8, w, h, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, rgba)
|
||||
self.w, self.h = w, h
|
||||
else:
|
||||
GL.glTexSubImage2D(GL.GL_TEXTURE_2D, 0, 0, 0, w, h, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, rgba)
|
||||
return
|
||||
img, fmt, kind = _gl_image(GL, img, self.hdr)
|
||||
GL.glBindTexture(GL.GL_TEXTURE_2D, self.tex)
|
||||
if (w, h) != (self.w, self.h):
|
||||
@@ -397,9 +422,49 @@ class _Panel:
|
||||
else:
|
||||
GL.glTexSubImage2D(GL.GL_TEXTURE_2D, 0, 0, 0, w, h, GL.GL_BGR, kind, img)
|
||||
|
||||
def blit(self, x: int, top: int) -> None:
|
||||
"""top: GL y of the image's top edge (GL y grows upwards; image rows go down)."""
|
||||
_PROG: dict = {} # per GL module: the program and vertex array that draw a see-through panel
|
||||
|
||||
def _draw_blended(self, x: int, top: int, target: int) -> None:
|
||||
GL = self.GL
|
||||
if id(GL) not in self._PROG:
|
||||
from OpenGL.GL import shaders
|
||||
vs = """#version 450
|
||||
void main() { vec2 p = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2); gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0); }"""
|
||||
fs = """#version 450
|
||||
layout(binding = 0) uniform sampler2D img;
|
||||
uniform ivec2 origin;
|
||||
out vec4 color;
|
||||
void main() {
|
||||
ivec2 q = ivec2(gl_FragCoord.xy) - origin;
|
||||
color = texelFetch(img, ivec2(q.x, textureSize(img, 0).y - 1 - q.y), 0);
|
||||
}"""
|
||||
prog = shaders.compileProgram(shaders.compileShader(vs, GL.GL_VERTEX_SHADER),
|
||||
shaders.compileShader(fs, GL.GL_FRAGMENT_SHADER), validate=False)
|
||||
self._PROG[id(GL)] = (prog, GL.glGetUniformLocation(prog, "origin"), GL.glGenVertexArrays(1))
|
||||
prog, u_origin, vao = self._PROG[id(GL)]
|
||||
old_vp = GL.glGetIntegerv(GL.GL_VIEWPORT)
|
||||
GL.glBindFramebuffer(GL.GL_DRAW_FRAMEBUFFER, target)
|
||||
GL.glViewport(x, top - self.h, self.w, self.h)
|
||||
GL.glUseProgram(prog)
|
||||
GL.glUniform2i(u_origin, x, top - self.h)
|
||||
GL.glActiveTexture(GL.GL_TEXTURE0)
|
||||
GL.glBindTexture(GL.GL_TEXTURE_2D, self.tex)
|
||||
GL.glBindVertexArray(vao)
|
||||
GL.glEnable(GL.GL_BLEND)
|
||||
GL.glBlendFuncSeparate(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA, GL.GL_ZERO, GL.GL_ONE) # window alpha stays 1
|
||||
GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3)
|
||||
GL.glDisable(GL.GL_BLEND)
|
||||
GL.glBindVertexArray(0)
|
||||
GL.glUseProgram(0)
|
||||
GL.glViewport(*[int(v) for v in old_vp])
|
||||
|
||||
def blit(self, x: int, top: int, target: int = 0) -> None:
|
||||
"""top: GL y of the image's top edge (GL y grows upwards; image rows go down). target: the framebuffer it goes
|
||||
into (only a see-through panel needs it: a plain one is copied into whatever is bound)."""
|
||||
GL = self.GL
|
||||
if self.blend:
|
||||
self._draw_blended(x, top, target)
|
||||
return
|
||||
GL.glBindFramebuffer(GL.GL_READ_FRAMEBUFFER, self.fbo)
|
||||
GL.glBlitFramebuffer(0, 0, self.w, self.h, x, top, x + self.w, top - self.h, GL.GL_COLOR_BUFFER_BIT,
|
||||
GL.GL_NEAREST)
|
||||
@@ -498,6 +563,11 @@ def _graphs(t: MiniText, frametimes, ui: float, gpu=None) -> np.ndarray:
|
||||
rows = [("frametime", ft, max(3 * target, float(np.percentile(ft, 99)) * 1.15 if ft else 3 * target), target,
|
||||
colour_ft), ("pipeline", g.get("busy", [])[-gw:], 2 * tick, tick, colour_busy),
|
||||
("delay", g.get("delay", [])[-gw:], 120.0, None, colour_delay)]
|
||||
show = g.get("show")
|
||||
if show is not None: # the rows the settings turn off
|
||||
rows = [r for r in rows if r[0] in show]
|
||||
if show is not None and "gpu" not in show:
|
||||
gpu = None
|
||||
th_big = cv2.getTextSize("120", font, big, th + 1)[0][1]
|
||||
th_lbl = cv2.getTextSize("Ag", font, small, 1)[0][1]
|
||||
gpu_d = gpu.latest if gpu is not None else None
|
||||
@@ -606,7 +676,7 @@ class GLWindowSink:
|
||||
NAMES = {"fsr": "FSR 1", "vsr": "RTX VSR", "bilinear": "bilinear"}
|
||||
|
||||
# Shown when the window opens, and again when the mouse moves over it.
|
||||
KEYS_HINT = "M: menu F: fullscreen Esc: quit"
|
||||
KEYS_HINT = "Right-click or M: menu F: fullscreen Esc: quit"
|
||||
fill, panorama = "fit", None # Fill the screen (set from the settings in __init__)
|
||||
|
||||
def __init__(self, w: int, h: int, settings=None, restart_reasons=None, choices: dict | None = None,
|
||||
@@ -648,6 +718,8 @@ class GLWindowSink:
|
||||
self._borderless = False # fullscreen as a borderless window (_set_borderless)
|
||||
glfw.set_key_callback(self.win, self._on_key)
|
||||
glfw.set_cursor_pos_callback(self.win, self._on_mouse)
|
||||
glfw.set_mouse_button_callback(self.win, self._on_click)
|
||||
glfw.set_scroll_callback(self.win, self._on_scroll)
|
||||
self._hint_next = 0.0 # moving the mouse shows the keys again, at most this often
|
||||
# settle(): in the first seconds chiaki-ng can show its window again (settle_hook, run.py, minimises it; later
|
||||
# too, each time the video window gets the keys back, as Alt+Tab to chiaki-ng's window restores it), and all
|
||||
@@ -750,8 +822,10 @@ class GLWindowSink:
|
||||
self.mini_panel, self._mini_state = _Panel(GL, self.hdr10), None
|
||||
self._frametimes, self._last_write, self._graph_drawn, self._gpu = collections.deque(maxlen=480), 0.0, 0.0, None
|
||||
self._menu_top = None # (GL y of the menu's top, the picture's (y0, height) it was placed in) while it's open
|
||||
self._menu_at = None # (x, GL y of the top) the menu was last drawn at, for the mouse
|
||||
self._cursor = (0.0, 0.0) # the mouse, in window coordinates
|
||||
self._menu_state = self._toast_state = None
|
||||
self._toast_text, self._toast_until = "M: menu F: fullscreen Esc: quit", time.perf_counter() + 8.0
|
||||
self._toast_text, self._toast_until = self.KEYS_HINT, time.perf_counter() + 8.0
|
||||
self._waited = -1 # wait(): the whole second the "no picture" note last said (-1: no wait going on)
|
||||
self._unwatch = st.watch(self._apply)
|
||||
self._overlay_at, self._overlay_check = None, 0.0
|
||||
@@ -859,11 +933,59 @@ class GLWindowSink:
|
||||
|
||||
def _on_mouse(self, win, x, y) -> None:
|
||||
"""Moving the mouse over the video shows the keys for a moment (not over the menu or another message)."""
|
||||
self._cursor = (x, y)
|
||||
if self.menu.open:
|
||||
p = self._menu_xy()
|
||||
if p is not None:
|
||||
self.menu.hover(*p)
|
||||
return
|
||||
now = time.perf_counter()
|
||||
if now >= self._hint_next and not self.menu.open and now >= self._toast_until:
|
||||
self._hint_next = now + 20.0
|
||||
self.toast(self.KEYS_HINT, 3.0 if self.hotkeys is None else 6.0)
|
||||
|
||||
def _menu_xy(self) -> tuple[float, float] | None:
|
||||
"""The mouse as a pixel of the menu's image (from its top-left), or None while the menu isn't on screen."""
|
||||
if self._menu_at is None or not self.menu.open:
|
||||
return None
|
||||
glfw = self.glfw
|
||||
ww, wh = glfw.get_window_size(self.win)
|
||||
fw, fh = glfw.get_framebuffer_size(self.win)
|
||||
if not ww or not wh:
|
||||
return None
|
||||
cx, cy = self._cursor[0] * fw / ww, self._cursor[1] * fh / wh # framebuffer pixels from the top-left
|
||||
mx, top = self._menu_at
|
||||
return cx - mx, cy - (fh - top)
|
||||
|
||||
def _on_click(self, win, button, action, mods) -> None:
|
||||
"""Mouse: right-click on the video opens the menu. In the menu, left-click is the next choice (or opens a
|
||||
section, or Close/Back), right-click the one before, a click outside closes it."""
|
||||
glfw = self.glfw
|
||||
if action != glfw.PRESS or self.overlay_of is not None:
|
||||
return
|
||||
right = button == glfw.MOUSE_BUTTON_RIGHT
|
||||
if not self.menu.open:
|
||||
if right:
|
||||
self.menu.open = True
|
||||
self._toast_until = 0.0
|
||||
return
|
||||
p = self._menu_xy()
|
||||
inside = p is not None and 0 <= p[0] < self.menu_panel.w and 0 <= p[1] < self.menu_panel.h
|
||||
if not inside:
|
||||
self.menu.open = False
|
||||
return
|
||||
r = self.menu.click(*p, right=right)
|
||||
if r == "close":
|
||||
self.menu.open = False
|
||||
elif r == "restart":
|
||||
self.menu.open = False
|
||||
self.cmd = "restart"
|
||||
|
||||
def _on_scroll(self, win, dx, dy) -> None:
|
||||
"""The wheel moves the highlight in the menu (the keys' Up and Down)."""
|
||||
if self.menu.open and dy:
|
||||
self.menu.key("up" if dy > 0 else "down")
|
||||
|
||||
def _name(self, mode: str) -> str:
|
||||
if mode == "vsr":
|
||||
return f"RTX VSR q{self.vsr.quality if self.vsr is not None else self.settings.vsr_quality}"
|
||||
@@ -1272,13 +1394,15 @@ class GLWindowSink:
|
||||
|
||||
def _draw_ui(self, x0: int, y0: int, dw: int, dh: int) -> None:
|
||||
"""Menu and messages, on top of everything else. Images are redrawn only when they change."""
|
||||
ui = max(0.5, dh / 1080)
|
||||
ui = max(0.5, dh / 1080) * self.settings.ui_scale
|
||||
op = self.settings.ui_opacity
|
||||
if not self.menu.open:
|
||||
self._menu_top = None
|
||||
self._menu_top = self._menu_at = None
|
||||
if self.menu.open:
|
||||
state = (self.menu.state(), round(ui, 3))
|
||||
state = (self.menu.state(), round(ui, 3), op)
|
||||
if state != self._menu_state:
|
||||
self.menu_panel.update(self.menu.render(ui))
|
||||
from .menu import BG, ROW_SEL
|
||||
self.menu_panel.update(self.menu.render(ui), [BG, ROW_SEL], op)
|
||||
self._menu_state = state
|
||||
p = self.menu_panel
|
||||
# Centred when it opens, then its top stays put (a row appearing, such as Restart now, grows it
|
||||
@@ -1287,15 +1411,17 @@ class GLWindowSink:
|
||||
if self._menu_top is None or self._menu_top[1] != where:
|
||||
self._menu_top = (y0 + (dh + p.h) // 2, where)
|
||||
top = min(y0 + dh, max(self._menu_top[0], y0 + p.h))
|
||||
self.menu_panel.blit(x0 + int(32 * ui), top)
|
||||
mx = x0 + int(32 * ui)
|
||||
self._menu_at = (mx, top) # where the mouse finds it (see _menu_xy)
|
||||
self.menu_panel.blit(mx, top, self._target())
|
||||
elif time.perf_counter() < self._toast_until:
|
||||
state = (self._toast_text, round(ui, 3))
|
||||
state = (self._toast_text, round(ui, 3), op)
|
||||
if state != self._toast_state:
|
||||
self.toast_panel.update(_pill(self._toast_text, ui))
|
||||
self.toast_panel.update(_pill(self._toast_text, ui), [(34, 30, 28)], op)
|
||||
self._toast_state = state
|
||||
p = self.toast_panel
|
||||
m = int(16 * ui)
|
||||
p.blit(x0 + dw - p.w - m, y0 + dh - m)
|
||||
p.blit(x0 + dw - p.w - m, y0 + dh - m, self._target())
|
||||
|
||||
def busy(self, fn, text: str):
|
||||
"""Run fn() on a worker thread while the window stays responsive, showing the last frame and `text`.
|
||||
@@ -1434,9 +1560,9 @@ class GLWindowSink:
|
||||
label += " + panorama"
|
||||
self.timer.end(q, f"{label} {dw}x{dh}")
|
||||
if self.overlay and isinstance(text, MiniText) and getattr(text, "graph", None):
|
||||
self._draw_graphs(text, ux, uy, uh)
|
||||
self._draw_graphs(text, ux, uy, uw, uh)
|
||||
elif self.overlay and isinstance(text, MiniText):
|
||||
self._draw_mini(text, ux, uy, uh)
|
||||
self._draw_mini(text, ux, uy, uw, uh)
|
||||
elif self.overlay and text:
|
||||
first, nl, rest = text.partition("\n") # the upscaler goes on the first line; a second is the test line
|
||||
self._blit_text(f"{first} {label}{nl}{rest}", ux, uy + uh, s)
|
||||
@@ -1511,28 +1637,39 @@ class GLWindowSink:
|
||||
self.GL.glDeleteTextures(sorted({int(tex), int(self.vsr_out[5])})) # a view and its storage, or one
|
||||
self.vsr_out = None
|
||||
|
||||
def _draw_mini(self, text: MiniText, x0: int, y0: int, dh: int) -> None:
|
||||
"""The minimal overlay in the top-left corner, at the screen's own size (drawn again only when it changes)."""
|
||||
ui = max(0.5, dh / 1080)
|
||||
state = (str(text), text.parts, text.health, round(ui, 3))
|
||||
if state != self._mini_state:
|
||||
self.mini_panel.update(_mini(text, ui))
|
||||
self._mini_state = state
|
||||
def _blit_corner(self, panel, x0: int, y0: int, dw: int, dh: int, ui: float) -> None:
|
||||
"""The stats panel in the corner the settings pick, a margin in from the picture's edges."""
|
||||
m = int(12 * ui)
|
||||
self.mini_panel.blit(x0 + m, y0 + dh - m)
|
||||
corner = self.settings.overlay_corner
|
||||
x = x0 + dw - panel.w - m if corner.endswith("right") else x0 + m
|
||||
top = y0 + m + panel.h if corner.startswith("bottom") else y0 + dh - m
|
||||
panel.blit(x, top, self._target())
|
||||
|
||||
def _draw_graphs(self, text: MiniText, x0: int, y0: int, dh: int) -> None:
|
||||
"""The Graphs overlay in the top-left corner (drawn about 30 times a second: the graphs scroll)."""
|
||||
if self._gpu is None:
|
||||
def _stats_ui(self, dh: int) -> float:
|
||||
return max(0.5, dh / 1080) * self.settings.overlay_scale
|
||||
|
||||
def _draw_mini(self, text: MiniText, x0: int, y0: int, dw: int, dh: int) -> None:
|
||||
"""The minimal overlay in a corner, at the screen's own size (drawn again only when it changes)."""
|
||||
ui = self._stats_ui(dh)
|
||||
state = (str(text), text.parts, text.health, round(ui, 3), self.settings.overlay_opacity)
|
||||
if state != self._mini_state:
|
||||
self.mini_panel.update(_mini(text, ui), [(30, 27, 25)], self.settings.overlay_opacity)
|
||||
self._mini_state = state
|
||||
self._blit_corner(self.mini_panel, x0, y0, dw, dh, ui)
|
||||
|
||||
def _draw_graphs(self, text: MiniText, x0: int, y0: int, dw: int, dh: int) -> None:
|
||||
"""The Graphs overlay in a corner (drawn about 30 times a second: the graphs scroll)."""
|
||||
if self._gpu is None and self.settings.graph_gpu:
|
||||
from .gpustats import GpuStats
|
||||
self._gpu = GpuStats()
|
||||
ui = max(0.5, dh / 1080)
|
||||
ui = self._stats_ui(dh)
|
||||
now = time.perf_counter()
|
||||
if now - self._graph_drawn >= 1 / 30 or self._mini_state != ("graphs", round(ui, 3)):
|
||||
self.mini_panel.update(_graphs(text, self._frametimes, ui, self._gpu))
|
||||
self._graph_drawn, self._mini_state = now, ("graphs", round(ui, 3))
|
||||
m = int(12 * ui)
|
||||
self.mini_panel.blit(x0 + m, y0 + dh - m)
|
||||
key = ("graphs", round(ui, 3), self.settings.overlay_opacity)
|
||||
if now - self._graph_drawn >= 1 / 30 or self._mini_state != key:
|
||||
self.mini_panel.update(_graphs(text, self._frametimes, ui, self._gpu), [GRAPH_BG, GRAPH_PLOT],
|
||||
self.settings.overlay_opacity)
|
||||
self._graph_drawn, self._mini_state = now, key
|
||||
self._blit_corner(self.mini_panel, x0, y0, dw, dh, ui)
|
||||
|
||||
def _blit_text(self, text: str, x0: int, top: int, s: float) -> None:
|
||||
GL = self.GL
|
||||
|
||||
+60
-4
@@ -54,6 +54,7 @@ class Menu:
|
||||
self._at = 0
|
||||
self._left: dict[str, str] = {} # each section's highlighted row when the player last left it
|
||||
self._size_key, self._size = None, (0, 0) # render(): the longest page and the tallest help, for these rows
|
||||
self._hit: dict = {} # render()'s layout in the image's pixels, for the mouse: head_h, row_h, n, w, buttons
|
||||
|
||||
def sections(self) -> list[str]:
|
||||
"""The sections that have a setting that applies right now, in SPECS order."""
|
||||
@@ -133,6 +134,48 @@ class Menu:
|
||||
self.settings.step(item.key, -1 if name == "left" else 1, self.allowed)
|
||||
return None
|
||||
|
||||
# ---- mouse (x, y: pixels in the image render() made, from its top-left corner)
|
||||
def _row_at(self, x: float, y: float) -> int | None:
|
||||
h = self._hit
|
||||
if not h or not (0 <= x < h["w"]) or y < h["head_h"]:
|
||||
return None
|
||||
i = int((y - h["head_h"]) // h["row_h"])
|
||||
return i if 0 <= i < len(self.rows()) else None
|
||||
|
||||
def hover(self, x: float, y: float) -> None:
|
||||
"""Moving the mouse over a row highlights it."""
|
||||
i = self._row_at(x, y)
|
||||
if i is not None and i != self.index:
|
||||
rows = self.rows()
|
||||
self.index, self._sel, self._at = i, self._row_key(rows[i]), i
|
||||
|
||||
def click(self, x: float, y: float, right: bool = False) -> str | None:
|
||||
"""A click: the Close and Back buttons, a section (opens it), Restart now, or a setting (left: the next
|
||||
choice, right: the one before). A right click in the header goes back a page. Returns \"close\", \"restart\" or None."""
|
||||
for name, (x0, y0, x1, y1) in self._hit.get("buttons", {}).items():
|
||||
if x0 <= x <= x1 and y0 <= y <= y1:
|
||||
if name == "close":
|
||||
return "close"
|
||||
self._show(None)
|
||||
return None
|
||||
i = self._row_at(x, y)
|
||||
if i is None:
|
||||
if right and self.page is not None:
|
||||
self._show(None)
|
||||
return None
|
||||
rows = self.rows()
|
||||
self.index, self._sel, self._at = i, self._row_key(rows[i]), i
|
||||
kind, item = rows[i]
|
||||
if kind == "restart":
|
||||
return None if right else "restart"
|
||||
if kind == "section":
|
||||
if not right:
|
||||
self._show(item)
|
||||
return None
|
||||
if not self.disabled(item.key):
|
||||
self.settings.step(item.key, -1 if right else 1, self.allowed)
|
||||
return None
|
||||
|
||||
def state(self) -> tuple:
|
||||
"""Everything the drawing depends on (the window redraws only when this changes)."""
|
||||
return (self.page, self.index, tuple(self.settings.values().items()), tuple(self.restart_reasons()),
|
||||
@@ -188,16 +231,28 @@ class Menu:
|
||||
h = head_h + n * row_h + foot_h
|
||||
img = np.full((h, w, 3), BG, np.uint8)
|
||||
|
||||
buttons: dict[str, tuple[int, int, int, int]] = {}
|
||||
|
||||
def button(name: str, label: str, right: int) -> int:
|
||||
"""A clickable box in the header, ending at `right`; returns where it starts."""
|
||||
tw, tht = cv2.getTextSize(label, FONT, small, th)[0]
|
||||
x1, x0, y0, y1 = right, right - tw - 2 * px(12), px(20), px(60)
|
||||
cv2.rectangle(img, (x0, y0), (x1, y1), ROW_SEL, -1)
|
||||
cv2.rectangle(img, (x0, y0), (x1, y1), ACCENT, 1)
|
||||
cv2.putText(img, label, (x0 + px(12), (y0 + y1 + tht) // 2), FONT, small, TEXT, th, cv2.LINE_AA)
|
||||
buttons[name] = (x0, y0, x1, y1)
|
||||
return x0
|
||||
|
||||
title_scale = 1.05 * ui
|
||||
if self.page is None:
|
||||
cv2.putText(img, "Settings", (pad, px(50)), TITLE_FONT, title_scale, TEXT, th + 1, cv2.LINE_AA)
|
||||
_text_right(img, "M / Esc to close", w - pad, px(48), small, MUTED, th)
|
||||
button("close", "Close", w - pad)
|
||||
else: # Settings > Smoothness
|
||||
x = pad
|
||||
for part, color in (("Settings > ", MUTED), (self.page, TEXT)):
|
||||
cv2.putText(img, part, (x, px(50)), TITLE_FONT, title_scale, color, th + 1, cv2.LINE_AA)
|
||||
x += cv2.getTextSize(part, TITLE_FONT, title_scale, th + 1)[0][0]
|
||||
_text_right(img, "Esc: back", w - pad, px(48), small, MUTED, th)
|
||||
button("back", "< Back", button("close", "Close", w - pad) - px(10))
|
||||
cv2.line(img, (pad, head_h - px(10)), (w - pad, head_h - px(10)), RULE, max(1, px(1.5)))
|
||||
|
||||
for i, (kind, item) in enumerate(rows):
|
||||
@@ -251,12 +306,13 @@ class Menu:
|
||||
y += px(26)
|
||||
cv2.putText(img, line, (pad, y), FONT, small, IMPACT, th, cv2.LINE_AA)
|
||||
cv2.putText(img, KEYS[self.page is not None], (pad, h - px(16)), FONT, small * 0.9, DIM, th, cv2.LINE_AA)
|
||||
self._hit = {"w": w, "head_h": head_h, "row_h": row_h, "buttons": buttons}
|
||||
return img
|
||||
|
||||
|
||||
# The keys, at the bottom: on the main page, and on a section's page
|
||||
KEYS = ("Up/Down select Left/Right change Enter open M close Q quit",
|
||||
"Up/Down select Left/Right change Esc back M close Q quit")
|
||||
KEYS = ("Click: next Right-click: previous Wheel: select Enter: open M: close Q: quit",
|
||||
"Click: next Right-click: previous Wheel: select Esc: back M: close Q: quit")
|
||||
|
||||
|
||||
def _text_right(img: np.ndarray, text: str, right: int, base: int, scale: float, color, th: int) -> int:
|
||||
|
||||
@@ -474,8 +474,10 @@ class Pipeline:
|
||||
# Under the numbers, only while a test runs: the delay test, or test mode (with the advice, when the game
|
||||
# isn't the 60 fps it needs; outside tests that advice is a message and the amber game rate).
|
||||
second = (delay_test.status() if delay_test is not None else (note or test_line()) if testing else "")
|
||||
graph = ({"hz": hz, "tick_ms": TICK * 1000, "busy": st.busy_ms[-300:], "delay": st.real_delay_ms[-300:]}
|
||||
if graphs else None)
|
||||
show = [k for k, on in (("frametime", settings.graph_frametime), ("pipeline", settings.graph_pipeline),
|
||||
("delay", settings.graph_delay), ("gpu", settings.graph_gpu)) if on] if graphs else None
|
||||
graph = ({"hz": hz, "tick_ms": TICK * 1000, "busy": st.busy_ms[-300:], "delay": st.real_delay_ms[-300:],
|
||||
"show": show} if graphs else None)
|
||||
return MiniText.make(parts, health, second, graph)
|
||||
|
||||
for tick in source:
|
||||
|
||||
@@ -59,6 +59,9 @@ class Spec:
|
||||
|
||||
|
||||
ON_OFF = {True: "On", False: "Off"}
|
||||
OPACITY = (1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3) # menu and stats opacity choices
|
||||
PCT = {v: f"{round(v * 100)}%" for v in OPACITY}
|
||||
PCT_SIZE = {v: f"{round(v * 100)}%" for v in (0.6, 0.75, 0.9, 1.0, 1.25, 1.5, 2.0)}
|
||||
CAPTURE_SIZES = {"1080p": (1920, 1080), "1440p": (2560, 1440), "2160p": (3840, 2160)} # Capture card resolution
|
||||
IMPACT_PC = "an RTX 4070 Ti SUPER" # where the numbers in `impact` and the hints were measured
|
||||
# scripts/looks_timing.py: GPU time of the built-in looks on a 1440p window (0.02 to 0.34 ms) and at 1080p for clips
|
||||
@@ -288,6 +291,30 @@ SPECS: tuple[Spec, ...] = (
|
||||
"dot: green, your PC keeps up; amber, it's tight; red, it's falling behind, so try a lower frame rate. "
|
||||
"Detailed adds the AI frame time, the upscaler and, at 90 and 120 fps, the extra frames shown (below "
|
||||
"about 95%, your PC is falling behind)."),
|
||||
Spec("overlay_corner", "Stats overlay corner", ("top-left", "top-right", "bottom-left", "bottom-right"), "top-left",
|
||||
{"top-left": "Top left", "top-right": "Top right", "bottom-left": "Bottom left",
|
||||
"bottom-right": "Bottom right"}, group="Display", applies=("overlay", (True,)),
|
||||
help="Which corner of the picture the stats go in."),
|
||||
Spec("overlay_scale", "Stats overlay size", (0.75, 1.0, 1.25, 1.5, 2.0), 1.0, PCT_SIZE, group="Display",
|
||||
applies=("overlay", (True,)), help="How big the stats are, as a share of their normal size."),
|
||||
Spec("overlay_opacity", "Stats overlay opacity", OPACITY, 0.8, PCT, group="Display",
|
||||
applies=("overlay", (True,)),
|
||||
help="How see-through the stats' background is. Text and graph lines stay solid; lower shows more of the "
|
||||
"game behind."),
|
||||
Spec("graph_frametime", "Graph: frametime", (True, False), True, ON_OFF, group="Display",
|
||||
applies=("overlay_style", ("graphs",)), help="Graphs style: the time between frames on screen, with 1% lows."),
|
||||
Spec("graph_pipeline", "Graph: pipeline", (True, False), True, ON_OFF, group="Display",
|
||||
applies=("overlay_style", ("graphs",)),
|
||||
help="Graphs style: SmoovPlay's time for each tick against its budget."),
|
||||
Spec("graph_delay", "Graph: delay", (True, False), True, ON_OFF, group="Display",
|
||||
applies=("overlay_style", ("graphs",)), help="Graphs style: how long each real frame takes to reach the screen."),
|
||||
Spec("graph_gpu", "Graph: graphics card", (True, False), True, ON_OFF, group="Display",
|
||||
applies=("overlay_style", ("graphs",)),
|
||||
help="Graphs style: the graphics card's load, power, temperature, clock and memory (needs nvidia-smi)."),
|
||||
Spec("ui_opacity", "Menu opacity", OPACITY, 0.9, PCT, group="Display",
|
||||
help="How see-through the settings menu and its messages are. Text stays solid."),
|
||||
Spec("ui_scale", "Menu size", (0.6, 0.75, 0.9, 1.0), 1.0, PCT_SIZE, group="Display",
|
||||
help="How big the settings menu and its messages are (100% is as big as the window allows)."),
|
||||
# framegen/replay.py. Memory in the hints: replay.memory_mb() for 1080p at 90 fps (tests/test_replay.py checks).
|
||||
Spec("replay", "Instant replay", (0, 30, 60, 120, 300), 0,
|
||||
{0: "Off", 30: "Last 30 seconds", 60: "Last minute", 120: "Last 2 minutes", 300: "Last 5 minutes"},
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
"""Mouse use of the in-video menu (framegen/menu.py) and the see-through panels' alpha (framegen/io._Panel)."""
|
||||
import numpy as np
|
||||
|
||||
from framegen import io
|
||||
from framegen.menu import Menu
|
||||
from framegen.settings import BY_KEY, Settings
|
||||
|
||||
|
||||
def _menu():
|
||||
m = Menu(Settings({}))
|
||||
m.open = True
|
||||
img = m.render(1.0)
|
||||
return m, img
|
||||
|
||||
|
||||
def _row_centre(m, i):
|
||||
h = m._hit
|
||||
return 100, h["head_h"] + i * h["row_h"] + h["row_h"] // 2
|
||||
|
||||
|
||||
def test_hover_moves_the_highlight():
|
||||
m, _ = _menu()
|
||||
m.hover(*_row_centre(m, 2))
|
||||
assert m.index == 2
|
||||
|
||||
|
||||
def test_left_click_steps_a_setting_forward_and_right_click_back():
|
||||
m, _ = _menu()
|
||||
rows = m.rows()
|
||||
i = next(i for i, (k, s) in enumerate(rows) if k == "setting" and len(s.choices) > 1)
|
||||
key = rows[i][1].key
|
||||
before = m.settings.values()[key]
|
||||
m.click(*_row_centre(m, i))
|
||||
after = m.settings.values()[key]
|
||||
assert after != before
|
||||
m.click(*_row_centre(m, i), right=True)
|
||||
assert m.settings.values()[key] == before
|
||||
|
||||
|
||||
def test_clicking_a_section_opens_it_and_back_button_returns():
|
||||
m, _ = _menu()
|
||||
i = next(i for i, (k, _s) in enumerate(m.rows()) if k == "section")
|
||||
m.click(*_row_centre(m, i))
|
||||
assert m.page is not None
|
||||
m.render(1.0)
|
||||
x0, y0, x1, y1 = m._hit["buttons"]["back"]
|
||||
m.click((x0 + x1) / 2, (y0 + y1) / 2)
|
||||
assert m.page is None
|
||||
|
||||
|
||||
def test_close_button_and_clicks_outside_rows():
|
||||
m, _ = _menu()
|
||||
x0, y0, x1, y1 = m._hit["buttons"]["close"]
|
||||
assert m.click((x0 + x1) / 2, (y0 + y1) / 2) == "close"
|
||||
assert m.click(5, 2) is None and m.click(10_000, 10_000) is None
|
||||
|
||||
|
||||
def test_menu_has_the_opacity_and_overlay_controls():
|
||||
keys = {s.key for _k, s in Menu(Settings({"overlay_style": "graphs"})).page_rows("Display") if _k == "setting"}
|
||||
assert {"ui_opacity", "ui_scale", "overlay_opacity", "overlay_scale", "overlay_corner", "graph_frametime",
|
||||
"graph_pipeline", "graph_delay", "graph_gpu"} <= keys
|
||||
assert BY_KEY["ui_opacity"].default == 0.9 and BY_KEY["overlay_opacity"].default == 0.8
|
||||
|
||||
|
||||
def test_alpha_keeps_text_solid_and_makes_the_background_see_through():
|
||||
bg = (34, 30, 28)
|
||||
img = np.full((10, 10, 3), bg, np.uint8)
|
||||
img[5, 5] = (238, 238, 238) # text
|
||||
out = io._Panel.with_alpha(img, [bg], 0.5)
|
||||
assert out.shape == (10, 10, 4)
|
||||
assert abs(int(out[0, 0, 3]) - 127) <= 1 # background: half
|
||||
assert out[5, 5, 3] == 255 # text: solid
|
||||
|
||||
|
||||
def test_graphs_rows_can_be_turned_off():
|
||||
t = io.MiniText.make([("60 fps", "main")], "", "", {"hz": 60, "tick_ms": 16.7, "busy": [5.0] * 50,
|
||||
"delay": [30.0] * 50, "show": ["frametime"]})
|
||||
full = io.MiniText.make([("60 fps", "main")], "", "", {"hz": 60, "tick_ms": 16.7, "busy": [5.0] * 50,
|
||||
"delay": [30.0] * 50})
|
||||
assert io._graphs(t, [8.3] * 60, 1.0).shape[0] < io._graphs(full, [8.3] * 60, 1.0).shape[0]
|
||||
Reference in new issue
Block a user