nodes/_back_button.pyΒΆ
Part of Dungeon Explorer.
1"""Back-button helpers shared across popup screens.
2
3Renders a circular touch-friendly back glyph in the top-left corner. Hit-test
4returns True for clicks landing on the disc. Used by popups that want a tap
5target equivalent to pressing Esc.
6"""
7
8import math
9
10__all__ = [
11 "BACK_BTN_CX",
12 "BACK_BTN_CY",
13 "BACK_BTN_R",
14 "draw_back_button",
15 "back_button_hit",
16 "check_menu_click",
17]
18
19# Circular back button anchored to the top-left corner. ~56px diameter is
20# finger-friendly and visually consistent across popups.
21BACK_BTN_CX = 40.0
22BACK_BTN_CY = 40.0
23BACK_BTN_R = 28.0
24
25
26def draw_back_button(renderer) -> None:
27 """Draw a tappable "back" glyph (curved arrow) in the top-left corner."""
28 cx, cy, r = BACK_BTN_CX, BACK_BTN_CY, BACK_BTN_R
29 renderer.circle((cx, cy), r, fill=(0.08, 0.08, 0.10, 0.85))
30 renderer.circle((cx, cy), r - 3, fill=(0.22, 0.22, 0.26, 0.9))
31 arc_r = r * 0.55
32 for deg in range(-70, 200, 12):
33 rad = math.radians(deg)
34 px = cx + math.cos(rad) * arc_r
35 py = cy + math.sin(rad) * arc_r
36 renderer.rect((px - 1.5, py - 1.5), (3, 3), fill=(1.0, 0.9, 0.3, 1.0))
37 ax = cx + math.cos(math.radians(200)) * arc_r
38 ay = cy + math.sin(math.radians(200)) * arc_r
39 renderer.polygon([(ax - 8, ay), (ax + 4, ay - 7), (ax + 4, ay + 7)], fill=(1.0, 0.9, 0.3, 1.0))
40
41
42def back_button_hit(mx: float, my: float) -> bool:
43 """True when a click/touch lands on the back button."""
44 dx = mx - BACK_BTN_CX
45 dy = my - BACK_BTN_CY
46 return (dx * dx + dy * dy) <= BACK_BTN_R * BACK_BTN_R
47
48
49def check_menu_click(
50 mx: float, my: float, count: int, origin_x: float, origin_y: float, item_w: float, item_h: float, spacing: float
51) -> int | None:
52 """Return the index of the clicked menu item in a vertical list, or None."""
53 for i in range(count):
54 iy = origin_y + i * spacing
55 if origin_x <= mx <= origin_x + item_w and iy - 4 <= my <= iy + item_h:
56 return i
57 return None