Files
windroost/p2_godot_sample/scripts/ui_theme.gd
T
wushenghua e7b55e1c17 feat: align runtime with paper numbers and build the battle HUD carrier
Numbers. The paper runner (tools/mvp_validation_sim.py) and the Godot
runtime adapter were two independent rewrites of the same rules and had
drifted: seed derivation, damage order, the weakness rule and the shield
rebuild semantics all differed, so the runtime never actually reproduced
the paper numbers and P2-BAT-01 could never pass.

- add data/mvp_battle_rules.json as the single source of balance data and
  of the per-run seed derivation, read by both engines
- derive the efficiency factor from SHA-256 (48-bit prefix) so Python and
  GDScript land on the same float64
- port the paper semantics into battle_sim.gd and expose the simulation as
  a stepwise state machine so a live scene can drive it at 1x/2x/3x
- add tools/mvp_parity_gate.py: diffs 300 results field by field, event
  log by event log, and exits non-zero on any difference
- freeze data/mvp_validation_seed_reference.json so unapproved numeric
  drift fails the gate
- note that GDScript's roundf() is half-away-from-zero while Python's
  round() is banker's rounding; the runtime uses String.num instead

UI. The sample had no real UI carrier, no localization and no touch path,
so the NinePatch, dynamic text, safe area and input cases had nothing to
be proven against.

- add the battle HUD, laid out from the contract table rather than container
  stretch ratios, reproducing the 390x844 rectangles exactly and adapting
  360x800 / 430x932 with the stage kept as priority space
- add runtime text tables for zh_CN and en_US and a key validator that
  proves both locales carry the same keys and every content id has a string
- add NinePatch frames with a 6px border band and 6px cut corners
- add the live battle scene, the capture runner and the 1x/2x/3x
  equivalence test

tools/run_p2_checks.sh runs the nine gates in one command; all pass.
2026-11-16 22:22:55 +08:00

151 lines
5.5 KiB
GDScript

