Changeset - r10660:e9232b868967
[Not reviewed]
master
0 1 0
rubidium - 16 years ago 2009-01-10 16:39:18
rubidium@openttd.org
(svn r14962) -Codechange: add the concept of patch entries and patch (sub) pages
1 file changed with 100 insertions and 18 deletions:
0 comments (0 inline, 0 general)
src/settings_gui.cpp
Show inline comments
 
@@ -565,75 +565,161 @@ public:
 
			case GDW_CANCEL: // Cancel button - close window, abandon changes
 
				delete this;
 
				break;
 
		}
 
	}
 

	
 
	virtual void OnTick()
 
	{
 
		if (this->timeout != 0) {
 
			this->timeout--;
 
			if (this->timeout == 0) this->clicked_button = NO_SETTINGS_BUTTON;
 
			this->SetDirty();
 
		}
 
	}
 
};
 

	
 
void ShowGameDifficulty()
 
{
 
	DeleteWindowById(WC_GAME_OPTIONS, 0);
 
	new GameDifficultyWindow();
 
}
 

	
 
static const int SETTING_HEIGHT = 11;         ///< Height of a single patch setting in the tree view
 

	
 
/** Data structure describing a single patch in a tab */
 
struct PatchEntry {
 
/** Flags for #PatchEntry */
 
enum PatchEntryFlags {
 
	PEF_LAST_FIELD = 0x04, ///< This entry is the last one in a (sub-)page
 

	
 
	/* Entry kind */
 
	PEF_SETTING_KIND = 0x10, ///< Entry kind: Entry is a setting
 
	PEF_SUBTREE_KIND = 0x20, ///< Entry kind: Entry is a sub-tree
 
	PEF_KIND_MASK    = (PEF_SETTING_KIND | PEF_SUBTREE_KIND), ///< Bit-mask for fetching entry kind
 
};
 

	
 
struct PatchPage; // Forward declaration
 

	
 
/** Data fields for a sub-page (#PEF_SUBTREE_KIND kind)*/
 
struct PatchEntrySubtree {
 
	PatchPage *page; ///< Pointer to the sub-page
 
	bool folded;     ///< Sub-page is folded (not visible except for its title)
 
	StringID title;  ///< Title of the sub-page
 
};
 

	
 
/** Data fields for a single setting (#PEF_SETTING_KIND kind) */
 
struct PatchEntrySetting {
 
	const char *name;           ///< Name of the setting
 
	const SettingDesc *setting; ///< Setting description of the setting
 
	uint index;                 ///< Index of the setting in the settings table
 
};
 

	
 
/** Data structure describing a single patch in a tab */
 
struct PatchEntry {
 
	byte flags; ///< Flags of the patch entry. @see PatchEntryFlags
 
	byte level; ///< Nesting level of this patch entry
 
	union {
 
		PatchEntrySetting entry; ///< Data fields if entry is a setting
 
		PatchEntrySubtree sub;   ///< Data fields if entry is a sub-page
 
	} d; ///< Data fields for each kind
 

	
 
	PatchEntry(const char *nm);
 
	PatchEntry(PatchPage *sub, StringID title);
 

	
 
	void Init(byte level, bool last_field);
 
};
 

	
 
/** Data structure describing one page of patches in the patch settings window. */
 
struct PatchPage {
 
	PatchEntry *entries; ///< Array of patch entries of the page.
 
	byte num;            ///< Number of entries on the page (statically filled).
 

	
 
