core/menusystem: use a more logical naming scheme for variables in grids
This commit is contained in:
parent
ce5b4ea20b
commit
8c53f69663
1 changed files with 47 additions and 47 deletions
|
@ -3,14 +3,14 @@ local Menu = require(cwd .. "parent")
|
||||||
|
|
||||||
local GridBox = Menu:extend()
|
local GridBox = Menu:extend()
|
||||||
|
|
||||||
function GridBox:new(menusystem, name, x, y, w, h, slots_hor, slots_vert)
|
function GridBox:new(menusystem, name, x, y, w, h, colNumber, lineNumber)
|
||||||
self.slots = slots_hor * slots_vert
|
self.view.slotNumber = colNumber * lineNumber
|
||||||
self.slots_hor = slots_hor
|
self.view.colNumber = colNumber
|
||||||
self.slots_vert = slots_vert
|
self.view.colNumber = lineNumber
|
||||||
GridBox.super.new(self, menusystem, name, x, y, w, h)
|
GridBox.super.new(self, menusystem, name, x, y, w, h)
|
||||||
self.begin = 1
|
self.begin = 1
|
||||||
self.h = slots_vert * self.widget.h -- On fait en sorte que la hauteur
|
self.h = lineNumber * self.widget.h -- On fait en sorte que la hauteur
|
||||||
self.w = slots_hor * self.widget.w -- et la largeur
|
self.w = colNumber * self.widget.w -- et la largeur
|
||||||
-- soit un multiple du nombre de slot et de leur dimensions
|
-- soit un multiple du nombre de slot et de leur dimensions
|
||||||
self.cursor = {}
|
self.cursor = {}
|
||||||
self.cursor.x = 0
|
self.cursor.x = 0
|
||||||
|
@ -18,36 +18,36 @@ function GridBox:new(menusystem, name, x, y, w, h, slots_hor, slots_vert)
|
||||||
|
|
||||||
-- La gridbox possède la particularité de pouvoir fusioner des slots, on fait
|
-- La gridbox possède la particularité de pouvoir fusioner des slots, on fait
|
||||||
-- donc une liste de slots disponibles, qui serviront par la suite.
|
-- donc une liste de slots disponibles, qui serviront par la suite.
|
||||||
self.listSlot = {}
|
self.slots = {}
|
||||||
for i= 1, self.slots do
|
for i= 1, self.view.slotNumber do
|
||||||
self.listSlot[i] = {}
|
self.slots[i] = {}
|
||||||
self.listSlot[i].sizeH = 1
|
self.slots[i].height = 1
|
||||||
self.listSlot[i].sizeW = 1
|
self.slots[i].width = 1
|
||||||
self.listSlot[i].isChild = 0
|
self.slots[i].isChild = 0
|
||||||
self.listSlot[i].widgetID = i
|
self.slots[i].widgetID = i
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function GridBox:updateWidgetSize()
|
function GridBox:updateWidgetSize()
|
||||||
self.widget.h = math.floor( self.h / self.slots_vert )
|
self.widget.h = math.floor( self.h / self.view.colNumber )
|
||||||
self.widget.w = math.floor( self.w / self.slots_hor )
|
self.widget.w = math.floor( self.w / self.view.colNumber )
|
||||||
end
|
end
|
||||||
|
|
||||||
function GridBox:update(dt)
|
function GridBox:update(dt)
|
||||||
self.begin = 1
|
self.begin = 1
|
||||||
local slotID = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
|
local slotID = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
|
||||||
if self.listSlot[slotID].isChild > 0 then
|
if self.slots[slotID].isChild > 0 then
|
||||||
slotID = self.listSlot[slotID].isChild
|
slotID = self.slots[slotID].isChild
|
||||||
end
|
end
|
||||||
self.widget.selected = self.listSlot[slotID].widgetID
|
self.widget.selected = self.slots[slotID].widgetID
|
||||||
self.cursor.x, self.cursor.y = self:getCoord(slotID)
|
self.cursor.x, self.cursor.y = self:getCoord(slotID)
|
||||||
end
|
end
|
||||||
|
|
||||||
function GridBox:regenSlots()
|
function GridBox:regenSlots()
|
||||||
local widgetID = 1
|
local widgetID = 1
|
||||||
for i,v in ipairs(self.listSlot) do
|
for i,v in ipairs(self.slots) do
|
||||||
if v.isChild == 0 and (widgetID <= #self.widget.list) then
|
if v.isChild == 0 and (widgetID <= #self.widget.list) then
|
||||||
self.listSlot[i].widgetID = widgetID
|
self.slots[i].widgetID = widgetID
|
||||||
widgetID = widgetID + 1
|
widgetID = widgetID + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -55,41 +55,41 @@ end
|
||||||
|
|
||||||
function GridBox:addCol(slotID)
|
function GridBox:addCol(slotID)
|
||||||
local col, line = self:getCoord(slotID)
|
local col, line = self:getCoord(slotID)
|
||||||
if (col + self.listSlot[slotID].sizeW + 1) <= self.slots_hor then
|
if (col + self.slots[slotID].width + 1) <= self.view.colNumber then
|
||||||
slotChild = slotID + self.listSlot[slotID].sizeW
|
slotChild = slotID + self.slots[slotID].width
|
||||||
for i = 1, self.listSlot[slotID].sizeH do
|
for i = 1, self.slots[slotID].height do
|
||||||
self.listSlot[slotChild + ((i-1)* self.slots_hor)].isChild = slotID
|
self.slots[slotChild + ((i-1)* self.view.colNumber)].isChild = slotID
|
||||||
end
|
end
|
||||||
self.listSlot[slotID].sizeW = self.listSlot[slotID].sizeW + 1
|
self.slots[slotID].width = self.slots[slotID].width + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function GridBox:addLine(slotID)
|
function GridBox:addLine(slotID)
|
||||||
local col, line = self:getCoord(slotID)
|
local col, line = self:getCoord(slotID)
|
||||||
if (line + self.listSlot[slotID].sizeH + 1) <= self.slots_vert then
|
if (line + self.slots[slotID].height + 1) <= self.view.colNumber then
|
||||||
slotChild = slotID + (self.listSlot[slotID].sizeH * self.slots_hor)
|
slotChild = slotID + (self.slots[slotID].height * self.view.colNumber)
|
||||||
for i = 1, self.listSlot[slotID].sizeW do
|
for i = 1, self.slots[slotID].width do
|
||||||
self.listSlot[slotChild + (i-1)].isChild = slotID
|
self.slots[slotChild + (i-1)].isChild = slotID
|
||||||
end
|
end
|
||||||
self.listSlot[slotID].sizeH = self.listSlot[slotID].sizeH + 1
|
self.slots[slotID].height = self.slots[slotID].height + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function GridBox:getCoord(id_selected)
|
function GridBox:getCoord(id_selected)
|
||||||
id_selected = id_selected - 1 -- On simplifie les calcul en prenant 0 comme départ
|
id_selected = id_selected - 1 -- On simplifie les calcul en prenant 0 comme départ
|
||||||
local line, col
|
local line, col
|
||||||
line = math.floor(id_selected / self.slots_hor)
|
line = math.floor(id_selected / self.view.colNumber)
|
||||||
col = id_selected - (line * self.slots_hor)
|
col = id_selected - (line * self.view.colNumber)
|
||||||
return col, line
|
return col, line
|
||||||
end
|
end
|
||||||
|
|
||||||
function GridBox:getSlotbyCoord(col, line)
|
function GridBox:getSlotbyCoord(col, line)
|
||||||
return (line * self.slots_hor) + col + 1
|
return (line * self.view.colNumber) + col + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
function GridBox:getSlot(widgetID)
|
function GridBox:getSlot(widgetID)
|
||||||
local slotID
|
local slotID
|
||||||
for i,v in ipairs(self.listSlot) do
|
for i,v in ipairs(self.slots) do
|
||||||
if v.widgetID == widgetID then
|
if v.widgetID == widgetID then
|
||||||
return i
|
return i
|
||||||
end
|
end
|
||||||
|
@ -103,25 +103,25 @@ function GridBox:moveCursor(newcol, newline)
|
||||||
self.cursor.x, self.cursor.y = newcol, newline
|
self.cursor.x, self.cursor.y = newcol, newline
|
||||||
|
|
||||||
while self.cursor.y < 0 do
|
while self.cursor.y < 0 do
|
||||||
self.cursor.y = self.cursor.y + self.slots_vert
|
self.cursor.y = self.cursor.y + self.view.colNumber
|
||||||
end
|
end
|
||||||
|
|
||||||
while self.cursor.x < 0 do
|
while self.cursor.x < 0 do
|
||||||
self.cursor.x = self.cursor.x + self.slots_hor
|
self.cursor.x = self.cursor.x + self.view.colNumber
|
||||||
end
|
end
|
||||||
|
|
||||||
while self.cursor.y >= self.slots_vert do
|
while self.cursor.y >= self.view.colNumber do
|
||||||
self.cursor.y = self.cursor.y - self.slots_vert
|
self.cursor.y = self.cursor.y - self.view.colNumber
|
||||||
end
|
end
|
||||||
|
|
||||||
while self.cursor.x >= self.slots_hor do
|
while self.cursor.x >= self.view.colNumber do
|
||||||
self.cursor.x = self.cursor.x - self.slots_hor
|
self.cursor.x = self.cursor.x - self.view.colNumber
|
||||||
end
|
end
|
||||||
previousSlot = self:getSlotbyCoord(col, line)
|
previousSlot = self:getSlotbyCoord(col, line)
|
||||||
newSlot = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
|
newSlot = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
|
||||||
|
|
||||||
if (self.listSlot[newSlot].isChild > 0) or (self.listSlot[newSlot].widgetID > #self.widget.list) then
|
if (self.slots[newSlot].isChild > 0) or (self.slots[newSlot].widgetID > #self.widget.list) then
|
||||||
if (self.listSlot[newSlot].isChild == previousSlot) or (self.listSlot[newSlot].widgetID > #self.widget.list) then
|
if (self.slots[newSlot].isChild == previousSlot) or (self.slots[newSlot].widgetID > #self.widget.list) then
|
||||||
self:moveCursor(self.cursor.x + relcol, self.cursor.y + relline)
|
self:moveCursor(self.cursor.x + relcol, self.cursor.y + relline)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -182,15 +182,15 @@ function GridBox:mousepressed(x, y, button, isTouch)
|
||||||
|
|
||||||
newline = beginline + math.floor(y / self.widget.h)
|
newline = beginline + math.floor(y / self.widget.h)
|
||||||
newcol = math.floor(x / self.widget.w)
|
newcol = math.floor(x / self.widget.w)
|
||||||
newselect = (newline * self.slots_hor) + newcol + 1
|
newselect = (newline * self.view.colNumber) + newcol + 1
|
||||||
|
|
||||||
if self.listSlot[newselect].isChild > 0 then
|
if self.slots[newselect].isChild > 0 then
|
||||||
slotID = self.listSlot[newselect].isChild
|
slotID = self.slots[newselect].isChild
|
||||||
else
|
else
|
||||||
slotID = newselect
|
slotID = newselect
|
||||||
end
|
end
|
||||||
|
|
||||||
self.widget.selected = self.listSlot[slotID].widgetID
|
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
|
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()
|
self.widget.list[self.widget.selected]:action()
|
||||||
|
@ -202,7 +202,7 @@ function GridBox:draw()
|
||||||
local widgetx = self.x
|
local widgetx = self.x
|
||||||
|
|
||||||
self:regenSlots() -- On reget les slots au cas où :p
|
self:regenSlots() -- On reget les slots au cas où :p
|
||||||
for i,v in ipairs(self.listSlot) do
|
for i,v in ipairs(self.slots) do
|
||||||
if (v.isChild == 0) and (v.widgetID <= #self.widget.list) then
|
if (v.isChild == 0) and (v.widgetID <= #self.widget.list) then
|
||||||
--self.widget.list[v.widgetID]:draw(widgetx, widgety, self.widget.w * v.sizeW, self.widget.h * v.sizeH)
|
--self.widget.list[v.widgetID]:draw(widgetx, widgety, self.widget.w * v.sizeW, self.widget.h * v.sizeH)
|
||||||
if self.widget.selected == v.widgetID and self:haveFocus() == true then
|
if self.widget.selected == v.widgetID and self:haveFocus() == true then
|
||||||
|
|
Loading…
Reference in a new issue