class_name UiTheme
extends RefCounted
## Shared UI theme factory.
##
## Every panel, button, tab and bar is built from the same pixel frames in
## `assets/ui/`, so the HUD stays consistent and every edge stays on the pixel
## grid. Frames carry a 6px border and 6px transparent cut corners; the
## NinePatch margins must match that or the corners smear when a panel grows.
const FRAME_PATCH_MARGIN := 6
const FRAME_PANEL := "res://assets/ui/ui_frame_panel.png"
const FRAME_BUTTON := "res://assets/ui/ui_frame_button.png"
const FRAME_BUTTON_PRESSED := "res://assets/ui/ui_frame_button_pressed.png"
const FRAME_TAB := "res://assets/ui/ui_frame_tab.png"
const FRAME_TAB_SELECTED := "res://assets/ui/ui_frame_tab_selected.png"
const FRAME_TILE := "res://assets/ui/ui_frame_tile.png"
const FRAME_STAGE := "res://assets/ui/ui_frame_stage.png"
const ICON_SWORD := "res://assets/ui/ui_icon_sword.png"
const ICON_BOLT := "res://assets/ui/ui_icon_bolt.png"
const ICON_STAR := "res://assets/ui/ui_icon_star.png"
const COLOR_BG := Color("#0f192a")
const COLOR_PANEL := Color("#1c2d44")
const COLOR_BORDER := Color("#e8c978")
const COLOR_TEXT := Color("#fff4cd")
const COLOR_TEXT_DIM := Color("#c2a878")
const COLOR_MUTED := Color("#9a8464")
const COLOR_MINT := Color("#6fdab2")
const COLOR_ORANGE := Color("#e17448")
const COLOR_PURPLE := Color("#7e5d9a")
const COLOR_DANGER := Color("#d35a5a")
static func frame_style(path: String, content_margin: int = 0) -> StyleBoxTexture:
var style := StyleBoxTexture.new()
var texture := load(path) as Texture2D
if texture == null:
push_warning("UI frame texture missing: %s" % path)
return style
style.texture = texture
style.set_texture_margin_all(FRAME_PATCH_MARGIN)
# The 6px frame band must not add layout padding on its own: the HUD
# sizes each region from the layout contract, so the content inset is
# controlled explicitly by the caller.
style.content_margin_left = content_margin
style.content_margin_top = content_margin
style.content_margin_right = content_margin
style.content_margin_bottom = content_margin
style.axis_stretch_horizontal = StyleBoxTexture.AXIS_STRETCH_MODE_STRETCH
style.axis_stretch_vertical = StyleBoxTexture.AXIS_STRETCH_MODE_STRETCH
return style
static func flat_style(
color: Color, border_color: Color = Color(0, 0, 0, 0), border_width: int = 0
) -> StyleBoxFlat:
var style := StyleBoxFlat.new()
style.bg_color = color
if border_width > 0:
style.border_color = border_color
style.set_border_width_all(border_width)
return style
static func build_font() -> Font:
# SystemFont lets the engine resolve a CJK-capable face without shipping a
# font binary in the repository. Godot's built-in font has no CJK glyphs,
# so the zh_CN pass would render boxes without this.
var font := SystemFont.new()
font.font_names = PackedStringArray(
["PingFang SC", "Hiragino Sans GB", "Heiti SC", "Noto Sans CJK SC", "Songti SC"]
)
font.font_weight = 500
font.allow_system_fallback = true
font.subpixel_positioning = TextServer.SUBPIXEL_POSITIONING_DISABLED
return font
static func build_theme() -> Theme:
var theme := Theme.new()
var font := build_font()
if font != null:
theme.default_font = font
theme.default_font_size = 13
theme.set_stylebox("panel", "PanelContainer", frame_style(FRAME_PANEL))
theme.set_stylebox("panel", "Panel", frame_style(FRAME_PANEL))
var button_normal := frame_style(FRAME_BUTTON)
var button_pressed := frame_style(FRAME_BUTTON_PRESSED)
var button_hover := frame_style(FRAME_BUTTON)
theme.set_stylebox("normal", "Button", button_normal)
theme.set_stylebox("pressed", "Button", button_pressed)
theme.set_stylebox("hover", "Button", button_hover)
theme.set_stylebox("focus", "Button", flat_style(Color(0, 0, 0, 0)))
theme.set_stylebox("disabled", "Button", frame_style(FRAME_BUTTON))
theme.set_color("font_color", "Button", COLOR_TEXT)
theme.set_color("font_pressed_color", "Button", COLOR_TEXT)
theme.set_color("font_hover_color", "Button", Color("#fff8dc"))
theme.set_color("font_disabled_color", "Button", COLOR_TEXT_DIM)
theme.set_font_size("font_size", "Button", 13)
theme.set_stylebox("panel", "ProgressBar", flat_style(Color("#080f1b")))
theme.set_stylebox("fill", "ProgressBar", flat_style(COLOR_MINT))
theme.set_color("font_color", "ProgressBar", COLOR_TEXT)
theme.set_color("font_color", "Label", COLOR_TEXT)
var panel_box := frame_style(FRAME_PANEL)
theme.set_stylebox("panel", "PanelContainer", panel_box)
return theme
static func make_panel(frame_path: String = FRAME_PANEL) -> PanelContainer:
var panel := PanelContainer.new()
panel.add_theme_stylebox_override("panel", frame_style(frame_path))
return panel
static func make_label(text: String, font_size: int = 13, color: Color = COLOR_TEXT) -> Label:
var label := Label.new()
label.text = text
label.add_theme_font_size_override("font_size", font_size)
label.add_theme_color_override("font_color", color)
label.mouse_filter = Control.MOUSE_FILTER_IGNORE
return label
static func make_button(text: String, font_size: int = 13) -> Button:
var button := Button.new()
button.text = text
button.add_theme_font_size_override("font_size", font_size)
button.focus_mode = Control.FOCUS_NONE
button.custom_minimum_size = Vector2(48, 48)
return button
static func make_bar(color: Color, height: int = 10) -> ProgressBar:
var bar := ProgressBar.new()
bar.min_value = 0.0
bar.max_value = 1.0
bar.value = 1.0
bar.show_percentage = false
bar.custom_minimum_size = Vector2(0, height)
bar.add_theme_stylebox_override("background", flat_style(Color("#080f1b")))
bar.add_theme_stylebox_override("fill", flat_style(color))
return bar