mouse handling refact
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user