From 0238a2b56708c17bd268edfe778837cda4f8a335 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sat, 12 Aug 2023 18:05:00 +0200 Subject: [PATCH] Codechange: use std::variant instead of using bitflags in the value (#11191) --- src/news_gui.cpp | 6 +++++- src/stdafx.h | 1 + src/vehicle_gui.cpp | 2 +- src/viewport.cpp | 14 ++++++-------- src/viewport_func.h | 3 ++- src/widget.cpp | 6 +++--- src/widget_type.h | 2 +- 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/news_gui.cpp b/src/news_gui.cpp index e52dc18bf0..f6ffc7e683 100644 --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -308,7 +308,11 @@ struct NewsWindow : Window { /* Initialize viewport if it exists. */ NWidgetViewport *nvp = this->GetWidget(WID_N_VIEWPORT); if (nvp != nullptr) { - nvp->InitializeViewport(this, ni->reftype1 == NR_VEHICLE ? 0x80000000 | ni->ref1 : (uint32_t)GetReferenceTile(ni->reftype1, ni->ref1),ScaleZoomGUI(ZOOM_LVL_NEWS)); + if (ni->reftype1 == NR_VEHICLE) { + nvp->InitializeViewport(this, GetReferenceTile(ni->reftype1, ni->ref1), ScaleZoomGUI(ZOOM_LVL_NEWS)); + } else { + nvp->InitializeViewport(this, static_cast(ni->ref1), ScaleZoomGUI(ZOOM_LVL_NEWS)); + } if (this->ni->flags & NF_NO_TRANSPARENT) nvp->disp_flags |= ND_NO_TRANSPARENCY; if ((this->ni->flags & NF_INCOLOUR) == 0) { nvp->disp_flags |= ND_SHADE_GREY; diff --git a/src/stdafx.h b/src/stdafx.h index 2e4d3a7586..ae6805efe6 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -80,6 +80,7 @@ #include #include #include +#include #include #if defined(UNIX) || defined(__MINGW32__) diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index ed4d6acb02..9810c9c17f 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -2915,7 +2915,7 @@ public: } this->FinishInitNested(window_number); this->owner = v->owner; - this->GetWidget(WID_VV_VIEWPORT)->InitializeViewport(this, this->window_number | (1 << 31), ScaleZoomGUI(_vehicle_view_zoom_levels[v->type])); + this->GetWidget(WID_VV_VIEWPORT)->InitializeViewport(this, static_cast(this->window_number), ScaleZoomGUI(_vehicle_view_zoom_levels[v->type])); this->GetWidget(WID_VV_START_STOP)->tool_tip = STR_VEHICLE_VIEW_TRAIN_STATUS_START_STOP_TOOLTIP + v->type; this->GetWidget(WID_VV_RENAME)->tool_tip = STR_VEHICLE_DETAILS_TRAIN_RENAME + v->type; diff --git a/src/viewport.cpp b/src/viewport.cpp index 02548dd442..02fd8796dd 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -213,13 +213,11 @@ void DeleteWindowViewport(Window *w) * @param y Offset of top edge of viewport with respect to top edge window \a w * @param width Width of the viewport * @param height Height of the viewport - * @param follow_flags Flags controlling the viewport. - * - If bit 31 is set, the lower 20 bits are the vehicle that the viewport should follow. - * - If bit 31 is clear, it is a #TileIndex. + * @param focus Either the tile index or vehicle ID to focus. * @param zoom Zoomlevel to display */ void InitializeWindowViewport(Window *w, int x, int y, - int width, int height, uint32_t follow_flags, ZoomLevel zoom) + int width, int height, std::variant focus, ZoomLevel zoom) { assert(w->viewport == nullptr); @@ -237,15 +235,15 @@ void InitializeWindowViewport(Window *w, int x, int y, Point pt; - if (follow_flags & 0x80000000) { + if (std::holds_alternative(focus)) { const Vehicle *veh; - vp->follow_vehicle = (VehicleID)(follow_flags & 0xFFFFF); + vp->follow_vehicle = std::get(focus); veh = Vehicle::Get(vp->follow_vehicle); pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos); } else { - x = TileX(follow_flags) * TILE_SIZE; - y = TileY(follow_flags) * TILE_SIZE; + x = TileX(std::get(focus)) * TILE_SIZE; + y = TileY(std::get(focus)) * TILE_SIZE; vp->follow_vehicle = INVALID_VEHICLE; pt = MapXYZToViewport(vp, x, y, GetSlopePixelZ(x, y)); diff --git a/src/viewport_func.h b/src/viewport_func.h index d6be050979..a9111c4375 100644 --- a/src/viewport_func.h +++ b/src/viewport_func.h @@ -15,13 +15,14 @@ #include "window_type.h" #include "tile_map.h" #include "station_type.h" +#include "vehicle_type.h" static const int TILE_HEIGHT_STEP = 50; ///< One Z unit tile height difference is displayed as 50m. void SetSelectionRed(bool); void DeleteWindowViewport(Window *w); -void InitializeWindowViewport(Window *w, int x, int y, int width, int height, uint32_t follow_flags, ZoomLevel zoom); +void InitializeWindowViewport(Window *w, int x, int y, int width, int height, std::variant focus, ZoomLevel zoom); Viewport *IsPtInWindowViewport(const Window *w, int x, int y); Point TranslateXYToTileCoord(const Viewport *vp, int x, int y, bool clamp_to_map = true); Point GetTileBelowCursor(); diff --git a/src/widget.cpp b/src/widget.cpp index 5dafacd7a2..e41dd57cdc 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -2333,12 +2333,12 @@ void NWidgetViewport::Draw(const Window *w) /** * Initialize the viewport of the window. * @param w Window owning the viewport. - * @param follow_flags Type of viewport, see #InitializeWindowViewport(). + * @param focus Either the tile index or vehicle ID to focus. * @param zoom Zoom level. */ -void NWidgetViewport::InitializeViewport(Window *w, uint32_t follow_flags, ZoomLevel zoom) +void NWidgetViewport::InitializeViewport(Window *w, std::variant focus, ZoomLevel zoom) { - InitializeWindowViewport(w, this->pos_x, this->pos_y, this->current_x, this->current_y, follow_flags, zoom); + InitializeWindowViewport(w, this->pos_x, this->pos_y, this->current_x, this->current_y, focus, zoom); } /** diff --git a/src/widget_type.h b/src/widget_type.h index 96776c1af1..6edbdf0533 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -629,7 +629,7 @@ public: void SetupSmallestSize(Window *w, bool init_array) override; void Draw(const Window *w) override; - void InitializeViewport(Window *w, uint32_t follow_flags, ZoomLevel zoom); + void InitializeViewport(Window *w, std::variant focus, ZoomLevel zoom); void UpdateViewportCoordinates(Window *w); };