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