forked from rosebudai/OpenRCT2-Web
Add support for custom cell draw functions
This commit is contained in:
1 parent
966057f35a
commit
d2b3b05f4d
2 files changed
+46
-9
No files matched your search
@@ -10,10 +10,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <openrct2-ui/UiStringIds.h>
|
||||
#include <openrct2/core/EnumUtils.hpp>
|
||||
#include <openrct2/core/StringTypes.h>
|
||||
#include <openrct2/interface/Window.h>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
|
||||
struct ImageId;
|
||||
@@ -32,6 +34,9 @@ namespace OpenRCT2::Dropdown
|
||||
constexpr int32_t kItemsMaxSize = 512;
|
||||
|
||||
struct DropdownState;
|
||||
|
||||
using CellDrawFunction = std::function<void(Drawing::RenderTarget&, const Item&, int32_t)>;
|
||||
|
||||
} // namespace OpenRCT2::Dropdown
|
||||
|
||||
namespace OpenRCT2::Ui::Windows
|
||||
@@ -54,6 +59,9 @@ namespace OpenRCT2::Ui::Windows
|
||||
void WindowDropdownShowImage(
|
||||
const ScreenCoordsXY& screenPos, int32_t extray, ColourWithFlags colour, uint8_t flags, int32_t numItems,
|
||||
int32_t itemWidth, int32_t itemHeight, int32_t numColumns);
|
||||
void WindowDropdownShowCustom(
|
||||
const ScreenCoordsXY& screenPos, int32_t extraY, ColourWithFlags colour, uint8_t flags,
|
||||
Dropdown::CellDrawFunction drawFunction, int32_t numItems, int32_t itemWidth, int32_t itemHeight, int32_t numColumns);
|
||||
|
||||
void WindowDropdownClose();
|
||||
|
||||
@@ -203,6 +211,8 @@ namespace OpenRCT2::Dropdown
|
||||
int32_t lastTooltipHover{};
|
||||
int32_t highlightedIndex{};
|
||||
int32_t defaultIndex{};
|
||||
|
||||
std::optional<CellDrawFunction> cellDrawFunction;
|
||||
};
|
||||
|
||||
template<int N>
|
||||
|
||||
@@ -90,15 +90,8 @@ namespace OpenRCT2::Ui::Windows
|
||||
|
||||
void drawItem(RenderTarget& rt, ScreenCoordsXY screenCoords, int32_t i)
|
||||
{
|
||||
int32_t highlightedIndex = gDropdown.highlightedIndex;
|
||||
bool highlighted = (i == highlightedIndex);
|
||||
|
||||
if (highlighted)
|
||||
{
|
||||
// Darken the cell's background slightly when highlighted
|
||||
const ScreenCoordsXY rightBottom = screenCoords + ScreenCoordsXY{ ItemWidth - 1, ItemHeight - 1 };
|
||||
Rectangle::filter(rt, { screenCoords, rightBottom }, FilterPaletteID::paletteDarken3);
|
||||
}
|
||||
const int32_t highlightedIndex = gDropdown.highlightedIndex;
|
||||
const bool highlighted = (i == highlightedIndex);
|
||||
|
||||
const auto& item = gDropdown.items[i];
|
||||
switch (item.type)
|
||||
@@ -185,6 +178,8 @@ namespace OpenRCT2::Ui::Windows
|
||||
{
|
||||
drawWidgets(rt);
|
||||
|
||||
int32_t highlightedIndex = gDropdown.highlightedIndex;
|
||||
|
||||
for (int32_t i = 0; i < gDropdown.numItems; i++)
|
||||
{
|
||||
ScreenCoordsXY cellCoords;
|
||||
@@ -196,10 +191,26 @@ namespace OpenRCT2::Ui::Windows
|
||||
ScreenCoordsXY screenCoords = windowPos
|
||||
+ ScreenCoordsXY{ 2 + (cellCoords.x * ItemWidth), 2 + (cellCoords.y * ItemHeight) };
|
||||
|
||||
bool highlighted = (i == highlightedIndex);
|
||||
if (highlighted)
|
||||
{
|
||||
// Darken the cell's background slightly when highlighted
|
||||
const ScreenCoordsXY rightBottom = screenCoords + ScreenCoordsXY{ ItemWidth - 1, ItemHeight - 1 };
|
||||
Rectangle::filter(rt, { screenCoords, rightBottom }, FilterPaletteID::paletteDarken3);
|
||||
}
|
||||
|
||||
if (gDropdown.items[i].isSeparator())
|
||||
{
|
||||
drawSeparator(rt, screenCoords);
|
||||
}
|
||||
else if (gDropdown.cellDrawFunction.has_value())
|
||||
{
|
||||
RenderTarget clippedRT;
|
||||
if (ClipRenderTarget(clippedRT, rt, screenCoords, ItemWidth, ItemHeight))
|
||||
{
|
||||
gDropdown.cellDrawFunction.value()(clippedRT, gDropdown.items[i], highlighted);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
drawItem(rt, screenCoords, i);
|
||||
@@ -376,6 +387,8 @@ namespace OpenRCT2::Ui::Windows
|
||||
|
||||
WindowDropdownShowTextCustomWidth(
|
||||
screenPos, extray, colour, 0, flags, num_items, maxStringWidth + 3, prefRowsPerColumn);
|
||||
|
||||
gDropdown.cellDrawFunction = std::nullopt;
|
||||
}
|
||||
|
||||
void WindowDropdownShowText(
|
||||
@@ -417,6 +430,8 @@ namespace OpenRCT2::Ui::Windows
|
||||
auto numRowsPerColumn = prefRowsPerColumn > 0 ? static_cast<int32_t>(prefRowsPerColumn) : Dropdown::kItemsMaxSize;
|
||||
w->setTextItems(screenPos, extray, colour, customItemHeight, flags, num_items, width, numRowsPerColumn);
|
||||
}
|
||||
|
||||
gDropdown.cellDrawFunction = std::nullopt;
|
||||
}
|
||||
|
||||
void WindowDropdownShowTextCustomWidth(
|
||||
@@ -460,6 +475,18 @@ namespace OpenRCT2::Ui::Windows
|
||||
{
|
||||
w->setImageItems(screenPos, extray, colour, numItems, itemWidth, itemHeight, numColumns);
|
||||
}
|
||||
|
||||
gDropdown.cellDrawFunction = std::nullopt;
|
||||
}
|
||||
|
||||
void WindowDropdownShowCustom(
|
||||
const ScreenCoordsXY& screenPos, int32_t extraY, ColourWithFlags colour, uint8_t flags,
|
||||
Dropdown::CellDrawFunction drawFunction, int32_t numItems, int32_t itemWidth, int32_t itemHeight, int32_t numColumns)
|
||||
{
|
||||
// Fall back to image internals
|
||||
WindowDropdownShowImage(screenPos, extraY, colour, flags, numItems, itemWidth, itemHeight, numColumns);
|
||||
|
||||
gDropdown.cellDrawFunction = drawFunction;
|
||||
}
|
||||
|
||||
void WindowDropdownClose()
|
||||
|
||||
Reference in new issue
Block a user