1
0
Fork 0

Codechange: Return name, description and URL as std::string from GRFConfig. (#13868)

pull/13870/head
frosch 2025-03-22 20:30:40 +01:00 committed by GitHub
parent ae2d7947d7
commit 667d013726
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 26 additions and 22 deletions

View File

@ -262,8 +262,8 @@ public:
} }
/* NewGRF name */ /* NewGRF name */
if (td.grf != nullptr) { if (td.grf.has_value()) {
this->landinfo_data.push_back(GetString(STR_LAND_AREA_INFORMATION_NEWGRF_NAME, td.grf)); this->landinfo_data.push_back(GetString(STR_LAND_AREA_INFORMATION_NEWGRF_NAME, std::move(*td.grf)));
} }
/* Cargo acceptance is displayed in a extra multiline */ /* Cargo acceptance is displayed in a extra multiline */

View File

@ -82,28 +82,32 @@ void GRFConfig::CopyParams(const GRFConfig &src)
* the filename is returned. * the filename is returned.
* @return The name of filename of this grf. * @return The name of filename of this grf.
*/ */
const char *GRFConfig::GetName() const std::string GRFConfig::GetName() const
{ {
const char *name = GetGRFStringFromGRFText(this->name); const char *name = GetGRFStringFromGRFText(this->name);
return StrEmpty(name) ? this->filename.c_str() : name; return StrEmpty(name) ? this->filename : name;
} }
/** /**
* Get the grf info. * Get the grf info.
* @return A string with a description of this grf. * @return A string with a description of this grf.
*/ */
const char *GRFConfig::GetDescription() const std::optional<std::string> GRFConfig::GetDescription() const
{ {
return GetGRFStringFromGRFText(this->info); const char *str = GetGRFStringFromGRFText(this->info);
if (StrEmpty(str)) return std::nullopt;
return std::string(str);
} }
/** /**
* Get the grf url. * Get the grf url.
* @return A string with an url of this grf. * @return A string with an url of this grf.
*/ */
const char *GRFConfig::GetURL() const std::optional<std::string> GRFConfig::GetURL() const
{ {
return GetGRFStringFromGRFText(this->url); const char *str = GetGRFStringFromGRFText(this->url);
if (StrEmpty(str)) return std::nullopt;
return std::string(str);
} }
/** Set the default value for all parameters as specified by action14. */ /** Set the default value for all parameters as specified by action14. */

View File

