Skip to content

Commit

Permalink
Automatically use multiple dropdown columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Gymnasiast committed May 18, 2024
1 parent 270c632 commit 99b99ef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/openrct2-ui/interface/Dropdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ namespace OpenRCT2::Ui::Windows
void WindowDropdownShowColourAvailable(
WindowBase* w, Widget* widget, uint8_t dropdownColour, uint8_t selectedColour, uint32_t availableColours);
uint32_t DropdownGetAppropriateImageDropdownItemsPerRow(uint32_t numItems);
bool WindowDropDownHasMultipleColumns(size_t numItems);

colour_t ColourDropDownIndexToColour(uint8_t ddidx);
} // namespace OpenRCT2::Ui::Windows
Expand Down
38 changes: 21 additions & 17 deletions src/openrct2-ui/windows/Dropdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/

#include <algorithm>
#include <bitset>
#include <iterator>
#include <openrct2-ui/interface/Dropdown.h>
Expand All @@ -24,9 +25,6 @@

namespace OpenRCT2::Ui::Windows
{
// The maximum number of rows to list before items overflow into new columns
constexpr int32_t DROPDOWN_TEXT_MAX_ROWS = 32;

constexpr int32_t DROPDOWN_ITEM_HEIGHT = 12;
constexpr int32_t DROPDOWN_ITEM_HEIGHT_TOUCH = 24;

Expand Down Expand Up @@ -90,12 +88,12 @@ namespace OpenRCT2::Ui::Windows

static int32_t GetDefaultRowHeight()
{
return gConfigInterface.EnlargedUi ? DROPDOWN_ITEM_HEIGHT_TOUCH : DROPDOWN_ITEM_HEIGHT;
return Config::Get().interface.EnlargedUi ? DROPDOWN_ITEM_HEIGHT_TOUCH : DROPDOWN_ITEM_HEIGHT;
}

static int32_t GetAdditionalRowPadding()
{
return gConfigInterface.EnlargedUi ? 6 : 0;
return Config::Get().interface.EnlargedUi ? 6 : 0;
}

void OnDraw(DrawPixelInfo& dpi) override
Expand Down Expand Up @@ -174,26 +172,37 @@ namespace OpenRCT2::Ui::Windows
}
}

static int32_t getSpaceUntilBottom(const ScreenCoordsXY& screenPos, int32_t dropdownButtonHeight)
{
auto* mainWindow = WindowGetMain();
if (mainWindow == nullptr)
return 400;

return std::max(1, mainWindow->height - (screenPos.y + dropdownButtonHeight + 5));
}

void SetTextItems(
const ScreenCoordsXY& screenPos, int32_t extraY, uint8_t colour, uint8_t customHeight, uint8_t txtFlags,
size_t numItems, int32_t itemWidth)
{
// Set and calculate num items, rows and columns
ItemWidth = itemWidth;
ItemHeight = (txtFlags & Dropdown::Flag::CustomHeight) ? customHeight : GetDefaultRowHeight();
gDropdownNumItems = static_cast<int32_t>(numItems);
// There must always be at least one column to prevent dividing by zero
if (gDropdownNumItems == 0)
if (gDropdownNumItems <= 1)
{
NumColumns = 1;
NumRows = 1;
NumColumns = 1;
}
else
{
NumColumns = (gDropdownNumItems + DROPDOWN_TEXT_MAX_ROWS - 1) / DROPDOWN_TEXT_MAX_ROWS;
NumRows = (gDropdownNumItems + NumColumns - 1) / NumColumns;
const int32_t numAvailableRows = std::max(1, getSpaceUntilBottom(screenPos, extraY) / ItemHeight);
NumRows = std::min(numAvailableRows, gDropdownNumItems);
NumColumns = (gDropdownNumItems + NumRows - 1) / NumRows;
}

ItemWidth = itemWidth;

// Text dropdowns are listed horizontally
ListVertically = true;

Expand Down Expand Up @@ -343,7 +352,7 @@ namespace OpenRCT2::Ui::Windows
int32_t width)
{
InputSetFlag(static_cast<INPUT_FLAGS>(INPUT_FLAG_DROPDOWN_STAY_OPEN | INPUT_FLAG_DROPDOWN_MOUSE_UP), false);
if (flags & Dropdown::Flag::StayOpen || gConfigInterface.EnlargedUi)
if (flags & Dropdown::Flag::StayOpen || Config::Get().interface.EnlargedUi)
InputSetFlag(INPUT_FLAG_DROPDOWN_STAY_OPEN, true);

WindowDropdownClose();
Expand Down Expand Up @@ -375,7 +384,7 @@ namespace OpenRCT2::Ui::Windows
int32_t itemHeight, int32_t numColumns)
{
InputSetFlag(static_cast<INPUT_FLAGS>(INPUT_FLAG_DROPDOWN_STAY_OPEN | INPUT_FLAG_DROPDOWN_MOUSE_UP), false);
if (flags & Dropdown::Flag::StayOpen || gConfigInterface.EnlargedUi)
if (flags & Dropdown::Flag::StayOpen || Config::Get().interface.EnlargedUi)
InputSetFlag(INPUT_FLAG_DROPDOWN_STAY_OPEN, true);

// Close existing dropdown
Expand Down Expand Up @@ -523,11 +532,6 @@ static constexpr colour_t kColoursDropdownOrder[] = {
return _appropriateImageDropdownItemsPerRow[std::min<uint32_t>(
numItems, static_cast<uint32_t>(std::size(_appropriateImageDropdownItemsPerRow) - 1))];
}

bool WindowDropDownHasMultipleColumns(size_t numItems)
{
return numItems > DROPDOWN_TEXT_MAX_ROWS;
}
} // namespace OpenRCT2::Ui::Windows

using namespace OpenRCT2::Ui::Windows;
Expand Down
3 changes: 1 addition & 2 deletions src/openrct2-ui/windows/Ride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1981,10 +1981,9 @@ static_assert(std::size(RatingNames) == 6);
}

Widget* dropdownWidget = widget - 1;
auto ddWidth = WindowDropDownHasMultipleColumns(numItems) ? dropdownWidget->width() - 24 : dropdownWidget->width();
WindowDropdownShowTextCustomWidth(
{ windowPos.x + dropdownWidget->left, windowPos.y + dropdownWidget->top }, dropdownWidget->height() + 1,
colours[1], 0, Dropdown::Flag::StayOpen, numItems, ddWidth);
colours[1], 0, Dropdown::Flag::StayOpen, numItems, dropdownWidget->width());

// Find the current vehicle type in the ordered list.
int32_t pos = 0;
Expand Down

0 comments on commit 99b99ef

Please sign in to comment.