From 1a18c42b9ef5b85857bd74328f58b364cff003da Mon Sep 17 00:00:00 2001 From: codegirl-007 Date: Fri, 19 Jun 2026 02:21:49 -0700 Subject: [PATCH] clean up button creation + center text --- game/controls.odin | 45 +++++++++++++++++++++++---------------------- game/utils.odin | 27 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 22 deletions(-) create mode 100644 game/utils.odin diff --git a/game/controls.odin b/game/controls.odin index ea2f69f..ee90cb3 100644 --- a/game/controls.odin +++ b/game/controls.odin @@ -36,34 +36,35 @@ poll_controls_command :: proc(world: ^World) -> Command { return Command{kind = .None} } -render_controls :: proc(world: ^World) { - if world.phase != .Build do return - - bar_y := bottom_bar_y() - rl.DrawRectangle(0, bar_y, SCREEN_W, CONTROL_BAR_H, CONTROL_BAR_BG) - +draw_archer_button :: proc(world: ^World, archer: rl.Rectangle) { arch := get_archetype(.Archer) - archer, start_wave_btn := build_controls_layout() mouse := rl.GetMousePosition() arch_bg := CONTROLS_SHOP_IDLE if world.selected_tower == .Archer do arch_bg = CONTROL_SHOP_SELECTED if rl.CheckCollisionPointRec(mouse, archer) do arch_bg = CONTROL_BUTTON_HOVER - rl.DrawRectangleRec(archer, arch_bg) + label := fmt.ctprintf("Archer - %dg", arch.cost) - rl.DrawText(label, i32(archer.x) + 6, i32(archer.y) + 4, 14, OVERLAY_TEXT_COLOR) - - - wave_bg := CONTROL_BUTTON_COLOR - if rl.CheckCollisionPointRec(mouse, start_wave_btn) do wave_bg = CONTROLS_SHOP_HOVER - rl.DrawRectangleRec(start_wave_btn, wave_bg) - - rl.DrawText( - "Start Wave", - i32(start_wave_btn.x) + 8, - i32(start_wave_btn.y) + 3, - 16, - OVERLAY_TEXT_COLOR, - ) + draw_button(archer, label, arch_bg, 14, OVERLAY_TEXT_COLOR) +} + +draw_start_wave_button :: proc(start_wave_rect: rl.Rectangle, mouse: rl.Vector2) { + bg := CONTROL_BUTTON_COLOR + if rl.CheckCollisionPointRec(mouse, start_wave_rect) do bg = CONTROL_BUTTON_HOVER + + draw_button(start_wave_rect, "Start Wave", bg, 16, OVERLAY_TEXT_COLOR) +} + +render_controls :: proc(world: ^World) { + if world.phase != .Build do return + mouse := rl.GetMousePosition() + + bar_y := bottom_bar_y() + rl.DrawRectangle(0, bar_y, SCREEN_W, CONTROL_BAR_H, CONTROL_BAR_BG) + + archer, start_wave_rect := build_controls_layout() + draw_archer_button(world, archer) + draw_start_wave_button(start_wave_rect, mouse) + } diff --git a/game/utils.odin b/game/utils.odin new file mode 100644 index 0000000..b6866f7 --- /dev/null +++ b/game/utils.odin @@ -0,0 +1,27 @@ +package game + +import rl "vendor:raylib" + +draw_text_centered_in_rect :: proc( + text: cstring, + rect: rl.Rectangle, + font_size: i32, + color: rl.Color, +) { + text_w := rl.MeasureText(text, font_size) + x := i32(rect.x) + (i32(rect.width) - text_w) / 2 + y := i32(rect.y) + (i32(rect.height) - font_size) / 2 + rl.DrawText(text, x, y, font_size, color) +} + +draw_button :: proc( + rect: rl.Rectangle, + label: cstring, + bg: rl.Color, + font_size: i32, + text_color: rl.Color, +) { + rl.DrawRectangleRec(rect, bg) + draw_text_centered_in_rect(label, rect, font_size, text_color) +} +