summaryrefslogtreecommitdiffstatshomepage
path: root/plugins/autofire/init.lua
diff options
context:
space:
mode:
author Vas Crabb <vas@vastheman.com>2021-11-04 09:55:26 +1100
committer Vas Crabb <vas@vastheman.com>2021-11-04 09:55:26 +1100
commit8c6cb643e3d38251ae3c4f8b18a1ccd58020a707 (patch)
tree79b599cfd0f2d64fcfe868b3630f294aa904e01e /plugins/autofire/init.lua
parente44d51c21fe2248a33ab8c760e1ac52c6f70d6fb (diff)
plugins: Simplify logic in autofire and inputmacro plugins to try and avoid leaking state across sessions.
Diffstat (limited to 'plugins/autofire/init.lua')
-rw-r--r--plugins/autofire/init.lua86
1 files changed, 37 insertions, 49 deletions
diff --git a/plugins/autofire/init.lua b/plugins/autofire/init.lua
index d93bf854168..ca81f3ad410 100644
--- a/plugins/autofire/init.lua
+++ b/plugins/autofire/init.lua
@@ -13,69 +13,50 @@ function autofire.startplugin()
-- List of autofire buttons, each being a table with keys:
-- 'port' - port name of the button being autofired
- -- 'field' - field name of the button being autofired
- -- 'key' - input_code of the keybinding
+ -- 'mask' - mask of the button field being autofired
+ -- 'type' - input type of the button being autofired
+ -- 'key' - input_seq of the keybinding
-- 'on_frames' - number of frames button is pressed
-- 'off_frames' - number of frames button is released
-- 'button' - reference to ioport_field
-- 'counter' - position in autofire cycle
local buttons = {}
- local current_rom = nil
+ local menu_handler
- local function process_button(button)
- local pressed = manager.machine.input:seq_pressed(button.key)
- if pressed then
- local state = button.counter < button.on_frames and 1 or 0
- button.counter = (button.counter + 1) % (button.on_frames + button.off_frames)
- return state
- else
- button.counter = 0
- return 0
- end
- end
+ local function process_frame()
+ local input = manager.machine.input
- local function button_states_key(button)
- return button.port .. '\0' .. button.field
- end
+ local function process_button(button)
+ local pressed = input:seq_pressed(button.key)
+ if pressed then
+ local state = button.counter < button.on_frames and 1 or 0
+ button.counter = (button.counter + 1) % (button.on_frames + button.off_frames)
+ return state
+ else
+ button.counter = 0
+ return 0
+ end
+ end
- local function process_frame()
-- Resolves conflicts between multiple autofire keybindings for the same button.
local button_states = {}
for i, button in ipairs(buttons) do
- local state = button_states[button_states_key(button)]
- if not state then
- state = 0
- end
- state = process_button(button) | state
- button_states[button_states_key(button)] = state
+ local key = button.port .. '\0' .. button.mask .. '.' .. button.type
+ local state = button_states[key] or { 0, button.button }
+ state[1] = process_button(button) | state[1]
+ button_states[key] = state
end
- for i, button in ipairs(buttons) do
- button.button:set_value(button_states[button_states_key(button)])
- end
- end
-
- local function reinit_buttons()
- for i, button in ipairs(buttons) do
- button.counter = 0
- button.button = manager.machine.ioport.ports[button.port].fields[button.field]
+ for i, state in pairs(button_states) do
+ state[2]:set_value(state[1])
end
end
local function load_settings()
- if current_rom == emu.romname() then
- reinit_buttons()
- else
- local loader = require('autofire/autofire_save')
- if loader then
- buttons = loader:load_settings()
- end
- end
- current_rom = emu.romname()
- local menu_handler = require('autofire/autofire_menu')
- if menu_handler then
- menu_handler:init_menu(buttons)
+ local loader = require('autofire/autofire_save')
+ if loader then
+ buttons = loader:load_settings()
end
end
@@ -84,10 +65,12 @@ function autofire.startplugin()
if saver then
saver:save_settings(buttons)
end
+
+ menu_handler = nil
+ buttons = {}
end
local function menu_callback(index, event)
- local menu_handler = require('autofire/autofire_menu')
if menu_handler then
return menu_handler:handle_menu_event(index, event, buttons)
else
@@ -96,7 +79,12 @@ function autofire.startplugin()
end
local function menu_populate()
- local menu_handler = require('autofire/autofire_menu')
+ if not menu_handler then
+ menu_handler = require('autofire/autofire_menu')
+ if menu_handler then
+ menu_handler:init_menu(buttons)
+ end
+ end
if menu_handler then
return menu_handler:populate_menu(buttons)
else
@@ -104,8 +92,8 @@ function autofire.startplugin()
end
end
- emu.register_frame_done(process_frame)
- emu.register_start(load_settings)
+ emu.register_frame(process_frame)
+ emu.register_prestart(load_settings)
emu.register_stop(save_settings)
emu.register_menu(menu_callback, menu_populate, _p('plugin-autofire', 'Autofire'))
end