2019-02-10 11:56:45 +01:00
|
|
|
local cwd = (...):gsub('%.flowbox$', '') .. "."
|
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 FlowBox = Menu:extend()
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-12 18:52:38 +01:00
|
|
|
local menuutils = require(cwd .. "widgets.utils")
|
|
|
|
|
2019-02-11 19:53:34 +01:00
|
|
|
function FlowBox:new(menusystem, name, x, y, w, h, slots_hor, slots_vert)
|
2019-02-13 17:44:59 +01:00
|
|
|
self.view = {}
|
|
|
|
self.view.slotNumber = slots_hor * slots_vert
|
|
|
|
self.view.lineNumber = slots_vert
|
|
|
|
self.view.colNumber = slots_hor
|
|
|
|
self.view.firstSlot = 1
|
2019-02-12 19:27:02 +01:00
|
|
|
FlowBox.super.new(self, menusystem, name, x, y, w, h)
|
2019-02-10 14:34:04 +01:00
|
|
|
self.widget.h = math.floor( self.h / slots_vert )
|
|
|
|
self.widget.w = math.floor( self.w / slots_hor )
|
|
|
|
self.h = slots_vert * self.widget.h -- On fait en sorte que la hauteur
|
|
|
|
self.w = slots_hor * 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
|
|
|
|
end
|
|
|
|
|
2019-02-11 19:59:13 +01:00
|
|
|
function FlowBox:updateWidgetSize()
|
2019-02-13 17:44:59 +01:00
|
|
|
self.widget.h = math.floor( self.h / self.view.lineNumber )
|
|
|
|
self.widget.w = math.floor( self.w / self.view.colNumber )
|
2019-02-10 21:05:31 +01:00
|
|
|
end
|
|
|
|
|
2019-02-10 11:56:45 +01:00
|
|
|
function FlowBox:update(dt)
|
2019-02-10 14:28:08 +01:00
|
|
|
local col, line = self:getCoord(self.widget.selected)
|
2019-02-13 17:44:59 +01:00
|
|
|
local begincol, beginline = self:getCoord(self.view.firstSlot)
|
2019-02-10 11:56:45 +01:00
|
|
|
|
|
|
|
if line < beginline then
|
|
|
|
beginline = line
|
|
|
|
end
|
|
|
|
|
2019-02-13 17:44:59 +01:00
|
|
|
if line > beginline + self.view.lineNumber - 1 then
|
|
|
|
beginline = line - self.view.lineNumber + 1
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if beginline < 0 then
|
|
|
|
beginline = 0
|
|
|
|
end
|
|
|
|
|
2019-02-13 17:44:59 +01:00
|
|
|
self.view.firstSlot = beginline * self.view.colNumber + 1
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function FlowBox:getCoord(id_selected)
|
|
|
|
id_selected = id_selected - 1 -- On simplifie les calcul en prenant 0 comme départ
|
|
|
|
local line, col
|
2019-02-13 17:44:59 +01:00
|
|
|
line = math.floor(id_selected / self.view.colNumber)
|
|
|
|
col = id_selected - (line * self.view.colNumber)
|
2019-02-10 11:56:45 +01:00
|
|
|
return col, line
|
|
|
|
end
|
|
|
|
|
|
|
|
function FlowBox:moveCursor(new_col, new_line)
|
2019-02-10 14:28:08 +01:00
|
|
|
local col, line = self:getCoord(self.widget.selected)
|
|
|
|
local lastcol, lastline = self:getCoord(#self.widget.list)
|
2019-02-10 11:56:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
if new_line < 0 then
|
|
|
|
new_line = lastline
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_line > lastline then
|
|
|
|
new_line = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
if (new_line == lastline) then
|
|
|
|
if new_col < 0 then
|
|
|
|
new_col = lastcol
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_col > lastcol then
|
|
|
|
if (line == lastline) then
|
|
|
|
new_col = 0
|
|
|
|
else
|
|
|
|
new_col = lastcol
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if new_col < 0 then
|
2019-02-13 17:44:59 +01:00
|
|
|
new_col = self.view.colNumber - 1
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
2019-02-13 17:44:59 +01:00
|
|
|
if new_col == self.view.colNumber then
|
2019-02-10 11:56:45 +01:00
|
|
|
new_col = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-13 17:44:59 +01:00
|
|
|
self.widget.selected = (new_line * self.view.colNumber) + new_col + 1
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function FlowBox:keyreleased(key, code)
|
2019-02-10 14:28:08 +01:00
|
|
|
local col, line = self:getCoord(self.widget.selected)
|
2019-02-10 11:56:45 +01:00
|
|
|
if key == 'left' then
|
|
|
|
self:moveCursor(col - 1, line)
|
|
|
|
end
|
|
|
|
|
|
|
|
if key == 'right' then
|
|
|
|
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" then
|
2019-02-12 18:58:26 +01:00
|
|
|
if (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 FlowBox:mousemoved(x, y)
|
2019-02-10 14:28:08 +01:00
|
|
|
local col, line = self:getCoord(self.widget.selected)
|
2019-02-13 17:44:59 +01:00
|
|
|
local begincol, beginline = self:getCoord(self.view.firstSlot)
|
2019-02-12 18:15:17 +01:00
|
|
|
local newcol, newline, widget_selected
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-10 14:34:04 +01:00
|
|
|
newline = beginline + math.floor(y / self.widget.h)
|
|
|
|
newcol = math.floor(x / self.widget.w)
|
2019-02-13 17:44:59 +01:00
|
|
|
widget_selected = (newline * self.view.colNumber) + newcol + 1
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-12 18:15:17 +01:00
|
|
|
if widget_selected >= 1 and widget_selected <= #self.widget.list then
|
|
|
|
self.widget.selected = widget_selected
|
2019-02-12 18:31:32 +01:00
|
|
|
self:getFocus()
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function FlowBox:mousepressed(x, y, button, isTouch)
|
2019-02-10 14:28:08 +01:00
|
|
|
local col, line = self:getCoord(self.widget.selected)
|
2019-02-13 17:44:59 +01:00
|
|
|
local begincol, beginline = self:getCoord(self.view.firstSlot)
|
2019-02-12 18:15:17 +01:00
|
|
|
local newline, newcol, widget_selected
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-10 14:34:04 +01:00
|
|
|
newline = beginline + math.floor(y / self.widget.h)
|
|
|
|
newcol = math.floor(x / self.widget.w)
|
2019-02-13 17:44:59 +01:00
|
|
|
widget_selected = (newline * self.view.colNumber) + newcol + 1
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-12 18:15:17 +01:00
|
|
|
if widget_selected >= 1 and widget_selected <= #self.widget.list then
|
|
|
|
self.widget.selected = widget_selected
|
2019-02-12 18:31:32 +01:00
|
|
|
self:getFocus()
|
2019-02-10 14:28:08 +01:00
|
|
|
self.widget.list[self.widget.selected]:action()
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
2019-02-12 18:31:32 +01:00
|
|
|
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function FlowBox:draw()
|
|
|
|
local widgety = self.y
|
|
|
|
local widgetx = self.x
|
2019-02-10 14:28:08 +01:00
|
|
|
for i,v in ipairs(self.widget.list) do
|
2019-02-13 17:44:59 +01:00
|
|
|
if (i >= self.view.firstSlot) and (i < self.view.firstSlot + self.view.slotNumber) then
|
2019-02-10 14:34:04 +01:00
|
|
|
v:draw(widgetx, widgety, self.widget.w, self.widget.h)
|
2019-02-12 18:31:32 +01:00
|
|
|
if self.widget.selected == i and self:haveFocus() == true then
|
2019-02-10 14:34:04 +01:00
|
|
|
v:drawSelected(widgetx, widgety, self.widget.w, self.widget.h)
|
2019-02-10 11:56:45 +01:00
|
|
|
else
|
2019-02-10 14:34:04 +01:00
|
|
|
v:draw(widgetx, widgety, self.widget.w, self.widget.h)
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
2019-02-10 14:34:04 +01:00
|
|
|
widgetx = widgetx + self.widget.w
|
2019-02-10 11:56:45 +01:00
|
|
|
if widgetx == (self.x + self.w) then
|
|
|
|
widgetx = self.x
|
2019-02-10 14:34:04 +01:00
|
|
|
widgety = widgety + self.widget.h
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-12 18:52:38 +01:00
|
|
|
function FlowBox:drawCursor()
|
2019-02-12 18:58:26 +01:00
|
|
|
if (self.widget.selected >= 1 and self.widget.selected <= #self.widget.list) then
|
|
|
|
local w, h = self:getWidgetSize()
|
|
|
|
local col, line = self:getCoord(self.widget.selected)
|
|
|
|
local x = (col) * h
|
|
|
|
local y = (line) * h
|
|
|
|
menuutils.drawCursor(self.x + x, self.y + y, w, h)
|
|
|
|
end
|
2019-02-12 18:52:38 +01:00
|
|
|
end
|
|
|
|
|
2019-02-10 11:56:45 +01:00
|
|
|
return FlowBox
|