From f2839bb69c078d0df7c4c3fac71dd257be9acfd6 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sun, 15 Nov 2015 09:43:29 -0800 Subject: [PATCH] allow a single function for simple callbacks --- Lua/cheat menu.lua | 52 ++++++++++++-------------------------------- Lua/menu classes.lua | 10 ++++++++- Lua/menus/warp.lua | 3 +-- 3 files changed, 24 insertions(+), 41 deletions(-) diff --git a/Lua/cheat menu.lua b/Lua/cheat menu.lua index 478ae62..27bf20e 100755 --- a/Lua/cheat menu.lua +++ b/Lua/cheat menu.lua @@ -43,17 +43,12 @@ local function save() serialize(fn, saved) end -dummy = Callbacks() - function Setter(t) - local cb = Callbacks() - function cb:on() - for addr, value in pairs(t) do - addr(value) + return function() + for func, value in pairs(t) do + func(value) end end - cb.hold = cb.on - return cb end local passives = {} @@ -101,18 +96,7 @@ function any_item:tick_on() addrs.buttons_enabled(0) end -local everything = Callbacks() -function everything:on() - dofile("oneshot.lua") -end - -local escape_cutscene = Callbacks() -function escape_cutscene:on() - addrs.cutscene_status_2(3) -end - -local soft_reset = Callbacks() -function soft_reset:on() +local function soft_reset() if oot then -- FIXME: Link voids out on title screen. -- need to load title screen save? @@ -128,8 +112,7 @@ function soft_reset:on() end end -local save_pos = Callbacks() -function save_pos:on() +local function save_pos() local la = addrs.link_actor saved.pos = {} local pos = saved.pos @@ -141,8 +124,7 @@ function save_pos:on() pos.isg = la.sword_active() save() end -local load_pos = Callbacks() -function load_pos:on() +local function load_pos() local la = addrs.link_actor local pos = saved.pos if pos == nil then return end @@ -157,27 +139,23 @@ function load_pos:on() la.sword_active(pos.isg) end -reload_scene = Callbacks() -function reload_scene:on() +local function reload_scene() local ev = addrs.exit_value() addrs.warp_begin(0x14) addrs.warp_destination(ev) end -local save_scene = Callbacks() -function save_scene:on() +local function save_scene() saved.scene = addrs.exit_value() save() end -local load_scene = Callbacks() -function load_scene:on() +local function load_scene() if saved.scene == nil then return end addrs.warp_begin(0x14) addrs.warp_destination(saved.scene) end -local save_scene_pos = Callbacks() -function save_scene_pos:on() +local function save_scene_pos() saved.scenepos = {} local sp = saved.scenepos sp.scene = addrs.exit_value() @@ -189,8 +167,7 @@ function save_scene_pos:on() --sp.room = la.room_number() save() end -local load_scene_pos = Callbacks() -function load_scene_pos:on() +local function load_scene_pos() local sp = saved.scenepos if sp == nil then return end addrs.warp_begin(0x14) @@ -207,8 +184,7 @@ function load_scene_pos:on() --voidout_room_number(sp.room) end -local kill_fades = Callbacks() -function kill_fades:on() +local function kill_fades() local et = addrs.entrance_table if et == nil then return end local et_size = 1244 @@ -275,10 +251,10 @@ local main_menu = Menu{ Toggle("Infinite Items", infinite_items), Toggle("Use Any Item", any_item), Text(""), - Oneshot("100% Items", everything), + Oneshot("100% Items", Setter{[dofile]="oneshot.lua"}), LinkTo("Set Progress...", progress_menu), Text(""), - Oneshot("Escape Cutscene", escape_cutscene), + Oneshot("Escape Cutscene", Setter{[addrs.cutscene_status_2]=3}), Text(""), LinkTo("Play as...", playas_menu), Oneshot("Kill Link", Setter{[addrs.hearts]=0}), diff --git a/Lua/menu classes.lua b/Lua/menu classes.lua index cd5b93f..f549f85 100644 --- a/Lua/menu classes.lua +++ b/Lua/menu classes.lua @@ -33,6 +33,8 @@ end function Callbacks:release() end +local dummy = Callbacks() + function MenuItem:init() self.focused = false end @@ -89,7 +91,13 @@ end function Active:init(text, callbacks) Text.init(self, text) - self.callbacks = callbacks or {} + if type(callbacks) == 'function' then + local f = callbacks + callbacks = Callbacks() + function callbacks:on() f() end + callbacks.hold = callbacks.on + end + self.callbacks = callbacks or dummy end function Toggle:init(text, callbacks) diff --git a/Lua/menus/warp.lua b/Lua/menus/warp.lua index 1f76d55..81ae2e6 100644 --- a/Lua/menus/warp.lua +++ b/Lua/menus/warp.lua @@ -157,8 +157,7 @@ for si=0x00,0x7F do if j ~= 0 then break end ename = "[crash?]" end - local callback = Callbacks() - function callback:on() + local callback = function() addrs.warp_destination(make_exit_value(si,j,0)) addrs.warp_begin(0x14) end