feat: add more actions in menus
This commit is contained in:
parent
f7e1beae68
commit
a677902f6c
5 changed files with 80 additions and 3 deletions
|
@ -36,6 +36,7 @@ function ListBox:new(name, x, y, w, h, slotNumber)
|
|||
ListBox.super.new(self, name, x, y, w, h)
|
||||
self.h = slotNumber * self.widgetSize.h -- On fait en sorte que la hauteur
|
||||
-- soit un multiple du nombre de slot et de leur hauteur
|
||||
self.lateralFunc = nil
|
||||
end
|
||||
|
||||
-- UPDATE FUNCTIONS
|
||||
|
@ -54,6 +55,10 @@ function ListBox:resetView()
|
|||
self.view:reset()
|
||||
end
|
||||
|
||||
function ListBox:addLateralAction(func)
|
||||
self.lateralFunc = func
|
||||
end
|
||||
|
||||
-- KEYBOARD FUNCTIONS
|
||||
-- Handle input from keyboard/controllers.
|
||||
|
||||
|
@ -69,6 +74,10 @@ function ListBox:moveByKeys(key)
|
|||
self:playNavigationSound()
|
||||
self.canvas.needRedraw = true
|
||||
end
|
||||
|
||||
if (self.lateralFunc ~= nil and (key == 'left' or key == 'right')) then
|
||||
self.widget:lateralAction(self.lateralFunc, key)
|
||||
end
|
||||
end
|
||||
|
||||
-- POSITION FUNCTIONS
|
||||
|
|
|
@ -21,6 +21,7 @@ function MenuModel:new()
|
|||
self.cancel = 0
|
||||
self.limit = -1
|
||||
-- self:updateWidgetSize()
|
||||
self.hoverFunc = nil
|
||||
end
|
||||
|
||||
function MenuModel:clear()
|
||||
|
@ -33,6 +34,7 @@ end
|
|||
|
||||
function MenuModel:addPage(pageName)
|
||||
local page = Page()
|
||||
page.name = pageName
|
||||
self.pages[pageName] = page
|
||||
self.currentPage = pageName
|
||||
return page
|
||||
|
@ -58,6 +60,7 @@ end
|
|||
function MenuModel:switch(pageName)
|
||||
if (self:pageExists(pageName)) then
|
||||
self.currentPage = pageName
|
||||
self:hoverAction()
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -70,6 +73,10 @@ function MenuModel:getCurrentPage()
|
|||
return self:getPage(self.currentPage)
|
||||
end
|
||||
|
||||
function MenuModel:getCurrentPageName()
|
||||
return self.currentPage
|
||||
end
|
||||
|
||||
-- UPDATE/DRAW FUNCTIONS
|
||||
-- All the update functions
|
||||
|
||||
|
@ -91,6 +98,10 @@ end
|
|||
-- ACTION FUNCTIONS
|
||||
-- All the actions callback used by the widgets
|
||||
|
||||
function MenuModel:addHoverAction(func)
|
||||
self.hoverFunc = func
|
||||
end
|
||||
|
||||
function MenuModel:cancelAction()
|
||||
local page = self:getCurrentPage()
|
||||
page:cancelAction()
|
||||
|
@ -106,6 +117,19 @@ function MenuModel:selectedAction()
|
|||
page:selectedAction()
|
||||
end
|
||||
|
||||
function MenuModel:hoverAction()
|
||||
local page = self:getCurrentPage()
|
||||
if (self.hoverFunc ~= nil) then
|
||||
page:hoverAction(self.hoverFunc)
|
||||
end
|
||||
end
|
||||
|
||||
function MenuModel:lateralAction(func, key)
|
||||
local page = self:getCurrentPage()
|
||||
page:lateralAction(func, key)
|
||||
end
|
||||
|
||||
|
||||
-- WIDGET FUNCTIONS
|
||||
-- All the functions to handle widgets
|
||||
|
||||
|
@ -167,22 +191,29 @@ end
|
|||
|
||||
function MenuModel:trySelectWidget(cursorid)
|
||||
local page = self:getCurrentPage()
|
||||
return page:trySelectWidget(cursorid)
|
||||
local isSuccess = page:trySelectWidget(cursorid)
|
||||
if (isSuccess) then
|
||||
self:hoverAction()
|
||||
end
|
||||
return isSuccess
|
||||
end
|
||||
|
||||
function MenuModel:setCursor(cursorid)
|
||||
local page = self:getCurrentPage()
|
||||
page:setCursor(cursorid)
|
||||
self:hoverAction()
|
||||
end
|
||||
|
||||
function MenuModel:moveCursorAbsolute(newSelected)
|
||||
local page = self:getCurrentPage()
|
||||
page:moveCursorAbsolute(newSelected)
|
||||
self:hoverAction()
|
||||
end
|
||||
|
||||
function MenuModel:moveCursor(relative)
|
||||
local page = self:getCurrentPage()
|
||||
page:moveCursor(relative)
|
||||
self:hoverAction()
|
||||
end
|
||||
|
||||
-- DRAW
|
||||
|
|
|
@ -84,6 +84,18 @@ function Page:selectedAction()
|
|||
end
|
||||
end
|
||||
|
||||
function Page:hoverAction(func)
|
||||
if (self:widgetExist(self.selected)) then
|
||||
func(self.widgets[self.selected])
|
||||
end
|
||||
end
|
||||
|
||||
function Page:lateralAction(func, key)
|
||||
if (self:widgetExist(self.selected)) then
|
||||
func(key, self.widgets[self.selected], self.selected, self.name)
|
||||
end
|
||||
end
|
||||
|
||||
-- WIDGET FUNCTIONS
|
||||
-- All the functions to handle widgets
|
||||
|
||||
|
|
|
@ -42,6 +42,18 @@ function Menu:new(name, x, y, w, h)
|
|||
self:updateWidgetSize()
|
||||
|
||||
self:initCanvas()
|
||||
self.cancelFunc = nil
|
||||
end
|
||||
|
||||
-- FUNCTIONS FUNCTIONS
|
||||
-- Add functions to the menu system
|
||||
|
||||
function Menu:addCancelAction(func)
|
||||
self.cancelFunc = func
|
||||
end
|
||||
|
||||
function Menu:addHoverAction(func)
|
||||
self.widget:addHoverAction(func)
|
||||
end
|
||||
|
||||
-- INTERACTION FUNCTIONS
|
||||
|
@ -112,14 +124,20 @@ function Menu:getPage(pageName)
|
|||
self.widget:getPage(pageName)
|
||||
end
|
||||
|
||||
function Menu:getCurrentPageName()
|
||||
return self.widget:getCurrentPageName()
|
||||
end
|
||||
|
||||
function Menu:switch(pageName)
|
||||
self.widget:switch(pageName)
|
||||
self:resetView()
|
||||
self.canvas.needRedraw = true
|
||||
end
|
||||
|
||||
function Menu:back()
|
||||
self.widget:back()
|
||||
self:resetView()
|
||||
self.canvas.needRedraw = true
|
||||
end
|
||||
|
||||
function GuiElement:setSubFocus(widgetId, pageName)
|
||||
|
@ -133,8 +151,12 @@ end
|
|||
-- Send actions to the widgets
|
||||
|
||||
function Menu:cancelAction()
|
||||
if (self.cancelFunc ~= nil) then
|
||||
self.cancelFunc(self)
|
||||
else
|
||||
self.widget:cancelAction()
|
||||
end
|
||||
end
|
||||
|
||||
function Menu:clear()
|
||||
self.widget:clear()
|
||||
|
|
|
@ -41,7 +41,7 @@ function TextMenu:new(name, font, x, y, w, slotNumber, padding, lineSize)
|
|||
TextMenu.super.new(self, name, x, y, w, (self.lineHeight * slotNumber), slotNumber)
|
||||
end
|
||||
|
||||
function TextMenu:addItem(text, position, func, type, additionnalItems, color)
|
||||
function TextMenu:addItem(text, position, func, type, additionnalItems, color, additionnalDatas)
|
||||
local widget = TextMenu.baseWidgets.Base(self.name, text, position)
|
||||
widget:setFunc(func)
|
||||
widget.type = type or "select"
|
||||
|
@ -53,6 +53,9 @@ function TextMenu:addItem(text, position, func, type, additionnalItems, color)
|
|||
if (color ~= nil) then
|
||||
widget:setColor(color[1], color[2], color[3])
|
||||
end
|
||||
if (additionnalDatas ~= nil) then
|
||||
widget.datas = additionnalDatas
|
||||
end
|
||||
end
|
||||
|
||||
function TextMenu:generateSubmenu(pageName, label, parent, list, func, backWidget)
|
||||
|
|
Loading…
Reference in a new issue