00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "currency.h"
00014 #include "gui.h"
00015 #include "textbuf_gui.h"
00016 #include "command_func.h"
00017 #include "screenshot.h"
00018 #include "network/network.h"
00019 #include "town.h"
00020 #include "settings_internal.h"
00021 #include "newgrf_townname.h"
00022 #include "strings_func.h"
00023 #include "window_func.h"
00024 #include "string_func.h"
00025 #include "widgets/dropdown_type.h"
00026 #include "widgets/dropdown_func.h"
00027 #include "highscore.h"
00028 #include "base_media_base.h"
00029 #include "company_base.h"
00030 #include "company_func.h"
00031 #include "viewport_func.h"
00032 #include "core/geometry_func.hpp"
00033 #include "ai/ai.hpp"
00034 #include "language.h"
00035
00036 #include "table/sprites.h"
00037 #include "table/strings.h"
00038
00039 static const StringID _units_dropdown[] = {
00040 STR_GAME_OPTIONS_MEASURING_UNITS_IMPERIAL,
00041 STR_GAME_OPTIONS_MEASURING_UNITS_METRIC,
00042 STR_GAME_OPTIONS_MEASURING_UNITS_SI,
00043 INVALID_STRING_ID
00044 };
00045
00046 static const StringID _driveside_dropdown[] = {
00047 STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_LEFT,
00048 STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_RIGHT,
00049 INVALID_STRING_ID
00050 };
00051
00052 static const StringID _autosave_dropdown[] = {
00053 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_OFF,
00054 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_1_MONTH,
00055 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_3_MONTHS,
00056 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_6_MONTHS,
00057 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_12_MONTHS,
00058 INVALID_STRING_ID,
00059 };
00060
00067 static StringID *BuildDynamicDropdown(StringID base, int num)
00068 {
00069 static StringID buf[32 + 1];
00070 StringID *p = buf;
00071 while (--num >= 0) *p++ = base++;
00072 *p = INVALID_STRING_ID;
00073 return buf;
00074 }
00075
00076 int _nb_orig_names = SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1;
00077 static StringID *_grf_names = NULL;
00078 static int _nb_grf_names = 0;
00079
00081 void InitGRFTownGeneratorNames()
00082 {
00083 free(_grf_names);
00084 _grf_names = GetGRFTownNameList();
00085 _nb_grf_names = 0;
00086 for (StringID *s = _grf_names; *s != INVALID_STRING_ID; s++) _nb_grf_names++;
00087 }
00088
00094 static inline StringID TownName(int town_name)
00095 {
00096 if (town_name < _nb_orig_names) return STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + town_name;
00097 town_name -= _nb_orig_names;
00098 if (town_name < _nb_grf_names) return _grf_names[town_name];
00099 return STR_UNDEFINED;
00100 }
00101
00106 static int GetCurRes()
00107 {
00108 int i;
00109
00110 for (i = 0; i != _num_resolutions; i++) {
00111 if ((int)_resolutions[i].width == _screen.width &&
00112 (int)_resolutions[i].height == _screen.height) {
00113 break;
00114 }
00115 }
00116 return i;
00117 }
00118
00120 enum GameOptionsWidgets {
00121 GOW_BACKGROUND,
00122 GOW_CURRENCY_DROPDOWN,
00123 GOW_DISTANCE_DROPDOWN,
00124 GOW_ROADSIDE_DROPDOWN,
00125 GOW_TOWNNAME_DROPDOWN,
00126 GOW_AUTOSAVE_DROPDOWN,
00127 GOW_LANG_DROPDOWN,
00128 GOW_RESOLUTION_DROPDOWN,
00129 GOW_FULLSCREEN_BUTTON,
00130 GOW_SCREENSHOT_DROPDOWN,
00131 GOW_BASE_GRF_DROPDOWN,
00132 GOW_BASE_GRF_STATUS,
00133 GOW_BASE_GRF_DESCRIPTION,
00134 GOW_BASE_SFX_DROPDOWN,
00135 GOW_BASE_SFX_DESCRIPTION,
00136 GOW_BASE_MUSIC_DROPDOWN,
00137 GOW_BASE_MUSIC_STATUS,
00138 GOW_BASE_MUSIC_DESCRIPTION,
00139 };
00140
00146 static void ShowTownnameDropdown(Window *w, int sel)
00147 {
00148 typedef std::map<StringID, int, StringIDCompare> TownList;
00149 TownList townnames;
00150
00151
00152 for (int i = 0; i < _nb_orig_names; i++) townnames[STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + i] = i;
00153
00154
00155 for (int i = 0; i < _nb_grf_names; i++) townnames[_grf_names[i]] = _nb_orig_names + i;
00156
00157 DropDownList *list = new DropDownList();
00158 for (TownList::iterator it = townnames.begin(); it != townnames.end(); it++) {
00159 list->push_back(new DropDownListStringItem((*it).first, (*it).second, !(_game_mode == GM_MENU || Town::GetNumItems() == 0 || (*it).second == sel)));
00160 }
00161
00162 ShowDropDownList(w, list, sel, GOW_TOWNNAME_DROPDOWN);
00163 }
00164
00165 static void ShowCustCurrency();
00166
00167 template <class T>
00168 static void ShowSetMenu(Window *w, int widget)
00169 {
00170 int n = T::GetNumSets();
00171 int current = T::GetIndexOfUsedSet();
00172
00173 DropDownList *list = new DropDownList();
00174 for (int i = 0; i < n; i++) {
00175 list->push_back(new DropDownListCharStringItem(T::GetSet(i)->name, i, (_game_mode == GM_MENU) ? false : (current != i)));
00176 }
00177
00178 ShowDropDownList(w, list, current, widget);
00179 }
00180
00181 struct GameOptionsWindow : Window {
00182 GameSettings *opt;
00183 bool reload;
00184
00185 GameOptionsWindow(const WindowDesc *desc) : Window()
00186 {
00187 this->opt = &GetGameSettings();
00188 this->reload = false;
00189
00190 this->InitNested(desc);
00191 this->OnInvalidateData(0);
00192 }
00193
00194 ~GameOptionsWindow()
00195 {
00196 DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
00197 if (this->reload) _switch_mode = SM_MENU;
00198 }
00199
00200 virtual void SetStringParameters(int widget) const
00201 {
00202 switch (widget) {
00203 case GOW_CURRENCY_DROPDOWN: SetDParam(0, _currency_specs[this->opt->locale.currency].name); break;
00204 case GOW_DISTANCE_DROPDOWN: SetDParam(0, STR_GAME_OPTIONS_MEASURING_UNITS_IMPERIAL + this->opt->locale.units); break;
00205 case GOW_ROADSIDE_DROPDOWN: SetDParam(0, STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_LEFT + this->opt->vehicle.road_side); break;
00206 case GOW_TOWNNAME_DROPDOWN: SetDParam(0, TownName(this->opt->game_creation.town_name)); break;
00207 case GOW_AUTOSAVE_DROPDOWN: SetDParam(0, _autosave_dropdown[_settings_client.gui.autosave]); break;
00208 case GOW_LANG_DROPDOWN: SetDParamStr(0, _current_language->own_name); break;
00209 case GOW_RESOLUTION_DROPDOWN: SetDParam(0, GetCurRes() == _num_resolutions ? STR_GAME_OPTIONS_RESOLUTION_OTHER : SPECSTR_RESOLUTION_START + GetCurRes()); break;
00210 case GOW_SCREENSHOT_DROPDOWN: SetDParam(0, SPECSTR_SCREENSHOT_START + _cur_screenshot_format); break;
00211 case GOW_BASE_GRF_DROPDOWN: SetDParamStr(0, BaseGraphics::GetUsedSet()->name); break;
00212 case GOW_BASE_GRF_STATUS: SetDParam(0, BaseGraphics::GetUsedSet()->GetNumInvalid()); break;
00213 case GOW_BASE_SFX_DROPDOWN: SetDParamStr(0, BaseSounds::GetUsedSet()->name); break;
00214 case GOW_BASE_MUSIC_DROPDOWN: SetDParamStr(0, BaseMusic::GetUsedSet()->name); break;
00215 case GOW_BASE_MUSIC_STATUS: SetDParam(0, BaseMusic::GetUsedSet()->GetNumInvalid()); break;
00216 }
00217 }
00218
00219 virtual void DrawWidget(const Rect &r, int widget) const
00220 {
00221 switch (widget) {
00222 case GOW_BASE_GRF_DESCRIPTION:
00223 SetDParamStr(0, BaseGraphics::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
00224 DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
00225 break;
00226
00227 case GOW_BASE_SFX_DESCRIPTION:
00228 SetDParamStr(0, BaseSounds::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
00229 DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
00230 break;
00231
00232 case GOW_BASE_MUSIC_DESCRIPTION:
00233 SetDParamStr(0, BaseMusic::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
00234 DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
00235 break;
00236 }
00237 }
00238
00239 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00240 {
00241 switch (widget) {
00242 case GOW_BASE_GRF_DESCRIPTION:
00243
00244 for (int i = 0; i < BaseGraphics::GetNumSets(); i++) {
00245 SetDParamStr(0, BaseGraphics::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
00246 size->height = max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
00247 }
00248 break;
00249
00250 case GOW_BASE_GRF_STATUS:
00251
00252 for (int i = 0; i < BaseGraphics::GetNumSets(); i++) {
00253 uint invalid_files = BaseGraphics::GetSet(i)->GetNumInvalid();
00254 if (invalid_files == 0) continue;
00255
00256 SetDParam(0, invalid_files);
00257 *size = maxdim(*size, GetStringBoundingBox(STR_GAME_OPTIONS_BASE_GRF_STATUS));
00258 }
00259 break;
00260
00261 case GOW_BASE_SFX_DESCRIPTION:
00262
00263 for (int i = 0; i < BaseSounds::GetNumSets(); i++) {
00264 SetDParamStr(0, BaseSounds::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
00265 size->height = max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
00266 }
00267 break;
00268
00269 case GOW_BASE_MUSIC_DESCRIPTION:
00270
00271 for (int i = 0; i < BaseMusic::GetNumSets(); i++) {
00272 SetDParamStr(0, BaseMusic::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
00273 size->height = max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
00274 }
00275 break;
00276
00277 case GOW_BASE_MUSIC_STATUS:
00278
00279 for (int i = 0; i < BaseMusic::GetNumSets(); i++) {
00280 uint invalid_files = BaseMusic::GetSet(i)->GetNumInvalid();
00281 if (invalid_files == 0) continue;
00282
00283 SetDParam(0, invalid_files);
00284 *size = maxdim(*size, GetStringBoundingBox(STR_GAME_OPTIONS_BASE_MUSIC_STATUS));
00285 }
00286 break;
00287 }
00288 }
00289
00290 virtual void OnClick(Point pt, int widget, int click_count)
00291 {
00292 switch (widget) {
00293 case GOW_CURRENCY_DROPDOWN:
00294 ShowDropDownMenu(this, BuildCurrencyDropdown(), this->opt->locale.currency, GOW_CURRENCY_DROPDOWN, _game_mode == GM_MENU ? 0 : ~GetMaskOfAllowedCurrencies(), 0);
00295 break;
00296
00297 case GOW_DISTANCE_DROPDOWN:
00298 ShowDropDownMenu(this, _units_dropdown, this->opt->locale.units, GOW_DISTANCE_DROPDOWN, 0, 0);
00299 break;
00300
00301 case GOW_ROADSIDE_DROPDOWN: {
00302 int i = 0;
00303 extern bool RoadVehiclesAreBuilt();
00304
00305
00306
00307 if ((_game_mode != GM_MENU && RoadVehiclesAreBuilt()) || (_networking && !_network_server)) {
00308 i = (-1) ^ (1 << this->opt->vehicle.road_side);
00309 }
00310
00311 ShowDropDownMenu(this, _driveside_dropdown, this->opt->vehicle.road_side, GOW_ROADSIDE_DROPDOWN, i, 0);
00312 break;
00313 }
00314
00315 case GOW_TOWNNAME_DROPDOWN:
00316 ShowTownnameDropdown(this, this->opt->game_creation.town_name);
00317 break;
00318
00319 case GOW_AUTOSAVE_DROPDOWN:
00320 ShowDropDownMenu(this, _autosave_dropdown, _settings_client.gui.autosave, GOW_AUTOSAVE_DROPDOWN, 0, 0);
00321 break;
00322
00323 case GOW_LANG_DROPDOWN: {
00324 typedef std::map<StringID, int, StringIDCompare> LangList;
00325
00326
00327 LangList langs;
00328 int current_lang = 0;
00329 for (int i = 0; i < (int)_languages.Length(); i++) {
00330 if (&_languages[i] == _current_language) current_lang = i;
00331 langs[SPECSTR_LANGUAGE_START + i] = i;
00332 }
00333
00334 DropDownList *list = new DropDownList();
00335 for (LangList::iterator it = langs.begin(); it != langs.end(); it++) {
00336 list->push_back(new DropDownListStringItem((*it).first, (*it).second, false));
00337 }
00338
00339 ShowDropDownList(this, list, current_lang, GOW_LANG_DROPDOWN);
00340 break;
00341 }
00342
00343 case GOW_RESOLUTION_DROPDOWN:
00344 ShowDropDownMenu(this, BuildDynamicDropdown(SPECSTR_RESOLUTION_START, _num_resolutions), GetCurRes(), GOW_RESOLUTION_DROPDOWN, 0, 0);
00345 break;
00346
00347 case GOW_FULLSCREEN_BUTTON:
00348
00349 if (!ToggleFullScreen(!_fullscreen)) {
00350 ShowErrorMessage(STR_ERROR_FULLSCREEN_FAILED, INVALID_STRING_ID, WL_ERROR);
00351 }
00352 this->SetWidgetLoweredState(GOW_FULLSCREEN_BUTTON, _fullscreen);
00353 this->SetDirty();
00354 break;
00355
00356 case GOW_SCREENSHOT_DROPDOWN:
00357 ShowDropDownMenu(this, BuildDynamicDropdown(SPECSTR_SCREENSHOT_START, _num_screenshot_formats), _cur_screenshot_format, GOW_SCREENSHOT_DROPDOWN, 0, 0);
00358 break;
00359
00360 case GOW_BASE_GRF_DROPDOWN:
00361 ShowSetMenu<BaseGraphics>(this, GOW_BASE_GRF_DROPDOWN);
00362 break;
00363
00364 case GOW_BASE_SFX_DROPDOWN:
00365 ShowSetMenu<BaseSounds>(this, GOW_BASE_SFX_DROPDOWN);
00366 break;
00367
00368 case GOW_BASE_MUSIC_DROPDOWN:
00369 ShowSetMenu<BaseMusic>(this, GOW_BASE_MUSIC_DROPDOWN);
00370 break;
00371 }
00372 }
00373
00379 template <class T>
00380 void SetMediaSet(int index)
00381 {
00382 if (_game_mode == GM_MENU) {
00383 const char *name = T::GetSet(index)->name;
00384
00385 free(const_cast<char *>(T::ini_set));
00386 T::ini_set = strdup(name);
00387
00388 T::SetSet(name);
00389 this->reload = true;
00390 this->InvalidateData();
00391 }
00392 }
00393
00394 virtual void OnDropdownSelect(int widget, int index)
00395 {
00396 switch (widget) {
00397 case GOW_CURRENCY_DROPDOWN:
00398 if (index == CUSTOM_CURRENCY_ID) ShowCustCurrency();
00399 this->opt->locale.currency = index;
00400 ReInitAllWindows();
00401 break;
00402
00403 case GOW_DISTANCE_DROPDOWN:
00404 this->opt->locale.units = index;
00405 MarkWholeScreenDirty();
00406 break;
00407
00408 case GOW_ROADSIDE_DROPDOWN:
00409 if (this->opt->vehicle.road_side != index) {
00410 uint i;
00411 if (GetSettingFromName("vehicle.road_side", &i) == NULL) NOT_REACHED();
00412 SetSettingValue(i, index);
00413 MarkWholeScreenDirty();
00414 }
00415 break;
00416
00417 case GOW_TOWNNAME_DROPDOWN:
00418 if (_game_mode == GM_MENU || Town::GetNumItems() == 0) {
00419 this->opt->game_creation.town_name = index;
00420 SetWindowDirty(WC_GAME_OPTIONS, 0);
00421 }
00422 break;
00423
00424 case GOW_AUTOSAVE_DROPDOWN:
00425 _settings_client.gui.autosave = index;
00426 this->SetDirty();
00427 break;
00428
00429 case GOW_LANG_DROPDOWN:
00430 ReadLanguagePack(&_languages[index]);
00431 DeleteWindowByClass(WC_QUERY_STRING);
00432 CheckForMissingGlyphsInLoadedLanguagePack();
00433 UpdateAllVirtCoords();
00434 ReInitAllWindows();
00435 break;
00436
00437 case GOW_RESOLUTION_DROPDOWN:
00438 if (index < _num_resolutions && ChangeResInGame(_resolutions[index].width, _resolutions[index].height)) {
00439 this->SetDirty();
00440 }
00441 break;
00442
00443 case GOW_SCREENSHOT_DROPDOWN:
00444 SetScreenshotFormat(index);
00445 this->SetDirty();
00446 break;
00447
00448 case GOW_BASE_GRF_DROPDOWN:
00449 this->SetMediaSet<BaseGraphics>(index);
00450 break;
00451
00452 case GOW_BASE_SFX_DROPDOWN:
00453 this->SetMediaSet<BaseSounds>(index);
00454 break;
00455
00456 case GOW_BASE_MUSIC_DROPDOWN:
00457 this->SetMediaSet<BaseMusic>(index);
00458 break;
00459 }
00460 }
00461
00462 virtual void OnInvalidateData(int data)
00463 {
00464 this->SetWidgetLoweredState(GOW_FULLSCREEN_BUTTON, _fullscreen);
00465
00466 bool missing_files = BaseGraphics::GetUsedSet()->GetNumMissing() == 0;
00467 this->GetWidget<NWidgetCore>(GOW_BASE_GRF_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_GRF_STATUS, STR_NULL);
00468
00469 missing_files = BaseMusic::GetUsedSet()->GetNumInvalid() == 0;
00470 this->GetWidget<NWidgetCore>(GOW_BASE_MUSIC_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_MUSIC_STATUS, STR_NULL);
00471 }
00472 };
00473
00474 static const NWidgetPart _nested_game_options_widgets[] = {
00475 NWidget(NWID_HORIZONTAL),
00476 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00477 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00478 EndContainer(),
00479 NWidget(WWT_PANEL, COLOUR_GREY, GOW_BACKGROUND), SetPIP(6, 6, 10),
00480 NWidget(NWID_HORIZONTAL), SetPIP(10, 10, 10),
00481 NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
00482 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CURRENCY_UNITS_FRAME, STR_NULL),
00483 NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_CURRENCY_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_CURRENCY_UNITS_DROPDOWN_TOOLTIP), SetFill(1, 0),
00484 EndContainer(),
00485 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_ROAD_VEHICLES_FRAME, STR_NULL),
00486 NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_ROADSIDE_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_TOOLTIP), SetFill(1, 0),
00487 EndContainer(),
00488 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_AUTOSAVE_FRAME, STR_NULL),
00489 NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_AUTOSAVE_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_TOOLTIP), SetFill(1, 0),
00490 EndContainer(),
00491 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_RESOLUTION, STR_NULL),
00492 NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_RESOLUTION_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_RESOLUTION_TOOLTIP), SetFill(1, 0), SetPadding(0, 0, 3, 0),
00493 NWidget(NWID_HORIZONTAL),
00494 NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_FULLSCREEN, STR_NULL),
00495 NWidget(WWT_TEXTBTN, COLOUR_GREY, GOW_FULLSCREEN_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_FULLSCREEN_TOOLTIP),
00496 EndContainer(),
00497 EndContainer(),
00498 EndContainer(),
00499
00500 NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
00501 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_MEASURING_UNITS_FRAME, STR_NULL),
00502 NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_DISTANCE_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_MEASURING_UNITS_DROPDOWN_TOOLTIP), SetFill(1, 0),
00503 EndContainer(),
00504 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_TOWN_NAMES_FRAME, STR_NULL),
00505 NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_TOWNNAME_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_TOWN_NAMES_DROPDOWN_TOOLTIP), SetFill(1, 0),
00506 EndContainer(),
00507 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_LANGUAGE, STR_NULL),
00508 NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_LANG_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_LANGUAGE_TOOLTIP), SetFill(1, 0),
00509 EndContainer(),
00510 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_SCREENSHOT_FORMAT, STR_NULL),
00511 NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_SCREENSHOT_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_SCREENSHOT_FORMAT_TOOLTIP), SetFill(1, 0),
00512 EndContainer(),
00513 NWidget(NWID_SPACER), SetMinimalSize(0, 0), SetFill(0, 1),
00514 EndContainer(),
00515 EndContainer(),
00516
00517 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_GRF, STR_NULL), SetPadding(0, 10, 0, 10),
00518 NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
00519 NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_BASE_GRF_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_GRF_TOOLTIP),
00520 NWidget(WWT_TEXT, COLOUR_GREY, GOW_BASE_GRF_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
00521 EndContainer(),
00522 NWidget(WWT_TEXT, COLOUR_GREY, GOW_BASE_GRF_DESCRIPTION), SetMinimalSize(330, 0), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_BASE_GRF_DESCRIPTION_TOOLTIP), SetFill(1, 0), SetPadding(6, 0, 0, 0),
00523 EndContainer(),
00524
00525 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_SFX, STR_NULL), SetPadding(0, 10, 0, 10),
00526 NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
00527 NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_BASE_SFX_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_SFX_TOOLTIP),
00528 NWidget(NWID_SPACER), SetFill(1, 0),
00529 EndContainer(),
00530 NWidget(WWT_TEXT, COLOUR_GREY, GOW_BASE_SFX_DESCRIPTION), SetMinimalSize(330, 0), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_BASE_SFX_DESCRIPTION_TOOLTIP), SetFill(1, 0), SetPadding(6, 0, 0, 0),
00531 EndContainer(),
00532
00533 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_MUSIC, STR_NULL), SetPadding(0, 10, 0, 10),
00534 NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
00535 NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_BASE_MUSIC_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_MUSIC_TOOLTIP),
00536 NWidget(WWT_TEXT, COLOUR_GREY, GOW_BASE_MUSIC_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
00537 EndContainer(),
00538 NWidget(WWT_TEXT, COLOUR_GREY, GOW_BASE_MUSIC_DESCRIPTION), SetMinimalSize(330, 0), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_BASE_MUSIC_DESCRIPTION_TOOLTIP), SetFill(1, 0), SetPadding(6, 0, 0, 0),
00539 EndContainer(),
00540 EndContainer(),
00541 };
00542
00543 static const WindowDesc _game_options_desc(
00544 WDP_CENTER, 0, 0,
00545 WC_GAME_OPTIONS, WC_NONE,
00546 WDF_UNCLICK_BUTTONS,
00547 _nested_game_options_widgets, lengthof(_nested_game_options_widgets)
00548 );
00549
00551 void ShowGameOptions()
00552 {
00553 DeleteWindowById(WC_GAME_OPTIONS, 0);
00554 new GameOptionsWindow(&_game_options_desc);
00555 }
00556
00557 extern void StartupEconomy();
00558
00559
00560
00561 enum GameDifficultyWidgets {
00562 GDW_LVL_EASY,
00563 GDW_LVL_MEDIUM,
00564 GDW_LVL_HARD,
00565 GDW_LVL_CUSTOM,
00566 GDW_HIGHSCORE,
00567 GDW_ACCEPT,
00568 GDW_CANCEL,
00569
00570 GDW_OPTIONS_START,
00571 };
00572
00573 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt);
00574
00575 class GameDifficultyWindow : public Window {
00576 private:
00577
00578 GameSettings opt_mod_temp;
00579
00580 public:
00582 static const uint GAME_DIFFICULTY_NUM = 18;
00584 static const uint WIDGETS_PER_DIFFICULTY = 3;
00585
00586 GameDifficultyWindow(const WindowDesc *desc) : Window()
00587 {
00588 this->InitNested(desc);
00589
00590
00591
00592 this->opt_mod_temp = GetGameSettings();
00593
00594
00595 this->SetWidgetsDisabledState(_game_mode != GM_MENU,
00596 GDW_LVL_EASY,
00597 GDW_LVL_MEDIUM,
00598 GDW_LVL_HARD,
00599 GDW_LVL_CUSTOM,
00600 WIDGET_LIST_END);
00601 this->SetWidgetDisabledState(GDW_HIGHSCORE, _game_mode == GM_EDITOR || _networking);
00602 this->SetWidgetDisabledState(GDW_ACCEPT, _networking && !_network_server);
00603 this->LowerWidget(GDW_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00604 this->OnInvalidateData();
00605 }
00606
00607 virtual void SetStringParameters(int widget) const
00608 {
00609 widget -= GDW_OPTIONS_START;
00610 if (widget < 0 || (widget % 3) != 2) return;
00611
00612 widget /= 3;
00613
00614 uint i;
00615 const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i) + widget;
00616 int32 value = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00617 SetDParam(0, sd->desc.str + value);
00618 }
00619
00620 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00621 {
00622
00623 int index = widget - GDW_OPTIONS_START;
00624 if (index < 0 || (index % 3) != 2) return;
00625
00626 index /= 3;
00627
00628 uint i;
00629 const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i) + index;
00630 const SettingDescBase *sdb = &sd->desc;
00631
00632
00633 StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
00634 for (int32 value = sdb->min; (uint32)value <= sdb->max; value += sdb->interval) {
00635 SetDParam(0, sdb->str + value);
00636 *size = maxdim(*size, GetStringBoundingBox(str));
00637 }
00638 }
00639
00640 virtual void OnClick(Point pt, int widget, int click_count)
00641 {
00642 if (widget >= GDW_OPTIONS_START) {
00643 widget -= GDW_OPTIONS_START;
00644 if ((widget % 3) == 2) return;
00645
00646
00647 if (_networking && !_network_server) return;
00648
00649 uint i;
00650 const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i) + (widget / 3);
00651 const SettingDescBase *sdb = &sd->desc;
00652
00653 int32 val = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00654 if (widget % 3 == 1) {
00655
00656 val = min(val + sdb->interval, (int32)sdb->max);
00657 } else {
00658
00659 val -= sdb->interval;
00660 val = max(val, sdb->min);
00661 }
00662
00663
00664 WriteValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv, val);
00665 this->RaiseWidget(GDW_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00666 SetDifficultyLevel(3, &this->opt_mod_temp.difficulty);
00667 this->LowerWidget(GDW_LVL_CUSTOM);
00668 this->InvalidateData();
00669
00670 if (widget / 3 == 0 &&
00671 #ifdef ENABLE_AI
00672 AI::GetInfoList()->size() == 0 &&
00673 #endif
00674 this->opt_mod_temp.difficulty.max_no_competitors != 0) {
00675 ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
00676 }
00677 return;
00678 }
00679
00680 switch (widget) {
00681 case GDW_LVL_EASY:
00682 case GDW_LVL_MEDIUM:
00683 case GDW_LVL_HARD:
00684 case GDW_LVL_CUSTOM:
00685
00686 this->RaiseWidget(GDW_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00687 SetDifficultyLevel(widget - GDW_LVL_EASY, &this->opt_mod_temp.difficulty);
00688 this->LowerWidget(GDW_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00689 this->InvalidateData();
00690 break;
00691
00692 case GDW_HIGHSCORE:
00693 ShowHighscoreTable(this->opt_mod_temp.difficulty.diff_level, -1);
00694 break;
00695
00696 case GDW_ACCEPT: {
00697 GameSettings *opt_ptr = &GetGameSettings();
00698
00699 uint i;
00700 GetSettingFromName("difficulty.diff_level", &i);
00701 DoCommandP(0, i, this->opt_mod_temp.difficulty.diff_level, CMD_CHANGE_SETTING);
00702
00703 const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i);
00704 for (uint btn = 0; btn != GAME_DIFFICULTY_NUM; btn++, sd++) {
00705 int32 new_val = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00706 int32 cur_val = (int32)ReadValue(GetVariableAddress(opt_ptr, &sd->save), sd->save.conv);
00707
00708 if (new_val != cur_val) {
00709 DoCommandP(0, i + btn, new_val, CMD_CHANGE_SETTING);
00710 }
00711 }
00712 delete this;
00713
00714
00715
00716 if (_game_mode == GM_EDITOR) StartupEconomy();
00717 break;
00718 }
00719
00720 case GDW_CANCEL:
00721 delete this;
00722 break;
00723 }
00724 }
00725
00726 virtual void OnInvalidateData(int data = 0)
00727 {
00728 uint i;
00729 const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i);
00730 for (i = 0; i < GAME_DIFFICULTY_NUM; i++, sd++) {
00731 const SettingDescBase *sdb = &sd->desc;
00732
00733 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
00734 int32 value = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00735 bool disable = (sd->desc.flags & SGF_NEWGAME_ONLY) &&
00736 (_game_mode == GM_NORMAL ||
00737 (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0));
00738
00739 this->SetWidgetDisabledState(GDW_OPTIONS_START + i * 3 + 0, disable || sdb->min == value);
00740 this->SetWidgetDisabledState(GDW_OPTIONS_START + i * 3 + 1, disable || sdb->max == (uint32)value);
00741 }
00742 }
00743 };
00744
00745 static NWidgetBase *MakeDifficultyOptionsWidgets(int *biggest_index)
00746 {
00747 NWidgetVertical *vert_desc = new NWidgetVertical;
00748
00749 int widnum = GDW_OPTIONS_START;
00750 uint i, j;
00751 const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i);
00752
00753 for (i = 0, j = 0; i < GameDifficultyWindow::GAME_DIFFICULTY_NUM; i++, sd++, widnum += GameDifficultyWindow::WIDGETS_PER_DIFFICULTY) {
00754 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
00755
00756 NWidgetHorizontal *hor = new NWidgetHorizontal;
00757
00758
00759 NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum, AWV_DECREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
00760 hor->Add(leaf);
00761
00762
00763 leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum + 1, AWV_INCREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
00764 hor->Add(leaf);
00765
00766
00767 NWidgetSpacer *spacer = new NWidgetSpacer(5, 0);
00768 hor->Add(spacer);
00769
00770
00771 leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, widnum + 2, STR_DIFFICULTY_LEVEL_SETTING_MAXIMUM_NO_COMPETITORS + (j++), STR_NULL);
00772 leaf->SetFill(1, 0);
00773 hor->Add(leaf);
00774 vert_desc->Add(hor);
00775
00776
00777 vert_desc->Add(new NWidgetSpacer(0, 2));
00778 }
00779 *biggest_index = widnum - 1;
00780 return vert_desc;
00781 }
00782
00783
00785 static const NWidgetPart _nested_game_difficulty_widgets[] = {
00786 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_DIFFICULTY_LEVEL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00787 NWidget(WWT_PANEL, COLOUR_MAUVE),
00788 NWidget(NWID_VERTICAL), SetPIP(2, 0, 2),
00789 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
00790 NWidget(WWT_TEXTBTN, COLOUR_YELLOW, GDW_LVL_EASY), SetDataTip(STR_DIFFICULTY_LEVEL_EASY, STR_NULL), SetFill(1, 0),
00791 NWidget(WWT_TEXTBTN, COLOUR_YELLOW, GDW_LVL_MEDIUM), SetDataTip(STR_DIFFICULTY_LEVEL_MEDIUM, STR_NULL), SetFill(1, 0),
00792 NWidget(WWT_TEXTBTN, COLOUR_YELLOW, GDW_LVL_HARD), SetDataTip(STR_DIFFICULTY_LEVEL_HARD, STR_NULL), SetFill(1, 0),
00793 NWidget(WWT_TEXTBTN, COLOUR_YELLOW, GDW_LVL_CUSTOM), SetDataTip(STR_DIFFICULTY_LEVEL_CUSTOM, STR_NULL), SetFill(1, 0),
00794 EndContainer(),
00795 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
00796 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, GDW_HIGHSCORE), SetDataTip(STR_DIFFICULTY_LEVEL_HIGH_SCORE_BUTTON, STR_NULL), SetFill(1, 0),
00797 EndContainer(),
00798 EndContainer(),
00799 EndContainer(),
00800 NWidget(WWT_PANEL, COLOUR_MAUVE),
00801 NWidget(NWID_VERTICAL), SetPIP(3, 0, 1),
00802 NWidget(NWID_HORIZONTAL), SetPIP(5, 0, 5),
00803 NWidgetFunction(MakeDifficultyOptionsWidgets),
00804 EndContainer(),
00805 EndContainer(),
00806 EndContainer(),
00807 NWidget(WWT_PANEL, COLOUR_MAUVE),
00808 NWidget(NWID_VERTICAL), SetPIP(2, 0, 2),
00809 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
00810 NWidget(NWID_SPACER), SetFill(1, 0),
00811 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, GDW_ACCEPT), SetDataTip(STR_DIFFICULTY_LEVEL_SAVE, STR_NULL), SetFill(1, 0),
00812 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, GDW_CANCEL), SetDataTip(STR_BUTTON_CANCEL, STR_NULL), SetFill(1, 0),
00813 NWidget(NWID_SPACER), SetFill(1, 0),
00814 EndContainer(),
00815 EndContainer(),
00816 EndContainer(),
00817 };
00818
00820 static const WindowDesc _game_difficulty_desc(
00821 WDP_CENTER, 0, 0,
00822 WC_GAME_OPTIONS, WC_NONE,
00823 WDF_UNCLICK_BUTTONS,
00824 _nested_game_difficulty_widgets, lengthof(_nested_game_difficulty_widgets)
00825 );
00826
00828 void ShowGameDifficulty()
00829 {
00830 DeleteWindowById(WC_GAME_OPTIONS, 0);
00831 new GameDifficultyWindow(&_game_difficulty_desc);
00832 }
00833
00834 static int SETTING_HEIGHT = 11;
00835 static const int LEVEL_WIDTH = 15;
00836
00841 enum SettingEntryFlags {
00842 SEF_LEFT_DEPRESSED = 0x01,
00843 SEF_RIGHT_DEPRESSED = 0x02,
00844 SEF_BUTTONS_MASK = (SEF_LEFT_DEPRESSED | SEF_RIGHT_DEPRESSED),
00845
00846 SEF_LAST_FIELD = 0x04,
00847
00848
00849 SEF_SETTING_KIND = 0x10,
00850 SEF_SUBTREE_KIND = 0x20,
00851 SEF_KIND_MASK = (SEF_SETTING_KIND | SEF_SUBTREE_KIND),
00852 };
00853
00854 struct SettingsPage;
00855
00857 struct SettingEntrySubtree {
00858 SettingsPage *page;
00859 bool folded;
00860 StringID title;
00861 };
00862
00864 struct SettingEntrySetting {
00865 const char *name;
00866 const SettingDesc *setting;
00867 uint index;
00868 };
00869
00871 struct SettingEntry {
00872 byte flags;
00873 byte level;
00874 union {
00875 SettingEntrySetting entry;
00876 SettingEntrySubtree sub;
00877 } d;
00878
00879 SettingEntry(const char *nm);
00880 SettingEntry(SettingsPage *sub, StringID title);
00881
00882 void Init(byte level, bool last_field);
00883 void FoldAll();
00884 void SetButtons(byte new_val);
00885
00886 uint Length() const;
00887 SettingEntry *FindEntry(uint row, uint *cur_row);
00888
00889 uint Draw(GameSettings *settings_ptr, int base_x, int base_y, int max_x, uint first_row, uint max_row, uint cur_row, uint parent_last);
00890
00891 private:
00892 void DrawSetting(GameSettings *settings_ptr, const SettingDesc *sd, int x, int y, int max_x, int state);
00893 };
00894
00896 struct SettingsPage {
00897 SettingEntry *entries;
00898 byte num;
00899
00900 void Init(byte level = 0);
00901 void FoldAll();
00902
00903 uint Length() const;
00904 SettingEntry *FindEntry(uint row, uint *cur_row) const;
00905
00906 uint Draw(GameSettings *settings_ptr, int base_x, int base_y, int max_x, uint first_row, uint max_row, uint cur_row = 0, uint parent_last = 0) const;
00907 };
00908
00909
00910
00911
00916 SettingEntry::SettingEntry(const char *nm)
00917 {
00918 this->flags = SEF_SETTING_KIND;
00919 this->level = 0;
00920 this->d.entry.name = nm;
00921 this->d.entry.setting = NULL;
00922 this->d.entry.index = 0;
00923 }
00924
00930 SettingEntry::SettingEntry(SettingsPage *sub, StringID title)
00931 {
00932 this->flags = SEF_SUBTREE_KIND;
00933 this->level = 0;
00934 this->d.sub.page = sub;
00935 this->d.sub.folded = true;
00936 this->d.sub.title = title;
00937 }
00938
00944 void SettingEntry::Init(byte level, bool last_field)
00945 {
00946 this->level = level;
00947 if (last_field) this->flags |= SEF_LAST_FIELD;
00948
00949 switch (this->flags & SEF_KIND_MASK) {
00950 case SEF_SETTING_KIND:
00951 this->d.entry.setting = GetSettingFromName(this->d.entry.name, &this->d.entry.index);
00952 assert(this->d.entry.setting != NULL);
00953 break;
00954 case SEF_SUBTREE_KIND:
00955 this->d.sub.page->Init(level + 1);
00956 break;
00957 default: NOT_REACHED();
00958 }
00959 }
00960
00962 void SettingEntry::FoldAll()
00963 {
00964 switch (this->flags & SEF_KIND_MASK) {
00965 case SEF_SETTING_KIND:
00966 break;
00967
00968 case SEF_SUBTREE_KIND:
00969 this->d.sub.folded = true;
00970 this->d.sub.page->FoldAll();
00971 break;
00972
00973 default: NOT_REACHED();
00974 }
00975 }
00976
00977
00983 void SettingEntry::SetButtons(byte new_val)
00984 {
00985 assert((new_val & ~SEF_BUTTONS_MASK) == 0);
00986 this->flags = (this->flags & ~SEF_BUTTONS_MASK) | new_val;
00987 }
00988
00990 uint SettingEntry::Length() const
00991 {
00992 switch (this->flags & SEF_KIND_MASK) {
00993 case SEF_SETTING_KIND:
00994 return 1;
00995 case SEF_SUBTREE_KIND:
00996 if (this->d.sub.folded) return 1;
00997
00998 return 1 + this->d.sub.page->Length();
00999 default: NOT_REACHED();
01000 }
01001 }
01002
01009 SettingEntry *SettingEntry::FindEntry(uint row_num, uint *cur_row)
01010 {
01011 if (row_num == *cur_row) return this;
01012
01013 switch (this->flags & SEF_KIND_MASK) {
01014 case SEF_SETTING_KIND:
01015 (*cur_row)++;
01016 break;
01017 case SEF_SUBTREE_KIND:
01018 (*cur_row)++;
01019 if (this->d.sub.folded) {
01020 break;
01021 }
01022
01023
01024 return this->d.sub.page->FindEntry(row_num, cur_row);
01025 default: NOT_REACHED();
01026 }
01027 return NULL;
01028 }
01029
01056 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)
01057 {
01058 if (cur_row >= max_row) return cur_row;
01059
01060 bool rtl = _current_text_dir == TD_RTL;
01061 int offset = rtl ? -4 : 4;
01062 int level_width = rtl ? -LEVEL_WIDTH : LEVEL_WIDTH;
01063
01064 int x = rtl ? right : left;
01065 int y = base_y;
01066 if (cur_row >= first_row) {
01067 int colour = _colour_gradient[COLOUR_ORANGE][4];
01068 y = base_y + (cur_row - first_row) * SETTING_HEIGHT;
01069
01070
01071 for (uint lvl = 0; lvl < this->level; lvl++) {
01072 if (!HasBit(parent_last, lvl)) GfxDrawLine(x + offset, y, x + offset, y + SETTING_HEIGHT - 1, colour);
01073 x += level_width;
01074 }
01075
01076 int halfway_y = y + SETTING_HEIGHT / 2;
01077 int bottom_y = (flags & SEF_LAST_FIELD) ? halfway_y : y + SETTING_HEIGHT - 1;
01078 GfxDrawLine(x + offset, y, x + offset, bottom_y, colour);
01079
01080 GfxDrawLine(x + offset, halfway_y, x + level_width - offset, halfway_y, colour);
01081 x += level_width;
01082 }
01083
01084 switch (this->flags & SEF_KIND_MASK) {
01085 case SEF_SETTING_KIND:
01086 if (cur_row >= first_row) {
01087 DrawSetting(settings_ptr, this->d.entry.setting, rtl ? left : x, rtl ? x : right, y, this->flags & SEF_BUTTONS_MASK);
01088 }
01089 cur_row++;
01090 break;
01091 case SEF_SUBTREE_KIND:
01092 if (cur_row >= first_row) {
01093 DrawSprite((this->d.sub.folded ? SPR_CIRCLE_FOLDED : SPR_CIRCLE_UNFOLDED), PAL_NONE, rtl ? x - 8 : x, y + (SETTING_HEIGHT - 11) / 2);
01094 DrawString(rtl ? left : x + 12, rtl ? x - 12 : right, y, this->d.sub.title);
01095 }
01096 cur_row++;
01097 if (!this->d.sub.folded) {
01098 if (this->flags & SEF_LAST_FIELD) {
01099 assert(this->level < sizeof(parent_last));
01100 SetBit(parent_last, this->level);
01101 }
01102
01103 cur_row = this->d.sub.page->Draw(settings_ptr, left, right, base_y, first_row, max_row, cur_row, parent_last);
01104 }
01105 break;
01106 default: NOT_REACHED();
01107 }
01108 return cur_row;
01109 }
01110
01111 static const void *ResolveVariableAddress(const GameSettings *settings_ptr, const SettingDesc *sd)
01112 {
01113 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01114 if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01115 return GetVariableAddress(&Company::Get(_local_company)->settings, &sd->save);
01116 } else {
01117 return GetVariableAddress(&_settings_client.company, &sd->save);
01118 }
01119 } else {
01120 return GetVariableAddress(settings_ptr, &sd->save);
01121 }
01122 }
01123
01133 void SettingEntry::DrawSetting(GameSettings *settings_ptr, const SettingDesc *sd, int left, int right, int y, int state)
01134 {
01135 const SettingDescBase *sdb = &sd->desc;
01136 const void *var = ResolveVariableAddress(settings_ptr, sd);
01137 bool editable = true;
01138 bool disabled = false;
01139
01140 bool rtl = _current_text_dir == TD_RTL;
01141 uint buttons_left = rtl ? right - 19 : left;
01142 uint text_left = left + (rtl ? 0 : 25);
01143 uint text_right = right - (rtl ? 25 : 0);
01144 uint button_y = y + (SETTING_HEIGHT - 11) / 2;
01145
01146
01147 if (!(sd->save.conv & SLF_NETWORK_NO) && _networking && !_network_server && !(sdb->flags & SGF_PER_COMPANY)) editable = false;
01148 if ((sdb->flags & SGF_NETWORK_ONLY) && !_networking) editable = false;
01149 if ((sdb->flags & SGF_NO_NETWORK) && _networking) editable = false;
01150
01151 if (sdb->cmd == SDT_BOOLX) {
01152 static const Colours _bool_ctabs[2][2] = {{COLOUR_CREAM, COLOUR_RED}, {COLOUR_DARK_GREEN, COLOUR_GREEN}};
01153
01154 bool on = ReadValue(var, sd->save.conv) != 0;
01155
01156 DrawFrameRect(buttons_left, button_y, buttons_left + 19, button_y + 8, _bool_ctabs[!!on][!!editable], on ? FR_LOWERED : FR_NONE);
01157 SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
01158 } else {
01159 int32 value;
01160
01161 value = (int32)ReadValue(var, sd->save.conv);
01162
01163
01164 DrawArrowButtons(buttons_left, button_y, COLOUR_YELLOW, state, editable && value != (sdb->flags & SGF_0ISDISABLED ? 0 : sdb->min), editable && (uint32)value != sdb->max);
01165
01166 disabled = (value == 0) && (sdb->flags & SGF_0ISDISABLED);
01167 if (disabled) {
01168 SetDParam(0, STR_CONFIG_SETTING_DISABLED);
01169 } else {
01170 if (sdb->flags & SGF_CURRENCY) {
01171 SetDParam(0, STR_JUST_CURRENCY);
01172 } else if (sdb->flags & SGF_MULTISTRING) {
01173 SetDParam(0, sdb->str - sdb->min + value + 1);
01174 } else {
01175 SetDParam(0, (sdb->flags & SGF_NOCOMMA) ? STR_JUST_INT : STR_JUST_COMMA);
01176 }
01177 SetDParam(1, value);
01178 }
01179 }
01180 DrawString(text_left, text_right, y, (sdb->str) + disabled);
01181 }
01182
01183
01184
01185
01190 void SettingsPage::Init(byte level)
01191 {
01192 for (uint field = 0; field < this->num; field++) {
01193 this->entries[field].Init(level, field + 1 == num);
01194 }
01195 }
01196
01198 void SettingsPage::FoldAll()
01199 {
01200 for (uint field = 0; field < this->num; field++) {
01201 this->entries[field].FoldAll();
01202 }
01203 }
01204
01206 uint SettingsPage::Length() const
01207 {
01208 uint length = 0;
01209 for (uint field = 0; field < this->num; field++) {
01210 length += this->entries[field].Length();
01211 }
01212 return length;
01213 }
01214
01221 SettingEntry *SettingsPage::FindEntry(uint row_num, uint *cur_row) const
01222 {
01223 SettingEntry *pe = NULL;
01224
01225 for (uint field = 0; field < this->num; field++) {
01226 pe = this->entries[field].FindEntry(row_num, cur_row);
01227 if (pe != NULL) {
01228 break;
01229 }
01230 }
01231 return pe;
01232 }
01233
01251 uint SettingsPage::Draw(GameSettings *settings_ptr, int left, int right, int base_y, uint first_row, uint max_row, uint cur_row, uint parent_last) const
01252 {
01253 if (cur_row >= max_row) return cur_row;
01254
01255 for (uint i = 0; i < this->num; i++) {
01256 cur_row = this->entries[i].Draw(settings_ptr, left, right, base_y, first_row, max_row, cur_row, parent_last);
01257 if (cur_row >= max_row) {
01258 break;
01259 }
01260 }
01261 return cur_row;
01262 }
01263
01264
01265 static SettingEntry _settings_ui_display[] = {
01266 SettingEntry("gui.date_format_in_default_names"),
01267 SettingEntry("gui.population_in_label"),
01268 SettingEntry("gui.measure_tooltip"),
01269 SettingEntry("gui.loading_indicators"),
01270 SettingEntry("gui.liveries"),
01271 SettingEntry("gui.show_track_reservation"),
01272 SettingEntry("gui.expenses_layout"),
01273 SettingEntry("gui.smallmap_land_colour"),
01274 };
01276 static SettingsPage _settings_ui_display_page = {_settings_ui_display, lengthof(_settings_ui_display)};
01277
01278 static SettingEntry _settings_ui_interaction[] = {
01279 SettingEntry("gui.window_snap_radius"),
01280 SettingEntry("gui.window_soft_limit"),
01281 SettingEntry("gui.link_terraform_toolbar"),
01282 SettingEntry("gui.prefer_teamchat"),
01283 SettingEntry("gui.autoscroll"),
01284 SettingEntry("gui.reverse_scroll"),
01285 SettingEntry("gui.smooth_scroll"),
01286 SettingEntry("gui.left_mouse_btn_scrolling"),
01287
01288
01289
01290 SettingEntry("gui.scrollwheel_scrolling"),
01291 SettingEntry("gui.scrollwheel_multiplier"),
01292 #ifdef __APPLE__
01293
01294 SettingEntry("gui.right_mouse_btn_emulation"),
01295 #endif
01296 };
01298 static SettingsPage _settings_ui_interaction_page = {_settings_ui_interaction, lengthof(_settings_ui_interaction)};
01299
01300 static SettingEntry _settings_ui[] = {
01301 SettingEntry(&_settings_ui_display_page, STR_CONFIG_SETTING_DISPLAY_OPTIONS),
01302 SettingEntry(&_settings_ui_interaction_page, STR_CONFIG_SETTING_INTERACTION),
01303 SettingEntry("gui.show_finances"),
01304 SettingEntry("gui.errmsg_duration"),
01305 SettingEntry("gui.hover_delay"),
01306 SettingEntry("gui.toolbar_pos"),
01307 SettingEntry("gui.statusbar_pos"),
01308 SettingEntry("gui.pause_on_newgame"),
01309 SettingEntry("gui.advanced_vehicle_list"),
01310 SettingEntry("gui.timetable_in_ticks"),
01311 SettingEntry("gui.timetable_arrival_departure"),
01312 SettingEntry("gui.quick_goto"),
01313 SettingEntry("gui.default_rail_type"),
01314 SettingEntry("gui.disable_unsuitable_building"),
01315 SettingEntry("gui.persistent_buildingtools"),
01316 SettingEntry("gui.coloured_news_year"),
01317 };
01319 static SettingsPage _settings_ui_page = {_settings_ui, lengthof(_settings_ui)};
01320
01321 static SettingEntry _settings_construction_signals[] = {
01322 SettingEntry("construction.signal_side"),
01323 SettingEntry("gui.enable_signal_gui"),
01324 SettingEntry("gui.drag_signals_density"),
01325 SettingEntry("gui.semaphore_build_before"),
01326 SettingEntry("gui.default_signal_type"),
01327 SettingEntry("gui.cycle_signal_types"),
01328 };
01330 static SettingsPage _settings_construction_signals_page = {_settings_construction_signals, lengthof(_settings_construction_signals)};
01331
01332 static SettingEntry _settings_construction[] = {
01333 SettingEntry(&_settings_construction_signals_page, STR_CONFIG_SETTING_CONSTRUCTION_SIGNALS),
01334 SettingEntry("construction.build_on_slopes"),
01335 SettingEntry("construction.autoslope"),
01336 SettingEntry("construction.extra_dynamite"),
01337 SettingEntry("construction.max_bridge_length"),
01338 SettingEntry("construction.max_tunnel_length"),
01339 SettingEntry("station.never_expire_airports"),
01340 SettingEntry("construction.freeform_edges"),
01341 SettingEntry("construction.extra_tree_placement"),
01342 SettingEntry("construction.command_pause_level"),
01343 };
01345 static SettingsPage _settings_construction_page = {_settings_construction, lengthof(_settings_construction)};
01346
01347 static SettingEntry _settings_stations_cargo[] = {
01348 SettingEntry("order.improved_load"),
01349 SettingEntry("order.gradual_loading"),
01350 SettingEntry("order.selectgoods"),
01351 };
01353 static SettingsPage _settings_stations_cargo_page = {_settings_stations_cargo, lengthof(_settings_stations_cargo)};
01354
01355 static SettingEntry _settings_stations[] = {
01356 SettingEntry(&_settings_stations_cargo_page, STR_CONFIG_SETTING_STATIONS_CARGOHANDLING),
01357 SettingEntry("station.adjacent_stations"),
01358 SettingEntry("station.distant_join_stations"),
01359 SettingEntry("station.station_spread"),
01360 SettingEntry("economy.station_noise_level"),
01361 SettingEntry("station.modified_catchment"),
01362 SettingEntry("construction.road_stop_on_town_road"),
01363 SettingEntry("construction.road_stop_on_competitor_road"),
01364 };
01366 static SettingsPage _settings_stations_page = {_settings_stations, lengthof(_settings_stations)};
01367
01368 static SettingEntry _settings_economy_towns[] = {
01369 SettingEntry("economy.bribe"),
01370 SettingEntry("economy.exclusive_rights"),
01371 SettingEntry("economy.town_layout"),
01372 SettingEntry("economy.allow_town_roads"),
01373 SettingEntry("economy.allow_town_level_crossings"),
01374 SettingEntry("economy.found_town"),
01375 SettingEntry("economy.mod_road_rebuild"),
01376 SettingEntry("economy.town_growth_rate"),
01377 SettingEntry("economy.larger_towns"),
01378 SettingEntry("economy.initial_city_size"),
01379 };
01381 static SettingsPage _settings_economy_towns_page = {_settings_economy_towns, lengthof(_settings_economy_towns)};
01382
01383 static SettingEntry _settings_economy_industries[] = {
01384 SettingEntry("construction.raw_industry_construction"),
01385 SettingEntry("construction.industry_platform"),
01386 SettingEntry("economy.multiple_industry_per_town"),
01387 SettingEntry("game_creation.oil_refinery_limit"),
01388 };
01390 static SettingsPage _settings_economy_industries_page = {_settings_economy_industries, lengthof(_settings_economy_industries)};
01391
01392 static SettingEntry _settings_economy[] = {
01393 SettingEntry(&_settings_economy_towns_page, STR_CONFIG_SETTING_ECONOMY_TOWNS),
01394 SettingEntry(&_settings_economy_industries_page, STR_CONFIG_SETTING_ECONOMY_INDUSTRIES),
01395 SettingEntry("economy.inflation"),
01396 SettingEntry("economy.smooth_economy"),
01397 SettingEntry("economy.feeder_payment_share"),
01398 };
01400 static SettingsPage _settings_economy_page = {_settings_economy, lengthof(_settings_economy)};
01401
01402 static SettingEntry _settings_ai_npc[] = {
01403 SettingEntry("ai.ai_in_multiplayer"),
01404 SettingEntry("ai.ai_disable_veh_train"),
01405 SettingEntry("ai.ai_disable_veh_roadveh"),
01406 SettingEntry("ai.ai_disable_veh_aircraft"),
01407 SettingEntry("ai.ai_disable_veh_ship"),
01408 SettingEntry("ai.ai_max_opcode_till_suspend"),
01409 };
01411 static SettingsPage _settings_ai_npc_page = {_settings_ai_npc, lengthof(_settings_ai_npc)};
01412
01413 static SettingEntry _settings_ai[] = {
01414 SettingEntry(&_settings_ai_npc_page, STR_CONFIG_SETTING_AI_NPC),
01415 SettingEntry("economy.give_money"),
01416 SettingEntry("economy.allow_shares"),
01417 };
01419 static SettingsPage _settings_ai_page = {_settings_ai, lengthof(_settings_ai)};
01420
01421 static SettingEntry _settings_vehicles_routing[] = {
01422 SettingEntry("pf.pathfinder_for_trains"),
01423 SettingEntry("pf.forbid_90_deg"),
01424 SettingEntry("pf.pathfinder_for_roadvehs"),
01425 SettingEntry("pf.roadveh_queue"),
01426 SettingEntry("pf.pathfinder_for_ships"),
01427 };
01429 static SettingsPage _settings_vehicles_routing_page = {_settings_vehicles_routing, lengthof(_settings_vehicles_routing)};
01430
01431 static SettingEntry _settings_vehicles_autorenew[] = {
01432 SettingEntry("company.engine_renew"),
01433 SettingEntry("company.engine_renew_months"),
01434 SettingEntry("company.engine_renew_money"),
01435 };
01437 static SettingsPage _settings_vehicles_autorenew_page = {_settings_vehicles_autorenew, lengthof(_settings_vehicles_autorenew)};
01438
01439 static SettingEntry _settings_vehicles_servicing[] = {
01440 SettingEntry("vehicle.servint_ispercent"),
01441 SettingEntry("vehicle.servint_trains"),
01442 SettingEntry("vehicle.servint_roadveh"),
01443 SettingEntry("vehicle.servint_ships"),
01444 SettingEntry("vehicle.servint_aircraft"),
01445 SettingEntry("order.no_servicing_if_no_breakdowns"),
01446 SettingEntry("order.serviceathelipad"),
01447 };
01449 static SettingsPage _settings_vehicles_servicing_page = {_settings_vehicles_servicing, lengthof(_settings_vehicles_servicing)};
01450
01451 static SettingEntry _settings_vehicles_trains[] = {
01452 SettingEntry("pf.reverse_at_signals"),
01453 SettingEntry("vehicle.train_acceleration_model"),
01454 SettingEntry("vehicle.train_slope_steepness"),
01455 SettingEntry("vehicle.max_train_length"),
01456 SettingEntry("vehicle.wagon_speed_limits"),
01457 SettingEntry("vehicle.disable_elrails"),
01458 SettingEntry("vehicle.freight_trains"),
01459 SettingEntry("gui.stop_location"),
01460 };
01462 static SettingsPage _settings_vehicles_trains_page = {_settings_vehicles_trains, lengthof(_settings_vehicles_trains)};
01463
01464 static SettingEntry _settings_vehicles[] = {
01465 SettingEntry(&_settings_vehicles_routing_page, STR_CONFIG_SETTING_VEHICLES_ROUTING),
01466 SettingEntry(&_settings_vehicles_autorenew_page, STR_CONFIG_SETTING_VEHICLES_AUTORENEW),
01467 SettingEntry(&_settings_vehicles_servicing_page, STR_CONFIG_SETTING_VEHICLES_SERVICING),
01468 SettingEntry(&_settings_vehicles_trains_page, STR_CONFIG_SETTING_VEHICLES_TRAINS),
01469 SettingEntry("gui.new_nonstop"),
01470 SettingEntry("gui.order_review_system"),
01471 SettingEntry("gui.vehicle_income_warn"),
01472 SettingEntry("gui.lost_vehicle_warn"),
01473 SettingEntry("vehicle.never_expire_vehicles"),
01474 SettingEntry("vehicle.max_trains"),
01475 SettingEntry("vehicle.max_roadveh"),
01476 SettingEntry("vehicle.max_aircraft"),
01477 SettingEntry("vehicle.max_ships"),
01478 SettingEntry("vehicle.plane_speed"),
01479 SettingEntry("vehicle.plane_crashes"),
01480 SettingEntry("vehicle.dynamic_engines"),
01481 SettingEntry("vehicle.roadveh_acceleration_model"),
01482 SettingEntry("vehicle.roadveh_slope_steepness"),
01483 SettingEntry("vehicle.smoke_amount"),
01484 };
01486 static SettingsPage _settings_vehicles_page = {_settings_vehicles, lengthof(_settings_vehicles)};
01487
01488 static SettingEntry _settings_main[] = {
01489 SettingEntry(&_settings_ui_page, STR_CONFIG_SETTING_GUI),
01490 SettingEntry(&_settings_construction_page, STR_CONFIG_SETTING_CONSTRUCTION),
01491 SettingEntry(&_settings_vehicles_page, STR_CONFIG_SETTING_VEHICLES),
01492 SettingEntry(&_settings_stations_page, STR_CONFIG_SETTING_STATIONS),
01493 SettingEntry(&_settings_economy_page, STR_CONFIG_SETTING_ECONOMY),
01494 SettingEntry(&_settings_ai_page, STR_CONFIG_SETTING_AI),
01495 };
01496
01498 static SettingsPage _settings_main_page = {_settings_main, lengthof(_settings_main)};
01499
01501 enum GameSettingsWidgets {
01502 SETTINGSEL_OPTIONSPANEL,
01503 SETTINGSEL_SCROLLBAR,
01504 };
01505
01506 struct GameSettingsWindow : Window {
01507 static const int SETTINGTREE_LEFT_OFFSET = 5;
01508 static const int SETTINGTREE_RIGHT_OFFSET = 5;
01509 static const int SETTINGTREE_TOP_OFFSET = 5;
01510 static const int SETTINGTREE_BOTTOM_OFFSET = 5;
01511
01512 static GameSettings *settings_ptr;
01513
01514 SettingEntry *valuewindow_entry;
01515 SettingEntry *clicked_entry;
01516
01517 Scrollbar *vscroll;
01518
01519 GameSettingsWindow(const WindowDesc *desc) : Window()
01520 {
01521 static bool first_time = true;
01522
01523 settings_ptr = &GetGameSettings();
01524
01525
01526 if (first_time) {
01527 _settings_main_page.Init();
01528 first_time = false;
01529 } else {
01530 _settings_main_page.FoldAll();
01531 }
01532
01533 this->valuewindow_entry = NULL;
01534 this->clicked_entry = NULL;
01535
01536 this->CreateNestedTree(desc);
01537 this->vscroll = this->GetScrollbar(SETTINGSEL_SCROLLBAR);
01538 this->FinishInitNested(desc, 0);
01539
01540 this->vscroll->SetCount(_settings_main_page.Length());
01541 }
01542
01543 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01544 {
01545 if (widget != SETTINGSEL_OPTIONSPANEL) return;
01546
01547 resize->height = SETTING_HEIGHT = max(11, FONT_HEIGHT_NORMAL + 1);
01548 resize->width = 1;
01549
01550 size->height = 5 * resize->height + SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET;
01551 }
01552
01553 virtual void DrawWidget(const Rect &r, int widget) const
01554 {
01555 if (widget != SETTINGSEL_OPTIONSPANEL) return;
01556
01557 _settings_main_page.Draw(settings_ptr, r.left + SETTINGTREE_LEFT_OFFSET, r.right - SETTINGTREE_RIGHT_OFFSET, r.top + SETTINGTREE_TOP_OFFSET,
01558 this->vscroll->GetPosition(), this->vscroll->GetPosition() + this->vscroll->GetCapacity());
01559 }
01560
01561 virtual void OnClick(Point pt, int widget, int click_count)
01562 {
01563 if (widget != SETTINGSEL_OPTIONSPANEL) return;
01564
01565 uint btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, SETTINGSEL_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET - 1);
01566 if (btn == INT_MAX) return;
01567
01568 uint cur_row = 0;
01569 SettingEntry *pe = _settings_main_page.FindEntry(btn, &cur_row);
01570
01571 if (pe == NULL) return;
01572
01573 int x = (_current_text_dir == TD_RTL ? this->width - pt.x : pt.x) - SETTINGTREE_LEFT_OFFSET - (pe->level + 1) * LEVEL_WIDTH;
01574 if (x < 0) return;
01575
01576 if ((pe->flags & SEF_KIND_MASK) == SEF_SUBTREE_KIND) {
01577 pe->d.sub.folded = !pe->d.sub.folded;
01578
01579 this->vscroll->SetCount(_settings_main_page.Length());
01580 this->SetDirty();
01581 return;
01582 }
01583
01584 assert((pe->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
01585 const SettingDesc *sd = pe->d.entry.setting;
01586
01587
01588 if (!(sd->save.conv & SLF_NETWORK_NO) && _networking && !_network_server && !(sd->desc.flags & SGF_PER_COMPANY)) return;
01589 if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking) return;
01590 if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return;
01591
01592 const void *var = ResolveVariableAddress(settings_ptr, sd);
01593 int32 value = (int32)ReadValue(var, sd->save.conv);
01594
01595
01596 if (x < 21) {
01597 const SettingDescBase *sdb = &sd->desc;
01598 int32 oldvalue = value;
01599
01600 switch (sdb->cmd) {
01601 case SDT_BOOLX: value ^= 1; break;
01602 case SDT_ONEOFMANY:
01603 case SDT_NUMX: {
01604
01605
01606
01607
01608 uint32 step = (sdb->interval == 0) ? ((sdb->max - sdb->min) / 50) : sdb->interval;
01609 if (step == 0) step = 1;
01610
01611
01612 if ((this->flags4 & WF_TIMEOUT_MASK) > WF_TIMEOUT_TRIGGER) {
01613 _left_button_clicked = false;
01614 return;
01615 }
01616
01617
01618 if (x >= 10) {
01619 value += step;
01620 if (sdb->min < 0) {
01621 assert((int32)sdb->max >= 0);
01622 if (value > (int32)sdb->max) value = (int32)sdb->max;
01623 } else {
01624 if ((uint32)value > sdb->max) value = (int32)sdb->max;
01625 }
01626 if (value < sdb->min) value = sdb->min;
01627 } else {
01628 value -= step;
01629 if (value < sdb->min) value = (sdb->flags & SGF_0ISDISABLED) ? 0 : sdb->min;
01630 }
01631
01632
01633 if (value != oldvalue && !(sd->desc.flags & SGF_MULTISTRING)) {
01634 if (this->clicked_entry != NULL) {
01635 this->clicked_entry->SetButtons(0);
01636 }
01637 this->clicked_entry = pe;
01638 this->clicked_entry->SetButtons((x >= 10) != (_current_text_dir == TD_RTL) ? SEF_RIGHT_DEPRESSED : SEF_LEFT_DEPRESSED);
01639 this->flags4 |= WF_TIMEOUT_BEGIN;
01640 _left_button_clicked = false;
01641 }
01642 break;
01643 }
01644
01645 default: NOT_REACHED();
01646 }
01647
01648 if (value != oldvalue) {
01649 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01650 SetCompanySetting(pe->d.entry.index, value);
01651 } else {
01652 SetSettingValue(pe->d.entry.index, value);
01653 }
01654 this->SetDirty();
01655 }
01656 } else {
01657
01658 if (sd->desc.cmd != SDT_BOOLX && !(sd->desc.flags & SGF_MULTISTRING)) {
01659
01660 if (sd->desc.flags & SGF_CURRENCY) value *= _currency->rate;
01661
01662 this->valuewindow_entry = pe;
01663 SetDParam(0, value);
01664 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, 100, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
01665 }
01666 }
01667 }
01668
01669 virtual void OnTimeout()
01670 {
01671 if (this->clicked_entry != NULL) {
01672 this->clicked_entry->SetButtons(0);
01673 this->clicked_entry = NULL;
01674 this->SetDirty();
01675 }
01676 }
01677
01678 virtual void OnQueryTextFinished(char *str)
01679 {
01680
01681 if (str == NULL) return;
01682
01683 assert(this->valuewindow_entry != NULL);
01684 assert((this->valuewindow_entry->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
01685 const SettingDesc *sd = this->valuewindow_entry->d.entry.setting;
01686
01687 int32 value;
01688 if (!StrEmpty(str)) {
01689 value = atoi(str);
01690
01691
01692 if (sd->desc.flags & SGF_CURRENCY) value /= _currency->rate;
01693 } else {
01694 value = (int32)(size_t)sd->desc.def;
01695 }
01696
01697 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01698 SetCompanySetting(this->valuewindow_entry->d.entry.index, value);
01699 } else {
01700 SetSettingValue(this->valuewindow_entry->d.entry.index, value);
01701 }
01702 this->SetDirty();
01703 }
01704
01705 virtual void OnResize()
01706 {
01707 this->vscroll->SetCapacityFromWidget(this, SETTINGSEL_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET);
01708 }
01709 };
01710
01711 GameSettings *GameSettingsWindow::settings_ptr = NULL;
01712
01713 static const NWidgetPart _nested_settings_selection_widgets[] = {
01714 NWidget(NWID_HORIZONTAL),
01715 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
01716 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_CONFIG_SETTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01717 EndContainer(),
01718 NWidget(NWID_HORIZONTAL),
01719 NWidget(WWT_PANEL, COLOUR_MAUVE, SETTINGSEL_OPTIONSPANEL), SetMinimalSize(400, 174), SetScrollbar(SETTINGSEL_SCROLLBAR), EndContainer(),
01720 NWidget(NWID_VERTICAL),
01721 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, SETTINGSEL_SCROLLBAR),
01722 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
01723 EndContainer(),
01724 EndContainer(),
01725 };
01726
01727 static const WindowDesc _settings_selection_desc(
01728 WDP_CENTER, 450, 397,
01729 WC_GAME_OPTIONS, WC_NONE,
01730 0,
01731 _nested_settings_selection_widgets, lengthof(_nested_settings_selection_widgets)
01732 );
01733
01735 void ShowGameSettings()
01736 {
01737 DeleteWindowById(WC_GAME_OPTIONS, 0);
01738 new GameSettingsWindow(&_settings_selection_desc);
01739 }
01740
01741
01751 void DrawArrowButtons(int x, int y, Colours button_colour, byte state, bool clickable_left, bool clickable_right)
01752 {
01753 int colour = _colour_gradient[button_colour][2];
01754
01755 DrawFrameRect(x, y + 1, x + 9, y + 9, button_colour, (state == 1) ? FR_LOWERED : FR_NONE);
01756 DrawFrameRect(x + 10, y + 1, x + 19, y + 9, button_colour, (state == 2) ? FR_LOWERED : FR_NONE);
01757 DrawSprite(SPR_ARROW_LEFT, PAL_NONE, x + WD_IMGBTN_LEFT, y + WD_IMGBTN_TOP);
01758 DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, x + WD_IMGBTN_LEFT + 10, y + WD_IMGBTN_TOP);
01759
01760
01761 bool rtl = _current_text_dir == TD_RTL;
01762 if (rtl ? !clickable_right : !clickable_left) {
01763 GfxFillRect(x + 1, y + 1, x + 1 + 8, y + 8, colour, FILLRECT_CHECKER);
01764 }
01765 if (rtl ? !clickable_left : !clickable_right) {
01766 GfxFillRect(x + 11, y + 1, x + 11 + 8, y + 8, colour, FILLRECT_CHECKER);
01767 }
01768 }
01769
01771 enum CustomCurrencyWidgets {
01772 CUSTCURR_RATE_DOWN,
01773 CUSTCURR_RATE_UP,
01774 CUSTCURR_RATE,
01775 CUSTCURR_SEPARATOR_EDIT,
01776 CUSTCURR_SEPARATOR,
01777 CUSTCURR_PREFIX_EDIT,
01778 CUSTCURR_PREFIX,
01779 CUSTCURR_SUFFIX_EDIT,
01780 CUSTCURR_SUFFIX,
01781 CUSTCURR_YEAR_DOWN,
01782 CUSTCURR_YEAR_UP,
01783 CUSTCURR_YEAR,
01784 CUSTCURR_PREVIEW,
01785 };
01786
01787 struct CustomCurrencyWindow : Window {
01788 int query_widget;
01789
01790 CustomCurrencyWindow(const WindowDesc *desc) : Window()
01791 {
01792 this->InitNested(desc);
01793
01794 SetButtonState();
01795 }
01796
01797 void SetButtonState()
01798 {
01799 this->SetWidgetDisabledState(CUSTCURR_RATE_DOWN, _custom_currency.rate == 1);
01800 this->SetWidgetDisabledState(CUSTCURR_RATE_UP, _custom_currency.rate == UINT16_MAX);
01801 this->SetWidgetDisabledState(CUSTCURR_YEAR_DOWN, _custom_currency.to_euro == CF_NOEURO);
01802 this->SetWidgetDisabledState(CUSTCURR_YEAR_UP, _custom_currency.to_euro == MAX_YEAR);
01803 }
01804
01805 virtual void SetStringParameters(int widget) const
01806 {
01807 switch (widget) {
01808 case CUSTCURR_RATE: SetDParam(0, 1); SetDParam(1, 1); break;
01809 case CUSTCURR_SEPARATOR: SetDParamStr(0, _custom_currency.separator); break;
01810 case CUSTCURR_PREFIX: SetDParamStr(0, _custom_currency.prefix); break;
01811 case CUSTCURR_SUFFIX: SetDParamStr(0, _custom_currency.suffix); break;
01812 case CUSTCURR_YEAR:
01813 SetDParam(0, (_custom_currency.to_euro != CF_NOEURO) ? STR_CURRENCY_SWITCH_TO_EURO : STR_CURRENCY_SWITCH_TO_EURO_NEVER);
01814 SetDParam(1, _custom_currency.to_euro);
01815 break;
01816
01817 case CUSTCURR_PREVIEW:
01818 SetDParam(0, 10000);
01819 break;
01820 }
01821 }
01822
01823 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01824 {
01825 switch (widget) {
01826
01827 case CUSTCURR_SEPARATOR_EDIT:
01828 case CUSTCURR_PREFIX_EDIT:
01829 case CUSTCURR_SUFFIX_EDIT:
01830 size->width = this->GetWidget<NWidgetBase>(CUSTCURR_RATE_DOWN)->smallest_x + this->GetWidget<NWidgetBase>(CUSTCURR_RATE_UP)->smallest_x;
01831 break;
01832
01833
01834 case CUSTCURR_RATE:
01835 SetDParam(0, 1);
01836 SetDParam(1, INT32_MAX);
01837 *size = GetStringBoundingBox(STR_CURRENCY_EXCHANGE_RATE);
01838 break;
01839 }
01840 }
01841
01842 virtual void OnClick(Point pt, int widget, int click_count)
01843 {
01844 int line = 0;
01845 int len = 0;
01846 StringID str = 0;
01847 CharSetFilter afilter = CS_ALPHANUMERAL;
01848
01849 switch (widget) {
01850 case CUSTCURR_RATE_DOWN:
01851 if (_custom_currency.rate > 1) _custom_currency.rate--;
01852 if (_custom_currency.rate == 1) this->DisableWidget(CUSTCURR_RATE_DOWN);
01853 this->EnableWidget(CUSTCURR_RATE_UP);
01854 break;
01855
01856 case CUSTCURR_RATE_UP:
01857 if (_custom_currency.rate < UINT16_MAX) _custom_currency.rate++;
01858 if (_custom_currency.rate == UINT16_MAX) this->DisableWidget(CUSTCURR_RATE_UP);
01859 this->EnableWidget(CUSTCURR_RATE_DOWN);
01860 break;
01861
01862 case CUSTCURR_RATE:
01863 SetDParam(0, _custom_currency.rate);
01864 str = STR_JUST_INT;
01865 len = 5;
01866 line = CUSTCURR_RATE;
01867 afilter = CS_NUMERAL;
01868 break;
01869
01870 case CUSTCURR_SEPARATOR_EDIT:
01871 case CUSTCURR_SEPARATOR:
01872 SetDParamStr(0, _custom_currency.separator);
01873 str = STR_JUST_RAW_STRING;
01874 len = 1;
01875 line = CUSTCURR_SEPARATOR;
01876 break;
01877
01878 case CUSTCURR_PREFIX_EDIT:
01879 case CUSTCURR_PREFIX:
01880 SetDParamStr(0, _custom_currency.prefix);
01881 str = STR_JUST_RAW_STRING;
01882 len = 12;
01883 line = CUSTCURR_PREFIX;
01884 break;
01885
01886 case CUSTCURR_SUFFIX_EDIT:
01887 case CUSTCURR_SUFFIX:
01888 SetDParamStr(0, _custom_currency.suffix);
01889 str = STR_JUST_RAW_STRING;
01890 len = 12;
01891 line = CUSTCURR_SUFFIX;
01892 break;
01893
01894 case CUSTCURR_YEAR_DOWN:
01895 _custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ? CF_NOEURO : _custom_currency.to_euro - 1;
01896 if (_custom_currency.to_euro == CF_NOEURO) this->DisableWidget(CUSTCURR_YEAR_DOWN);
01897 this->EnableWidget(CUSTCURR_YEAR_UP);
01898 break;
01899
01900 case CUSTCURR_YEAR_UP:
01901 _custom_currency.to_euro = Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
01902 if (_custom_currency.to_euro == MAX_YEAR) this->DisableWidget(CUSTCURR_YEAR_UP);
01903 this->EnableWidget(CUSTCURR_YEAR_DOWN);
01904 break;
01905
01906 case CUSTCURR_YEAR:
01907 SetDParam(0, _custom_currency.to_euro);
01908 str = STR_JUST_INT;
01909 len = 7;
01910 line = CUSTCURR_YEAR;
01911 afilter = CS_NUMERAL;
01912 break;
01913 }
01914
01915 if (len != 0) {
01916 this->query_widget = line;
01917 ShowQueryString(str, STR_CURRENCY_CHANGE_PARAMETER, len + 1, 250, this, afilter, QSF_NONE);
01918 }
01919
01920 this->flags4 |= WF_TIMEOUT_BEGIN;
01921 this->SetDirty();
01922 }
01923
01924 virtual void OnQueryTextFinished(char *str)
01925 {
01926 if (str == NULL) return;
01927
01928 switch (this->query_widget) {
01929 case CUSTCURR_RATE:
01930 _custom_currency.rate = Clamp(atoi(str), 1, UINT16_MAX);
01931 break;
01932
01933 case CUSTCURR_SEPARATOR:
01934 strecpy(_custom_currency.separator, str, lastof(_custom_currency.separator));
01935 break;
01936
01937 case CUSTCURR_PREFIX:
01938 strecpy(_custom_currency.prefix, str, lastof(_custom_currency.prefix));
01939 break;
01940
01941 case CUSTCURR_SUFFIX:
01942 strecpy(_custom_currency.suffix, str, lastof(_custom_currency.suffix));
01943 break;
01944
01945 case CUSTCURR_YEAR: {
01946 int val = atoi(str);
01947
01948 _custom_currency.to_euro = (val < 2000 ? CF_NOEURO : min(val, MAX_YEAR));
01949 break;
01950 }
01951 }
01952 MarkWholeScreenDirty();
01953 SetButtonState();
01954 }
01955
01956 virtual void OnTimeout()
01957 {
01958 this->SetDirty();
01959 }
01960 };
01961
01962 static const NWidgetPart _nested_cust_currency_widgets[] = {
01963 NWidget(NWID_HORIZONTAL),
01964 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01965 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CURRENCY_WINDOW, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01966 EndContainer(),
01967 NWidget(WWT_PANEL, COLOUR_GREY),
01968 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(7, 3, 0),
01969 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
01970 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_RATE_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_EXCHANGE_RATE_TOOLTIP),
01971 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_RATE_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_EXCHANGE_RATE_TOOLTIP),
01972 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
01973 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_RATE), SetDataTip(STR_CURRENCY_EXCHANGE_RATE, STR_CURRENCY_SET_EXCHANGE_RATE_TOOLTIP), SetFill(1, 0),
01974 EndContainer(),
01975 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
01976 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, CUSTCURR_SEPARATOR_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(0, 1),
01977 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
01978 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_SEPARATOR), SetDataTip(STR_CURRENCY_SEPARATOR, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(1, 0),
01979 EndContainer(),
01980 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
01981 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, CUSTCURR_PREFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(0, 1),
01982 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
01983 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_PREFIX), SetDataTip(STR_CURRENCY_PREFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(1, 0),
01984 EndContainer(),
01985 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
01986 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, CUSTCURR_SUFFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(0, 1),
01987 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
01988 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_SUFFIX), SetDataTip(STR_CURRENCY_SUFFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(1, 0),
01989 EndContainer(),
01990 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
01991 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_YEAR_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
01992 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_YEAR_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
01993 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
01994 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_YEAR), SetDataTip(STR_JUST_STRING, STR_CURRENCY_SET_CUSTOM_CURRENCY_TO_EURO_TOOLTIP), SetFill(1, 0),
01995 EndContainer(),
01996 EndContainer(),
01997 NWidget(WWT_LABEL, COLOUR_BLUE, CUSTCURR_PREVIEW),
01998 SetDataTip(STR_CURRENCY_PREVIEW, STR_CURRENCY_CUSTOM_CURRENCY_PREVIEW_TOOLTIP), SetPadding(15, 1, 18, 2),
01999 EndContainer(),
02000 };
02001
02002 static const WindowDesc _cust_currency_desc(
02003 WDP_CENTER, 0, 0,
02004 WC_CUSTOM_CURRENCY, WC_NONE,
02005 WDF_UNCLICK_BUTTONS,
02006 _nested_cust_currency_widgets, lengthof(_nested_cust_currency_widgets)
02007 );
02008
02010 static void ShowCustCurrency()
02011 {
02012 DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
02013 new CustomCurrencyWindow(&_cust_currency_desc);
02014 }