modules/menusystem: more code cleanup

This commit is contained in:
Kazhnuz 2019-04-11 16:58:42 +02:00
parent 96f56a7284
commit c9e3f5b82a
3 changed files with 45 additions and 36 deletions

View file

@ -45,14 +45,14 @@ function FlowBox:new(menusystem, name, x, y, w, h, slots_hor, slots_vert)
-- soit un multiple du nombre de slot et de leur dimensions -- soit un multiple du nombre de slot et de leur dimensions
end end
-- UPDATE FUNCTIONS
-- Update the menu and its view
function FlowBox:updateWidgetSize() function FlowBox:updateWidgetSize()
self.widget.h = math.floor( self.h / self.view.lineNumber ) self.widget.h = math.floor( self.h / self.view.lineNumber )
self.widget.w = math.floor( self.w / self.view.colNumber ) self.widget.w = math.floor( self.w / self.view.colNumber )
end end
-- UPDATE FUNCTIONS
-- Update the menu and its view
function FlowBox:update(dt) function FlowBox:update(dt)
self:updateView() self:updateView()
end end
@ -76,6 +76,9 @@ function FlowBox:updateView()
self.view.firstSlot = beginline * self.view.colNumber + 1 self.view.firstSlot = beginline * self.view.colNumber + 1
end end
-- INFO FUNCTIONS
-- Get informations
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
@ -84,6 +87,9 @@ function FlowBox:getCoord(id_selected)
return col, line return col, line
end end
-- CURSOR FUNCTIONS
-- Handle the cursor in a 2D menu
function FlowBox:moveCursor(new_col, new_line) function FlowBox:moveCursor(new_col, new_line)
local col, line = self:getCoord(self.widget.selected) local col, line = self:getCoord(self.widget.selected)
local lastcol, lastline = self:getCoord(#self.widget.list) local lastcol, lastline = self:getCoord(#self.widget.list)
@ -122,6 +128,9 @@ function FlowBox:moveCursor(new_col, new_line)
self.widget.selected = (new_line * self.view.colNumber) + new_col + 1 self.widget.selected = (new_line * self.view.colNumber) + new_col + 1
end end
-- KEYS FUNCTIONS
-- Handle the keyboard/controller inputs
function FlowBox:keyreleased(key, code) function FlowBox:keyreleased(key, code)
local col, line = self:getCoord(self.widget.selected) local col, line = self:getCoord(self.widget.selected)
if key == 'left' then if key == 'left' then
@ -147,6 +156,9 @@ function FlowBox:keyreleased(key, code)
end end
end end
-- MOUSE FUNCTIONS
-- Handle the mouse/touch pointer
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.view.firstSlot) local begincol, beginline = self:getCoord(self.view.firstSlot)

View file

@ -36,9 +36,6 @@ MenuSystem.TextMenu = require(cwd .. "textmenu")
-- load widgets object -- load widgets object
MenuSystem.Widget = require(cwd .. "widgets") MenuSystem.Widget = require(cwd .. "widgets")
--local VirtualPad = require "modules.virtualpad"
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- Initialize and configure the menu controller -- Initialize and configure the menu controller
@ -62,29 +59,6 @@ function MenuSystem:menuExist(name)
return (self.menus[name] ~= nil) return (self.menus[name] ~= nil)
end end
-- UPDATE FUNCTIONS
-- Update the menus of the menusystem
function MenuSystem:update(dt)
self:removeDestroyedMenus()
for k,v in pairs(self.menus) do
v:update(dt)
v:updateWidgets(dt)
end
if self.menus[self.focusedMenu] ~= nil then
-- Only check buttons if the current focused menu is actually active
if self.menus[self.focusedMenu].isActive then
for k,v in pairs(self.keys) do
if self.keys[k].isPressed then
self.menus[self.focusedMenu]:keyreleased(k)
end
end
end
end
end
function MenuSystem:switchMenu(menu) function MenuSystem:switchMenu(menu)
for k,v in pairs(self.menus) do for k,v in pairs(self.menus) do
if k == menu then if k == menu then
@ -151,6 +125,29 @@ function MenuSystem:removeDestroyedMenus()
end end
end end
-- UPDATE FUNCTIONS
-- Update the menus of the menusystem
function MenuSystem:update(dt)
self:removeDestroyedMenus()
for k,v in pairs(self.menus) do
v:update(dt)
v:updateWidgets(dt)
end
if self.menus[self.focusedMenu] ~= nil then
-- Only check buttons if the current focused menu is actually active
if self.menus[self.focusedMenu].isActive then
for k,v in pairs(self.keys) do
if self.keys[k].isPressed then
self.menus[self.focusedMenu]:keyreleased(k)
end
end
end
end
end
function MenuSystem:keyreleased(key, code) function MenuSystem:keyreleased(key, code)
-- TODO:depreciated function -- TODO:depreciated function
end end

View file

@ -121,13 +121,6 @@ function Menu:cancelAction()
end end
end end
-- UPDATE FUNCTIONS
-- Update the menu every game update
function Menu:update(dt)
-- Cette fonction ne contient rien par défaut
end
function Menu:clear() function Menu:clear()
self.widget.list = {} self.widget.list = {}
self.widget.cancel = 0 self.widget.cancel = 0
@ -146,6 +139,13 @@ function Menu:destroy()
self.destroyed = true self.destroyed = true
end end
-- UPDATE FUNCTIONS
-- Update the menu every game update
function Menu:update(dt)
-- Cette fonction ne contient rien par défaut
end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS
-- Draw the menu and its content -- Draw the menu and its content