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