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 if widget_selected >= 1 and widget_selected <= #self.widget.list then
self.widget.selected = widget_selected self.widget.selected = widget_selected
self:getFocus()
end end
end end
@ -134,6 +135,7 @@ function FlowBox:mousepressed(x, y, button, isTouch)
if widget_selected >= 1 and widget_selected <= #self.widget.list then if widget_selected >= 1 and widget_selected <= #self.widget.list then
self.widget.selected = widget_selected self.widget.selected = widget_selected
self:getFocus()
self.widget.list[self.widget.selected]:action() self.widget.list[self.widget.selected]:action()
end end
@ -145,7 +147,7 @@ function FlowBox:draw()
for i,v in ipairs(self.widget.list) do for i,v in ipairs(self.widget.list) do
if (i >= self.begin) and (i < self.begin + self.slots) then if (i >= self.begin) and (i < self.begin + self.slots) then
v:draw(widgetx, widgety, self.widget.w, self.widget.h) 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) v:drawSelected(widgetx, widgety, self.widget.w, self.widget.h)
else else
v:draw(widgetx, widgety, self.widget.w, self.widget.h) 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 for i,v in ipairs(self.listSlot) do
if (v.isSlave == 0) and (v.widgetID <= #self.widget.list) then 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) --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) self.widget.list[v.widgetID]:drawSelected(widgetx, widgety, self.widget.w * v.sizeW, self.widget.h * v.sizeH)
else else
self.widget.list[v.widgetID]:draw(widgetx, widgety, self.widget.w * v.sizeW, self.widget.h * v.sizeH) 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() function MenuSystem:new()
self.menus = {} self.menus = {}
self.focusedMenu = ""
end end
function MenuSystem:reset() function MenuSystem:reset()
@ -27,17 +28,19 @@ end
function MenuSystem:update(dt) function MenuSystem:update(dt)
self:removeDestroyedMenus() self:removeDestroyedMenus()
for l,v in pairs(self.menus) do for k,v in pairs(self.menus) do
v:update(dt) v:update(dt)
v:updateWidgets(dt) v:updateWidgets(dt)
if v.haveFocus == true then end
for k,w in pairs(self.keys) do
if self.keys[k].isPressed then if self.menus[self.focusedMenu] ~= nil then
v:keyreleased(k) for k,v in pairs(self.keys) do
end if self.keys[k].isPressed then
self.menus[self.focusedMenu]:keyreleased(k)
end end
end end
end end
end end
function MenuSystem:removeDestroyedMenus() function MenuSystem:removeDestroyedMenus()
@ -58,10 +61,6 @@ function MenuSystem:mousemoved(x, y, dx, dy)
if v.isActive then 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 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) 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 end
end end
@ -72,10 +71,6 @@ function MenuSystem:mousepressed( x, y, button, istouch )
if v.isActive then 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 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 ) 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 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 if widget_selected >= 1 and widget_selected <= #self.widget.list then
self.widget.selected = widget_selected self.widget.selected = widget_selected
self:getFocus()
end end
end end
@ -58,6 +59,7 @@ function ListBox:mousepressed(x, y, button, isTouch)
if widget_selected >= 1 and widget_selected <= #self.widget.list then if widget_selected >= 1 and widget_selected <= #self.widget.list then
self.widget.selected = widget_selected self.widget.selected = widget_selected
self:getFocus()
if #self.widget.list > 0 then if #self.widget.list > 0 then
self.widget.list[self.widget.selected]:action() self.widget.list[self.widget.selected]:action()
end end
@ -70,7 +72,7 @@ function ListBox:draw()
for i,v in ipairs(self.widget.list) do for i,v in ipairs(self.widget.list) do
if (i >= self.begin) and (i < self.begin + self.slots) then if (i >= self.begin) and (i < self.begin + self.slots) then
v:draw(self.x, widgety, self.w, self.widget.h) 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) v:drawSelected(self.x, widgety, self.w, self.widget.h)
else else
v:draw(self.x, widgety, self.w, self.widget.h) 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:updateWidgetSize()
self.isDestroyed = false self.isDestroyed = false
self.haveFocus = false
self.isVisible = true self.isVisible = true
self.isActive = true self.isActive = true
@ -28,6 +27,14 @@ function Menu:new(menusystem, name, x, y, w, h)
self:register() self:register()
end end
function Menu:getFocus()
self.menusystem.focusedMenu = self.name
end
function Menu:haveFocus()
return (self.menusystem.focusedMenu == self.name)
end
function Menu:register() function Menu:register()
self.menusystem:addMenu(self.name, self) self.menusystem:addMenu(self.name, self)
end end