From a677902f6c6c0a04d925906bf99c1129da50ce7e Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 2 Jan 2022 17:11:42 +0100 Subject: [PATCH] feat: add more actions in menus --- .../birb/modules/gui/menus/listbox.lua | 9 +++++ .../birb/modules/gui/menus/model/init.lua | 33 ++++++++++++++++++- .../birb/modules/gui/menus/model/page.lua | 12 +++++++ .../birb/modules/gui/menus/parent.lua | 24 +++++++++++++- .../birb/modules/gui/textmenu/init.lua | 5 ++- 5 files changed, 80 insertions(+), 3 deletions(-) diff --git a/sonic-radiance.love/birb/modules/gui/menus/listbox.lua b/sonic-radiance.love/birb/modules/gui/menus/listbox.lua index 934d502..ad6d9e5 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/listbox.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/listbox.lua @@ -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 diff --git a/sonic-radiance.love/birb/modules/gui/menus/model/init.lua b/sonic-radiance.love/birb/modules/gui/menus/model/init.lua index 39e8b53..95ebeb1 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/model/init.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/model/init.lua @@ -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 diff --git a/sonic-radiance.love/birb/modules/gui/menus/model/page.lua b/sonic-radiance.love/birb/modules/gui/menus/model/page.lua index d99c471..93f60ab 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/model/page.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/model/page.lua @@ -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 diff --git a/sonic-radiance.love/birb/modules/gui/menus/parent.lua b/sonic-radiance.love/birb/modules/gui/menus/parent.lua index d9b5708..579c993 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/parent.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/parent.lua @@ -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,7 +151,11 @@ end -- Send actions to the widgets function Menu:cancelAction() - self.widget:cancelAction() + if (self.cancelFunc ~= nil) then + self.cancelFunc(self) + else + self.widget:cancelAction() + end end function Menu:clear() diff --git a/sonic-radiance.love/birb/modules/gui/textmenu/init.lua b/sonic-radiance.love/birb/modules/gui/textmenu/init.lua index 6370ea0..aa0fc95 100644 --- a/sonic-radiance.love/birb/modules/gui/textmenu/init.lua +++ b/sonic-radiance.love/birb/modules/gui/textmenu/init.lua @@ -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)