Codechange: Tidy up GRFParameterInfo. (#13114)

Use member-initialisation, reorder members to reduce space, and prefer references.

SetValue/GetValue are moved to GRFConfig as they set the config's parameter values.
This commit is contained in:
Peter Nelson 2024-11-23 12:13:52 +00:00 committed by GitHub
parent b84a164590
commit 00ae20fa02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 55 deletions

View File

@ -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<uint>(config->num_params, this->param_nr + 1);
this->num_params = std::max<uint>(this->num_params, info.param_nr + 1);
SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
}

View File

@ -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<uint32_t, GRFTextList> 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<uint32_t, GRFTextList> 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<uint32_t> &pars);
void CopyParams(const GRFConfig &src);
uint32_t GetValue(const GRFParameterInfo &info) const;
void SetValue(const GRFParameterInfo &info, uint32_t value);
std::optional<std::string> GetTextfile(TextfileType type) const;
const char *GetName() const;
const char *GetDescription() const;

View File

@ -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<uint32_t>(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();
}