	void Init(byte level = 0);
 
};
 

	
 

	
 
/* == PatchEntry methods == */
 

	
 
/**
 
 * Constructor for a single setting in the 'advanced settings' window
 
 * @param nm Name of the setting in the setting table
 
 */
 
PatchEntry::PatchEntry(const char *nm)
 
{
 
	this->name = nm;
 
	this->setting = NULL;
 
	this->index = 0;
 
	this->flags = PEF_SETTING_KIND;
 
	this->level = 0;
 
	this->d.entry.name = nm;
 
	this->d.entry.setting = NULL;
 
	this->d.entry.index = 0;
 
}
 

	
 
/**
 
 * Constructor for a sub-page in the 'advanced settings' window
 
 * @param sub   Sub-page
 
 * @param title Title of the sub-page
 
 */
 
PatchEntry::PatchEntry(PatchPage *sub, StringID title)
 
{
 
	this->flags = PEF_SUBTREE_KIND;
 
	this->level = 0;
 
	this->d.sub.page = sub;
 
	this->d.sub.folded = true;
 
	this->d.sub.title = title;
 
}
 

	
 
/**
 
 * Initialization of a patch entry
 
 * @param level      Page nesting level of this entry
 
 * @param last_field Boolean indicating this entry is the last at the (sub-)page
 
 */
 
void PatchEntry::Init(byte level, bool last_field)
 
{
 
	this->level = level;
 
	if (last_field) this->flags |= PEF_LAST_FIELD;
 

	
 
	switch (this->flags & PEF_KIND_MASK) {
 
		case PEF_SETTING_KIND:
 
			this->d.entry.setting = GetPatchFromName(this->d.entry.name, &this->d.entry.index);
 
			assert(this->d.entry.setting != NULL);
 
			break;
 
		case PEF_SUBTREE_KIND:
 
			this->d.sub.page->Init(level + 1);
 
			break;
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 

	
 
/* == PatchPage 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)
 
 */
 
void PatchPage::Init(byte level)
 
{
 
	for (uint field = 0; field < this->num; field++) {
 
		this->entries[field].Init(level, field + 1 == num);
 
	}
 
}
 

	
 

	
 
static PatchEntry _patches_ui[] = {
 
	PatchEntry("gui.vehicle_speed"),
 
	PatchEntry("gui.status_long_date"),
 
	PatchEntry("gui.date_format_in_default_names"),
 
	PatchEntry("gui.show_finances"),
 
	PatchEntry("gui.autoscroll"),
 
	PatchEntry("gui.reverse_scroll"),
 
	PatchEntry("gui.smooth_scroll"),
 
	PatchEntry("gui.errmsg_duration"),
 
	PatchEntry("gui.toolbar_pos"),
 
	PatchEntry("gui.measure_tooltip"),
 
	PatchEntry("gui.window_snap_radius"),
 
	PatchEntry("gui.window_soft_limit"),
 
	PatchEntry("gui.population_in_label"),
 
	PatchEntry("gui.link_terraform_toolbar"),
 
	PatchEntry("gui.liveries"),
 
	PatchEntry("gui.prefer_teamchat"),
 
	/* While the horizontal scrollwheel scrolling is written as general code, only
 
	 *  the cocoa (OSX) driver generates input for it.
 
	 *  Since it's also able to completely disable the scrollwheel will we display it on all platforms anyway */
 
	PatchEntry("gui.scrollwheel_scrolling"),
 
@@ -774,86 +860,80 @@ struct PatchesSelectionWindow : Window {
 
	static const int SETTINGTREE_TOP_OFFSET;  ///< Position of top edge of patch values
 

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

	
 
	int page;
 
	int entry;
 
	int click;
 

	
 
	PatchesSelectionWindow(const WindowDesc *desc) : Window(desc)
 
	{
 
		/* Check that the widget doesn't get moved without adapting the constant as well.
 
		 *  - SETTINGTREE_LEFT_OFFSET should be 5 pixels to the right of the left edge of the panel
 
		 *  - SETTINGTREE_TOP_OFFSET should be 5 pixels below the top edge of the panel
 
		 */
 
		assert(this->widget[PATCHSEL_OPTIONSPANEL].left + 5 == SETTINGTREE_LEFT_OFFSET);
 
		assert(this->widget[PATCHSEL_OPTIONSPANEL].top + 5 == SETTINGTREE_TOP_OFFSET);
 

	
 
		static bool first_time = true;
 

	
 
		patches_ptr = (_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game;
 

	
 
		/* Build up the dynamic settings-array only once per OpenTTD session */
 
		if (first_time) {
 
			for (PatchPage *page = &_patches_page[0]; page != endof(_patches_page); page++) {
 
				for (uint i = 0; i != page->num; i++) {
 
					uint index;
 
					const SettingDesc *sd = GetPatchFromName(page->entries[i].name, &index);
 
					assert(sd != NULL);
 

	
 
					page->entries[i].setting = sd;
 
					page->entries[i].index = index;
 
				}
 
				page->Init();
 
			}
 
			first_time = false;
 
		}
 

	
 
		this->page = 0;
 
		this->vscroll.pos = 0;
 
		this->vscroll.cap = (this->widget[PATCHSEL_OPTIONSPANEL].bottom - this->widget[PATCHSEL_OPTIONSPANEL].top - 8) / SETTING_HEIGHT;
 
		SetVScrollCount(this, _patches_page[this->page].num);
 

	
 
		this->resize.step_height = SETTING_HEIGHT;
 
		this->resize.height = this->height;
 
		this->resize.step_width = 1;
 
		this->resize.width = this->width;
 

	
 
		this->LowerWidget(this->page + PATCHSEL_INTERFACE); // Depress button of currently selected page
 

	
 
		this->FindWindowPlacementAndResize(desc);
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		const PatchPage *page = &_patches_page[this->page];
 

	
 
		/* Set up selected category */
 
		this->DrawWidgets();
 

	
 
		int x = SETTINGTREE_LEFT_OFFSET;
 
		int y = SETTINGTREE_TOP_OFFSET;
 
		for (uint i = this->vscroll.pos; i != page->num && this->vscroll.pos + this->vscroll.cap - i > 0; i++) {
 
			const SettingDesc *sd = page->entries[i].setting;
 
			assert((page->entries[i].flags & PEF_KIND_MASK) == PEF_SETTING_KIND);
 
			const SettingDesc *sd = page->entries[i].d.entry.setting;
 
			DrawPatch(patches_ptr, sd, x, y, this->click - (i * 2));
 
			y += SETTING_HEIGHT;
 
		}
 
	}
 

	
 
	void DrawPatch(GameSettings *patches_ptr, const SettingDesc *sd, int x, int y, int state)
 
	{
 
		const SettingDescBase *sdb = &sd->desc;
 
		const void *var = GetVariableAddress(patches_ptr, &sd->save);
 
		bool editable = true;
 
		bool disabled = false;
 

	
 
		/* 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) 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 int _bool_ctabs[2][2] = {{9, 4}, {7, 6}};
 
			/* Draw checkbox for boolean-value either on/off */
 
			bool on = (*(bool*)var);
 

	
 
			DrawFrameRect(x, y, x + 19, y + 8, _bool_ctabs[!!on][!!editable], on ? FR_LOWERED : FR_NONE);
 
			SetDParam(0, on ? STR_CONFIG_PATCHES_ON : STR_CONFIG_PATCHES_OFF);
 
@@ -878,49 +958,50 @@ struct PatchesSelectionWindow : Window {
 
				}
 
				SetDParam(1, value);
 
			}
 
		}
 
		DrawString(x + 25, y, (sdb->str) + disabled, TC_FROMSTRING);
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget)
 
	{
 
		switch (widget) {
 
			case PATCHSEL_OPTIONSPANEL: {
 
				int y = pt.y - SETTINGTREE_TOP_OFFSET;  // Shift y coordinate
 
				if (y < 0) return;  // Clicked above first entry
 

	
 
				int x = pt.x - SETTINGTREE_LEFT_OFFSET;  // Shift x coordinate
 
				if (x < 0) return;  // Clicked left of the entry
 

	
 
				byte btn = this->vscroll.pos + y / SETTING_HEIGHT;  // Compute which setting is selected
 
				if (y % SETTING_HEIGHT > SETTING_HEIGHT - 2) return;  // Clicked too low at the setting
 

	
 
				const PatchPage *page = &_patches_page[this->page];
 

	
 
				if (btn >= page->num) return;  // Clicked below the last setting of the page
 

	
 
				const SettingDesc *sd = page->entries[btn].setting;
 
				assert((page->entries[btn].flags & PEF_KIND_MASK) == PEF_SETTING_KIND);
 
				const SettingDesc *sd = page->entries[btn].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) return;
 
				if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking) return;
 
				if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return;
 

	
 
				void *var = GetVariableAddress(patches_ptr, &sd->save);
 
				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 patch. */
 
						uint32 step = (sdb->interval == 0) ? ((sdb->max - sdb->min) / 50) : sdb->interval;
 
						if (step == 0) step = 1;
 
@@ -931,93 +1012,94 @@ struct PatchesSelectionWindow : Window {
 
							return;
 
						}
 

	
 
						/* Increase or decrease the value and clamp it to extremes */
 
						if (x >= 10) {
 
							value += step;
 
							if (value > sdb->max) value = 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)) {
 
							this->click = btn * 2 + 1 + ((x >= 10) ? 1 : 0);
 
							this->flags4 |= WF_TIMEOUT_BEGIN;
 
							_left_button_clicked = false;
 
						}
 
					} break;
 
					default: NOT_REACHED();
 
					}
 

	
 
					if (value != oldvalue) {
 
						SetPatchValue(page->entries[btn].index, value);
 
						SetPatchValue(page->entries[btn].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->entry = btn;
 
						SetDParam(0, value);
 
						ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_CONFIG_PATCHES_QUERY_CAPT, 10, 100, this, CS_NUMERAL, QSF_NONE);
 
					}
 
				}
 
			} break;
 

	
 
			case PATCHSEL_INTERFACE: case PATCHSEL_CONSTRUCTION: case PATCHSEL_VEHICLES:
 
			case PATCHSEL_STATIONS:  case PATCHSEL_ECONOMY:      case PATCHSEL_COMPETITORS:
 
				this->RaiseWidget(this->page + PATCHSEL_INTERFACE);
 
				this->page = widget - PATCHSEL_INTERFACE;
 
				this->LowerWidget(this->page + PATCHSEL_INTERFACE);
 
				SetVScrollCount(this, _patches_page[this->page].num);
 
				DeleteWindowById(WC_QUERY_STRING, 0);
 
				this->SetDirty();
 
				break;
 
		}
 
	}
 

	
 
	virtual void OnTimeout()
 
	{
 
		this->click = 0;
 
		this->SetDirty();
 
	}
 

	
 
	virtual void OnQueryTextFinished(char *str)
 
	{
 
		if (!StrEmpty(str)) {
 
			const PatchEntry *pe = &_patches_page[this->page].entries[this->entry];
 
			const SettingDesc *sd = pe->setting;
 
			assert((pe->flags & PEF_KIND_MASK) == PEF_SETTING_KIND);
 
			const SettingDesc *sd = pe->d.entry.setting;
 
			int32 value = atoi(str);
 

	
 
			/* Save the correct currency-translated value */
 
			if (sd->desc.flags & SGF_CURRENCY) value /= _currency->rate;
 

	
 
			SetPatchValue(pe->index, value);
 
			SetPatchValue(pe->d.entry.index, value);
 
			this->SetDirty();
 
		}
 
	}
 

	
 
	virtual void OnResize(Point new_size, Point delta)
 
	{
 
		this->vscroll.cap += delta.y / SETTING_HEIGHT;
 
		SetVScrollCount(this, _patches_page[this->page].num);
 
	}
 
};
 

	
 
