1 /*****************************************************************************
2  * Copyright (c) 2014-2020 OpenRCT2 developers
3  *
4  * For a complete list of all authors, please refer to contributors.md
5  * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
6  *
7  * OpenRCT2 is licensed under the GNU General Public License version 3.
8  *****************************************************************************/
9 
10 #pragma once
11 
12 #include <openrct2-ui/interface/Window.h>
13 #include <openrct2/common.h>
14 #include <openrct2/localisation/StringIds.h>
15 
16 namespace Dropdown
17 {
18     constexpr const rct_string_id SeparatorString = 0;
19     constexpr const rct_string_id FormatColourPicker = 0xFFFE;
20     constexpr const rct_string_id FormatLandPicker = 0xFFFF;
21     constexpr const int32_t ItemsMaxSize = 512;
22 
23     enum Flag
24     {
25         CustomHeight = (1 << 6),
26         StayOpen = (1 << 7)
27     };
28 
29     bool IsChecked(int32_t index);
30     bool IsDisabled(int32_t index);
31     void SetChecked(int32_t index, bool value);
32     void SetDisabled(int32_t index, bool value);
33 } // namespace Dropdown
34 
35 extern int32_t gDropdownNumItems;
36 extern rct_string_id gDropdownItemsFormat[Dropdown::ItemsMaxSize];
37 extern int64_t gDropdownItemsArgs[Dropdown::ItemsMaxSize];
38 extern bool gDropdownIsColour;
39 extern int32_t gDropdownLastColourHover;
40 extern int32_t gDropdownHighlightedIndex;
41 extern int32_t gDropdownDefaultIndex;
42 
43 void WindowDropdownShowText(const ScreenCoordsXY& screenPos, int32_t extray, uint8_t colour, uint8_t flags, size_t num_items);
44 void WindowDropdownShowTextCustomWidth(
45     const ScreenCoordsXY& screenPos, int32_t extray, uint8_t colour, uint8_t custom_height, uint8_t flags, size_t num_items,
46     int32_t width);
47 void WindowDropdownShowImage(
48     int32_t x, int32_t y, int32_t extray, uint8_t colour, uint8_t flags, int32_t numItems, int32_t itemWidth,
49     int32_t itemHeight, int32_t numColumns);
50 void WindowDropdownClose();
51 int32_t DropdownIndexFromPoint(const ScreenCoordsXY& loc, rct_window* w);
52 void WindowDropdownShowColour(rct_window* w, rct_widget* widget, uint8_t dropdownColour, uint8_t selectedColour);
53 void WindowDropdownShowColourAvailable(
54     rct_window* w, rct_widget* widget, uint8_t dropdownColour, uint8_t selectedColour, uint32_t availableColours);
55 uint32_t DropdownGetAppropriateImageDropdownItemsPerRow(uint32_t numItems);
56 
57 namespace Dropdown
58 {
59     struct Item
60     {
ItemItem61         constexpr Item(int32_t _expectedItemIndex, uint32_t _itemFormat, rct_string_id _stringId)
62             : expectedItemIndex(_expectedItemIndex)
63             , itemFormat(_itemFormat)
64             , stringId(_stringId)
65         {
66         }
67 
68         int32_t expectedItemIndex;
69         uint32_t itemFormat;
70         rct_string_id stringId;
71     };
72 
ToggleOption(int32_t _expectedItemIndex,rct_string_id _stringId)73     constexpr Item ToggleOption(int32_t _expectedItemIndex, rct_string_id _stringId)
74     {
75         return Item(_expectedItemIndex, STR_TOGGLE_OPTION, _stringId);
76     }
77 
Separator()78     constexpr Item Separator()
79     {
80         return Item(-1, Dropdown::SeparatorString, STR_EMPTY);
81     }
82 
SetItems(const Dropdown::Item (& items)[N])83     template<int N> void SetItems(const Dropdown::Item (&items)[N])
84     {
85         for (int i = 0; i < N; ++i)
86         {
87             const Item& item = items[i];
88             gDropdownItemsFormat[i] = item.itemFormat;
89             gDropdownItemsArgs[i] = item.stringId;
90         }
91     }
92 
ItemIDsMatchIndices(const Dropdown::Item (& items)[N])93     template<int N> constexpr bool ItemIDsMatchIndices(const Dropdown::Item (&items)[N])
94     {
95         for (int i = 0; i < N; ++i)
96         {
97             const Dropdown::Item& item = items[i];
98             if (item.expectedItemIndex >= 0 && item.expectedItemIndex != i)
99                 return false;
100         }
101 
102         return true;
103     }
104 
105 } // namespace Dropdown
106