--- a/src/settings_gui.cpp Mon May 19 14:14:33 2008 +0000
+++ b/src/settings_gui.cpp Mon May 19 15:13:58 2008 +0000
@@ -20,7 +20,6 @@
#include "strings_func.h"
#include "functions.h"
#include "window_func.h"
-#include "vehicle_base.h"
#include "core/alloc_func.hpp"
#include "string_func.h"
#include "gfx_func.h"
@@ -102,17 +101,6 @@
return i;
}
-static inline bool RoadVehiclesAreBuilt()
-{
- const Vehicle* v;
-
- FOR_ALL_VEHICLES(v) {
- if (v->type == VEH_ROAD) return true;
- }
- return false;
-}
-
-
enum GameOptionsWidgets {
GAMEOPT_CURRENCY_BTN = 4,
GAMEOPT_DISTANCE_BTN = 6,
@@ -151,199 +139,178 @@
ShowDropDownList(w, list, sel, GAMEOPT_TOWNNAME_BTN);
}
-/**
- * Update/redraw the languages dropdown
- * @param w the window the dropdown belongs to
- */
-static void ShowLangDropdown(Window *w)
-{
- typedef std::map<StringID, int, StringIDCompare> LangList;
-
- /* Sort language names */
- LangList langs;
- for (int i = 0; i < _dynlang.num; i++) langs[SPECSTR_LANGUAGE_START + i] = i;
-
- DropDownList *list = new DropDownList();
- for (LangList::iterator it = langs.begin(); it != langs.end(); it++) {
- list->push_back(new DropDownListStringItem((*it).first, (*it).second, false));
- }
-
- ShowDropDownList(w, list, _dynlang.curr, GAMEOPT_LANG_BTN);
-}
-
static void ShowCustCurrency();
-static void GameOptionsWndProc(Window *w, WindowEvent *e)
-{
- switch (e->event) {
- case WE_PAINT: {
- int i;
- StringID str = STR_02BE_DEFAULT;
-
- w->SetWidgetDisabledState(GAMEOPT_VEHICLENAME_SAVE, !(_vehicle_design_names & 1));
- if (!w->IsWidgetDisabled(GAMEOPT_VEHICLENAME_SAVE)) str = STR_02BF_CUSTOM;
- SetDParam(0, str);
- SetDParam(1, _currency_specs[_opt_ptr->currency].name);
- SetDParam(2, STR_UNITS_IMPERIAL + _opt_ptr->units);
- SetDParam(3, STR_02E9_DRIVE_ON_LEFT + _opt_ptr->road_side);
- SetDParam(4, TownName(_opt_ptr->town_name));
- SetDParam(5, _autosave_dropdown[_opt_ptr->autosave]);
- SetDParam(6, SPECSTR_LANGUAGE_START + _dynlang.curr);
- i = GetCurRes();
- SetDParam(7, i == _num_resolutions ? STR_RES_OTHER : SPECSTR_RESOLUTION_START + i);
- SetDParam(8, SPECSTR_SCREENSHOT_START + _cur_screenshot_format);
- w->SetWidgetLoweredState(GAMEOPT_FULLSCREEN, _fullscreen);
-
- DrawWindowWidgets(w);
- DrawString(20, 175, STR_OPTIONS_FULLSCREEN, TC_FROMSTRING); // fullscreen
- } break;
-
- case WE_CLICK:
- switch (e->we.click.widget) {
- case GAMEOPT_CURRENCY_BTN: /* Setup currencies dropdown */
- ShowDropDownMenu(w, BuildCurrencyDropdown(), _opt_ptr->currency, GAMEOPT_CURRENCY_BTN, _game_mode == GM_MENU ? 0 : ~GetMaskOfAllowedCurrencies(), 0);
- break;
-
- case GAMEOPT_DISTANCE_BTN: /* Setup distance unit dropdown */
- ShowDropDownMenu(w, _units_dropdown, _opt_ptr->units, GAMEOPT_DISTANCE_BTN, 0, 0);
- break;
-
- case GAMEOPT_ROADSIDE_BTN: { /* Setup road-side dropdown */
- int i = 0;
-
- /* You can only change the drive side if you are in the menu or ingame with
- * no vehicles present. In a networking game only the server can change it */
- if ((_game_mode != GM_MENU && RoadVehiclesAreBuilt()) || (_networking && !_network_server))
- i = (-1) ^ (1 << _opt_ptr->road_side); // disable the other value
-
- ShowDropDownMenu(w, _driveside_dropdown, _opt_ptr->road_side, GAMEOPT_ROADSIDE_BTN, i, 0);
- } break;
-
- case GAMEOPT_TOWNNAME_BTN: /* Setup townname dropdown */
- ShowTownnameDropdown(w, _opt_ptr->town_name);
- break;
-
- case GAMEOPT_AUTOSAVE_BTN: /* Setup autosave dropdown */
- ShowDropDownMenu(w, _autosave_dropdown, _opt_ptr->autosave, GAMEOPT_AUTOSAVE_BTN, 0, 0);
- break;
-
- case GAMEOPT_VEHICLENAME_BTN: /* Setup customized vehicle-names dropdown */
- ShowDropDownMenu(w, _designnames_dropdown, (_vehicle_design_names & 1) ? 1 : 0, GAMEOPT_VEHICLENAME_BTN, (_vehicle_design_names & 2) ? 0 : 2, 0);
- break;
-
- case GAMEOPT_VEHICLENAME_SAVE: /* Save customized vehicle-names to disk */
- break; // not implemented
-
- case GAMEOPT_LANG_BTN: /* Setup interface language dropdown */
- ShowLangDropdown(w);
- break;
-
- case GAMEOPT_RESOLUTION_BTN: /* Setup resolution dropdown */
- ShowDropDownMenu(w, BuildDynamicDropdown(SPECSTR_RESOLUTION_START, _num_resolutions), GetCurRes(), GAMEOPT_RESOLUTION_BTN, 0, 0);
- break;
-
- case GAMEOPT_FULLSCREEN: /* Click fullscreen on/off */
- /* try to toggle full-screen on/off */
- if (!ToggleFullScreen(!_fullscreen)) {
- ShowErrorMessage(INVALID_STRING_ID, STR_FULLSCREEN_FAILED, 0, 0);
- }
- w->SetWidgetLoweredState(GAMEOPT_FULLSCREEN, _fullscreen);
- w->SetDirty();
- break;
-
- case GAMEOPT_SCREENSHOT_BTN: /* Setup screenshot format dropdown */
- ShowDropDownMenu(w, BuildDynamicDropdown(SPECSTR_SCREENSHOT_START, _num_screenshot_formats), _cur_screenshot_format, GAMEOPT_SCREENSHOT_BTN, 0, 0);
- break;
- }
- break;
-
- case WE_DROPDOWN_SELECT:
- switch (e->we.dropdown.button) {
- case GAMEOPT_VEHICLENAME_BTN: /* Vehicle design names */
- if (e->we.dropdown.index == 0) {
- DeleteCustomEngineNames();
- MarkWholeScreenDirty();
- } else if (!(_vehicle_design_names & 1)) {
- LoadCustomEngineNames();
- MarkWholeScreenDirty();
- }
- break;
-
- case GAMEOPT_CURRENCY_BTN: /* Currency */
- if (e->we.dropdown.index == CUSTOM_CURRENCY_ID) ShowCustCurrency();
- _opt_ptr->currency = e->we.dropdown.index;
- MarkWholeScreenDirty();
- break;
-
- case GAMEOPT_DISTANCE_BTN: /* Measuring units */
- _opt_ptr->units = e->we.dropdown.index;
- MarkWholeScreenDirty();
- break;
-
- case GAMEOPT_ROADSIDE_BTN: /* Road side */
- if (_opt_ptr->road_side != e->we.dropdown.index) { // only change if setting changed
- DoCommandP(0, e->we.dropdown.index, 0, NULL, CMD_SET_ROAD_DRIVE_SIDE | CMD_MSG(STR_00B4_CAN_T_DO_THIS));
- MarkWholeScreenDirty();
- }
- break;
-
- case GAMEOPT_TOWNNAME_BTN: /* Town names */
- if (_game_mode == GM_MENU) {
- _opt_ptr->town_name = e->we.dropdown.index;
- InvalidateWindow(WC_GAME_OPTIONS, 0);
- }
- break;
-
- case GAMEOPT_AUTOSAVE_BTN: /* Autosave options */
- _opt.autosave = _opt_newgame.autosave = e->we.dropdown.index;
- w->SetDirty();
- break;
-
- case GAMEOPT_LANG_BTN: /* Change interface language */
- ReadLanguagePack(e->we.dropdown.index);
- CheckForMissingGlyphsInLoadedLanguagePack();
- UpdateAllStationVirtCoord();
- UpdateAllWaypointSigns();
- MarkWholeScreenDirty();
- break;
-
- case GAMEOPT_RESOLUTION_BTN: /* Change resolution */
- if (e->we.dropdown.index < _num_resolutions && ChangeResInGame(_resolutions[e->we.dropdown.index][0], _resolutions[e->we.dropdown.index][1]))
- w->SetDirty();
- break;
-
- case GAMEOPT_SCREENSHOT_BTN: /* Change screenshot format */
- SetScreenshotFormat(e->we.dropdown.index);
- w->SetDirty();
- break;
- }
- break;
-
- case WE_DESTROY:
- DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
- break;
+struct GameOptionsWindow : Window {
+ GameOptionsWindow(const WindowDesc *desc) : Window(desc)
+ {
+ this->FindWindowPlacementAndResize(desc);
}
-}
+ ~GameOptionsWindow()
+ {
+ DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
+ }
-/** Change the side of the road vehicles drive on (server only).
- * @param tile unused
- * @param flags operation to perform
- * @param p1 the side of the road; 0 = left side and 1 = right side
- * @param p2 unused
- */
-CommandCost CmdSetRoadDriveSide(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
-{
- /* Check boundaries and you can only change this if NO vehicles have been built yet,
- * except in the intro-menu where of course it's always possible to do so. */
- if (p1 > 1 || (_game_mode != GM_MENU && RoadVehiclesAreBuilt())) return CMD_ERROR;
+ virtual void OnPaint()
+ {
+ StringID str = STR_02BE_DEFAULT;
- if (flags & DC_EXEC) {
- _opt_ptr->road_side = p1;
- InvalidateWindow(WC_GAME_OPTIONS, 0);
+ this->SetWidgetDisabledState(GAMEOPT_VEHICLENAME_SAVE, !(_vehicle_design_names & 1));
+ if (!this->IsWidgetDisabled(GAMEOPT_VEHICLENAME_SAVE)) str = STR_02BF_CUSTOM;
+ SetDParam(0, str);
+ SetDParam(1, _currency_specs[_opt_ptr->currency].name);
+ SetDParam(2, STR_UNITS_IMPERIAL + _opt_ptr->units);
+ SetDParam(3, STR_02E9_DRIVE_ON_LEFT + _opt_ptr->road_side);
+ SetDParam(4, TownName(_opt_ptr->town_name));
+ SetDParam(5, _autosave_dropdown[_opt_ptr->autosave]);
+ SetDParam(6, SPECSTR_LANGUAGE_START + _dynlang.curr);
+ int i = GetCurRes();
+ SetDParam(7, i == _num_resolutions ? STR_RES_OTHER : SPECSTR_RESOLUTION_START + i);
+ SetDParam(8, SPECSTR_SCREENSHOT_START + _cur_screenshot_format);
+ this->SetWidgetLoweredState(GAMEOPT_FULLSCREEN, _fullscreen);
+
+ this->DrawWidgets();
+ DrawString(20, 175, STR_OPTIONS_FULLSCREEN, TC_FROMSTRING); // fullscreen
}
- return CommandCost();
-}
+
+ virtual void OnClick(Point pt, int widget)
+ {
+ switch (widget) {
+ case GAMEOPT_CURRENCY_BTN: // Setup currencies dropdown
+ ShowDropDownMenu(this, BuildCurrencyDropdown(), _opt_ptr->currency, GAMEOPT_CURRENCY_BTN, _game_mode == GM_MENU ? 0 : ~GetMaskOfAllowedCurrencies(), 0);
+ break;
+
+ case GAMEOPT_DISTANCE_BTN: // Setup distance unit dropdown
+ ShowDropDownMenu(this, _units_dropdown, _opt_ptr->units, GAMEOPT_DISTANCE_BTN, 0, 0);
+ break;
+
+ case GAMEOPT_ROADSIDE_BTN: { // Setup road-side dropdown
+ int i = 0;
+ extern bool RoadVehiclesAreBuilt();
+
+ /* You can only change the drive side if you are in the menu or ingame with
+ * no vehicles present. In a networking game only the server can change it */
+ if ((_game_mode != GM_MENU && RoadVehiclesAreBuilt()) || (_networking && !_network_server)) {
+ i = (-1) ^ (1 << _opt_ptr->road_side); // disable the other value
+ }
+
+ ShowDropDownMenu(this, _driveside_dropdown, _opt_ptr->road_side, GAMEOPT_ROADSIDE_BTN, i, 0);
+ } break;
+
+ case GAMEOPT_TOWNNAME_BTN: // Setup townname dropdown
+ ShowTownnameDropdown(this, _opt_ptr->town_name);
+ break;
+
+ case GAMEOPT_AUTOSAVE_BTN: // Setup autosave dropdown
+ ShowDropDownMenu(this, _autosave_dropdown, _opt_ptr->autosave, GAMEOPT_AUTOSAVE_BTN, 0, 0);
+ break;
+
+ case GAMEOPT_VEHICLENAME_BTN: // Setup customized vehicle-names dropdown
+ ShowDropDownMenu(this, _designnames_dropdown, (_vehicle_design_names & 1) ? 1 : 0, GAMEOPT_VEHICLENAME_BTN, (_vehicle_design_names & 2) ? 0 : 2, 0);
+ break;
+
+ case GAMEOPT_VEHICLENAME_SAVE: // Save customized vehicle-names to disk
+ break; // not implemented
+
+ case GAMEOPT_LANG_BTN: { // Setup interface language dropdown
+ typedef std::map<StringID, int, StringIDCompare> LangList;
+
+ /* Sort language names */
+ LangList langs;
+ for (int i = 0; i < _dynlang.num; i++) langs[SPECSTR_LANGUAGE_START + i] = i;
+
+ DropDownList *list = new DropDownList();
+ for (LangList::iterator it = langs.begin(); it != langs.end(); it++) {
+ list->push_back(new DropDownListStringItem((*it).first, (*it).second, false));
+ }
+
+ ShowDropDownList(this, list, _dynlang.curr, GAMEOPT_LANG_BTN);
+ } break;
+
+ case GAMEOPT_RESOLUTION_BTN: // Setup resolution dropdown
+ ShowDropDownMenu(this, BuildDynamicDropdown(SPECSTR_RESOLUTION_START, _num_resolutions), GetCurRes(), GAMEOPT_RESOLUTION_BTN, 0, 0);
+ break;
+
+ case GAMEOPT_FULLSCREEN: // Click fullscreen on/off
+ /* try to toggle full-screen on/off */
+ if (!ToggleFullScreen(!_fullscreen)) {
+ ShowErrorMessage(INVALID_STRING_ID, STR_FULLSCREEN_FAILED, 0, 0);
+ }
+ this->SetWidgetLoweredState(GAMEOPT_FULLSCREEN, _fullscreen);
+ this->SetDirty();
+ break;
+
+ case GAMEOPT_SCREENSHOT_BTN: // Setup screenshot format dropdown
+ ShowDropDownMenu(this, BuildDynamicDropdown(SPECSTR_SCREENSHOT_START, _num_screenshot_formats), _cur_screenshot_format, GAMEOPT_SCREENSHOT_BTN, 0, 0);
+ break;
+ }
+ }
+
+ virtual void OnDropdownSelect(int widget, int index)
+ {
+ switch (widget) {
+ case GAMEOPT_VEHICLENAME_BTN: // Vehicle design names
+ if (index == 0) {
+ DeleteCustomEngineNames();
+ MarkWholeScreenDirty();
+ } else if (!(_vehicle_design_names & 1)) {
+ LoadCustomEngineNames();
+ MarkWholeScreenDirty();
+ }
+ break;
+
+ case GAMEOPT_CURRENCY_BTN: /* Currency */
+ if (index == CUSTOM_CURRENCY_ID) ShowCustCurrency();
+ _opt_ptr->currency = index;
+ MarkWholeScreenDirty();
+ break;
+
+ case GAMEOPT_DISTANCE_BTN: // Measuring units
+ _opt_ptr->units = index;
+ MarkWholeScreenDirty();
+ break;
+
+ case GAMEOPT_ROADSIDE_BTN: // Road side
+ if (_opt_ptr->road_side != index) { // only change if setting changed
+ DoCommandP(0, index, 0, NULL, CMD_SET_ROAD_DRIVE_SIDE | CMD_MSG(STR_00B4_CAN_T_DO_THIS));
+ MarkWholeScreenDirty();
+ }
+ break;
+
+ case GAMEOPT_TOWNNAME_BTN: // Town names
+ if (_game_mode == GM_MENU) {
+ _opt_ptr->town_name = index;
+ InvalidateWindow(WC_GAME_OPTIONS, 0);
+ }
+ break;
+
+ case GAMEOPT_AUTOSAVE_BTN: // Autosave options
+ _opt.autosave = _opt_newgame.autosave = index;
+ this->SetDirty();
+ break;
+
+ case GAMEOPT_LANG_BTN: // Change interface language
+ ReadLanguagePack(index);
+ CheckForMissingGlyphsInLoadedLanguagePack();
+ UpdateAllStationVirtCoord();
+ UpdateAllWaypointSigns();
+ MarkWholeScreenDirty();
+ break;
+
+ case GAMEOPT_RESOLUTION_BTN: // Change resolution
+ if (index < _num_resolutions && ChangeResInGame(_resolutions[index][0], _resolutions[index][1])) {
+ this->SetDirty();
+ }
+ break;
+
+ case GAMEOPT_SCREENSHOT_BTN: // Change screenshot format
+ SetScreenshotFormat(index);
+ this->SetDirty();
+ break;
+ }
+ }
+};
static const Widget _game_options_widgets[] = {
{ WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
@@ -382,14 +349,13 @@
WC_GAME_OPTIONS, WC_NONE,
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
_game_options_widgets,
- GameOptionsWndProc
};
void ShowGameOptions()
{
DeleteWindowById(WC_GAME_OPTIONS, 0);
- new Window(&_game_options_desc);
+ new GameOptionsWindow(&_game_options_desc);
}
struct GameSettingData {
@@ -478,194 +444,6 @@
extern void StartupEconomy();
-enum {
- GAMEDIFF_WND_TOP_OFFSET = 45,
- GAMEDIFF_WND_ROWSIZE = 9
-};
-
-/* Temporary holding place of values in the difficulty window until 'Save' is clicked */
-static GameOptions _opt_mod_temp;
-// 0x383E = (1 << 13) | (1 << 12) | (1 << 11) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1)
-#define DIFF_INGAME_DISABLED_BUTTONS 0x383E
-
-#define NO_SETTINGS_BUTTON 0xFF
-
-/** Carriage for the game settings window data */
-struct difficulty_d {
- bool clicked_increase;
- uint8 clicked_button;
- uint8 timeout;
-};
-assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(difficulty_d));
-
-/* Names of the game difficulty settings window */
-enum GameDifficultyWidgets {
- GDW_CLOSEBOX = 0,
- GDW_CAPTION,
- GDW_UPPER_BG,
- GDW_LVL_EASY,
- GDW_LVL_MEDIUM,
- GDW_LVL_HARD,
- GDW_LVL_CUSTOM,
- GDW_HIGHSCORE,
- GDW_SETTING_BG,
- GDW_LOWER_BG,
- GDW_ACCEPT,
- GDW_CANCEL,
-};
-
-static void GameDifficultyWndProc(Window *w, WindowEvent *e)
-{
- difficulty_d *diffic_d = &WP(w, difficulty_d);
- switch (e->event) {
- case WE_CREATE:
- diffic_d->clicked_increase = false;
- diffic_d->clicked_button = NO_SETTINGS_BUTTON;
- diffic_d->timeout = 0;
- /* Hide the closebox to make sure that the user aborts or confirms his changes */
- w->HideWidget(GDW_CLOSEBOX);
- w->widget[GDW_CAPTION].left = 0;
- /* Setup disabled buttons when creating window
- * disable all other difficulty buttons during gameplay except for 'custom' */
- w->SetWidgetsDisabledState(_game_mode == GM_NORMAL,
- GDW_LVL_EASY,
- GDW_LVL_MEDIUM,
- GDW_LVL_HARD,
- GDW_LVL_CUSTOM,
- WIDGET_LIST_END);
- w->SetWidgetDisabledState(GDW_HIGHSCORE, _game_mode == GM_EDITOR || _networking); // highscore chart in multiplayer
- w->SetWidgetDisabledState(GDW_ACCEPT, _networking && !_network_server); // Save-button in multiplayer (and if client)
- w->LowerWidget(GDW_LVL_EASY + _opt_mod_temp.diff_level);
- break;
-
- case WE_PAINT: {
- DrawWindowWidgets(w);
-
- /* XXX - Disabled buttons in normal gameplay or during muliplayer as non server.
- * Bitshifted for each button to see if that bit is set. If it is set, the
- * button is disabled */
- uint32 disabled = 0;
- if (_networking && !_network_server) {
- disabled = MAX_UVALUE(uint32); // Disable all
- } else if (_game_mode == GM_NORMAL) {
- disabled = DIFF_INGAME_DISABLED_BUTTONS;
- }
-
- int value;
- int y = GAMEDIFF_WND_TOP_OFFSET;
- for (uint i = 0; i != GAME_DIFFICULTY_NUM; i++) {
- const GameSettingData *gsd = &_game_setting_info[i];
- value = ((GDType*)&_opt_mod_temp.diff)[i];
-
- DrawArrowButtons(5, y, 3,
- (diffic_d->clicked_button == i) ? 1 + !!diffic_d->clicked_increase : 0,
- !(HasBit(disabled, i) || gsd->min == value),
- !(HasBit(disabled, i) || gsd->max == value));
-
- value += _game_setting_info[i].str;
- if (i == 4) value *= 1000; // XXX - handle currency option
- SetDParam(0, value);
- DrawString(30, y, STR_6805_MAXIMUM_NO_COMPETITORS + i, TC_FROMSTRING);
-
- y += GAMEDIFF_WND_ROWSIZE + 2; // space items apart a bit
- }
- } break;
-
- case WE_CLICK:
- switch (e->we.click.widget) {
- case GDW_SETTING_BG: { /* Difficulty settings widget, decode click */
- /* Don't allow clients to make any changes */
- if (_networking && !_network_server) return;
-
- const int x = e->we.click.pt.x - 5;
- if (!IsInsideMM(x, 0, 21)) // Button area
- return;
-
- const int y = e->we.click.pt.y - GAMEDIFF_WND_TOP_OFFSET;
- if (y < 0) return;
-
- /* Get button from Y coord. */
- const uint8 btn = y / (GAMEDIFF_WND_ROWSIZE + 2);
- if (btn >= GAME_DIFFICULTY_NUM || y % (GAMEDIFF_WND_ROWSIZE + 2) >= 9)
- return;
-
- /* Clicked disabled button? */
- if (_game_mode == GM_NORMAL && HasBit(DIFF_INGAME_DISABLED_BUTTONS, btn))
- return;
-
- diffic_d->timeout = 5;
-
- int16 val = ((GDType*)&_opt_mod_temp.diff)[btn];
-
- const GameSettingData *info = &_game_setting_info[btn]; // get information about the difficulty setting
- if (x >= 10) {
- /* Increase button clicked */
- val = min(val + info->step, info->max);
- diffic_d->clicked_increase = true;
- } else {
- /* Decrease button clicked */
- val -= info->step;
- val = max(val, info->min);
- diffic_d->clicked_increase = false;
- }
- diffic_d->clicked_button = btn;
-
- /* save value in temporary variable */
- ((GDType*)&_opt_mod_temp.diff)[btn] = val;
- w->RaiseWidget(GDW_LVL_EASY + _opt_mod_temp.diff_level);
- SetDifficultyLevel(3, &_opt_mod_temp); // set difficulty level to custom
- w->LowerWidget(GDW_LVL_CUSTOM);
- w->SetDirty();
- } break;
-
- case GDW_LVL_EASY:
- case GDW_LVL_MEDIUM:
- case GDW_LVL_HARD:
- case GDW_LVL_CUSTOM:
- /* temporarily change difficulty level */
- w->RaiseWidget(GDW_LVL_EASY + _opt_mod_temp.diff_level);
- SetDifficultyLevel(e->we.click.widget - GDW_LVL_EASY, &_opt_mod_temp);
- w->LowerWidget(GDW_LVL_EASY + _opt_mod_temp.diff_level);
- w->SetDirty();
- break;
-
- case GDW_HIGHSCORE: // Highscore Table
- ShowHighscoreTable(_opt_mod_temp.diff_level, -1);
- break;
-
- case GDW_ACCEPT: { // Save button - save changes
- GDType btn, val;
- for (btn = 0; btn != GAME_DIFFICULTY_NUM; btn++) {
- val = ((GDType*)&_opt_mod_temp.diff)[btn];
- /* if setting has changed, change it */
- if (val != ((GDType*)&_opt_ptr->diff)[btn])
- DoCommandP(0, btn, val, NULL, CMD_CHANGE_DIFFICULTY_LEVEL);
- }
- DoCommandP(0, UINT_MAX, _opt_mod_temp.diff_level, NULL, CMD_CHANGE_DIFFICULTY_LEVEL);
- delete w;
- /* If we are in the editor, we should reload the economy.
- * This way when you load a game, the max loan and interest rate
- * are loaded correctly. */
- if (_game_mode == GM_EDITOR) StartupEconomy();
- break;
- }
-
- case GDW_CANCEL: // Cancel button - close window, abandon changes
- delete w;
- break;
- } break;
-
- case WE_TICK: /* Handle the visual 'clicking' of the buttons */
- if (diffic_d->timeout != 0) {
- diffic_d->timeout--;
- if (diffic_d->timeout == 0) diffic_d->clicked_button = NO_SETTINGS_BUTTON;
- w->SetDirty();
- }
- break;
- }
-}
-#undef DIFF_INGAME_DISABLED_BUTTONS
-
/* Widget definition for the game difficulty settings window */
static const Widget _game_difficulty_widgets[] = {
{ WWT_CLOSEBOX, RESIZE_NONE, 10, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, // GDW_CLOSEBOX
@@ -689,16 +467,198 @@
WC_GAME_OPTIONS, WC_NONE,
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
_game_difficulty_widgets,
- GameDifficultyWndProc
+};
+
+struct GameDifficultyWindow : public Window {
+private:
+ bool clicked_increase;
+ uint8 clicked_button;
+ uint8 timeout;
+
+ /* Temporary holding place of values in the difficulty window until 'Save' is clicked */
+ GameOptions opt_mod_temp;
+
+ enum {
+ GAMEDIFF_WND_TOP_OFFSET = 45,
+ GAMEDIFF_WND_ROWSIZE = 9,
+ // 0x383E = (1 << 13) | (1 << 12) | (1 << 11) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1)
+ DIFF_INGAME_DISABLED_BUTTONS = 0x383E,
+ NO_SETTINGS_BUTTON = 0xFF,
+ };
+
+ /* Names of the game difficulty settings window */
+ enum GameDifficultyWidgets {
+ GDW_CLOSEBOX = 0,
+ GDW_CAPTION,
+ GDW_UPPER_BG,
+ GDW_LVL_EASY,
+ GDW_LVL_MEDIUM,
+ GDW_LVL_HARD,
+ GDW_LVL_CUSTOM,
+ GDW_HIGHSCORE,
+ GDW_SETTING_BG,
+ GDW_LOWER_BG,
+ GDW_ACCEPT,
+ GDW_CANCEL,
+ };
+
+public:
+ GameDifficultyWindow() : Window(&_game_difficulty_desc)
+ {
+ /* Copy current settings (ingame or in intro) to temporary holding place
+ * change that when setting stuff, copy back on clicking 'OK' */
+ this->opt_mod_temp = *_opt_ptr;
+ this->clicked_increase = false;
+ this->clicked_button = NO_SETTINGS_BUTTON;
+ this->timeout = 0;
+ /* Hide the closebox to make sure that the user aborts or confirms his changes */
+ this->HideWidget(GDW_CLOSEBOX);
+ this->widget[GDW_CAPTION].left = 0;
+ /* Setup disabled buttons when creating window
+ * disable all other difficulty buttons during gameplay except for 'custom' */
+ this->SetWidgetsDisabledState(_game_mode == GM_NORMAL,
+ GDW_LVL_EASY,
+ GDW_LVL_MEDIUM,
+ GDW_LVL_HARD,
+ GDW_LVL_CUSTOM,
+ WIDGET_LIST_END);
+ this->SetWidgetDisabledState(GDW_HIGHSCORE, _game_mode == GM_EDITOR || _networking); // highscore chart in multiplayer
+ this->SetWidgetDisabledState(GDW_ACCEPT, _networking && !_network_server); // Save-button in multiplayer (and if client)
+ this->LowerWidget(GDW_LVL_EASY + this->opt_mod_temp.diff_level);
+ this->FindWindowPlacementAndResize(&_game_difficulty_desc);
+ }
+
+ virtual void OnPaint()
+ {
+ this->DrawWidgets();
+
+ /* XXX - Disabled buttons in normal gameplay or during muliplayer as non server.
+ * Bitshifted for each button to see if that bit is set. If it is set, the
+ * button is disabled */
+ uint32 disabled = 0;
+ if (_networking && !_network_server) {
+ disabled = MAX_UVALUE(uint32); // Disable all
+ } else if (_game_mode == GM_NORMAL) {
+ disabled = DIFF_INGAME_DISABLED_BUTTONS;
+ }
+
+ int value;
+ int y = GAMEDIFF_WND_TOP_OFFSET;
+ for (uint i = 0; i != GAME_DIFFICULTY_NUM; i++) {
+ const GameSettingData *gsd = &_game_setting_info[i];
+ value = ((GDType*)&this->opt_mod_temp.diff)[i];
+
+ DrawArrowButtons(5, y, 3,
+ (this->clicked_button == i) ? 1 + !!this->clicked_increase : 0,
+ !(HasBit(disabled, i) || gsd->min == value),
+ !(HasBit(disabled, i) || gsd->max == value));
+
+ value += _game_setting_info[i].str;
+ if (i == 4) value *= 1000; // XXX - handle currency option
+ SetDParam(0, value);
+ DrawString(30, y, STR_6805_MAXIMUM_NO_COMPETITORS + i, TC_FROMSTRING);
+
+ y += GAMEDIFF_WND_ROWSIZE + 2; // space items apart a bit
+ }
+ }
+
+ virtual void OnClick(Point pt, int widget)
+ {
+ switch (widget) {
+ case GDW_SETTING_BG: { /* Difficulty settings widget, decode click */
+ /* Don't allow clients to make any changes */
+ if (_networking && !_network_server) return;
+
+ const int x = pt.x - 5;
+ if (!IsInsideMM(x, 0, 21)) return; // Button area
+
+ const int y = pt.y - GAMEDIFF_WND_TOP_OFFSET;
+ if (y < 0) return;
+
+ /* Get button from Y coord. */
+ const uint8 btn = y / (GAMEDIFF_WND_ROWSIZE + 2);
+ if (btn >= GAME_DIFFICULTY_NUM || y % (GAMEDIFF_WND_ROWSIZE + 2) >= 9) return;
+
+ /* Clicked disabled button? */
+ if (_game_mode == GM_NORMAL && HasBit((int)DIFF_INGAME_DISABLED_BUTTONS, btn)) return;
+
+ this->timeout = 5;
+
+ int16 val = ((GDType*)&this->opt_mod_temp.diff)[btn];
+
+ const GameSettingData *info = &_game_setting_info[btn]; // get information about the difficulty setting
+ if (x >= 10) {
+ /* Increase button clicked */
+ val = min(val + info->step, info->max);
+ this->clicked_increase = true;
+ } else {
+ /* Decrease button clicked */
+ val -= info->step;
+ val = max(val, info->min);
+ this->clicked_increase = false;
+ }
+ this->clicked_button = btn;
+
+ /* save value in temporary variable */
+ ((GDType*)&this->opt_mod_temp.diff)[btn] = val;
+ this->RaiseWidget(GDW_LVL_EASY + this->opt_mod_temp.diff_level);
+ SetDifficultyLevel(3, &this->opt_mod_temp); // set difficulty level to custom
+ this->LowerWidget(GDW_LVL_CUSTOM);
+ this->SetDirty();
+ } break;
+
+ case GDW_LVL_EASY:
+ case GDW_LVL_MEDIUM:
+ case GDW_LVL_HARD:
+ case GDW_LVL_CUSTOM:
+ /* temporarily change difficulty level */
+ this->RaiseWidget(GDW_LVL_EASY + this->opt_mod_temp.diff_level);
+ SetDifficultyLevel(widget - GDW_LVL_EASY, &this->opt_mod_temp);
+ this->LowerWidget(GDW_LVL_EASY + this->opt_mod_temp.diff_level);
+ this->SetDirty();
+ break;
+
+ case GDW_HIGHSCORE: // Highscore Table
+ ShowHighscoreTable(this->opt_mod_temp.diff_level, -1);
+ break;
+
+ case GDW_ACCEPT: { // Save button - save changes
+ GDType btn, val;
+ for (btn = 0; btn != GAME_DIFFICULTY_NUM; btn++) {
+ val = ((GDType*)&this->opt_mod_temp.diff)[btn];
+ /* if setting has changed, change it */
+ if (val != ((GDType*)&_opt_ptr->diff)[btn])
+ DoCommandP(0, btn, val, NULL, CMD_CHANGE_DIFFICULTY_LEVEL);
+ }
+ DoCommandP(0, UINT_MAX, this->opt_mod_temp.diff_level, NULL, CMD_CHANGE_DIFFICULTY_LEVEL);
+ delete this;
+ /* If we are in the editor, we should reload the economy.
+ * This way when you load a game, the max loan and interest rate
+ * are loaded correctly. */
+ if (_game_mode == GM_EDITOR) StartupEconomy();
+ break;
+ }
+
+ 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);
- /* Copy current settings (ingame or in intro) to temporary holding place
- * change that when setting stuff, copy back on clicking 'OK' */
- _opt_mod_temp = *_opt_ptr;
- new Window(&_game_difficulty_desc);
+ new GameDifficultyWindow();
}
static const char *_patches_ui[] = {
@@ -854,228 +814,232 @@
PATCHSEL_COMPETITORS
};
-/** The main patches window. Shows a number of categories on top and
- * 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)
-{
+struct PatchesSelectionWindow : Window {
static Patches *patches_ptr;
- static int patches_max = 0;
-
- switch (e->event) {
- case WE_CREATE: {
- static bool first_time = true;
-
- patches_ptr = (_game_mode == GM_MENU) ? &_patches_newgame : &_patches;
-
- /* Build up the dynamic settings-array only once per OpenTTD session */
- if (first_time) {
- PatchPage *page;
- for (page = &_patches_page[0]; page != endof(_patches_page); page++) {
- uint i;
-
- if (patches_max < page->num) patches_max = page->num;
-
- page->entries = MallocT<PatchEntry>(page->num);
- for (i = 0; i != page->num; i++) {
- uint index;
- const SettingDesc *sd = GetPatchFromName(page->names[i], &index);
- assert(sd != NULL);
-
- page->entries[i].setting = sd;
- page->entries[i].index = index;
- }
- }
- first_time = false;
- }
-
- /* Resize the window to fit the largest patch tab */
- ResizeWindowForWidget(w, PATCHSEL_OPTIONSPANEL, 0, patches_max * 11);
-
- /* Recentre the window for the new size */
- w->top = w->top - (patches_max * 11) / 2;
-
- w->LowerWidget(4);
- } break;
+ static int patches_max;
- case WE_PAINT: {
- int x, y;
- const PatchPage *page = &_patches_page[WP(w, def_d).data_1];
- uint i;
-
- /* Set up selected category */
- DrawWindowWidgets(w);
-
- x = 5;
- y = 47;
- for (i = 0; i != page->num; i++) {
- const SettingDesc *sd = page->entries[i].setting;
- 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;
+ int page;
+ int entry;
+ int click;
- 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);
- } else {
- int32 value;
-
- value = (int32)ReadValue(var, sd->save.conv);
+ PatchesSelectionWindow(const WindowDesc *desc) : Window(desc)
+ {
+ static bool first_time = true;
- /* 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));
+ patches_ptr = (_game_mode == GM_MENU) ? &_patches_newgame : &_patches;
- disabled = (value == 0) && (sdb->flags & SGF_0ISDISABLED);
- if (disabled) {
- SetDParam(0, STR_CONFIG_PATCHES_DISABLED);
+ /* Build up the dynamic settings-array only once per OpenTTD session */
+ if (first_time) {
+ PatchPage *page;
+ for (page = &_patches_page[0]; page != endof(_patches_page); page++) {
+ uint i;
+
+ if (patches_max < page->num) patches_max = page->num;
+
+ page->entries = MallocT<PatchEntry>(page->num);
+ for (i = 0; i != page->num; i++) {
+ uint index;
+ const SettingDesc *sd = GetPatchFromName(page->names[i], &index);
+ assert(sd != NULL);
+
+ page->entries[i].setting = sd;
+ page->entries[i].index = index;
+ }
+ }
+ first_time = false;
+ }
+
+ /* Resize the window to fit the largest patch tab */
+ ResizeWindowForWidget(this, PATCHSEL_OPTIONSPANEL, 0, patches_max * 11);
+
+ /* Recentre the window for the new size */
+ this->top = this->top - (patches_max * 11) / 2;
+
+ this->LowerWidget(4);
+
+ this->FindWindowPlacementAndResize(desc);
+ }
+
+ virtual void OnPaint()
+ {
+ int x, y;
+ const PatchPage *page = &_patches_page[this->page];
+ uint i;
+
+ /* Set up selected category */
+ this->DrawWidgets();
+
+ x = 5;
+ y = 47;
+ for (i = 0; i != page->num; i++) {
+ const SettingDesc *sd = page->entries[i].setting;
+ 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);
+ } else {
+ int32 value;
+
+ value = (int32)ReadValue(var, sd->save.conv);
+
+ /* Draw [<][>] boxes for settings of an integer-type */
+ DrawArrowButtons(x, y, 3, this->click - (i * 2), (editable && value != sdb->min), (editable && value != sdb->max));
+
+ disabled = (value == 0) && (sdb->flags & SGF_0ISDISABLED);
+ if (disabled) {
+ SetDParam(0, STR_CONFIG_PATCHES_DISABLED);
+ } else {
+ if (sdb->flags & SGF_CURRENCY) {
+ SetDParam(0, STR_CONFIG_PATCHES_CURRENCY);
+ } else if (sdb->flags & SGF_MULTISTRING) {
+ SetDParam(0, sdb->str + value + 1);
} else {
- if (sdb->flags & SGF_CURRENCY) {
- SetDParam(0, STR_CONFIG_PATCHES_CURRENCY);
- } else if (sdb->flags & SGF_MULTISTRING) {
- SetDParam(0, sdb->str + value + 1);
+ SetDParam(0, (sdb->flags & SGF_NOCOMMA) ? STR_CONFIG_PATCHES_INT32 : STR_7024);
+ }
+ SetDParam(1, value);
+ }
+ }
+ DrawString(30, y, (sdb->str) + disabled, TC_FROMSTRING);
+ y += 11;
+ }
+ }
+
+ virtual void OnClick(Point pt, int widget)
+ {
+ switch (widget) {
+ case PATCHSEL_OPTIONSPANEL: {
+ const PatchPage *page = &_patches_page[this->page];
+ const SettingDesc *sd;
+ void *var;
+ int32 value;
+ int x, y;
+ byte btn;
+
+ y = pt.y - 46 - 1;
+ if (y < 0) return;
+
+ x = pt.x - 5;
+ if (x < 0) return;
+
+ btn = y / 11;
+ if (y % 11 > 9) return;
+ if (btn >= page->num) return;
+
+ sd = page->entries[btn].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;
+
+ var = GetVariableAddress(patches_ptr, &sd->save);
+ 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_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;
+
+ // don't allow too fast scrolling
+ if ((this->flags4 & WF_TIMEOUT_MASK) > 2 << WF_TIMEOUT_SHL) {
+ _left_button_clicked = false;
+ return;
+ }
+
+ /* Increase or decrease the value and clamp it to extremes */
+ if (x >= 10) {
+ value += step;
+ if (value > sdb->max) value = sdb->max;
} else {
- SetDParam(0, (sdb->flags & SGF_NOCOMMA) ? STR_CONFIG_PATCHES_INT32 : STR_7024);
+ value -= step;
+ if (value < sdb->min) value = (sdb->flags & SGF_0ISDISABLED) ? 0 : sdb->min;
}
- SetDParam(1, value);
+
+ /* 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 |= 5 << WF_TIMEOUT_SHL;
+ _left_button_clicked = false;
+ }
+ } break;
+ default: NOT_REACHED();
+ }
+
+ if (value != oldvalue) {
+ SetPatchValue(page->entries[btn].index, patches_ptr, 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);
}
}
- DrawString(30, y, (sdb->str) + disabled, TC_FROMSTRING);
- y += 11;
- }
- } break;
-
- case WE_CLICK:
- switch (e->we.click.widget) {
- case PATCHSEL_OPTIONSPANEL: {
- const PatchPage *page = &_patches_page[WP(w, def_d).data_1];
- const SettingDesc *sd;
- void *var;
- int32 value;
- int x, y;
- byte btn;
-
- y = e->we.click.pt.y - 46 - 1;
- if (y < 0) return;
-
- x = e->we.click.pt.x - 5;
- if (x < 0) return;
-
- btn = y / 11;
- if (y % 11 > 9) return;
- if (btn >= page->num) return;
-
- sd = page->entries[btn].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;
-
- var = GetVariableAddress(patches_ptr, &sd->save);
- 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_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;
-
- // don't allow too fast scrolling
- if ((w->flags4 & WF_TIMEOUT_MASK) > 2 << WF_TIMEOUT_SHL) {
- _left_button_clicked = false;
- return;
- }
+ } break;
- /* Increase or decrease the value and clamp it to extremes */
- if (x >= 10) {
- value += step;
- if (value > sdb->max) value = sdb->max;
- } 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)) {
- WP(w, def_d).data_2 = btn * 2 + 1 + ((x >= 10) ? 1 : 0);
- w->flags4 |= 5 << WF_TIMEOUT_SHL;
- _left_button_clicked = false;
- }
- } break;
- default: NOT_REACHED();
- }
-
- if (value != oldvalue) {
- SetPatchValue(page->entries[btn].index, patches_ptr, value);
- w->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;
+ 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);
+ DeleteWindowById(WC_QUERY_STRING, 0);
+ this->SetDirty();
+ break;
+ }
+ }
- WP(w, def_d).data_3 = btn;
- SetDParam(0, value);
- ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_CONFIG_PATCHES_QUERY_CAPT, 10, 100, w, CS_NUMERAL);
- }
- }
- } break;
-
- case PATCHSEL_INTERFACE: case PATCHSEL_CONSTRUCTION: case PATCHSEL_VEHICLES:
- case PATCHSEL_STATIONS: case PATCHSEL_ECONOMY: case PATCHSEL_COMPETITORS:
- w->RaiseWidget(WP(w, def_d).data_1 + PATCHSEL_INTERFACE);
- WP(w, def_d).data_1 = e->we.click.widget - PATCHSEL_INTERFACE;
- w->LowerWidget(WP(w, def_d).data_1 + PATCHSEL_INTERFACE);
- DeleteWindowById(WC_QUERY_STRING, 0);
- w->SetDirty();
- break;
- }
- break;
+ virtual void OnTimeout()
+ {
+ this->click = 0;
+ this->SetDirty();
+ }
- case WE_TIMEOUT:
- WP(w, def_d).data_2 = 0;
- w->SetDirty();
- break;
-
- case WE_ON_EDIT_TEXT:
- if (!StrEmpty(e->we.edittext.str)) {
- const PatchEntry *pe = &_patches_page[WP(w, def_d).data_1].entries[WP(w, def_d).data_3];
- const SettingDesc *sd = pe->setting;
- int32 value = atoi(e->we.edittext.str);
+ virtual void OnQueryTextFinished(char *str)
+ {
+ if (!StrEmpty(str)) {
+ const PatchEntry *pe = &_patches_page[this->page].entries[this->entry];
+ const SettingDesc *sd = pe->setting;
+ int32 value = atoi(str);
- /* Save the correct currency-translated value */
- if (sd->desc.flags & SGF_CURRENCY) value /= _currency->rate;
+ /* Save the correct currency-translated value */
+ if (sd->desc.flags & SGF_CURRENCY) value /= _currency->rate;
- SetPatchValue(pe->index, patches_ptr, value);
- w->SetDirty();
- }
- break;
+ SetPatchValue(pe->index, patches_ptr, value);
+ this->SetDirty();
+ }
+ }
+};
- 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[] = {
{ WWT_CLOSEBOX, RESIZE_NONE, 10, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
@@ -1097,13 +1061,12 @@
WC_GAME_OPTIONS, WC_NONE,
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
_patches_selection_widgets,
- PatchesSelectionWndProc,
};
void ShowPatchesSelection()
{
DeleteWindowById(WC_GAME_OPTIONS, 0);
- new Window(&_patches_selection_desc);
+ new PatchesSelectionWindow(&_patches_selection_desc);
}
@@ -1143,176 +1106,178 @@
CUSTCURR_TO_EURO,
};
-static char _str_separator[2];
-
-static void CustCurrencyWndProc(Window *w, WindowEvent *e)
-{
- switch (e->event) {
- case WE_PAINT: {
- int x;
- int y = 20;
- int clk = WP(w, def_d).data_1;
- DrawWindowWidgets(w);
-
- /* exchange rate */
- DrawArrowButtons(10, y, 3, GB(clk, 0, 2), true, true);
- SetDParam(0, 1);
- SetDParam(1, 1);
- DrawString(35, y + 1, STR_CURRENCY_EXCHANGE_RATE, TC_FROMSTRING);
- y += 12;
-
- /* separator */
- DrawFrameRect(10, y + 1, 29, y + 9, 0, GB(clk, 2, 2) ? FR_LOWERED : FR_NONE);
- x = DrawString(35, y + 1, STR_CURRENCY_SEPARATOR, TC_FROMSTRING);
- DoDrawString(_str_separator, x + 4, y + 1, TC_ORANGE);
- y += 12;
-
- /* prefix */
- DrawFrameRect(10, y + 1, 29, y + 9, 0, GB(clk, 4, 2) ? FR_LOWERED : FR_NONE);
- x = DrawString(35, y + 1, STR_CURRENCY_PREFIX, TC_FROMSTRING);
- DoDrawString(_custom_currency.prefix, x + 4, y + 1, TC_ORANGE);
- y += 12;
-
- /* suffix */
- DrawFrameRect(10, y + 1, 29, y + 9, 0, GB(clk, 6, 2) ? FR_LOWERED : FR_NONE);
- x = DrawString(35, y + 1, STR_CURRENCY_SUFFIX, TC_FROMSTRING);
- DoDrawString(_custom_currency.suffix, x + 4, y + 1, TC_ORANGE);
- y += 12;
-
- /* switch to euro */
- DrawArrowButtons(10, y, 3, GB(clk, 8, 2), true, true);
- 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);
- y += 12;
+struct CustomCurrencyWindow : Window {
+ char separator[2];
+ int click;
+ int query_widget;
- /* Preview */
- y += 12;
- SetDParam(0, 10000);
- DrawString(35, y + 1, STR_CURRENCY_PREVIEW, TC_FROMSTRING);
- } break;
-
- case WE_CLICK: {
- int line = (e->we.click.pt.y - 20) / 12;
- int len = 0;
- int x = e->we.click.pt.x;
- StringID str = 0;
- CharSetFilter afilter = CS_ALPHANUMERAL;
+ CustomCurrencyWindow(const WindowDesc *desc) : Window(desc)
+ {
+ this->separator[0] = _custom_currency.separator;
+ this->separator[1] = '\0';
+ this->FindWindowPlacementAndResize(desc);
+ }
- switch (line) {
- case CUSTCURR_EXCHANGERATE:
- if (IsInsideMM(x, 10, 30)) { // clicked buttons
- if (x < 20) {
- if (_custom_currency.rate > 1) _custom_currency.rate--;
- WP(w, def_d).data_1 = 1 << (line * 2 + 0);
- } else {
- if (_custom_currency.rate < 5000) _custom_currency.rate++;
- WP(w, def_d).data_1 = 1 << (line * 2 + 1);
- }
- } else { // enter text
- SetDParam(0, _custom_currency.rate);
- str = STR_CONFIG_PATCHES_INT32;
- len = 4;
- afilter = CS_NUMERAL;
- }
- break;
+ virtual void OnPaint()
+ {
+ int x;
+ int y = 20;
+ this->DrawWidgets();
- case CUSTCURR_SEPARATOR:
- if (IsInsideMM(x, 10, 30)) { // clicked button
- WP(w, def_d).data_1 = 1 << (line * 2 + 1);
- }
- str = BindCString(_str_separator);
- len = 1;
- break;
+ /* exchange rate */
+ DrawArrowButtons(10, y, 3, GB(this->click, 0, 2), true, true);
+ SetDParam(0, 1);
+ SetDParam(1, 1);
+ DrawString(35, y + 1, STR_CURRENCY_EXCHANGE_RATE, TC_FROMSTRING);
+ y += 12;
- case CUSTCURR_PREFIX:
- if (IsInsideMM(x, 10, 30)) { // clicked button
- WP(w, def_d).data_1 = 1 << (line * 2 + 1);
+ /* separator */
+ 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);
+ DoDrawString(this->separator, x + 4, y + 1, TC_ORANGE);
+ y += 12;
+
+ /* prefix */
+ 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);
+ DoDrawString(_custom_currency.prefix, x + 4, y + 1, TC_ORANGE);
+ y += 12;
+
+ /* suffix */
+ 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);
+ DoDrawString(_custom_currency.suffix, x + 4, y + 1, TC_ORANGE);
+ y += 12;
+
+ /* switch to euro */
+ DrawArrowButtons(10, y, 3, GB(this->click, 8, 2), true, true);
+ 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);
+ y += 12;
+
+ /* Preview */
+ y += 12;
+ SetDParam(0, 10000);
+ DrawString(35, y + 1, STR_CURRENCY_PREVIEW, TC_FROMSTRING);
+ }
+
+ virtual void OnClick(Point pt, int widget)
+ {
+ int line = (pt.y - 20) / 12;
+ int len = 0;
+ int x = pt.x;
+ StringID str = 0;
+ CharSetFilter afilter = CS_ALPHANUMERAL;
+
+ switch (line) {
+ case CUSTCURR_EXCHANGERATE:
+ if (IsInsideMM(x, 10, 30)) { // clicked buttons
+ if (x < 20) {
+ if (_custom_currency.rate > 1) _custom_currency.rate--;
+ this->click = 1 << (line * 2 + 0);
+ } else {
+ if (_custom_currency.rate < 5000) _custom_currency.rate++;
+ this->click = 1 << (line * 2 + 1);
}
- str = BindCString(_custom_currency.prefix);
- len = 12;
- break;
-
- case CUSTCURR_SUFFIX:
- if (IsInsideMM(x, 10, 30)) { // clicked button
- WP(w, def_d).data_1 = 1 << (line * 2 + 1);
- }
- str = BindCString(_custom_currency.suffix);
- len = 12;
- break;
-
- case CUSTCURR_TO_EURO:
- if (IsInsideMM(x, 10, 30)) { // clicked buttons
- if (x < 20) {
- _custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ?
- CF_NOEURO : _custom_currency.to_euro - 1;
- WP(w, def_d).data_1 = 1 << (line * 2 + 0);
- } else {
- _custom_currency.to_euro =
- Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
- WP(w, def_d).data_1 = 1 << (line * 2 + 1);
- }
- } else { // enter text
- SetDParam(0, _custom_currency.to_euro);
- str = STR_CONFIG_PATCHES_INT32;
- len = 4;
- afilter = CS_NUMERAL;
- }
- break;
- }
-
- if (len != 0) {
- WP(w, def_d).data_2 = line;
- ShowQueryString(str, STR_CURRENCY_CHANGE_PARAMETER, len + 1, 250, w, afilter);
- }
-
- w->flags4 |= 5 << WF_TIMEOUT_SHL;
- w->SetDirty();
- } break;
+ } else { // enter text
+ SetDParam(0, _custom_currency.rate);
+ str = STR_CONFIG_PATCHES_INT32;
+ len = 4;
+ afilter = CS_NUMERAL;
+ }
+ break;
- case WE_ON_EDIT_TEXT: {
- if (e->we.edittext.str == NULL) break;
-
- const char *b = e->we.edittext.str;
-
- switch (WP(w, def_d).data_2) {
- case CUSTCURR_EXCHANGERATE:
- _custom_currency.rate = Clamp(atoi(b), 1, 5000);
- break;
-
- case CUSTCURR_SEPARATOR: /* Thousands seperator */
- _custom_currency.separator = StrEmpty(b) ? ' ' : b[0];
- ttd_strlcpy(_str_separator, b, lengthof(_str_separator));
- break;
-
- case CUSTCURR_PREFIX:
- ttd_strlcpy(_custom_currency.prefix, b, lengthof(_custom_currency.prefix));
- break;
+ case CUSTCURR_SEPARATOR:
+ if (IsInsideMM(x, 10, 30)) { // clicked button
+ this->click = 1 << (line * 2 + 1);
+ }
+ str = BindCString(this->separator);
+ len = 1;
+ break;
- case CUSTCURR_SUFFIX:
- ttd_strlcpy(_custom_currency.suffix, b, lengthof(_custom_currency.suffix));
- break;
+ case CUSTCURR_PREFIX:
+ if (IsInsideMM(x, 10, 30)) { // clicked button
+ this->click = 1 << (line * 2 + 1);
+ }
+ str = BindCString(_custom_currency.prefix);
+ len = 12;
+ break;
- case CUSTCURR_TO_EURO: { /* Year to switch to euro */
- int val = atoi(b);
+ case CUSTCURR_SUFFIX:
+ if (IsInsideMM(x, 10, 30)) { // clicked button
+ this->click = 1 << (line * 2 + 1);
+ }
+ str = BindCString(_custom_currency.suffix);
+ len = 12;
+ break;
- _custom_currency.to_euro = (val < 2000 ? CF_NOEURO : min(val, MAX_YEAR));
- break;
+ case CUSTCURR_TO_EURO:
+ if (IsInsideMM(x, 10, 30)) { // clicked buttons
+ if (x < 20) {
+ _custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ?
+ CF_NOEURO : _custom_currency.to_euro - 1;
+ this->click = 1 << (line * 2 + 0);
+ } else {
+ _custom_currency.to_euro =
+ Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
+ this->click = 1 << (line * 2 + 1);
+ }
+ } else { // enter text
+ SetDParam(0, _custom_currency.to_euro);
+ str = STR_CONFIG_PATCHES_INT32;
+ len = 4;
+ afilter = CS_NUMERAL;
}
+ break;
+ }
+
+ if (len != 0) {
+ this->query_widget = line;
+ ShowQueryString(str, STR_CURRENCY_CHANGE_PARAMETER, len + 1, 250, this, afilter);
+ }
+
+ this->flags4 |= 5 << WF_TIMEOUT_SHL;
+ this->SetDirty();
+ }
+
+ virtual void OnQueryTextFinished(char *str)
+ {
+ if (str == NULL) return;
+
+ switch (this->query_widget) {
+ case CUSTCURR_EXCHANGERATE:
+ _custom_currency.rate = Clamp(atoi(str), 1, 5000);
+ break;
+
+ case CUSTCURR_SEPARATOR: /* Thousands seperator */
+ _custom_currency.separator = StrEmpty(str) ? ' ' : str[0];
+ ttd_strlcpy(this->separator, str, lengthof(this->separator));
+ break;
+
+ case CUSTCURR_PREFIX:
+ ttd_strlcpy(_custom_currency.prefix, str, lengthof(_custom_currency.prefix));
+ break;
+
+ case CUSTCURR_SUFFIX:
+ ttd_strlcpy(_custom_currency.suffix, str, lengthof(_custom_currency.suffix));
+ break;
+
+ case CUSTCURR_TO_EURO: { /* Year to switch to euro */
+ int val = atoi(str);
+
+ _custom_currency.to_euro = (val < 2000 ? CF_NOEURO : min(val, MAX_YEAR));
+ break;
}
- MarkWholeScreenDirty();
- } break;
+ }
+ MarkWholeScreenDirty();
+ }
- case WE_TIMEOUT:
- WP(w, def_d).data_1 = 0;
- w->SetDirty();
- break;
-
- case WE_DESTROY:
- DeleteWindowById(WC_QUERY_STRING, 0);
- MarkWholeScreenDirty();
- break;
+ virtual void OnTimeout()
+ {
+ this->click = 0;
+ this->SetDirty();
}
-}
+};
static const Widget _cust_currency_widgets[] = {
{ WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
@@ -1326,14 +1291,10 @@
WC_CUSTOM_CURRENCY, WC_NONE,
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
_cust_currency_widgets,
- CustCurrencyWndProc,
};
static void ShowCustCurrency()
{
- _str_separator[0] = _custom_currency.separator;
- _str_separator[1] = '\0';
-
DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
- new Window(&_cust_currency_desc);
+ new CustomCurrencyWindow(&_cust_currency_desc);
}