From 4c00f0b3fbfa84e684bfea413afb8f8d39bacc55 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Tue, 26 May 2015 08:15:36 -0700 Subject: [PATCH] abstract menu handling, make Text unselectable --- Lua/boilerplate.lua | 22 +++++++++++------ Lua/cheat menu.lua | 20 ++------------- Lua/menu classes.lua | 59 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 68 insertions(+), 33 deletions(-) diff --git a/Lua/boilerplate.lua b/Lua/boilerplate.lua index 5130e97..367b9b3 100755 --- a/Lua/boilerplate.lua +++ b/Lua/boilerplate.lua @@ -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, ...) diff --git a/Lua/cheat menu.lua b/Lua/cheat menu.lua index 8e80d48..032267c 100755 --- a/Lua/cheat menu.lua +++ b/Lua/cheat menu.lua @@ -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() diff --git a/Lua/menu classes.lua b/Lua/menu classes.lua index ad84a27..800769e 100644 --- a/Lua/menu classes.lua +++ b/Lua/menu classes.lua @@ -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