313 lines
8.9 KiB
Lua
313 lines
8.9 KiB
Lua
local cwd = (...):gsub('%.grid$', '') .. "."
|
|
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.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
|
|
self.cursor = {}
|
|
self.cursor.x = 0
|
|
self.cursor.y = 0
|
|
|
|
-- La gridbox possède la particularité de pouvoir fusioner des slots, on fait
|
|
-- donc une liste de slots disponibles, qui serviront par la suite.
|
|
self.slots = {}
|
|
for i= 1, self.view.slotNumber do
|
|
self.slots[i] = {}
|
|
self.slots[i].height = 1
|
|
self.slots[i].width = 1
|
|
self.slots[i].parent = 0
|
|
self.slots[i].widgetID = i
|
|
end
|
|
end
|
|
|
|
function GridBox:updateWidgetSize()
|
|
self.widget.h = math.floor( self.h / self.view.lineNumber )
|
|
self.widget.w = math.floor( self.w / self.view.colNumber )
|
|
end
|
|
|
|
function GridBox:getWidgetSize(id)
|
|
local slot = self:getWidgetSlot(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].parent == 0) then
|
|
return self.slots[slot].widgetID
|
|
else
|
|
return self.slots[self.slots[slot].parent].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
|
|
if (self.slots[i].widgetID == widgetID) then
|
|
slot = i
|
|
end
|
|
end
|
|
|
|
return slot
|
|
end
|
|
|
|
function GridBox:update(dt)
|
|
self.view.firstSlot = 1
|
|
local slotID = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
|
|
if self.slots[slotID].parent > 0 then
|
|
slotID = self.slots[slotID].parent
|
|
end
|
|
self.widget.selected = self.slots[slotID].widgetID
|
|
self.cursor.x, self.cursor.y = self:getCoord(slotID)
|
|
end
|
|
|
|
function GridBox:regenSlots()
|
|
local widgetID = 1
|
|
for i,v in ipairs(self.slots) do
|
|
if v.parent == 0 and (widgetID <= #self.widget.list) then
|
|
self.slots[i].widgetID = widgetID
|
|
widgetID = widgetID + 1
|
|
end
|
|
end
|
|
end
|
|
|
|
function GridBox:addCol(slotID)
|
|
local col, line = self:getCoord(slotID)
|
|
if (col + self.slots[slotID].width + 1) <= self.view.colNumber then
|
|
slotChild = slotID + self.slots[slotID].width
|
|
for i = 1, self.slots[slotID].height do
|
|
self.slots[slotChild + ((i-1)* self.view.colNumber)].parent = slotID
|
|
end
|
|
self.slots[slotID].width = self.slots[slotID].width + 1
|
|
end
|
|
end
|
|
|
|
function GridBox:addLine(slotID)
|
|
local col, line = self:getCoord(slotID)
|
|
if (line + self.slots[slotID].height + 1) <= self.view.colNumber then
|
|
slotChild = slotID + (self.slots[slotID].height * self.view.colNumber)
|
|
for i = 1, self.slots[slotID].width do
|
|
self.slots[slotChild + (i-1)].parent = slotID
|
|
end
|
|
self.slots[slotID].height = self.slots[slotID].height + 1
|
|
end
|
|
end
|
|
|
|
function GridBox:getCoord(id_selected)
|
|
id_selected = id_selected - 1 -- On simplifie les calcul en prenant 0 comme départ
|
|
local line, col
|
|
line = math.floor(id_selected / self.view.colNumber)
|
|
col = id_selected - (line * self.view.colNumber)
|
|
return col, line
|
|
end
|
|
|
|
function GridBox:getSlotbyCoord(col, line)
|
|
return (line * self.view.colNumber) + col + 1
|
|
end
|
|
|
|
function GridBox:getSlot(widgetID)
|
|
local slotID
|
|
for i,v in ipairs(self.slots) do
|
|
if v.widgetID == widgetID then
|
|
return i
|
|
end
|
|
end
|
|
return 0
|
|
end
|
|
|
|
function GridBox:moveCursor(newcol, newline)
|
|
local col, line = self.cursor.x, self.cursor.y
|
|
self:moveCursorRelative(newcol - self.cursor.x, newline - self.cursor.y)
|
|
end
|
|
|
|
function GridBox:moveCursorRelative(dx, dy)
|
|
|
|
local i = 0
|
|
self.cursor.x = self.cursor.x + dx
|
|
self.cursor.y = self.cursor.y + dy
|
|
self:fixCursorOverflow()
|
|
print(self.cursor.x, self.cursor.y, self:haveValidCursorPosition())
|
|
while not(self:haveValidCursorPosition()) do
|
|
self.cursor.x = self.cursor.x + utils.math.sign(dx)
|
|
self.cursor.y = self.cursor.y + utils.math.sign(dy)
|
|
self:fixCursorOverflow()
|
|
print(self.cursor.x, self.cursor.y, self:haveValidCursorPosition())
|
|
i = i + 1
|
|
if i == 200 then
|
|
break
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
function GridBox:haveValidCursorPosition()
|
|
local newSlot = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
|
|
local isValid = true
|
|
|
|
if (self.slots[newSlot].parent > 0) or (self.slots[newSlot].widgetID > #self.widget.list) then
|
|
if (self.slots[newSlot].parent == previousSlot) or (self.slots[newSlot].widgetID > #self.widget.list) then
|
|
isValid = false
|
|
end
|
|
end
|
|
|
|
return isValid
|
|
end
|
|
|
|
function GridBox:fixCursorOverflow()
|
|
if self.cursor.x < 0 then
|
|
self.cursor.x = self.view.colNumber - 1
|
|
end
|
|
|
|
if self.cursor.x > self.view.colNumber - 1 then
|
|
self.cursor.x = 0
|
|
end
|
|
|
|
if self.cursor.y < 0 then
|
|
self.cursor.y = self.view.lineNumber - 1
|
|
end
|
|
|
|
if self.cursor.y > self.view.lineNumber - 1 then
|
|
self.cursor.y = 0
|
|
end
|
|
end
|
|
|
|
function GridBox:keyreleased(key, code)
|
|
slotID = self:getSlot(self.widget.selected)
|
|
local col, line = self.cursor.x, self.cursor.y
|
|
if key == 'left' then
|
|
--self:moveCol(-1)
|
|
self:moveCursor(col - 1, line)
|
|
end
|
|
|
|
if key == 'right' then
|
|
--self:moveCol(1)
|
|
self:moveCursor(col + 1, line)
|
|
end
|
|
|
|
if key == 'up' then
|
|
self:moveCursor(col, line - 1)
|
|
end
|
|
|
|
if key == 'down' then
|
|
self:moveCursor(col, line + 1)
|
|
end
|
|
|
|
if key == "A" and self.widget.selected <= #self.widget.list then
|
|
self.widget.list[self.widget.selected]:action()
|
|
end
|
|
end
|
|
|
|
function GridBox:mousemoved(x, y)
|
|
local col, line = self:getCoord(self.widget.selected)
|
|
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)
|
|
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
|
|
end
|
|
if self.widget.selected > #self.widget.list then
|
|
self.widget.selected = #self.widget.list
|
|
end
|
|
|
|
end
|
|
|
|
function GridBox:mousepressed(x, y, button, isTouch)
|
|
local col, line = self:getCoord(self.widget.selected)
|
|
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)
|
|
newselect = (newline * self.view.colNumber) + newcol + 1
|
|
|
|
self:getFocus()
|
|
|
|
if self.slots[newselect].parent > 0 then
|
|
slotID = self.slots[newselect].parent
|
|
else
|
|
slotID = newselect
|
|
end
|
|
|
|
self.widget.selected = self.slots[slotID].widgetID
|
|
|
|
if #self.widget.list > 0 and self.widget.selected > 1 and self.widget.selected <= #self.widget.list then
|
|
self.widget.list[self.widget.selected]:action()
|
|
end
|
|
end
|
|
|
|
function GridBox:draw()
|
|
local widgety = self.y
|
|
local widgetx = self.x
|
|
|
|
self:regenSlots() -- On reget les slots au cas où :p
|
|
for i,v in ipairs(self.slots) do
|
|
if (v.parent == 0) and (v.widgetID <= #self.widget.list) then
|
|
--self.widget.list[v.widgetID]:draw(widgetx, widgety, self.widget.w * v.width, self.widget.h * v.height)
|
|
if self.widget.selected == v.widgetID and self:haveFocus() == true then
|
|
self.widget.list[v.widgetID]:drawSelected(widgetx, widgety, self.widget.w * v.width, self.widget.h * v.height)
|
|
else
|
|
self.widget.list[v.widgetID]:draw(widgetx, widgety, self.widget.w * v.width, self.widget.h * v.height)
|
|
end
|
|
end
|
|
if (v.parent > 0) and false then
|
|
love.graphics.setColor(255,255,255,128)
|
|
love.graphics.rectangle("fill", widgetx, widgety, self.widget.w, self.widget.h)
|
|
end
|
|
local col, line = self:getCoord(i)
|
|
|
|
if (col == self.cursor.x) and (line == self.cursor.y) and false then
|
|
love.graphics.setColor(255,255,0,128)
|
|
love.graphics.rectangle("fill", widgetx, widgety, self.widget.w, self.widget.h)
|
|
end
|
|
|
|
--love.graphics.setColor(0,0,0,10)
|
|
--love.graphics.rectangle("line", widgetx, widgety, self.widget.w, self.widget.h)
|
|
widgetx = widgetx + self.widget.w
|
|
if widgetx == (self.x + self.w) then
|
|
widgetx = self.x
|
|
widgety = widgety + self.widget.h
|
|
end
|
|
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
|