GameSettings *PatchesSelectionWindow::patches_ptr = NULL;
 
const int PatchesSelectionWindow::SETTINGTREE_LEFT_OFFSET = 5;
 
const int PatchesSelectionWindow::SETTINGTREE_TOP_OFFSET = 47;
 

	
 
static const Widget _patches_selection_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_MAUVE,     0,    10,     0,    13, STR_00C5,                        STR_018B_CLOSE_WINDOW},
 
{    WWT_CAPTION,  RESIZE_RIGHT,  COLOUR_MAUVE,    11,   381,     0,    13, STR_CONFIG_PATCHES_CAPTION,      STR_018C_WINDOW_TITLE_DRAG_THIS},
 
{      WWT_PANEL,  RESIZE_RIGHT,  COLOUR_MAUVE,     0,   381,    14,    41, 0x0,                             STR_NULL},
 
{      WWT_PANEL,     RESIZE_RB,  COLOUR_MAUVE,     0,   369,    42,   215, 0x0,                             STR_NULL}, // PATCHSEL_OPTIONSPANEL
 
{  WWT_SCROLLBAR,    RESIZE_LRB,  COLOUR_MAUVE,   370,   381,    42,   203, 0x0,                             STR_0190_SCROLL_BAR_SCROLLS_LIST}, // PATCHSEL_SCROLLBAR
 
{  WWT_RESIZEBOX,   RESIZE_LRTB,  COLOUR_MAUVE,   370,   381,   204,   215, 0x0,                             STR_RESIZE_BUTTON}, // PATCHSEL_RESIZE
 

	
 
{    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_YELLOW,   10,   100,    16,    27, STR_CONFIG_PATCHES_GUI,          STR_NULL}, // PATCHSEL_INTERFACE
0 comments (0 inline, 0 general)