Skip to content

Commit

Permalink
plugis/viewswitch: Added hotkeys for cycling through views.
Browse files Browse the repository at this point in the history
  • Loading branch information
cuavas committed Jan 23, 2024
1 parent acd0117 commit 63dd8a6
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 68 deletions.
6 changes: 3 additions & 3 deletions plugins/ksys573_da_offset/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Konami System 573 digital audio offset plugin

This plugin works by intercepting audio timer register reads in Konami System 573 digital hardware games and may not work perfectly in every game depending on how it uses the timer register.
This plugin works by intercepting audio timer register reads in Konami System 573 digital hardware games. It may not work perfectly in every game, depending on how it uses the timer register.

## Configuration

Here is an example **settings.json** commented to demonstrate how to add game-specific offsets and a default offset to be used for all games on System 573 digital hardware without a specific offset specified.
Here is an example **settings.json** commented to demonstrate how to add game-specific offsets, as well as a default offset to be used for all games on System 573 digital hardware without a specific offset specified.

A default offset of 28 ms is what I personally found to be most useful when running MAME on Windows, with the PortAudio sound output module using the WASAPI back-end for lowest audio latency. I suggest you experiment with the default offset to figure out what works best for your setup.

Expand All @@ -15,7 +15,7 @@ A default offset of 28 ms is what I personally found to be most useful when run
// Can be specified as a number of samples (e.g. 1234) or a delay in milliseconds (e.g. 1234ms)
"overrides": {
"ddrmax": 1234, // Offset for the game "ddrmax" specified as a number of samples
"ddr5m": "50ms", // Offset for the gaem "ddr5m" specified as a delay in milliseconds (automatically converted to a number of samples)
"ddr5m": "50ms", // Offset for the game "ddr5m" specified as a delay in milliseconds (automatically converted to a number of samples)
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion plugins/viewswitch/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Quick view switch plugin

This plugin allows you to configure hotkeys to switch views.
This plugin allows you to configure hotkeys to switch directly to views or to cycle through available views.

## License

Expand Down
23 changes: 19 additions & 4 deletions plugins/viewswitch/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- copyright-holders:Vas Crabb
local exports = {
name = 'viewswitch',
version = '0.0.1',
version = '0.0.2',
description = 'Quick view switch plugin',
license = 'BSD-3-Clause',
author = { name = 'Vas Crabb' } }
Expand All @@ -13,6 +13,7 @@ local stop_subscription

function viewswitch.startplugin()
local switch_hotkeys = { }
local cycle_hotkeys = { }

local input_manager
local ui_manager = { menu_active = true, ui_active = true }
Expand All @@ -26,12 +27,25 @@ function viewswitch.startplugin()
render_targets[hotkey.target].view_index = hotkey.view
end
end
for k, hotkey in pairs(cycle_hotkeys) do
if input_manager:seq_pressed(hotkey.sequence) then
if not hotkey.pressed then
local target = render_targets[hotkey.target]
local index = target.view_index + hotkey.increment
local count = #target.view_names
target.view_index = (index < 1) and count or (index > count) and 1 or index
hotkey.pressed = true
end
else
hotkey.pressed = false
end
end
end
end

local function start()
local persister = require('viewswitch/viewswitch_persist')
switch_hotkeys = persister:load_settings()
switch_hotkeys, cycle_hotkeys = persister:load_settings()

local machine = manager.machine
input_manager = machine.input
Expand All @@ -41,13 +55,14 @@ function viewswitch.startplugin()

local function stop()
local persister = require('viewswitch/viewswitch_persist')
persister:save_settings(switch_hotkeys)
persister:save_settings(switch_hotkeys, cycle_hotkeys)

menu_handler = nil
render_targets = nil
ui_manager = { menu_active = true, ui_active = true }
input_manager = nil
switch_hotkeys = { }
cycle_hotkeys = { }
end

local function menu_callback(index, event)
Expand All @@ -61,7 +76,7 @@ function viewswitch.startplugin()
emu.print_error(string.format('Error loading quick view switch menu: %s', msg))
end
if menu_handler then
menu_handler:init(switch_hotkeys)
menu_handler:init(switch_hotkeys, cycle_hotkeys)
end
end
if menu_handler then
Expand Down
2 changes: 1 addition & 1 deletion plugins/viewswitch/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"plugin": {
"name": "viewswitch",
"description": "Quick view switch plugin",
"version": "0.0.1",
"version": "0.0.2",
"author": "Vas Crabb",
"type": "plugin",
"start": "false"
Expand Down
143 changes: 138 additions & 5 deletions plugins/viewswitch/viewswitch_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

local MENU_TYPES = {
MAIN = 0,
SWITCH = 2 }
SWITCH = 2,
CYCLE = 3 }


-- helper functions
Expand Down Expand Up @@ -33,14 +34,15 @@ local menu_stack
local commonui

local switch_hotkeys


-- quick switch hotkeys menu
local cycle_hotkeys

local switch_target_start
local switch_done
local switch_poll


-- quick switch hotkeys menu

local function handle_switch(index, event)
if switch_poll then
-- special handling for entering hotkey
Expand Down Expand Up @@ -149,13 +151,138 @@ local function populate_switch()
end


-- cycle hotkeys menu

local function handle_cycle(index, event)
if switch_poll then
-- special handling for entering hotkey
if switch_poll.poller:poll() then
if switch_poll.poller.sequence then
local found
for k, hotkey in pairs(cycle_hotkeys) do
if (hotkey.target == switch_poll.target) and (hotkey.increment == switch_poll.increment) then
found = hotkey
break
end
end
if not found then
found = { target = switch_poll.target, increment = switch_poll.increment }
table.insert(cycle_hotkeys, found)
end
found.sequence = switch_poll.poller.sequence
found.config = manager.machine.input:seq_to_tokens(switch_poll.poller.sequence)
end
switch_poll = nil
return true
end
return false
end

if (event == 'back') or ((event == 'select') and (index == switch_done)) then
switch_target_start = nil
switch_done = nil
table.remove(menu_stack)
return true
else
for target = #switch_target_start, 1, -1 do
if index >= switch_target_start[target] then
local increment = ((index - switch_target_start[target]) == 0) and 1 or -1
if event == 'select' then
if not commonui then
commonui = require('commonui')
end
switch_poll = { target = target, increment = increment, poller = commonui.switch_polling_helper() }
return true
elseif event == 'clear' then
for k, hotkey in pairs(cycle_hotkeys) do
if (hotkey.target == target) and (hotkey.increment == increment) then
table.remove(cycle_hotkeys, k)
return true
end
end
end
return false
end
end
end
return false
end

local function populate_cycle()
-- find targets with selectable views
local targets = get_targets()

switch_target_start = { }
local items = { }

table.insert(items, { 'Cycle Hotkeys', '', 'off' })
table.insert(items, { string.format('Press %s to clear hotkey', general_input_setting('UI_CLEAR')), '', 'off' })

if #targets == 0 then
table.insert(items, { '---', '', '' })
table.insert(items, { 'No selectable views', '', 'off' })
else
local input = manager.machine.input
for i, target in pairs(targets) do
-- add separator and target heading if multiple targets
table.insert(items, { '---', '', '' })
if #targets > 1 then
table.insert(items, { string.format('Screen #%d', target.index - 1), '', 'off' })
end
table.insert(switch_target_start, #items + 1)

-- add items for next view and previous view
local seq
local flags
seq = 'None'
flags = ''
for k, hotkey in pairs(cycle_hotkeys) do
if (hotkey.target == target.index) and (hotkey.increment == 1) then
seq = input:seq_name(hotkey.sequence)
break
end
end
if switch_poll and (switch_poll.target == target.index) and (switch_poll.increment == 1) then
flags = 'lr'
end
table.insert(items, { 'Next view', seq, flags })
seq = 'None'
flags = ''
for k, hotkey in pairs(cycle_hotkeys) do
if (hotkey.target == target.index) and (hotkey.increment == -1) then
seq = input:seq_name(hotkey.sequence)
break
end
end
if switch_poll and (switch_poll.target == target.index) and (switch_poll.increment == -1) then
flags = 'lr'
end
table.insert(items, { 'Previous view', seq, flags })
end
end

table.insert(items, { '---', '', '' })
table.insert(items, { 'Done', '', '' })
switch_done = #items

if switch_poll then
return switch_poll.poller:overlay(items)
else
return items
end
end


-- main menu

local function handle_main(index, event)
if event == 'select' then
if index == 3 then
table.insert(menu_stack, MENU_TYPES.SWITCH)
return true
elseif index == 4 then
table.insert(menu_stack, MENU_TYPES.CYCLE)
return true
end
end
return false
Expand All @@ -167,6 +294,7 @@ local function populate_main()
table.insert(items, { 'Quick View Switch', '', 'off' })
table.insert(items, { '---', '', '' })
table.insert(items, { 'Quick switch hotkeys', '', '' })
table.insert(items, { 'Cycle hotkeys', '', '' })

return items
end
Expand All @@ -176,9 +304,10 @@ end

local lib = { }

function lib:init(switch)
function lib:init(switch, cycle)
menu_stack = { MENU_TYPES.MAIN }
switch_hotkeys = switch
cycle_hotkeys = cycle
end

function lib:handle_event(index, event)
Expand All @@ -187,6 +316,8 @@ function lib:handle_event(index, event)
return handle_main(index, event)
elseif current == MENU_TYPES.SWITCH then
return handle_switch(index, event)
elseif current == MENU_TYPES.CYCLE then
return handle_cycle(index, event)
end
end

Expand All @@ -196,6 +327,8 @@ function lib:populate()
return populate_main()
elseif current == MENU_TYPES.SWITCH then
return populate_switch()
elseif current == MENU_TYPES.CYCLE then
return populate_cycle()
end
end

Expand Down
Loading

0 comments on commit 63dd8a6

Please sign in to comment.