core/menusystem: improve view variables names

This commit is contained in:
Kazhnuz 2019-02-13 17:44:59 +01:00
parent d352987aca
commit f2b520e2b3
2 changed files with 36 additions and 34 deletions

View file

@ -6,11 +6,12 @@ local FlowBox = Menu:extend()
local menuutils = require(cwd .. "widgets.utils") local menuutils = require(cwd .. "widgets.utils")
function FlowBox:new(menusystem, name, x, y, w, h, slots_hor, slots_vert) function FlowBox:new(menusystem, name, x, y, w, h, slots_hor, slots_vert)
self.slots = slots_hor * slots_vert self.view = {}
self.slots_hor = slots_hor self.view.slotNumber = slots_hor * slots_vert
self.slots_vert = slots_vert self.view.lineNumber = slots_vert
self.view.colNumber = slots_hor
self.view.firstSlot = 1
FlowBox.super.new(self, menusystem, name, x, y, w, h) FlowBox.super.new(self, menusystem, name, x, y, w, h)
self.begin = 1
self.widget.h = math.floor( self.h / slots_vert ) self.widget.h = math.floor( self.h / slots_vert )
self.widget.w = math.floor( self.w / slots_hor ) self.widget.w = math.floor( self.w / slots_hor )
self.h = slots_vert * self.widget.h -- On fait en sorte que la hauteur self.h = slots_vert * self.widget.h -- On fait en sorte que la hauteur
@ -19,34 +20,34 @@ function FlowBox:new(menusystem, name, x, y, w, h, slots_hor, slots_vert)
end end
function FlowBox:updateWidgetSize() function FlowBox:updateWidgetSize()
self.widget.h = math.floor( self.h / self.slots_vert ) self.widget.h = math.floor( self.h / self.view.lineNumber )
self.widget.w = math.floor( self.w / self.slots_hor ) self.widget.w = math.floor( self.w / self.view.colNumber )
end end
function FlowBox:update(dt) function FlowBox:update(dt)
local col, line = self:getCoord(self.widget.selected) local col, line = self:getCoord(self.widget.selected)
local begincol, beginline = self:getCoord(self.begin) local begincol, beginline = self:getCoord(self.view.firstSlot)
if line < beginline then if line < beginline then
beginline = line beginline = line
end end
if line > beginline + self.slots_vert - 1 then if line > beginline + self.view.lineNumber - 1 then
beginline = line - self.slots_vert + 1 beginline = line - self.view.lineNumber + 1
end end
if beginline < 0 then if beginline < 0 then
beginline = 0 beginline = 0
end end
self.begin = beginline * self.slots_hor + 1 self.view.firstSlot = beginline * self.view.colNumber + 1
end end
function FlowBox:getCoord(id_selected) function FlowBox: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
@ -77,15 +78,15 @@ function FlowBox:moveCursor(new_col, new_line)
end end
else else
if new_col < 0 then if new_col < 0 then
new_col = self.slots_hor - 1 new_col = self.view.colNumber - 1
end end
if new_col == self.slots_hor then if new_col == self.view.colNumber then
new_col = 0 new_col = 0
end end
end end
self.widget.selected = (new_line * self.slots_hor) + new_col + 1 self.widget.selected = (new_line * self.view.colNumber) + new_col + 1
end end
function FlowBox:keyreleased(key, code) function FlowBox:keyreleased(key, code)
@ -115,12 +116,12 @@ end
function FlowBox:mousemoved(x, y) function FlowBox:mousemoved(x, y)
local col, line = self:getCoord(self.widget.selected) 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, widget_selected local newcol, newline, widget_selected
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)
widget_selected = (newline * self.slots_hor) + newcol + 1 widget_selected = (newline * self.view.colNumber) + newcol + 1
if widget_selected >= 1 and widget_selected <= #self.widget.list then if widget_selected >= 1 and widget_selected <= #self.widget.list then
self.widget.selected = widget_selected self.widget.selected = widget_selected
@ -130,12 +131,12 @@ end
function FlowBox:mousepressed(x, y, button, isTouch) function FlowBox:mousepressed(x, y, button, isTouch)
local col, line = self:getCoord(self.widget.selected) local col, line = self:getCoord(self.widget.selected)
local begincol, beginline = self:getCoord(self.begin) local begincol, beginline = self:getCoord(self.view.firstSlot)
local newline, newcol, widget_selected local newline, newcol, widget_selected
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)
widget_selected = (newline * self.slots_hor) + newcol + 1 widget_selected = (newline * self.view.colNumber) + newcol + 1
if widget_selected >= 1 and widget_selected <= #self.widget.list then if widget_selected >= 1 and widget_selected <= #self.widget.list then
self.widget.selected = widget_selected self.widget.selected = widget_selected
@ -149,7 +150,7 @@ function FlowBox:draw()
local widgety = self.y local widgety = self.y
local widgetx = self.x local widgetx = self.x
for i,v in ipairs(self.widget.list) do for i,v in ipairs(self.widget.list) do
if (i >= self.begin) and (i < self.begin + self.slots) then if (i >= self.view.firstSlot) and (i < self.view.firstSlot + self.view.slotNumber) then
v:draw(widgetx, widgety, self.widget.w, self.widget.h) v:draw(widgetx, widgety, self.widget.w, self.widget.h)
if self.widget.selected == i and self:haveFocus() == true then if self.widget.selected == i and self:haveFocus() == true then
v:drawSelected(widgetx, widgety, self.widget.w, self.widget.h) v:drawSelected(widgetx, widgety, self.widget.w, self.widget.h)

