From 45a1184b0ea7dfd92068e2d66311d936825b13d0 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sun, 3 May 2020 17:07:27 +0100 Subject: [PATCH] Implement selection --- distribution/openrct2.d.ts | 5 +++-- src/openrct2-ui/scripting/CustomWindow.cpp | 26 ++++++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index f21db9fb60..7f1c169bcb 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -1097,9 +1097,10 @@ declare global { items?: string[] | ListViewItem[]; selectedIndex?: number; highlightedIndex?: number; + canSelect?: boolean; - onHighlight: (index: number) => void; - onClick: (index: number) => void; + onHighlight: (item: number, column: number) => void; + onClick: (item: number, column: number) => void; getCell(row: number, column: number): string; setCell(row: number, column: number, value: string): void; diff --git a/src/openrct2-ui/scripting/CustomWindow.cpp b/src/openrct2-ui/scripting/CustomWindow.cpp index a1be644d16..17a2bf7368 100644 --- a/src/openrct2-ui/scripting/CustomWindow.cpp +++ b/src/openrct2-ui/scripting/CustomWindow.cpp @@ -233,6 +233,7 @@ namespace OpenRCT2::Ui::Windows bool HasBorder{}; bool ShowColumnHeaders{}; bool IsStriped{}; + bool CanSelect{}; // Event handlers DukValue OnClick; @@ -309,6 +310,7 @@ namespace OpenRCT2::Ui::Windows result.IsStriped = AsOrDefault(desc["isStriped"], false); result.OnClick = desc["onClick"]; result.OnHighlight = desc["onHighlight"]; + result.CanSelect = AsOrDefault(desc["canSelect"], false); } else if (result.Type == "spinner") { @@ -481,6 +483,7 @@ namespace OpenRCT2::Ui::Windows std::vector SortedItems; std::optional HighlightedCell; std::optional LastHighlightedCell; + std::optional SelectedCell; std::optional ColumnHeaderPressed; bool ColumnHeaderPressedCurrentState{}; bool ShowColumnHeaders{}; @@ -491,6 +494,7 @@ namespace OpenRCT2::Ui::Windows size_t CurrentSortColumn{}; bool LastIsMouseDown{}; bool IsMouseDown{}; + bool CanSelect{}; DukValue OnClick; DukValue OnHighlight; @@ -664,6 +668,11 @@ namespace OpenRCT2::Ui::Windows { if (hitResult->Row != HEADER_ROW && OnClick.context() != nullptr && OnClick.is_function()) { + if (CanSelect) + { + SelectedCell = hitResult; + } + auto ctx = OnClick.context(); duk_push_int(ctx, static_cast(hitResult->Row)); auto dukRow = DukValue::take_from_stack(ctx, -1); @@ -715,13 +724,23 @@ namespace OpenRCT2::Ui::Windows if (y + LIST_ROW_HEIGHT >= dpi->y) { + const auto& itemIndex = SortedItems[i]; + const auto& item = Items[itemIndex]; + // Background colour auto isStriped = IsStriped && (i & 1); - auto isHighlighted = (HighlightedCell && i == HighlightedCell->Row); + auto isHighlighted = (HighlightedCell && itemIndex == HighlightedCell->Row); + auto isSelected = (SelectedCell && itemIndex == SelectedCell->Row); if (isHighlighted) { gfx_filter_rect(dpi, dpi->x, y, dpi->x + dpi->width, y + (LIST_ROW_HEIGHT - 1), PALETTE_DARKEN_1); } + else if (isSelected) + { + // gfx_fill_rect(dpi, dpi->x, y, dpi->x + dpi->width, y + LIST_ROW_HEIGHT - 1, + // ColourMapA[w->colours[1]].dark); + gfx_filter_rect(dpi, dpi->x, y, dpi->x + dpi->width, y + (LIST_ROW_HEIGHT - 1), PALETTE_DARKEN_2); + } else if (isStriped) { gfx_fill_rect( @@ -730,8 +749,6 @@ namespace OpenRCT2::Ui::Windows } // Columns - const auto& itemIndex = SortedItems[i]; - const auto& item = Items[itemIndex]; if (Columns.size() == 0) { const auto& text = item.Cells[0]; @@ -853,7 +870,7 @@ namespace OpenRCT2::Ui::Windows if (row >= 0 && row < static_cast(Items.size())) { result = RowColumn(); - result->Row = row; + result->Row = static_cast(SortedItems[row]); } } @@ -1546,6 +1563,7 @@ namespace OpenRCT2::Ui::Windows listView.IsStriped = widgetDesc.IsStriped; listView.OnClick = widgetDesc.OnClick; listView.OnHighlight = widgetDesc.OnHighlight; + listView.CanSelect = widgetDesc.CanSelect; info.ListViews.push_back(std::move(listView)); } }