File diff r16430:b3f65f9bc976 → r16431:ec558deca9d7
src/settings_gui.cpp
Show inline comments
 
@@ -1001,177 +1001,177 @@ SettingEntry *SettingEntry::FindEntry(ui
 

	
 
	switch (this->flags & SEF_KIND_MASK) {
 
		case SEF_SETTING_KIND:
 
			(*cur_row)++;
 
			break;
 
		case SEF_SUBTREE_KIND:
 
			(*cur_row)++; // add one for row containing the title
 
			if (this->d.sub.folded) {
 
				break;
 
			}
 

	
 
			/* sub-page is visible => search it too */
 
			return this->d.sub.page->FindEntry(row_num, cur_row);
 
		default: NOT_REACHED();
 
	}
 
	return NULL;
 
}
 

	
 
/**
 
 * Draw a row in the settings panel.
 
 *
 
 * See SettingsPage::Draw() for an explanation about how drawing is performed.
 
 *
 
 * The \a parent_last parameter ensures that the vertical lines at the left are
 
 * only drawn when another entry follows, that it prevents output like
 
 * \verbatim
 
 *  |-- setting
 
 *  |-- (-) - Title
 
 *  |    |-- setting
 
 *  |    |-- setting
 
 * \endverbatim
 
 * The left-most vertical line is not wanted. It is prevented by setting the
 
 * appropiate bit in the \a parent_last parameter.
 
 *
 
 * @param settings_ptr Pointer to current values of all settings
 
 * @param left         Left-most position in window/panel to start drawing \a first_row
 
 * @param right        Right-most x position to draw strings at.
 
 * @param base_y       Upper-most position in window/panel to start drawing \a first_row
 
 * @param first_row    First row number to draw
 
 * @param max_row      Row-number to stop drawing (the row-number of the row below the last row to draw)
 
 * @param cur_row      Current row number (internal variable)
 
 * @param parent_last  Last-field booleans of parent page level (page level \e i sets bit \e i to 1 if it is its last field)
 
 * @return Row number of the next row to draw
 
 */
 
uint SettingEntry::Draw(GameSettings *settings_ptr, int left, int right, int base_y, uint first_row, uint max_row, uint cur_row, uint parent_last)
 
{
 
	if (cur_row >= max_row) return cur_row;
 

	
 
	bool rtl = _dynlang.text_dir == TD_RTL;
 
	bool rtl = _current_text_dir == TD_RTL;
 
	int offset = rtl ? -4 : 4;
 
	int level_width = rtl ? -LEVEL_WIDTH : LEVEL_WIDTH;
 

	
 
	int x = rtl ? right : left;
 
	int y = base_y;
 
	if (cur_row >= first_row) {
 
		int colour = _colour_gradient[COLOUR_ORANGE][4];
 
		y = base_y + (cur_row - first_row) * SETTING_HEIGHT; // Compute correct y start position
 

	
 
		/* Draw vertical for parent nesting levels */
 
		for (uint lvl = 0; lvl < this->level; lvl++) {
 
			if (!HasBit(parent_last, lvl)) GfxDrawLine(x + offset, y, x + offset, y + SETTING_HEIGHT - 1, colour);
 
			x += level_width;
 
		}
 
		/* draw own |- prefix */
 
		int halfway_y = y + SETTING_HEIGHT / 2;
 
		int bottom_y = (flags & SEF_LAST_FIELD) ? halfway_y : y + SETTING_HEIGHT - 1;
 
		GfxDrawLine(x + offset, y, x + offset, bottom_y, colour);
 
		/* Small horizontal line from the last vertical line */
 
		GfxDrawLine(x + offset, halfway_y, x + level_width - offset, halfway_y, colour);
 
		x += level_width;
 
	}
 

	
 
	switch (this->flags & SEF_KIND_MASK) {
 
		case SEF_SETTING_KIND:
 
			if (cur_row >= first_row) {
 
				DrawSetting(settings_ptr, this->d.entry.setting, rtl ? left : x, rtl ? x : right, y, this->flags & SEF_BUTTONS_MASK);
 
			}
 
			cur_row++;
 
			break;
 
		case SEF_SUBTREE_KIND:
 
			if (cur_row >= first_row) {
 
				DrawSprite((this->d.sub.folded ? SPR_CIRCLE_FOLDED : SPR_CIRCLE_UNFOLDED), PAL_NONE, rtl ? x - 8 : x, y + (SETTING_HEIGHT - 11) / 2);
 
				DrawString(rtl ? left : x + 12, rtl ? x - 12 : right, y, this->d.sub.title);
 
			}
 
			cur_row++;
 
			if (!this->d.sub.folded) {
 
				if (this->flags & SEF_LAST_FIELD) {
 
					assert(this->level < sizeof(parent_last));
 
					SetBit(parent_last, this->level); // Add own last-field state
 
				}
 

	
 
				cur_row = this->d.sub.page->Draw(settings_ptr, left, right, base_y, first_row, max_row, cur_row, parent_last);
 
			}
 
			break;
 
		default: NOT_REACHED();
 
	}
 
	return cur_row;
 
}
 

	
 
