# main_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: main_gui.cpp 18809 2010-01-15 16:41:15Z rubidium $ */
    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 "currency.h"
    00014 #include "spritecache.h"
    00015 #include "window_gui.h"
    00016 #include "window_func.h"
    00017 #include "textbuf_gui.h"
    00018 #include "viewport_func.h"
    00019 #include "command_func.h"
    00020 #include "console_gui.h"
    00021 #include "genworld.h"
    00022 #include "transparency_gui.h"
    00023 #include "functions.h"
    00024 #include "sound_func.h"
    00025 #include "transparency.h"
    00026 #include "strings_func.h"
    00027 #include "zoom_func.h"
    00028 #include "company_base.h"
    00029 #include "company_func.h"
    00030 #include "toolbar_gui.h"
    00031 #include "statusbar_gui.h"
    00032 #include "tilehighlight_func.h"
    00033
    00034 #include "network/network.h"
    00035 #include "network/network_func.h"
    00036 #include "network/network_gui.h"
    00037 #include "network/network_base.h"
    00038
    00039 #include "table/sprites.h"
    00040 #include "table/strings.h"
    00041
    00042 static int _rename_id = 1;
    00043 static int _rename_what = -1;
    00044
    00045 void CcGiveMoney(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    00046 {
    00047 #ifdef ENABLE_NETWORK
    00048   if (result.Failed() || !_settings_game.economy.give_money) return;
    00049
    00050   /* Inform the company of the action of one of it's clients (controllers). */
    00051   char msg[64];
    00052   SetDParam(0, p2);
    00053   GetString(msg, STR_COMPANY_NAME, lastof(msg));
    00054
    00055   if (!_network_server) {
    00056     NetworkClientSendChat(NETWORK_ACTION_GIVE_MONEY, DESTTYPE_TEAM, p2, msg, p1);
    00057   } else {
    00058     NetworkServerSendChat(NETWORK_ACTION_GIVE_MONEY, DESTTYPE_TEAM, p2, msg, CLIENT_ID_SERVER, p1);
    00059   }
    00060 #endif /* ENABLE_NETWORK */
    00061 }
    00062
    00063 void HandleOnEditText(const char *str)
    00064 {
    00065   switch (_rename_what) {
    00066 #ifdef ENABLE_NETWORK
    00067   case 3: { // Give money, you can only give money in excess of loan
    00068     const Company *c = Company::GetIfValid(_local_company);
    00069     if (c == NULL) break;
    00070     Money money = min(c->money - c->current_loan, (Money)(atoi(str) / _currency->rate));
    00071
    00072     uint32 money_c = Clamp(ClampToI32(money), 0, 20000000); // Clamp between 20 million and 0
    00073
    00074     /* Give 'id' the money, and substract it from ourself */
    00075     DoCommandP(0, money_c, _rename_id, CMD_GIVE_MONEY | CMD_MSG(STR_ERROR_INSUFFICIENT_FUNDS), CcGiveMoney, str);
    00076   } break;
    00077 #endif /* ENABLE_NETWORK */
    00078     default: NOT_REACHED();
    00079   }
    00080
    00081   _rename_id = _rename_what = -1;
    00082 }
    00083
    00095 bool HandlePlacePushButton(Window *w, int widget, CursorID cursor, HighLightStyle mode, PlaceProc *placeproc)
    00096 {
    00097   if (w->IsWidgetDisabled(widget)) return false;
    00098
    00099   SndPlayFx(SND_15_BEEP);
    00100   w->SetDirty();
    00101
    00102   if (w->IsWidgetLowered(widget)) {
    00103     ResetObjectToPlace();
    00104     return false;
    00105   }
    00106
    00107   SetObjectToPlace(cursor, PAL_NONE, mode, w->window_class, w->window_number);
    00108   w->LowerWidget(widget);
    00109   _place_proc = placeproc;
    00110   return true;
    00111 }
    00112
    00113
    00114 void CcPlaySound10(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    00115 {
    00116   if (result.Succeeded()) SndPlayTileFx(SND_12_EXPLOSION, tile);
    00117 }
    00118
    00119 #ifdef ENABLE_NETWORK
    00120 void ShowNetworkGiveMoneyWindow(CompanyID company)
    00121 {
    00122   _rename_id = company;
    00123   _rename_what = 3;
    00124   ShowQueryString(STR_EMPTY, STR_NETWORK_GIVE_MONEY_CAPTION, 30, 180, NULL, CS_NUMERAL, QSF_NONE);
    00125 }
    00126 #endif /* ENABLE_NETWORK */
    00127
    00128
    00129 /* Zooms a viewport in a window in or out
    00130  * No button handling or what so ever */
    00131 bool DoZoomInOutWindow(int how, Window *w)
    00132 {
    00133   ViewPort *vp;
    00134
    00135   assert(w != NULL);
    00136   vp = w->viewport;
    00137
    00138   switch (how) {
    00139     case ZOOM_IN:
    00140       if (vp->zoom == ZOOM_LVL_MIN) return false;
    00141       vp->zoom = (ZoomLevel)((int)vp->zoom - 1);
    00142       vp->virtual_width >>= 1;
    00143       vp->virtual_height >>= 1;
    00144
    00145       w->viewport->scrollpos_x += vp->virtual_width >> 1;
    00146       w->viewport->scrollpos_y += vp->virtual_height >> 1;
    00147       w->viewport->dest_scrollpos_x = w->viewport->scrollpos_x;
    00148       w->viewport->dest_scrollpos_y = w->viewport->scrollpos_y;
    00149       break;
    00150     case ZOOM_OUT:
    00151       if (vp->zoom == ZOOM_LVL_MAX) return false;
    00152       vp->zoom = (ZoomLevel)((int)vp->zoom + 1);
    00153
    00154       w->viewport->scrollpos_x -= vp->virtual_width >> 1;
    00155       w->viewport->scrollpos_y -= vp->virtual_height >> 1;
    00156       w->viewport->dest_scrollpos_x = w->viewport->scrollpos_x;
    00157       w->viewport->dest_scrollpos_y = w->viewport->scrollpos_y;
    00158
    00159       vp->virtual_width <<= 1;
    00160       vp->virtual_height <<= 1;
    00161       break;
    00162   }
    00163   if (vp != NULL) { // the vp can be null when how == ZOOM_NONE
    00164     vp->virtual_left = w->viewport->scrollpos_x;
    00165     vp->virtual_top = w->viewport->scrollpos_y;
    00166   }
    00167   /* Update the windows that have zoom-buttons to perhaps disable their buttons */
    00168   w->InvalidateData();
    00169   return true;
    00170 }
    00171
    00172 void ZoomInOrOutToCursorWindow(bool in, Window *w)
    00173 {
    00174   assert(w != NULL);
    00175
    00176   if (_game_mode != GM_MENU) {
    00177     ViewPort *vp = w->viewport;
    00178     if ((in && vp->zoom == ZOOM_LVL_MIN) || (!in && vp->zoom == ZOOM_LVL_MAX))
    00179       return;
    00180
    00181     Point pt = GetTileZoomCenterWindow(in, w);
    00182     if (pt.x != -1) {
    00183       ScrollWindowTo(pt.x, pt.y, -1, w, true);
    00184
    00185       DoZoomInOutWindow(in ? ZOOM_IN : ZOOM_OUT, w);
    00186     }
    00187   }
    00188 }
    00189
    00191 enum MainWindowWidgets {
    00192   MW_VIEWPORT,
    00193 };
    00194
    00195 static const struct NWidgetPart _nested_main_window_widgets[] = {
    00196   NWidget(NWID_VIEWPORT, INVALID_COLOUR, MW_VIEWPORT), SetResize(1, 1),
    00197 };
    00198
    00199 static const WindowDesc _main_window_desc(
    00200   WDP_MANUAL, 0, 0,
    00201   WC_MAIN_WINDOW, WC_NONE,
    00202   0,
    00203   _nested_main_window_widgets, lengthof(_nested_main_window_widgets)
    00204 );
    00205
    00206 struct MainWindow : Window
    00207 {
    00208   MainWindow() : Window()
    00209   {
    00210     this->InitNested(&_main_window_desc, 0);
    00211     ResizeWindow(this, _screen.width, _screen.height);
    00212
    00213     NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(MW_VIEWPORT);
    00214     nvp->InitializeViewport(this, TileXY(32, 32), ZOOM_LVL_VIEWPORT);
    00215   }
    00216
    00217   virtual void OnPaint()
    00218   {
    00219     this->DrawWidgets();
    00220     if (_game_mode == GM_MENU) {
    00221       int off_x = this->width / 2;
    00222
    00223       DrawSprite(SPR_OTTD_O, PAL_NONE, off_x - 120, 50);
    00224       DrawSprite(SPR_OTTD_P, PAL_NONE, off_x -  86, 50);
    00225       DrawSprite(SPR_OTTD_E, PAL_NONE, off_x -  53, 50);
    00226       DrawSprite(SPR_OTTD_N, PAL_NONE, off_x -  22, 50);
    00227
    00228       DrawSprite(SPR_OTTD_T, PAL_NONE, off_x +  34, 50);
    00229       DrawSprite(SPR_OTTD_T, PAL_NONE, off_x +  65, 50);
    00230       DrawSprite(SPR_OTTD_D, PAL_NONE, off_x +  96, 50);
    00231     }
    00232   }
    00233
    00234   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00235   {
    00236     switch (keycode) {
    00237       case 'Q' | WKC_CTRL:
    00238       case 'Q' | WKC_META:
    00239         HandleExitGameRequest();
    00240         return ES_HANDLED;
    00241     }
    00242
    00243     /* Disable all key shortcuts, except quit shortcuts when
    00244      * generating the world, otherwise they create threading
    00245      * problem during the generating, resulting in random
    00246      * assertions that are hard to trigger and debug */
    00247     if (IsGeneratingWorld()) return ES_NOT_HANDLED;
    00248
    00249     if (keycode == WKC_BACKQUOTE) {
    00250       IConsoleSwitch();
    00251       return ES_HANDLED;
    00252     }
    00253
    00254     if (keycode == ('B' | WKC_CTRL)) {
    00255       extern bool _draw_bounding_boxes;
    00256       _draw_bounding_boxes = !_draw_bounding_boxes;
    00257       MarkWholeScreenDirty();
    00258       return ES_HANDLED;
    00259     }
    00260
    00261     if (_game_mode == GM_MENU) return ES_NOT_HANDLED;
    00262
    00263     switch (keycode) {
    00264       case 'C':
    00265       case 'Z': {
    00266         Point pt = GetTileBelowCursor();
    00267         if (pt.x != -1) {
    00268           if (keycode == 'Z') MaxZoomInOut(ZOOM_IN, this);
    00269           ScrollMainWindowTo(pt.x, pt.y);
    00270         }
    00271         break;
    00272       }
    00273
    00274       case WKC_ESC: ResetObjectToPlace(); break;
    00275       case WKC_DELETE: DeleteNonVitalWindows(); break;
    00276       case WKC_DELETE | WKC_SHIFT: DeleteAllNonVitalWindows(); break;
    00277       case 'R' | WKC_CTRL: MarkWholeScreenDirty(); break;
    00278
    00279 #if defined(_DEBUG)
    00280       case '0' | WKC_ALT: // Crash the game
    00281         *(byte*)0 = 0;
    00282         break;
    00283
    00284       case '1' | WKC_ALT: // Gimme money
    00285         /* Server can not cheat in advertise mode either! */
    00286 #ifdef ENABLE_NETWORK
    00287         if (!_networking || !_network_server || !_settings_client.network.server_advertise)
    00288 #endif /* ENABLE_NETWORK */
    00289           DoCommandP(0, 10000000, 0, CMD_MONEY_CHEAT);
    00290         break;
    00291
    00292       case '2' | WKC_ALT: // Update the coordinates of all station signs
    00293         UpdateAllVirtCoords();
    00294         break;
    00295 #endif
    00296
    00297       case '1' | WKC_CTRL:
    00298       case '2' | WKC_CTRL:
    00299       case '3' | WKC_CTRL:
    00300       case '4' | WKC_CTRL:
    00301       case '5' | WKC_CTRL:
    00302       case '6' | WKC_CTRL:
    00303       case '7' | WKC_CTRL:
    00304       case '8' | WKC_CTRL:
    00305       case '9' | WKC_CTRL:
    00306         /* Transparency toggle hot keys */
    00307         ToggleTransparency((TransparencyOption)(keycode - ('1' | WKC_CTRL)));
    00308         MarkWholeScreenDirty();
    00309         break;
    00310
    00311       case '1' | WKC_CTRL | WKC_SHIFT:
    00312       case '2' | WKC_CTRL | WKC_SHIFT:
    00313       case '3' | WKC_CTRL | WKC_SHIFT:
    00314       case '4' | WKC_CTRL | WKC_SHIFT:
    00315       case '5' | WKC_CTRL | WKC_SHIFT:
    00316       case '6' | WKC_CTRL | WKC_SHIFT:
    00317       case '7' | WKC_CTRL | WKC_SHIFT:
    00318       case '8' | WKC_CTRL | WKC_SHIFT:
    00319         /* Invisibility toggle hot keys */
    00320         ToggleInvisibilityWithTransparency((TransparencyOption)(keycode - ('1' | WKC_CTRL | WKC_SHIFT)));
    00321         MarkWholeScreenDirty();
    00322         break;
    00323
    00324       case 'X' | WKC_CTRL:
    00325         ShowTransparencyToolbar();
    00326         break;
    00327
    00328       case 'X':
    00329         ResetRestoreAllTransparency();
    00330         break;
    00331
    00332 #ifdef ENABLE_NETWORK
    00333       case WKC_RETURN: case 'T': // smart chat; send to team if any, otherwise to all
    00334         if (_networking) {
    00335           const NetworkClientInfo *cio = NetworkFindClientInfoFromClientID(_network_own_client_id);
    00336           if (cio == NULL) break;
    00337
    00338           ShowNetworkChatQueryWindow(NetworkClientPreferTeamChat(cio) ? DESTTYPE_TEAM : DESTTYPE_BROADCAST, cio->client_playas);
    00339         }
    00340         break;
    00341
    00342       case WKC_SHIFT | WKC_RETURN: case WKC_SHIFT | 'T': // send text message to all clients
    00343         if (_networking) ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST, 0);
    00344         break;
    00345
    00346       case WKC_CTRL | WKC_RETURN: case WKC_CTRL | 'T': // send text to all team mates
    00347         if (_networking) {
    00348           const NetworkClientInfo *cio = NetworkFindClientInfoFromClientID(_network_own_client_id);
    00349           if (cio == NULL) break;
    00350
    00351           ShowNetworkChatQueryWindow(DESTTYPE_TEAM, cio->client_playas);
    00352         }
    00353         break;
    00354 #endif
    00355
    00356       default: return ES_NOT_HANDLED;
    00357     }
    00358     return ES_HANDLED;
    00359   }
    00360
    00361   virtual void OnScroll(Point delta)
    00362   {
    00363     ViewPort *vp = IsPtInWindowViewport(this, _cursor.pos.x, _cursor.pos.y);
    00364
    00365     if (vp == NULL) {
    00366       _cursor.fix_at = false;
    00367       _scrolling_viewport = false;
    00368     }
    00369
    00370     this->viewport->scrollpos_x += ScaleByZoom(delta.x, vp->zoom);
    00371     this->viewport->scrollpos_y += ScaleByZoom(delta.y, vp->zoom);
    00372     this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
    00373     this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
    00374   };
    00375
    00376   virtual void OnMouseWheel(int wheel)
    00377   {
    00378     ZoomInOrOutToCursorWindow(wheel < 0, this);
    00379   }
    00380
    00381   virtual void OnResize()
    00382   {
    00383     if (this->viewport != NULL) {
    00384       NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(MW_VIEWPORT);
    00385       nvp->UpdateViewportCoordinates(this);
    00386     }
    00387   }
    00388
    00389   virtual void OnInvalidateData(int data)
    00390   {
    00391     /* Forward the message to the appropiate toolbar (ingame or scenario editor) */
    00392     InvalidateWindowData(WC_MAIN_TOOLBAR, 0, data);
    00393   }
    00394 };
    00395
    00396
    00397 void ShowSelectGameWindow();
    00398
    00399 void SetupColoursAndInitialWindow()
    00400 {
    00401   for (uint i = 0; i != 16; i++) {
    00402     const byte *b = GetNonSprite(PALETTE_RECOLOUR_START + i, ST_RECOLOUR);
    00403
    00404     assert(b);
    00405     memcpy(_colour_gradient[i], b + 0xC6, sizeof(_colour_gradient[i]));
    00406   }
    00407
    00408   new MainWindow;
    00409
    00410   /* XXX: these are not done */
    00411   switch (_game_mode) {
    00412     default: NOT_REACHED();
    00413     case GM_MENU:
    00414       ShowSelectGameWindow();
    00415       break;
    00416
    00417     case GM_NORMAL:
    00418     case GM_EDITOR:
    00419       ShowVitalWindows();
    00420       break;
    00421   }
    00422 }
    00423
    00424 void ShowVitalWindows()
    00425 {
    00426   AllocateToolbar();
    00427
    00428   /* Status bad only for normal games */
    00429   if (_game_mode == GM_EDITOR) return;
    00430
    00431   ShowStatusBar();
    00432 }
    00433
    00438 void GameSizeChanged()
    00439 {
    00440   _cur_resolution.width  = _screen.width;
    00441   _cur_resolution.height = _screen.height;
    00442   ScreenSizeChanged();
    00443   RelocateAllWindows(_screen.width, _screen.height);
    00444   MarkWholeScreenDirty();
    00445 }