mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-09 15:41:15 +00:00
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:
parent
b84a164590
commit
00ae20fa02
@ -127,9 +127,9 @@ void GRFConfig::SetParameterDefaults()
|
|||||||
|
|
||||||
if (!this->has_param_defaults) return;
|
if (!this->has_param_defaults) return;
|
||||||
|
|
||||||
for (uint i = 0; i < this->param_info.size(); i++) {
|
for (const auto &info : this->param_info) {
|
||||||
if (!this->param_info[i]) continue;
|
if (!info.has_value()) continue;
|
||||||
this->param_info[i]->SetValue(this, this->param_info[i]->def_value);
|
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.
|
* Get the value of the given user-changeable parameter.
|
||||||
* @param nr The newgrf parameter that is changed.
|
* @param info The grf parameter info to get the value for.
|
||||||
*/
|
|
||||||
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.
|
|
||||||
* @return The value of this parameter.
|
* @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. */
|
/* GB doesn't work correctly with nbits == 32, so handle that case here. */
|
||||||
if (this->num_bit == 32) return config->param[this->param_nr];
|
if (info.num_bit == 32) return this->param[info.param_nr];
|
||||||
return GB(config->param[this->param_nr], this->first_bit, this->num_bit);
|
|
||||||
|
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.
|
* Set the value of the given user-changeable parameter.
|
||||||
* @param config The GRFConfig to set the value in.
|
* @param info The grf parameter info to set the value for.
|
||||||
* @param value The new value.
|
* @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. */
|
/* SB doesn't work correctly with nbits == 32, so handle that case here. */
|
||||||
if (this->num_bit == 32) {
|
if (info.num_bit == 32) {
|
||||||
config->param[this->param_nr] = value;
|
this->param[info.param_nr] = value;
|
||||||
} else {
|
} 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);
|
SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +116,7 @@ struct GRFError {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/** The possible types of a newgrf parameter. */
|
/** 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_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_BOOL, ///< The parameter is either 0 or 1
|
||||||
PTYPE_END, ///< Invalid parameter type
|
PTYPE_END, ///< Invalid parameter type
|
||||||
@ -124,21 +124,29 @@ enum GRFParameterType {
|
|||||||
|
|
||||||
/** Information about one grf parameter. */
|
/** Information about one grf parameter. */
|
||||||
struct GRFParameterInfo {
|
struct GRFParameterInfo {
|
||||||
GRFParameterInfo(uint nr);
|
/**
|
||||||
GRFTextList name; ///< The name of this parameter
|
* Create a new empty GRFParameterInfo object.
|
||||||
GRFTextList desc; ///< The description of this parameter
|
* @param nr The newgrf parameter that is changed.
|
||||||
GRFParameterType type; ///< The type of this parameter
|
*/
|
||||||
uint32_t min_value; ///< The minimal value this parameter can have
|
explicit GRFParameterInfo(uint nr) : param_nr(nr) {}
|
||||||
uint32_t max_value; ///< The maximal value of this parameter
|
|
||||||
uint32_t def_value; ///< Default value of this parameter
|
GRFTextList name = {}; ///< The name of this parameter
|
||||||
uint8_t param_nr; ///< GRF parameter to store content in
|
GRFTextList desc = {}; ///< The description of this parameter
|
||||||
uint8_t first_bit; ///< First bit to use in the GRF parameter
|
|
||||||
uint8_t num_bit; ///< Number of bits to use for this parameter
|
uint32_t min_value = 0; ///< The minimal value this parameter can have
|
||||||
std::map<uint32_t, GRFTextList> value_names; ///< Names for each value.
|
uint32_t max_value = UINT32_MAX; ///< The maximal value of this parameter
|
||||||
bool complete_labels; ///< True if all values have a label.
|
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();
|
void Finalize();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -176,6 +184,9 @@ struct GRFConfig : ZeroedMemoryAllocator {
|
|||||||
void SetParams(const std::vector<uint32_t> &pars);
|
void SetParams(const std::vector<uint32_t> &pars);
|
||||||
void CopyParams(const GRFConfig &src);
|
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;
|
std::optional<std::string> GetTextfile(TextfileType type) const;
|
||||||
const char *GetName() const;
|
const char *GetName() const;
|
||||||
const char *GetDescription() const;
|
const char *GetDescription() const;
|
||||||
|
@ -284,12 +284,12 @@ struct NewGRFParametersWindow : public Window {
|
|||||||
int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2;
|
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++) {
|
for (int32_t i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
|
||||||
GRFParameterInfo &par_info = this->GetParameterInfo(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);
|
bool selected = (i == this->clicked_row);
|
||||||
|
|
||||||
if (par_info.type == PTYPE_BOOL) {
|
if (par_info.type == PTYPE_BOOL) {
|
||||||
DrawBoolButton(buttons_left, ir.top + button_y_offset, current_value != 0, this->editable);
|
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) {
|
} else if (par_info.type == PTYPE_UINT_ENUM) {
|
||||||
if (par_info.complete_labels) {
|
if (par_info.complete_labels) {
|
||||||
DrawDropDownButton(buttons_left, ir.top + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && this->clicked_dropdown, this->editable);
|
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);
|
GRFParameterInfo &par_info = this->GetParameterInfo(num);
|
||||||
|
|
||||||
/* One of the arrows is clicked */
|
/* 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 (par_info.type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info.complete_labels) {
|
||||||
if (this->clicked_dropdown) {
|
if (this->clicked_dropdown) {
|
||||||
/* unclick the dropdown */
|
/* unclick the dropdown */
|
||||||
@ -416,7 +416,7 @@ struct NewGRFParametersWindow : public Window {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (val != old_val) {
|
if (val != old_val) {
|
||||||
par_info.SetValue(this->grf_config, val);
|
this->grf_config->SetValue(par_info, val);
|
||||||
|
|
||||||
this->clicked_button = num;
|
this->clicked_button = num;
|
||||||
this->unclick_timeout.Reset();
|
this->unclick_timeout.Reset();
|
||||||
@ -448,8 +448,7 @@ struct NewGRFParametersWindow : public Window {
|
|||||||
if (!str.has_value() || str->empty()) return;
|
if (!str.has_value() || str->empty()) return;
|
||||||
int32_t value = atoi(str->c_str());
|
int32_t value = atoi(str->c_str());
|
||||||
GRFParameterInfo &par_info = this->GetParameterInfo(this->clicked_row);
|
GRFParameterInfo &par_info = this->GetParameterInfo(this->clicked_row);
|
||||||
uint32_t val = Clamp<uint32_t>(value, par_info.min_value, par_info.max_value);
|
this->grf_config->SetValue(par_info, value);
|
||||||
par_info.SetValue(this->grf_config, val);
|
|
||||||
this->SetDirty();
|
this->SetDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -458,7 +457,7 @@ struct NewGRFParametersWindow : public Window {
|
|||||||
if (widget != WID_NP_SETTING_DROPDOWN) return;
|
if (widget != WID_NP_SETTING_DROPDOWN) return;
|
||||||
assert(this->clicked_dropdown);
|
assert(this->clicked_dropdown);
|
||||||
GRFParameterInfo &par_info = this->GetParameterInfo(this->clicked_row);
|
GRFParameterInfo &par_info = this->GetParameterInfo(this->clicked_row);
|
||||||
par_info.SetValue(this->grf_config, index);
|
this->grf_config->SetValue(par_info, index);
|
||||||
this->SetDirty();
|
this->SetDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user