2019-02-10 11:56:45 +01:00
|
|
|
local cwd = (...):gsub('%.grid$', '') .. "."
|
2019-02-10 12:00:02 +01:00
|
|
|
local Menu = require(cwd .. "parent")
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-12 19:27:02 +01:00
|
|
|
local GridBox = Menu:extend()
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-13 19:47:00 +01:00
|
|
|
local menuutils = require(cwd .. "widgets.utils")
|
|
|
|
|
2019-02-13 18:25:41 +01:00
|
|
|
function GridBox:new(menusystem, name, x, y, w, h, colNumber, lineNumber)
|
2019-02-13 18:50:06 +01:00
|
|
|
self.view = {}
|
2019-02-13 19:07:44 +01:00
|
|
|
self.view.slotNumber = colNumber * lineNumber
|
|
|
|
self.view.colNumber = colNumber
|
|
|
|
self.view.lineNumber = lineNumber
|
2019-02-13 19:47:00 +01:00
|
|
|
self.view.firstSlot = 1
|
2019-02-11 20:04:24 +01:00
|
|
|
GridBox.super.new(self, menusystem, name, x, y, w, h)
|
2019-02-13 18:25:41 +01:00
|
|
|
self.h = lineNumber * self.widget.h -- On fait en sorte que la hauteur
|
2019-02-13 19:07:44 +01:00
|
|
|
self.w = colNumber * self.widget.w -- et la largeur
|
2019-02-10 11:56:45 +01:00
|
|
|
-- 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.
|
2019-02-13 18:25:41 +01:00
|
|
|
self.slots = {}
|
2019-02-14 18:32:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function GridBox:addSlot(widgetID, x, y, w, h)
|
|
|
|
local slot = {}
|
|
|
|
slot.x = x
|
|
|
|
slot.y = y
|
|
|
|
slot.w = w
|
|
|
|
slot.h = h
|
|
|
|
slot.widgetID = widgetID
|
|
|
|
|
|
|
|
table.insert(self.slots, slot)
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
2019-02-11 19:56:36 +01:00
|
|
|
function GridBox:updateWidgetSize()
|
2019-02-13 19:07:44 +01:00
|
|
|
self.widget.h = math.floor( self.h / self.view.lineNumber )
|
2019-02-13 18:25:41 +01:00
|
|
|
self.widget.w = math.floor( self.w / self.view.colNumber )
|
2019-02-10 21:05:31 +01:00
|
|
|
end
|
|
|
|
|
2019-02-13 18:35:13 +01:00
|
|
|
function GridBox:getWidgetSize(id)
|
|
|
|
local slot = self:getWidgetSlot(id)
|
2019-02-14 18:32:52 +01:00
|
|
|
if slot == 0 then
|
|
|
|
return 1, 1
|
|
|
|
else
|
|
|
|
return self.widget.w * self.slots[slot].w, self.widget.h * self.slots[slot].h
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function GridBox:getSlotHitbox(slot)
|
|
|
|
local x, y, w, h
|
|
|
|
x = self.slots[slot].x * self.widget.w
|
|
|
|
y = self.slots[slot].y * self.widget.h
|
|
|
|
w = self.slots[slot].w * self.widget.w
|
|
|
|
h = self.slots[slot].h * self.widget.h
|
|
|
|
|
|
|
|
return x, y, w, h
|
2019-02-13 18:35:13 +01:00
|
|
|
end
|
|
|
|
|
2019-02-14 20:47:56 +01:00
|
|
|
function GridBox:getSlotCenter(slot)
|
|
|
|
local x, y, w, h = self:getSlotHitbox(slot)
|
|
|
|
|
|
|
|
return x + (w/2), y + (h/2)
|
|
|
|
end
|
|
|
|
|
2019-02-13 19:47:00 +01:00
|
|
|
function GridBox:getWidgetID(slot)
|
2019-02-14 18:32:52 +01:00
|
|
|
local widgetID
|
|
|
|
if self.slots[slot] ~= nil then
|
|
|
|
widgetID = self.slots[slot].widgetID
|
2019-02-13 19:47:00 +01:00
|
|
|
else
|
2019-02-14 18:32:52 +01:00
|
|
|
widgetID = 0
|
2019-02-13 19:47:00 +01:00
|
|
|
end
|
2019-02-14 18:32:52 +01:00
|
|
|
return widgetID
|
2019-02-13 19:47:00 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function GridBox:haveWidget(slot)
|
|
|
|
local id = self:getWidgetID(slot)
|
|
|
|
return self.widget.list[id] ~= nil
|
|
|
|
end
|
|
|
|
|
2019-02-13 18:35:13 +01:00
|
|
|
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
|
|
|
|
|
2019-02-14 18:32:52 +01:00
|
|
|
function GridBox:getWidgetAtPoint(x, y)
|
|
|
|
local x = x or 0
|
|
|
|
local y = y or 0
|
|
|
|
local widgetID = nil
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-13 18:25:41 +01:00
|
|
|
for i,v in ipairs(self.slots) do
|
2019-02-14 18:32:52 +01:00
|
|
|
local xx, yy, ww, hh = self:getSlotHitbox(i)
|
|
|
|
if (x >= xx) and (y >= yy) and (x < xx + ww) and (y < yy + hh) then
|
|
|
|
widgetID = v.widgetID
|
2019-02-13 22:11:04 +01:00
|
|
|
end
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
2019-02-14 18:32:52 +01:00
|
|
|
return widgetID
|
2019-02-13 22:11:04 +01:00
|
|
|
end
|
|
|
|
|
2019-02-14 18:32:52 +01:00
|
|
|
function GridBox:update(dt)
|
|
|
|
self.view.firstSlot = 1
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function GridBox:keyreleased(key, code)
|
2019-02-14 18:32:52 +01:00
|
|
|
slotID = self:getWidgetSlot(self.widget.selected)
|
2019-02-10 11:56:45 +01:00
|
|
|
local col, line = self.cursor.x, self.cursor.y
|
|
|
|
if key == 'left' then
|
2019-02-14 20:47:56 +01:00
|
|
|
self:moveCol(-1)
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if key == 'right' then
|
2019-02-14 20:47:56 +01:00
|
|
|
self:moveCol(1)
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if key == 'up' then
|
2019-02-14 20:47:56 +01:00
|
|
|
self:moveLine(-1)
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if key == 'down' then
|
2019-02-14 20:47:56 +01:00
|
|
|
self:moveLine(1)
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
2019-02-10 14:28:08 +01:00
|
|
|
if key == "A" and self.widget.selected <= #self.widget.list then
|
|
|
|
self.widget.list[self.widget.selected]:action()
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-14 20:47:56 +01:00
|
|
|
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
|
|
|
|
|
2019-02-10 11:56:45 +01:00
|
|
|
function GridBox:mousemoved(x, y)
|
2019-02-14 18:32:52 +01:00
|
|
|
local widgetID = self:getWidgetAtPoint(x, y)
|
2019-02-13 19:47:00 +01:00
|
|
|
|
2019-02-14 18:32:52 +01:00
|
|
|
if widgetID ~= nil then
|
|
|
|
self.widget.selected = widgetID
|
|
|
|
self:getFocus()
|
|
|
|
end
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-10 14:28:08 +01:00
|
|
|
if self.widget.selected < 1 then
|
|
|
|
self.widget.selected = 1
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
2019-02-10 14:28:08 +01:00
|
|
|
if self.widget.selected > #self.widget.list then
|
|
|
|
self.widget.selected = #self.widget.list
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function GridBox:mousepressed(x, y, button, isTouch)
|
2019-02-14 18:32:52 +01:00
|
|
|
local widgetID = self:getWidgetAtPoint(x, y)
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-14 18:32:52 +01:00
|
|
|
if widgetID ~= nil then
|
|
|
|
self.widget.selected = widgetID
|
|
|
|
self:getFocus()
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-14 18:32:52 +01:00
|
|
|
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
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function GridBox:draw()
|
|
|
|
|
2019-02-13 18:25:41 +01:00
|
|
|
for i,v in ipairs(self.slots) do
|
2019-02-14 18:32:52 +01:00
|
|
|
if self:haveWidget(i) then
|
|
|
|
local widgetx = self.x + (v.x * self.widget.w)
|
|
|
|
local widgety = self.y + (v.y * self.widget.h)
|
2019-02-12 18:31:32 +01:00
|
|
|
if self.widget.selected == v.widgetID and self:haveFocus() == true then
|
2019-02-14 18:32:52 +01:00
|
|
|
self.widget.list[v.widgetID]:drawSelected(widgetx, widgety)
|
2019-02-10 11:56:45 +01:00
|
|
|
else
|
2019-02-14 18:32:52 +01:00
|
|
|
self.widget.list[v.widgetID]:draw(widgetx, widgety)
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-13 19:47:00 +01:00
|
|
|
function GridBox:drawCursor()
|
|
|
|
self:updateView()
|
|
|
|
if (self.widget.selected >= 1 and self.widget.selected <= #self.widget.list) then
|
2019-02-14 18:32:52 +01:00
|
|
|
local slot = self:getWidgetSlot(self.widget.selected)
|
|
|
|
local w, h = self:getWidgetSize(slot)
|
|
|
|
local x = self.slots[slot].x * self.widget.w
|
|
|
|
local y = self.slots[slot].y * self.widget.h
|
2019-02-13 19:47:00 +01:00
|
|
|
menuutils.drawCursor(self.x + x, self.y + y, w, h)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-10 11:56:45 +01:00
|
|
|
return GridBox
|