View file

@ -5,29 +5,30 @@ local menuutils = require(cwd .. "widgets.utils")
local ListBox = Menu:extend() local ListBox = Menu:extend()
function ListBox:new(menusystem, name, x, y, w, h, slots) function ListBox:new(menusystem, name, x, y, w, h, slotNumber)
self.slots = slots self.view = {}
self.view.slotNumber = slotNumber
self.view.firstSlot = 1
ListBox.super.new(self, menusystem, name, x, y, w, h) ListBox.super.new(self, menusystem, name, x, y, w, h)
self.begin = 1 self.h = slotNumber * self.widget.h -- On fait en sorte que la hauteur
self.h = slots * self.widget.h -- On fait en sorte que la hauteur
-- soit un multiple du nombre de slot et de leur hauteur -- soit un multiple du nombre de slot et de leur hauteur
end end
function ListBox:updateWidgetSize() function ListBox:updateWidgetSize()
self.widget.h = math.floor( self.h / self.slots ) self.widget.h = math.floor( self.h / self.view.slotNumber )
self.widget.w = self.w self.widget.w = self.w
end end
function ListBox:update(dt) function ListBox:update(dt)
if self.widget.selected < self.begin then if self.widget.selected < self.view.firstSlot then
self.begin = self.widget.selected self.view.firstSlot = self.widget.selected
end end
if self.widget.selected > self.begin + self.slots - 1 then if self.widget.selected > self.view.firstSlot + self.view.slotNumber - 1 then
self.begin = self.widget.selected - self.slots + 1 self.view.firstSlot = self.widget.selected - self.view.slotNumber + 1
end end
if self.begin < 1 then if self.view.firstSlot < 1 then
self.begin = 1 self.view.firstSlot = 1
end end
end end
@ -50,7 +51,7 @@ function ListBox:keyreleased(key, code)
end end
function ListBox:mousemoved(x, y) function ListBox:mousemoved(x, y)
local widget_selected = self.begin + math.floor(y / self.widget.h) local widget_selected = self.view.firstSlot + math.floor(y / self.widget.h)
if widget_selected >= 1 and widget_selected <= #self.widget.list then if widget_selected >= 1 and widget_selected <= #self.widget.list then
self.widget.selected = widget_selected self.widget.selected = widget_selected
@ -59,7 +60,7 @@ function ListBox:mousemoved(x, y)
end end
function ListBox:mousepressed(x, y, button, isTouch) function ListBox:mousepressed(x, y, button, isTouch)
local widget_selected = self.begin + math.floor(y / self.widget.h) local widget_selected = self.view.firstSlot + math.floor(y / self.widget.h)
if widget_selected >= 1 and widget_selected <= #self.widget.list then if widget_selected >= 1 and widget_selected <= #self.widget.list then
self.widget.selected = widget_selected self.widget.selected = widget_selected
@ -74,7 +75,7 @@ end
function ListBox:draw() function ListBox:draw()
local widgety = self.y local widgety = self.y
for i,v in ipairs(self.widget.list) do for i,v in ipairs(self.widget.list) do
if (i >= self.begin) and (i < self.begin + self.slots) then if (i >= self.view.firstSlot) and (i < self.view.firstSlot + self.view.slotNumber) then
v:draw(self.x, widgety, self.w, self.widget.h) v:draw(self.x, widgety, self.w, self.widget.h)
if self.widget.selected == i and self:haveFocus() == true then if self.widget.selected == i and self:haveFocus() == true then
v:drawSelected(self.x, widgety, self.w, self.widget.h) v:drawSelected(self.x, widgety, self.w, self.widget.h)