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
This commit is contained in:
Kazhnuz 2019-02-12 18:31:32 +01:00
parent c4ce0f5c5d
commit b3febf0fb3
5 changed files with 26 additions and 20 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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