From f2b520e2b3ceb4bc89d769e7654de1a05531cd32 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Wed, 13 Feb 2019 17:44:59 +0100 Subject: [PATCH] core/menusystem: improve view variables names --- .../core/modules/menusystem/flowbox.lua | 41 ++++++++++--------- .../core/modules/menusystem/listbox.lua | 29 ++++++------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/sonic-boost.love/core/modules/menusystem/flowbox.lua b/sonic-boost.love/core/modules/menusystem/flowbox.lua index 4f6d978..ebfa53f 100644 --- a/sonic-boost.love/core/modules/menusystem/flowbox.lua +++ b/sonic-boost.love/core/modules/menusystem/flowbox.lua @@ -6,11 +6,12 @@ local FlowBox = Menu:extend() local menuutils = require(cwd .. "widgets.utils") function FlowBox:new(menusystem, name, x, y, w, h, slots_hor, slots_vert) - self.slots = slots_hor * slots_vert - self.slots_hor = slots_hor - self.slots_vert = slots_vert + self.view = {} + self.view.slotNumber = slots_hor * 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) - self.begin = 1 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 @@ -19,34 +20,34 @@ function FlowBox:new(menusystem, name, x, y, w, h, slots_hor, slots_vert) end function FlowBox:updateWidgetSize() - self.widget.h = math.floor( self.h / self.slots_vert ) - self.widget.w = math.floor( self.w / self.slots_hor ) + self.widget.h = math.floor( self.h / self.view.lineNumber ) + self.widget.w = math.floor( self.w / self.view.colNumber ) end function FlowBox:update(dt) 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 beginline = line end - if line > beginline + self.slots_vert - 1 then - beginline = line - self.slots_vert + 1 + if line > beginline + self.view.lineNumber - 1 then + beginline = line - self.view.lineNumber + 1 end if beginline < 0 then beginline = 0 end - self.begin = beginline * self.slots_hor + 1 + self.view.firstSlot = beginline * self.view.colNumber + 1 end function FlowBox: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.slots_hor) - col = id_selected - (line * self.slots_hor) + line = math.floor(id_selected / self.view.colNumber) + col = id_selected - (line * self.view.colNumber) return col, line end @@ -77,15 +78,15 @@ function FlowBox:moveCursor(new_col, new_line) end else if new_col < 0 then - new_col = self.slots_hor - 1 + new_col = self.view.colNumber - 1 end - if new_col == self.slots_hor then + if new_col == self.view.colNumber then new_col = 0 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 function FlowBox:keyreleased(key, code) @@ -115,12 +116,12 @@ end function FlowBox:mousemoved(x, y) 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 newline = beginline + math.floor(y / self.widget.h) 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 self.widget.selected = widget_selected @@ -130,12 +131,12 @@ end function FlowBox:mousepressed(x, y, button, isTouch) 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 newline = beginline + math.floor(y / self.widget.h) 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 self.widget.selected = widget_selected @@ -149,7 +150,7 @@ function FlowBox:draw() local widgety = self.y local widgetx = self.x 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) if self.widget.selected == i and self:haveFocus() == true then v:drawSelected(widgetx, widgety, self.widget.w, self.widget.h) diff --git a/sonic-boost.love/core/modules/menusystem/listbox.lua b/sonic-boost.love/core/modules/menusystem/listbox.lua index d7e3ee6..eb5f797 100644 --- a/sonic-boost.love/core/modules/menusystem/listbox.lua +++ b/sonic-boost.love/core/modules/menusystem/listbox.lua @@ -5,29 +5,30 @@ local menuutils = require(cwd .. "widgets.utils") local ListBox = Menu:extend() -function ListBox:new(menusystem, name, x, y, w, h, slots) - self.slots = slots +function ListBox:new(menusystem, name, x, y, w, h, slotNumber) + self.view = {} + self.view.slotNumber = slotNumber + self.view.firstSlot = 1 ListBox.super.new(self, menusystem, name, x, y, w, h) - self.begin = 1 - self.h = slots * self.widget.h -- On fait en sorte que la hauteur + self.h = slotNumber * self.widget.h -- On fait en sorte que la hauteur -- soit un multiple du nombre de slot et de leur hauteur end 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 end function ListBox:update(dt) - if self.widget.selected < self.begin then - self.begin = self.widget.selected + if self.widget.selected < self.view.firstSlot then + self.view.firstSlot = self.widget.selected end - if self.widget.selected > self.begin + self.slots - 1 then - self.begin = self.widget.selected - self.slots + 1 + if self.widget.selected > self.view.firstSlot + self.view.slotNumber - 1 then + self.view.firstSlot = self.widget.selected - self.view.slotNumber + 1 end - if self.begin < 1 then - self.begin = 1 + if self.view.firstSlot < 1 then + self.view.firstSlot = 1 end end @@ -50,7 +51,7 @@ function ListBox:keyreleased(key, code) end 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 self.widget.selected = widget_selected @@ -59,7 +60,7 @@ function ListBox:mousemoved(x, y) end 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 self.widget.selected = widget_selected @@ -74,7 +75,7 @@ end function ListBox:draw() local widgety = self.y 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) if self.widget.selected == i and self:haveFocus() == true then v:drawSelected(self.x, widgety, self.w, self.widget.h)