diff --git a/src/openrct2-ui/windows/TileInspector.cpp b/src/openrct2-ui/windows/TileInspector.cpp index 625ffcfd88..e66b2c2b0a 100644 --- a/src/openrct2-ui/windows/TileInspector.cpp +++ b/src/openrct2-ui/windows/TileInspector.cpp @@ -558,10 +558,7 @@ public: if (tileInspectorPage == TileInspectorPage::Default || windowTileInspectorSelectedIndex == -1) return; - const TileElement* const tileElement = GetSelectedElement(); - - // Update selection, can be nullptr. - OpenRCT2::TileInspector::SetSelectedElement(tileElement); + const TileElement* const tileElement = OpenRCT2::TileInspector::GetSelectedElement(); if (tileElement == nullptr) return; @@ -713,12 +710,12 @@ public: void OnClose() override { ToolCancel(); - TileElement* const elem = GetSelectedElement(); + TileElement* const elem = OpenRCT2::TileInspector::GetSelectedElement(); if (elem != nullptr) { MapInvalidateElement(_toolMap, elem); } - OpenRCT2::TileInspector::SetSelectedElement(nullptr); + windowTileInspectorSelectedIndex = -1; } void OnResize() override @@ -769,7 +766,7 @@ public: if (tileInspectorPage == TileInspectorPage::Default || windowTileInspectorSelectedIndex == -1) return; - const TileElement* tileElement = GetSelectedElement(); + const TileElement* tileElement = OpenRCT2::TileInspector::GetSelectedElement(); if (tileElement == nullptr) return; @@ -928,7 +925,7 @@ public: if (dropdownIndex == -1) return; // Get selected element - const TileElement* const tileElement = GetSelectedElement(); + const TileElement* const tileElement = OpenRCT2::TileInspector::GetSelectedElement(); if (tileInspectorPage == TileInspectorPage::Wall) { Guard::Assert(tileElement->GetType() == TileElementType::Wall, "Element is not a wall"); @@ -1049,7 +1046,7 @@ public: + ScreenCoordsXY{ widgets[WIDX_GROUPBOX_DETAILS].left + 7, widgets[WIDX_GROUPBOX_DETAILS].top + 14 }; // Get map element - const TileElement* const tileElement = GetSelectedElement(); + const TileElement* const tileElement = OpenRCT2::TileInspector::GetSelectedElement(); if (tileElement == nullptr) return; @@ -1764,7 +1761,6 @@ private: _tileSelected = true; _toolMap = mapCoords; windowTileInspectorTile = TileCoordsXY(mapCoords); - OpenRCT2::TileInspector::SetSelectedElement(clickedElement); LoadTile(clickedElement); } @@ -1773,13 +1769,10 @@ private: if (index < 0 || index >= windowTileInspectorElementCount) { windowTileInspectorSelectedIndex = -1; - OpenRCT2::TileInspector::SetSelectedElement(nullptr); } else { windowTileInspectorSelectedIndex = index; - const TileElement* const tileElement = GetSelectedElement(); - OpenRCT2::TileInspector::SetSelectedElement(tileElement); } Invalidate(); } @@ -1839,7 +1832,7 @@ private: void CopyElement() { - const TileElement* const tileElement = GetSelectedElement(); + const TileElement* const tileElement = OpenRCT2::TileInspector::GetSelectedElement(); Guard::Assert(tileElement != nullptr, "Invalid tile element"); // Copy value, in case the element gets moved _copiedElement = *tileElement; @@ -1992,21 +1985,9 @@ private: GameActions::Execute(&modifyTile); } - TileElement* GetSelectedElement() - { - if (windowTileInspectorSelectedIndex == -1) - { - return nullptr; - } - Guard::Assert( - windowTileInspectorSelectedIndex >= 0 && windowTileInspectorSelectedIndex < windowTileInspectorElementCount, - "Selected list item out of range"); - return MapGetNthElementAt(_toolMap, windowTileInspectorSelectedIndex); - } - void OnPrepareDraw() override { - const TileElement* const tileElement = GetSelectedElement(); + const TileElement* const tileElement = OpenRCT2::TileInspector::GetSelectedElement(); // Set the correct page automatically TileInspectorPage p = TileInspectorPage::Default; diff --git a/src/openrct2/paint/Paint.h b/src/openrct2/paint/Paint.h index 017df904c0..0139e6b6dc 100644 --- a/src/openrct2/paint/Paint.h +++ b/src/openrct2/paint/Paint.h @@ -190,6 +190,7 @@ struct PaintSessionCore TileElement* CurrentlyDrawnTileElement; const TileElement* PathElementOnSameHeight; const TileElement* TrackElementOnSameHeight; + const TileElement* SelectedElement; PaintStruct* WoodenSupportsPrependTo; CoordsXY SpritePosition; CoordsXY MapPosition; diff --git a/src/openrct2/paint/Painter.cpp b/src/openrct2/paint/Painter.cpp index 989aa1591a..419ffe1de2 100644 --- a/src/openrct2/paint/Painter.cpp +++ b/src/openrct2/paint/Painter.cpp @@ -25,6 +25,7 @@ #include "../profiling/Profiling.h" #include "../title/TitleScreen.h" #include "../ui/UiContext.h" +#include "../world/TileInspector.h" using namespace OpenRCT2; using namespace OpenRCT2::Drawing; @@ -169,6 +170,7 @@ PaintSession* Painter::CreateSession(DrawPixelInfo& dpi, uint32_t viewFlags) session->CurrentlyDrawnEntity = nullptr; session->CurrentlyDrawnTileElement = nullptr; session->Surface = nullptr; + session->SelectedElement = OpenRCT2::TileInspector::GetSelectedElement(); return session; } diff --git a/src/openrct2/paint/tile_element/Paint.Banner.cpp b/src/openrct2/paint/tile_element/Paint.Banner.cpp index 269533171b..92068d04fa 100644 --- a/src/openrct2/paint/tile_element/Paint.Banner.cpp +++ b/src/openrct2/paint/tile_element/Paint.Banner.cpp @@ -103,7 +103,7 @@ void PaintBanner(PaintSession& session, uint8_t direction, int32_t height, const session.InteractionType = ViewportInteractionItem::None; imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); } - else if (OpenRCT2::TileInspector::IsElementSelected(reinterpret_cast(&bannerElement))) + else if (session.SelectedElement == reinterpret_cast(&bannerElement)) { imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); } diff --git a/src/openrct2/paint/tile_element/Paint.Entrance.cpp b/src/openrct2/paint/tile_element/Paint.Entrance.cpp index 53a960c931..e5b8d4706b 100644 --- a/src/openrct2/paint/tile_element/Paint.Entrance.cpp +++ b/src/openrct2/paint/tile_element/Paint.Entrance.cpp @@ -149,7 +149,7 @@ static void PaintRideEntranceExit(PaintSession& session, uint8_t direction, int3 session.InteractionType = ViewportInteractionItem::None; imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); } - else if (OpenRCT2::TileInspector::IsElementSelected(reinterpret_cast(&entranceEl))) + else if (session.SelectedElement == reinterpret_cast(&entranceEl)) { imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); } @@ -277,7 +277,7 @@ static void PaintParkEntrance(PaintSession& session, uint8_t direction, int32_t session.InteractionType = ViewportInteractionItem::None; imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); } - else if (OpenRCT2::TileInspector::IsElementSelected(reinterpret_cast(&entranceEl))) + else if (session.SelectedElement == reinterpret_cast(&entranceEl)) { imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); } diff --git a/src/openrct2/paint/tile_element/Paint.LargeScenery.cpp b/src/openrct2/paint/tile_element/Paint.LargeScenery.cpp index c75369601a..2f50f0cc51 100644 --- a/src/openrct2/paint/tile_element/Paint.LargeScenery.cpp +++ b/src/openrct2/paint/tile_element/Paint.LargeScenery.cpp @@ -362,7 +362,7 @@ void PaintLargeScenery(PaintSession& session, uint8_t direction, uint16_t height imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); isGhost = true; } - else if (OpenRCT2::TileInspector::IsElementSelected(reinterpret_cast(&tileElement))) + else if (session.SelectedElement == reinterpret_cast(&tileElement)) { imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); isGhost = true; diff --git a/src/openrct2/paint/tile_element/Paint.Path.cpp b/src/openrct2/paint/tile_element/Paint.Path.cpp index bc35450bc5..0e667c06b1 100644 --- a/src/openrct2/paint/tile_element/Paint.Path.cpp +++ b/src/openrct2/paint/tile_element/Paint.Path.cpp @@ -789,7 +789,7 @@ void PaintPath(PaintSession& session, uint16_t height, const PathElement& tileEl session.InteractionType = ViewportInteractionItem::None; imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); } - else if (TileInspector::IsElementSelected(reinterpret_cast(&tileElement))) + else if (session.SelectedElement == reinterpret_cast(&tileElement)) { imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); sceneryImageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); diff --git a/src/openrct2/paint/tile_element/Paint.SmallScenery.cpp b/src/openrct2/paint/tile_element/Paint.SmallScenery.cpp index 925811d5f7..5654eb2679 100644 --- a/src/openrct2/paint/tile_element/Paint.SmallScenery.cpp +++ b/src/openrct2/paint/tile_element/Paint.SmallScenery.cpp @@ -346,7 +346,7 @@ void PaintSmallScenery(PaintSession& session, uint8_t direction, int32_t height, session.InteractionType = ViewportInteractionItem::None; imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); } - else if (OpenRCT2::TileInspector::IsElementSelected(reinterpret_cast(&sceneryElement))) + else if (session.SelectedElement == reinterpret_cast(&sceneryElement)) { imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); } diff --git a/src/openrct2/paint/tile_element/Paint.Surface.cpp b/src/openrct2/paint/tile_element/Paint.Surface.cpp index 52135499de..d6fa6d5992 100644 --- a/src/openrct2/paint/tile_element/Paint.Surface.cpp +++ b/src/openrct2/paint/tile_element/Paint.Surface.cpp @@ -1138,7 +1138,7 @@ void PaintSurface(PaintSession& session, uint8_t direction, uint16_t height, con imageId = imageId.WithTransparency(FilterPaletteID::PaletteDarken1); } - if (OpenRCT2::TileInspector::IsElementSelected(elementPtr)) + if (session.SelectedElement == elementPtr) { imageId = imageId.WithRemap(FilterPaletteID::PaletteGhost); } diff --git a/src/openrct2/paint/tile_element/Paint.Wall.cpp b/src/openrct2/paint/tile_element/Paint.Wall.cpp index f004d12797..33443ad93b 100644 --- a/src/openrct2/paint/tile_element/Paint.Wall.cpp +++ b/src/openrct2/paint/tile_element/Paint.Wall.cpp @@ -347,7 +347,7 @@ void PaintWall(PaintSession& session, uint8_t direction, int32_t height, const W imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); isGhost = true; } - else if (OpenRCT2::TileInspector::IsElementSelected(reinterpret_cast(&wallElement))) + else if (session.SelectedElement == reinterpret_cast(&wallElement)) { imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost); isGhost = true; diff --git a/src/openrct2/world/TileInspector.cpp b/src/openrct2/world/TileInspector.cpp index 5a5d2d7326..d9b6ec3040 100644 --- a/src/openrct2/world/TileInspector.cpp +++ b/src/openrct2/world/TileInspector.cpp @@ -37,7 +37,7 @@ TileCoordsXY windowTileInspectorTile; int32_t windowTileInspectorElementCount = 0; -int32_t windowTileInspectorSelectedIndex; +int32_t windowTileInspectorSelectedIndex = -1; using namespace OpenRCT2::TrackMetaData; namespace OpenRCT2::TileInspector @@ -955,17 +955,15 @@ namespace OpenRCT2::TileInspector return GameActions::Result(); } - // NOTE: The pointer is exclusively used to determine the current selection, - // do not access the data, points to potentially invalid memory. - static const TileElement* _highlightedElement = nullptr; - - void SetSelectedElement(const TileElement* elem) + TileElement* GetSelectedElement() { - _highlightedElement = elem; - } - - bool IsElementSelected(const TileElement* elem) - { - return _highlightedElement == elem; + if (windowTileInspectorSelectedIndex == -1) + { + return nullptr; + } + Guard::Assert( + windowTileInspectorSelectedIndex >= 0 && windowTileInspectorSelectedIndex < windowTileInspectorElementCount, + "Selected list item out of range"); + return MapGetNthElementAt(windowTileInspectorTile.ToCoordsXY(), windowTileInspectorSelectedIndex); } } // namespace OpenRCT2::TileInspector diff --git a/src/openrct2/world/TileInspector.h b/src/openrct2/world/TileInspector.h index 03a7f8501a..e1a5b44998 100644 --- a/src/openrct2/world/TileInspector.h +++ b/src/openrct2/world/TileInspector.h @@ -19,8 +19,7 @@ namespace GameActions namespace OpenRCT2::TileInspector { - void SetSelectedElement(const TileElement* elem); - bool IsElementSelected(const TileElement* elem); + TileElement* GetSelectedElement(); GameActions::Result InsertCorruptElementAt(const CoordsXY& loc, int16_t elementIndex, bool isExecuting); GameActions::Result RemoveElementAt(const CoordsXY& loc, int16_t elementIndex, bool isExecuting);