4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file main_gui.cpp Handling of the main viewport. */
14 #include "spritecache.h"
15 #include "window_gui.h"
16 #include "window_func.h"
17 #include "textbuf_gui.h"
18 #include "viewport_func.h"
19 #include "command_func.h"
20 #include "console_gui.h"
22 #include "transparency_gui.h"
24 #include "sound_func.h"
25 #include "transparency.h"
26 #include "strings_func.h"
27 #include "zoom_func.h"
28 #include "company_base.h"
29 #include "company_func.h"
30 #include "toolbar_gui.h"
31 #include "statusbar_gui.h"
32 #include "linkgraph/linkgraph_gui.h"
33 #include "tilehighlight_func.h"
36 #include "saveload/saveload.h"
38 #include "widgets/main_widget.h"
40 #include "network/network.h"
41 #include "network/network_func.h"
42 #include "network/network_gui.h"
43 #include "network/network_base.h"
45 #include "table/sprites.h"
46 #include "table/strings.h"
48 #include "safeguards.h"
50 static int _rename_id
= 1;
51 static int _rename_what
= -1;
53 void CcGiveMoney(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
56 if (result
.Failed() || !_settings_game
.economy
.give_money
) return;
58 /* Inform the company of the action of one of its clients (controllers). */
61 GetString(msg
, STR_COMPANY_NAME
, lastof(msg
));
63 if (!_network_server
) {
64 NetworkClientSendChat(NETWORK_ACTION_GIVE_MONEY
, DESTTYPE_TEAM
, p2
, msg
, p1
);
66 NetworkServerSendChat(NETWORK_ACTION_GIVE_MONEY
, DESTTYPE_TEAM
, p2
, msg
, CLIENT_ID_SERVER
, p1
);
68 #endif /* ENABLE_NETWORK */
71 void HandleOnEditText(const char *str
)
73 switch (_rename_what
) {
75 case 3: { // Give money, you can only give money in excess of loan
76 const Company
*c
= Company::GetIfValid(_local_company
);
78 Money money
= min(c
->money
- c
->current_loan
, (Money
)(atoi(str
) / _currency
->rate
));
80 uint32 money_c
= Clamp(ClampToI32(money
), 0, 20000000); // Clamp between 20 million and 0
82 /* Give 'id' the money, and subtract it from ourself */
83 DoCommandP(0, money_c
, _rename_id
, CMD_GIVE_MONEY
| CMD_MSG(STR_ERROR_INSUFFICIENT_FUNDS
), CcGiveMoney
, str
);
86 #endif /* ENABLE_NETWORK */
87 default: NOT_REACHED();
90 _rename_id
= _rename_what
= -1;
94 * This code is shared for the majority of the pushbuttons.
95 * Handles e.g. the pressing of a button (to build things), playing of click sound and sets certain parameters
97 * @param w Window which called the function
98 * @param widget ID of the widget (=button) that called this function
99 * @param cursor How should the cursor image change? E.g. cursor with depot image in it
100 * @param mode Tile highlighting mode, e.g. drawing a rectangle or a dot on the ground
101 * @return true if the button is clicked, false if it's unclicked
103 bool HandlePlacePushButton(Window
*w
, int widget
, CursorID cursor
, HighLightStyle mode
)
105 if (w
->IsWidgetDisabled(widget
)) return false;
107 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
110 if (w
->IsWidgetLowered(widget
)) {
111 ResetObjectToPlace();
115 SetObjectToPlace(cursor
, PAL_NONE
, mode
, w
->window_class
, w
->window_number
);
116 w
->LowerWidget(widget
);
121 void CcPlaySound_EXPLOSION(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
123 if (result
.Succeeded() && _settings_client
.sound
.confirm
) SndPlayTileFx(SND_12_EXPLOSION
, tile
);
126 #ifdef ENABLE_NETWORK
127 void ShowNetworkGiveMoneyWindow(CompanyID company
)
129 _rename_id
= company
;
131 ShowQueryString(STR_EMPTY
, STR_NETWORK_GIVE_MONEY_CAPTION
, 30, NULL
, CS_NUMERAL
, QSF_NONE
);
133 #endif /* ENABLE_NETWORK */
137 * Zooms a viewport in a window in or out.
138 * @param how Zooming direction.
139 * @param w Window owning the viewport.
140 * @return Returns \c true if zooming step could be done, \c false if further zooming is not possible.
141 * @note No button handling or what so ever is done.
143 bool DoZoomInOutWindow(ZoomStateChange how
, Window
*w
)
152 /* On initialisation of the viewport we don't do anything. */
156 if (vp
->zoom
<= _settings_client
.gui
.zoom_min
) return false;
157 vp
->zoom
= (ZoomLevel
)((int)vp
->zoom
- 1);
158 vp
->virtual_width
>>= 1;
159 vp
->virtual_height
>>= 1;
161 w
->viewport
->scrollpos_x
+= vp
->virtual_width
>> 1;
162 w
->viewport
->scrollpos_y
+= vp
->virtual_height
>> 1;
163 w
->viewport
->dest_scrollpos_x
= w
->viewport
->scrollpos_x
;
164 w
->viewport
->dest_scrollpos_y
= w
->viewport
->scrollpos_y
;
165 w
->viewport
->follow_vehicle
= INVALID_VEHICLE
;
168 if (vp
->zoom
>= _settings_client
.gui
.zoom_max
) return false;
169 vp
->zoom
= (ZoomLevel
)((int)vp
->zoom
+ 1);
171 w
->viewport
->scrollpos_x
-= vp
->virtual_width
>> 1;
172 w
->viewport
->scrollpos_y
-= vp
->virtual_height
>> 1;
173 w
->viewport
->dest_scrollpos_x
= w
->viewport
->scrollpos_x
;
174 w
->viewport
->dest_scrollpos_y
= w
->viewport
->scrollpos_y
;
176 vp
->virtual_width
<<= 1;
177 vp
->virtual_height
<<= 1;
178 w
->viewport
->follow_vehicle
= INVALID_VEHICLE
;
181 if (vp
!= NULL
) { // the vp can be null when how == ZOOM_NONE
182 vp
->virtual_left
= w
->viewport
->scrollpos_x
;
183 vp
->virtual_top
= w
->viewport
->scrollpos_y
;
185 /* Update the windows that have zoom-buttons to perhaps disable their buttons */
190 void ZoomInOrOutToCursorWindow(bool in
, Window
*w
)
194 if (_game_mode
!= GM_MENU
) {
195 ViewPort
*vp
= w
->viewport
;
196 if ((in
&& vp
->zoom
<= _settings_client
.gui
.zoom_min
) || (!in
&& vp
->zoom
>= _settings_client
.gui
.zoom_max
)) return;
198 Point pt
= GetTileZoomCenterWindow(in
, w
);
200 ScrollWindowTo(pt
.x
, pt
.y
, -1, w
, true);
202 DoZoomInOutWindow(in
? ZOOM_IN
: ZOOM_OUT
, w
);
207 static const struct NWidgetPart _nested_main_window_widgets
[] = {
208 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_M_VIEWPORT
), SetResize(1, 1),
219 GHK_RESET_OBJECT_TO_PLACE
,
221 GHK_DELETE_NONVITAL_WINDOWS
,
226 GHK_TOGGLE_TRANSPARENCY
,
227 GHK_TOGGLE_INVISIBILITY
= GHK_TOGGLE_TRANSPARENCY
+ 9,
228 GHK_TRANSPARENCY_TOOLBAR
= GHK_TOGGLE_INVISIBILITY
+ 8,
236 struct MainWindow
: Window
240 static const uint LINKGRAPH_REFRESH_PERIOD
= 0xff;
241 static const uint LINKGRAPH_DELAY
= 0xf;
243 MainWindow(WindowDesc
*desc
) : Window(desc
)
246 CLRBITS(this->flags
, WF_WHITE_BORDER
);
247 ResizeWindow(this, _screen
.width
, _screen
.height
);
249 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_M_VIEWPORT
);
250 nvp
->InitializeViewport(this, TileXY(32, 32), ZOOM_LVL_VIEWPORT
);
252 this->viewport
->overlay
= new LinkGraphOverlay(this, WID_M_VIEWPORT
, 0, 0, 3);
253 this->refresh
= LINKGRAPH_DELAY
;
256 virtual void OnTick()
258 if (--this->refresh
> 0) return;
260 this->refresh
= LINKGRAPH_REFRESH_PERIOD
;
262 if (this->viewport
->overlay
->GetCargoMask() == 0 ||
263 this->viewport
->overlay
->GetCompanyMask() == 0) {
267 this->viewport
->overlay
->RebuildCache();
268 this->GetWidget
<NWidgetBase
>(WID_M_VIEWPORT
)->SetDirty(this);
271 virtual void OnPaint()
274 if (_game_mode
== GM_MENU
) {
275 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
};
276 static const uint LETTER_SPACING
= 10;
277 int name_width
= (lengthof(title_sprites
) - 1) * LETTER_SPACING
;
279 for (uint i
= 0; i
< lengthof(title_sprites
); i
++) {
280 name_width
+= GetSpriteSize(title_sprites
[i
]).width
;
282 int off_x
= (this->width
- name_width
) / 2;
284 for (uint i
= 0; i
< lengthof(title_sprites
); i
++) {
285 DrawSprite(title_sprites
[i
], PAL_NONE
, off_x
, 50);
286 off_x
+= GetSpriteSize(title_sprites
[i
]).width
+ LETTER_SPACING
;
291 virtual EventState
OnHotkey(int hotkey
)
293 if (hotkey
== GHK_QUIT
) {
294 HandleExitGameRequest();
298 /* Disable all key shortcuts, except quit shortcuts when
299 * generating the world, otherwise they create threading
300 * problem during the generating, resulting in random
301 * assertions that are hard to trigger and debug */
302 if (HasModalProgress()) return ES_NOT_HANDLED
;
306 /* No point returning from the main menu to itself */
307 if (_game_mode
== GM_MENU
) return ES_HANDLED
;
308 if (_settings_client
.gui
.autosave_on_exit
) {
310 _switch_mode
= SM_MENU
;
320 case GHK_BOUNDING_BOXES
:
321 ToggleBoundingBoxes();
324 case GHK_DIRTY_BLOCKS
:
329 if (_game_mode
== GM_MENU
) return ES_NOT_HANDLED
;
333 case GHK_CENTER_ZOOM
: {
334 Point pt
= GetTileBelowCursor();
336 bool instant
= (hotkey
== GHK_CENTER_ZOOM
&& this->viewport
->zoom
!= _settings_client
.gui
.zoom_min
);
337 if (hotkey
== GHK_CENTER_ZOOM
) MaxZoomInOut(ZOOM_IN
, this);
338 ScrollMainWindowTo(pt
.x
, pt
.y
, -1, instant
);
343 case GHK_RESET_OBJECT_TO_PLACE
: ResetObjectToPlace(); break;
344 case GHK_DELETE_WINDOWS
: DeleteNonVitalWindows(); break;
345 case GHK_DELETE_NONVITAL_WINDOWS
: DeleteAllNonVitalWindows(); break;
346 case GHK_REFRESH_SCREEN
: MarkWholeScreenDirty(); break;
348 case GHK_CRASH
: // Crash the game
349 *(volatile byte
*)0 = 0;
352 case GHK_MONEY
: // Gimme money
353 /* You can only cheat for money in single player. */
354 if (!_networking
) DoCommandP(0, 10000000, 0, CMD_MONEY_CHEAT
);
357 case GHK_UPDATE_COORDS
: // Update the coordinates of all station signs
358 UpdateAllVirtCoords();
361 case GHK_TOGGLE_TRANSPARENCY
:
362 case GHK_TOGGLE_TRANSPARENCY
+ 1:
363 case GHK_TOGGLE_TRANSPARENCY
+ 2:
364 case GHK_TOGGLE_TRANSPARENCY
+ 3:
365 case GHK_TOGGLE_TRANSPARENCY
+ 4:
366 case GHK_TOGGLE_TRANSPARENCY
+ 5:
367 case GHK_TOGGLE_TRANSPARENCY
+ 6:
368 case GHK_TOGGLE_TRANSPARENCY
+ 7:
369 case GHK_TOGGLE_TRANSPARENCY
+ 8:
370 /* Transparency toggle hot keys */
371 ToggleTransparency((TransparencyOption
)(hotkey
- GHK_TOGGLE_TRANSPARENCY
));
372 MarkWholeScreenDirty();
375 case GHK_TOGGLE_INVISIBILITY
:
376 case GHK_TOGGLE_INVISIBILITY
+ 1:
377 case GHK_TOGGLE_INVISIBILITY
+ 2:
378 case GHK_TOGGLE_INVISIBILITY
+ 3:
379 case GHK_TOGGLE_INVISIBILITY
+ 4:
380 case GHK_TOGGLE_INVISIBILITY
+ 5:
381 case GHK_TOGGLE_INVISIBILITY
+ 6:
382 case GHK_TOGGLE_INVISIBILITY
+ 7:
383 /* Invisibility toggle hot keys */
384 ToggleInvisibilityWithTransparency((TransparencyOption
)(hotkey
- GHK_TOGGLE_INVISIBILITY
));
385 MarkWholeScreenDirty();
388 case GHK_TRANSPARENCY_TOOLBAR
:
389 ShowTransparencyToolbar();
392 case GHK_TRANSPARANCY
:
393 ResetRestoreAllTransparency();
396 #ifdef ENABLE_NETWORK
397 case GHK_CHAT
: // smart chat; send to team if any, otherwise to all
399 const NetworkClientInfo
*cio
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
400 if (cio
== NULL
) break;
402 ShowNetworkChatQueryWindow(NetworkClientPreferTeamChat(cio
) ? DESTTYPE_TEAM
: DESTTYPE_BROADCAST
, cio
->client_playas
);
406 case GHK_CHAT_ALL
: // send text message to all clients
407 if (_networking
) ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST
, 0);
410 case GHK_CHAT_COMPANY
: // send text to all team mates
412 const NetworkClientInfo
*cio
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
413 if (cio
== NULL
) break;
415 ShowNetworkChatQueryWindow(DESTTYPE_TEAM
, cio
->client_playas
);
419 case GHK_CHAT_SERVER
: // send text to the server
420 if (_networking
&& !_network_server
) {
421 ShowNetworkChatQueryWindow(DESTTYPE_CLIENT
, CLIENT_ID_SERVER
);
426 default: return ES_NOT_HANDLED
;
431 virtual void OnScroll(Point delta
)
433 this->viewport
->scrollpos_x
+= ScaleByZoom(delta
.x
, this->viewport
->zoom
);
434 this->viewport
->scrollpos_y
+= ScaleByZoom(delta
.y
, this->viewport
->zoom
);
435 this->viewport
->dest_scrollpos_x
= this->viewport
->scrollpos_x
;
436 this->viewport
->dest_scrollpos_y
= this->viewport
->scrollpos_y
;
437 this->refresh
= LINKGRAPH_DELAY
;
440 virtual void OnMouseWheel(int wheel
)
442 if (_settings_client
.gui
.scrollwheel_scrolling
== 0) {
443 ZoomInOrOutToCursorWindow(wheel
< 0, this);
447 virtual void OnResize()
449 if (this->viewport
!= NULL
) {
450 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_M_VIEWPORT
);
451 nvp
->UpdateViewportCoordinates(this);
452 this->refresh
= LINKGRAPH_DELAY
;
457 * Some data on this window has become invalid.
458 * @param data Information about the changed data.
459 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
461 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
463 if (!gui_scope
) return;
464 /* Forward the message to the appropriate toolbar (ingame or scenario editor) */
465 InvalidateWindowData(WC_MAIN_TOOLBAR
, 0, data
, true);
468 static HotkeyList hotkeys
;
471 const uint16 _ghk_quit_keys
[] = {'Q' | WKC_CTRL
, 'Q' | WKC_META
, 0};
472 const uint16 _ghk_abandon_keys
[] = {'W' | WKC_CTRL
, 'W' | WKC_META
, 0};
473 const uint16 _ghk_chat_keys
[] = {WKC_RETURN
, 'T', 0};
474 const uint16 _ghk_chat_all_keys
[] = {WKC_SHIFT
| WKC_RETURN
, WKC_SHIFT
| 'T', 0};
475 const uint16 _ghk_chat_company_keys
[] = {WKC_CTRL
| WKC_RETURN
, WKC_CTRL
| 'T', 0};
476 const uint16 _ghk_chat_server_keys
[] = {WKC_CTRL
| WKC_SHIFT
| WKC_RETURN
, WKC_CTRL
| WKC_SHIFT
| 'T', 0};
478 static Hotkey global_hotkeys
[] = {
479 Hotkey(_ghk_quit_keys
, "quit", GHK_QUIT
),
480 Hotkey(_ghk_abandon_keys
, "abandon", GHK_ABANDON
),
481 Hotkey(WKC_BACKQUOTE
, "console", GHK_CONSOLE
),
482 Hotkey('B' | WKC_CTRL
, "bounding_boxes", GHK_BOUNDING_BOXES
),
483 Hotkey('I' | WKC_CTRL
, "dirty_blocks", GHK_DIRTY_BLOCKS
),
484 Hotkey('C', "center", GHK_CENTER
),
485 Hotkey('Z', "center_zoom", GHK_CENTER_ZOOM
),
486 Hotkey(WKC_ESC
, "reset_object_to_place", GHK_RESET_OBJECT_TO_PLACE
),
487 Hotkey(WKC_DELETE
, "delete_windows", GHK_DELETE_WINDOWS
),
488 Hotkey(WKC_DELETE
| WKC_SHIFT
, "delete_all_windows", GHK_DELETE_NONVITAL_WINDOWS
),
489 Hotkey('R' | WKC_CTRL
, "refresh_screen", GHK_REFRESH_SCREEN
),
491 Hotkey('0' | WKC_ALT
, "crash_game", GHK_CRASH
),
492 Hotkey('1' | WKC_ALT
, "money", GHK_MONEY
),
493 Hotkey('2' | WKC_ALT
, "update_coordinates", GHK_UPDATE_COORDS
),
495 Hotkey('1' | WKC_CTRL
, "transparency_signs", GHK_TOGGLE_TRANSPARENCY
),
496 Hotkey('2' | WKC_CTRL
, "transparency_trees", GHK_TOGGLE_TRANSPARENCY
+ 1),
497 Hotkey('3' | WKC_CTRL
, "transparency_houses", GHK_TOGGLE_TRANSPARENCY
+ 2),
498 Hotkey('4' | WKC_CTRL
, "transparency_industries", GHK_TOGGLE_TRANSPARENCY
+ 3),
499 Hotkey('5' | WKC_CTRL
, "transparency_buildings", GHK_TOGGLE_TRANSPARENCY
+ 4),
500 Hotkey('6' | WKC_CTRL
, "transparency_bridges", GHK_TOGGLE_TRANSPARENCY
+ 5),
501 Hotkey('7' | WKC_CTRL
, "transparency_structures", GHK_TOGGLE_TRANSPARENCY
+ 6),
502 Hotkey('8' | WKC_CTRL
, "transparency_catenary", GHK_TOGGLE_TRANSPARENCY
+ 7),
503 Hotkey('9' | WKC_CTRL
, "transparency_loading", GHK_TOGGLE_TRANSPARENCY
+ 8),
504 Hotkey('1' | WKC_CTRL
| WKC_SHIFT
, "invisibility_signs", GHK_TOGGLE_INVISIBILITY
),
505 Hotkey('2' | WKC_CTRL
| WKC_SHIFT
, "invisibility_trees", GHK_TOGGLE_INVISIBILITY
+ 1),
506 Hotkey('3' | WKC_CTRL
| WKC_SHIFT
, "invisibility_houses", GHK_TOGGLE_INVISIBILITY
+ 2),
507 Hotkey('4' | WKC_CTRL
| WKC_SHIFT
, "invisibility_industries", GHK_TOGGLE_INVISIBILITY
+ 3),
508 Hotkey('5' | WKC_CTRL
| WKC_SHIFT
, "invisibility_buildings", GHK_TOGGLE_INVISIBILITY
+ 4),
509 Hotkey('6' | WKC_CTRL
| WKC_SHIFT
, "invisibility_bridges", GHK_TOGGLE_INVISIBILITY
+ 5),
510 Hotkey('7' | WKC_CTRL
| WKC_SHIFT
, "invisibility_structures", GHK_TOGGLE_INVISIBILITY
+ 6),
511 Hotkey('8' | WKC_CTRL
| WKC_SHIFT
, "invisibility_catenary", GHK_TOGGLE_INVISIBILITY
+ 7),
512 Hotkey('X' | WKC_CTRL
, "transparency_toolbar", GHK_TRANSPARENCY_TOOLBAR
),
513 Hotkey('X', "toggle_transparency", GHK_TRANSPARANCY
),
514 #ifdef ENABLE_NETWORK
515 Hotkey(_ghk_chat_keys
, "chat", GHK_CHAT
),
516 Hotkey(_ghk_chat_all_keys
, "chat_all", GHK_CHAT_ALL
),
517 Hotkey(_ghk_chat_company_keys
, "chat_company", GHK_CHAT_COMPANY
),
518 Hotkey(_ghk_chat_server_keys
, "chat_server", GHK_CHAT_SERVER
),
522 HotkeyList
MainWindow::hotkeys("global", global_hotkeys
);
524 static WindowDesc
_main_window_desc(
525 WDP_MANUAL
, NULL
, 0, 0,
526 WC_MAIN_WINDOW
, WC_NONE
,
528 _nested_main_window_widgets
, lengthof(_nested_main_window_widgets
),
533 * Does the given keycode match one of the keycodes bound to 'quit game'?
534 * @param keycode The keycode that was pressed by the user.
535 * @return True iff the keycode matches one of the hotkeys for 'quit'.
537 bool IsQuitKey(uint16 keycode
)
539 int num
= MainWindow::hotkeys
.CheckMatch(keycode
);
540 return num
== GHK_QUIT
;
544 void ShowSelectGameWindow();
547 * Initialise the default colours (remaps and the likes), and load the main windows.
549 void SetupColoursAndInitialWindow()
551 for (uint i
= 0; i
!= 16; i
++) {
552 const byte
*b
= GetNonSprite(PALETTE_RECOLOUR_START
+ i
, ST_RECOLOUR
);
555 memcpy(_colour_gradient
[i
], b
+ 0xC6, sizeof(_colour_gradient
[i
]));
558 new MainWindow(&_main_window_desc
);
560 /* XXX: these are not done */
561 switch (_game_mode
) {
562 default: NOT_REACHED();
564 ShowSelectGameWindow();
575 * Show the vital in-game windows.
577 void ShowVitalWindows()
581 /* Status bad only for normal games */
582 if (_game_mode
== GM_EDITOR
) return;
588 * Size of the application screen changed.
589 * Adapt the game screen-size, re-allocate the open windows, and repaint everything
591 void GameSizeChanged()
593 _cur_resolution
.width
= _screen
.width
;
594 _cur_resolution
.height
= _screen
.height
;
596 RelocateAllWindows(_screen
.width
, _screen
.height
);
597 MarkWholeScreenDirty();