1
0
Fork 0

Codechange: Store EncodedString for tooltip text.

This replaces capturing and storing string parameters.
pull/13528/head
Peter Nelson 2024-12-07 01:41:31 +00:00 committed by Peter Nelson
parent 1f21e9dc74
commit 2cb9f55183
12 changed files with 67 additions and 77 deletions

View File

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

View File

@ -584,7 +584,7 @@ struct CheatWindow : Window {
const SettingDesc *desc = this->sandbox_settings[row];
const IntSettingDesc *sd = desc->AsIntSetting();
GuiShowTooltips(this, sd->GetHelp(), close_cond);
GuiShowTooltips(this, GetEncodedString(sd->GetHelp()), close_cond);
return true;
}

View File

@ -899,9 +899,11 @@ struct DepotWindow : Window {
}
/* Show tooltip window */
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);
if (whole_chain) {
GuiShowTooltips(this, GetEncodedString(STR_DEPOT_VEHICLE_TOOLTIP_CHAIN, num, details), TCC_RIGHT_CLICK);
} else {
GuiShowTooltips(this, GetEncodedString(STR_DEPOT_VEHICLE_TOOLTIP, v->engine_type, details), TCC_RIGHT_CLICK);
}
return true;
}

View File

@ -3181,7 +3181,7 @@ struct IndustryCargoesWindow : public Window {
case CFT_INDUSTRY:
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, GetEncodedString(STR_INDUSTRY_CARGOES_INDUSTRY_TOOLTIP), close_cond);
}
return true;
@ -3190,8 +3190,7 @@ struct IndustryCargoesWindow : public Window {
}
if (IsValidCargoType(cargo_type) && (this->ind_cargo < NUM_INDUSTRYTYPES || cargo_type != this->ind_cargo - NUM_INDUSTRYTYPES)) {
const CargoSpec *csp = CargoSpec::Get(cargo_type);
SetDParam(0, csp->name);
GuiShowTooltips(this, STR_INDUSTRY_CARGOES_CARGO_TOOLTIP, close_cond, 1);
GuiShowTooltips(this, GetEncodedString(STR_INDUSTRY_CARGOES_CARGO_TOOLTIP, csp->name), close_cond);
return true;
}

View File

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

View File

@ -653,15 +653,12 @@ static WindowDesc _tool_tips_desc(
/** Window for displaying a tooltip. */
struct TooltipsWindow : public Window
{
StringID string_id; ///< String to display as tooltip.
std::vector<StringParameterData> params; ///< The string parameters.
EncodedString text; ///< String to display as tooltip.
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, EncodedString &&text, TooltipCloseCondition close_tooltip) : Window(_tool_tips_desc), text(std::move(text))
{
this->parent = parent;
this->string_id = str;
CopyOutDParam(this->params, paramcount);
this->close_cond = close_tooltip;
this->InitNested();
@ -692,10 +689,10 @@ struct TooltipsWindow : public Window
void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
{
if (widget != WID_TT_BACKGROUND) return;
CopyInDParam(this->params);
size.width = std::min<uint>(GetStringBoundingBox(this->string_id).width, ScaleGUITrad(194));
size.height = GetStringHeight(this->string_id, size.width);
auto str = this->text.GetDecodedString();
size.width = std::min<uint>(GetStringBoundingBox(str).width, ScaleGUITrad(194));
size.height = GetStringHeight(str, size.width);
/* Increase slightly to have some space around the box. */
size.width += WidgetDimensions::scaled.framerect.Horizontal() + WidgetDimensions::scaled.fullbevel.Horizontal();
@ -708,8 +705,7 @@ struct TooltipsWindow : public Window
GfxFillRect(r, PC_BLACK);
GfxFillRect(r.Shrink(WidgetDimensions::scaled.bevel), PC_LIGHT_YELLOW);
CopyInDParam(this->params);
DrawStringMultiLine(r.Shrink(WidgetDimensions::scaled.framerect).Shrink(WidgetDimensions::scaled.fullbevel), this->string_id, TC_BLACK, SA_CENTER);
DrawStringMultiLine(r.Shrink(WidgetDimensions::scaled.framerect).Shrink(WidgetDimensions::scaled.fullbevel), this->text.GetDecodedString(), TC_BLACK, SA_CENTER);
}
void OnMouseLoop() override
@ -739,17 +735,16 @@ struct TooltipsWindow : public Window
/**
* Shows a tooltip
* @param parent The window this tooltip is related to.
* @param str String to be displayed
* @param text String to be displayed. May include encoded parameters.
* @param close_tooltip the condition under which the tooltip closes
* @param paramcount number of params to deal with
*/
void GuiShowTooltips(Window *parent, StringID str, TooltipCloseCondition close_tooltip, uint paramcount)
void GuiShowTooltips(Window *parent, EncodedString &&text, TooltipCloseCondition close_tooltip)
{
CloseWindowById(WC_TOOLTIPS, 0);
if (str == STR_NULL || !_cursor.in_window) return;
if (text.empty() || !_cursor.in_window) return;
new TooltipsWindow(parent, str, paramcount, close_tooltip);
new TooltipsWindow(parent, std::move(text), close_tooltip);
}
void QueryString::HandleEditBox(Window *w, WidgetID wid)

View File

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

View File

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

View File

@ -2818,7 +2818,7 @@ struct VehicleDetailsWindow : Window {
} 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;
}
GuiShowTooltips(this, tool_tip, close_cond);
GuiShowTooltips(this, GetEncodedString(tool_tip), close_cond);
return true;
}

