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(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 CheckForMissingGlyphs();
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->SetWidgetsDisabledState(_game_mode != GM_MENU,
00665 GDW_LVL_EASY,
00666 GDW_LVL_MEDIUM,
00667 GDW_LVL_HARD,
00668 GDW_LVL_CUSTOM,
00669 WIDGET_LIST_END);
00670 this->SetWidgetDisabledState(GDW_HIGHSCORE, _game_mode == GM_EDITOR || _networking);
00671 this->SetWidgetDisabledState(GDW_ACCEPT, _networking && !_network_server);
00672
00673
00674 this->OnInvalidateData(GOID_DIFFICULTY_CHANGED);
00675 }
00676
00677 virtual void SetStringParameters(int widget) const
00678 {
00679 widget -= GDW_OPTIONS_START;
00680 if (widget < 0 || (widget % 3) != 2) return;
00681
00682 widget /= 3;
00683
00684 uint i;
00685 const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i) + widget;
00686 int32 value = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00687 SetDParam(0, sd->desc.val_str + value);
00688 }
00689
00690 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00691 {
00692
00693 int index = widget - GDW_OPTIONS_START;
00694 if (index < 0 || (index % 3) != 2) return;
00695
00696 index /= 3;
00697
00698 uint i;
00699 const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i) + index;
00700 const SettingDescBase *sdb = &sd->desc;
00701
00702
00703 StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
00704 for (int32 value = sdb->min; (uint32)value <= sdb->max; value += sdb->interval) {
00705 SetDParam(0, sdb->val_str + value);
00706 *size = maxdim(*size, GetStringBoundingBox(str));
00707 }
00708 }
00709
00710 virtual void OnClick(Point pt, int widget, int click_count)
00711 {
00712 if (widget >= GDW_OPTIONS_START) {
00713 widget -= GDW_OPTIONS_START;
00714 if ((widget % 3) == 2) return;
00715
00716
00717 if (_networking && !_network_server) return;
00718
00719 uint i;
00720 const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i) + (widget / 3);
00721 const SettingDescBase *sdb = &sd->desc;
00722
00723 int32 val = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00724 if (widget % 3 == 1) {
00725
00726 val = min(val + sdb->interval, (int32)sdb->max);
00727 } else {
00728
00729 val -= sdb->interval;
00730 val = max(val, sdb->min);
00731 }
00732
00733
00734 WriteValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv, val);
00735 this->RaiseWidget(GDW_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00736 SetDifficultyLevel(3, &this->opt_mod_temp.difficulty);
00737 this->LowerWidget(GDW_LVL_CUSTOM);
00738 this->InvalidateData();
00739
00740 if (widget / 3 == 0 &&
00741 AI::GetInfoList()->size() == 0 &&
00742 this->opt_mod_temp.difficulty.max_no_competitors != 0) {
00743 ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
00744 }
00745 return;
00746 }
00747
00748 switch (widget) {
00749 case GDW_LVL_EASY:
00750 case GDW_LVL_MEDIUM:
00751 case GDW_LVL_HARD:
00752 case GDW_LVL_CUSTOM:
00753
00754 this->RaiseWidget(GDW_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00755 SetDifficultyLevel(widget - GDW_LVL_EASY, &this->opt_mod_temp.difficulty);
00756 this->LowerWidget(GDW_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00757 this->InvalidateData();
00758 break;
00759
00760 case GDW_HIGHSCORE:
00761 ShowHighscoreTable(this->opt_mod_temp.difficulty.diff_level, -1);
00762 break;
00763
00764 case GDW_ACCEPT: {
00765 GameSettings *opt_ptr = &GetGameSettings();
00766
00767 uint i;
00768 GetSettingFromName("difficulty.diff_level", &i);
00769 DoCommandP(0, i, this->opt_mod_temp.difficulty.diff_level, CMD_CHANGE_SETTING);
00770
00771 const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i);
00772 for (uint btn = 0; btn != GAME_DIFFICULTY_NUM; btn++, sd++) {
00773 int32 new_val = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00774 int32 cur_val = (int32)ReadValue(GetVariableAddress(opt_ptr, &sd->save), sd->save.conv);
00775
00776 if (new_val != cur_val) {
00777 DoCommandP(0, i + btn, new_val, CMD_CHANGE_SETTING);
00778 }
00779 }
00780 delete this;
00781
00782
00783
00784 if (_game_mode == GM_EDITOR) StartupEconomy();
00785 break;
00786 }
00787
00788 case GDW_CANCEL:
00789 delete this;
00790 break;
00791 }
00792 }
00793
00799 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00800 {
00801 if (!gui_scope) return;
00802
00803 if (data == GOID_DIFFICULTY_CHANGED) {
00804
00805
00806
00807
00808 this->opt_mod_temp = GetGameSettings();
00809
00810 this->LowerWidget(GDW_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00811 }
00812
00813 uint i;
00814 const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i);
00815 for (i = 0; i < GAME_DIFFICULTY_NUM; i++, sd++) {
00816 const SettingDescBase *sdb = &sd->desc;
00817
00818 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
00819 int32 value = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00820 bool disable = (sd->desc.flags & SGF_NEWGAME_ONLY) &&
00821 (_game_mode == GM_NORMAL ||
00822 (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0));
00823
00824 this->SetWidgetDisabledState(GDW_OPTIONS_START + i * 3 + 0, disable || sdb->min == value);
00825 this->SetWidgetDisabledState(GDW_OPTIONS_START + i * 3 + 1, disable || sdb->max == (uint32)value);
00826 }
00827 }
00828 };
00829
00830 static NWidgetBase *MakeDifficultyOptionsWidgets(int *biggest_index)
00831 {
00832 NWidgetVertical *vert_desc = new NWidgetVertical;
00833
00834 int widnum = GDW_OPTIONS_START;
00835 uint i, j;
00836 const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i);
00837
00838 for (i = 0, j = 0; i < GameDifficultyWindow::GAME_DIFFICULTY_NUM; i++, sd++, widnum += GameDifficultyWindow::WIDGETS_PER_DIFFICULTY) {
00839 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
00840
00841 NWidgetHorizontal *hor = new NWidgetHorizontal;
00842
00843
00844 NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum, AWV_DECREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
00845 hor->Add(leaf);
00846
00847
00848 leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum + 1, AWV_INCREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
00849 hor->Add(leaf);
00850
00851
00852 NWidgetSpacer *spacer = new NWidgetSpacer(5, 0);
00853 hor->Add(spacer);
00854
00855
00856 leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, widnum + 2, STR_DIFFICULTY_LEVEL_SETTING_MAXIMUM_NO_COMPETITORS + (j++), STR_NULL);
00857 leaf->SetFill(1, 0);
00858 hor->Add(leaf);
00859 vert_desc->Add(hor);
00860
00861
00862 vert_desc->Add(new NWidgetSpacer(0, 2));
00863 }
00864 *biggest_index = widnum - 1;
00865 return vert_desc;
00866 }
00867
00868
00870 static const NWidgetPart _nested_game_difficulty_widgets[] = {
00871 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_DIFFICULTY_LEVEL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00872 NWidget(WWT_PANEL, COLOUR_MAUVE),
00873 NWidget(NWID_VERTICAL), SetPIP(2, 0, 2),
00874 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
00875 NWidget(WWT_TEXTBTN, COLOUR_YELLOW, GDW_LVL_EASY), SetDataTip(STR_DIFFICULTY_LEVEL_EASY, STR_NULL), SetFill(1, 0),
00876 NWidget(WWT_TEXTBTN, COLOUR_YELLOW, GDW_LVL_MEDIUM), SetDataTip(STR_DIFFICULTY_LEVEL_MEDIUM, STR_NULL), SetFill(1, 0),
00877 NWidget(WWT_TEXTBTN, COLOUR_YELLOW, GDW_LVL_HARD), SetDataTip(STR_DIFFICULTY_LEVEL_HARD, STR_NULL), SetFill(1, 0),
00878 NWidget(WWT_TEXTBTN, COLOUR_YELLOW, GDW_LVL_CUSTOM), SetDataTip(STR_DIFFICULTY_LEVEL_CUSTOM, STR_NULL), SetFill(1, 0),
00879 EndContainer(),
00880 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
00881 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, GDW_HIGHSCORE), SetDataTip(STR_DIFFICULTY_LEVEL_HIGH_SCORE_BUTTON, STR_NULL), SetFill(1, 0),
00882 EndContainer(),
00883 EndContainer(),
00884 EndContainer(),
00885 NWidget(WWT_PANEL, COLOUR_MAUVE),
00886 NWidget(NWID_VERTICAL), SetPIP(3, 0, 1),
00887 NWidget(NWID_HORIZONTAL), SetPIP(5, 0, 5),
00888 NWidgetFunction(MakeDifficultyOptionsWidgets),
00889 EndContainer(),
00890 EndContainer(),
00891 EndContainer(),
00892 NWidget(WWT_PANEL, COLOUR_MAUVE),
00893 NWidget(NWID_VERTICAL), SetPIP(2, 0, 2),
00894 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
00895 NWidget(NWID_SPACER), SetFill(1, 0),
00896 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, GDW_ACCEPT), SetDataTip(STR_DIFFICULTY_LEVEL_SAVE, STR_NULL), SetFill(1, 0),
00897 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, GDW_CANCEL), SetDataTip(STR_BUTTON_CANCEL, STR_NULL), SetFill(1, 0),
00898 NWidget(NWID_SPACER), SetFill(1, 0),
00899 EndContainer(),
00900 EndContainer(),
00901 EndContainer(),
00902 };
00903
00905 static const WindowDesc _game_difficulty_desc(
00906 WDP_CENTER, 0, 0,
00907 WC_GAME_OPTIONS, WC_NONE,
00908 WDF_UNCLICK_BUTTONS,
00909 _nested_game_difficulty_widgets, lengthof(_nested_game_difficulty_widgets)
00910 );
00911
00913 void ShowGameDifficulty()
00914 {
00915 DeleteWindowById(WC_GAME_OPTIONS, 0);
00916 new GameDifficultyWindow(&_game_difficulty_desc);
00917 }
00918
00919 static int SETTING_HEIGHT = 11;
00920 static const int LEVEL_WIDTH = 15;
00921
00926 enum SettingEntryFlags {
00927 SEF_LEFT_DEPRESSED = 0x01,
00928 SEF_RIGHT_DEPRESSED = 0x02,
00929 SEF_BUTTONS_MASK = (SEF_LEFT_DEPRESSED | SEF_RIGHT_DEPRESSED),
00930
00931 SEF_LAST_FIELD = 0x04,
00932
00933
00934 SEF_SETTING_KIND = 0x10,
00935 SEF_SUBTREE_KIND = 0x20,
00936 SEF_KIND_MASK = (SEF_SETTING_KIND | SEF_SUBTREE_KIND),
00937 };
00938
00939 struct SettingsPage;
00940
00942 struct SettingEntrySubtree {
00943 SettingsPage *page;
00944 bool folded;
00945 StringID title;
00946 };
00947
00949 struct SettingEntrySetting {
00950 const char *name;
00951 const SettingDesc *setting;
00952 uint index;
00953 };
00954
00956 struct SettingEntry {
00957 byte flags;
00958 byte level;
00959 union {
00960 SettingEntrySetting entry;
00961 SettingEntrySubtree sub;
00962 } d;
00963
00964 SettingEntry(const char *nm);
00965 SettingEntry(SettingsPage *sub, StringID title);
00966
00967 void Init(byte level, bool last_field);
00968 void FoldAll();
00969 void SetButtons(byte new_val);
00970
00971 uint Length() const;
00972 SettingEntry *FindEntry(uint row, uint *cur_row);
00973
00974 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);
00975
00976 private:
00977 void DrawSetting(GameSettings *settings_ptr, const SettingDesc *sd, int x, int y, int max_x, int state);
00978 };
00979
00981 struct SettingsPage {
00982 SettingEntry *entries;
00983 byte num;
00984
00985 void Init(byte level = 0);
00986 void FoldAll();
00987
00988 uint Length() const;
00989 SettingEntry *FindEntry(uint row, uint *cur_row) const;
00990
00991 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;
00992 };
00993
00994
00995
00996
01001 SettingEntry::SettingEntry(const char *nm)
01002 {
01003 this->flags = SEF_SETTING_KIND;
01004 this->level = 0;
01005 this->d.entry.name = nm;
01006 this->d.entry.setting = NULL;
01007 this->d.entry.index = 0;
01008 }
01009
01015 SettingEntry::SettingEntry(SettingsPage *sub, StringID title)
01016 {
01017 this->flags = SEF_SUBTREE_KIND;
01018 this->level = 0;
01019 this->d.sub.page = sub;
01020 this->d.sub.folded = true;
01021 this->d.sub.title = title;
01022 }
01023
01029 void SettingEntry::Init(byte level, bool last_field)
01030 {
01031 this->level = level;
01032 if (last_field) this->flags |= SEF_LAST_FIELD;
01033
01034 switch (this->flags & SEF_KIND_MASK) {
01035 case SEF_SETTING_KIND:
01036 this->d.entry.setting = GetSettingFromName(this->d.entry.name, &this->d.entry.index);
01037 assert(this->d.entry.setting != NULL);
01038 break;
01039 case SEF_SUBTREE_KIND:
01040 this->d.sub.page->Init(level + 1);
01041 break;
01042 default: NOT_REACHED();
01043 }
01044 }
01045
01047 void SettingEntry::FoldAll()
01048 {
01049 switch (this->flags & SEF_KIND_MASK) {
01050 case SEF_SETTING_KIND:
01051 break;
01052
01053 case SEF_SUBTREE_KIND:
01054 this->d.sub.folded = true;
01055 this->d.sub.page->FoldAll();
01056 break;
01057
01058 default: NOT_REACHED();
01059 }
01060 }
01061
01062
01068 void SettingEntry::SetButtons(byte new_val)
01069 {
01070 assert((new_val & ~SEF_BUTTONS_MASK) == 0);
01071 this->flags = (this->flags & ~SEF_BUTTONS_MASK) | new_val;
01072 }
01073
01075 uint SettingEntry::Length() const
01076 {
01077 switch (this->flags & SEF_KIND_MASK) {
01078 case SEF_SETTING_KIND:
01079 return 1;
01080 case SEF_SUBTREE_KIND:
01081 if (this->d.sub.folded) return 1;
01082
01083 return 1 + this->d.sub.page->Length();
01084 default: NOT_REACHED();
01085 }
01086 }
01087
01094 SettingEntry *SettingEntry::FindEntry(uint row_num, uint *cur_row)
01095 {
01096 if (row_num == *cur_row) return this;
01097
01098 switch (this->flags & SEF_KIND_MASK) {
01099 case SEF_SETTING_KIND:
01100 (*cur_row)++;
01101 break;
01102 case SEF_SUBTREE_KIND:
01103 (*cur_row)++;
01104 if (this->d.sub.folded) {
01105 break;
01106 }
01107
01108
01109 return this->d.sub.page->FindEntry(row_num, cur_row);
01110 default: NOT_REACHED();
01111 }
01112 return NULL;
01113 }
01114
01141 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)
01142 {
01143 if (cur_row >= max_row) return cur_row;
01144
01145 bool rtl = _current_text_dir == TD_RTL;
01146 int offset = rtl ? -4 : 4;
01147 int level_width = rtl ? -LEVEL_WIDTH : LEVEL_WIDTH;
01148
01149 int x = rtl ? right : left;
01150 int y = base_y;
01151 if (cur_row >= first_row) {
01152 int colour = _colour_gradient[COLOUR_ORANGE][4];
01153 y = base_y + (cur_row - first_row) * SETTING_HEIGHT;
01154
01155
01156 for (uint lvl = 0; lvl < this->level; lvl++) {
01157 if (!HasBit(parent_last, lvl)) GfxDrawLine(x + offset, y, x + offset, y + SETTING_HEIGHT - 1, colour);
01158 x += level_width;
01159 }
01160
01161 int halfway_y = y + SETTING_HEIGHT / 2;
01162 int bottom_y = (flags & SEF_LAST_FIELD) ? halfway_y : y + SETTING_HEIGHT - 1;
01163 GfxDrawLine(x + offset, y, x + offset, bottom_y, colour);
01164
01165 GfxDrawLine(x + offset, halfway_y, x + level_width - offset, halfway_y, colour);
01166 x += level_width;
01167 }
01168
01169 switch (this->flags & SEF_KIND_MASK) {
01170 case SEF_SETTING_KIND:
01171 if (cur_row >= first_row) {
01172 DrawSetting(settings_ptr, this->d.entry.setting, rtl ? left : x, rtl ? x : right, y, this->flags & SEF_BUTTONS_MASK);
01173 }
01174 cur_row++;
01175 break;
01176 case SEF_SUBTREE_KIND:
01177 if (cur_row >= first_row) {
01178 DrawSprite((this->d.sub.folded ? SPR_CIRCLE_FOLDED : SPR_CIRCLE_UNFOLDED), PAL_NONE, rtl ? x - 8 : x, y + (SETTING_HEIGHT - 11) / 2);
01179 DrawString(rtl ? left : x + 12, rtl ? x - 12 : right, y, this->d.sub.title);
01180 }
01181 cur_row++;
01182 if (!this->d.sub.folded) {
01183 if (this->flags & SEF_LAST_FIELD) {
01184 assert(this->level < sizeof(parent_last));
01185 SetBit(parent_last, this->level);
01186 }
01187
01188 cur_row = this->d.sub.page->Draw(settings_ptr, left, right, base_y, first_row, max_row, cur_row, parent_last);
01189 }
01190 break;
01191 default: NOT_REACHED();
01192 }
01193 return cur_row;
01194 }
01195
01196 static const void *ResolveVariableAddress(const GameSettings *settings_ptr, const SettingDesc *sd)
01197 {
01198 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01199 if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01200 return GetVariableAddress(&Company::Get(_local_company)->settings, &sd->save);
01201 } else {
01202 return GetVariableAddress(&_settings_client.company, &sd->save);
01203 }
01204 } else {
01205 return GetVariableAddress(settings_ptr, &sd->save);
01206 }
01207 }
01208
01218 void SettingEntry::DrawSetting(GameSettings *settings_ptr, const SettingDesc *sd, int left, int right, int y, int state)
01219 {
01220 const SettingDescBase *sdb = &sd->desc;
01221 const void *var = ResolveVariableAddress(settings_ptr, sd);
01222 bool editable = true;
01223 bool disabled = false;
01224
01225 bool rtl = _current_text_dir == TD_RTL;
01226 uint buttons_left = rtl ? right - 19 : left;
01227 uint text_left = left + (rtl ? 0 : 25);
01228 uint text_right = right - (rtl ? 25 : 0);
01229 uint button_y = y + (SETTING_HEIGHT - 11) / 2;
01230
01231
01232 if (!(sd->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !_network_server && !(sdb->flags & SGF_PER_COMPANY)) editable = false;
01233 if ((sdb->flags & SGF_NETWORK_ONLY) && !_networking) editable = false;
01234 if ((sdb->flags & SGF_NO_NETWORK) && _networking) editable = false;
01235
01236 if (sdb->cmd == SDT_BOOLX) {
01237 static const Colours _bool_ctabs[2][2] = {{COLOUR_CREAM, COLOUR_RED}, {COLOUR_DARK_GREEN, COLOUR_GREEN}};
01238
01239 bool on = ReadValue(var, sd->save.conv) != 0;
01240
01241 DrawFrameRect(buttons_left, button_y, buttons_left + 19, button_y + 8, _bool_ctabs[!!on][!!editable], on ? FR_LOWERED : FR_NONE);
01242 SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
01243 } else {
01244 int32 value;
01245
01246 value = (int32)ReadValue(var, sd->save.conv);
01247
01248
01249 DrawArrowButtons(buttons_left, button_y, COLOUR_YELLOW, state, editable && value != (sdb->flags & SGF_0ISDISABLED ? 0 : sdb->min), editable && (uint32)value != sdb->max);
01250
01251 disabled = (value == 0) && (sdb->flags & SGF_0ISDISABLED);
01252 if (disabled) {
01253 SetDParam(0, STR_CONFIG_SETTING_DISABLED);
01254 } else {
01255 if (sdb->flags & SGF_CURRENCY) {
01256 SetDParam(0, STR_JUST_CURRENCY_LONG);
01257 } else if (sdb->flags & SGF_MULTISTRING) {
01258 SetDParam(0, sdb->val_str - sdb->min + value);
01259 } else {
01260 SetDParam(0, (sdb->flags & SGF_NOCOMMA) ? STR_JUST_INT : STR_JUST_COMMA);
01261 }
01262 SetDParam(1, value);
01263 }
01264 }
01265 DrawString(text_left, text_right, y, (sdb->str) + disabled);
01266 }
01267
01268
01269
01270
01275 void SettingsPage::Init(byte level)
01276 {
01277 for (uint field = 0; field < this->num; field++) {
01278 this->entries[field].Init(level, field + 1 == num);
01279 }
01280 }
01281
01283 void SettingsPage::FoldAll()
01284 {
01285 for (uint field = 0; field < this->num; field++) {
01286 this->entries[field].FoldAll();
01287 }
01288 }
01289
01291 uint SettingsPage::Length() const
01292 {
01293 uint length = 0;
01294 for (uint field = 0; field < this->num; field++) {
01295 length += this->entries[field].Length();
01296 }
01297 return length;
01298 }
01299
01306 SettingEntry *SettingsPage::FindEntry(uint row_num, uint *cur_row) const
01307 {
01308 SettingEntry *pe = NULL;
01309
01310 for (uint field = 0; field < this->num; field++) {
01311 pe = this->entries[field].FindEntry(row_num, cur_row);
01312 if (pe != NULL) {
01313 break;
01314 }
01315 }
01316 return pe;
01317 }
01318
01336 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
01337 {
01338 if (cur_row >= max_row) return cur_row;
01339
01340 for (uint i = 0; i < this->num; i++) {
01341 cur_row = this->entries[i].Draw(settings_ptr, left, right, base_y, first_row, max_row, cur_row, parent_last);
01342 if (cur_row >= max_row) {
01343 break;
01344 }
01345 }
01346 return cur_row;
01347 }
01348
01349
01350 static SettingEntry _settings_ui_display[] = {
01351 SettingEntry("gui.date_format_in_default_names"),
01352 SettingEntry("gui.population_in_label"),
01353 SettingEntry("gui.measure_tooltip"),
01354 SettingEntry("gui.loading_indicators"),
01355 SettingEntry("gui.liveries"),
01356 SettingEntry("gui.show_track_reservation"),
01357 SettingEntry("gui.expenses_layout"),
01358 SettingEntry("gui.smallmap_land_colour"),
01359 SettingEntry("gui.zoom_min"),
01360 SettingEntry("gui.zoom_max"),
01361 };
01363 static SettingsPage _settings_ui_display_page = {_settings_ui_display, lengthof(_settings_ui_display)};
01364
01365 static SettingEntry _settings_ui_interaction[] = {
01366 SettingEntry("gui.window_snap_radius"),
01367 SettingEntry("gui.window_soft_limit"),
01368 SettingEntry("gui.link_terraform_toolbar"),
01369 SettingEntry("gui.prefer_teamchat"),
01370 SettingEntry("gui.autoscroll"),
01371 SettingEntry("gui.reverse_scroll"),
01372 SettingEntry("gui.smooth_scroll"),
01373 SettingEntry("gui.left_mouse_btn_scrolling"),
01374
01375
01376
01377 SettingEntry("gui.scrollwheel_scrolling"),
01378 SettingEntry("gui.scrollwheel_multiplier"),
01379 #ifdef __APPLE__
01380
01381 SettingEntry("gui.right_mouse_btn_emulation"),
01382 #endif
01383 };
01385 static SettingsPage _settings_ui_interaction_page = {_settings_ui_interaction, lengthof(_settings_ui_interaction)};
01386
01387 static SettingEntry _settings_ui[] = {
01388 SettingEntry(&_settings_ui_display_page, STR_CONFIG_SETTING_DISPLAY_OPTIONS),
01389 SettingEntry(&_settings_ui_interaction_page, STR_CONFIG_SETTING_INTERACTION),
01390 SettingEntry("gui.show_finances"),
01391 SettingEntry("gui.errmsg_duration"),
01392 SettingEntry("gui.hover_delay"),
01393 SettingEntry("gui.toolbar_pos"),
01394 SettingEntry("gui.statusbar_pos"),
01395 SettingEntry("gui.newgrf_default_palette"),
01396 SettingEntry("gui.pause_on_newgame"),
01397 SettingEntry("gui.advanced_vehicle_list"),
01398 SettingEntry("gui.timetable_in_ticks"),
01399 SettingEntry("gui.timetable_arrival_departure"),
01400 SettingEntry("gui.quick_goto"),
01401 SettingEntry("gui.default_rail_type"),
01402 SettingEntry("gui.disable_unsuitable_building"),
01403 SettingEntry("gui.persistent_buildingtools"),
01404 SettingEntry("gui.coloured_news_year"),
01405 };
01407 static SettingsPage _settings_ui_page = {_settings_ui, lengthof(_settings_ui)};
01408
01409 static SettingEntry _settings_construction_signals[] = {
01410 SettingEntry("construction.signal_side"),
01411 SettingEntry("gui.enable_signal_gui"),
01412 SettingEntry("gui.drag_signals_density"),
01413 SettingEntry("gui.semaphore_build_before"),
01414 SettingEntry("gui.default_signal_type"),
01415 SettingEntry("gui.cycle_signal_types"),
01416 };
01418 static SettingsPage _settings_construction_signals_page = {_settings_construction_signals, lengthof(_settings_construction_signals)};
01419
01420 static SettingEntry _settings_construction[] = {
01421 SettingEntry(&_settings_construction_signals_page, STR_CONFIG_SETTING_CONSTRUCTION_SIGNALS),
01422 SettingEntry("construction.build_on_slopes"),
01423 SettingEntry("construction.autoslope"),
01424 SettingEntry("construction.extra_dynamite"),
01425 SettingEntry("construction.max_bridge_length"),
01426 SettingEntry("construction.max_tunnel_length"),
01427 SettingEntry("station.never_expire_airports"),
01428 SettingEntry("construction.freeform_edges"),
01429 SettingEntry("construction.extra_tree_placement"),
01430 SettingEntry("construction.command_pause_level"),
01431 };
01433 static SettingsPage _settings_construction_page = {_settings_construction, lengthof(_settings_construction)};
01434
01435 static SettingEntry _settings_stations_cargo[] = {
01436 SettingEntry("order.improved_load"),
01437 SettingEntry("order.gradual_loading"),
01438 SettingEntry("order.selectgoods"),
01439 };
01441 static SettingsPage _settings_stations_cargo_page = {_settings_stations_cargo, lengthof(_settings_stations_cargo)};
01442
01443 static SettingEntry _settings_stations[] = {
01444 SettingEntry(&_settings_stations_cargo_page, STR_CONFIG_SETTING_STATIONS_CARGOHANDLING),
01445 SettingEntry("station.adjacent_stations"),
01446 SettingEntry("station.distant_join_stations"),
01447 SettingEntry("station.station_spread"),
01448 SettingEntry("economy.station_noise_level"),
01449 SettingEntry("station.modified_catchment"),
01450 SettingEntry("construction.road_stop_on_town_road"),
01451 SettingEntry("construction.road_stop_on_competitor_road"),
01452 };
01454 static SettingsPage _settings_stations_page = {_settings_stations, lengthof(_settings_stations)};
01455
01456 static SettingEntry _settings_economy_towns[] = {
01457 SettingEntry("economy.bribe"),
01458 SettingEntry("economy.exclusive_rights"),
01459 SettingEntry("economy.fund_roads"),
01460 SettingEntry("economy.fund_buildings"),
01461 SettingEntry("economy.town_layout"),
01462 SettingEntry("economy.allow_town_roads"),
01463 SettingEntry("economy.allow_town_level_crossings"),
01464 SettingEntry("economy.found_town"),
01465 SettingEntry("economy.mod_road_rebuild"),
01466 SettingEntry("economy.town_growth_rate"),
01467 SettingEntry("economy.larger_towns"),
01468 SettingEntry("economy.initial_city_size"),
01469 };
01471 static SettingsPage _settings_economy_towns_page = {_settings_economy_towns, lengthof(_settings_economy_towns)};
01472
01473 static SettingEntry _settings_economy_industries[] = {
01474 SettingEntry("construction.raw_industry_construction"),
01475 SettingEntry("construction.industry_platform"),
01476 SettingEntry("economy.multiple_industry_per_town"),
01477 SettingEntry("game_creation.oil_refinery_limit"),
01478 };
01480 static SettingsPage _settings_economy_industries_page = {_settings_economy_industries, lengthof(_settings_economy_industries)};
01481
01482 static SettingEntry _settings_economy_scripts[] = {
01483 SettingEntry("script.script_max_opcode_till_suspend"),
01484 };
01486 static SettingsPage _settings_economy_scripts_page = {_settings_economy_scripts, lengthof(_settings_economy_scripts)};
01487
01488 static SettingEntry _settings_economy[] = {
01489 SettingEntry(&_settings_economy_towns_page, STR_CONFIG_SETTING_ECONOMY_TOWNS),
01490 SettingEntry(&_settings_economy_industries_page, STR_CONFIG_SETTING_ECONOMY_INDUSTRIES),
01491 SettingEntry(&_settings_economy_scripts_page, STR_CONFIG_SETTING_ECONOMY_SCRIPTS),
01492 SettingEntry("economy.inflation"),
01493 SettingEntry("economy.smooth_economy"),
01494 SettingEntry("economy.feeder_payment_share"),
01495 SettingEntry("economy.infrastructure_maintenance"),
01496 };
01498 static SettingsPage _settings_economy_page = {_settings_economy, lengthof(_settings_economy)};
01499
01500 static SettingEntry _settings_linkgraph[] = {
01501 SettingEntry("linkgraph.recalc_interval"),
01502 SettingEntry("linkgraph.distribution_pax"),
01503 SettingEntry("linkgraph.distribution_mail"),
01504 SettingEntry("linkgraph.distribution_armoured"),
01505 SettingEntry("linkgraph.distribution_default"),
01506 SettingEntry("linkgraph.accuracy"),
01507 SettingEntry("linkgraph.demand_distance"),
01508 SettingEntry("linkgraph.demand_size"),
01509 SettingEntry("linkgraph.short_path_saturation"),
01510 };
01512 static SettingsPage _settings_linkgraph_page = {_settings_linkgraph, lengthof(_settings_linkgraph)};
01513
01514 static SettingEntry _settings_ai_npc[] = {
01515 SettingEntry("ai.ai_in_multiplayer"),
01516 SettingEntry("ai.ai_disable_veh_train"),
01517 SettingEntry("ai.ai_disable_veh_roadveh"),
01518 SettingEntry("ai.ai_disable_veh_aircraft"),
01519 SettingEntry("ai.ai_disable_veh_ship"),
01520 };
01522 static SettingsPage _settings_ai_npc_page = {_settings_ai_npc, lengthof(_settings_ai_npc)};
01523
01524 static SettingEntry _settings_ai[] = {
01525 SettingEntry(&_settings_ai_npc_page, STR_CONFIG_SETTING_AI_NPC),
01526 SettingEntry("economy.give_money"),
01527 SettingEntry("economy.allow_shares"),
01528 };
01530 static SettingsPage _settings_ai_page = {_settings_ai, lengthof(_settings_ai)};
01531
01532 static SettingEntry _settings_vehicles_routing[] = {
01533 SettingEntry("pf.pathfinder_for_trains"),
01534 SettingEntry("pf.forbid_90_deg"),
01535 SettingEntry("pf.pathfinder_for_roadvehs"),
01536 SettingEntry("pf.roadveh_queue"),
01537 SettingEntry("pf.pathfinder_for_ships"),
01538 };
01540 static SettingsPage _settings_vehicles_routing_page = {_settings_vehicles_routing, lengthof(_settings_vehicles_routing)};
01541
01542 static SettingEntry _settings_vehicles_autorenew[] = {
01543 SettingEntry("company.engine_renew"),
01544 SettingEntry("company.engine_renew_months"),
01545 SettingEntry("company.engine_renew_money"),
01546 };
01548 static SettingsPage _settings_vehicles_autorenew_page = {_settings_vehicles_autorenew, lengthof(_settings_vehicles_autorenew)};
01549
01550 static SettingEntry _settings_vehicles_servicing[] = {
01551 SettingEntry("vehicle.servint_ispercent"),
01552 SettingEntry("vehicle.servint_trains"),
01553 SettingEntry("vehicle.servint_roadveh"),
01554 SettingEntry("vehicle.servint_ships"),
01555 SettingEntry("vehicle.servint_aircraft"),
01556 SettingEntry("order.no_servicing_if_no_breakdowns"),
01557 SettingEntry("order.serviceathelipad"),
01558 };
01560 static SettingsPage _settings_vehicles_servicing_page = {_settings_vehicles_servicing, lengthof(_settings_vehicles_servicing)};
01561
01562 static SettingEntry _settings_vehicles_trains[] = {
01563 SettingEntry("pf.reverse_at_signals"),
01564 SettingEntry("vehicle.train_acceleration_model"),
01565 SettingEntry("vehicle.train_slope_steepness"),
01566 SettingEntry("vehicle.max_train_length"),
01567 SettingEntry("vehicle.wagon_speed_limits"),
01568 SettingEntry("vehicle.disable_elrails"),
01569 SettingEntry("vehicle.freight_trains"),
01570 SettingEntry("gui.stop_location"),
01571 };
01573 static SettingsPage _settings_vehicles_trains_page = {_settings_vehicles_trains, lengthof(_settings_vehicles_trains)};
01574
01575 static SettingEntry _settings_vehicles[] = {
01576 SettingEntry(&_settings_vehicles_routing_page, STR_CONFIG_SETTING_VEHICLES_ROUTING),
01577 SettingEntry(&_settings_vehicles_autorenew_page, STR_CONFIG_SETTING_VEHICLES_AUTORENEW),
01578 SettingEntry(&_settings_vehicles_servicing_page, STR_CONFIG_SETTING_VEHICLES_SERVICING),
01579 SettingEntry(&_settings_vehicles_trains_page, STR_CONFIG_SETTING_VEHICLES_TRAINS),
01580 SettingEntry("gui.new_nonstop"),
01581 SettingEntry("gui.order_review_system"),
01582 SettingEntry("gui.vehicle_income_warn"),
01583 SettingEntry("gui.lost_vehicle_warn"),
01584 SettingEntry("vehicle.never_expire_vehicles"),
01585 SettingEntry("vehicle.max_trains"),
01586 SettingEntry("vehicle.max_roadveh"),
01587 SettingEntry("vehicle.max_aircraft"),
01588 SettingEntry("vehicle.max_ships"),
01589 SettingEntry("vehicle.plane_speed"),
01590 SettingEntry("vehicle.plane_crashes"),
01591 SettingEntry("vehicle.dynamic_engines"),
01592 SettingEntry("vehicle.roadveh_acceleration_model"),
01593 SettingEntry("vehicle.roadveh_slope_steepness"),
01594 SettingEntry("vehicle.smoke_amount"),
01595 };
01597 static SettingsPage _settings_vehicles_page = {_settings_vehicles, lengthof(_settings_vehicles)};
01598
01599 static SettingEntry _settings_main[] = {
01600 SettingEntry(&_settings_ui_page, STR_CONFIG_SETTING_GUI),
01601 SettingEntry(&_settings_construction_page, STR_CONFIG_SETTING_CONSTRUCTION),
01602 SettingEntry(&_settings_vehicles_page, STR_CONFIG_SETTING_VEHICLES),
01603 SettingEntry(&_settings_stations_page, STR_CONFIG_SETTING_STATIONS),
01604 SettingEntry(&_settings_economy_page, STR_CONFIG_SETTING_ECONOMY),
01605 SettingEntry(&_settings_linkgraph_page, STR_CONFIG_SETTING_LINKGRAPH),
01606 SettingEntry(&_settings_ai_page, STR_CONFIG_SETTING_AI),
01607 };
01608
01610 static SettingsPage _settings_main_page = {_settings_main, lengthof(_settings_main)};
01611
01613 enum GameSettingsWidgets {
01614 SETTINGSEL_OPTIONSPANEL,
01615 SETTINGSEL_SCROLLBAR,
01616 };
01617
01618 struct GameSettingsWindow : Window {
01619 static const int SETTINGTREE_LEFT_OFFSET = 5;
01620 static const int SETTINGTREE_RIGHT_OFFSET = 5;
01621 static const int SETTINGTREE_TOP_OFFSET = 5;
01622 static const int SETTINGTREE_BOTTOM_OFFSET = 5;
01623
01624 static GameSettings *settings_ptr;
01625
01626 SettingEntry *valuewindow_entry;
01627 SettingEntry *clicked_entry;
01628
01629 Scrollbar *vscroll;
01630
01631 GameSettingsWindow(const WindowDesc *desc) : Window()
01632 {
01633 static bool first_time = true;
01634
01635 settings_ptr = &GetGameSettings();
01636
01637
01638 if (first_time) {
01639 _settings_main_page.Init();
01640 first_time = false;
01641 } else {
01642 _settings_main_page.FoldAll();
01643 }
01644
01645 this->valuewindow_entry = NULL;
01646 this->clicked_entry = NULL;
01647
01648 this->CreateNestedTree(desc);
01649 this->vscroll = this->GetScrollbar(SETTINGSEL_SCROLLBAR);
01650 this->FinishInitNested(desc, 0);
01651
01652 this->vscroll->SetCount(_settings_main_page.Length());
01653 }
01654
01655 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01656 {
01657 if (widget != SETTINGSEL_OPTIONSPANEL) return;
01658
01659 resize->height = SETTING_HEIGHT = max(11, FONT_HEIGHT_NORMAL + 1);
01660 resize->width = 1;
01661
01662 size->height = 5 * resize->height + SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET;
01663 }
01664
01665 virtual void DrawWidget(const Rect &r, int widget) const
01666 {
01667 if (widget != SETTINGSEL_OPTIONSPANEL) return;
01668
01669 _settings_main_page.Draw(settings_ptr, r.left + SETTINGTREE_LEFT_OFFSET, r.right - SETTINGTREE_RIGHT_OFFSET, r.top + SETTINGTREE_TOP_OFFSET,
01670 this->vscroll->GetPosition(), this->vscroll->GetPosition() + this->vscroll->GetCapacity());
01671 }
01672
01673 virtual void OnClick(Point pt, int widget, int click_count)
01674 {
01675 if (widget != SETTINGSEL_OPTIONSPANEL) return;
01676
01677 uint btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, SETTINGSEL_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET - 1);
01678 if (btn == INT_MAX) return;
01679
01680 uint cur_row = 0;
01681 SettingEntry *pe = _settings_main_page.FindEntry(btn, &cur_row);
01682
01683 if (pe == NULL) return;
01684
01685 int x = (_current_text_dir == TD_RTL ? this->width - pt.x : pt.x) - SETTINGTREE_LEFT_OFFSET - (pe->level + 1) * LEVEL_WIDTH;
01686 if (x < 0) return;
01687
01688 if ((pe->flags & SEF_KIND_MASK) == SEF_SUBTREE_KIND) {
01689 pe->d.sub.folded = !pe->d.sub.folded;
01690
01691 this->vscroll->SetCount(_settings_main_page.Length());
01692 this->SetDirty();
01693 return;
01694 }
01695
01696 assert((pe->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
01697 const SettingDesc *sd = pe->d.entry.setting;
01698
01699
01700 if (!(sd->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !_network_server && !(sd->desc.flags & SGF_PER_COMPANY)) return;
01701 if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking) return;
01702 if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return;
01703
01704 const void *var = ResolveVariableAddress(settings_ptr, sd);
01705 int32 value = (int32)ReadValue(var, sd->save.conv);
01706
01707
01708 if (x < 21) {
01709 const SettingDescBase *sdb = &sd->desc;
01710 int32 oldvalue = value;
01711
01712 switch (sdb->cmd) {
01713 case SDT_BOOLX: value ^= 1; break;
01714 case SDT_ONEOFMANY:
01715 case SDT_NUMX: {
01716
01717
01718
01719
01720 uint32 step = (sdb->interval == 0) ? ((sdb->max - sdb->min) / 50) : sdb->interval;
01721 if (step == 0) step = 1;
01722
01723
01724 if ((this->flags4 & WF_TIMEOUT_MASK) > WF_TIMEOUT_TRIGGER) {
01725 _left_button_clicked = false;
01726 return;
01727 }
01728
01729
01730 if (x >= 10) {
01731 value += step;
01732 if (sdb->min < 0) {
01733 assert((int32)sdb->max >= 0);
01734 if (value > (int32)sdb->max) value = (int32)sdb->max;
01735 } else {
01736 if ((uint32)value > sdb->max) value = (int32)sdb->max;
01737 }
01738 if (value < sdb->min) value = sdb->min;
01739 } else {
01740 value -= step;
01741 if (value < sdb->min) value = (sdb->flags & SGF_0ISDISABLED) ? 0 : sdb->min;
01742 }
01743
01744
01745 if (value != oldvalue && !(sd->desc.flags & SGF_MULTISTRING)) {
01746 if (this->clicked_entry != NULL) {
01747 this->clicked_entry->SetButtons(0);
01748 }
01749 this->clicked_entry = pe;
01750 this->clicked_entry->SetButtons((x >= 10) != (_current_text_dir == TD_RTL) ? SEF_RIGHT_DEPRESSED : SEF_LEFT_DEPRESSED);
01751 this->flags4 |= WF_TIMEOUT_BEGIN;
01752 _left_button_clicked = false;
01753 }
01754 break;
01755 }
01756
01757 default: NOT_REACHED();
01758 }
01759
01760 if (value != oldvalue) {
01761 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01762 SetCompanySetting(pe->d.entry.index, value);
01763 } else {
01764 SetSettingValue(pe->d.entry.index, value);
01765 }
01766 this->SetDirty();
01767 }
01768 } else {
01769
01770 if (sd->desc.cmd != SDT_BOOLX && !(sd->desc.flags & SGF_MULTISTRING)) {
01771
01772 if (sd->desc.flags & SGF_CURRENCY) value *= _currency->rate;
01773
01774 this->valuewindow_entry = pe;
01775 SetDParam(0, value);
01776 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
01777 }
01778 }
01779 }
01780
01781 virtual void OnTimeout()
01782 {
01783 if (this->clicked_entry != NULL) {
01784 this->clicked_entry->SetButtons(0);
01785 this->clicked_entry = NULL;
01786 this->SetDirty();
01787 }
01788 }
01789
01790 virtual void OnQueryTextFinished(char *str)
01791 {
01792
01793 if (str == NULL) return;
01794
01795 assert(this->valuewindow_entry != NULL);
01796 assert((this->valuewindow_entry->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
01797 const SettingDesc *sd = this->valuewindow_entry->d.entry.setting;
01798
01799 int32 value;
01800 if (!StrEmpty(str)) {
01801 value = atoi(str);
01802
01803
01804 if (sd->desc.flags & SGF_CURRENCY) value /= _currency->rate;
01805 } else {
01806 value = (int32)(size_t)sd->desc.def;
01807 }
01808
01809 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01810 SetCompanySetting(this->valuewindow_entry->d.entry.index, value);
01811 } else {
01812 SetSettingValue(this->valuewindow_entry->d.entry.index, value);
01813 }
01814 this->SetDirty();
01815 }
01816
01817 virtual void OnResize()
01818 {
01819 this->vscroll->SetCapacityFromWidget(this, SETTINGSEL_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET);
01820 }
01821 };
01822
01823 GameSettings *GameSettingsWindow::settings_ptr = NULL;
01824
01825 static const NWidgetPart _nested_settings_selection_widgets[] = {
01826 NWidget(NWID_HORIZONTAL),
01827 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
01828 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_CONFIG_SETTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01829 EndContainer(),
01830 NWidget(NWID_HORIZONTAL),
01831 NWidget(WWT_PANEL, COLOUR_MAUVE, SETTINGSEL_OPTIONSPANEL), SetMinimalSize(400, 174), SetScrollbar(SETTINGSEL_SCROLLBAR), EndContainer(),
01832 NWidget(NWID_VERTICAL),
01833 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, SETTINGSEL_SCROLLBAR),
01834 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
01835 EndContainer(),
01836 EndContainer(),
01837 };
01838
01839 static const WindowDesc _settings_selection_desc(
01840 WDP_CENTER, 450, 397,
01841 WC_GAME_OPTIONS, WC_NONE,
01842 0,
01843 _nested_settings_selection_widgets, lengthof(_nested_settings_selection_widgets)
01844 );
01845
01847 void ShowGameSettings()
01848 {
01849 DeleteWindowById(WC_GAME_OPTIONS, 0);
01850 new GameSettingsWindow(&_settings_selection_desc);
01851 }
01852
01853
01863 void DrawArrowButtons(int x, int y, Colours button_colour, byte state, bool clickable_left, bool clickable_right)
01864 {
01865 int colour = _colour_gradient[button_colour][2];
01866
01867 DrawFrameRect(x, y + 1, x + 9, y + 9, button_colour, (state == 1) ? FR_LOWERED : FR_NONE);
01868 DrawFrameRect(x + 10, y + 1, x + 19, y + 9, button_colour, (state == 2) ? FR_LOWERED : FR_NONE);
01869 DrawSprite(SPR_ARROW_LEFT, PAL_NONE, x + WD_IMGBTN_LEFT, y + WD_IMGBTN_TOP);
01870 DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, x + WD_IMGBTN_LEFT + 10, y + WD_IMGBTN_TOP);
01871
01872
01873 bool rtl = _current_text_dir == TD_RTL;
01874 if (rtl ? !clickable_right : !clickable_left) {
01875 GfxFillRect(x + 1, y + 1, x + 1 + 8, y + 8, colour, FILLRECT_CHECKER);
01876 }
01877 if (rtl ? !clickable_left : !clickable_right) {
01878 GfxFillRect(x + 11, y + 1, x + 11 + 8, y + 8, colour, FILLRECT_CHECKER);
01879 }
01880 }
01881
01883 enum CustomCurrencyWidgets {
01884 CUSTCURR_RATE_DOWN,
01885 CUSTCURR_RATE_UP,
01886 CUSTCURR_RATE,
01887 CUSTCURR_SEPARATOR_EDIT,
01888 CUSTCURR_SEPARATOR,
01889 CUSTCURR_PREFIX_EDIT,
01890 CUSTCURR_PREFIX,
01891 CUSTCURR_SUFFIX_EDIT,
01892 CUSTCURR_SUFFIX,
01893 CUSTCURR_YEAR_DOWN,
01894 CUSTCURR_YEAR_UP,
01895 CUSTCURR_YEAR,
01896 CUSTCURR_PREVIEW,
01897 };
01898
01899 struct CustomCurrencyWindow : Window {
01900 int query_widget;
01901
01902 CustomCurrencyWindow(const WindowDesc *desc) : Window()
01903 {
01904 this->InitNested(desc);
01905
01906 SetButtonState();
01907 }
01908
01909 void SetButtonState()
01910 {
01911 this->SetWidgetDisabledState(CUSTCURR_RATE_DOWN, _custom_currency.rate == 1);
01912 this->SetWidgetDisabledState(CUSTCURR_RATE_UP, _custom_currency.rate == UINT16_MAX);
01913 this->SetWidgetDisabledState(CUSTCURR_YEAR_DOWN, _custom_currency.to_euro == CF_NOEURO);
01914 this->SetWidgetDisabledState(CUSTCURR_YEAR_UP, _custom_currency.to_euro == MAX_YEAR);
01915 }
01916
01917 virtual void SetStringParameters(int widget) const
01918 {
01919 switch (widget) {
01920 case CUSTCURR_RATE: SetDParam(0, 1); SetDParam(1, 1); break;
01921 case CUSTCURR_SEPARATOR: SetDParamStr(0, _custom_currency.separator); break;
01922 case CUSTCURR_PREFIX: SetDParamStr(0, _custom_currency.prefix); break;
01923 case CUSTCURR_SUFFIX: SetDParamStr(0, _custom_currency.suffix); break;
01924 case CUSTCURR_YEAR:
01925 SetDParam(0, (_custom_currency.to_euro != CF_NOEURO) ? STR_CURRENCY_SWITCH_TO_EURO : STR_CURRENCY_SWITCH_TO_EURO_NEVER);
01926 SetDParam(1, _custom_currency.to_euro);
01927 break;
01928
01929 case CUSTCURR_PREVIEW:
01930 SetDParam(0, 10000);
01931 break;
01932 }
01933 }
01934
01935 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01936 {
01937 switch (widget) {
01938
01939 case CUSTCURR_SEPARATOR_EDIT:
01940 case CUSTCURR_PREFIX_EDIT:
01941 case CUSTCURR_SUFFIX_EDIT:
01942 size->width = this->GetWidget<NWidgetBase>(CUSTCURR_RATE_DOWN)->smallest_x + this->GetWidget<NWidgetBase>(CUSTCURR_RATE_UP)->smallest_x;
01943 break;
01944
01945
01946 case CUSTCURR_RATE:
01947 SetDParam(0, 1);
01948 SetDParam(1, INT32_MAX);
01949 *size = GetStringBoundingBox(STR_CURRENCY_EXCHANGE_RATE);
01950 break;
01951 }
01952 }
01953
01954 virtual void OnClick(Point pt, int widget, int click_count)
01955 {
01956 int line = 0;
01957 int len = 0;
01958 StringID str = 0;
01959 CharSetFilter afilter = CS_ALPHANUMERAL;
01960
01961 switch (widget) {
01962 case CUSTCURR_RATE_DOWN:
01963 if (_custom_currency.rate > 1) _custom_currency.rate--;
01964 if (_custom_currency.rate == 1) this->DisableWidget(CUSTCURR_RATE_DOWN);
01965 this->EnableWidget(CUSTCURR_RATE_UP);
01966 break;
01967
01968 case CUSTCURR_RATE_UP:
01969 if (_custom_currency.rate < UINT16_MAX) _custom_currency.rate++;
01970 if (_custom_currency.rate == UINT16_MAX) this->DisableWidget(CUSTCURR_RATE_UP);
01971 this->EnableWidget(CUSTCURR_RATE_DOWN);
01972 break;
01973
01974 case CUSTCURR_RATE:
01975 SetDParam(0, _custom_currency.rate);
01976 str = STR_JUST_INT;
01977 len = 5;
01978 line = CUSTCURR_RATE;
01979 afilter = CS_NUMERAL;
01980 break;
01981
01982 case CUSTCURR_SEPARATOR_EDIT:
01983 case CUSTCURR_SEPARATOR:
01984 SetDParamStr(0, _custom_currency.separator);
01985 str = STR_JUST_RAW_STRING;
01986 len = 1;
01987 line = CUSTCURR_SEPARATOR;
01988 break;
01989
01990 case CUSTCURR_PREFIX_EDIT:
01991 case CUSTCURR_PREFIX:
01992 SetDParamStr(0, _custom_currency.prefix);
01993 str = STR_JUST_RAW_STRING;
01994 len = 12;
01995 line = CUSTCURR_PREFIX;
01996 break;
01997
01998 case CUSTCURR_SUFFIX_EDIT:
01999 case CUSTCURR_SUFFIX:
02000 SetDParamStr(0, _custom_currency.suffix);
02001 str = STR_JUST_RAW_STRING;
02002 len = 12;
02003 line = CUSTCURR_SUFFIX;
02004 break;
02005
02006 case CUSTCURR_YEAR_DOWN:
02007 _custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ? CF_NOEURO : _custom_currency.to_euro - 1;
02008 if (_custom_currency.to_euro == CF_NOEURO) this->DisableWidget(CUSTCURR_YEAR_DOWN);
02009 this->EnableWidget(CUSTCURR_YEAR_UP);
02010 break;
02011
02012 case CUSTCURR_YEAR_UP:
02013 _custom_currency.to_euro = Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
02014 if (_custom_currency.to_euro == MAX_YEAR) this->DisableWidget(CUSTCURR_YEAR_UP);
02015 this->EnableWidget(CUSTCURR_YEAR_DOWN);
02016 break;
02017
02018 case CUSTCURR_YEAR:
02019 SetDParam(0, _custom_currency.to_euro);
02020 str = STR_JUST_INT;
02021 len = 7;
02022 line = CUSTCURR_YEAR;
02023 afilter = CS_NUMERAL;
02024 break;
02025 }
02026
02027 if (len != 0) {
02028 this->query_widget = line;
02029 ShowQueryString(str, STR_CURRENCY_CHANGE_PARAMETER, len + 1, this, afilter, QSF_NONE);
02030 }
02031
02032 this->flags4 |= WF_TIMEOUT_BEGIN;
02033 this->SetDirty();
02034 }
02035
02036 virtual void OnQueryTextFinished(char *str)
02037 {
02038 if (str == NULL) return;
02039
02040 switch (this->query_widget) {
02041 case CUSTCURR_RATE:
02042 _custom_currency.rate = Clamp(atoi(str), 1, UINT16_MAX);
02043 break;
02044
02045 case CUSTCURR_SEPARATOR:
02046 strecpy(_custom_currency.separator, str, lastof(_custom_currency.separator));
02047 break;
02048
02049 case CUSTCURR_PREFIX:
02050 strecpy(_custom_currency.prefix, str, lastof(_custom_currency.prefix));
02051 break;
02052
02053 case CUSTCURR_SUFFIX:
02054 strecpy(_custom_currency.suffix, str, lastof(_custom_currency.suffix));
02055 break;
02056
02057 case CUSTCURR_YEAR: {
02058 int val = atoi(str);
02059
02060 _custom_currency.to_euro = (val < 2000 ? CF_NOEURO : min(val, MAX_YEAR));
02061 break;
02062 }
02063 }
02064 MarkWholeScreenDirty();
02065 SetButtonState();
02066 }
02067
02068 virtual void OnTimeout()
02069 {
02070 this->SetDirty();
02071 }
02072 };
02073
02074 static const NWidgetPart _nested_cust_currency_widgets[] = {
02075 NWidget(NWID_HORIZONTAL),
02076 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
02077 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CURRENCY_WINDOW, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
02078 EndContainer(),
02079 NWidget(WWT_PANEL, COLOUR_GREY),
02080 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(7, 3, 0),
02081 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02082 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_RATE_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_EXCHANGE_RATE_TOOLTIP),
02083 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_RATE_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_EXCHANGE_RATE_TOOLTIP),
02084 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02085 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_RATE), SetDataTip(STR_CURRENCY_EXCHANGE_RATE, STR_CURRENCY_SET_EXCHANGE_RATE_TOOLTIP), SetFill(1, 0),
02086 EndContainer(),
02087 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02088 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, CUSTCURR_SEPARATOR_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(0, 1),
02089 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02090 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_SEPARATOR), SetDataTip(STR_CURRENCY_SEPARATOR, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(1, 0),
02091 EndContainer(),
02092 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02093 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, CUSTCURR_PREFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(0, 1),
02094 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02095 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_PREFIX), SetDataTip(STR_CURRENCY_PREFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(1, 0),
02096 EndContainer(),
02097 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02098 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, CUSTCURR_SUFFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(0, 1),
02099 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02100 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_SUFFIX), SetDataTip(STR_CURRENCY_SUFFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(1, 0),
02101 EndContainer(),
02102 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02103 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_YEAR_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
02104 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_YEAR_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
02105 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02106 NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_YEAR), SetDataTip(STR_JUST_STRING, STR_CURRENCY_SET_CUSTOM_CURRENCY_TO_EURO_TOOLTIP), SetFill(1, 0),
02107 EndContainer(),
02108 EndContainer(),
02109 NWidget(WWT_LABEL, COLOUR_BLUE, CUSTCURR_PREVIEW),
02110 SetDataTip(STR_CURRENCY_PREVIEW, STR_CURRENCY_CUSTOM_CURRENCY_PREVIEW_TOOLTIP), SetPadding(15, 1, 18, 2),
02111 EndContainer(),
02112 };
02113
02114 static const WindowDesc _cust_currency_desc(
02115 WDP_CENTER, 0, 0,
02116 WC_CUSTOM_CURRENCY, WC_NONE,
02117 WDF_UNCLICK_BUTTONS,
02118 _nested_cust_currency_widgets, lengthof(_nested_cust_currency_widgets)
02119 );
02120
02122 static void ShowCustCurrency()
02123 {
02124 DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
02125 new CustomCurrencyWindow(&_cust_currency_desc);
02126 }