From 97c0b53f08af6332f7ffe2d743576b0a2f8db902 Mon Sep 17 00:00:00 2001 From: notomo Date: Sat, 24 Jun 2023 11:11:46 +0900 Subject: [PATCH] Small refactor point --- lua/gesture/core/point.lua | 60 ++++++++++++++++++-------------------- lua/gesture/core/state.lua | 5 +++- lua/gesture/view/init.lua | 2 +- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/lua/gesture/core/point.lua b/lua/gesture/core/point.lua index 464d4f1..e340460 100644 --- a/lua/gesture/core/point.lua +++ b/lua/gesture/core/point.lua @@ -1,7 +1,13 @@ local listlib = require("gesture.lib.list") local Point = {} -Point.__index = Point + +function Point.new(x, y) + return { + x = x, + y = y, + } +end local Y = function(p1, p2) local b = (p1.x * p2.y - p2.x * p1.y) / (p1.x - p2.x) @@ -11,22 +17,22 @@ local Y = function(p1, p2) end end -function Point.interpolate(self, point) +function Point.interpolate(p1, p2) local points = {} - if self.x == point.x then - local p1 = self - local p2 = point + if p1.x == p2.x then + local p_start = p1 + local p_end = p2 local reverse = false - if self.y > point.y then - p1 = point - p2 = self - table.insert(points, p1) + if p1.y > p2.y then + p_start = p2 + p_end = p1 + table.insert(points, p_start) reverse = true end - local x = p1.x - local y = p1.y + 1 - while y < p2.y do + local x = p_start.x + local y = p_start.y + 1 + while y < p_end.y do table.insert(points, Point.new(x, y)) y = y + 1 end @@ -34,24 +40,24 @@ function Point.interpolate(self, point) if reverse then return listlib.reverse(points) end - table.insert(points, p2) + table.insert(points, p_end) return points end - local p1 = self - local p2 = point + local p_start = p1 + local p_end = p2 local reverse = false - if self.x > point.x then - p1 = point - p2 = self + if p1.x > p2.x then + p_start = p2 + p_end = p1 reverse = true end - table.insert(points, p1) + table.insert(points, p_start) local offset = 0.1 - local x = p1.x + offset - local get_y = Y(p1, p2) - while x < p2.x do + local x = p_start.x + offset + local get_y = Y(p_start, p_end) + while x < p_end.x do local y = math.floor(get_y(x) + 0.5) local new = Point.new(math.floor(x + 0.5), y) local last = points[#points] @@ -65,16 +71,8 @@ function Point.interpolate(self, point) return listlib.reverse(points) end table.remove(points, 1) - table.insert(points, p2) + table.insert(points, p_end) return points end -function Point.new(x, y) - local tbl = { - x = x, - y = y, - } - return setmetatable(tbl, Point) -end - return Point diff --git a/lua/gesture/core/state.lua b/lua/gesture/core/state.lua index 42a2acd..5e39ce2 100644 --- a/lua/gesture/core/state.lua +++ b/lua/gesture/core/state.lua @@ -82,7 +82,10 @@ end function State.action_context(self) local point = self.view.current_point() return { - last_position = { point.y, point.x }, + last_position = { + point.y, + point.x, + }, inputs = vim.tbl_map(function(input) return input diff --git a/lua/gesture/view/init.lua b/lua/gesture/view/init.lua index 8a0df5b..d09e838 100644 --- a/lua/gesture/view/init.lua +++ b/lua/gesture/view/init.lua @@ -38,7 +38,7 @@ function View.focus(self, last_point) else last = self._new_points[#self._new_points] or last_point end - self._new_points = last:interpolate(current_point) + self._new_points = Point.interpolate(last, current_point) return current_point end