Add non-animated SSSE3 blitter
[openttd/fttd.git] / src / cheat_gui.cpp
blob9eb405d2ab588ee4f9496fab8ea0dba0cea7204f
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_func.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"
29 #include "widgets/cheat_widget.h"
31 #include "table/sprites.h"
34 /**
35 * The 'amount' to cheat with.
36 * This variable is semantically a constant value, but because the cheat
37 * code requires to be able to write to the variable it is not constified.
39 static int32 _money_cheat_amount = 10000000;
41 /**
42 * Handle cheating of money.
43 * Note that the amount of money of a company must be changed through a command
44 * rather than by setting a variable. Since the cheat data structure expects a
45 * variable, the amount of given/taken money is used for this purpose.
46 * @param p1 not used.
47 * @param p2 is -1 or +1 (down/up)
48 * @return Amount of money cheat.
50 static int32 ClickMoneyCheat(int32 p1, int32 p2)
52 DoCommandP(0, (uint32)(p2 * _money_cheat_amount), 0, CMD_MONEY_CHEAT);
53 return _money_cheat_amount;
56 /**
57 * Handle changing of company.
58 * @param p1 company to set to
59 * @param p2 is -1 or +1 (down/up)
60 * @return The new company.
62 static int32 ClickChangeCompanyCheat(int32 p1, int32 p2)
64 while ((uint)p1 < Company::GetPoolSize()) {
65 if (Company::IsValidID((CompanyID)p1)) {
66 SetLocalCompany((CompanyID)p1);
67 return _local_company;
69 p1 += p2;
72 return _local_company;
75 /**
76 * Allow (or disallow) changing production of all industries.
77 * @param p1 new value
78 * @param p2 unused
79 * @return New value allowing change of industry production.
81 static int32 ClickSetProdCheat(int32 p1, int32 p2)
83 _cheats.setup_prod.value = (p1 != 0);
84 InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
85 return _cheats.setup_prod.value;
88 extern void EnginesMonthlyLoop();
90 /**
91 * Handle changing of the current year.
92 * @param p1 Unused.
93 * @param p2 +1 (increase) or -1 (decrease).
94 * @return New year.
96 static int32 ClickChangeDateCheat(int32 p1, int32 p2)
98 YearMonthDay ymd;
99 ConvertDateToYMD(_date, &ymd);
101 p1 = Clamp(p1, MIN_YEAR, MAX_YEAR);
102 if (p1 == _cur_year) return _cur_year;
104 Date new_date = ConvertYMDToDate(p1, ymd.month, ymd.day);
105 LinkGraphSchedule::Instance()->ShiftDates(new_date - _date);
106 SetDate(new_date, _date_fract);
107 EnginesMonthlyLoop();
108 SetWindowDirty(WC_STATUS_BAR, 0);
109 InvalidateWindowClassesData(WC_BUILD_STATION, 0);
110 InvalidateWindowClassesData(WC_BUILD_OBJECT, 0);
111 ResetSignalVariant();
112 return _cur_year;
115 /** Available cheats. */
116 enum CheatNumbers {
117 CHT_MONEY, ///< Change amount of money.
118 CHT_CHANGE_COMPANY, ///< Switch company.
119 CHT_EXTRA_DYNAMITE, ///< Dynamite anything.
120 CHT_CROSSINGTUNNELS, ///< Allow tunnels to cross each other.
121 CHT_NO_JETCRASH, ///< Disable jet-airplane crashes.
122 CHT_SETUP_PROD, ///< Allow manually editing of industry production.
123 CHT_CHANGE_DATE, ///< Do time traveling.
125 CHT_NUM_CHEATS, ///< Number of cheats.
129 * Signature of handler function when user clicks at a cheat.
130 * @param p1 The new value.
131 * @param p2 Change direction (+1, +1), \c 0 for boolean settings.
133 typedef int32 CheckButtonClick(int32 p1, int32 p2);
135 /** Information of a cheat. */
136 struct CheatEntry {
137 VarType type; ///< type of selector
138 StringID str; ///< string with descriptive text
139 void *variable; ///< pointer to the variable
140 bool *been_used; ///< has this cheat been used before?
141 CheckButtonClick *proc;///< procedure
145 * The available cheats.
146 * Order matches with the values of #CheatNumbers
148 static const CheatEntry _cheats_ui[] = {
149 {SLE_INT32, STR_CHEAT_MONEY, &_money_cheat_amount, &_cheats.money.been_used, &ClickMoneyCheat },
150 {SLE_UINT8, STR_CHEAT_CHANGE_COMPANY, &_local_company, &_cheats.switch_company.been_used, &ClickChangeCompanyCheat },
151 {SLE_BOOL, STR_CHEAT_EXTRA_DYNAMITE, &_cheats.magic_bulldozer.value, &_cheats.magic_bulldozer.been_used, NULL },
152 {SLE_BOOL, STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value, &_cheats.crossing_tunnels.been_used, NULL },
153 {SLE_BOOL, STR_CHEAT_NO_JETCRASH, &_cheats.no_jetcrash.value, &_cheats.no_jetcrash.been_used, NULL },
154 {SLE_BOOL, STR_CHEAT_SETUP_PROD, &_cheats.setup_prod.value, &_cheats.setup_prod.been_used, &ClickSetProdCheat },
155 {SLE_INT32, STR_CHEAT_CHANGE_DATE, &_cur_year, &_cheats.change_date.been_used, &ClickChangeDateCheat },
158 assert_compile(CHT_NUM_CHEATS == lengthof(_cheats_ui));
160 /** Widget definitions of the cheat GUI. */
161 static const NWidgetPart _nested_cheat_widgets[] = {
162 NWidget(NWID_HORIZONTAL),
163 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
164 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CHEATS, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
165 NWidget(WWT_SHADEBOX, COLOUR_GREY),
166 NWidget(WWT_STICKYBOX, COLOUR_GREY),
167 EndContainer(),
168 NWidget(WWT_PANEL, COLOUR_GREY, WID_C_PANEL), SetDataTip(0x0, STR_CHEATS_TOOLTIP), EndContainer(),
171 /** GUI for the cheats. */
172 struct CheatWindow : Window {
173 int clicked;
174 int header_height;
176 CheatWindow(WindowDesc *desc) : Window(desc)
178 this->InitNested();
181 virtual void DrawWidget(const Rect &r, int widget) const
183 if (widget != WID_C_PANEL) return;
185 int y = r.top + WD_FRAMERECT_TOP + this->header_height;
186 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, y, STR_CHEATS_WARNING, TC_FROMSTRING, SA_CENTER);
188 bool rtl = _current_text_dir == TD_RTL;
189 uint box_left = rtl ? r.right - 12 : r.left + 5;
190 uint button_left = rtl ? r.right - 20 - SETTING_BUTTON_WIDTH : r.left + 20;
191 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : 30 + SETTING_BUTTON_WIDTH);
192 uint text_right = r.right - (rtl ? 30 + SETTING_BUTTON_WIDTH : WD_FRAMERECT_RIGHT);
194 for (int i = 0; i != lengthof(_cheats_ui); i++) {
195 const CheatEntry *ce = &_cheats_ui[i];
197 DrawSprite((*ce->been_used) ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, box_left, y + 2);
199 switch (ce->type) {
200 case SLE_BOOL: {
201 bool on = (*(bool*)ce->variable);
203 DrawBoolButton(button_left, y, on, true);
204 SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
205 break;
208 default: {
209 int32 val = (int32)ReadValue(ce->variable, ce->type);
210 char buf[512];
212 /* Draw [<][>] boxes for settings of an integer-type */
213 DrawArrowButtons(button_left, y, COLOUR_YELLOW, clicked - (i * 2), true, true);
215 switch (ce->str) {
216 /* Display date for change date cheat */
217 case STR_CHEAT_CHANGE_DATE: SetDParam(0, _date); break;
219 /* Draw coloured flag for change company cheat */
220 case STR_CHEAT_CHANGE_COMPANY: {
221 SetDParam(0, val + 1);
222 GetString(buf, STR_CHEAT_CHANGE_COMPANY, lastof(buf));
223 uint offset = 10 + GetStringBoundingBox(buf).width;
224 DrawCompanyIcon(_local_company, rtl ? text_right - offset - 10 : text_left + offset, y + 2);
225 break;
228 default: SetDParam(0, val);
230 break;
234 DrawString(text_left, text_right, y + 1, ce->str);
236 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
240 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
242 if (widget != WID_C_PANEL) return;
244 uint width = 0;
245 for (int i = 0; i != lengthof(_cheats_ui); i++) {
246 const CheatEntry *ce = &_cheats_ui[i];
247 switch (ce->type) {
248 case SLE_BOOL:
249 SetDParam(0, STR_CONFIG_SETTING_ON);
250 width = max(width, GetStringBoundingBox(ce->str).width);
251 SetDParam(0, STR_CONFIG_SETTING_OFF);
252 width = max(width, GetStringBoundingBox(ce->str).width);
253 break;
255 default:
256 switch (ce->str) {
257 /* Display date for change date cheat */
258 case STR_CHEAT_CHANGE_DATE:
259 SetDParam(0, ConvertYMDToDate(MAX_YEAR, 11, 31));
260 width = max(width, GetStringBoundingBox(ce->str).width);
261 break;
263 /* Draw coloured flag for change company cheat */
264 case STR_CHEAT_CHANGE_COMPANY:
265 SetDParamMaxValue(0, MAX_COMPANIES);
266 width = max(width, GetStringBoundingBox(ce->str).width + 10 + 10);
267 break;
269 default:
270 SetDParam(0, INT64_MAX);
271 width = max(width, GetStringBoundingBox(ce->str).width);
272 break;
274 break;
278 size->width = width + 50 /* stuff on the left */ + 10 /* extra spacing on right */;
279 this->header_height = GetStringHeight(STR_CHEATS_WARNING, size->width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT) + WD_PAR_VSEP_WIDE;
280 size->height = this->header_height + WD_FRAMERECT_TOP + WD_PAR_VSEP_NORMAL + WD_FRAMERECT_BOTTOM + (FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL) * lengthof(_cheats_ui);
283 virtual void OnClick(Point pt, int widget, int click_count)
285 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_C_PANEL);
286 uint btn = (pt.y - wid->pos_y - WD_FRAMERECT_TOP - this->header_height) / (FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL);
287 uint x = pt.x - wid->pos_x;
288 bool rtl = _current_text_dir == TD_RTL;
289 if (rtl) x = wid->current_x - x;
291 if (btn >= lengthof(_cheats_ui)) return;
293 const CheatEntry *ce = &_cheats_ui[btn];
294 int value = (int32)ReadValue(ce->variable, ce->type);
295 int oldvalue = value;
297 if (btn == CHT_CHANGE_DATE && x >= 20 + SETTING_BUTTON_WIDTH) {
298 /* Click at the date text directly. */
299 SetDParam(0, value);
300 ShowQueryString(STR_JUST_INT, STR_CHEAT_CHANGE_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
301 return;
304 /* Not clicking a button? */
305 if (!IsInsideMM(x, 20, 20 + SETTING_BUTTON_WIDTH)) return;
307 *ce->been_used = true;
309 switch (ce->type) {
310 case SLE_BOOL:
311 value ^= 1;
312 if (ce->proc != NULL) ce->proc(value, 0);
313 break;
315 default:
316 /* Take whatever the function returns */
317 value = ce->proc(value + ((x >= 20 + SETTING_BUTTON_WIDTH / 2) ? 1 : -1), (x >= 20 + SETTING_BUTTON_WIDTH / 2) ? 1 : -1);
319 /* The first cheat (money), doesn't return a different value. */
320 if (value != oldvalue || btn == CHT_MONEY) this->clicked = btn * 2 + 1 + ((x >= 20 + SETTING_BUTTON_WIDTH / 2) != rtl ? 1 : 0);
321 break;
324 if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
326 this->SetTimeout();
328 this->SetDirty();
331 virtual void OnTimeout()
333 this->clicked = 0;
334 this->SetDirty();
337 virtual void OnQueryTextFinished(char *str)
339 /* Was 'cancel' pressed or nothing entered? */
340 if (str == NULL || StrEmpty(str)) return;
342 const CheatEntry *ce = &_cheats_ui[CHT_CHANGE_DATE];
343 int oldvalue = (int32)ReadValue(ce->variable, ce->type);
344 int value = atoi(str);
345 *ce->been_used = true;
346 value = ce->proc(value, value - oldvalue);
348 if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
349 this->SetDirty();
353 /** Window description of the cheats GUI. */
354 static WindowDesc _cheats_desc(
355 WDP_AUTO, "cheats", 0, 0,
356 WC_CHEATS, WC_NONE,
358 _nested_cheat_widgets, lengthof(_nested_cheat_widgets)
361 /** Open cheat window. */
362 void ShowCheatWindow()
364 DeleteWindowById(WC_CHEATS, 0);
365 new CheatWindow(&_cheats_desc);