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