static const void *ResolveVariableAddress(const GameSettings *settings_ptr, const SettingDesc *sd)
 
{
 
	if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
 
		if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
 
			return GetVariableAddress(&Company::Get(_local_company)->settings, &sd->save);
 
		} else {
 
			return GetVariableAddress(&_settings_client.company, &sd->save);
 
		}
 
	} else {
 
		return GetVariableAddress(settings_ptr, &sd->save);
 
	}
 
}
 

	
 
/**
 
 * Private function to draw setting value (button + text + current value)
 
 * @param settings_ptr Pointer to current values of all settings
 
 * @param sd           Pointer to value description of setting to draw
 
 * @param left         Left-most position in window/panel to start drawing
 
 * @param right        Right-most position in window/panel to draw
 
 * @param y            Upper-most position in window/panel to start drawing
 
 * @param state        State of the left + right arrow buttons to draw for the setting
 
 */
 
void SettingEntry::DrawSetting(GameSettings *settings_ptr, const SettingDesc *sd, int left, int right, int y, int state)
 
{
 
	const SettingDescBase *sdb = &sd->desc;
 
	const void *var = ResolveVariableAddress(settings_ptr, sd);
 
	bool editable = true;
 
	bool disabled = false;
 

	
 
	bool rtl = _dynlang.text_dir == TD_RTL;
 
	bool rtl = _current_text_dir == TD_RTL;
 
	uint buttons_left = rtl ? right - 19 : left;
 
	uint text_left  = left + (rtl ? 0 : 25);
 
	uint text_right = right - (rtl ? 25 : 0);
 
	uint button_y = y + (SETTING_HEIGHT - 11) / 2;
 

	
 
	/* We do not allow changes of some items when we are a client in a networkgame */
 
	if (!(sd->save.conv & SLF_NETWORK_NO) && _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;
 

	
 
	if (sdb->cmd == SDT_BOOLX) {
 
		static const Colours _bool_ctabs[2][2] = {{COLOUR_CREAM, COLOUR_RED}, {COLOUR_DARK_GREEN, COLOUR_GREEN}};
 
		/* Draw checkbox for boolean-value either on/off */
 
		bool on = (*(bool*)var);
 

	
 
		DrawFrameRect(buttons_left, button_y, buttons_left + 19, button_y + 8, _bool_ctabs[!!on][!!editable], on ? FR_LOWERED : FR_NONE);
 
		SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
 
	} else {
 
		int32 value;
 

	
 
		value = (int32)ReadValue(var, sd->save.conv);
 

	
 
		/* 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);
 

	
 
		disabled = (value == 0) && (sdb->flags & SGF_0ISDISABLED);
 
		if (disabled) {
 
			SetDParam(0, STR_CONFIG_SETTING_DISABLED);
 
		} else {
 
			if (sdb->flags & SGF_CURRENCY) {
 
				SetDParam(0, STR_JUST_CURRENCY);
 
			} else if (sdb->flags & SGF_MULTISTRING) {
 
				SetDParam(0, sdb->str - sdb->min + value + 1);
 
			} else {
 
				SetDParam(0, (sdb->flags & SGF_NOCOMMA) ? STR_JUST_INT : STR_JUST_COMMA);
 
			}
 
			SetDParam(1, value);
 
		}
 
	}
 
	DrawString(text_left, text_right, y, (sdb->str) + disabled);
 
}
 

	
 

	
 
/* == SettingsPage methods == */
 

	
 
/**
 
 * Initialization of an entire setting page
 
 * @param level Nesting level of this page (internal variable, do not provide a value for it when calling)
 
@@ -1521,162 +1521,162 @@ struct GameSettingsWindow : Window {
 
			_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->CreateNestedTree(desc);
 
		this->vscroll = this->GetScrollbar(SETTINGSEL_SCROLLBAR);
 
		this->FinishInitNested(desc, 0);
 

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

	
 
	virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
 
	{
 
		if (widget != SETTINGSEL_OPTIONSPANEL) return;
 

	
 
		resize->height = SETTING_HEIGHT = max(11, FONT_HEIGHT_NORMAL + 1);
 
		resize->width  = 1;
 

	
 
		size->height = 5 * resize->height + SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET;
 
	}
 

	
 
	virtual void DrawWidget(const Rect &r, int widget) const
 
	{
 
		if (widget != SETTINGSEL_OPTIONSPANEL) return;
 

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

	
 
	virtual void OnPaint()
 
	{
 
		this->DrawWidgets();
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget, int click_count)
 
	{
 
		if (widget != SETTINGSEL_OPTIONSPANEL) return;
 

	
 
		uint btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, SETTINGSEL_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET - 1);
 
		if (btn == INT_MAX) return;
 

	
 
		uint cur_row = 0;
 
		SettingEntry *pe = _settings_main_page.FindEntry(btn, &cur_row);
 

	
 
		if (pe == NULL) return;  // Clicked below the last setting of the page
 

	
 
		int x = (_dynlang.text_dir == TD_RTL ? this->width - pt.x : pt.x) - SETTINGTREE_LEFT_OFFSET - (pe->level + 1) * LEVEL_WIDTH;  // Shift x coordinate
 
		int x = (_current_text_dir == TD_RTL ? this->width - pt.x : pt.x) - SETTINGTREE_LEFT_OFFSET - (pe->level + 1) * LEVEL_WIDTH;  // Shift x coordinate
 
		if (x < 0) return;  // Clicked left of the entry
 

	
 
		if ((pe->flags & SEF_KIND_MASK) == SEF_SUBTREE_KIND) {
 
			pe->d.sub.folded = !pe->d.sub.folded; // Flip 'folded'-ness of the sub-page
 

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

	
 
		assert((pe->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
 
		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_NETWORK_NO) && _networking && !_network_server && !(sd->desc.flags & SGF_PER_COMPANY)) return;
 
		if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking) return;
 
		if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) 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 < 21) {
 
			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. */
 
					uint32 step = (sdb->interval == 0) ? ((sdb->max - sdb->min) / 50) : sdb->interval;
 
					if (step == 0) step = 1;
 

	
 
					/* don't allow too fast scrolling */
 
					if ((this->flags4 & WF_TIMEOUT_MASK) > WF_TIMEOUT_TRIGGER) {
 
						_left_button_clicked = false;
 
						return;
 
					}
 

	
 
					/* Increase or decrease the value and clamp it to extremes */
 
					if (x >= 10) {
 
						value += step;
 
						if (sdb->min < 0) {
 
							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 (this->clicked_entry != NULL) { // Release previous buttons if any
 
							this->clicked_entry->SetButtons(0);
 
						}
 
						this->clicked_entry = pe;
 
						this->clicked_entry->SetButtons((x >= 10) != (_dynlang.text_dir == TD_RTL) ? SEF_RIGHT_DEPRESSED : SEF_LEFT_DEPRESSED);
 
						this->clicked_entry->SetButtons((x >= 10) != (_current_text_dir == TD_RTL) ? SEF_RIGHT_DEPRESSED : SEF_LEFT_DEPRESSED);
 
						this->flags4 |= WF_TIMEOUT_BEGIN;
 
						_left_button_clicked = false;
 
					}
 
					break;
 
				}
 

	
 
				default: NOT_REACHED();
 
			}
 

	
 
			if (value != oldvalue) {
 
				if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
 
					SetCompanySetting(pe->d.entry.index, value);
 
				} else {
 
					SetSettingValue(pe->d.entry.index, value);
 
				}
 
				this->SetDirty();
 
			}
 
		} else {
 
			/* only open editbox for types that its sensible for */
 
			if (sd->desc.cmd != SDT_BOOLX && !(sd->desc.flags & SGF_MULTISTRING)) {
 
				/* Show the correct currency-translated value */
 
				if (sd->desc.flags & SGF_CURRENCY) value *= _currency->rate;
 

	
 
				this->valuewindow_entry = pe;
 
				SetDParam(0, value);
 
				ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, 100, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
 
			}
 
		}
 
	}
 

	
 
	virtual void OnTimeout()
 
	{
 
		if (this->clicked_entry != NULL) { // On timeout, release any depressed buttons
 
			this->clicked_entry->SetButtons(0);
 
			this->clicked_entry = NULL;
 
			this->SetDirty();
 
		}
 
	}
 

	
 
	virtual void OnQueryTextFinished(char *str)
 
	{
 
		/* The user pressed cancel */
 
		if (str == NULL) return;
 

	
 
		assert(this->valuewindow_entry != NULL);
 
		assert((this->valuewindow_entry->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
 
		const SettingDesc *sd = this->valuewindow_entry->d.entry.setting;
 

	
 
@@ -1708,97 +1708,97 @@ GameSettings *GameSettingsWindow::settin
 

	
 
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),
 
	EndContainer(),
 
	NWidget(NWID_HORIZONTAL),
 
		NWidget(WWT_PANEL, COLOUR_MAUVE, SETTINGSEL_OPTIONSPANEL), SetMinimalSize(400, 174), SetScrollbar(SETTINGSEL_SCROLLBAR), EndContainer(),
 
		NWidget(NWID_VERTICAL),
 
			NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, SETTINGSEL_SCROLLBAR),
 
			NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
 
		EndContainer(),
 
	EndContainer(),
 
};
 

	
 
