Changeset - r28595:15a15f0a567b
[Not reviewed]
master
0 3 0
frosch - 3 months ago 2024-01-27 20:07:07
frosch@openttd.org
Codechange: Move settings string formatting into IntSettingDesc members.
3 files changed with 54 insertions and 41 deletions:
0 comments (0 inline, 0 general)
src/settings.cpp
Show inline comments
 
@@ -448,6 +448,47 @@ size_t BoolSettingDesc::ParseValue(const
 
}
 

	
 
/**
 
 * Get the title of the setting.
 
 * The string should include a {STRING2} to show the current value.
 
 * @return The title string.
 
 */
 
StringID IntSettingDesc::GetTitle() const
 
{
 
	return this->str;
 
}
 

	
 
/**
 
 * Get the help text of the setting.
 
 * @return The requested help text.
 
 */
 
StringID IntSettingDesc::GetHelp() const
 
{
 
	return this->str_help;
 
}
 

	
 
/**
 
 * Set the DParams for drawing the value of the setting.
 
 * @param first_param First DParam to use
 
 * @param value Setting value to set params for.
 
 */
 
void IntSettingDesc::SetValueDParams(uint first_param, int32_t value) const
 
{
 
	if (this->IsBoolSetting()) {
 
		SetDParam(first_param++, value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
 
	} else {
 
		if ((this->flags & SF_GUI_DROPDOWN) != 0) {
 
			SetDParam(first_param++, this->str_val - this->min + value);
 
		} else if ((this->flags & SF_GUI_NEGATIVE_IS_SPECIAL) != 0) {
 
			SetDParam(first_param++, this->str_val + ((value >= 0) ? 1 : 0));
 
			value = abs(value);
 
		} else {
 
			SetDParam(first_param++, this->str_val + ((value == 0 && (this->flags & SF_GUI_0_IS_SPECIAL) != 0) ? 1 : 0));
 
		}
 
		SetDParam(first_param++, value);
 
	}
 
}
 

	
 
/**
 
 * Make the value valid and then write it to the setting.
 
 * See #MakeValidValid and #Write for more details.
 
 * @param object The object the setting is to be saved in.
src/settings_gui.cpp
Show inline comments
 
@@ -1275,17 +1275,6 @@ struct SettingEntry : BaseSettingEntry {
 

	
 
	void SetButtons(byte new_val);
 

	
 
	/**
 
	 * Get the help text of a single setting.
 
	 * @return The requested help text.
 
	 */
 
	inline StringID GetHelpText() const
 
	{
 
		return this->setting->str_help;
 
	}
 

	
 
	void SetValueDParams(uint first_param, int32_t value) const;
 

	
 
protected:
 
	void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const override;
 

	
 
@@ -1499,7 +1488,7 @@ uint SettingEntry::Length() const
 
 */
 
uint SettingEntry::GetMaxHelpHeight(int maxw)
 
{
 
	return GetStringHeight(this->GetHelpText(), maxw);
 
	return GetStringHeight(this->setting->GetHelp(), maxw);
 
}
 

	
 
/**
 
@@ -1560,8 +1549,8 @@ bool SettingEntry::UpdateFilterState(Set
 
		filter.string.ResetState();
 

	
 
		SetDParam(0, STR_EMPTY);
 
		filter.string.AddLine(sd->str);
 
		filter.string.AddLine(this->GetHelpText());
 
		filter.string.AddLine(sd->GetTitle());
 
		filter.string.AddLine(sd->GetHelp());
 

	
 
		visible = filter.string.GetState();
 
	}
 
@@ -1593,28 +1582,6 @@ static const void *ResolveObject(const G
 
}
 

	
 
/**
 
 * Set the DParams for drawing the value of a setting.
 
 * @param first_param First DParam to use
 
 * @param value Setting value to set params for.
 
 */
 
void SettingEntry::SetValueDParams(uint first_param, int32_t value) const
 
{
 
	if (this->setting->IsBoolSetting()) {
 
		SetDParam(first_param++, value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
 
	} else {
 
		if ((this->setting->flags & SF_GUI_DROPDOWN) != 0) {
 
			SetDParam(first_param++, this->setting->str_val - this->setting->min + value);
 
		} else if ((this->setting->flags & SF_GUI_NEGATIVE_IS_SPECIAL) != 0) {
 
			SetDParam(first_param++, this->setting->str_val + ((value >= 0) ? 1 : 0));
 
			value = abs(value);
 
		} else {
 
			SetDParam(first_param++, this->setting->str_val + ((value == 0 && (this->setting->flags & SF_GUI_0_IS_SPECIAL) != 0) ? 1 : 0));
 
		}
 
		SetDParam(first_param++, value);
 
	}
 
}
 

	
 
/**
 
 * Function to draw setting value (button + text + current value)
 
 * @param settings_ptr Pointer to current values of all settings
 
 * @param left         Left-most position in window/panel to start drawing
 
@@ -1649,8 +1616,8 @@ void SettingEntry::DrawSetting(GameSetti
 
		DrawArrowButtons(buttons_left, button_y, COLOUR_YELLOW, state,
 
				editable && value != (sd->flags & SF_GUI_0_IS_SPECIAL ? 0 : sd->min), editable && (uint32_t)value != sd->max);
 
	}
 
	this->SetValueDParams(1, value);
 
	DrawString(text_left, text_right, y + (SETTING_HEIGHT - GetCharacterHeight(FS_NORMAL)) / 2, sd->str, highlight ? TC_WHITE : TC_LIGHT_BLUE);
 
	sd->SetValueDParams(1, value);
 
	DrawString(text_left, text_right, y + (SETTING_HEIGHT - GetCharacterHeight(FS_NORMAL)) / 2, sd->GetTitle(), highlight ? TC_WHITE : TC_LIGHT_BLUE);
 
}
 

	
 
/* == SettingsContainer methods == */
 
@@ -2524,11 +2491,11 @@ struct GameSettingsWindow : Window {
 
					DrawString(tr, STR_CONFIG_SETTING_TYPE);
 
					tr.top += GetCharacterHeight(FS_NORMAL);
 

	
 
					this->last_clicked->SetValueDParams(0, sd->def);
 
					sd->SetValueDParams(0, sd->def);
 
					DrawString(tr, STR_CONFIG_SETTING_DEFAULT_VALUE);
 
					tr.top += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal;
 

	
 
					DrawStringMultiLine(tr, this->last_clicked->GetHelpText(), TC_WHITE);
 
					DrawStringMultiLine(tr, sd->GetHelp(), TC_WHITE);
 
				}
 
				break;
 

	
 
@@ -2655,7 +2622,8 @@ struct GameSettingsWindow : Window {
 

	
 
					DropDownList list;
 
					for (int i = sd->min; i <= (int)sd->max; i++) {
 
						list.push_back(std::make_unique<DropDownListStringItem>(sd->str_val + i - sd->min, i, false));
 
						sd->SetValueDParams(0, i);
 
						list.push_back(std::make_unique<DropDownListStringItem>(STR_JUST_STRING2, i, false));
 
					}
 

	
 
					ShowDropDownListAt(this, std::move(list), value, WID_GS_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE);
src/settings_internal.h
Show inline comments
 
@@ -209,6 +209,10 @@ struct IntSettingDesc : SettingDesc {
 
	PreChangeCheck *pre_check;         ///< Callback to check for the validity of the setting.
 
	PostChangeCallback *post_callback; ///< Callback when the setting has been changed.
 

	
 
	StringID GetTitle() const;
 
	StringID GetHelp() const;
 
	void SetValueDParams(uint first_param, int32_t value) const;
 

	
 
	/**
 
	 * Check whether this setting is a boolean type setting.
 
	 * @return True when the underlying type is an integer.
0 comments (0 inline, 0 general)