summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame/ui/pluginopt.cpp
blob: 809047357fcb3466bb559b13ec07451af3f6b25e (plain) (blame)
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
// license:BSD-3-Clause
// copyright-holders:Nicola Salmoria, Aaron Giles, Nathan Woods
/*********************************************************************

    ui/pluginopt.cpp

    Internal menu for the plugin interface.

*********************************************************************/

#include "emu.h"
#include "pluginopt.h"

#include "ui/utils.h"

#include "luaengine.h"
#include "mame.h"


namespace ui {

bool menu_plugin::handle(event const *ev)
{
	if (ev && ev->itemref)
	{
		if (ev->iptkey == IPT_UI_SELECT)
			menu::stack_push<menu_plugin_opt>(ui(), container(), (char *)ev->itemref, false);
	}
	return false;
}

menu_plugin::menu_plugin(mame_ui_manager &mui, render_container &container) :
	menu(mui, container),
	m_plugins(mame_machine_manager::instance()->lua()->get_menu())
{
	set_heading(_("Plugin Options"));
}

void menu_plugin::populate()
{
	for (auto &curplugin : m_plugins)
		item_append(curplugin, 0, (void *)curplugin.c_str());
	item_append(menu_item_type::SEPARATOR);
}

void menu_plugin::show_menu(mame_ui_manager &mui, render_container &container, std::string_view menu)
{
	// add the plugin menu entry
	menu::stack_push<menu_plugin_opt>(mui, container, menu, true);

	// force the menus on
	mui.show_menu();
}

menu_plugin::~menu_plugin()
{
}

menu_plugin_opt::menu_plugin_opt(mame_ui_manager &mui, render_container &container, std::string_view menu, bool one_shot) :
	ui::menu(mui, container),
	m_menu(menu),
	m_need_idle(false)
{
	set_one_shot(one_shot);
}

bool menu_plugin_opt::handle(event const *ev)
{
	void *const itemref = ev ? ev->itemref : get_selection_ref();
	std::string key;
	if (ev)
	{
		switch (ev->iptkey)
		{
		case IPT_UI_UP:
			key = "up";
			break;
		case IPT_UI_DOWN:
			key = "down";
			break;
		case IPT_UI_LEFT:
			key = "left";
			break;
		case IPT_UI_RIGHT:
			key = "right";
			break;
		case IPT_UI_PREV_GROUP:
			key = "prevgroup";
			break;
		case IPT_UI_NEXT_GROUP:
			key = "nextgroup";
			break;
		case IPT_UI_SELECT:
			key = "select";
			break;
		case IPT_UI_DISPLAY_COMMENT:
			key = "comment";
			break;
		case IPT_UI_CLEAR:
			key = "clear";
			break;
		case IPT_UI_BACK:
			key = "back";
			break;
		case IPT_UI_CANCEL:
			key = "cancel";
			break;
		case IPT_SPECIAL:
			key = std::to_string((u32)ev->unichar);
			break;
		default:
			break;
		}
	}

	if (key.empty() && !m_need_idle)
		return false;

	auto const result = mame_machine_manager::instance()->lua()->menu_callback(m_menu, uintptr_t(itemref), key);
	if (result.second)
		set_selection(reinterpret_cast<void *>(uintptr_t(*result.second)));
	if (result.first)
		reset(reset_options::REMEMBER_REF);
	else if (ev && (ev->iptkey == IPT_UI_BACK))
		stack_pop();

	return result.second && !result.first;
}

void menu_plugin_opt::populate()
{
	std::vector<std::tuple<std::string, std::string, std::string>> menu_list;
	std::string flags;
	auto const sel = mame_machine_manager::instance()->lua()->menu_populate(m_menu, menu_list, flags);

	uintptr_t i = 1;
	for (auto &item : menu_list)
	{
		std::string &text = std::get<0>(item);
		std::string &subtext = std::get<1>(item);
		std::string_view tflags = std::get<2>(item);

		uint32_t item_flags_or = uint32_t(0);
		uint32_t item_flags_and = ~uint32_t(0);
		auto flag_start = tflags.find_first_not_of(' ');
		while (std::string_view::npos != flag_start)
		{
			tflags.remove_prefix(flag_start);
			auto const flag_end = tflags.find(' ');
			auto const flag = tflags.substr(0, flag_end);
			tflags.remove_prefix(flag.length());
			flag_start = tflags.find_first_not_of(' ');

			if (flag == "off")
				item_flags_or |= FLAG_DISABLE;
			else if (flag == "on")
				item_flags_and &= ~FLAG_DISABLE;
			else if (flag == "l")
				item_flags_or |= FLAG_LEFT_ARROW;
			else if (flag == "r")
				item_flags_or |= FLAG_RIGHT_ARROW;
			else if (flag == "lr")
				item_flags_or |= FLAG_RIGHT_ARROW | FLAG_LEFT_ARROW;
			else if (flag == "invert")
				item_flags_or |= FLAG_INVERT;
			else if (flag == "heading")
				item_flags_or |= FLAG_DISABLE | FLAG_UI_HEADING;
			else
				osd_printf_info("menu_plugin_opt: unknown flag '%s' for item %d (%s)\n", flag, i, text);
		}

		if (text == "---")
			item_append(menu_item_type::SEPARATOR);
		else
			item_append(std::move(text), std::move(subtext), item_flags_or & item_flags_and, reinterpret_cast<void *>(i));
		++i;
	}
	item_append(menu_item_type::SEPARATOR);

	if (sel)
		set_selection(reinterpret_cast<void *>(uintptr_t(*sel)));

	uint32_t process_flags = 0U;
	m_need_idle = false;
	if (!flags.empty())
	{
		std::string_view mflags = flags;
		auto flag_start = mflags.find_first_not_of(' ');
		while (std::string_view::npos != flag_start)
		{
			mflags.remove_prefix(flag_start);
			auto const flag_end = mflags.find(' ');
			auto const flag = mflags.substr(0, flag_end);
			mflags.remove_prefix(flag.length());
			flag_start = mflags.find_first_not_of(' ');

			if (flag == "nokeys")
				process_flags |= PROCESS_NOKEYS;
			else if (flag == "lralways")
				process_flags |= PROCESS_LR_ALWAYS;
			else if (flag == "lrrepeat")
				process_flags |= PROCESS_LR_REPEAT;
			else if (flag == "customnav")
				process_flags |= PROCESS_CUSTOM_NAV;
			else if (flag == "ignorepause")
				process_flags |= PROCESS_IGNOREPAUSE;
			else if (flag == "idle")
				m_need_idle = true;
			else
				osd_printf_info("menu_plugin_opt: unknown processing flag '%s'\n", flag);
		}
		if (process_flags & PROCESS_NOKEYS)
			m_need_idle = true;
	}
	set_process_flags(process_flags);
}

menu_plugin_opt::~menu_plugin_opt()
{
}

} // namespace ui