mirror of
https://github.com/notwa/mm
synced 2025-02-05 13:23:23 -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)
|
function A(addr, atype)
|
||||||
|
-- TODO: inherit type, read, and write fields from appropriate class
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
addr=addr,
|
addr=addr,
|
||||||
type=atype,
|
type=atype,
|
||||||
|
@ -57,19 +58,24 @@ function A(addr, atype)
|
||||||
end
|
end
|
||||||
|
|
||||||
Class = function(inherit)
|
Class = function(inherit)
|
||||||
--[[ don't entirely like the idea but leaving it here
|
local class = {}
|
||||||
if type(inherit) == 'string' then
|
local mt_obj = {__index = class}
|
||||||
inherit = require("classes."..inherit)
|
local mt_class = {
|
||||||
end
|
|
||||||
--]]
|
|
||||||
return setmetatable({}, {
|
|
||||||
__call = function(self, ...)
|
__call = function(self, ...)
|
||||||
local obj = setmetatable({}, {__index = self})
|
local obj = setmetatable({}, mt_obj)
|
||||||
obj:init(...)
|
obj:init(...)
|
||||||
return obj
|
return obj
|
||||||
end,
|
end,
|
||||||
__index = inherit,
|
__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
|
end
|
||||||
|
|
||||||
function printf(fmt, ...)
|
function printf(fmt, ...)
|
||||||
|
|
|
@ -138,27 +138,11 @@ local input = InputHandler{
|
||||||
right = "P1 DPad R",
|
right = "P1 DPad R",
|
||||||
}
|
}
|
||||||
|
|
||||||
local menu = nil
|
local handle = MenuHandler(main_menu)
|
||||||
|
|
||||||
while mm or oot do
|
while mm or oot do
|
||||||
local ctrl, pressed = input:update()
|
local ctrl, pressed = input:update()
|
||||||
|
handle:update(ctrl, pressed)
|
||||||
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
|
|
||||||
|
|
||||||
for i, passive in ipairs(passives) do
|
for i, passive in ipairs(passives) do
|
||||||
passive:tick()
|
passive:tick()
|
||||||
|
|
|
@ -59,7 +59,10 @@ function Text:init(text)
|
||||||
self.text = text
|
self.text = text
|
||||||
end
|
end
|
||||||
function Text:draw(brush, y)
|
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)
|
brush(0, y, color, self.text)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -79,7 +82,7 @@ function LinkTo:run()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Active:init(text, callbacks)
|
function Active:init(text, callbacks)
|
||||||
self.text = text
|
Text.init(self, text)
|
||||||
self.callbacks = callbacks or {}
|
self.callbacks = callbacks or {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -174,12 +177,29 @@ function Screen:navigate(ctrl, pressed)
|
||||||
local i = self.item_sel
|
local i = self.item_sel
|
||||||
local old = self.items[i]
|
local old = self.items[i]
|
||||||
|
|
||||||
if pressed.down then i = i + 1 end
|
local direction
|
||||||
if pressed.up then i = i - 1 end
|
if pressed.down then direction = 'down' end
|
||||||
i = wrap(i, #self.items)
|
if pressed.up then direction = 'up' end
|
||||||
self.item_sel = i
|
|
||||||
|
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
|
if item ~= old then
|
||||||
old:unfocus()
|
old:unfocus()
|
||||||
|
@ -245,3 +265,28 @@ end
|
||||||
function Menu:draw(brush, y)
|
function Menu:draw(brush, y)
|
||||||
self.screens[self.screen_sel]:draw(brush, y)
|
self.screens[self.screen_sel]:draw(brush, y)
|
||||||
end
|
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…
Add table
Reference in a new issue