diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 05837cad16..341a796a1c 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -127,9 +127,9 @@ void GRFConfig::SetParameterDefaults() if (!this->has_param_defaults) return; - for (uint i = 0; i < this->param_info.size(); i++) { - if (!this->param_info[i]) continue; - this->param_info[i]->SetValue(this, this->param_info[i]->def_value); + for (const auto &info : this->param_info) { + if (!info.has_value()) continue; + this->SetValue(info.value(), info->def_value); } } @@ -176,49 +176,35 @@ GRFError::GRFError(StringID severity, StringID message) : message(message), seve } /** - * Create a new empty GRFParameterInfo object. - * @param nr The newgrf parameter that is changed. - */ -GRFParameterInfo::GRFParameterInfo(uint nr) : - name(), - desc(), - type(PTYPE_UINT_ENUM), - min_value(0), - max_value(UINT32_MAX), - def_value(0), - param_nr(nr), - first_bit(0), - num_bit(32), - value_names(), - complete_labels(false) -{} - -/** - * Get the value of this user-changeable parameter from the given config. - * @param config The GRFConfig to get the value from. + * Get the value of the given user-changeable parameter. + * @param info The grf parameter info to get the value for. * @return The value of this parameter. */ -uint32_t GRFParameterInfo::GetValue(struct GRFConfig *config) const +uint32_t GRFConfig::GetValue(const GRFParameterInfo &info) const { /* GB doesn't work correctly with nbits == 32, so handle that case here. */ - if (this->num_bit == 32) return config->param[this->param_nr]; - return GB(config->param[this->param_nr], this->first_bit, this->num_bit); + if (info.num_bit == 32) return this->param[info.param_nr]; + + return GB(this->param[info.param_nr], info.first_bit, info.num_bit); } /** - * Set the value of this user-changeable parameter in the given config. - * @param config The GRFConfig to set the value in. + * Set the value of the given user-changeable parameter. + * @param info The grf parameter info to set the value for. * @param value The new value. */ -void GRFParameterInfo::SetValue(struct GRFConfig *config, uint32_t value) +void GRFConfig::SetValue(const GRFParameterInfo &info, uint32_t value) { + value = Clamp(value, info.min_value, info.max_value); + /* SB doesn't work correctly with nbits == 32, so handle that case here. */ - if (this->num_bit == 32) { - config->param[this->param_nr] = value; + if (info.num_bit == 32) { + this->param[info.param_nr] = value; } else { - SB(config->param[this->param_nr], this->first_bit, this->num_bit, value); + SB(this->param[info.param_nr], info.first_bit, info.num_bit, value); } - config->num_params = std::max(config->num_params, this->param_nr + 1); + + this->num_params = std::max(this->num_params, info.param_nr + 1); SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE); } diff --git a/src/newgrf_config.h b/src/newgrf_config.h index aad86f7d33..a5e41ff5dc 100644 --- a/src/newgrf_config.h +++ b/src/newgrf_config.h @@ -116,7 +116,7 @@ struct GRFError { }; /** The possible types of a newgrf parameter. */ -enum GRFParameterType { +enum GRFParameterType : uint8_t { PTYPE_UINT_ENUM, ///< The parameter allows a range of numbers, each of which can have a special name PTYPE_BOOL, ///< The parameter is either 0 or 1 PTYPE_END, ///< Invalid parameter type @@ -124,21 +124,29 @@ enum GRFParameterType { /** Information about one grf parameter. */ struct GRFParameterInfo { - GRFParameterInfo(uint nr); - GRFTextList name; ///< The name of this parameter - GRFTextList desc; ///< The description of this parameter - GRFParameterType type; ///< The type of this parameter - uint32_t min_value; ///< The minimal value this parameter can have - uint32_t max_value; ///< The maximal value of this parameter - uint32_t def_value; ///< Default value of this parameter - uint8_t param_nr; ///< GRF parameter to store content in - uint8_t first_bit; ///< First bit to use in the GRF parameter - uint8_t num_bit; ///< Number of bits to use for this parameter - std::map value_names; ///< Names for each value. - bool complete_labels; ///< True if all values have a label. + /** + * Create a new empty GRFParameterInfo object. + * @param nr The newgrf parameter that is changed. + */ + explicit GRFParameterInfo(uint nr) : param_nr(nr) {} + + GRFTextList name = {}; ///< The name of this parameter + GRFTextList desc = {}; ///< The description of this parameter + + uint32_t min_value = 0; ///< The minimal value this parameter can have + uint32_t max_value = UINT32_MAX; ///< The maximal value of this parameter + uint32_t def_value = 0; ///< Default value of this parameter + + GRFParameterType type = PTYPE_UINT_ENUM; ///< The type of this parameter + + uint8_t param_nr; ///< GRF parameter to store content in + uint8_t first_bit = 0; ///< First bit to use in the GRF parameter + uint8_t num_bit = 32; ///< Number of bits to use for this parameter + + bool complete_labels = false; ///< True if all values have a label. + + std::map value_names = {}; ///< Names for each value. - uint32_t GetValue(struct GRFConfig *config) const; - void SetValue(struct GRFConfig *config, uint32_t value); void Finalize(); }; @@ -176,6 +184,9 @@ struct GRFConfig : ZeroedMemoryAllocator { void SetParams(const std::vector &pars); void CopyParams(const GRFConfig &src); + uint32_t GetValue(const GRFParameterInfo &info) const; + void SetValue(const GRFParameterInfo &info, uint32_t value); + std::optional GetTextfile(TextfileType type) const; const char *GetName() const; const char *GetDescription() const; diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index a4e73f626a..53f286aa50 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -284,12 +284,12 @@ struct NewGRFParametersWindow : public Window { int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2; for (int32_t i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) { GRFParameterInfo &par_info = this->GetParameterInfo(i); - uint32_t current_value = par_info.GetValue(this->grf_config); + uint32_t current_value = this->grf_config->GetValue(par_info); bool selected = (i == this->clicked_row); if (par_info.type == PTYPE_BOOL) { DrawBoolButton(buttons_left, ir.top + button_y_offset, current_value != 0, this->editable); - SetDParam(2, par_info.GetValue(this->grf_config) == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON); + SetDParam(2, this->grf_config->GetValue(par_info) == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON); } else if (par_info.type == PTYPE_UINT_ENUM) { if (par_info.complete_labels) { DrawDropDownButton(buttons_left, ir.top + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && this->clicked_dropdown, this->editable); @@ -371,7 +371,7 @@ struct NewGRFParametersWindow : public Window { GRFParameterInfo &par_info = this->GetParameterInfo(num); /* One of the arrows is clicked */ - uint32_t old_val = par_info.GetValue(this->grf_config); + uint32_t old_val = this->grf_config->GetValue(par_info); if (par_info.type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info.complete_labels) { if (this->clicked_dropdown) { /* unclick the dropdown */ @@ -416,7 +416,7 @@ struct NewGRFParametersWindow : public Window { } } if (val != old_val) { - par_info.SetValue(this->grf_config, val); + this->grf_config->SetValue(par_info, val); this->clicked_button = num; this->unclick_timeout.Reset(); @@ -448,8 +448,7 @@ struct NewGRFParametersWindow : public Window { if (!str.has_value() || str->empty()) return; int32_t value = atoi(str->c_str()); GRFParameterInfo &par_info = this->GetParameterInfo(this->clicked_row); - uint32_t val = Clamp(value, par_info.min_value, par_info.max_value); - par_info.SetValue(this->grf_config, val); + this->grf_config->SetValue(par_info, value); this->SetDirty(); } @@ -458,7 +457,7 @@ struct NewGRFParametersWindow : public Window { if (widget != WID_NP_SETTING_DROPDOWN) return; assert(this->clicked_dropdown); GRFParameterInfo &par_info = this->GetParameterInfo(this->clicked_row); - par_info.SetValue(this->grf_config, index); + this->grf_config->SetValue(par_info, index); this->SetDirty(); }