diff options
author | 2019-05-14 07:26:27 -0700 | |
---|---|---|
committer | 2019-05-14 10:26:27 -0400 | |
commit | 0040650300e4d60406e3ad27d7aee914195dd69e (patch) | |
tree | 4a91198dd84e2252a01211830e80d47fcb51f7be /plugins/autofire/init.lua | |
parent | e24a0a19308457e5a23cd84a35fffea53701eea5 (diff) |
Add plugin for autofire (#5050)
* Hardcoded autofire plugin
* Changed register_frame to register_frame_done, removed pause check
* Added support for multiple buttons loaded from a file
* Implemented saving settings on quit
* Fixed multiple keybindings for same button from overwriting each other
* Replaced double-quotes with single-quotes
* Refactored saving/loading into a separate module
* Changed button format to use input_code rather than string token
Settings format is unchanged (still saved as string token).
* Rewrote table initialization in save/load
* Implemented menus
* Added helper messages for "on frames"/"off frames"
Diffstat (limited to 'plugins/autofire/init.lua')
-rw-r--r-- | plugins/autofire/init.lua | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/plugins/autofire/init.lua b/plugins/autofire/init.lua new file mode 100644 index 00000000000..fd268b65ca3 --- /dev/null +++ b/plugins/autofire/init.lua @@ -0,0 +1,95 @@ +-- license:BSD-3-Clause +-- copyright-holders:Jack Li +local exports = {} +exports.name = 'autofire' +exports.version = '0.0.1' +exports.description = 'Autofire plugin' +exports.license = 'The BSD 3-Clause License' +exports.author = { name = 'Jack Li' } + +local autofire = exports + +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 + -- '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 function process_button(button) + local pressed = manager:machine():input():code_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 button_states_key(button) + return button.port .. '\0' .. button.field + 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 + end + for i, button in ipairs(buttons) do + button.button:set_value(button_states[button_states_key(button)]) + end + end + + local function load_settings() + local loader = require('autofire/autofire_save') + if loader then + buttons = loader:load_settings() + end + end + + local function save_settings() + local saver = require('autofire/autofire_save') + if saver then + saver:save_settings(buttons) + end + 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 + return false + end + end + + local function menu_populate() + local menu_handler = require('autofire/autofire_menu') + if menu_handler then + return menu_handler:populate_menu(buttons) + else + return {{_('Failed to load autofire menu'), '', ''}} + end + end + + emu.register_frame_done(process_frame) + emu.register_start(load_settings) + emu.register_stop(save_settings) + emu.register_menu(menu_callback, menu_populate, _('Autofire')) +end + +return exports |