Add: Toggle for showing industry names in small map

This commit is contained in:
Edmond Chui 2024-06-10 00:29:31 +01:00 committed by rubidium42
parent a1e94ccddb
commit ff3be45c23
3 changed files with 86 additions and 11 deletions

View File

@ -802,9 +802,11 @@ STR_SMALLMAP_TOWN :{TINY_FONT}{WHI
STR_SMALLMAP_DISABLE_ALL :{BLACK}Disable all
STR_SMALLMAP_ENABLE_ALL :{BLACK}Enable all
STR_SMALLMAP_SHOW_HEIGHT :{BLACK}Show height
STR_SMALLMAP_SHOW_INDUSTRY_NAMES :{BLACK}Show industry names
STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES :{BLACK}Display no industries on the map
STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES :{BLACK}Display all industries on the map
STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT :{BLACK}Toggle display of heightmap
STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRY_NAMES :{BLACK}Toggle display of industry names
STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES :{BLACK}Display no company property on the map
STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES :{BLACK}Display all company property on the map
STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS :{BLACK}Display no cargoes on the map

View File

@ -172,6 +172,8 @@ static LegendAndColour _legend_linkstats[NUM_CARGO + lengthof(_linkstat_colours_
static LegendAndColour _legend_from_industries[NUM_INDUSTRYTYPES + 1];
/** For connecting industry type to position in industries list(small map legend) */
static uint _industry_to_list_pos[NUM_INDUSTRYTYPES];
/** The string bounding box width for each industry type in the smallmap */
static uint16_t _industry_to_name_string_width[NUM_INDUSTRYTYPES];
/** Show heightmap in industry and owner mode of smallmap window. */
static bool _smallmap_show_heightmap = false;
/** Highlight a specific industry type */
@ -635,6 +637,7 @@ protected:
static SmallMapType map_type; ///< Currently displayed legends.
static bool show_towns; ///< Display town names in the smallmap.
static bool show_ind_names; ///< Display industry names in the smallmap.
static int map_height_limit; ///< Currently used/cached map height limit.
static const uint INDUSTRY_MIN_NUMBER_OF_COLUMNS = 2; ///< Minimal number of columns in the #WID_SM_LEGEND widget for the #SMT_INDUSTRY legend.
@ -829,7 +832,7 @@ protected:
if (map_type == SMT_LINKSTATS) this->overlay->SetDirty();
if (map_type != SMT_INDUSTRY) this->BreakIndustryChainLink();
this->SetDirty();
this->ReInit();
}
/**
@ -984,13 +987,13 @@ protected:
* Adds town names to the smallmap.
* @param dpi the part of the smallmap to be drawn into
*/
void DrawTowns(const DrawPixelInfo *dpi) const
void DrawTowns(const DrawPixelInfo *dpi, const int vertical_padding) const
{
for (const Town *t : Town::Iterate()) {
/* Remap the town coordinate */
Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);
int y = pt.y;
int y = pt.y + vertical_padding;
/* Check if the town sign is within bounds */
if (x + t->cache.sign.width_small > dpi->left &&
@ -1004,6 +1007,45 @@ protected:
}
}
/**
* Adds industry names to the smallmap.
* @param dpi the part of the smallmap to be drawn into
*/
void DrawIndustryNames(const DrawPixelInfo *dpi, const int vertical_padding) const
{
if (this->map_type != SMT_INDUSTRY) return;
for (const Industry *i : Industry::Iterate()) {
const LegendAndColour &tbl = _legend_from_industries[_industry_to_list_pos[i->type]];
if (!tbl.show_on_map) continue;
/* Industry names blink together with their blobs in the smallmap. */
const bool is_blinking = i->type == _smallmap_industry_highlight && !_smallmap_industry_highlight_state;
if (is_blinking) continue;
if (_industry_to_name_string_width[i->type] == 0) {
_industry_to_name_string_width[i->type] = GetStringBoundingBox(tbl.legend, FS_SMALL).width;
}
const uint16_t &legend_text_width = _industry_to_name_string_width[i->type];
/* Remap the industry coordinate */
const TileIndex &tile = i->location.GetCenterTile();
const Point pt = this->RemapTile(TileX(tile), TileY(tile));
const int x = pt.x - this->subscroll - (legend_text_width / 2);
const int y = pt.y + vertical_padding;
/* Check if the industry name is within bounds */
if (x + legend_text_width > dpi->left &&
x < dpi->left + dpi->width &&
y + GetCharacterHeight(FS_SMALL) > dpi->top &&
y < dpi->top + dpi->height) {
/* And draw it. */
DrawString(x, x + legend_text_width, y, tbl.legend, TC_WHITE, SA_LEFT, false, FS_SMALL);
}
}
}
/**
* Draws the small map.
*
@ -1064,8 +1106,13 @@ protected:
/* Draw link stat overlay */
if (this->map_type == SMT_LINKSTATS) this->overlay->Draw(dpi);
const int map_labels_vertical_padding = ScaleGUITrad(2);
/* Draw town names */
if (this->show_towns) this->DrawTowns(dpi);
if (this->show_towns) this->DrawTowns(dpi, map_labels_vertical_padding);
/* Draw industry names */
if (this->show_ind_names) this->DrawIndustryNames(dpi, map_labels_vertical_padding);
/* Draw map indicators */
this->DrawMapIndicators();
@ -1225,41 +1272,47 @@ protected:
StringID legend_tooltip;
StringID enable_all_tooltip;
StringID disable_all_tooltip;
int plane;
int industry_names_select_plane;
int select_buttons_plane;
switch (this->map_type) {
case SMT_INDUSTRY:
legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
plane = 0;
industry_names_select_plane = 0;
select_buttons_plane = 0;
break;
case SMT_OWNER:
legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
plane = 0;
industry_names_select_plane = SZSP_NONE;
select_buttons_plane = 0;
break;
case SMT_LINKSTATS:
legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
plane = 0;
industry_names_select_plane = SZSP_NONE;
select_buttons_plane = 0;
break;
default:
legend_tooltip = STR_NULL;
enable_all_tooltip = STR_NULL;
disable_all_tooltip = STR_NULL;
plane = 1;
industry_names_select_plane = SZSP_NONE;
select_buttons_plane = 1;
break;
}
this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetDataTip(STR_NULL, legend_tooltip);
this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetDataTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetDataTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
this->GetWidget<NWidgetStacked>(WID_SM_SHOW_IND_NAMES_SEL)->SetDisplayedPlane(industry_names_select_plane);
this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(select_buttons_plane);
}
/**
@ -1412,6 +1465,7 @@ public:
this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
this->SetWidgetLoweredState(WID_SM_SHOW_IND_NAMES, this->show_ind_names);
this->SetupWidgetData();
@ -1520,6 +1574,9 @@ public:
/* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
this->column_width = min_width + WidgetDimensions::scaled.hsep_normal + this->legend_width + WidgetDimensions::scaled.framerect.Horizontal();
/* Cached string widths of industry names in the smallmap. Calculation is deferred to DrawIndustryNames(). */
std::fill(std::begin(_industry_to_name_string_width), std::end(_industry_to_name_string_width), 0);
}
void OnPaint() override
@ -1685,6 +1742,14 @@ public:
if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
break;
case WID_SM_SHOW_IND_NAMES: // Toggle industry names
this->show_ind_names = !this->show_ind_names;
this->SetWidgetLoweredState(WID_SM_SHOW_IND_NAMES, this->show_ind_names);
this->SetDirty();
if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
break;
case WID_SM_LEGEND: // Legend
if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_LINKSTATS || this->map_type == SMT_OWNER) {
int click_pos = this->GetPositionOnLegend(pt);
@ -1833,6 +1898,7 @@ public:
SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
bool SmallMapWindow::show_towns = true;
bool SmallMapWindow::show_ind_names = false;
int SmallMapWindow::map_height_limit = -1;
/**
@ -1955,7 +2021,6 @@ static std::unique_ptr<NWidgetBase> SmallMapDisplay()
return map_display;
}
static constexpr NWidgetPart _nested_smallmap_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
@ -1972,6 +2037,12 @@ static constexpr NWidgetPart _nested_smallmap_widgets[] = {
NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_ENABLE_ALL), SetDataTip(STR_SMALLMAP_ENABLE_ALL, STR_NULL),
NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_DISABLE_ALL), SetDataTip(STR_SMALLMAP_DISABLE_ALL, STR_NULL),
NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_SM_SHOW_HEIGHT), SetDataTip(STR_SMALLMAP_SHOW_HEIGHT, STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT),
/* 'show industry names' button and container. Only shown for the industry map type. */
NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SM_SHOW_IND_NAMES_SEL),
NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_SM_SHOW_IND_NAMES), SetDataTip(STR_SMALLMAP_SHOW_INDUSTRY_NAMES, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRY_NAMES),
EndContainer(),
NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
EndContainer(),
EndContainer(),

View File

@ -32,6 +32,8 @@ enum SmallMapWidgets : WidgetID {
WID_SM_ENABLE_ALL, ///< Button to enable display of all legend entries.
WID_SM_DISABLE_ALL, ///< Button to disable display of all legend entries.
WID_SM_SHOW_HEIGHT, ///< Show heightmap toggle button.
WID_SM_SHOW_IND_NAMES, ///< Show industry names toggle button.
WID_SM_SHOW_IND_NAMES_SEL, ///< Container for the 'show industry names' button, which can be hidden.
};
#endif /* WIDGETS_SMALLMAP_WIDGET_H */