diff --git a/sonic-boost.love/core/modules/menusystem/grid.lua b/sonic-boost.love/core/modules/menusystem/grid.lua index a85b4e8..2ca2a8e 100644 --- a/sonic-boost.love/core/modules/menusystem/grid.lua +++ b/sonic-boost.love/core/modules/menusystem/grid.lua @@ -59,6 +59,12 @@ function GridBox:getSlotHitbox(slot) return x, y, w, h end +function GridBox:getSlotCenter(slot) + local x, y, w, h = self:getSlotHitbox(slot) + + return x + (w/2), y + (h/2) +end + function GridBox:getWidgetID(slot) local widgetID if self.slots[slot] ~= nil then @@ -109,21 +115,19 @@ function GridBox:keyreleased(key, code) slotID = self:getWidgetSlot(self.widget.selected) local col, line = self.cursor.x, self.cursor.y if key == 'left' then - --self:moveCol(-1) - --self:moveCursor(col - 1, line) + self:moveCol(-1) end if key == 'right' then - --self:moveCol(1) - --self:moveCursor(col + 1, line) + self:moveCol(1) end if key == 'up' then - --self:moveCursor(col, line - 1) + self:moveLine(-1) end if key == 'down' then - --self:moveCursor(col, line + 1) + self:moveLine(1) end if key == "A" and self.widget.selected <= #self.widget.list then @@ -131,6 +135,48 @@ function GridBox:keyreleased(key, code) end end +function GridBox:moveCol(direction) + local orig_x, orig_y = self:getSlotCenter(self.widget.selected) + local distance = self.w -- on met directement à la distance max possible le système + local nearastWidget = 0 + for i,v in ipairs(self.slots) do + local xx, yy = self:getSlotCenter(i) + -- On commence par vérifier si le slot est bien positionné par rapport au + -- widget de base + if utils.math.sign(xx - orig_x) == direction then + if utils.math.pointDistance(orig_x, orig_y, xx, yy) < distance then + distance = utils.math.pointDistance(orig_x, orig_y, xx, yy) + nearestWidget = v.widgetID + end + end + end + + if nearestWidget ~= 0 then + self.widget.selected = nearestWidget + end +end + +function GridBox:moveLine(direction) + local orig_x, orig_y = self:getSlotCenter(self.widget.selected) + local distance = self.h -- on met directement à la distance max possible le système + local nearastWidget = 0 + for i,v in ipairs(self.slots) do + local xx, yy = self:getSlotCenter(i) + -- On commence par vérifier si le slot est bien positionné par rapport au + -- widget de base + if utils.math.sign(yy - orig_y) == direction then + if utils.math.pointDistance(orig_x, orig_y, xx, yy) < distance then + distance = utils.math.pointDistance(orig_x, orig_y, xx, yy) + nearestWidget = v.widgetID + end + end + end + + if nearestWidget ~= 0 then + self.widget.selected = nearestWidget + end +end + function GridBox:mousemoved(x, y) local widgetID = self:getWidgetAtPoint(x, y)