From fb06ddafbae814639a55d8067449788a3e625aea Mon Sep 17 00:00:00 2001 From: Rubidium Date: Thu, 2 Jan 2025 12:51:56 +0100 Subject: [PATCH] Codechange: add and use SetSprite over directly accessing widget_data --- src/depot_gui.cpp | 24 ++++++++++++------------ src/group_gui.cpp | 2 +- src/music_gui.cpp | 6 +++--- src/rail_gui.cpp | 16 ++++++++-------- src/road_gui.cpp | 12 ++++++------ src/vehicle_gui.cpp | 4 ++-- src/widget.cpp | 9 +++++++++ src/widget_type.h | 3 ++- 8 files changed, 43 insertions(+), 33 deletions(-) diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index 3a197686b0..5225a58b35 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -612,36 +612,36 @@ struct DepotWindow : Window { this->GetWidget(WID_D_VEHICLE_LIST)->widget_data = STR_TRAIN; /* Sprites */ - this->GetWidget(WID_D_SELL)->widget_data = SPR_SELL_TRAIN; - this->GetWidget(WID_D_SELL_ALL)->widget_data = SPR_SELL_ALL_TRAIN; - this->GetWidget(WID_D_AUTOREPLACE)->widget_data = SPR_REPLACE_TRAIN; + this->GetWidget(WID_D_SELL)->SetSprite(SPR_SELL_TRAIN); + this->GetWidget(WID_D_SELL_ALL)->SetSprite(SPR_SELL_ALL_TRAIN); + this->GetWidget(WID_D_AUTOREPLACE)->SetSprite(SPR_REPLACE_TRAIN); break; case VEH_ROAD: this->GetWidget(WID_D_VEHICLE_LIST)->widget_data = STR_LORRY; /* Sprites */ - this->GetWidget(WID_D_SELL)->widget_data = SPR_SELL_ROADVEH; - this->GetWidget(WID_D_SELL_ALL)->widget_data = SPR_SELL_ALL_ROADVEH; - this->GetWidget(WID_D_AUTOREPLACE)->widget_data = SPR_REPLACE_ROADVEH; + this->GetWidget(WID_D_SELL)->SetSprite(SPR_SELL_ROADVEH); + this->GetWidget(WID_D_SELL_ALL)->SetSprite(SPR_SELL_ALL_ROADVEH); + this->GetWidget(WID_D_AUTOREPLACE)->SetSprite(SPR_REPLACE_ROADVEH); break; case VEH_SHIP: this->GetWidget(WID_D_VEHICLE_LIST)->widget_data = STR_SHIP; /* Sprites */ - this->GetWidget(WID_D_SELL)->widget_data = SPR_SELL_SHIP; - this->GetWidget(WID_D_SELL_ALL)->widget_data = SPR_SELL_ALL_SHIP; - this->GetWidget(WID_D_AUTOREPLACE)->widget_data = SPR_REPLACE_SHIP; + this->GetWidget(WID_D_SELL)->SetSprite(SPR_SELL_SHIP); + this->GetWidget(WID_D_SELL_ALL)->SetSprite(SPR_SELL_ALL_SHIP); + this->GetWidget(WID_D_AUTOREPLACE)->SetSprite(SPR_REPLACE_SHIP); break; case VEH_AIRCRAFT: this->GetWidget(WID_D_VEHICLE_LIST)->widget_data = STR_PLANE; /* Sprites */ - this->GetWidget(WID_D_SELL)->widget_data = SPR_SELL_AIRCRAFT; - this->GetWidget(WID_D_SELL_ALL)->widget_data = SPR_SELL_ALL_AIRCRAFT; - this->GetWidget(WID_D_AUTOREPLACE)->widget_data = SPR_REPLACE_AIRCRAFT; + this->GetWidget(WID_D_SELL)->SetSprite(SPR_SELL_AIRCRAFT); + this->GetWidget(WID_D_SELL_ALL)->SetSprite(SPR_SELL_ALL_AIRCRAFT); + this->GetWidget(WID_D_AUTOREPLACE)->SetSprite(SPR_REPLACE_AIRCRAFT); break; } } diff --git a/src/group_gui.cpp b/src/group_gui.cpp index 839ca6577f..031d88ef46 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -585,7 +585,7 @@ public: /* If not a default group and the group has replace protection, show an enabled replace sprite. */ uint16_t protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN; if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && HasBit(Group::Get(this->vli.index)->flags, GroupFlags::GF_REPLACE_PROTECTION)) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN; - this->GetWidget(WID_GL_REPLACE_PROTECTION)->widget_data = protect_sprite + this->vli.vtype; + this->GetWidget(WID_GL_REPLACE_PROTECTION)->SetSprite(protect_sprite + this->vli.vtype); /* Set text of "group by" dropdown widget. */ this->GetWidget(WID_GL_GROUP_BY_DROPDOWN)->widget_data = std::data(this->vehicle_group_by_names)[this->grouping]; diff --git a/src/music_gui.cpp b/src/music_gui.cpp index 815166579d..e2d2a0c4b9 100644 --- a/src/music_gui.cpp +++ b/src/music_gui.cpp @@ -736,9 +736,9 @@ struct MusicWindow : public Window { /* Hack-ish: set the proper widget data; only needs to be done once * per (Re)Init as that's the only time the language changes. */ - case WID_M_PREV: this->GetWidget(WID_M_PREV)->widget_data = _current_text_dir == TD_RTL ? SPR_IMG_SKIP_TO_NEXT : SPR_IMG_SKIP_TO_PREV; break; - case WID_M_NEXT: this->GetWidget(WID_M_NEXT)->widget_data = _current_text_dir == TD_RTL ? SPR_IMG_SKIP_TO_PREV : SPR_IMG_SKIP_TO_NEXT; break; - case WID_M_PLAY: this->GetWidget(WID_M_PLAY)->widget_data = _current_text_dir == TD_RTL ? SPR_IMG_PLAY_MUSIC_RTL : SPR_IMG_PLAY_MUSIC; break; + case WID_M_PREV: this->GetWidget(WID_M_PREV)->SetSprite(_current_text_dir == TD_RTL ? SPR_IMG_SKIP_TO_NEXT : SPR_IMG_SKIP_TO_PREV); break; + case WID_M_NEXT: this->GetWidget(WID_M_NEXT)->SetSprite(_current_text_dir == TD_RTL ? SPR_IMG_SKIP_TO_PREV : SPR_IMG_SKIP_TO_NEXT); break; + case WID_M_PLAY: this->GetWidget(WID_M_PLAY)->SetSprite(_current_text_dir == TD_RTL ? SPR_IMG_PLAY_MUSIC_RTL : SPR_IMG_PLAY_MUSIC); break; } } diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 83558a57a1..a31b9a1cde 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -500,14 +500,14 @@ struct BuildRailToolbarWindow : Window { { /* Configure the rail toolbar for the railtype. */ const RailTypeInfo *rti = GetRailTypeInfo(this->railtype); - this->GetWidget(WID_RAT_BUILD_NS)->widget_data = rti->gui_sprites.build_ns_rail; - this->GetWidget(WID_RAT_BUILD_X)->widget_data = rti->gui_sprites.build_x_rail; - this->GetWidget(WID_RAT_BUILD_EW)->widget_data = rti->gui_sprites.build_ew_rail; - this->GetWidget(WID_RAT_BUILD_Y)->widget_data = rti->gui_sprites.build_y_rail; - this->GetWidget(WID_RAT_AUTORAIL)->widget_data = rti->gui_sprites.auto_rail; - this->GetWidget(WID_RAT_BUILD_DEPOT)->widget_data = rti->gui_sprites.build_depot; - this->GetWidget(WID_RAT_CONVERT_RAIL)->widget_data = rti->gui_sprites.convert_rail; - this->GetWidget(WID_RAT_BUILD_TUNNEL)->widget_data = rti->gui_sprites.build_tunnel; + this->GetWidget(WID_RAT_BUILD_NS)->SetSprite(rti->gui_sprites.build_ns_rail); + this->GetWidget(WID_RAT_BUILD_X)->SetSprite(rti->gui_sprites.build_x_rail); + this->GetWidget(WID_RAT_BUILD_EW)->SetSprite(rti->gui_sprites.build_ew_rail); + this->GetWidget(WID_RAT_BUILD_Y)->SetSprite(rti->gui_sprites.build_y_rail); + this->GetWidget(WID_RAT_AUTORAIL)->SetSprite(rti->gui_sprites.auto_rail); + this->GetWidget(WID_RAT_BUILD_DEPOT)->SetSprite(rti->gui_sprites.build_depot); + this->GetWidget(WID_RAT_CONVERT_RAIL)->SetSprite(rti->gui_sprites.convert_rail); + this->GetWidget(WID_RAT_BUILD_TUNNEL)->SetSprite(rti->gui_sprites.build_tunnel); } /** diff --git a/src/road_gui.cpp b/src/road_gui.cpp index 1f8c916248..f4007856bb 100644 --- a/src/road_gui.cpp +++ b/src/road_gui.cpp @@ -419,14 +419,14 @@ struct BuildRoadToolbarWindow : Window { { /* Configure the road toolbar for the roadtype. */ const RoadTypeInfo *rti = GetRoadTypeInfo(this->roadtype); - this->GetWidget(WID_ROT_ROAD_X)->widget_data = rti->gui_sprites.build_x_road; - this->GetWidget(WID_ROT_ROAD_Y)->widget_data = rti->gui_sprites.build_y_road; - this->GetWidget(WID_ROT_AUTOROAD)->widget_data = rti->gui_sprites.auto_road; + this->GetWidget(WID_ROT_ROAD_X)->SetSprite(rti->gui_sprites.build_x_road); + this->GetWidget(WID_ROT_ROAD_Y)->SetSprite(rti->gui_sprites.build_y_road); + this->GetWidget(WID_ROT_AUTOROAD)->SetSprite(rti->gui_sprites.auto_road); if (_game_mode != GM_EDITOR) { - this->GetWidget(WID_ROT_DEPOT)->widget_data = rti->gui_sprites.build_depot; + this->GetWidget(WID_ROT_DEPOT)->SetSprite(rti->gui_sprites.build_depot); } - this->GetWidget(WID_ROT_CONVERT_ROAD)->widget_data = rti->gui_sprites.convert_road; - this->GetWidget(WID_ROT_BUILD_TUNNEL)->widget_data = rti->gui_sprites.build_tunnel; + this->GetWidget(WID_ROT_CONVERT_ROAD)->SetSprite(rti->gui_sprites.convert_road); + this->GetWidget(WID_ROT_BUILD_TUNNEL)->SetSprite(rti->gui_sprites.build_tunnel); } /** diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 9768e33e39..7f62db7744 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -3069,7 +3069,7 @@ public: SPR_SEND_AIRCRAFT_TODEPOT, }; const Vehicle *v = Vehicle::Get(window_number); - this->GetWidget(WID_VV_GOTO_DEPOT)->widget_data = vehicle_view_goto_depot_sprites[v->type]; + this->GetWidget(WID_VV_GOTO_DEPOT)->SetSprite(vehicle_view_goto_depot_sprites[v->type]); /* Sprites for the 'clone vehicle' button indexed by vehicle type. */ static const SpriteID vehicle_view_clone_sprites[] = { @@ -3078,7 +3078,7 @@ public: SPR_CLONE_SHIP, SPR_CLONE_AIRCRAFT, }; - this->GetWidget(WID_VV_CLONE)->widget_data = vehicle_view_clone_sprites[v->type]; + this->GetWidget(WID_VV_CLONE)->SetSprite(vehicle_view_clone_sprites[v->type]); switch (v->type) { case VEH_TRAIN: diff --git a/src/widget.cpp b/src/widget.cpp index ef5ed0cb80..c0600798b8 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -1134,6 +1134,15 @@ void NWidgetCore::SetDataTip(uint32_t widget_data, StringID tool_tip) this->tool_tip = tool_tip; } +/** + * Set sprite of the nested widget. + * @param sprite The new sprite. + */ +void NWidgetCore::SetSprite(SpriteID sprite) +{ + this->widget_data = sprite; +} + /** * Set the text style of the nested widget. * @param colour TextColour to use. diff --git a/src/widget_type.h b/src/widget_type.h index 52fb757f69..e5b37f1c85 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -374,7 +374,8 @@ public: void SetDataTip(uint32_t widget_data, StringID tool_tip); void SetStringTip(StringID string, StringID tool_tip) { this->SetDataTip(string, tool_tip); } - void SetSpriteTip(SpriteID sprite, StringID tool_tip) { this->SetDataTip(sprite, tool_tip); } + void SetSprite(SpriteID sprite); + void SetSpriteTip(SpriteID sprite, StringID tool_tip) { this->SetSprite(sprite); this->SetToolTip(tool_tip); } void SetMatrixDataTip(uint8_t cols, uint8_t rows, StringID tip) { this->SetDataTip(static_cast((rows << MAT_ROW_START) | (cols << MAT_COL_START)), tip); } void SetToolTip(StringID tool_tip); StringID GetToolTip() const;