core/menusystem: restore keyboard navigation support in grid system

This commit is contained in:
Kazhnuz 2019-02-14 20:47:56 +01:00
parent 10f5b05eca
commit 6b9bbc4286

View file

@ -59,6 +59,12 @@ function GridBox:getSlotHitbox(slot)
return x, y, w, h return x, y, w, h
end 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) function GridBox:getWidgetID(slot)
local widgetID local widgetID
if self.slots[slot] ~= nil then if self.slots[slot] ~= nil then
@ -109,21 +115,19 @@ function GridBox:keyreleased(key, code)
slotID = self:getWidgetSlot(self.widget.selected) slotID = self:getWidgetSlot(self.widget.selected)
local col, line = self.cursor.x, self.cursor.y local col, line = self.cursor.x, self.cursor.y
if key == 'left' then if key == 'left' then
--self:moveCol(-1) self:moveCol(-1)
--self:moveCursor(col - 1, line)
end end
if key == 'right' then if key == 'right' then
--self:moveCol(1) self:moveCol(1)
--self:moveCursor(col + 1, line)
end end
if key == 'up' then if key == 'up' then
--self:moveCursor(col, line - 1) self:moveLine(-1)
end end
if key == 'down' then if key == 'down' then
--self:moveCursor(col, line + 1) self:moveLine(1)
end end
if key == "A" and self.widget.selected <= #self.widget.list then if key == "A" and self.widget.selected <= #self.widget.list then
@ -131,6 +135,48 @@ function GridBox:keyreleased(key, code)
end end
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) function GridBox:mousemoved(x, y)
local widgetID = self:getWidgetAtPoint(x, y) local widgetID = self:getWidgetAtPoint(x, y)