Changeset - r19399:5c03bff73a09
[Not reviewed]
master
0 1 0
frosch - 12 years ago 2012-06-01 15:19:38
frosch@openttd.org
(svn r24316) -Feature: Allow setting adv. settings with limited range using a dropdown list.
1 file changed with 84 insertions and 3 deletions:
0 comments (0 inline, 0 general)
src/settings_gui.cpp
Show inline comments
 
@@ -1324,24 +1324,27 @@ void SettingEntry::DrawSetting(GameSetti
 
	uint button_y = y + (SETTING_HEIGHT - SETTING_BUTTON_HEIGHT) / 2;
 

	
 
	/* We do not allow changes of some items when we are a client in a networkgame */
 
	if (!(sd->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !_network_server && !(sdb->flags & SGF_PER_COMPANY)) editable = false;
 
	if ((sdb->flags & SGF_NETWORK_ONLY) && !_networking) editable = false;
 
	if ((sdb->flags & SGF_NO_NETWORK) && _networking) editable = false;
 

	
 
	SetDParam(0, highlight ? STR_ORANGE_STRING1_WHITE : STR_ORANGE_STRING1_LTBLUE);
 
	int32 value = (int32)ReadValue(var, sd->save.conv);
 
	if (sdb->cmd == SDT_BOOLX) {
 
		/* Draw checkbox for boolean-value either on/off */
 
		DrawBoolButton(buttons_left, button_y, value != 0, editable);
 
	} else if ((sdb->flags & SGF_MULTISTRING) != 0) {
 
		/* Draw [v] button for settings of an enum-type */
 
		DrawDropDownButton(buttons_left, button_y, COLOUR_YELLOW, state != 0, editable);
 
	} else {
 
		/* Draw [<][>] boxes for settings of an integer-type */
 
		DrawArrowButtons(buttons_left, button_y, COLOUR_YELLOW, state,
 
				editable && value != (sdb->flags & SGF_0ISDISABLED ? 0 : sdb->min), editable && (uint32)value != sdb->max);
 
	}
 
	this->SetValueDParams(1, value);
 
	DrawString(text_left, text_right, y, sdb->str, highlight ? TC_WHITE : TC_LIGHT_BLUE);
 
}
 

	
 

	
 
/* == SettingsPage methods == */
 

	
 
@@ -1690,44 +1693,48 @@ static SettingsPage _settings_main_page 
 

	
 
struct GameSettingsWindow : Window {
 
	static const int SETTINGTREE_LEFT_OFFSET   = 5; ///< Position of left edge of setting values
 
	static const int SETTINGTREE_RIGHT_OFFSET  = 5; ///< Position of right edge of setting values
 
	static const int SETTINGTREE_TOP_OFFSET    = 5; ///< Position of top edge of setting values
 
	static const int SETTINGTREE_BOTTOM_OFFSET = 5; ///< Position of bottom edge of setting values
 

	
 
	static GameSettings *settings_ptr; ///< Pointer to the game settings being displayed and modified.
 

	
 
	SettingEntry *valuewindow_entry;   ///< If non-NULL, pointer to setting for which a value-entering window has been opened.
 
	SettingEntry *clicked_entry;       ///< If non-NULL, pointer to a clicked numeric setting (with a depressed left or right button).
 
	SettingEntry *last_clicked;        ///< If non-NULL, pointer to the last clicked setting.
 
	SettingEntry *valuedropdown_entry; ///< If non-NULL, pointer to the value for which a dropdown window is currently opened.
 
	bool closing_dropdown;             ///< True, if the dropdown list is currently closing.
 

	
 
	Scrollbar *vscroll;
 

	
 
	GameSettingsWindow(const WindowDesc *desc) : Window()
 
	{
 
		static bool first_time = true;
 

	
 
		settings_ptr = &GetGameSettings();
 

	
 
		/* Build up the dynamic settings-array only once per OpenTTD session */
 
		if (first_time) {
 
			_settings_main_page.Init();
 
			first_time = false;
 
		} else {
 
			_settings_main_page.FoldAll(); // Close all sub-pages
 
		}
 

	
 
		this->valuewindow_entry = NULL; // No setting entry for which a entry window is opened
 
		this->clicked_entry = NULL; // No numeric setting buttons are depressed
 
		this->last_clicked = NULL;
 
		this->valuedropdown_entry = NULL;
 
		this->closing_dropdown = false;
 

	
 
		this->CreateNestedTree(desc);
 
		this->vscroll = this->GetScrollbar(WID_GS_SCROLLBAR);
 
		this->FinishInitNested(desc, WN_GAME_OPTIONS_GAME_SETTINGS);
 

	
 
		this->vscroll->SetCount(_settings_main_page.Length());
 
	}
 

	
 
	virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
 
	{
 
		switch (widget) {
 
			case WID_GS_OPTIONSPANEL:
 
@@ -1738,24 +1745,35 @@ struct GameSettingsWindow : Window {
 
				break;
 

	
 
			case WID_GS_HELP_TEXT:
 
				size->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL +
 
						max(size->height, _settings_main_page.GetMaxHelpHeight(size->width));
 
				break;
 

	
 
			default:
 
				break;
 
		}
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		if (this->closing_dropdown) {
 
			this->closing_dropdown = false;
 
			assert(this->valuedropdown_entry != NULL);
 
			this->valuedropdown_entry->SetButtons(0);
 
			this->valuedropdown_entry = NULL;
 
		}
 
		this->DrawWidgets();
 
	}
 

	
 
	virtual void DrawWidget(const Rect &r, int widget) const
 
	{
 
		switch (widget) {
 
			case WID_GS_OPTIONSPANEL:
 
				_settings_main_page.Draw(settings_ptr, r.left + SETTINGTREE_LEFT_OFFSET, r.right - SETTINGTREE_RIGHT_OFFSET, r.top + SETTINGTREE_TOP_OFFSET,
 
						this->vscroll->GetPosition(), this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->last_clicked);
 
				break;
 

	
 
			case WID_GS_HELP_TEXT:
 
				if (this->last_clicked != NULL) {
 
					const SettingDesc *sd = this->last_clicked->d.entry.setting;
 
					int32 default_value = ReadValue(&sd->desc.def, sd->save.conv);
 
@@ -1812,26 +1830,63 @@ struct GameSettingsWindow : Window {
 
		const SettingDesc *sd = pe->d.entry.setting;
 

	
 
		/* return if action is only active in network, or only settable by server */
 
		if ((!(sd->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !_network_server && !(sd->desc.flags & SGF_PER_COMPANY)) ||
 
				((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking) || ((sd->desc.flags & SGF_NO_NETWORK) && _networking)) {
 
			this->SetDisplayedHelpText(pe);
 
			return;
 
		}
 

	
 
		const void *var = ResolveVariableAddress(settings_ptr, sd);
 
		int32 value = (int32)ReadValue(var, sd->save.conv);
 

	
 
		/* clicked on the icon on the left side. Either scroller or bool on/off */
 
		if (x < SETTING_BUTTON_WIDTH) {
 
		/* clicked on the icon on the left side. Either scroller, bool on/off or dropdown */
 
		if (x < SETTING_BUTTON_WIDTH && (sd->desc.flags & SGF_MULTISTRING)) {
 
			const SettingDescBase *sdb = &sd->desc;
 
			this->SetDisplayedHelpText(pe);
 

	
 
			if (this->valuedropdown_entry == pe) {
 
				/* unclick the dropdown */
 
				HideDropDownMenu(this);
 
				this->closing_dropdown = false;
 
				this->valuedropdown_entry->SetButtons(0);
 
				this->valuedropdown_entry = NULL;
 
			} else {
 
				if (this->valuedropdown_entry != NULL) this->valuedropdown_entry->SetButtons(0);
 
				this->closing_dropdown = false;
 

	
 
				const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_GS_OPTIONSPANEL);
 
				int rel_y = (pt.y - (int)wid->pos_y - SETTINGTREE_TOP_OFFSET) % wid->resize_y;
 

	
 
				Rect wi_rect;
 
				wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);
 
				wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
 
				wi_rect.top = pt.y - rel_y + (SETTING_HEIGHT - SETTING_BUTTON_HEIGHT) / 2;
 
				wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
 

	
 
				/* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
 
				if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
 
					this->valuedropdown_entry = pe;
 
					this->valuedropdown_entry->SetButtons(SEF_LEFT_DEPRESSED);
 

	
 
					DropDownList *list = new DropDownList();
 
					for (int i = sdb->min; i <= (int)sdb->max; i++) {
 
						list->push_back(new DropDownListStringItem(sdb->str_val + i - sdb->min, i, false));
 
					}
 

	
 
					ShowDropDownListAt(this, list, value, -1, wi_rect, COLOUR_ORANGE, true);
 
				}
 
			}
 
			this->SetDirty();
 
		} else if (x < SETTING_BUTTON_WIDTH) {
 
			this->SetDisplayedHelpText(pe);
 
			const SettingDescBase *sdb = &sd->desc;
 
			int32 oldvalue = value;
 

	
 
			switch (sdb->cmd) {
 
				case SDT_BOOLX: value ^= 1; break;
 
				case SDT_ONEOFMANY:
 
				case SDT_NUMX: {
 
					/* Add a dynamic step-size to the scroller. In a maximum of
 
					 * 50-steps you should be able to get from min to max,
 
					 * unless specified otherwise in the 'interval' variable
 
					 * of the current setting. */
 
@@ -1851,25 +1906,25 @@ struct GameSettingsWindow : Window {
 
							assert((int32)sdb->max >= 0);
 
							if (value > (int32)sdb->max) value = (int32)sdb->max;
 
						} else {
 
							if ((uint32)value > sdb->max) value = (int32)sdb->max;
 
						}
 
						if (value < sdb->min) value = sdb->min; // skip between "disabled" and minimum
 
					} else {
 
						value -= step;
 
						if (value < sdb->min) value = (sdb->flags & SGF_0ISDISABLED) ? 0 : sdb->min;
 
					}
 

	
 
					/* Set up scroller timeout for numeric values */
 
					if (value != oldvalue && !(sd->desc.flags & SGF_MULTISTRING)) {
 
					if (value != oldvalue) {
 
						if (this->clicked_entry != NULL) { // Release previous buttons if any
 
							this->clicked_entry->SetButtons(0);
 
						}
 
						this->clicked_entry = pe;
 
						this->clicked_entry->SetButtons((x >= SETTING_BUTTON_WIDTH / 2) != (_current_text_dir == TD_RTL) ? SEF_RIGHT_DEPRESSED : SEF_LEFT_DEPRESSED);
 
						this->SetTimeout();
 
						_left_button_clicked = false;
 
					}
 
					break;
 
				}
 

	
 
				default: NOT_REACHED();
 
@@ -1924,24 +1979,50 @@ struct GameSettingsWindow : Window {
 
		} else {
 
			value = (int32)(size_t)sd->desc.def;
 
		}
 

	
 
		if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
 
			SetCompanySetting(this->valuewindow_entry->d.entry.index, value);
 
		} else {
 
			SetSettingValue(this->valuewindow_entry->d.entry.index, value);
 
		}
 
		this->SetDirty();
 
	}
 

	
 
	virtual void OnDropdownSelect(int widget, int index)
 
	{
 
		assert(this->valuedropdown_entry != NULL);
 
		const SettingDesc *sd = this->valuedropdown_entry->d.entry.setting;
 
		assert(sd->desc.flags & SGF_MULTISTRING);
 

	
 
		if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
 
			SetCompanySetting(this->valuedropdown_entry->d.entry.index, index);
 
		} else {
 
			SetSettingValue(this->valuedropdown_entry->d.entry.index, index);
 
		}
 

	
 
		this->SetDirty();
 
	}
 

	
 
	virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
 
	{
 
		/* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
 
		 * the same dropdown button was clicked again, and then not open the dropdown again.
 
		 * So, we only remember that it was closed, and process it on the next OnPaint, which is
 
		 * after OnClick. */
 
		assert(this->valuedropdown_entry != NULL);
 
		this->closing_dropdown = true;
 
		this->SetDirty();
 
	}
 

	
 
	virtual void OnResize()
 
	{
 
		this->vscroll->SetCapacityFromWidget(this, WID_GS_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET);
 
	}
 
};
 

	
 
GameSettings *GameSettingsWindow::settings_ptr = NULL;
 

	
 
static const NWidgetPart _nested_settings_selection_widgets[] = {
 
	NWidget(NWID_HORIZONTAL),
 
		NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
 
		NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_CONFIG_SETTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
0 comments (0 inline, 0 general)