From 4a9974fda620633ed4c8bc7d9ee0d6801b00ca75 Mon Sep 17 00:00:00 2001 From: Richard Fine Date: Sun, 13 Sep 2020 16:15:28 -0400 Subject: [PATCH] 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. --- src/openrct2-ui/interface/Dropdown.h | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/openrct2-ui/interface/Dropdown.h b/src/openrct2-ui/interface/Dropdown.h index 8bc20503fb..83c7962aa9 100644 --- a/src/openrct2-ui/interface/Dropdown.h +++ b/src/openrct2-ui/interface/Dropdown.h @@ -11,6 +11,7 @@ #include #include +#include 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 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 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