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