File: offscreenreload_menu.lua

package info (click to toggle)
mame 0.286%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 930,472 kB
  • sloc: cpp: 5,499,413; xml: 2,250,892; ansic: 752,116; sh: 34,431; lisp: 19,643; python: 17,598; makefile: 13,246; java: 8,492; yacc: 8,152; javascript: 7,147; cs: 6,013; asm: 4,786; ada: 1,681; pascal: 1,191; lex: 1,174; perl: 585; ruby: 373
file content (418 lines) | stat: -rw-r--r-- 11,498 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
-- license:BSD-3-Clause
-- copyright-holders:Vas Crabb


-- Constants

local MENU_TYPES = { HELPERS = 0, ADD = 1, EDIT = 2, INPUT = 3 }


-- Globals

local commonui
local helpers
local menu_stack

local helpers_start_helper -- really for the helpers menu, but have to be declared local before edit menu functions
local helpers_item_first_helper
local helpers_selection_save


-- Helpers

local function new_helper()
	return {
		binding = nil,
		bindingcfg = '',
		axis = { port = nil, mask = nil, type = nil, field = nil },
		button = { port = nil, mask = nil, type = nil, field = nil } }
end

local function input_display_name(input)
	if input.field then
		return input.field.name
	elseif input.port then
		return _p('plugin-offscreenreload', 'n/a')
	else
		return _p('plugin-offscreenreload', '[not set]')
	end
end


-- Input menu

local input_menu
local input_start_field

local function start_input_menu(handler, title, filter, start_field)
	local function action(field)
		if field then
			handler(field)
		end
		table.remove(menu_stack)
		input_menu = nil
		input_start_field = nil
	end

	if not commonui then
		commonui = require('commonui')
	end
	input_menu = commonui.input_selection_menu(action, title, filter)
	input_start_field = start_field
	table.insert(menu_stack, MENU_TYPES.INPUT)
end

local function handle_input(index, action)
	return input_menu:handle(index, action)
end

local function populate_input()
	return input_menu:populate(input_start_field)
end


-- Add/edit menus

local edit_current_helper
local edit_start_selection
local edit_menu_active
local edit_items
local edit_item_delete
local edit_item_exit
local edit_switch_poller

local function current_helper_complete()
	if not edit_current_helper.binding then
		return false
	end
	if not edit_current_helper.axis.field then
		return false
	end
	if not edit_current_helper.button.field then
		return false
	end
	return true
end

local function handle_edit_items(index, event)
	if edit_switch_poller then
		if edit_switch_poller:poll() then
			if edit_switch_poller.sequence then
				edit_current_helper.binding = edit_switch_poller.sequence
				edit_current_helper.bindingcfg = manager.machine.input:seq_to_tokens(edit_switch_poller.sequence)
			end
			edit_switch_poller = nil
			return true
		end
		return false
	end

	local command = edit_items[index]

	if (event ~= 'select') or (not command) then
		return false
	elseif command.action == 'binding' then
		if not commonui then
			commonui = require('commonui')
		end
		edit_switch_poller = commonui.switch_polling_helper()
		return true
	elseif command.action == 'axis' then
		local function handler(field)
			local device = field.device.tag
			local player = field.player
			local guessbutton = false
			if (not edit_current_helper.axis) or (not edit_current_helper.axis.field) then
				guessbutton = true
			elseif (edit_current_helper.axis.field.device.tag ~= field) or (edit_current_helper.axis.field.player ~= player) then
				guessbutton = true
			end

			edit_current_helper.axis.port = field.port.tag
			edit_current_helper.axis.mask = field.mask
			edit_current_helper.axis.type = field.type
			edit_current_helper.axis.field = field

			if guessbutton then
				local ioport = manager.machine.ioport
				local b1type = ioport:token_to_input_type('P1_BUTTON1')
				local done = false
				for i, port in pairs(ioport.ports) do
					if port.device.tag == device then
						for j, f in pairs(port.fields) do
							if (f.player == player) and (f.type == b1type) then
								edit_current_helper.button.port = f.port.tag
								edit_current_helper.button.mask = f.mask
								edit_current_helper.button.type = f.type
								edit_current_helper.button.field = f
								done = true
								break
							end
						end
					end
					if done then
						break
					end
				end
			end
		end
		local function filter(field)
			if (not field.is_analog) or field.analog_wraps then
				return false
			else
				return true
			end
		end
		start_input_menu(handler, _p('plugin-offscreenreload', 'Set Axis'), filter, edit_current_helper.axis.field)
		edit_start_selection = index
		return true
	elseif command.action == 'button' then
		local function handler(field)
			edit_current_helper.button.port = field.port.tag
			edit_current_helper.button.mask = field.mask
			edit_current_helper.button.type = field.type
			edit_current_helper.button.field = field
		end
		local function filter(field)
			if field.is_analog or field.is_toggle then
				return false
			elseif (field.type_class == 'config') or (field.type_class == 'dipswitch') then
				return false
			else
				return true
			end
		end
		start_input_menu(handler, _p('plugin-offscreenreload', 'Set Trigger'), filter, edit_current_helper.axis.button)
		edit_start_selection = index
		return true
	end

	return false
