# main_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: main_gui.cpp 20698 2010-08-30 16:52:37Z 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       static const SpriteID title_sprites[] = {SPR_OTTD_O, SPR_OTTD_P, SPR_OTTD_E, SPR_OTTD_N, SPR_OTTD_T, SPR_OTTD_T, SPR_OTTD_D};
    00222       static const uint LETTER_SPACING = 10;
    00223       int name_width = (lengthof(title_sprites) - 1) * LETTER_SPACING;
    00224
    00225       for (uint i = 0; i < lengthof(title_sprites); i++) {
    00226         name_width += GetSpriteSize(title_sprites[i]).width;
    00227       }
    00228       int off_x = (this->width - name_width) / 2;
    00229
    00230       for (uint i = 0; i < lengthof(title_sprites); i++) {
    00231         DrawSprite(title_sprites[i], PAL_NONE, off_x, 50);
    00232         off_x += GetSpriteSize(title_sprites[i]).width + LETTER_SPACING;
    00233       }
    00234     }
    00235   }
    00236
    00237   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00238   {
    00239     switch (keycode) {
    00240       case 'Q' | WKC_CTRL:
    00241       case 'Q' | WKC_META:
    00242         HandleExitGameRequest();
    00243         return ES_HANDLED;
    00244     }
    00245
    00246     /* Disable all key shortcuts, except quit shortcuts when
    00247      * generating the world, otherwise they create threading
    00248      * problem during the generating, resulting in random
    00249      * assertions that are hard to trigger and debug */
    00250     if (IsGeneratingWorld()) return ES_NOT_HANDLED;
    00251
    00252     if (keycode == WKC_BACKQUOTE) {
    00253       IConsoleSwitch();
    00254       return ES_HANDLED;
    00255     }
    00256
    00257     if (keycode == ('B' | WKC_CTRL)) {
    00258       extern bool _draw_bounding_boxes;
    00259       _draw_bounding_boxes = !_draw_bounding_boxes;
    00260       MarkWholeScreenDirty();
    00261       return ES_HANDLED;
    00262     }
    00263
    00264     if (_game_mode == GM_MENU) return ES_NOT_HANDLED;
    00265
    00266     switch (keycode) {
    00267       case 'C':
    00268       case 'Z': {
    00269         Point pt = GetTileBelowCursor();
    00270         if (pt.x != -1) {
    00271           if (keycode == 'Z') MaxZoomInOut(ZOOM_IN, this);
    00272           ScrollMainWindowTo(pt.x, pt.y);
    00273         }
    00274         break;
    00275       }
    00276
    00277       case WKC_ESC: ResetObjectToPlace(); break;
    00278       case WKC_DELETE: DeleteNonVitalWindows(); break;
    00279       case WKC_DELETE | WKC_SHIFT: DeleteAllNonVitalWindows(); break;
    00280       case 'R' | WKC_CTRL: MarkWholeScreenDirty(); break;
    00281
    00282 #if defined(_DEBUG)
    00283       case '0' | WKC_ALT: // Crash the game
    00284         *(byte*)0 = 0;
    00285         break;
    00286
    00287       case '1' | WKC_ALT: // Gimme money
    00288         /* Server can not cheat in advertise mode either! */
    00289 #ifdef ENABLE_NETWORK
    00290         if (!_networking || !_network_server || !_settings_client.network.server_advertise)
    00291 #endif /* ENABLE_NETWORK */
    00292           DoCommandP(0, 10000000, 0, CMD_MONEY_CHEAT);
    00293         break;
    00294
    00295       case '2' | WKC_ALT: // Update the coordinates of all station signs
    00296         UpdateAllVirtCoords();
    00297         break;
    00298 #endif
    00299
    00300       case '1' | WKC_CTRL:
    00301       case '2' | WKC_CTRL:
    00302       case '3' | WKC_CTRL:
    00303       case '4' | WKC_CTRL:
    00304       case '5' | WKC_CTRL:
    00305       case '6' | WKC_CTRL:
    00306       case '7' | WKC_CTRL:
    00307       case '8' | WKC_CTRL:
    00308       case '9' | WKC_CTRL:
    00309         /* Transparency toggle hot keys */
    00310         ToggleTransparency((TransparencyOption)(keycode - ('1' | WKC_CTRL)));
    00311         MarkWholeScreenDirty();
    00312         break;
    00313
    00314       case '1' | WKC_CTRL | WKC_SHIFT:
    00315       case '2' | WKC_CTRL | WKC_SHIFT:
    00316       case '3' | WKC_CTRL | WKC_SHIFT:
    00317       case '4' | WKC_CTRL | WKC_SHIFT:
    00318       case '5' | WKC_CTRL | WKC_SHIFT:
    00319       case '6' | WKC_CTRL | WKC_SHIFT:
    00320       case '7' | WKC_CTRL | WKC_SHIFT:
    00321       case '8' | WKC_CTRL | WKC_SHIFT:
    00322         /* Invisibility toggle hot keys */
    00323         ToggleInvisibilityWithTransparency((TransparencyOption)(keycode - ('1' | WKC_CTRL | WKC_SHIFT)));
    00324         MarkWholeScreenDirty();
    00325         break;
    00326
    00327       case 'X' | WKC_CTRL:
    00328         ShowTransparencyToolbar();
    00329         break;
    00330
    00331       case 'X':
    00332         ResetRestoreAllTransparency();
    00333         break;
    00334
    00335 #ifdef ENABLE_NETWORK
    00336       case WKC_RETURN: case 'T': // smart chat; send to team if any, otherwise to all
    00337         if (_networking) {
    00338           const NetworkClientInfo *cio = NetworkFindClientInfoFromClientID(_network_own_client_id);
    00339           if (cio == NULL) break;
    00340
    00341           ShowNetworkChatQueryWindow(NetworkClientPreferTeamChat(cio) ? DESTTYPE_TEAM : DESTTYPE_BROADCAST, cio->client_playas);
    00342         }
    00343         break;
    00344
    00345       case WKC_SHIFT | WKC_RETURN: case WKC_SHIFT | 'T': // send text message to all clients
    00346         if (_networking) ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST, 0);
    00347         break;
    00348
    00349       case WKC_CTRL | WKC_RETURN: case WKC_CTRL | 'T': // send text to all team mates
    00350         if (_networking) {
    00351           const NetworkClientInfo *cio = NetworkFindClientInfoFromClientID(_network_own_client_id);
    00352           if (cio == NULL) break;
    00353
    00354           ShowNetworkChatQueryWindow(DESTTYPE_TEAM, cio->client_playas);
    00355         }
    00356         break;
    00357 #endif
    00358
    00359       default: return ES_NOT_HANDLED;
    00360     }
    00361     return ES_HANDLED;
    00362   }
    00363
    00364   virtual void OnScroll(Point delta)
    00365   {
    00366     ViewPort *vp = IsPtInWindowViewport(this, _cursor.pos.x, _cursor.pos.y);
    00367
    00368     if (vp == NULL) {
    00369       _cursor.fix_at = false;
    00370       _scrolling_viewport = false;
    00371     }
    00372
    00373     this->viewport->scrollpos_x += ScaleByZoom(delta.x, vp->zoom);
    00374     this->viewport->scrollpos_y += ScaleByZoom(delta.y, vp->zoom);
    00375     this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
    00376     this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
    00377   };
    00378
    00379   virtual void OnMouseWheel(int wheel)
    00380   {
    00381     ZoomInOrOutToCursorWindow(wheel < 0, this);
    00382   }
    00383
    00384   virtual void OnResize()
    00385   {
    00386     if (this->viewport != NULL) {
    00387       NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(MW_VIEWPORT);
    00388       nvp->UpdateViewportCoordinates(this);
    00389     }
    00390   }
    00391
    00392   virtual void OnInvalidateData(int data)
    00393   {
    00394     /* Forward the message to the appropiate toolbar (ingame or scenario editor) */
    00395     InvalidateWindowData(WC_MAIN_TOOLBAR, 0, data);
    00396   }
    00397 };
    00398
    00399
    00400 void ShowSelectGameWindow();
    00401
    00402 void SetupColoursAndInitialWindow()
    00403 {
    00404   for (uint i = 0; i != 16; i++) {
    00405     const byte *b = GetNonSprite(PALETTE_RECOLOUR_START + i, ST_RECOLOUR);
    00406
    00407     assert(b);
    00408     memcpy(_colour_gradient[i], b + 0xC6, sizeof(_colour_gradient[i]));
    00409   }
    00410
    00411   new MainWindow;
    00412
    00413   /* XXX: these are not done */
    00414   switch (_game_mode) {
    00415     default: NOT_REACHED();
    00416     case GM_MENU:
    00417       ShowSelectGameWindow();
    00418       break;
    00419
    00420     case GM_NORMAL:
    00421     case GM_EDITOR:
    00422       ShowVitalWindows();
    00423       break;
    00424   }
    00425 }
    00426
    00427 void ShowVitalWindows()
    00428 {
    00429   AllocateToolbar();
    00430
    00431   /* Status bad only for normal games */
    00432   if (_game_mode == GM_EDITOR) return;
    00433
    00434   ShowStatusBar();
    00435 }
    00436
    00441 void GameSizeChanged()
    00442 {
    00443   _cur_resolution.width  = _screen.width;
    00444   _cur_resolution.height = _screen.height;
    00445   ScreenSizeChanged();
    00446   RelocateAllWindows(_screen.width, _screen.height);
    00447   MarkWholeScreenDirty();
    00448 }