From b3febf0fb344fb7ad1970e5e4a54713021b6f71c Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Tue, 12 Feb 2019 18:31:32 +0100 Subject: [PATCH] core/menusystem: rework focus system Don't let every menus handle themselves if they have focus, it should be the menu system role to handle that --- .../core/modules/menusystem/flowbox.lua | 6 +++-- .../core/modules/menusystem/grid.lua | 2 +- .../core/modules/menusystem/init.lua | 23 ++++++++----------- .../core/modules/menusystem/listbox.lua | 6 +++-- .../core/modules/menusystem/parent.lua | 9 +++++++- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/sonic-boost.love/core/modules/menusystem/flowbox.lua b/sonic-boost.love/core/modules/menusystem/flowbox.lua index ffef17f..5a378bb 100644 --- a/sonic-boost.love/core/modules/menusystem/flowbox.lua +++ b/sonic-boost.love/core/modules/menusystem/flowbox.lua @@ -120,6 +120,7 @@ function FlowBox:mousemoved(x, y) if widget_selected >= 1 and widget_selected <= #self.widget.list then self.widget.selected = widget_selected + self:getFocus() end end @@ -134,9 +135,10 @@ function FlowBox:mousepressed(x, y, button, isTouch) if widget_selected >= 1 and widget_selected <= #self.widget.list then self.widget.selected = widget_selected + self:getFocus() self.widget.list[self.widget.selected]:action() end - + end function FlowBox:draw() @@ -145,7 +147,7 @@ function FlowBox:draw() for i,v in ipairs(self.widget.list) do if (i >= self.begin) and (i < self.begin + self.slots) then v:draw(widgetx, widgety, self.widget.w, self.widget.h) - if self.widget.selected == i and self.haveFocus == true then + if self.widget.selected == i and self:haveFocus() == true then v:drawSelected(widgetx, widgety, self.widget.w, self.widget.h) else v:draw(widgetx, widgety, self.widget.w, self.widget.h) diff --git a/sonic-boost.love/core/modules/menusystem/grid.lua b/sonic-boost.love/core/modules/menusystem/grid.lua index 07d07d6..29f74c6 100644 --- a/sonic-boost.love/core/modules/menusystem/grid.lua +++ b/sonic-boost.love/core/modules/menusystem/grid.lua @@ -206,7 +206,7 @@ function GridBox:draw() for i,v in ipairs(self.listSlot) do if (v.isSlave == 0) and (v.widgetID <= #self.widget.list) then --self.widget.list[v.widgetID]:draw(widgetx, widgety, self.widget.w * v.sizeW, self.widget.h * v.sizeH) - if self.widget.selected == v.widgetID and self.haveFocus == true then + if self.widget.selected == v.widgetID and self:haveFocus() == true then self.widget.list[v.widgetID]:drawSelected(widgetx, widgety, self.widget.w * v.sizeW, self.widget.h * v.sizeH) else self.widget.list[v.widgetID]:draw(widgetx, widgety, self.widget.w * v.sizeW, self.widget.h * v.sizeH) diff --git a/sonic-boost.love/core/modules/menusystem/init.lua b/sonic-boost.love/core/modules/menusystem/init.lua index 4d7bafe..b677a58 100644 --- a/sonic-boost.love/core/modules/menusystem/init.lua +++ b/sonic-boost.love/core/modules/menusystem/init.lua @@ -15,6 +15,7 @@ MenuSystem.Widget = require(cwd .. "widgets") function MenuSystem:new() self.menus = {} + self.focusedMenu = "" end function MenuSystem:reset() @@ -27,17 +28,19 @@ end function MenuSystem:update(dt) self:removeDestroyedMenus() - for l,v in pairs(self.menus) do + for k,v in pairs(self.menus) do v:update(dt) v:updateWidgets(dt) - if v.haveFocus == true then - for k,w in pairs(self.keys) do - if self.keys[k].isPressed then - v:keyreleased(k) - end + end + + if self.menus[self.focusedMenu] ~= nil 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 function MenuSystem:removeDestroyedMenus() @@ -58,10 +61,6 @@ function MenuSystem:mousemoved(x, y, dx, dy) if v.isActive then if (x > v.x) and (x < v.x + v.w) and (y > v.y) and (y < v.y + v.h) then v:mousemoved(x - v.x, y - v.y) - for j,u in ipairs(self.menus) do - u.haveFocus = false - end - v.haveFocus = true end end end @@ -72,10 +71,6 @@ function MenuSystem:mousepressed( x, y, button, istouch ) if v.isActive then if (x > v.x) and (x < v.x + v.w) and (y > v.y) and (y < v.y + v.h) then v:mousepressed(x - v.x, y - v.y, button, istouch ) - for j,u in ipairs(self.menus) do - u.haveFocus = false - end - v.haveFocus = true end end end diff --git a/sonic-boost.love/core/modules/menusystem/listbox.lua b/sonic-boost.love/core/modules/menusystem/listbox.lua index b818218..dd3b9ee 100644 --- a/sonic-boost.love/core/modules/menusystem/listbox.lua +++ b/sonic-boost.love/core/modules/menusystem/listbox.lua @@ -50,6 +50,7 @@ function ListBox:mousemoved(x, y) if widget_selected >= 1 and widget_selected <= #self.widget.list then self.widget.selected = widget_selected + self:getFocus() end end @@ -58,11 +59,12 @@ function ListBox:mousepressed(x, y, button, isTouch) 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 ListBox:draw() @@ -70,7 +72,7 @@ function ListBox:draw() for i,v in ipairs(self.widget.list) do if (i >= self.begin) and (i < self.begin + self.slots) then v:draw(self.x, widgety, self.w, self.widget.h) - if self.widget.selected == i and self.haveFocus == true then + if self.widget.selected == i and self:haveFocus() == true then v:drawSelected(self.x, widgety, self.w, self.widget.h) else v:draw(self.x, widgety, self.w, self.widget.h) diff --git a/sonic-boost.love/core/modules/menusystem/parent.lua b/sonic-boost.love/core/modules/menusystem/parent.lua index f754665..f20101e 100644 --- a/sonic-boost.love/core/modules/menusystem/parent.lua +++ b/sonic-boost.love/core/modules/menusystem/parent.lua @@ -17,7 +17,6 @@ function Menu:new(menusystem, name, x, y, w, h) self:updateWidgetSize() self.isDestroyed = false - self.haveFocus = false self.isVisible = true self.isActive = true @@ -28,6 +27,14 @@ function Menu:new(menusystem, name, x, y, w, h) self:register() end +function Menu:getFocus() + self.menusystem.focusedMenu = self.name +end + +function Menu:haveFocus() + return (self.menusystem.focusedMenu == self.name) +end + function Menu:register() self.menusystem:addMenu(self.name, self) end