cheat_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "command_func.h"
00014 #include "cheat_type.h"
00015 #include "company_base.h"
00016 #include "company_func.h"
00017 #include "date_func.h"
00018 #include "saveload/saveload.h"
00019 #include "textbuf_gui.h"
00020 #include "window_gui.h"
00021 #include "string_func.h"
00022 #include "strings_func.h"
00023 #include "window_func.h"
00024 #include "rail_gui.h"
00025 #include "settings_gui.h"
00026 #include "company_gui.h"
00027 
00028 #include "widgets/cheat_widget.h"
00029 
00030 #include "table/sprites.h"
00031 
00032 
00038 static int32 _money_cheat_amount = 10000000;
00039 
00049 static int32 ClickMoneyCheat(int32 p1, int32 p2)
00050 {
00051   DoCommandP(0, (uint32)(p2 * _money_cheat_amount), 0, CMD_MONEY_CHEAT);
00052   return _money_cheat_amount;
00053 }
00054 
00061 static int32 ClickChangeCompanyCheat(int32 p1, int32 p2)
00062 {
00063   while ((uint)p1 < Company::GetPoolSize()) {
00064     if (Company::IsValidID((CompanyID)p1)) {
00065       SetLocalCompany((CompanyID)p1);
00066       return _local_company;
00067     }
00068     p1 += p2;
00069   }
00070 
00071   return _local_company;
00072 }
00073 
00080 static int32 ClickSetProdCheat(int32 p1, int32 p2)
00081 {
00082   _cheats.setup_prod.value = (p1 != 0);
00083   InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
00084   return _cheats.setup_prod.value;
00085 }
00086 
00087 extern void EnginesMonthlyLoop();
00088 
00095 static int32 ClickChangeDateCheat(int32 p1, int32 p2)
00096 {
00097   YearMonthDay ymd;
00098   ConvertDateToYMD(_date, &ymd);
00099 
00100   p1 = Clamp(p1, MIN_YEAR, MAX_YEAR);
00101   if (p1 == _cur_year) return _cur_year;
00102 
00103   SetDate(ConvertYMDToDate(p1, ymd.month, ymd.day), _date_fract);
00104   EnginesMonthlyLoop();
00105   SetWindowDirty(WC_STATUS_BAR, 0);
00106   InvalidateWindowClassesData(WC_BUILD_STATION, 0);
00107   InvalidateWindowClassesData(WC_BUILD_OBJECT, 0);
00108   ResetSignalVariant();
00109   return _cur_year;
00110 }
00111 
00113 enum CheatNumbers {
00114   CHT_MONEY,           
00115   CHT_CHANGE_COMPANY,  
00116   CHT_EXTRA_DYNAMITE,  
00117   CHT_CROSSINGTUNNELS, 
00118   CHT_NO_JETCRASH,     
00119   CHT_SETUP_PROD,      
00120   CHT_CHANGE_DATE,     
00121 
00122   CHT_NUM_CHEATS,      
00123 };
00124 
00130 typedef int32 CheckButtonClick(int32 p1, int32 p2);
00131 
00133 struct CheatEntry {
00134   VarType type;          
00135   StringID str;          
00136   void *variable;        
00137   bool *been_used;       
00138   CheckButtonClick *proc;
00139 };
00140 
00145 static const CheatEntry _cheats_ui[] = {
00146   {SLE_INT32, STR_CHEAT_MONEY,           &_money_cheat_amount,                    &_cheats.money.been_used,            &ClickMoneyCheat         },
00147   {SLE_UINT8, STR_CHEAT_CHANGE_COMPANY,  &_local_company,                         &_cheats.switch_company.been_used,   &ClickChangeCompanyCheat },
00148   {SLE_BOOL,  STR_CHEAT_EXTRA_DYNAMITE,  &_cheats.magic_bulldozer.value,          &_cheats.magic_bulldozer.been_used,  NULL                     },
00149   {SLE_BOOL,  STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value,         &_cheats.crossing_tunnels.been_used, NULL                     },
00150   {SLE_BOOL,  STR_CHEAT_NO_JETCRASH,     &_cheats.no_jetcrash.value,              &_cheats.no_jetcrash.been_used,      NULL                     },
00151   {SLE_BOOL,  STR_CHEAT_SETUP_PROD,      &_cheats.setup_prod.value,               &_cheats.setup_prod.been_used,       &ClickSetProdCheat       },
00152   {SLE_INT32, STR_CHEAT_CHANGE_DATE,     &_cur_year,                              &_cheats.change_date.been_used,      &ClickChangeDateCheat    },
00153 };
00154 
00155 assert_compile(CHT_NUM_CHEATS == lengthof(_cheats_ui));
00156 
00158 static const NWidgetPart _nested_cheat_widgets[] = {
00159   NWidget(NWID_HORIZONTAL),
00160     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00161     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CHEATS, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00162     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00163     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00164   EndContainer(),
00165   NWidget(WWT_PANEL, COLOUR_GREY, WID_C_PANEL), SetDataTip(0x0, STR_CHEATS_TOOLTIP), EndContainer(),
00166 };
00167 
00169 struct CheatWindow : Window {
00170   int clicked;
00171   int header_height;
00172 
00173   CheatWindow(WindowDesc *desc) : Window(desc)
00174   {
00175     this->InitNested();
00176   }
00177 
00178   virtual void DrawWidget(const Rect &r, int widget) const
00179   {
00180     if (widget != WID_C_PANEL) return;
00181 
00182     int y = r.top + WD_FRAMERECT_TOP + this->header_height;
00183     DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, y, STR_CHEATS_WARNING, TC_FROMSTRING, SA_CENTER);
00184 
00185     bool rtl = _current_text_dir == TD_RTL;
00186     uint box_left    = rtl ? r.right - 12 : r.left + 5;
00187     uint button_left = rtl ? r.right - 20 - SETTING_BUTTON_WIDTH : r.left + 20;
00188     uint text_left   = r.left + (rtl ? WD_FRAMERECT_LEFT : 30 + SETTING_BUTTON_WIDTH);
00189     uint text_right  = r.right - (rtl ? 30 + SETTING_BUTTON_WIDTH : WD_FRAMERECT_RIGHT);
00190 
00191     for (int i = 0; i != lengthof(_cheats_ui); i++) {
00192       const CheatEntry *ce = &_cheats_ui[i];
00193 
00194       DrawSprite((*ce->been_used) ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, box_left, y + 2);
00195 
00196       switch (ce->type) {
00197         case SLE_BOOL: {
00198           bool on = (*(bool*)ce->variable);
00199 
00200           DrawBoolButton(button_left, y, on, true);
00201           SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
00202           break;
00203         }
00204 
00205         default: {
00206           int32 val = (int32)ReadValue(ce->variable, ce->type);
00207           char buf[512];
00208 
00209           /* Draw [<][>] boxes for settings of an integer-type */
00210           DrawArrowButtons(button_left, y, COLOUR_YELLOW, clicked - (i * 2), true, true);
00211 
00212           switch (ce->str) {
00213             /* Display date for change date cheat */
00214             case STR_CHEAT_CHANGE_DATE: SetDParam(0, _date); break;
00215 
00216             /* Draw coloured flag for change company cheat */
00217             case STR_CHEAT_CHANGE_COMPANY: {
00218               SetDParam(0, val + 1);
00219               GetString(buf, STR_CHEAT_CHANGE_COMPANY, lastof(buf));
00220               uint offset = 10 + GetStringBoundingBox(buf).width;
00221               DrawCompanyIcon(_local_company, rtl ? text_right - offset - 10 : text_left + offset, y + 2);
00222               break;
00223             }
00224 
00225             default: SetDParam(0, val);
00226           }
00227           break;
00228         }
00229       }
00230 
00231       DrawString(text_left, text_right, y + 1, ce->str);
00232 
00233       y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00234     }
00235   }
00236 
00237   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00238   {
00239     if (widget != WID_C_PANEL) return;
00240 
00241     uint width = 0;
00242     for (int i = 0; i != lengthof(_cheats_ui); i++) {
00243       const CheatEntry *ce = &_cheats_ui[i];
00244       switch (ce->type) {
00245         case SLE_BOOL:
00246           SetDParam(0, STR_CONFIG_SETTING_ON);
00247           width = max(width, GetStringBoundingBox(ce->str).width);
00248           SetDParam(0, STR_CONFIG_SETTING_OFF);
00249           width = max(width, GetStringBoundingBox(ce->str).width);
00250           break;
00251 
00252         default:
00253           switch (ce->str) {
00254             /* Display date for change date cheat */
00255             case STR_CHEAT_CHANGE_DATE:
00256               SetDParam(0, ConvertYMDToDate(MAX_YEAR, 11, 31));
00257               width = max(width, GetStringBoundingBox(ce->str).width);
00258               break;
00259 
00260             /* Draw coloured flag for change company cheat */
00261             case STR_CHEAT_CHANGE_COMPANY:
00262               SetDParamMaxValue(0, MAX_COMPANIES);
00263               width = max(width, GetStringBoundingBox(ce->str).width + 10 + 10);
00264               break;
00265 
00266             default:
00267               SetDParam(0, INT64_MAX);
00268               width = max(width, GetStringBoundingBox(ce->str).width);
00269               break;
00270           }
00271           break;
00272       }
00273     }
00274 
00275     size->width = width + 50 /* stuff on the left */ + 10 /* extra spacing on right */;
00276     this->header_height = GetStringHeight(STR_CHEATS_WARNING, size->width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT) + WD_PAR_VSEP_WIDE;
00277     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);
00278   }
00279 
00280   virtual void OnClick(Point pt, int widget, int click_count)
00281   {
00282     const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_C_PANEL);
00283     uint btn = (pt.y - wid->pos_y - WD_FRAMERECT_TOP - this->header_height) / (FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL);
00284     uint x = pt.x - wid->pos_x;
00285     bool rtl = _current_text_dir == TD_RTL;
00286     if (rtl) x = wid->current_x - x;
00287 
00288     if (btn >= lengthof(_cheats_ui)) return;
00289 
00290     const CheatEntry *ce = &_cheats_ui[btn];
00291     int value = (int32)ReadValue(ce->variable, ce->type);
00292     int oldvalue = value;
00293 
00294     if (btn == CHT_CHANGE_DATE && x >= 20 + SETTING_BUTTON_WIDTH) {
00295       /* Click at the date text directly. */
00296       SetDParam(0, value);
00297       ShowQueryString(STR_JUST_INT, STR_CHEAT_CHANGE_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
00298       return;
00299     }
00300 
00301     /* Not clicking a button? */
00302     if (!IsInsideMM(x, 20, 20 + SETTING_BUTTON_WIDTH)) return;
00303 
00304     *ce->been_used = true;
00305 
00306     switch (ce->type) {
00307       case SLE_BOOL:
00308         value ^= 1;
00309         if (ce->proc != NULL) ce->proc(value, 0);
00310         break;
00311 
00312       default:
00313         /* Take whatever the function returns */
00314         value = ce->proc(value + ((x >= 20 + SETTING_BUTTON_WIDTH / 2) ? 1 : -1), (x >= 20 + SETTING_BUTTON_WIDTH / 2) ? 1 : -1);
00315 
00316         /* The first cheat (money), doesn't return a different value. */
00317         if (value != oldvalue || btn == CHT_MONEY) this->clicked = btn * 2 + 1 + ((x >= 20 + SETTING_BUTTON_WIDTH / 2) != rtl ? 1 : 0);
00318         break;
00319     }
00320 
00321     if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
00322 
00323     this->SetTimeout();
00324 
00325     this->SetDirty();
00326   }
00327 
00328   virtual void OnTimeout()
00329   {
00330     this->clicked = 0;
00331     this->SetDirty();
00332   }
00333 
00334   virtual void OnQueryTextFinished(char *str)
00335   {
00336     /* Was 'cancel' pressed or nothing entered? */
00337     if (str == NULL || StrEmpty(str)) return;
00338 
00339     const CheatEntry *ce = &_cheats_ui[CHT_CHANGE_DATE];
00340     int oldvalue = (int32)ReadValue(ce->variable, ce->type);
00341     int value = atoi(str);
00342     *ce->been_used = true;
00343     value = ce->proc(value, value - oldvalue);
00344 
00345     if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
00346     this->SetDirty();
00347   }
00348 };
00349 
00351 static WindowDesc _cheats_desc(
00352   WDP_AUTO, "cheats", 0, 0,
00353   WC_CHEATS, WC_NONE,
00354   0,
00355   _nested_cheat_widgets, lengthof(_nested_cheat_widgets)
00356 );
00357 
00359 void ShowCheatWindow()
00360 {
00361   DeleteWindowById(WC_CHEATS, 0);
00362   new CheatWindow(&_cheats_desc);
00363 }