Remove ZeroedMemoryAllocator as a base class of Window
[openttd/fttd.git] / src / cheat_gui.cpp
blob021af2d3c6e6826526726411e297e3798345f3d1
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file cheat_gui.cpp GUI related to cheating. */
12 #include "stdafx.h"
13 #include "command_func.h"
14 #include "cheat_type.h"
15 #include "company_base.h"
16 #include "company_func.h"
17 #include "date_func.h"
18 #include "saveload/saveload.h"
19 #include "textbuf_gui.h"
20 #include "window_gui.h"
21 #include "string.h"
22 #include "strings_func.h"
23 #include "window_func.h"
24 #include "rail_gui.h"
25 #include "settings_gui.h"
26 #include "company_gui.h"
27 #include "linkgraph/linkgraphschedule.h"
28 #include "map/zoneheight.h"
29 #include "newgrf.h"
30 #include "error.h"
32 #include "widgets/cheat_widget.h"
34 #include "table/sprites.h"
37 /**
38 * The 'amount' to cheat with.
39 * This variable is semantically a constant value, but because the cheat
40 * code requires to be able to write to the variable it is not constified.
42 static int32 _money_cheat_amount = 10000000;
44 /**
45 * Handle cheating of money.
46 * Note that the amount of money of a company must be changed through a command
47 * rather than by setting a variable. Since the cheat data structure expects a
48 * variable, the amount of given/taken money is used for this purpose.
49 * @param p1 not used.
50 * @param p2 is -1 or +1 (down/up)
51 * @return Amount of money cheat.
53 static int32 ClickMoneyCheat(int32 p1, int32 p2)
55 DoCommandP(0, (uint32)(p2 * _money_cheat_amount), 0, CMD_MONEY_CHEAT);
56 return _money_cheat_amount;
59 /**
60 * Handle changing of company.
61 * @param p1 company to set to
62 * @param p2 is -1 or +1 (down/up)
63 * @return The new company.
65 static int32 ClickChangeCompanyCheat(int32 p1, int32 p2)
67 while ((uint)p1 < Company::GetPoolSize()) {
68 if (Company::IsValidID((CompanyID)p1)) {
69 SetLocalCompany((CompanyID)p1);
70 return _local_company;
72 p1 += p2;
75 return _local_company;
78 /**
79 * Allow (or disallow) changing production of all industries.
80 * @param p1 new value
81 * @param p2 unused
82 * @return New value allowing change of industry production.
84 static int32 ClickSetProdCheat(int32 p1, int32 p2)
86 _cheats.setup_prod.value = (p1 != 0);
87 InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
88 return _cheats.setup_prod.value;
91 extern void EnginesMonthlyLoop();
93 /**
94 * Handle changing of the current year.
95 * @param p1 Unused.
96 * @param p2 +1 (increase) or -1 (decrease).
97 * @return New year.
99 static int32 ClickChangeDateCheat(int32 p1, int32 p2)
101 YearMonthDay ymd;
102 ConvertDateToYMD(_date, &ymd);
104 p1 = Clamp(p1, MIN_YEAR, MAX_YEAR);
105 if (p1 == _cur_year) return _cur_year;
107 Date new_date = ConvertYMDToDate(p1, ymd.month, ymd.day);
108 LinkGraphSchedule::instance.ShiftDates(new_date - _date);
109 SetDate(new_date, _date_fract);
110 EnginesMonthlyLoop();
111 SetWindowDirty(WC_STATUS_BAR, 0);
112 InvalidateWindowClassesData(WC_BUILD_STATION, 0);
113 InvalidateWindowClassesData(WC_BUILD_OBJECT, 0);
114 ResetSignalVariant();
115 return _cur_year;
119 * Allow (or disallow) a change of the maximum allowed heightlevel.
120 * @param p1 new value
121 * @param p2 unused
122 * @return New value (or unchanged old value) of the maximum
123 * allowed heightlevel value.
125 static int32 ClickChangeMaxHlCheat(int32 p1, int32 p2)
127 p1 = Clamp(p1, MIN_MAX_HEIGHTLEVEL, MAX_MAX_HEIGHTLEVEL);
129 /* Check if at least one mountain on the map is higher than the new value.
130 * If yes, disallow the change. */
131 for (TileIndex t = 0; t < MapSize(); t++) {
132 if ((int32)TileHeight(t) > p1) {
133 ShowErrorMessage(STR_CONFIG_SETTING_TOO_HIGH_MOUNTAIN, INVALID_STRING_ID, WL_ERROR);
134 /* Return old, unchanged value */
135 return _settings_game.construction.max_heightlevel;
139 /* Execute the change and reload GRF Data */
140 _settings_game.construction.max_heightlevel = p1;
141 ReloadNewGRFData();
143 /* The smallmap uses an index from heightlevels to colours. Trigger rebuilding it. */
144 InvalidateWindowClassesData(WC_SMALLMAP, 2);
146 return _settings_game.construction.max_heightlevel;
149 /** Available cheats. */
150 enum CheatNumbers {
151 CHT_MONEY, ///< Change amount of money.
152 CHT_CHANGE_COMPANY, ///< Switch company.
153 CHT_EXTRA_DYNAMITE, ///< Dynamite anything.
154 CHT_CROSSINGTUNNELS, ///< Allow tunnels to cross each other.
155 CHT_NO_JETCRASH, ///< Disable jet-airplane crashes.
156 CHT_SETUP_PROD, ///< Allow manually editing of industry production.
157 CHT_EDIT_MAX_HL, ///< Edit maximum allowed heightlevel
158 CHT_CHANGE_DATE, ///< Do time traveling.
160 CHT_NUM_CHEATS, ///< Number of cheats.
164 * Signature of handler function when user clicks at a cheat.
165 * @param p1 The new value.
166 * @param p2 Change direction (+1, +1), \c 0 for boolean settings.
168 typedef int32 CheckButtonClick(int32 p1, int32 p2);
170 /** Information of a cheat. */
171 struct CheatEntry {
172 VarType type; ///< type of selector
173 StringID str; ///< string with descriptive text
174 void *variable; ///< pointer to the variable
175 bool *been_used; ///< has this cheat been used before?
176 CheckButtonClick *proc;///< procedure
180 * The available cheats.
181 * Order matches with the values of #CheatNumbers
183 static const CheatEntry _cheats_ui[] = {
184 {SLE_INT32, STR_CHEAT_MONEY, &_money_cheat_amount, &_cheats.money.been_used, &ClickMoneyCheat },
185 {SLE_UINT8, STR_CHEAT_CHANGE_COMPANY, &_local_company, &_cheats.switch_company.been_used, &ClickChangeCompanyCheat },
186 {SLE_BOOL, STR_CHEAT_EXTRA_DYNAMITE, &_cheats.magic_bulldozer.value, &_cheats.magic_bulldozer.been_used, NULL },
187 {SLE_BOOL, STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value, &_cheats.crossing_tunnels.been_used, NULL },
188 {SLE_BOOL, STR_CHEAT_NO_JETCRASH, &_cheats.no_jetcrash.value, &_cheats.no_jetcrash.been_used, NULL },
189 {SLE_BOOL, STR_CHEAT_SETUP_PROD, &_cheats.setup_prod.value, &_cheats.setup_prod.been_used, &ClickSetProdCheat },
190 {SLE_UINT8, STR_CHEAT_EDIT_MAX_HL, &_settings_game.construction.max_heightlevel, &_cheats.edit_max_hl.been_used, &ClickChangeMaxHlCheat },
191 {SLE_INT32, STR_CHEAT_CHANGE_DATE, &_cur_year, &_cheats.change_date.been_used, &ClickChangeDateCheat },
194 assert_compile(CHT_NUM_CHEATS == lengthof(_cheats_ui));
196 /** Widget definitions of the cheat GUI. */
197 static const NWidgetPart _nested_cheat_widgets[] = {
198 NWidget(NWID_HORIZONTAL),
199 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
200 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CHEATS, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
201 NWidget(WWT_SHADEBOX, COLOUR_GREY),
202 NWidget(WWT_STICKYBOX, COLOUR_GREY),
203 EndContainer(),
204 NWidget(WWT_PANEL, COLOUR_GREY, WID_C_PANEL), SetDataTip(0x0, STR_CHEATS_TOOLTIP), EndContainer(),
207 /** GUI for the cheats. */
208 struct CheatWindow : Window {
209 int clicked;
210 int header_height;
211 int clicked_widget;
212 uint line_height;
213 int box_width;
215 CheatWindow (const WindowDesc *desc)
216 : Window (desc), clicked (0), header_height (0),
217 clicked_widget (0), line_height (0),
218 box_width (GetSpriteSize(SPR_BOX_EMPTY).width)
220 this->InitNested();
223 virtual void DrawWidget(const Rect &r, int widget) const
225 if (widget != WID_C_PANEL) return;
227 int y = r.top + WD_FRAMERECT_TOP + this->header_height;
228 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, y, STR_CHEATS_WARNING, TC_FROMSTRING, SA_CENTER);
230 bool rtl = _current_text_dir == TD_RTL;
231 uint box_left = rtl ? r.right - this->box_width - 5 : r.left + 5;
232 uint button_left = rtl ? r.right - this->box_width - 10 - SETTING_BUTTON_WIDTH : r.left + this->box_width + 10;
233 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : 20 + this->box_width + SETTING_BUTTON_WIDTH);
234 uint text_right = r.right - (rtl ? 20 + this->box_width + SETTING_BUTTON_WIDTH : WD_FRAMERECT_RIGHT);
236 int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2;
237 int icon_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
239 for (int i = 0; i != lengthof(_cheats_ui); i++) {
240 const CheatEntry *ce = &_cheats_ui[i];
242 DrawSprite((*ce->been_used) ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, box_left, y + icon_y_offset + 2);
244 switch (ce->type) {
245 case SLE_BOOL: {
246 bool on = (*(bool*)ce->variable);
248 DrawBoolButton(button_left, y + icon_y_offset, on, true);
249 SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
250 break;
253 default: {
254 int32 val = (int32)ReadValue(ce->variable, ce->type);
256 /* Draw [<][>] boxes for settings of an integer-type */
257 DrawArrowButtons(button_left, y + icon_y_offset, COLOUR_YELLOW, clicked - (i * 2), true, true);
259 switch (ce->str) {
260 /* Display date for change date cheat */
261 case STR_CHEAT_CHANGE_DATE: SetDParam(0, _date); break;
263 /* Draw coloured flag for change company cheat */
264 case STR_CHEAT_CHANGE_COMPANY: {
265 char buf[512];
266 SetDParam(0, val + 1);
267 GetString (buf, STR_CHEAT_CHANGE_COMPANY);
268 uint offset = 10 + GetStringBoundingBox(buf).width;
269 DrawCompanyIcon(_local_company, rtl ? text_right - offset - 10 : text_left + offset, y + icon_y_offset + 2);
270 break;
273 default: SetDParam(0, val);
275 break;
279 DrawString(text_left, text_right, y + text_y_offset, ce->str);
281 y += this->line_height;
285 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
287 if (widget != WID_C_PANEL) return;
289 uint width = 0;
290 for (int i = 0; i != lengthof(_cheats_ui); i++) {
291 const CheatEntry *ce = &_cheats_ui[i];
292 switch (ce->type) {
293 case SLE_BOOL:
294 SetDParam(0, STR_CONFIG_SETTING_ON);
295 width = max(width, GetStringBoundingBox(ce->str).width);
296 SetDParam(0, STR_CONFIG_SETTING_OFF);
297 width = max(width, GetStringBoundingBox(ce->str).width);
298 break;
300 default:
301 switch (ce->str) {
302 /* Display date for change date cheat */
303 case STR_CHEAT_CHANGE_DATE:
304 SetDParam(0, ConvertYMDToDate(MAX_YEAR, 11, 31));
305 width = max(width, GetStringBoundingBox(ce->str).width);
306 break;
308 /* Draw coloured flag for change company cheat */
309 case STR_CHEAT_CHANGE_COMPANY:
310 SetDParamMaxValue(0, MAX_COMPANIES);
311 width = max(width, GetStringBoundingBox(ce->str).width + 10 + 10);
312 break;
314 default:
315 SetDParam(0, INT64_MAX);
316 width = max(width, GetStringBoundingBox(ce->str).width);
317 break;
319 break;
323 this->line_height = max(GetSpriteSize(SPR_BOX_CHECKED).height, GetSpriteSize(SPR_BOX_EMPTY).height);
324 this->line_height = max<uint>(this->line_height, SETTING_BUTTON_HEIGHT);
325 this->line_height = max<uint>(this->line_height, FONT_HEIGHT_NORMAL) + WD_PAR_VSEP_NORMAL;
327 size->width = width + 20 + this->box_width + SETTING_BUTTON_WIDTH /* stuff on the left */ + 10 /* extra spacing on right */;
328 this->header_height = GetStringHeight(STR_CHEATS_WARNING, size->width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT) + WD_PAR_VSEP_WIDE;
329 size->height = this->header_height + WD_FRAMERECT_TOP + WD_PAR_VSEP_NORMAL + WD_FRAMERECT_BOTTOM + this->line_height * lengthof(_cheats_ui);
332 virtual void OnClick(Point pt, int widget, int click_count)
334 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_C_PANEL);
335 uint btn = (pt.y - wid->pos_y - WD_FRAMERECT_TOP - this->header_height) / this->line_height;
336 int x = pt.x - wid->pos_x;
337 bool rtl = _current_text_dir == TD_RTL;
338 if (rtl) x = wid->current_x - x;
340 if (btn >= lengthof(_cheats_ui)) return;
342 const CheatEntry *ce = &_cheats_ui[btn];
343 int value = (int32)ReadValue(ce->variable, ce->type);
344 int oldvalue = value;
346 if (btn == CHT_CHANGE_DATE && x >= 20 + this->box_width + SETTING_BUTTON_WIDTH) {
347 /* Click at the date text directly. */
348 clicked_widget = CHT_CHANGE_DATE;
349 SetDParam(0, value);
350 ShowQueryString(STR_JUST_INT, STR_CHEAT_CHANGE_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
351 return;
352 } else if (btn == CHT_EDIT_MAX_HL && x >= 20 + this->box_width + SETTING_BUTTON_WIDTH) {
353 clicked_widget = CHT_EDIT_MAX_HL;
354 SetDParam(0, value);
355 ShowQueryString(STR_JUST_INT, STR_CHEAT_EDIT_MAX_HL_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
356 return;
359 /* Not clicking a button? */
360 if (!IsInsideMM(x, 10 + this->box_width, 10 + this->box_width + SETTING_BUTTON_WIDTH)) return;
362 *ce->been_used = true;
364 switch (ce->type) {
365 case SLE_BOOL:
366 value ^= 1;
367 if (ce->proc != NULL) ce->proc(value, 0);
368 break;
370 default:
371 /* Take whatever the function returns */
372 value = ce->proc(value + ((x >= 20 + SETTING_BUTTON_WIDTH / 2) ? 1 : -1), (x >= 10 + this->box_width + SETTING_BUTTON_WIDTH / 2) ? 1 : -1);
374 /* The first cheat (money), doesn't return a different value. */
375 if (value != oldvalue || btn == CHT_MONEY) this->clicked = btn * 2 + 1 + ((x >= 10 + this->box_width + SETTING_BUTTON_WIDTH / 2) != rtl ? 1 : 0);
376 break;
379 if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
381 this->SetTimeout();
383 this->SetDirty();
386 virtual void OnTimeout()
388 this->clicked = 0;
389 this->SetDirty();
392 virtual void OnQueryTextFinished(char *str)
394 /* Was 'cancel' pressed or nothing entered? */
395 if (str == NULL || StrEmpty(str)) return;
397 const CheatEntry *ce = &_cheats_ui[clicked_widget];
398 int oldvalue = (int32)ReadValue(ce->variable, ce->type);
399 int value = atoi(str);
400 *ce->been_used = true;
401 value = ce->proc(value, value - oldvalue);
403 if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
404 this->SetDirty();
408 /** Window preferences of the cheats GUI. */
409 static WindowDesc::Prefs _cheats_prefs ("cheats");
411 /** Window description of the cheats GUI. */
412 static const WindowDesc _cheats_desc(
413 WDP_AUTO, 0, 0,
414 WC_CHEATS, WC_NONE,
416 _nested_cheat_widgets, lengthof(_nested_cheat_widgets),
417 &_cheats_prefs
420 /** Open cheat window. */
421 void ShowCheatWindow()
423 DeleteWindowById(WC_CHEATS, 0);
424 new CheatWindow(&_cheats_desc);