From b8a08279cfab1244322c7f6e17a8daaade8cd803 Mon Sep 17 00:00:00 2001 From: Xinhao Yuan Date: Fri, 26 Feb 2021 19:25:15 -0500 Subject: [PATCH] Ensure that cursor fit in the selected area. --- switcher.lua | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/switcher.lua b/switcher.lua index a74dcf7..a64971c 100644 --- a/switcher.lua +++ b/switcher.lua @@ -114,14 +114,41 @@ function module.start(c, exit_keys) local selected_area_ = nil local function selected_area() if selected_area_ == nil then + local min_dis = nil for i, a in ipairs(areas) do - if not a.inhabitable and - a.x <= traverse_x and traverse_x < a.x + a.width and - a.y <= traverse_y and traverse_y < a.y + a.height - then - selected_area_ = i + if not a.inhabitable then + local dis = + math.abs(a.x + traverse_radius - traverse_x) + math.abs(a.x + a.width - traverse_radius - traverse_x) - a.width + + math.abs(a.y + traverse_radius - traverse_y) + math.abs(a.y + a.height - traverse_radius - traverse_y) - a.height + + traverse_radius * 4 + if min_dis == nil or min_dis > dis then + min_dis = dis + selected_area_ = i + end end end + + if min_dis > 0 then + local a = areas[selected_area_] + local corners = { + {a.x + traverse_radius, a.y + traverse_radius}, + {a.x + traverse_radius, a.y + a.height - traverse_radius}, + {a.x + a.width - traverse_radius, a.y + traverse_radius}, + {a.x + a.width - traverse_radius, a.y + a.height - traverse_radius} + } + min_dis = nil + local min_i + for i, c in ipairs(corners) do + local dis = math.abs(c[1] - traverse_x) + math.abs(c[2] - traverse_y) + if min_dis == nil or min_dis > dis then + min_dis = dis + min_i = i + end + end + + traverse_x = corners[min_i][1] + traverse_y = corners[min_i][2] + end end return selected_area_ end