core/menusystem: add cursor in gridbox

This commit is contained in:
Kazhnuz 2019-02-13 19:47:00 +01:00
parent fe7f4b1f87
commit 92bea8c2cd

View file

@ -3,13 +3,15 @@ local Menu = require(cwd .. "parent")
local GridBox = Menu:extend()
local menuutils = require(cwd .. "widgets.utils")
function GridBox:new(menusystem, name, x, y, w, h, colNumber, lineNumber)
self.view = {}
self.view.slotNumber = colNumber * lineNumber
self.view.colNumber = colNumber
self.view.lineNumber = lineNumber
self.view.firstSlot = 1
GridBox.super.new(self, menusystem, name, x, y, w, h)
self.begin = 1
self.h = lineNumber * self.widget.h -- On fait en sorte que la hauteur
self.w = colNumber * self.widget.w -- et la largeur
-- soit un multiple du nombre de slot et de leur dimensions
@ -39,6 +41,19 @@ function GridBox:getWidgetSize(id)
return self.widget.w * self.slots[slot].width, self.widget.h * self.slots[slot].height
end
function GridBox:getWidgetID(slot)
if (self.slots[slot].isChild == 0) then
return self.slots[slot].widgetID
else
return self.slots[self.slots[slot].isChild].widgetID
end
end
function GridBox:haveWidget(slot)
local id = self:getWidgetID(slot)
return self.widget.list[id] ~= nil
end
function GridBox:getWidgetSlot(widgetID)
local slot = 0
for i,v in ipairs(self.slots) do
@ -51,7 +66,7 @@ function GridBox:getWidgetSlot(widgetID)
end
function GridBox:update(dt)
self.begin = 1
self.view.firstSlot = 1
local slotID = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
if self.slots[slotID].isChild > 0 then
slotID = self.slots[slotID].isChild
@ -173,14 +188,21 @@ end
function GridBox:mousemoved(x, y)
local col, line = self:getCoord(self.widget.selected)
local begincol, beginline = self:getCoord(self.begin)
local begincol, beginline = self:getCoord(self.view.firstSlot)
local newcol, newline
local newselect, slotID
newline = beginline + math.floor(y / self.widget.h)
newcol = math.floor(x / self.widget.w)
self.cursor.x = newcol
self.cursor.y = newline
newselect = (newline * self.view.colNumber) + newcol + 1
if self:haveWidget(newselect) then
self.cursor.x = newcol
self.cursor.y = newline
end
print(self:haveWidget(newselect), newselect)
self:getFocus()
if self.widget.selected < 1 then
self.widget.selected = 1
@ -193,7 +215,7 @@ end
function GridBox:mousepressed(x, y, button, isTouch)
local col, line = self:getCoord(self.widget.selected)
local begincol, beginline = self:getCoord(self.begin)
local begincol, beginline = self:getCoord(self.view.firstSlot)
local newcol, newline
local newselect, slotID
@ -201,6 +223,8 @@ function GridBox:mousepressed(x, y, button, isTouch)
newcol = math.floor(x / self.widget.w)
newselect = (newline * self.view.colNumber) + newcol + 1
self:getFocus()
if self.slots[newselect].isChild > 0 then
slotID = self.slots[newselect].isChild
else
@ -249,4 +273,16 @@ function GridBox:draw()
end
end
function GridBox:drawCursor()
self:updateView()
local begincol, beginline = self:getCoord(self.view.firstSlot)
if (self.widget.selected >= 1 and self.widget.selected <= #self.widget.list) then
local w, h = self:getWidgetSize(self.widget.selected)
local col, line = self:getCoord(self.widget.selected)
local x = (col) * h
local y = (line - beginline) * h
menuutils.drawCursor(self.x + x, self.y + y, w, h)
end
end
return GridBox