mouse handling refact
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2026-04-02 22:12:58 +02:00
parent 211af18c26
commit 8921f02821
7 changed files with 55 additions and 45 deletions

1
.gitignore vendored
View File

@@ -7,3 +7,4 @@ docs
minify.lua minify.lua
*.tic *.tic
*.zip *.zip
NOTES_*

View File

@@ -144,25 +144,20 @@ function Decision.update(decisions, selected_decision_index)
selected_decision_index = Util.safeindex(decisions, selected_decision_index + 1) selected_decision_index = Util.safeindex(decisions, selected_decision_index + 1)
end end
if Mouse.clicked() then local bar_h = 16
local mx = Mouse.x() local bar_y = Config.screen.height - bar_h
local my = Mouse.y() local prev_zone = { x = 0, y = bar_y, w = 15, h = bar_h }
local bar_height = 16 local next_zone = { x = Config.screen.width-15, y = bar_y, w = 15, h = bar_h }
local bar_y = Config.screen.height - bar_height local confirm_zone = { x = 15, y = bar_y, w = Config.screen.width-30, h = bar_h }
if my >= bar_y then
if mx < 15 then if Mouse.zone(prev_zone) then
Audio.sfx_beep() Audio.sfx_beep()
Mouse.consume() selected_decision_index = Util.safeindex(decisions, selected_decision_index - 1)
selected_decision_index = Util.safeindex(decisions, selected_decision_index - 1) elseif Mouse.zone(next_zone) then
elseif mx > Config.screen.width - 15 then Audio.sfx_beep()
Audio.sfx_beep() selected_decision_index = Util.safeindex(decisions, selected_decision_index + 1)
Mouse.consume() elseif Mouse.zone(confirm_zone) then
selected_decision_index = Util.safeindex(decisions, selected_decision_index + 1) return selected_decision_index, true
else
Mouse.consume()
return selected_decision_index, true
end
end
end end
return selected_decision_index, false return selected_decision_index, false

View File

@@ -41,3 +41,36 @@ function Mouse.consume() _consumed = true end
function Mouse.in_rect(x, y, w, h) function Mouse.in_rect(x, y, w, h)
return _mx >= x and _mx < x + w and _my >= y and _my < y + h return _mx >= x and _mx < x + w and _my >= y and _my < y + h
end end
--- Returns true if the mouse is within the given circle.
--- @within Mouse
--- @param cx number Center x.
--- @param cy number Center y.
--- @param r number Radius.
function Mouse.in_circle(cx, cy, r)
local dx = _mx - cx
local dy = _my - cy
return (dx * dx + dy * dy) <= (r * r)
end
--- Returns true if the mouse was clicked inside the given rectangle, and consumes the click.
--- @within Mouse
--- @param rect table A table with fields: x, y, w, h.
function Mouse.zone(rect)
if Mouse.clicked() and Mouse.in_rect(rect.x, rect.y, rect.w, rect.h) then
Mouse.consume()
return true
end
return false
end
--- Returns true if the mouse was clicked inside the given circle, and consumes the click.
--- @within Mouse
--- @param circle table A table with fields: x, y, r.
function Mouse.zone_circle(circle)
if Mouse.clicked() and Mouse.in_circle(circle.x, circle.y, circle.r) then
Mouse.consume()
return true
end
return false
end

View File

@@ -58,9 +58,7 @@ function UI.update_menu(items, selected_item, x, y, centered)
end end
end end
if x ~= nil and y ~= nil and Mouse.clicked() then if x ~= nil and y ~= nil then
local mx = Mouse.x()
local my = Mouse.y()
local menu_x = x local menu_x = x
if centered then if centered then
local max_w = 0 local max_w = 0
@@ -71,9 +69,7 @@ function UI.update_menu(items, selected_item, x, y, centered)
menu_x = (Config.screen.width - max_w) / 2 menu_x = (Config.screen.width - max_w) / 2
end end
for i, _ in ipairs(items) do for i, _ in ipairs(items) do
local item_y = y + (i - 1) * 10 if Mouse.zone({ x = menu_x - 8, y = y + (i-1) * 10, w = Config.screen.width, h = 10 }) then
if my >= item_y and my < item_y + 10 and mx >= menu_x - 8 then
Mouse.consume()
return i, true return i, true
end end
end end

View File

@@ -355,14 +355,9 @@ function MinigameDDRWindow.update()
right = Input.right() right = Input.right()
} }
if Mouse.clicked() then for _, target in ipairs(mg.target_arrows) do
local mx = Mouse.x() if Mouse.zone({ x = target.x, y = mg.target_y, w = mg.arrow_size, h = mg.arrow_size }) then
local my = Mouse.y() input_map[target.dir] = true
for _, target in ipairs(mg.target_arrows) do
if mx >= target.x and mx < target.x + mg.arrow_size and
my >= mg.target_y and my < mg.target_y + mg.arrow_size then
input_map[target.dir] = true
end
end end
end end

View File

@@ -83,12 +83,7 @@ function MinigameButtonMashWindow.update()
return return
end end
local mouse_on_button = false local mouse_on_button = Mouse.zone_circle({ x = mg.button_x, y = mg.button_y, r = mg.button_size })
if Mouse.clicked() then
local dx = Mouse.x() - mg.button_x
local dy = Mouse.y() - mg.button_y
mouse_on_button = (dx * dx + dy * dy) <= (mg.button_size * mg.button_size)
end
if Input.select() or mouse_on_button then if Input.select() or mouse_on_button then
Audio.sfx_drum_high() Audio.sfx_drum_high()

View File

@@ -95,12 +95,7 @@ function MinigameRhythmWindow.update()
if mg.press_cooldown > 0 then if mg.press_cooldown > 0 then
mg.press_cooldown = mg.press_cooldown - 1 mg.press_cooldown = mg.press_cooldown - 1
end end
local mouse_on_button = false local mouse_on_button = Mouse.zone_circle({ x = mg.button_x, y = mg.button_y, r = mg.button_size })
if Mouse.clicked() then
local dx = Mouse.x() - mg.button_x
local dy = Mouse.y() - mg.button_y
mouse_on_button = (dx * dx + dy * dy) <= (mg.button_size * mg.button_size)
end
if (Input.select() or mouse_on_button) and mg.press_cooldown == 0 then if (Input.select() or mouse_on_button) and mg.press_cooldown == 0 then
mg.button_pressed_timer = mg.button_press_duration mg.button_pressed_timer = mg.button_press_duration