From 714a20ca2398c9e48c0f21ca6b6d78ffd81d8445 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 7 Apr 2019 11:44:00 +0200 Subject: [PATCH] modules/menusystem : add horizontal listbox --- gamecore/modules/menusystem/hlistbox.lua | 110 +++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 gamecore/modules/menusystem/hlistbox.lua diff --git a/gamecore/modules/menusystem/hlistbox.lua b/gamecore/modules/menusystem/hlistbox.lua new file mode 100644 index 0000000..9bccc5b --- /dev/null +++ b/gamecore/modules/menusystem/hlistbox.lua @@ -0,0 +1,110 @@ +local cwd = (...):gsub('%.hlistbox$', '') .. "." +local Menu = require(cwd .. "parent") + +local menuutils = require(cwd .. "widgets.utils") + +local HListBox = Menu:extend() + +function HListBox:new(menusystem, name, x, y, w, h, slotNumber) + self.view = {} + self.view.slotNumber = slotNumber + self.view.firstSlot = 1 + HListBox.super.new(self, menusystem, name, x, y, w, h) + self.w = slotNumber * self.widget.w -- On fait en sorte que la hauteur + -- soit un multiple du nombre de slot et de leur hauteur +end + +function HListBox:updateWidgetSize() + self.widget.h = self.h + self.widget.w = math.floor( self.w / self.view.slotNumber ) +end + +function HListBox:update(dt) + self:updateView() +end + +function HListBox:updateView() + if self.widget.selected < self.view.firstSlot then + self.view.firstSlot = self.widget.selected + end + 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.view.firstSlot < 1 then + self.view.firstSlot = 1 + end +end + +function HListBox:keyreleased(key, code) + + if key == 'left' then + self:moveCursor(self.widget.selected - 1) + end + + if key == 'right' then + self:moveCursor(self.widget.selected + 1) + end + + if key == "A" then + if (self.widget.selected >= 1 and self.widget.selected <= #self.widget.list) then + self.widget.list[self.widget.selected]:action() + end + end + + if key == "B" then + if (self.widget.cancel >= 1 and self.widget.cancel <= #self.widget.list) then + self.widget.list[self.widget.cancel]:action() + end + end + +end + +function HListBox:mousemoved(x, y) + local widget_selected = self.view.firstSlot + math.floor(x / self.widget.w) + + if widget_selected >= 1 and widget_selected <= #self.widget.list then + self.widget.selected = widget_selected + self:getFocus() + end +end + +function HListBox:mousepressed(x, y, button, isTouch) + local widget_selected = self.view.firstSlot + math.floor(x / self.widget.w) + + if widget_selected >= 1 and widget_selected <= #self.widget.list then + self.widget.selected = widget_selected + self:getFocus() + if #self.widget.list > 0 then + self.widget.list[self.widget.selected]:action() + end + end + +end + +function HListBox:draw() + self:updateView() + local widgetx = self.x + for i,v in ipairs(self.widget.list) do + if (i >= self.view.firstSlot) and (i < self.view.firstSlot + self.view.slotNumber) then + v:draw(widgetx, self.y, self.widget.w, self.h) + if self.widget.selected == i and self:haveFocus() == true then + v:drawSelected(widgetx, self.y, self.widget.w, self.h) + else + v:draw(widgetx, self.y, self.widget.w, self.h) + end + widgetx = widgetx + self.widget.w + end + end +end + +function HListBox:drawCursor() + self:updateView() + if (self.widget.selected >= 1 and self.widget.selected <= #self.widget.list) then + local w, h = self:getWidgetSize() + local x = (self.widget.selected - self.view.firstSlot) * w + menuutils.drawCursor(self.x + x,self.y, w, h) + end +end + +return HListBox