@ -190,9 +190,9 @@ struct GRFConfig {
void SetValue(const GRFParameterInfo &info, uint32_t value); void SetValue(const GRFParameterInfo &info, uint32_t value);
std::optional<std::string> GetTextfile(TextfileType type) const; std::optional<std::string> GetTextfile(TextfileType type) const;
const char *GetName() const; std::string GetName() const;
const char *GetDescription() const; std::optional<std::string> GetDescription() const;
const char *GetURL() const; std::optional<std::string> GetURL() const;
void SetParameterDefaults(); void SetParameterDefaults();
void SetSuitablePalette(); void SetSuitablePalette();

View File

@ -128,8 +128,8 @@ static void ShowNewGRFInfo(const GRFConfig &c, const Rect &r, bool show_params)
if (c.flags.Test(GRFConfigFlag::Compatible)) tr.top = DrawStringMultiLine(tr, STR_NEWGRF_COMPATIBLE_LOADED); if (c.flags.Test(GRFConfigFlag::Compatible)) tr.top = DrawStringMultiLine(tr, STR_NEWGRF_COMPATIBLE_LOADED);
/* Draw GRF info if it exists */ /* Draw GRF info if it exists */
if (!StrEmpty(c.GetDescription())) { if (auto desc = c.GetDescription(); desc.has_value()) {
tr.top = DrawStringMultiLine(tr, GetString(STR_JUST_RAW_STRING, c.GetDescription()), TC_BLACK); tr.top = DrawStringMultiLine(tr, GetString(STR_JUST_RAW_STRING, std::move(*desc)), TC_BLACK);
} else { } else {
tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_NO_INFO); tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_NO_INFO);
} }
@ -854,7 +854,7 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
int i = 0; int i = 0;
for (const auto &c : this->actives) { for (const auto &c : this->actives) {
if (this->vscroll->IsVisible(i)) { if (this->vscroll->IsVisible(i)) {
const char *text = c->GetName(); std::string text = c->GetName();
bool h = (this->active_sel == c.get()); bool h = (this->active_sel == c.get());
PaletteID pal = this->GetPalette(*c); PaletteID pal = this->GetPalette(*c);
@ -871,7 +871,7 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
DrawSprite(SPR_SQUARE, pal, square_left, tr.top + square_offset_y); DrawSprite(SPR_SQUARE, pal, square_left, tr.top + square_offset_y);
if (c->error.has_value()) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, tr.top + warning_offset_y); if (c->error.has_value()) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, tr.top + warning_offset_y);
uint txtoffset = !c->error.has_value() ? 0 : warning.width; uint txtoffset = !c->error.has_value() ? 0 : warning.width;
DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), tr.top + offset_y, text, h ? TC_WHITE : TC_ORANGE); DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), tr.top + offset_y, std::move(text), h ? TC_WHITE : TC_ORANGE);
tr.top += step_height; tr.top += step_height;
} }
i++; i++;
@ -894,10 +894,10 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
for (auto it = first; it != last; ++it) { for (auto it = first; it != last; ++it) {
const GRFConfig *c = *it; const GRFConfig *c = *it;
bool h = (c == this->avail_sel); bool h = (c == this->avail_sel);
const char *text = c->GetName(); std::string text = c->GetName();
if (h) GfxFillRect(br.left, tr.top, br.right, tr.top + step_height - 1, PC_DARK_BLUE); if (h) GfxFillRect(br.left, tr.top, br.right, tr.top + step_height - 1, PC_DARK_BLUE);
DrawString(tr.left, tr.right, tr.top + offset_y, text, h ? TC_WHITE : TC_SILVER); DrawString(tr.left, tr.right, tr.top + offset_y, std::move(text), h ? TC_WHITE : TC_SILVER);
tr.top += step_height; tr.top += step_height;
} }
break; break;
@ -948,8 +948,8 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
case WID_NS_OPEN_URL: { case WID_NS_OPEN_URL: {
const GRFConfig *c = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel; const GRFConfig *c = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel;
auto url = c->GetURL();
OpenBrowser(c->GetURL()); if (url.has_value()) OpenBrowser(std::move(*url));
break; break;
} }
@ -1263,7 +1263,7 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
for (TextfileType tft = TFT_CONTENT_BEGIN; tft < TFT_CONTENT_END; tft++) { for (TextfileType tft = TFT_CONTENT_BEGIN; tft < TFT_CONTENT_END; tft++) {
this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, selected_config == nullptr || !selected_config->GetTextfile(tft).has_value()); this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, selected_config == nullptr || !selected_config->GetTextfile(tft).has_value());
} }
this->SetWidgetDisabledState(WID_NS_OPEN_URL, selected_config == nullptr || StrEmpty(selected_config->GetURL())); this->SetWidgetDisabledState(WID_NS_OPEN_URL, selected_config == nullptr || !selected_config->GetURL().has_value());
this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0); this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0);
this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0); this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0);
@ -1413,7 +1413,7 @@ private:
filter.ResetState(); filter.ResetState();
filter.AddLine((*a)->GetName()); filter.AddLine((*a)->GetName());
filter.AddLine((*a)->filename); filter.AddLine((*a)->filename);
filter.AddLine((*a)->GetDescription()); if (auto desc = (*a)->GetDescription(); desc.has_value()) filter.AddLine(*desc);
return filter.GetState();; return filter.GetState();;
} }

View File

@ -60,7 +60,7 @@ struct TileDesc {
StringID airport_class{}; ///< Name of the airport class StringID airport_class{}; ///< Name of the airport class
StringID airport_name{}; ///< Name of the airport StringID airport_name{}; ///< Name of the airport
StringID airport_tile_name{}; ///< Name of the airport tile StringID airport_tile_name{}; ///< Name of the airport tile
const char *grf = nullptr; ///< newGRF used for the tile contents std::optional<std::string> grf = std::nullopt; ///< newGRF used for the tile contents
StringID railtype{}; ///< Type of rail on the tile. StringID railtype{}; ///< Type of rail on the tile.
uint16_t rail_speed = 0; ///< Speed limit of rail (bridges and track) uint16_t rail_speed = 0; ///< Speed limit of rail (bridges and track)
StringID roadtype{}; ///< Type of road on the tile. StringID roadtype{}; ///< Type of road on the tile.