1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-18 04:23:20 +01:00

Introduce mechanism for configuring dropdown menu with static data

For menus with static content, provide a mechanism for building the menu items as a constexpr array, allowing developers to specify menus more concisely and to put the data into the .rodata section where it can be quickly copied through.
This commit is contained in:
Richard Fine
2020-09-13 16:15:28 -04:00
parent 0fb307c12e
commit 4a9974fda6

View File

@@ -11,6 +11,7 @@
#include <openrct2-ui/interface/Window.h>
#include <openrct2/common.h>
#include <openrct2/localisation/StringIds.h>
constexpr const rct_string_id DROPDOWN_SEPARATOR = 0;
constexpr const rct_string_id DROPDOWN_FORMAT_COLOUR_PICKER = 0xFFFE;
@@ -50,3 +51,53 @@ void window_dropdown_show_colour(rct_window* w, rct_widget* widget, uint8_t drop
void window_dropdown_show_colour_available(
rct_window* w, rct_widget* widget, uint8_t dropdownColour, uint8_t selectedColour, uint32_t availableColours);
uint32_t dropdown_get_appropriate_image_dropdown_items_per_row(uint32_t numItems);
namespace Dropdown
{
struct Item
{
constexpr Item(int32_t _expectedItemIndex, uint32_t _itemFormat, rct_string_id _stringId)
: expectedItemIndex(_expectedItemIndex)
, itemFormat(_itemFormat)
, stringId(_stringId)
{
}
int32_t expectedItemIndex;
uint32_t itemFormat;
rct_string_id stringId;
};
constexpr Item ToggleOption(int32_t _expectedItemIndex, rct_string_id _stringId)
{
return Item(_expectedItemIndex, STR_TOGGLE_OPTION, _stringId);
}
constexpr Item Separator()
{
return Item(-1, DROPDOWN_SEPARATOR, STR_EMPTY);
}
template<int N> void SetItems(const Dropdown::Item (&items)[N])
{
for (int i = 0; i < N; ++i)
{
const Item& item = items[i];
gDropdownItemsFormat[i] = item.itemFormat;
gDropdownItemsArgs[i] = item.stringId;
}
}
template<int N> constexpr bool ItemIDsMatchIndices(const Dropdown::Item (&items)[N])
{
for (int i = 0; i < N; ++i)
{
const Dropdown::Item& item = items[i];
if (item.expectedItemIndex >= 0 && item.expectedItemIndex != i)
return false;
}
return true;
}
} // namespace Dropdown