static const WindowDesc _settings_selection_desc(
 
	WDP_CENTER, 450, 397,
 
	WC_GAME_OPTIONS, WC_NONE,
 
	0,
 
	_nested_settings_selection_widgets, lengthof(_nested_settings_selection_widgets)
 
);
 

	
 
void ShowGameSettings()
 
{
 
	DeleteWindowById(WC_GAME_OPTIONS, 0);
 
	new GameSettingsWindow(&_settings_selection_desc);
 
}
 

	
 

	
 
/**
 
 * Draw [<][>] boxes.
 
 * @param x the x position to draw
 
 * @param y the y position to draw
 
 * @param button_colour the colour of the button
 
 * @param state 0 = none clicked, 1 = first clicked, 2 = second clicked
 
 * @param clickable_left is the left button clickable?
 
 * @param clickable_right is the right button clickable?
 
 */
 
void DrawArrowButtons(int x, int y, Colours button_colour, byte state, bool clickable_left, bool clickable_right)
 
{
 
	int colour = _colour_gradient[button_colour][2];
 

	
 
	DrawFrameRect(x,      y + 1, x +  9, y + 9, button_colour, (state == 1) ? FR_LOWERED : FR_NONE);
 
	DrawFrameRect(x + 10, y + 1, x + 19, y + 9, button_colour, (state == 2) ? FR_LOWERED : FR_NONE);
 
	DrawSprite(SPR_ARROW_LEFT, PAL_NONE, x + WD_IMGBTN_LEFT, y + WD_IMGBTN_TOP);
 
	DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, x + WD_IMGBTN_LEFT + 10, y + WD_IMGBTN_TOP);
 

	
 
	/* Grey out the buttons that aren't clickable */
 
	bool rtl = _dynlang.text_dir == TD_RTL;
 
	bool rtl = _current_text_dir == TD_RTL;
 
	if (rtl ? !clickable_right : !clickable_left) {
 
		GfxFillRect(x +  1, y + 1, x +  1 + 8, y + 8, colour, FILLRECT_CHECKER);
 
	}
 
	if (rtl ? !clickable_left : !clickable_right) {
 
		GfxFillRect(x + 11, y + 1, x + 11 + 8, y + 8, colour, FILLRECT_CHECKER);
 
	}
 
}
 

	
 