View File

@ -2761,13 +2761,12 @@ void UpdateTileSelection()
/**
* Displays the measurement tooltips when selecting multiple tiles
* @param str String to be displayed
* @param paramcount number of params to deal with
* @param text String to be displayed
*/
static inline void ShowMeasurementTooltips(StringID str, uint paramcount)
static inline void ShowMeasurementTooltips(EncodedString &&text)
{
if (!_settings_client.gui.measure_tooltip) return;
GuiShowTooltips(_thd.GetCallbackWnd(), str, TCC_EXIT_VIEWPORT, paramcount);
GuiShowTooltips(_thd.GetCallbackWnd(), std::move(text), TCC_EXIT_VIEWPORT);
}
static void HideMeasurementTooltips()
@ -2843,8 +2842,7 @@ void VpSetPresizeRange(TileIndex from, TileIndex to)
/* show measurement only if there is any length to speak of */
if (distance > 1) {
SetDParam(0, distance);
ShowMeasurementTooltips(STR_MEASURE_LENGTH, 1);
ShowMeasurementTooltips(GetEncodedString(STR_MEASURE_LENGTH, distance));
} else {
HideMeasurementTooltips();
}
@ -3019,8 +3017,6 @@ static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_t
return (int)(h1 - h0) * TILE_HEIGHT_STEP;
}
static const StringID measure_strings_length[] = {STR_NULL, STR_MEASURE_LENGTH, STR_MEASURE_LENGTH_HEIGHTDIFF};
/**
* Check for underflowing the map.
* @param test the variable to test for underflowing
@ -3240,9 +3236,10 @@ static void CalcRaildirsDrawstyle(int x, int y, int method)
TileIndex t0 = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
TileIndex t1 = TileVirtXY(x, y);
uint distance = DistanceManhattan(t0, t1) + 1;
uint8_t index = 0;
if (distance != 1) {
if (distance == 1) {
HideMeasurementTooltips();
} else {
int heightdiff = CalcHeightdiff(b, distance, t0, t1);
/* If we are showing a tooltip for horizontal or vertical drags,
* 2 tiles have a length of 1. To bias towards the ceiling we add
@ -3251,11 +3248,12 @@ static void CalcRaildirsDrawstyle(int x, int y, int method)
distance = CeilDiv(distance, 2);
}
SetDParam(index++, distance);
if (heightdiff != 0) SetDParam(index++, heightdiff);
if (heightdiff == 0) {
ShowMeasurementTooltips(GetEncodedString(STR_MEASURE_LENGTH, distance));
} else {
ShowMeasurementTooltips(GetEncodedString(STR_MEASURE_LENGTH_HEIGHTDIFF, distance, heightdiff));
}
}
ShowMeasurementTooltips(measure_strings_length[index], index);
}
_thd.selend.x = x;
@ -3336,9 +3334,10 @@ calc_heightdiff_single_direction:;
TileIndex t0 = TileVirtXY(sx, sy);
TileIndex t1 = TileVirtXY(x, y);
uint distance = DistanceManhattan(t0, t1) + 1;
uint8_t index = 0;
if (distance != 1) {
if (distance == 1) {
HideMeasurementTooltips();
} else {
/* With current code passing a HT_LINE style to calculate the height
* difference is enough. However if/when a point-tool is created
* with this method, function should be called with new_style (below)
@ -3346,11 +3345,12 @@ calc_heightdiff_single_direction:;
* new_style := (_thd.next_drawstyle & HT_RECT) ? HT_LINE | style : _thd.next_drawstyle; */
int heightdiff = CalcHeightdiff(HT_LINE | style, 0, t0, t1);
SetDParam(index++, distance);
if (heightdiff != 0) SetDParam(index++, heightdiff);
if (heightdiff == 0) {
ShowMeasurementTooltips(GetEncodedString(STR_MEASURE_LENGTH, distance));
} else {
ShowMeasurementTooltips(GetEncodedString(STR_MEASURE_LENGTH_HEIGHTDIFF, distance, heightdiff));
}
}
ShowMeasurementTooltips(measure_strings_length[index], index);
}
break;
@ -3362,15 +3362,10 @@ calc_heightdiff_single_direction:;
case VPM_X_AND_Y: // drag an X by Y area
if (_settings_client.gui.measure_tooltip) {
static const StringID measure_strings_area[] = {
STR_NULL, STR_NULL, STR_MEASURE_AREA, STR_MEASURE_AREA_HEIGHTDIFF
};
TileIndex t0 = TileVirtXY(sx, sy);
TileIndex t1 = TileVirtXY(x, y);
uint dx = Delta(TileX(t0), TileX(t1)) + 1;
uint dy = Delta(TileY(t0), TileY(t1)) + 1;
uint8_t index = 0;
/* If dragging an area (eg dynamite tool) and it is actually a single
* row/column, change the type to 'line' to get proper calculation for height */
@ -3415,12 +3410,15 @@ calc_heightdiff_single_direction:;
if (dx != 1 || dy != 1) {
int heightdiff = CalcHeightdiff(style, 0, t0, t1);
SetDParam(index++, dx - (style & HT_POINT ? 1 : 0));
SetDParam(index++, dy - (style & HT_POINT ? 1 : 0));
if (heightdiff != 0) SetDParam(index++, heightdiff);
}
dx -= (style & HT_POINT ? 1 : 0);
dy -= (style & HT_POINT ? 1 : 0);
ShowMeasurementTooltips(measure_strings_area[index], index);
if (heightdiff == 0) {
ShowMeasurementTooltips(GetEncodedString(STR_MEASURE_AREA, dx, dy));
} else {
ShowMeasurementTooltips(GetEncodedString(STR_MEASURE_AREA_HEIGHTDIFF, dx, dy, heightdiff));
}
}
}
break;

View File

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

View File

@ -1008,7 +1008,7 @@ Twindow *AllocateWindowDescFront(WindowDesc &desc, WindowNumber window_number, T
void RelocateAllWindows(int neww, int newh);
void GuiShowTooltips(Window *parent, StringID str, TooltipCloseCondition close_tooltip, uint paramcount = 0);
void GuiShowTooltips(Window *parent, EncodedString &&text, TooltipCloseCondition close_tooltip);
/* widget.cpp */
WidgetID GetWidgetFromPos(const Window *w, int x, int y);