end

local function add_edit_items(items)
	edit_items = { }
	local input = manager.machine.input

	local binding = edit_current_helper.binding
	local activation = binding and input:seq_name(binding) or _p('plugin-offscreenreload', '[not set]')
	table.insert(items, { _p('plugin-offscreenreload', 'Reload combination'), activation, edit_switch_poller and 'lr' or '' })
	edit_items[#items] = { action = 'binding' }
	if not (edit_start_selection or edit_menu_active) then
		edit_start_selection = #items
	end
	edit_menu_active = true

	local axisname = input_display_name(edit_current_helper.axis)
	table.insert(items, { _p('plugin-offscreenreload', 'Axis input'), axisname, '' })
	edit_items[#items] = { action = 'axis' }

	local buttonname = input_display_name(edit_current_helper.button)
	table.insert(items, { _p('plugin-offscreenreload', 'Trigger input'), buttonname, '' })
	edit_items[#items] = { action = 'button' }
end

local function handle_add(index, event)
	local handled, selection = handle_edit_items(index, event)
	if handled then
		return true, selection
	elseif event == 'back' then
		edit_current_helper = nil
		edit_menu_active = false
		edit_items = nil
		table.remove(menu_stack)
		return true, selection
	elseif (index == edit_item_exit) and (event == 'select') then
		if current_helper_complete() then
			table.insert(helpers, edit_current_helper)
			helpers_start_helper = #helpers
		end
		edit_current_helper = nil
		edit_menu_active = false
		edit_items = nil
		table.remove(menu_stack)
		return true, selection
	end
	return false, selection
end

local function handle_edit(index, event)
	local handled, selection = handle_edit_items(index, event)
	if handled then
		return true, selection
	elseif (index == edit_item_delete) and (event == 'select') then
		local helper = helpers_selection_save - helpers_item_first_helper + 1
		table.remove(helpers, helper)
		if helper > #helpers then
			helpers_selection_save = helpers_selection_save - 1
		end
		edit_current_macro = nil
		edit_menu_active = false
		edit_items = nil
		table.remove(menu_stack)
		return true, selection
	elseif (event == 'back') or ((index == edit_item_exit) and (event == 'select')) then
		edit_current_helper = nil
		edit_menu_active = false
		edit_items = nil
		table.remove(menu_stack)
		return true, selection
	end
	return false, selection
end

local function populate_add()
	local items = { }

	table.insert(items, { _p('plugin-offscreenreload', 'Add Reload Helper'), '', 'off' })
	table.insert(items, { '---', '', '' })

	add_edit_items(items)

	table.insert(items, { '---', '', '' })
	if current_helper_complete() then
		table.insert(items, { _p('plugin-offscreenreload', 'Create'), '', '' })
	else
		table.insert(items, { _p('plugin-offscreenreload', 'Cancel'), '', '' })
	end
	edit_item_exit = #items

	local selection = edit_start_selection
	edit_start_selection = nil
	if edit_switch_poller then
		return edit_switch_poller:overlay(items, selection, '')
	else
		return items, selection, 'lrrepeat'
	end
end

local function populate_edit()
	local items = { }

	table.insert(items, { _p('plugin-offscreenreload', 'Edit Reload Helper'), '', 'off' })
	table.insert(items, { '---', '', '' })

	add_edit_items(items)

	table.insert(items, { '---', '', '' })
	table.insert(items, { _p('plugin-offscreenreload', 'Delete reload helper'), '', '' })
	edit_item_delete = #items

	table.insert(items, { '---', '', '' })
	table.insert(items, { _p('plugin-offscreenreload', 'Done'), '', '' })
	edit_item_exit = #items

	local selection = edit_start_selection
	edit_start_selection = nil
	if edit_switch_poller then
		return edit_switch_poller:overlay(items, selection, 'lrrepeat')
	else
		return items, selection, 'lrrepeat'
	end
end


-- Helpers menu

local helpers_item_add

function handle_helpers(index, event)
	if index == helpers_item_add then
		if event == 'select' then
			edit_current_helper = new_helper()
			helpers_selection_save = index
			table.insert(menu_stack, MENU_TYPES.ADD)
			return true
		end
	elseif index >= helpers_item_first_helper then
		local helper = index - helpers_item_first_helper + 1
		if event == 'select' then
			edit_current_helper = helpers[helper]
			helpers_selection_save = index
			table.insert(menu_stack, MENU_TYPES.EDIT)
			return true
		elseif event == 'clear' then
			table.remove(helpers, helper)
			if #helpers > 0 then
				helpers_selection_save = index
				if helper > #helpers then
					helpers_selection_save = helpers_selection_save - 1
				end
			end
			return true
		end
	end
	return false
end

local function populate_helpers()
	local input = manager.machine.input
	local ioport = manager.machine.ioport
	local items = { }

	table.insert(items, { _p('plugin-offscreenreload', 'Off-Screen Reload Helper'), '', 'off' })
	table.insert(items, { string.format(_p('plugin-offscreenreload', 'Press %s to delete'), manager.ui:get_general_input_setting(ioport:token_to_input_type('UI_CLEAR'))), '', 'off' })
	table.insert(items, { '---', '', '' })

	helpers_item_first_helper = #items + 1
	if #helpers > 0 then
		for index, helper in ipairs(helpers) do
			local bindingname = input:seq_name(helper.binding)
			local axisname = input_display_name(helper.axis)
			local buttonname = input_display_name(helper.button)
			table.insert(items, { string.format(_p('plugin-offscreenreload', '%s/%s'), axisname, buttonname), bindingname, '' })
			if helpers_start_helper == index then
				helpers_selection_save = #items
			end
		end
	else
		table.insert(items, { _p('plugin-offscreenreload', '[no reload helpers]'), '', 'off' })
	end
	helpers_start_helper = nil

	table.insert(items, { '---', '', '' })
	table.insert(items, { _p('plugin-offscreenreload', 'Add reload helper'), '', '' })
	helpers_item_add = #items

	local selection = helpers_selection_save
	helpers_selection_save = nil
	return items, selection
end


-- Entry points

local lib = { }

function lib:init(h)
	helpers = h
	menu_stack = { MENU_TYPES.HELPERS }
end

function lib:handle_event(index, event)
	local current = menu_stack[#menu_stack]
	if current == MENU_TYPES.HELPERS then
		return handle_helpers(index, event)
	elseif current == MENU_TYPES.ADD then
		return handle_add(index, event)
	elseif current == MENU_TYPES.EDIT then
		return handle_edit(index, event)
	elseif current == MENU_TYPES.INPUT then
		return handle_input(index, event)
	end
end

function lib:populate()
	local current = menu_stack[#menu_stack]
	if current == MENU_TYPES.HELPERS then
		return populate_helpers()
	elseif current == MENU_TYPES.ADD then
		return populate_add()
	elseif current == MENU_TYPES.EDIT then
		return populate_edit()
	elseif current == MENU_TYPES.INPUT then
		return populate_input()
	end
end

return lib