main_gui.cpp

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

Generated on Wed Dec 23 20:12:48 2009 for OpenTTD by  doxygen 1.5.6