# main_gui.cpp

Go to the documentation of this file.

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