(svn r13112) -Codechange: make classes of the patches and custom currency windows.

pull/155/head
rubidium 16 years ago
parent 01e79dde73
commit 045aaae1c7

@ -850,16 +850,16 @@ enum PatchesSelectionWidgets {
PATCHSEL_COMPETITORS PATCHSEL_COMPETITORS
}; };
/** The main patches window. Shows a number of categories on top and struct PatchesSelectionWindow : Window {
* a selection of patches in that category.
* Uses WP(w, def_d) macro - data_1, data_2, data_3 */
static void PatchesSelectionWndProc(Window *w, WindowEvent *e)
{
static Patches *patches_ptr; static Patches *patches_ptr;
static int patches_max = 0; static int patches_max;
switch (e->event) { int page;
case WE_CREATE: { int entry;
int click;
PatchesSelectionWindow(const WindowDesc *desc) : Window(desc)
{
static bool first_time = true; static bool first_time = true;
patches_ptr = (_game_mode == GM_MENU) ? &_patches_newgame : &_patches; patches_ptr = (_game_mode == GM_MENU) ? &_patches_newgame : &_patches;
@ -886,21 +886,24 @@ static void PatchesSelectionWndProc(Window *w, WindowEvent *e)
} }
/* Resize the window to fit the largest patch tab */ /* Resize the window to fit the largest patch tab */
ResizeWindowForWidget(w, PATCHSEL_OPTIONSPANEL, 0, patches_max * 11); ResizeWindowForWidget(this, PATCHSEL_OPTIONSPANEL, 0, patches_max * 11);
/* Recentre the window for the new size */ /* Recentre the window for the new size */
w->top = w->top - (patches_max * 11) / 2; this->top = this->top - (patches_max * 11) / 2;
w->LowerWidget(4); this->LowerWidget(4);
} break;
case WE_PAINT: { this->FindWindowPlacementAndResize(desc);
}
virtual void OnPaint()
{
int x, y; int x, y;
const PatchPage *page = &_patches_page[WP(w, def_d).data_1]; const PatchPage *page = &_patches_page[this->page];
uint i; uint i;
/* Set up selected category */ /* Set up selected category */
DrawWindowWidgets(w); DrawWindowWidgets(this);
x = 5; x = 5;
y = 47; y = 47;
@ -929,7 +932,7 @@ static void PatchesSelectionWndProc(Window *w, WindowEvent *e)
value = (int32)ReadValue(var, sd->save.conv); value = (int32)ReadValue(var, sd->save.conv);
/* Draw [<][>] boxes for settings of an integer-type */ /* Draw [<][>] boxes for settings of an integer-type */
DrawArrowButtons(x, y, 3, WP(w, def_d).data_2 - (i * 2), (editable && value != sdb->min), (editable && value != sdb->max)); DrawArrowButtons(x, y, 3, this->click - (i * 2), (editable && value != sdb->min), (editable && value != sdb->max));
disabled = (value == 0) && (sdb->flags & SGF_0ISDISABLED); disabled = (value == 0) && (sdb->flags & SGF_0ISDISABLED);
if (disabled) { if (disabled) {
@ -948,22 +951,23 @@ static void PatchesSelectionWndProc(Window *w, WindowEvent *e)
DrawString(30, y, (sdb->str) + disabled, TC_FROMSTRING); DrawString(30, y, (sdb->str) + disabled, TC_FROMSTRING);
y += 11; y += 11;
} }
} break; }
case WE_CLICK: virtual void OnClick(Point pt, int widget)
switch (e->we.click.widget) { {
switch (widget) {
case PATCHSEL_OPTIONSPANEL: { case PATCHSEL_OPTIONSPANEL: {
const PatchPage *page = &_patches_page[WP(w, def_d).data_1]; const PatchPage *page = &_patches_page[this->page];
const SettingDesc *sd; const SettingDesc *sd;
void *var; void *var;
int32 value; int32 value;
int x, y; int x, y;
byte btn; byte btn;
y = e->we.click.pt.y - 46 - 1; y = pt.y - 46 - 1;
if (y < 0) return; if (y < 0) return;
x = e->we.click.pt.x - 5; x = pt.x - 5;
if (x < 0) return; if (x < 0) return;
btn = y / 11; btn = y / 11;
@ -996,7 +1000,7 @@ static void PatchesSelectionWndProc(Window *w, WindowEvent *e)
if (step == 0) step = 1; if (step == 0) step = 1;
// don't allow too fast scrolling // don't allow too fast scrolling
if ((w->flags4 & WF_TIMEOUT_MASK) > 2 << WF_TIMEOUT_SHL) { if ((this->flags4 & WF_TIMEOUT_MASK) > 2 << WF_TIMEOUT_SHL) {
_left_button_clicked = false; _left_button_clicked = false;
return; return;
} }
@ -1012,8 +1016,8 @@ static void PatchesSelectionWndProc(Window *w, WindowEvent *e)
/* Set up scroller timeout for numeric values */ /* Set up scroller timeout for numeric values */
if (value != oldvalue && !(sd->desc.flags & SGF_MULTISTRING)) { if (value != oldvalue && !(sd->desc.flags & SGF_MULTISTRING)) {
WP(w, def_d).data_2 = btn * 2 + 1 + ((x >= 10) ? 1 : 0); this->click = btn * 2 + 1 + ((x >= 10) ? 1 : 0);
w->flags4 |= 5 << WF_TIMEOUT_SHL; this->flags4 |= 5 << WF_TIMEOUT_SHL;
_left_button_clicked = false; _left_button_clicked = false;
} }
} break; } break;
@ -1022,7 +1026,7 @@ static void PatchesSelectionWndProc(Window *w, WindowEvent *e)
if (value != oldvalue) { if (value != oldvalue) {
SetPatchValue(page->entries[btn].index, patches_ptr, value); SetPatchValue(page->entries[btn].index, patches_ptr, value);
w->SetDirty(); this->SetDirty();
} }
} else { } else {
/* only open editbox for types that its sensible for */ /* only open editbox for types that its sensible for */
@ -1030,48 +1034,48 @@ static void PatchesSelectionWndProc(Window *w, WindowEvent *e)
/* Show the correct currency-translated value */ /* Show the correct currency-translated value */
if (sd->desc.flags & SGF_CURRENCY) value *= _currency->rate; if (sd->desc.flags & SGF_CURRENCY) value *= _currency->rate;
WP(w, def_d).data_3 = btn; this->entry = btn;
SetDParam(0, value); SetDParam(0, value);
ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_CONFIG_PATCHES_QUERY_CAPT, 10, 100, w, CS_NUMERAL); ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_CONFIG_PATCHES_QUERY_CAPT, 10, 100, this, CS_NUMERAL);
} }
} }
} break; } break;
case PATCHSEL_INTERFACE: case PATCHSEL_CONSTRUCTION: case PATCHSEL_VEHICLES: case PATCHSEL_INTERFACE: case PATCHSEL_CONSTRUCTION: case PATCHSEL_VEHICLES:
case PATCHSEL_STATIONS: case PATCHSEL_ECONOMY: case PATCHSEL_COMPETITORS: case PATCHSEL_STATIONS: case PATCHSEL_ECONOMY: case PATCHSEL_COMPETITORS:
w->RaiseWidget(WP(w, def_d).data_1 + PATCHSEL_INTERFACE); this->RaiseWidget(this->page + PATCHSEL_INTERFACE);
WP(w, def_d).data_1 = e->we.click.widget - PATCHSEL_INTERFACE; this->page = widget - PATCHSEL_INTERFACE;
w->LowerWidget(WP(w, def_d).data_1 + PATCHSEL_INTERFACE); this->LowerWidget(this->page + PATCHSEL_INTERFACE);
DeleteWindowById(WC_QUERY_STRING, 0); DeleteWindowById(WC_QUERY_STRING, 0);
w->SetDirty(); this->SetDirty();
break; break;
} }
break; }
case WE_TIMEOUT: virtual void OnTimeout()
WP(w, def_d).data_2 = 0; {
w->SetDirty(); this->click = 0;
break; this->SetDirty();
}
case WE_ON_EDIT_TEXT: virtual void OnQueryTextFinished(char *str)
if (!StrEmpty(e->we.edittext.str)) { {
const PatchEntry *pe = &_patches_page[WP(w, def_d).data_1].entries[WP(w, def_d).data_3]; if (!StrEmpty(str)) {
const PatchEntry *pe = &_patches_page[this->page].entries[this->entry];
const SettingDesc *sd = pe->setting; const SettingDesc *sd = pe->setting;
int32 value = atoi(e->we.edittext.str); int32 value = atoi(str);
/* Save the correct currency-translated value */ /* Save the correct currency-translated value */
if (sd->desc.flags & SGF_CURRENCY) value /= _currency->rate; if (sd->desc.flags & SGF_CURRENCY) value /= _currency->rate;
SetPatchValue(pe->index, patches_ptr, value); SetPatchValue(pe->index, patches_ptr, value);
w->SetDirty(); this->SetDirty();
} }
break;
case WE_DESTROY:
DeleteWindowById(WC_QUERY_STRING, 0);
break;
} }
} };
Patches *PatchesSelectionWindow::patches_ptr = NULL;
int PatchesSelectionWindow::patches_max = 0;
static const Widget _patches_selection_widgets[] = { static const Widget _patches_selection_widgets[] = {
{ WWT_CLOSEBOX, RESIZE_NONE, 10, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, { WWT_CLOSEBOX, RESIZE_NONE, 10, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
@ -1093,13 +1097,13 @@ static const WindowDesc _patches_selection_desc = {
WC_GAME_OPTIONS, WC_NONE, WC_GAME_OPTIONS, WC_NONE,
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET, WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
_patches_selection_widgets, _patches_selection_widgets,
PatchesSelectionWndProc, NULL,
}; };
void ShowPatchesSelection() void ShowPatchesSelection()
{ {
DeleteWindowById(WC_GAME_OPTIONS, 0); DeleteWindowById(WC_GAME_OPTIONS, 0);
new Window(&_patches_selection_desc); new PatchesSelectionWindow(&_patches_selection_desc);
} }
@ -1139,44 +1143,51 @@ enum CustomCurrenciesWidgets {
CUSTCURR_TO_EURO, CUSTCURR_TO_EURO,
}; };
static char _str_separator[2]; struct CustomCurrencyWindow : Window {
char separator[2];
int click;
int query_widget;
static void CustCurrencyWndProc(Window *w, WindowEvent *e) CustomCurrencyWindow(const WindowDesc *desc) : Window(desc)
{ {
switch (e->event) { this->separator[0] = _custom_currency.separator;
case WE_PAINT: { this->separator[1] = '\0';
this->FindWindowPlacementAndResize(desc);
}
virtual void OnPaint()
{
int x; int x;
int y = 20; int y = 20;
int clk = WP(w, def_d).data_1; DrawWindowWidgets(this);
DrawWindowWidgets(w);
/* exchange rate */ /* exchange rate */
DrawArrowButtons(10, y, 3, GB(clk, 0, 2), true, true); DrawArrowButtons(10, y, 3, GB(this->click, 0, 2), true, true);
SetDParam(0, 1); SetDParam(0, 1);
SetDParam(1, 1); SetDParam(1, 1);
DrawString(35, y + 1, STR_CURRENCY_EXCHANGE_RATE, TC_FROMSTRING); DrawString(35, y + 1, STR_CURRENCY_EXCHANGE_RATE, TC_FROMSTRING);
y += 12; y += 12;
/* separator */ /* separator */
DrawFrameRect(10, y + 1, 29, y + 9, 0, GB(clk, 2, 2) ? FR_LOWERED : FR_NONE); DrawFrameRect(10, y + 1, 29, y + 9, 0, GB(this->click, 2, 2) ? FR_LOWERED : FR_NONE);
x = DrawString(35, y + 1, STR_CURRENCY_SEPARATOR, TC_FROMSTRING); x = DrawString(35, y + 1, STR_CURRENCY_SEPARATOR, TC_FROMSTRING);
DoDrawString(_str_separator, x + 4, y + 1, TC_ORANGE); DoDrawString(this->separator, x + 4, y + 1, TC_ORANGE);
y += 12; y += 12;
/* prefix */ /* prefix */
DrawFrameRect(10, y + 1, 29, y + 9, 0, GB(clk, 4, 2) ? FR_LOWERED : FR_NONE); DrawFrameRect(10, y + 1, 29, y + 9, 0, GB(this->click, 4, 2) ? FR_LOWERED : FR_NONE);
x = DrawString(35, y + 1, STR_CURRENCY_PREFIX, TC_FROMSTRING); x = DrawString(35, y + 1, STR_CURRENCY_PREFIX, TC_FROMSTRING);
DoDrawString(_custom_currency.prefix, x + 4, y + 1, TC_ORANGE); DoDrawString(_custom_currency.prefix, x + 4, y + 1, TC_ORANGE);
y += 12; y += 12;
/* suffix */ /* suffix */
DrawFrameRect(10, y + 1, 29, y + 9, 0, GB(clk, 6, 2) ? FR_LOWERED : FR_NONE); DrawFrameRect(10, y + 1, 29, y + 9, 0, GB(this->click, 6, 2) ? FR_LOWERED : FR_NONE);
x = DrawString(35, y + 1, STR_CURRENCY_SUFFIX, TC_FROMSTRING); x = DrawString(35, y + 1, STR_CURRENCY_SUFFIX, TC_FROMSTRING);
DoDrawString(_custom_currency.suffix, x + 4, y + 1, TC_ORANGE); DoDrawString(_custom_currency.suffix, x + 4, y + 1, TC_ORANGE);
y += 12; y += 12;
/* switch to euro */ /* switch to euro */
DrawArrowButtons(10, y, 3, GB(clk, 8, 2), true, true); DrawArrowButtons(10, y, 3, GB(this->click, 8, 2), true, true);
SetDParam(0, _custom_currency.to_euro); SetDParam(0, _custom_currency.to_euro);
DrawString(35, y + 1, (_custom_currency.to_euro != CF_NOEURO) ? STR_CURRENCY_SWITCH_TO_EURO : STR_CURRENCY_SWITCH_TO_EURO_NEVER, TC_FROMSTRING); DrawString(35, y + 1, (_custom_currency.to_euro != CF_NOEURO) ? STR_CURRENCY_SWITCH_TO_EURO : STR_CURRENCY_SWITCH_TO_EURO_NEVER, TC_FROMSTRING);
y += 12; y += 12;
@ -1185,12 +1196,13 @@ static void CustCurrencyWndProc(Window *w, WindowEvent *e)
y += 12; y += 12;
SetDParam(0, 10000); SetDParam(0, 10000);
DrawString(35, y + 1, STR_CURRENCY_PREVIEW, TC_FROMSTRING); DrawString(35, y + 1, STR_CURRENCY_PREVIEW, TC_FROMSTRING);
} break; }
case WE_CLICK: { virtual void OnClick(Point pt, int widget)
int line = (e->we.click.pt.y - 20) / 12; {
int line = (pt.y - 20) / 12;
int len = 0; int len = 0;
int x = e->we.click.pt.x; int x = pt.x;
StringID str = 0; StringID str = 0;
CharSetFilter afilter = CS_ALPHANUMERAL; CharSetFilter afilter = CS_ALPHANUMERAL;
@ -1199,10 +1211,10 @@ static void CustCurrencyWndProc(Window *w, WindowEvent *e)
if (IsInsideMM(x, 10, 30)) { // clicked buttons if (IsInsideMM(x, 10, 30)) { // clicked buttons
if (x < 20) { if (x < 20) {
if (_custom_currency.rate > 1) _custom_currency.rate--; if (_custom_currency.rate > 1) _custom_currency.rate--;
WP(w, def_d).data_1 = 1 << (line * 2 + 0); this->click = 1 << (line * 2 + 0);
} else { } else {
if (_custom_currency.rate < 5000) _custom_currency.rate++; if (_custom_currency.rate < 5000) _custom_currency.rate++;
WP(w, def_d).data_1 = 1 << (line * 2 + 1); this->click = 1 << (line * 2 + 1);
} }
} else { // enter text } else { // enter text
SetDParam(0, _custom_currency.rate); SetDParam(0, _custom_currency.rate);
@ -1214,15 +1226,15 @@ static void CustCurrencyWndProc(Window *w, WindowEvent *e)
case CUSTCURR_SEPARATOR: case CUSTCURR_SEPARATOR:
if (IsInsideMM(x, 10, 30)) { // clicked button if (IsInsideMM(x, 10, 30)) { // clicked button
WP(w, def_d).data_1 = 1 << (line * 2 + 1); this->click = 1 << (line * 2 + 1);
} }
str = BindCString(_str_separator); str = BindCString(this->separator);
len = 1; len = 1;
break; break;
case CUSTCURR_PREFIX: case CUSTCURR_PREFIX:
if (IsInsideMM(x, 10, 30)) { // clicked button if (IsInsideMM(x, 10, 30)) { // clicked button
WP(w, def_d).data_1 = 1 << (line * 2 + 1); this->click = 1 << (line * 2 + 1);
} }
str = BindCString(_custom_currency.prefix); str = BindCString(_custom_currency.prefix);
len = 12; len = 12;
@ -1230,7 +1242,7 @@ static void CustCurrencyWndProc(Window *w, WindowEvent *e)
case CUSTCURR_SUFFIX: case CUSTCURR_SUFFIX:
if (IsInsideMM(x, 10, 30)) { // clicked button if (IsInsideMM(x, 10, 30)) { // clicked button
WP(w, def_d).data_1 = 1 << (line * 2 + 1); this->click = 1 << (line * 2 + 1);
} }
str = BindCString(_custom_currency.suffix); str = BindCString(_custom_currency.suffix);
len = 12; len = 12;
@ -1241,11 +1253,11 @@ static void CustCurrencyWndProc(Window *w, WindowEvent *e)
if (x < 20) { if (x < 20) {
_custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ? _custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ?
CF_NOEURO : _custom_currency.to_euro - 1; CF_NOEURO : _custom_currency.to_euro - 1;
WP(w, def_d).data_1 = 1 << (line * 2 + 0); this->click = 1 << (line * 2 + 0);
} else { } else {
_custom_currency.to_euro = _custom_currency.to_euro =
Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR); Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
WP(w, def_d).data_1 = 1 << (line * 2 + 1); this->click = 1 << (line * 2 + 1);
} }
} else { // enter text } else { // enter text
SetDParam(0, _custom_currency.to_euro); SetDParam(0, _custom_currency.to_euro);
@ -1257,58 +1269,52 @@ static void CustCurrencyWndProc(Window *w, WindowEvent *e)
} }
if (len != 0) { if (len != 0) {
WP(w, def_d).data_2 = line; this->query_widget = line;
ShowQueryString(str, STR_CURRENCY_CHANGE_PARAMETER, len + 1, 250, w, afilter); ShowQueryString(str, STR_CURRENCY_CHANGE_PARAMETER, len + 1, 250, this, afilter);
} }
w->flags4 |= 5 << WF_TIMEOUT_SHL; this->flags4 |= 5 << WF_TIMEOUT_SHL;
w->SetDirty(); this->SetDirty();
} break; }
case WE_ON_EDIT_TEXT: {
if (e->we.edittext.str == NULL) break;
const char *b = e->we.edittext.str; virtual void OnQueryTextFinished(char *str)
{
if (str == NULL) return;
switch (WP(w, def_d).data_2) { switch (this->query_widget) {
case CUSTCURR_EXCHANGERATE: case CUSTCURR_EXCHANGERATE:
_custom_currency.rate = Clamp(atoi(b), 1, 5000); _custom_currency.rate = Clamp(atoi(str), 1, 5000);
break; break;
case CUSTCURR_SEPARATOR: /* Thousands seperator */ case CUSTCURR_SEPARATOR: /* Thousands seperator */
_custom_currency.separator = StrEmpty(b) ? ' ' : b[0]; _custom_currency.separator = StrEmpty(str) ? ' ' : str[0];
ttd_strlcpy(_str_separator, b, lengthof(_str_separator)); ttd_strlcpy(this->separator, str, lengthof(this->separator));
break; break;
case CUSTCURR_PREFIX: case CUSTCURR_PREFIX:
ttd_strlcpy(_custom_currency.prefix, b, lengthof(_custom_currency.prefix)); ttd_strlcpy(_custom_currency.prefix, str, lengthof(_custom_currency.prefix));
break; break;
case CUSTCURR_SUFFIX: case CUSTCURR_SUFFIX:
ttd_strlcpy(_custom_currency.suffix, b, lengthof(_custom_currency.suffix)); ttd_strlcpy(_custom_currency.suffix, str, lengthof(_custom_currency.suffix));
break; break;
case CUSTCURR_TO_EURO: { /* Year to switch to euro */ case CUSTCURR_TO_EURO: { /* Year to switch to euro */
int val = atoi(b); int val = atoi(str);
_custom_currency.to_euro = (val < 2000 ? CF_NOEURO : min(val, MAX_YEAR)); _custom_currency.to_euro = (val < 2000 ? CF_NOEURO : min(val, MAX_YEAR));
break; break;
} }
} }
MarkWholeScreenDirty(); MarkWholeScreenDirty();
} break; }
case WE_TIMEOUT:
WP(w, def_d).data_1 = 0;
w->SetDirty();
break;
case WE_DESTROY: virtual void OnTimeout()
DeleteWindowById(WC_QUERY_STRING, 0); {
MarkWholeScreenDirty(); this->click = 0;
break; this->SetDirty();
} }
} };
static const Widget _cust_currency_widgets[] = { static const Widget _cust_currency_widgets[] = {
{ WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
@ -1322,14 +1328,11 @@ static const WindowDesc _cust_currency_desc = {
WC_CUSTOM_CURRENCY, WC_NONE, WC_CUSTOM_CURRENCY, WC_NONE,
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
_cust_currency_widgets, _cust_currency_widgets,
CustCurrencyWndProc, NULL,
}; };
static void ShowCustCurrency() static void ShowCustCurrency()
{ {
_str_separator[0] = _custom_currency.separator;
_str_separator[1] = '\0';
DeleteWindowById(WC_CUSTOM_CURRENCY, 0); DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
new Window(&_cust_currency_desc); new CustomCurrencyWindow(&_cust_currency_desc);
} }

Loading…
Cancel
Save