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