mirror of
https://github.com/notwa/mm
synced 2024-11-04 20:09:03 -08:00
abstract menu handling, make Text unselectable
This commit is contained in:
parent
8395f3dc78
commit
4c00f0b3fb
3 changed files with 68 additions and 33 deletions
|
@ -48,6 +48,7 @@ local mt = {
|
|||
}
|
||||
|
||||
function A(addr, atype)
|
||||
-- TODO: inherit type, read, and write fields from appropriate class
|
||||
return setmetatable({
|
||||
addr=addr,
|
||||
type=atype,
|
||||
|
@ -57,19 +58,24 @@ function A(addr, atype)
|
|||
end
|
||||
|
||||
Class = function(inherit)
|
||||
--[[ don't entirely like the idea but leaving it here
|
||||
if type(inherit) == 'string' then
|
||||
inherit = require("classes."..inherit)
|
||||
end
|
||||
--]]
|
||||
return setmetatable({}, {
|
||||
local class = {}
|
||||
local mt_obj = {__index = class}
|
||||
local mt_class = {
|
||||
__call = function(self, ...)
|
||||
local obj = setmetatable({}, {__index = self})
|
||||
local obj = setmetatable({}, mt_obj)
|
||||
obj:init(...)
|
||||
return obj
|
||||
end,
|
||||
__index = inherit,
|
||||
})
|
||||
}
|
||||
|
||||
return setmetatable(class, mt_class)
|
||||
end
|
||||
|
||||
function getindex(obj)
|
||||
local gm = getmetatable(obj)
|
||||
if not gm then return end
|
||||
return gm.__index
|
||||
end
|
||||
|
||||
function printf(fmt, ...)
|
||||
|
|
|
@ -138,27 +138,11 @@ local input = InputHandler{
|
|||
right = "P1 DPad R",
|
||||
}
|
||||
|
||||
local menu = nil
|
||||
local handle = MenuHandler(main_menu)
|
||||
|
||||
while mm or oot do
|
||||
local ctrl, pressed = input:update()
|
||||
|
||||
local delay = false
|
||||
if not menu and pressed.enter then
|
||||
delay = true
|
||||
menu = main_menu
|
||||
menu:focus()
|
||||
end
|
||||
|
||||
if menu and not delay then
|
||||
local old = menu
|
||||
menu = menu:navigate(ctrl, pressed)
|
||||
if menu ~= old then
|
||||
old:unfocus()
|
||||
if menu then menu:focus() end
|
||||
end
|
||||
end
|
||||
if menu then menu:draw(T_TL, 0) end
|
||||
handle:update(ctrl, pressed)
|
||||
|
||||
for i, passive in ipairs(passives) do
|
||||
passive:tick()
|
||||
|
|
|
@ -59,7 +59,10 @@ function Text:init(text)
|
|||
self.text = text
|
||||
end
|
||||
function Text:draw(brush, y)
|
||||
local color = self.focused and 'yellow' or 'white'
|
||||
local color = 'cyan'
|
||||
if getindex(self) ~= Text then
|
||||
color = self.focused and 'yellow' or 'white'
|
||||
end
|
||||
brush(0, y, color, self.text)
|
||||
end
|
||||
|
||||
|
@ -79,7 +82,7 @@ function LinkTo:run()
|
|||
end
|
||||
|
||||
function Active:init(text, callbacks)
|
||||
self.text = text
|
||||
Text.init(self, text)
|
||||
self.callbacks = callbacks or {}
|
||||
end
|
||||
|
||||
|
@ -174,12 +177,29 @@ function Screen:navigate(ctrl, pressed)
|
|||
local i = self.item_sel
|
||||
local old = self.items[i]
|
||||
|
||||
if pressed.down then i = i + 1 end
|
||||
if pressed.up then i = i - 1 end
|
||||
i = wrap(i, #self.items)
|
||||
self.item_sel = i
|
||||
local direction
|
||||
if pressed.down then direction = 'down' end
|
||||
if pressed.up then direction = 'up' end
|
||||
|
||||
local item
|
||||
for give_up = 0, 100 do
|
||||
if give_up >= 100 then
|
||||
error("couldn't find a suitable menu item to select", 1)
|
||||
end
|
||||
|
||||
if direction == 'down' then i = i + 1 end
|
||||
if direction == 'up' then i = i - 1 end
|
||||
i = wrap(i, #self.items)
|
||||
self.item_sel = i
|
||||
item = self.items[i]
|
||||
|
||||
if getindex(item) ~= Text then
|
||||
break
|
||||
elseif direction == nil then
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
|
||||
local item = self.items[i]
|
||||
|
||||
if item ~= old then
|
||||
old:unfocus()
|
||||
|
@ -245,3 +265,28 @@ end
|
|||
function Menu:draw(brush, y)
|
||||
self.screens[self.screen_sel]:draw(brush, y)
|
||||
end
|
||||
|
||||
MenuHandler = Class()
|
||||
function MenuHandler:init(main_menu)
|
||||
self.main_menu = main_menu
|
||||
self.menu = nil
|
||||
end
|
||||
|
||||
function MenuHandler:update(ctrl, pressed)
|
||||
local delay = false
|
||||
if not self.menu and pressed.enter then
|
||||
delay = true
|
||||
self.menu = self.main_menu
|
||||
self.menu:focus()
|
||||
end
|
||||
|
||||
if self.menu and not delay then
|
||||
local old = self.menu
|
||||
self.menu = self.menu:navigate(ctrl, pressed)
|
||||
if self.menu ~= old then
|
||||
old:unfocus()
|
||||
if self.menu then self.menu:focus() end
|
||||
end
|
||||
end
|
||||
if self.menu then self.menu:draw(T_TL, 0) end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue