1
0
Fork 0

Codechange: use StringParameters to pass parameters for Tooltip strings

pull/11956/head
Patric Stout 2024-02-02 18:56:34 +01:00
parent 5a7a1f9543
commit b7bbeaf341
11 changed files with 43 additions and 52 deletions

View File

@ -702,10 +702,9 @@ public:
if (widget != WID_RV_TRAIN_WAGONREMOVE_TOGGLE) return false; if (widget != WID_RV_TRAIN_WAGONREMOVE_TOGGLE) return false;
if (Group::IsValidID(this->sel_group)) { if (Group::IsValidID(this->sel_group)) {
SetDParam(0, STR_REPLACE_REMOVE_WAGON_HELP); GuiShowTooltips(this, STR_REPLACE_REMOVE_WAGON_GROUP_HELP, close_cond, MakeParameters(STR_REPLACE_REMOVE_WAGON_HELP));
GuiShowTooltips(this, STR_REPLACE_REMOVE_WAGON_GROUP_HELP, close_cond, 1);
} else { } else {
GuiShowTooltips(this, STR_REPLACE_REMOVE_WAGON_HELP, close_cond); GuiShowTooltips(this, STR_REPLACE_REMOVE_WAGON_HELP, close_cond, MakeParameters());
} }
return true; return true;
} }

View File

@ -885,10 +885,7 @@ struct DepotWindow : Window {
details += GetString(STR_DEPOT_VEHICLE_TOOLTIP_CARGO); details += GetString(STR_DEPOT_VEHICLE_TOOLTIP_CARGO);
} }
/* Show tooltip window */ GuiShowTooltips(this, whole_chain ? STR_DEPOT_VEHICLE_TOOLTIP_CHAIN : STR_DEPOT_VEHICLE_TOOLTIP, TCC_RIGHT_CLICK, MakeParameters(whole_chain ? num : v->engine_type, details));
SetDParam(0, whole_chain ? num : v->engine_type);
SetDParamStr(1, details);
GuiShowTooltips(this, whole_chain ? STR_DEPOT_VEHICLE_TOOLTIP_CHAIN : STR_DEPOT_VEHICLE_TOOLTIP, TCC_RIGHT_CLICK, 2);
return true; return true;
} }

View File

@ -3165,7 +3165,7 @@ struct IndustryCargoesWindow : public Window {
case CFT_INDUSTRY: case CFT_INDUSTRY:
if (fld->u.industry.ind_type < NUM_INDUSTRYTYPES && (this->ind_cargo >= NUM_INDUSTRYTYPES || fieldxy.x != 2)) { if (fld->u.industry.ind_type < NUM_INDUSTRYTYPES && (this->ind_cargo >= NUM_INDUSTRYTYPES || fieldxy.x != 2)) {
GuiShowTooltips(this, STR_INDUSTRY_CARGOES_INDUSTRY_TOOLTIP, close_cond); GuiShowTooltips(this, STR_INDUSTRY_CARGOES_INDUSTRY_TOOLTIP, close_cond, MakeParameters());
} }
return true; return true;
@ -3174,8 +3174,7 @@ struct IndustryCargoesWindow : public Window {
} }
if (IsValidCargoID(cid) && (this->ind_cargo < NUM_INDUSTRYTYPES || cid != this->ind_cargo - NUM_INDUSTRYTYPES)) { if (IsValidCargoID(cid) && (this->ind_cargo < NUM_INDUSTRYTYPES || cid != this->ind_cargo - NUM_INDUSTRYTYPES)) {
const CargoSpec *csp = CargoSpec::Get(cid); const CargoSpec *csp = CargoSpec::Get(cid);
SetDParam(0, csp->name); GuiShowTooltips(this, STR_INDUSTRY_CARGOES_CARGO_TOOLTIP, close_cond, MakeParameters(csp->name));
GuiShowTooltips(this, STR_INDUSTRY_CARGOES_CARGO_TOOLTIP, close_cond, 1);
return true; return true;
} }

View File

@ -401,20 +401,15 @@ bool LinkGraphOverlay::ShowTooltip(Point pt, TooltipCloseCondition close_cond)
SetDParam(0, time); SetDParam(0, time);
tooltip_extension += GetString(STR_LINKGRAPH_STATS_TOOLTIP_TIME_EXTENSION); tooltip_extension += GetString(STR_LINKGRAPH_STATS_TOOLTIP_TIME_EXTENSION);
} }
SetDParam(0, link.cargo);
SetDParam(1, link.Usage());
SetDParam(2, i->first);
SetDParam(3, j->first);
SetDParam(4, link.Usage() * 100 / (link.capacity + 1));
SetDParamStr(5, tooltip_extension);
GuiShowTooltips(this->window, GuiShowTooltips(this->window,
TimerGameEconomy::UsingWallclockUnits() ? STR_LINKGRAPH_STATS_TOOLTIP_MINUTE : STR_LINKGRAPH_STATS_TOOLTIP_MONTH, TimerGameEconomy::UsingWallclockUnits() ? STR_LINKGRAPH_STATS_TOOLTIP_MINUTE : STR_LINKGRAPH_STATS_TOOLTIP_MONTH,
close_cond, 7); close_cond, MakeParameters(link.cargo, link.Usage(), i->first, j->first, link.Usage() * 100 / (link.capacity + 1), tooltip_extension));
return true; return true;
} }
} }
} }
GuiShowTooltips(this->window, STR_NULL, close_cond); GuiShowTooltips(this->window, STR_NULL, close_cond, MakeParameters());
return false; return false;
} }
@ -646,17 +641,15 @@ bool LinkGraphLegendWindow::OnTooltip([[maybe_unused]] Point, WidgetID widget, T
{ {
if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) { if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
if (this->IsWidgetDisabled(widget)) { if (this->IsWidgetDisabled(widget)) {
GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES, close_cond); GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES, close_cond, MakeParameters());
} else { } else {
SetDParam(0, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES); GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_COMPANY_TOOLTIP, close_cond, MakeParameters(STR_LINKGRAPH_LEGEND_SELECT_COMPANIES, (CompanyID)(widget - WID_LGL_COMPANY_FIRST)));
SetDParam(1, (CompanyID)(widget - WID_LGL_COMPANY_FIRST));
GuiShowTooltips(this, STR_LINKGRAPH_LEGEND_COMPANY_TOOLTIP, close_cond, 2);
} }
return true; return true;
} }
if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) { if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
const CargoSpec *cargo = _sorted_cargo_specs[widget - WID_LGL_CARGO_FIRST]; const CargoSpec *cargo = _sorted_cargo_specs[widget - WID_LGL_CARGO_FIRST];
GuiShowTooltips(this, cargo->name, close_cond); GuiShowTooltips(this, cargo->name, close_cond, MakeParameters());
return true; return true;
} }
return false; return false;

View File

@ -664,13 +664,14 @@ struct TooltipsWindow : public Window
std::vector<StringParameterBackup> params; ///< The string parameters. std::vector<StringParameterBackup> params; ///< The string parameters.
TooltipCloseCondition close_cond; ///< Condition for closing the window. TooltipCloseCondition close_cond; ///< Condition for closing the window.
TooltipsWindow(Window *parent, StringID str, uint paramcount, TooltipCloseCondition close_tooltip) : Window(&_tool_tips_desc) TooltipsWindow(Window *parent, StringID str, StringParameters &&params, TooltipCloseCondition close_tooltip) : Window(&_tool_tips_desc)
{ {
this->parent = parent; this->parent = parent;
this->string_id = str; this->string_id = str;
CopyOutDParam(this->params, paramcount); CopyOutDParam(this->params, std::move(params));
this->close_cond = close_tooltip; this->close_cond = close_tooltip;
CopyInDParam(this->params);
this->InitNested(); this->InitNested();
CLRBITS(this->flags, WF_WHITE_BORDER); CLRBITS(this->flags, WF_WHITE_BORDER);
@ -748,15 +749,15 @@ struct TooltipsWindow : public Window
* @param parent The window this tooltip is related to. * @param parent The window this tooltip is related to.
* @param str String to be displayed * @param str String to be displayed
* @param close_tooltip the condition under which the tooltip closes * @param close_tooltip the condition under which the tooltip closes
* @param paramcount number of params to deal with * @param params Parameters for the string.
*/ */
void GuiShowTooltips(Window *parent, StringID str, TooltipCloseCondition close_tooltip, uint paramcount) void GuiShowTooltips(Window *parent, StringID str, TooltipCloseCondition close_tooltip, StringParameters &&params)
{ {
CloseWindowById(WC_TOOLTIPS, 0); CloseWindowById(WC_TOOLTIPS, 0);
if (str == STR_NULL || !_cursor.in_window) return; if (str == STR_NULL || !_cursor.in_window) return;
new TooltipsWindow(parent, str, paramcount, close_tooltip); new TooltipsWindow(parent, str, std::move(params), close_tooltip);
} }
void QueryString::HandleEditBox(Window *w, WidgetID wid) void QueryString::HandleEditBox(Window *w, WidgetID wid)

View File

@ -1803,10 +1803,10 @@ public:
if (IsInsideMM(pt.x, player_icon_x, player_icon_x + d2.width)) { if (IsInsideMM(pt.x, player_icon_x, player_icon_x + d2.width)) {
if (index == this->player_self_index) { if (index == this->player_self_index) {
GuiShowTooltips(this, STR_NETWORK_CLIENT_LIST_PLAYER_ICON_SELF_TOOLTIP, close_cond); GuiShowTooltips(this, STR_NETWORK_CLIENT_LIST_PLAYER_ICON_SELF_TOOLTIP, close_cond, MakeParameters());
return true; return true;
} else if (index == this->player_host_index) { } else if (index == this->player_host_index) {
GuiShowTooltips(this, STR_NETWORK_CLIENT_LIST_PLAYER_ICON_HOST_TOOLTIP, close_cond); GuiShowTooltips(this, STR_NETWORK_CLIENT_LIST_PLAYER_ICON_HOST_TOOLTIP, close_cond, MakeParameters());
return true; return true;
} }
} }
@ -1814,7 +1814,7 @@ public:
ButtonCommon *button = this->GetButtonAtPoint(pt); ButtonCommon *button = this->GetButtonAtPoint(pt);
if (button == nullptr) return false; if (button == nullptr) return false;
GuiShowTooltips(this, button->tooltip, close_cond); GuiShowTooltips(this, button->tooltip, close_cond, MakeParameters());
return true; return true;
}; };
} }

View File

@ -458,7 +458,7 @@ struct BuildRailToolbarWindow : Window {
if (std::find(std::begin(can_build_widgets), std::end(can_build_widgets), widget) == std::end(can_build_widgets)) return false; if (std::find(std::begin(can_build_widgets), std::end(can_build_widgets), widget) == std::end(can_build_widgets)) return false;
GuiShowTooltips(this, STR_TOOLBAR_DISABLED_NO_VEHICLE_AVAILABLE, close_cond); GuiShowTooltips(this, STR_TOOLBAR_DISABLED_NO_VEHICLE_AVAILABLE, close_cond, MakeParameters());
return true; return true;
} }

View File

@ -2736,7 +2736,7 @@ struct VehicleDetailsWindow : Window {
} else { } else {
tool_tip = widget == WID_VD_INCREASE_SERVICING_INTERVAL ? STR_VEHICLE_DETAILS_INCREASE_SERVICING_INTERVAL_TOOLTIP_DAYS : STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP_DAYS; tool_tip = widget == WID_VD_INCREASE_SERVICING_INTERVAL ? STR_VEHICLE_DETAILS_INCREASE_SERVICING_INTERVAL_TOOLTIP_DAYS : STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP_DAYS;
} }
GuiShowTooltips(this, tool_tip, close_cond); GuiShowTooltips(this, tool_tip, close_cond, MakeParameters());
return true; return true;
} }

View File

@ -2699,12 +2699,12 @@ void UpdateTileSelection()
/** /**
* Displays the measurement tooltips when selecting multiple tiles * Displays the measurement tooltips when selecting multiple tiles
* @param str String to be displayed * @param str String to be displayed
* @param paramcount number of params to deal with * @param params Parameters of the string.
*/ */
static inline void ShowMeasurementTooltips(StringID str, uint paramcount) static inline void ShowMeasurementTooltips(StringID str, StringParameters &&params)
{ {
if (!_settings_client.gui.measure_tooltip) return; if (!_settings_client.gui.measure_tooltip) return;
GuiShowTooltips(_thd.GetCallbackWnd(), str, TCC_EXIT_VIEWPORT, paramcount); GuiShowTooltips(_thd.GetCallbackWnd(), str, TCC_EXIT_VIEWPORT, std::move(params));
} }
static void HideMeasurementTooltips() static void HideMeasurementTooltips()
@ -2780,8 +2780,7 @@ void VpSetPresizeRange(TileIndex from, TileIndex to)
/* show measurement only if there is any length to speak of */ /* show measurement only if there is any length to speak of */
if (distance > 1) { if (distance > 1) {
SetDParam(0, distance); ShowMeasurementTooltips(STR_MEASURE_LENGTH, MakeParameters(distance));
ShowMeasurementTooltips(STR_MEASURE_LENGTH, 1);
} else { } else {
HideMeasurementTooltips(); HideMeasurementTooltips();
} }
@ -3168,7 +3167,8 @@ static void CalcRaildirsDrawstyle(int x, int y, int method)
TileIndex t0 = TileVirtXY(_thd.selstart.x, _thd.selstart.y); TileIndex t0 = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
TileIndex t1 = TileVirtXY(x, y); TileIndex t1 = TileVirtXY(x, y);
uint distance = DistanceManhattan(t0, t1) + 1; uint distance = DistanceManhattan(t0, t1) + 1;
byte index = 0; auto params = MakeParameters(0, 0);
int index = 0;
if (distance != 1) { if (distance != 1) {
int heightdiff = CalcHeightdiff(b, distance, t0, t1); int heightdiff = CalcHeightdiff(b, distance, t0, t1);
@ -3179,11 +3179,11 @@ static void CalcRaildirsDrawstyle(int x, int y, int method)
distance = CeilDiv(distance, 2); distance = CeilDiv(distance, 2);
} }
SetDParam(index++, distance); params.SetParam(index++, distance);
if (heightdiff != 0) SetDParam(index++, heightdiff); if (heightdiff != 0) params.SetParam(index++, heightdiff);
} }
ShowMeasurementTooltips(measure_strings_length[index], index); ShowMeasurementTooltips(measure_strings_length[index], std::move(params));
} }
_thd.selend.x = x; _thd.selend.x = x;
@ -3264,6 +3264,7 @@ calc_heightdiff_single_direction:;
TileIndex t0 = TileVirtXY(sx, sy); TileIndex t0 = TileVirtXY(sx, sy);
TileIndex t1 = TileVirtXY(x, y); TileIndex t1 = TileVirtXY(x, y);
uint distance = DistanceManhattan(t0, t1) + 1; uint distance = DistanceManhattan(t0, t1) + 1;
auto params = MakeParameters(0, 0);
byte index = 0; byte index = 0;
if (distance != 1) { if (distance != 1) {
@ -3274,11 +3275,11 @@ calc_heightdiff_single_direction:;
* new_style := (_thd.next_drawstyle & HT_RECT) ? HT_LINE | style : _thd.next_drawstyle; */ * new_style := (_thd.next_drawstyle & HT_RECT) ? HT_LINE | style : _thd.next_drawstyle; */
int heightdiff = CalcHeightdiff(HT_LINE | style, 0, t0, t1); int heightdiff = CalcHeightdiff(HT_LINE | style, 0, t0, t1);
SetDParam(index++, distance); params.SetParam(index++, distance);
if (heightdiff != 0) SetDParam(index++, heightdiff); if (heightdiff != 0) params.SetParam(index++, heightdiff);
} }
ShowMeasurementTooltips(measure_strings_length[index], index); ShowMeasurementTooltips(measure_strings_length[index], std::move(params));
} }
break; break;
@ -3298,6 +3299,7 @@ calc_heightdiff_single_direction:;
TileIndex t1 = TileVirtXY(x, y); TileIndex t1 = TileVirtXY(x, y);
uint dx = Delta(TileX(t0), TileX(t1)) + 1; uint dx = Delta(TileX(t0), TileX(t1)) + 1;
uint dy = Delta(TileY(t0), TileY(t1)) + 1; uint dy = Delta(TileY(t0), TileY(t1)) + 1;
auto params = MakeParameters(0, 0, 0);
byte index = 0; byte index = 0;
/* If dragging an area (eg dynamite tool) and it is actually a single /* If dragging an area (eg dynamite tool) and it is actually a single
@ -3343,12 +3345,12 @@ calc_heightdiff_single_direction:;
if (dx != 1 || dy != 1) { if (dx != 1 || dy != 1) {
int heightdiff = CalcHeightdiff(style, 0, t0, t1); int heightdiff = CalcHeightdiff(style, 0, t0, t1);
SetDParam(index++, dx - (style & HT_POINT ? 1 : 0)); params.SetParam(index++, dx - (style & HT_POINT ? 1 : 0));
SetDParam(index++, dy - (style & HT_POINT ? 1 : 0)); params.SetParam(index++, dy - (style & HT_POINT ? 1 : 0));
if (heightdiff != 0) SetDParam(index++, heightdiff); if (heightdiff != 0) params.SetParam(index++, heightdiff);
} }
ShowMeasurementTooltips(measure_strings_area[index], index); ShowMeasurementTooltips(measure_strings_area[index], std::move(params));
} }
break; break;

View File

@ -758,7 +758,7 @@ static void DispatchRightClickEvent(Window *w, int x, int y)
/* Right-click close is enabled, but excluding sticky windows. */ /* Right-click close is enabled, but excluding sticky windows. */
w->Close(); w->Close();
} else if (_settings_client.gui.hover_delay_ms == 0 && !w->OnTooltip(pt, wid->index, TCC_RIGHT_CLICK) && wid->tool_tip != 0) { } else if (_settings_client.gui.hover_delay_ms == 0 && !w->OnTooltip(pt, wid->index, TCC_RIGHT_CLICK) && wid->tool_tip != 0) {
GuiShowTooltips(w, wid->tool_tip, TCC_RIGHT_CLICK); GuiShowTooltips(w, wid->tool_tip, TCC_RIGHT_CLICK, MakeParameters());
} }
} }
@ -779,7 +779,7 @@ static void DispatchHoverEvent(Window *w, int x, int y)
/* Show the tooltip if there is any */ /* Show the tooltip if there is any */
if (!w->OnTooltip(pt, wid->index, TCC_HOVER) && wid->tool_tip != 0) { if (!w->OnTooltip(pt, wid->index, TCC_HOVER) && wid->tool_tip != 0) {
GuiShowTooltips(w, wid->tool_tip, TCC_HOVER); GuiShowTooltips(w, wid->tool_tip, TCC_HOVER, MakeParameters());
return; return;
} }

View File

@ -1014,7 +1014,7 @@ Wcls *AllocateWindowDescFront(WindowDesc *desc, int window_number, bool return_e
void RelocateAllWindows(int neww, int newh); void RelocateAllWindows(int neww, int newh);
void GuiShowTooltips(Window *parent, StringID str, TooltipCloseCondition close_tooltip, uint paramcount = 0); void GuiShowTooltips(Window *parent, StringID str, TooltipCloseCondition close_tooltip, class StringParameters &&params);
/* widget.cpp */ /* widget.cpp */
WidgetID GetWidgetFromPos(const Window *w, int x, int y); WidgetID GetWidgetFromPos(const Window *w, int x, int y);