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 SettingEntry("economy.cargodest.mode_pax_mail"),
01479 SettingEntry("economy.cargodest.mode_town_cargo"),
01480 SettingEntry("economy.cargodest.mode_others"),
01481 };
01483 static SettingsPage _settings_economy_page = {_settings_economy, lengthof(_settings_economy)};
01484
01485 static SettingEntry _settings_ai_npc[] = {
01486 SettingEntry("ai.ai_in_multiplayer"),
01487 SettingEntry("ai.ai_disable_veh_train"),
01488 SettingEntry("ai.ai_disable_veh_roadveh"),
01489 SettingEntry("ai.ai_disable_veh_aircraft"),
01490 SettingEntry("ai.ai_disable_veh_ship"),
01491 SettingEntry("ai.ai_max_opcode_till_suspend"),
01492 };
01494 static SettingsPage _settings_ai_npc_page = {_settings_ai_npc, lengthof(_settings_ai_npc)};
01495
01496 static SettingEntry _settings_ai[] = {
01497 SettingEntry(&_settings_ai_npc_page, STR_CONFIG_SETTING_AI_NPC),
01498 SettingEntry("economy.give_money"),
01499 SettingEntry("economy.allow_shares"),
01500 };
01502 static SettingsPage _settings_ai_page = {_settings_ai, lengthof(_settings_ai)};
01503
01504 static SettingEntry _settings_vehicles_routing[] = {
01505 SettingEntry("pf.pathfinder_for_trains"),
01506 SettingEntry("pf.forbid_90_deg"),
01507 SettingEntry("pf.pathfinder_for_roadvehs"),
01508 SettingEntry("pf.roadveh_queue"),
01509 SettingEntry("pf.pathfinder_for_ships"),
01510 };
01512 static SettingsPage _settings_vehicles_routing_page = {_settings_vehicles_routing, lengthof(_settings_vehicles_routing)};
01513
01514 static SettingEntry _settings_vehicles_autorenew[] = {
01515 SettingEntry("company.engine_renew"),
01516 SettingEntry("company.engine_renew_months"),
01517 SettingEntry("company.engine_renew_money"),
01518 };
01520 static SettingsPage _settings_vehicles_autorenew_page = {_settings_vehicles_autorenew, lengthof(_settings_vehicles_autorenew)};
01521
01522 static SettingEntry _settings_vehicles_servicing[] = {
01523 SettingEntry("vehicle.servint_ispercent"),
01524 SettingEntry("vehicle.servint_trains"),
01525 SettingEntry("vehicle.servint_roadveh"),
01526 SettingEntry("vehicle.servint_ships"),
01527 SettingEntry("vehicle.servint_aircraft"),
01528 SettingEntry("order.no_servicing_if_no_breakdowns"),
01529 SettingEntry("order.serviceathelipad"),
01530 };
01532 static SettingsPage _settings_vehicles_servicing_page = {_settings_vehicles_servicing, lengthof(_settings_vehicles_servicing)};
01533
01534 static SettingEntry _settings_vehicles_trains[] = {
01535 SettingEntry("pf.reverse_at_signals"),
01536 SettingEntry("vehicle.train_acceleration_model"),
01537 SettingEntry("vehicle.train_slope_steepness"),
01538 SettingEntry("vehicle.max_train_length"),
01539 SettingEntry("vehicle.wagon_speed_limits"),
01540 SettingEntry("vehicle.disable_elrails"),
01541 SettingEntry("vehicle.freight_trains"),
01542 SettingEntry("gui.stop_location"),
01543 };
01545 static SettingsPage _settings_vehicles_trains_page = {_settings_vehicles_trains, lengthof(_settings_vehicles_trains)};
01546
01547 static SettingEntry _settings_vehicles[] = {
01548 SettingEntry(&_settings_vehicles_routing_page, STR_CONFIG_SETTING_VEHICLES_ROUTING),
01549 SettingEntry(&_settings_vehicles_autorenew_page, STR_CONFIG_SETTING_VEHICLES_AUTORENEW),
01550 SettingEntry(&_settings_vehicles_servicing_page, STR_CONFIG_SETTING_VEHICLES_SERVICING),
01551 SettingEntry(&_settings_vehicles_trains_page, STR_CONFIG_SETTING_VEHICLES_TRAINS),
01552 SettingEntry("gui.new_nonstop"),
01553 SettingEntry("gui.order_review_system"),
01554 SettingEntry("gui.vehicle_income_warn"),
01555 SettingEntry("gui.lost_vehicle_warn"),
01556 SettingEntry("vehicle.never_expire_vehicles"),
01557 SettingEntry("vehicle.max_trains"),
01558 SettingEntry("vehicle.max_roadveh"),
01559 SettingEntry("vehicle.max_aircraft"),
01560 SettingEntry("vehicle.max_ships"),
01561 SettingEntry("vehicle.plane_speed"),
01562 SettingEntry("vehicle.plane_crashes"),
01563 SettingEntry("vehicle.dynamic_engines"),
01564 SettingEntry("vehicle.roadveh_acceleration_model"),
01565 SettingEntry("vehicle.roadveh_slope_steepness"),
01566 SettingEntry("vehicle.smoke_amount"),
01567 };
01569 static SettingsPage _settings_vehicles_page = {_settings_vehicles, lengthof(_settings_vehicles)};
01570
01571 static SettingEntry _settings_main[] = {
01572 SettingEntry(&_settings_ui_page, STR_CONFIG_SETTING_GUI),
01573 SettingEntry(&_settings_construction_page, STR_CONFIG_SETTING_CONSTRUCTION),
01574 SettingEntry(&_settings_vehicles_page, STR_CONFIG_SETTING_VEHICLES),
01575 SettingEntry(&_settings_stations_page, STR_CONFIG_SETTING_STATIONS),
01576 SettingEntry(&_settings_economy_page, STR_CONFIG_SETTING_ECONOMY),
01577 SettingEntry(&_settings_ai_page, STR_CONFIG_SETTING_AI),
01578 };
01579
01581 static SettingsPage _settings_main_page = {_settings_main, lengthof(_settings_main)};
01582
01584 enum GameSettingsWidgets {
01585 SETTINGSEL_OPTIONSPANEL,
01586 SETTINGSEL_SCROLLBAR,
01587 };
01588
01589 struct GameSettingsWindow : Window {
01590 static const int SETTINGTREE_LEFT_OFFSET = 5;
01591 static const int SETTINGTREE_RIGHT_OFFSET = 5;
01592 static const int SETTINGTREE_TOP_OFFSET = 5;
01593 static const int SETTINGTREE_BOTTOM_OFFSET = 5;
01594
01595 static GameSettings *settings_ptr;
01596
01597 SettingEntry *valuewindow_entry;
01598 SettingEntry *clicked_entry;
01599
01600 Scrollbar *vscroll;
01601
01602 GameSettingsWindow(const WindowDesc *desc) : Window()
01603 {
01604 static bool first_time = true;
01605
01606 settings_ptr = &GetGameSettings();
01607
01608
01609 if (first_time) {
01610 _settings_main_page.Init();
01611 first_time = false;
01612 } else {
01613 _settings_main_page.FoldAll();
01614 }
01615
01616 this->valuewindow_entry = NULL;
01617 this->clicked_entry = NULL;
01618
01619 this->CreateNestedTree(desc);
01620 this->vscroll = this->GetScrollbar(SETTINGSEL_SCROLLBAR);
01621 this->FinishInitNested(desc, 0);
01622
01623 this->vscroll->SetCount(_settings_main_page.Length());
01624 }
01625
01626 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01627 {
01628 if (widget != SETTINGSEL_OPTIONSPANEL) return;
01629
01630 resize->height = SETTING_HEIGHT = max(11, FONT_HEIGHT_NORMAL + 1);
01631 resize->width = 1;
01632
01633 size->height = 5 * resize->height + SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET;
01634 }
01635
01636 virtual void DrawWidget(const Rect &r, int widget) const
01637 {
01638 if (widget != SETTINGSEL_OPTIONSPANEL) return;
01639
01640 _settings_main_page.Draw(settings_ptr, r.left + SETTINGTREE_LEFT_OFFSET, r.right - SETTINGTREE_RIGHT_OFFSET, r.top + SETTINGTREE_TOP_OFFSET,
01641 this->vscroll->GetPosition(), this->vscroll->GetPosition() + this->vscroll->GetCapacity());
01642 }
01643
01644 virtual void OnClick(Point pt, int widget, int click_count)
01645 {
01646 if (widget != SETTINGSEL_OPTIONSPANEL) return;
01647
01648 uint btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, SETTINGSEL_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET - 1);
01649 if (btn == INT_MAX) return;
01650
01651 uint cur_row = 0;
01652 SettingEntry *pe = _settings_main_page.FindEntry(btn, &cur_row);
01653
01654 if (pe == NULL) return;
01655
01656 int x = (_current_text_dir == TD_RTL ? this->width - pt.x : pt.x) - SETTINGTREE_LEFT_OFFSET - (pe->level + 1) * LEVEL_WIDTH;
01657 if (x < 0) return;
01658
01659 if ((pe->flags & SEF_KIND_MASK) == SEF_SUBTREE_KIND) {
01660 pe->d.sub.folded = !pe->d.sub.folded;
01661
01662 this->vscroll->SetCount(_settings_main_page.Length());
01663 this->SetDirty();
01664 return;
01665 }
01666
01667 assert((pe->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
01668 const SettingDesc *sd = pe->d.entry.setting;
01669
01670
01671 if (!(sd->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !_network_server && !(sd->desc.flags & SGF_PER_COMPANY)) return;
01672 if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking) return;
01673 if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return;
01674
01675 const void *var = ResolveVariableAddress(settings_ptr, sd);
01676 int32 value = (int32)ReadValue(var, sd->save.conv);
01677
01678
01679 if (x < 21) {
01680 const SettingDescBase *sdb = &sd->desc;
01681 int32 oldvalue = value;
01682
01683 switch (sdb->cmd) {
01684 case SDT_BOOLX: value ^= 1; break;
01685 case SDT_ONEOFMANY:
01686 case SDT_NUMX: {
01687
01688
01689
01690
01691 uint32 step = (sdb->interval == 0) ? ((sdb->max - sdb->min) / 50) : sdb->interval;
01692 if (step == 0) step = 1;
01693
01694
01695 if ((this->flags4 & WF_TIMEOUT_MASK) > WF_TIMEOUT_TRIGGER) {
01696 _left_button_clicked = false;
01697 return;
01698 }
01699
01700
01701 if (x >= 10) {
01702 value += step;
01703 if (sdb->min < 0) {
01704 assert((int32)sdb->max >= 0);
01705 if (value > (int32)sdb->max) value = (int32)sdb->max;
01706 } else {
01707 if ((uint32)value > sdb->max) value = (int32)sdb->max;
01708 }
01709 if (value < sdb->min) value = sdb->min;
01710 } else {
01711 value -= step;
01712 if (value < sdb->min) value = (sdb->flags & SGF_0ISDISABLED) ? 0 : sdb->min;
01713 }
01714
01715
01716 if (value != oldvalue && !(sd->desc.flags & SGF_MULTISTRING)) {
01717 if (this->clicked_entry != NULL) {
01718 this->clicked_entry->SetButtons(0);
01719 }
01720 this->clicked_entry = pe;
01721 this->clicked_entry->SetButtons((x >= 10) != (_current_text_dir == TD_RTL) ? SEF_RIGHT_DEPRESSED : SEF_LEFT_DEPRESSED);
01722 this->flags4 |= WF_TIMEOUT_BEGIN;
01723 _left_button_clicked = false;
01724 }
01725 break;
01726 }
01727
01728 default: NOT_REACHED();
01729 }
01730
01731 if (value != oldvalue) {
01732 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01733 SetCompanySetting(pe->d.entry.index, value);
01734 } else {
01735 SetSettingValue(pe->d.entry.index, value);
01736 }
01737 this->SetDirty();
01738 }
01739 } else {
01740
01741 if (sd->desc.cmd != SDT_BOOLX && !(sd->desc.flags & SGF_MULTISTRING)) {
01742
01743 if (sd->desc.flags & SGF_CURRENCY) value *= _currency->rate;
01744
01745 this->valuewindow_entry = pe;
01746 SetDParam(0, value);
01747 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
01748 }
01749 }
01750 }
01751
01752 virtual void OnTimeout()
01753 {
01754 if (this->clicked_entry != NULL) {
01755 this->clicked_entry->SetButtons(0);
01756 this->clicked_entry = NULL;
01757 this->SetDirty();
01758 }
01759 }
01760
01761 virtual void OnQueryTextFinished(char *str)
01762 {
01763
01764 if (str == NULL) return;
01765
01766 assert(this->valuewindow_entry != NULL);
01767 assert((this->valuewindow_entry->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
01768 const SettingDesc *sd = this->valuewindow_entry->d.entry.setting;
01769
01770 int32 value;
01771 if (!StrEmpty(str)) {
01772 value = atoi(str);
01773
01774
01775 if (sd->desc.flags & SGF_CURRENCY) value /= _currency->rate;
01776 } else {
01777 value = (int32)(size_t)sd->desc.def;
01778 }
01779
01780 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01781 SetCompanySetting(this->valuewindow_entry->d.entry.index, value);
01782 } else {
01783 SetSettingValue(this->valuewindow_entry->d.entry.index, value);
01784 }
01785 this->SetDirty();
01786 }
01787
01788 virtual void OnResize()
01789 {
01790 this->vscroll->SetCapacityFromWidget(this, SETTINGSEL_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET);
01791 }
01792 };
01793
01794 GameSettings *GameSettingsWindow::settings_ptr = NULL;
01795
01796 static const NWidgetPart _nested_settings_selection_widgets[] = {
01797 NWidget(NWID_HORIZONTAL),
01798 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
01799 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_CONFIG_SETTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01800 EndContainer(),
01801 NWidget(NWID_HORIZONTAL),
01802 NWidget(WWT_PANEL, COLOUR_MAUVE, SETTINGSEL_OPTIONSPANEL), SetMinimalSize(400, 174), SetScrollbar(SETTINGSEL_SCROLLBAR), EndContainer(),
01803 NWidget(NWID_VERTICAL),
01804 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, SETTINGSEL_SCROLLBAR),
01805 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
01806 EndContainer(),
01807 EndContainer(),
01808 };
01809
01810 static const WindowDesc _settings_selection_desc(
01811 WDP_CENTER, 450, 397,
01812 WC_GAME_OPTIONS, WC_NONE,
01813 0,
01814 _nested_settings_selection_widgets, lengthof(_nested_settings_selection_widgets)
01815 );
01816
01818 void ShowGameSettings()
01819 {
01820 DeleteWindowById(WC_GAME_OPTIONS, 0);
01821 new GameSettingsWindow(&_settings_selection_desc);
01822 }
01823
01824
01834 void DrawArrowButtons(int x, int y, Colours button_colour, byte state, bool clickable_left, bool clickable_right)
01835 {
01836 int colour = _colour_gradient[button_colour][2];
01837
01838 DrawFrameRect(x, y + 1, x + 9, y + 9, button_colour, (state == 1) ? FR_LOWERED : FR_NONE);
01839 DrawFrameRect(x + 10, y + 1, x + 19, y + 9, button_colour, (state == 2) ? FR_LOWERED : FR_NONE);
01840 DrawSprite(SPR_ARROW_LEFT, PAL_NONE, x + WD_IMGBTN_LEFT, y + WD_IMGBTN_TOP);
01841 DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, x + WD_IMGBTN_LEFT + 10, y + WD_IMGBTN_TOP);
01842
01843
01844 bool rtl = _current_text_dir == TD_RTL;
01845 if (rtl ? !clickable_right : !clickable_left) {
01846 GfxFillRect(x + 1, y + 1, x + 1 + 8, y + 8, colour, FILLRECT_CHECKER);
01847 }
01848 if (rtl ? !clickable_left : !clickable_right) {
01849 GfxFillRect(x + 11, y + 1, x + 11 + 8, y + 8, colour, FILLRECT_CHECKER);
01850 }
01851 }
01852
01854 enum CustomCurrencyWidgets {
01855 CUSTCURR_RATE_DOWN,
01856 CUSTCURR_RATE_UP,
01857 CUSTCURR_RATE,
01858 CUSTCURR_SEPARATOR_EDIT,
01859 CUSTCURR_SEPARATOR,
01860 CUSTCURR_PREFIX_EDIT,
01861 CUSTCURR_PREFIX,
01862 CUSTCURR_SUFFIX_EDIT,
01863 CUSTCURR_SUFFIX,
01864 CUSTCURR_YEAR_DOWN,
01865 CUSTCURR_YEAR_UP,
01866 CUSTCURR_YEAR,
01867 CUSTCURR_PREVIEW,
01868 };
01869
01870 struct CustomCurrencyWindow : Window {
01871 int query_widget;
01872
01873 CustomCurrencyWindow(const WindowDesc *desc) : Window()
01874 {
01875 this->InitNested(desc);
01876
01877 SetButtonState();
01878 }
01879
01880 void SetButtonState()
01881 {
01882 this->SetWidgetDisabledState(CUSTCURR_RATE_DOWN, _custom_currency.rate == 1);
01883 this->SetWidgetDisabledState(CUSTCURR_RATE_UP, _custom_currency.rate == UINT16_MAX);
01884 this->SetWidgetDisabledState(CUSTCURR_YEAR_DOWN, _custom_currency.to_euro == CF_NOEURO);
01885 this->SetWidgetDisabledState(CUSTCURR_YEAR_UP, _custom_currency.to_euro == MAX_YEAR);
01886 }
01887
01888 virtual void SetStringParameters(int widget) const
01889 {
01890 switch (widget) {
01891 case CUSTCURR_RATE: SetDParam(0, 1); SetDParam(1, 1); break;
01892 case CUSTCURR_SEPARATOR: SetDParamStr(0, _custom_currency.separator); break;
01893 case CUSTCURR_PREFIX: SetDParamStr(0, _custom_currency.prefix); break;
01894 case CUSTCURR_SUFFIX: SetDParamStr(0, _custom_currency.suffix); break;
01895 case CUSTCURR_YEAR:
01896 SetDParam(0, (_custom_currency.to_euro != CF_NOEURO) ? STR_CURRENCY_SWITCH_TO_EURO : STR_CURRENCY_SWITCH_TO_EURO_NEVER);
01897 SetDParam(1, _custom_currency.to_euro);
01898 break;
01899
01900 case CUSTCURR_PREVIEW:
01901 SetDParam(0, 10000);
01902 break;
01903 }
01904 }
01905
01906 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01907 {
01908 switch (widget) {
01909
01910 case CUSTCURR_SEPARATOR_EDIT:
01911 case CUSTCURR_PREFIX_EDIT:
01912 case CUSTCURR_SUFFIX_EDIT:
01913 size->width = this->GetWidget<NWidgetBase>(CUSTCURR_RATE_DOWN)->smallest_x + this->GetWidget<NWidgetBase>(CUSTCURR_RATE_UP)->smallest_x;
01914 break;
01915
01916
01917 case CUSTCURR_RATE:
01918 SetDParam(0, 1);
01919 SetDParam(1, INT32_MAX);
01920 *size = GetStringBoundingBox(STR_CURRENCY_EXCHANGE_RATE);
01921 break;
01922 }
01923 }
01924
01925 virtual void OnClick(Point pt, int widget, int click_count)
01926 {
01927 int line = 0;
01928 int len = 0;
01929 StringID str = 0;
01930 CharSetFilter afilter = CS_ALPHANUMERAL;
01931
01932 switch (widget) {
01933 case CUSTCURR_RATE_DOWN:
01934 if (_custom_currency.rate > 1) _custom_currency.rate--;
01935 if (_custom_currency.rate == 1) this->DisableWidget(CUSTCURR_RATE_DOWN);
01936 this->EnableWidget(CUSTCURR_RATE_UP);
01937 break;
01938
01939 case CUSTCURR_RATE_UP:
01940 if (_custom_currency.rate < UINT16_MAX) _custom_currency.rate++;
01941 if (_custom_currency.rate == UINT16_MAX) this->DisableWidget(CUSTCURR_RATE_UP);
01942 this->EnableWidget(CUSTCURR_RATE_DOWN);
01943 break;
01944
01945 case CUSTCURR_RATE:
01946 SetDParam(0, _custom_currency.rate);
01947 str = STR_JUST_INT;
01948 len = 5;
01949 line = CUSTCURR_RATE;
01950 afilter = CS_NUMERAL;
01951 break;
01952
01953 case CUSTCURR_SEPARATOR_EDIT:
01954 case CUSTCURR_SEPARATOR:
01955 SetDParamStr(0, _custom_currency.separator);
01956 str = STR_JUST_RAW_STRING;
01957 len = 1;
01958 line = CUSTCURR_SEPARATOR;
01959 break;
01960
01961 case CUSTCURR_PREFIX_EDIT:
01962 case CUSTCURR_PREFIX:
01963 SetDParamStr(0, _custom_currency.prefix);
01964 str = STR_JUST_RAW_STRING;
01965 len = 12;
01966 line = CUSTCURR_PREFIX;
01967 break;
01968
01969 case CUSTCURR_SUFFIX_EDIT:
01970 case CUSTCURR_SUFFIX:
01971 SetDParamStr(0, _custom_currency.suffix);
01972 str = STR_JUST_RAW_STRING;
01973 len = 12;
01974 line = CUSTCURR_SUFFIX;
01975 break;
01976
01977 case CUSTCURR_YEAR_DOWN:
01978 _custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ? CF_NOEURO : _custom_currency.to_euro - 1;
01979 if (_custom_currency.to_euro == CF_NOEURO) this->DisableWidget(CUSTCURR_YEAR_DOWN);
01980 this->EnableWidget(CUSTCURR_YEAR_UP);
01981 break;
01982
01983 case CUSTCURR_YEAR_UP:
01984 _custom_currency.to_euro = Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
01985 if (_custom_currency.to_euro == MAX_YEAR) this->DisableWidget(CUSTCURR_YEAR_UP);
01986 this->EnableWidget(CUSTCURR_YEAR_DOWN);
01987 break;
01988
01989 case CUSTCURR_YEAR:
01990 SetDParam(0, _custom_currency.to_euro);
01991 str = STR_JUST_INT;
01992 len = 7;
01993 line = CUSTCURR_YEAR;
01994 afilter = CS_NUMERAL;
01995 break;
01996 }
01997
01998 if (len != 0) {
01999 this->query_widget = line;
02000 ShowQueryString(str, STR_CURRENCY_CHANGE_PARAMETER, len + 1, this, afilter, QSF_NONE);
02001 }
02002
02003 this->flags4 |= WF_TIMEOUT_BEGIN;
02004 this->SetDirty();
02005 }
02006
02007 virtual void OnQueryTextFinished(char *str)
02008 {
02009 if (str == NULL) return;
02010
02011 switch (this->query_widget) {
02012 case CUSTCURR_RATE:
02013 _custom_currency.rate = Clamp(atoi(str), 1, UINT16_MAX);
02014 break;
02015
02016 case CUSTCURR_SEPARATOR:
02017 strecpy(_custom_currency.separator, str, lastof(_custom_currency.separator));
02018 break;
02019
02020 case CUSTCURR_PREFIX:
02021 strecpy(_custom_currency.prefix, str, lastof(_custom_currency.prefix));
02022 break;
02023
02024 case CUSTCURR_SUFFIX:
02025 strecpy(_custom_currency.suffix, str, lastof(_custom_currency.suffix));
02026 break;
02027
02028 case CUSTCURR_YEAR: {
02029 int val = atoi(str);
02030
02031 _custom_currency.to_euro = (val < 2000 ? CF_NOEURO : min(val, MAX_YEAR));
02032 break;
02033 }
02034 }
02035 MarkWholeScreenDirty();
02036 SetButtonState();
02037 }
02038
02039 virtual void OnTimeout()
02040 {
02041 this->SetDirty();
02042 }
02043 };
02044
02045 static const NWidgetPart _nested_cust_currency_widgets[] = {
02046 NWidget(NWID_HORIZONTAL),
02047 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
02048 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CURRENCY_WINDOW, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
02049 EndContainer(),
02050 NWidget(WWT_PANEL, COLOUR_GREY),
02051 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(7, 3, 0),
02052 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02053 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_RATE_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_EXCHANGE_RATE_TOOLTIP),
02054 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_RATE_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_EXCHANGE_RATE_TOOLTIP),
02055 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02056 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_RATE), SetDataTip(STR_CURRENCY_EXCHANGE_RATE, STR_CURRENCY_SET_EXCHANGE_RATE_TOOLTIP), SetFill(1, 0),
02057 EndContainer(),
02058 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02059 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, CUSTCURR_SEPARATOR_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(0, 1),
02060 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02061 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_SEPARATOR), SetDataTip(STR_CURRENCY_SEPARATOR, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(1, 0),
02062 EndContainer(),
02063 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02064 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, CUSTCURR_PREFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(0, 1),
02065 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02066 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_PREFIX), SetDataTip(STR_CURRENCY_PREFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(1, 0),
02067 EndContainer(),
02068 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02069 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, CUSTCURR_SUFFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(0, 1),
02070 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02071 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_SUFFIX), SetDataTip(STR_CURRENCY_SUFFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(1, 0),
02072 EndContainer(),
02073 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02074 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_YEAR_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
02075 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_YEAR_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
02076 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02077 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_YEAR), SetDataTip(STR_JUST_STRING, STR_CURRENCY_SET_CUSTOM_CURRENCY_TO_EURO_TOOLTIP), SetFill(1, 0),
02078 EndContainer(),
02079 EndContainer(),
02080 NWidget(WWT_LABEL, COLOUR_BLUE, CUSTCURR_PREVIEW),
02081 SetDataTip(STR_CURRENCY_PREVIEW, STR_CURRENCY_CUSTOM_CURRENCY_PREVIEW_TOOLTIP), SetPadding(15, 1, 18, 2),
02082 EndContainer(),
02083 };
02084
02085 static const WindowDesc _cust_currency_desc(
02086 WDP_CENTER, 0, 0,
02087 WC_CUSTOM_CURRENCY, WC_NONE,
02088 WDF_UNCLICK_BUTTONS,
02089 _nested_cust_currency_widgets, lengthof(_nested_cust_currency_widgets)
02090 );
02091
02093 static void ShowCustCurrency()
02094 {
02095 DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
02096 new CustomCurrencyWindow(&_cust_currency_desc);
02097 }