diff --git a/.gitignore b/.gitignore index b381695..36a1642 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ docs minify.lua *.tic *.zip +NOTES_* diff --git a/inc/decision/decision.manager.lua b/inc/decision/decision.manager.lua index 65412bc..b0ce2c1 100644 --- a/inc/decision/decision.manager.lua +++ b/inc/decision/decision.manager.lua @@ -144,25 +144,20 @@ function Decision.update(decisions, selected_decision_index) selected_decision_index = Util.safeindex(decisions, selected_decision_index + 1) end - if Mouse.clicked() then - local mx = Mouse.x() - local my = Mouse.y() - local bar_height = 16 - local bar_y = Config.screen.height - bar_height - if my >= bar_y then - if mx < 15 then - Audio.sfx_beep() - Mouse.consume() - selected_decision_index = Util.safeindex(decisions, selected_decision_index - 1) - elseif mx > Config.screen.width - 15 then - Audio.sfx_beep() - Mouse.consume() - selected_decision_index = Util.safeindex(decisions, selected_decision_index + 1) - else - Mouse.consume() - return selected_decision_index, true - end - end + local bar_h = 16 + local bar_y = Config.screen.height - bar_h + local prev_zone = { x = 0, y = bar_y, w = 15, h = bar_h } + local next_zone = { x = Config.screen.width-15, y = bar_y, w = 15, h = bar_h } + local confirm_zone = { x = 15, y = bar_y, w = Config.screen.width-30, h = bar_h } + + if Mouse.zone(prev_zone) then + Audio.sfx_beep() + selected_decision_index = Util.safeindex(decisions, selected_decision_index - 1) + elseif Mouse.zone(next_zone) then + Audio.sfx_beep() + selected_decision_index = Util.safeindex(decisions, selected_decision_index + 1) + elseif Mouse.zone(confirm_zone) then + return selected_decision_index, true end return selected_decision_index, false diff --git a/inc/system/system.mouse.lua b/inc/system/system.mouse.lua index 395147c..3140ffa 100644 --- a/inc/system/system.mouse.lua +++ b/inc/system/system.mouse.lua @@ -41,3 +41,36 @@ function Mouse.consume() _consumed = true end function Mouse.in_rect(x, y, w, h) return _mx >= x and _mx < x + w and _my >= y and _my < y + h 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 diff --git a/inc/system/system.ui.lua b/inc/system/system.ui.lua index f0dcb73..4fb219b 100644 --- a/inc/system/system.ui.lua +++ b/inc/system/system.ui.lua @@ -58,9 +58,7 @@ function UI.update_menu(items, selected_item, x, y, centered) end end - if x ~= nil and y ~= nil and Mouse.clicked() then - local mx = Mouse.x() - local my = Mouse.y() + if x ~= nil and y ~= nil then local menu_x = x if centered then 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 end for i, _ in ipairs(items) do - local item_y = y + (i - 1) * 10 - if my >= item_y and my < item_y + 10 and mx >= menu_x - 8 then - Mouse.consume() + if Mouse.zone({ x = menu_x - 8, y = y + (i-1) * 10, w = Config.screen.width, h = 10 }) then return i, true end end diff --git a/inc/window/window.minigame.ddr.lua b/inc/window/window.minigame.ddr.lua index 2cba4fd..5c92d1e 100644 --- a/inc/window/window.minigame.ddr.lua +++ b/inc/window/window.minigame.ddr.lua @@ -355,14 +355,9 @@ function MinigameDDRWindow.update() right = Input.right() } - if Mouse.clicked() then - local mx = Mouse.x() - local my = Mouse.y() - 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 + for _, target in ipairs(mg.target_arrows) do + if Mouse.zone({ x = target.x, y = mg.target_y, w = mg.arrow_size, h = mg.arrow_size }) then + input_map[target.dir] = true end end diff --git a/inc/window/window.minigame.mash.lua b/inc/window/window.minigame.mash.lua index 0d8af73..ec52b9b 100644 --- a/inc/window/window.minigame.mash.lua +++ b/inc/window/window.minigame.mash.lua @@ -83,12 +83,7 @@ function MinigameButtonMashWindow.update() return end - local mouse_on_button = false - 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 + local mouse_on_button = Mouse.zone_circle({ x = mg.button_x, y = mg.button_y, r = mg.button_size }) if Input.select() or mouse_on_button then Audio.sfx_drum_high() diff --git a/inc/window/window.minigame.rhythm.lua b/inc/window/window.minigame.rhythm.lua index 3f8abff..15a893c 100644 --- a/inc/window/window.minigame.rhythm.lua +++ b/inc/window/window.minigame.rhythm.lua @@ -95,12 +95,7 @@ function MinigameRhythmWindow.update() if mg.press_cooldown > 0 then mg.press_cooldown = mg.press_cooldown - 1 end - local mouse_on_button = false - 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 + local mouse_on_button = Mouse.zone_circle({ x = mg.button_x, y = mg.button_y, r = mg.button_size }) if (Input.select() or mouse_on_button) and mg.press_cooldown == 0 then mg.button_pressed_timer = mg.button_press_duration