/** Widget numbers of the custom currency window. */
 
enum CustomCurrencyWidgets {
 
	CUSTCURR_RATE_DOWN,
 
	CUSTCURR_RATE_UP,
 
	CUSTCURR_RATE,
 
	CUSTCURR_SEPARATOR_EDIT,
 
	CUSTCURR_SEPARATOR,
 
	CUSTCURR_PREFIX_EDIT,
 
	CUSTCURR_PREFIX,
 
	CUSTCURR_SUFFIX_EDIT,
 
	CUSTCURR_SUFFIX,
 
	CUSTCURR_YEAR_DOWN,
 
	CUSTCURR_YEAR_UP,
 
	CUSTCURR_YEAR,
 
	CUSTCURR_PREVIEW,
 
};
 

	
 
struct CustomCurrencyWindow : Window {
 
	int query_widget;
 

	
 
	CustomCurrencyWindow(const WindowDesc *desc) : Window()
 
	{
 
		this->InitNested(desc);
 

	
 
		SetButtonState();
 
	}
 

	
 
	void SetButtonState()
 
	{
 
		this->SetWidgetDisabledState(CUSTCURR_RATE_DOWN, _custom_currency.rate == 1);
 
		this->SetWidgetDisabledState(CUSTCURR_RATE_UP, _custom_currency.rate == UINT16_MAX);
 
		this->SetWidgetDisabledState(CUSTCURR_YEAR_DOWN, _custom_currency.to_euro == CF_NOEURO);
 
		this->SetWidgetDisabledState(CUSTCURR_YEAR_UP, _custom_currency.to_euro == MAX_YEAR);
 
	}
 

	
 
	virtual void SetStringParameters(int widget) const
 
	{
 
		switch (widget) {
 
			case CUSTCURR_RATE:      SetDParam(0, 1); SetDParam(1, 1);            break;
 
			case CUSTCURR_SEPARATOR: SetDParamStr(0, _custom_currency.separator); break;