core/menusystem: restore keyboard navigation support in grid system
This commit is contained in:
parent
10f5b05eca
commit
6b9bbc4286
1 changed files with 52 additions and 6 deletions
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue