summaryrefslogtreecommitdiffstats
path: root/src/frontend/mame/ui/slotopt.cpp
blob: 014931737310e671b41523072e3fad427a0913c2 (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
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
// license:BSD-3-Clause
// copyright-holders:Nicola Salmoria, Aaron Giles, Nathan Woods
/*********************************************************************

    ui/slotopt.cpp

    Internal menu for the slot options.

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

#include "emu.h"

#include "ui/ui.h"
#include "ui/slotopt.h"
#include "ui/devopt.h"

#include "emuopts.h"
#include "mameopts.h"

#include <algorithm>


/***************************************************************************
    UTILITY
***************************************************************************/

namespace {

// constants
void *const ITEMREF_RESET = ((void *)1);
constexpr char const DIVIDER[] = "------";

} // anonymous namespace


/***************************************************************************
    SLOT MENU
***************************************************************************/

namespace ui {

//-------------------------------------------------
//  menu_slot_devices constructor
//-------------------------------------------------

menu_slot_devices::menu_slot_devices(mame_ui_manager &mui, render_container &container) : menu(mui, container)
{
}

//-------------------------------------------------
//  menu_slot_devices destructor
//-------------------------------------------------

menu_slot_devices::~menu_slot_devices()
{
}


//-------------------------------------------------
//  get_current_option - returns the current
//  slot option
//-------------------------------------------------

device_slot_interface::slot_option const *menu_slot_devices::get_current_option(device_slot_interface &slot) const
{
	std::string current;

	if (!slot.fixed())
	{
		char const *const slot_option_name = slot.slot_name();
		current = machine().options().slot_option(slot_option_name).value();
	}
	else
	{
		if (!slot.default_option())
			return nullptr;
		current.assign(slot.default_option());
	}

	return slot.option(current.c_str());
}


//-------------------------------------------------
//  set_slot_device
//-------------------------------------------------

void menu_slot_devices::set_slot_device(device_slot_interface &slot, const char *val)
{
	// we might change slot options; in the spirit of user friendliness, we should record all current
	// options
	record_current_options();

	// find the slot option
	slot_option &opt(machine().options().slot_option(slot.slot_name()));

	// specify it
	opt.specify(val);

	// erase this from our recorded options list - this is the slot we're trying to change!
	m_slot_options.erase(slot.slot_name());

	// refresh any options that we might have annotated earlier
	while (try_refresh_current_options())
		;

	// changing the options may result in options changing; we need to reset
	reset(reset_options::REMEMBER_POSITION);
}


//-------------------------------------------------
//  record_current_options
//-------------------------------------------------

void menu_slot_devices::record_current_options()
{
	for (device_slot_interface &slot : slot_interface_iterator(m_config->root_device()))
	{
		// we're doing this out of a desire to honor user-selectable options; therefore it only
		// makes sense to record values for selectable options
		if (slot.has_selectable_options())
		{
			// get the slot option
			const slot_option &opt(machine().options().slot_option(slot.slot_name()));

			// and record the value in our local cache
			m_slot_options[slot.slot_name()] = opt.specified_value();
		}
	}
}


//-------------------------------------------------
//  try_refresh_current_options
//-------------------------------------------------

bool menu_slot_devices::try_refresh_current_options()
{
	// enumerate through all slot options that we've tracked
	for (const auto &opt : m_slot_options)
	{
		// do we have a value different than what we're tracking?
		slot_option *slotopt = machine().options().find_slot_option(opt.first);
		if (slotopt && slotopt->specified_value() != opt.second)
		{
			// specify this option (but catch errors)
			try
			{
				slotopt->specify(opt.second);

				// the option was successfully specified; it isn't safe to continue
				// checking slots as the act of specifying the slot may have drastically
				// changed the options list
				return true;
			}
			catch (options_exception &)
			{
				// this threw an exception - that is fine; we can just proceed
			}
		}
	}

	// we've went through all options without changing anything
	return false;
}


//-------------------------------------------------
//  populate
//-------------------------------------------------

void menu_slot_devices::populate(float &customtop, float &custombottom)
{
	// we need to keep our own copy of the machine_config because we
	// can change this out from under the caller
	m_config = std::make_unique<machine_config>(machine().system(), machine().options());

	// cycle through all devices for this system
	for (device_slot_interface &slot : slot_interface_iterator(m_config->root_device()))
	{
		// does this slot have any selectable options?
		bool has_selectable_options = slot.has_selectable_options();

		// name this option
		std::string opt_name(DIVIDER);
		device_slot_interface::slot_option const *option = get_current_option(slot);
		if (option)
		{
			opt_name = has_selectable_options
					? option->name()
					: string_format(_("%s [internal]"), option->name());
		}

		// choose item flags
		uint32_t const item_flags = has_selectable_options
				? FLAG_LEFT_ARROW | FLAG_RIGHT_ARROW
				: FLAG_DISABLE;

		item_append(slot.slot_name(), opt_name, item_flags, (void *)&slot);
	}
	item_append(menu_item_type::SEPARATOR);
	item_append(_("Reset"), "", 0, ITEMREF_RESET);

	// leave space for the name of the current option at the bottom
	custombottom = ui().get_line_height() + 3.0f * ui().box_tb_border();
}


//-------------------------------------------------
//  custom_render - draw extra menu content
//-------------------------------------------------

void menu_slot_devices::custom_render(void *selectedref, float top, float bottom, float origx1, float origy1, float origx2, float origy2)
{
	if (selectedref && (ITEMREF_RESET != selectedref))
	{
		device_slot_interface *const slot(reinterpret_cast<device_slot_interface *>(selectedref));
		device_slot_interface::slot_option const *const option(get_current_option(*slot));
		char const *const text[] = { option ? option->devtype().fullname() : _("[empty slot]") };
		draw_text_box(
				std::begin(text), std::end(text),
				origx1, origx2, origy2 + ui().box_tb_border(), origy2 + bottom,
				ui::text_layout::CENTER, ui::text_layout::TRUNCATE, false,
				ui().colors().text_color(), ui().colors().background_color(), 1.0f);
	}
}


//-------------------------------------------------
//  handle - process an input event
//-------------------------------------------------

void menu_slot_devices::handle()
{
	// process the menu
	event const *const menu_event(process(0));

	if (menu_event && menu_event->itemref != nullptr)
	{
		if (menu_event->itemref == ITEMREF_RESET)
		{
			if (menu_event->iptkey == IPT_UI_SELECT)
				machine().schedule_hard_reset();
		}
		else if (menu_event->iptkey == IPT_UI_LEFT || menu_event->iptkey == IPT_UI_RIGHT)
		{
			device_slot_interface *slot = (device_slot_interface *)menu_event->itemref;
			rotate_slot_device(*slot, menu_event->iptkey == IPT_UI_LEFT ? step_t::PREVIOUS : step_t::NEXT);
		}
		else if (menu_event->iptkey == IPT_UI_SELECT)
		{
			device_slot_interface *slot = (device_slot_interface *)menu_event->itemref;
			device_slot_interface::slot_option const *const option = get_current_option(*slot);
			if (option)
				menu::stack_push<menu_device_config>(ui(), container(), slot, option);
		}
	}
}


//-------------------------------------------------
//  rotate_slot_device - rotates the specified slot
//-------------------------------------------------

void menu_slot_devices::rotate_slot_device(device_slot_interface &slot, menu_slot_devices::step_t step)
{
	// first, we need to make sure our cache of options is up to date
	if (m_current_option_list_slot_tag != slot.device().tag())
	{
		device_slot_interface::slot_option const *current = get_current_option(slot);

		// build the option list, including the blank option
		m_current_option_list.clear();
		m_current_option_list.emplace_back("");
		for (const auto &ent : slot.option_list())
		{
			if (ent.second->selectable())
				m_current_option_list.emplace_back(ent.second->name());
		}

		// since the order is indeterminate, we need to sort the options
		std::sort(m_current_option_list.begin(), m_current_option_list.end());

		// find the current position
		char const *const target = current ? current->name() : "";
		m_current_option_list_iter = std::find_if(
				m_current_option_list.begin(),
				m_current_option_list.end(),
				[target] (const std::string &opt_value)
				{
					return opt_value == target;
				});

		// we expect the above search to succeed, because if an internal
		// option was selected, the menu item should be disabled
		assert(m_current_option_list_iter != m_current_option_list.end());

		// we've succeeded; don't do this again
		m_current_option_list_slot_tag.assign(slot.device().tag());
	}

	// At this point, the current option list and accompanying iterator should
	// be good; perform the rotation
	switch (step)
	{
	case step_t::NEXT:
		m_current_option_list_iter++;
		if (m_current_option_list_iter == m_current_option_list.end())
			m_current_option_list_iter = m_current_option_list.begin();
		break;

	case step_t::PREVIOUS:
		if (m_current_option_list_iter == m_current_option_list.begin())
			m_current_option_list_iter = m_current_option_list.end();
		m_current_option_list_iter--;
		break;

	default:
		throw false;
	}

	set_slot_device(slot, m_current_option_list_iter->c_str());
}

} // namespace ui