2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
8 /** @file main_gui.cpp Handling of the main viewport. */
12 #include "spritecache.h"
13 #include "window_gui.h"
14 #include "window_func.h"
15 #include "textbuf_gui.h"
16 #include "viewport_func.h"
17 #include "command_func.h"
18 #include "console_gui.h"
20 #include "transparency_gui.h"
22 #include "sound_func.h"
23 #include "transparency.h"
24 #include "strings_func.h"
25 #include "zoom_func.h"
26 #include "company_base.h"
27 #include "company_func.h"
28 #include "toolbar_gui.h"
29 #include "statusbar_gui.h"
30 #include "linkgraph/linkgraph_gui.h"
31 #include "tilehighlight_func.h"
33 #include "guitimer_func.h"
37 #include "saveload/saveload.h"
39 #include "widgets/main_widget.h"
41 #include "network/network.h"
42 #include "network/network_func.h"
43 #include "network/network_gui.h"
44 #include "network/network_base.h"
46 #include "table/sprites.h"
47 #include "table/strings.h"
49 #include "safeguards.h"
52 * This code is shared for the majority of the pushbuttons.
53 * Handles e.g. the pressing of a button (to build things), playing of click sound and sets certain parameters
55 * @param w Window which called the function
56 * @param widget ID of the widget (=button) that called this function
57 * @param cursor How should the cursor image change? E.g. cursor with depot image in it
58 * @param mode Tile highlighting mode, e.g. drawing a rectangle or a dot on the ground
59 * @return true if the button is clicked, false if it's unclicked
61 bool HandlePlacePushButton(Window
*w
, int widget
, CursorID cursor
, HighLightStyle mode
)
63 if (w
->IsWidgetDisabled(widget
)) return false;
65 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
68 if (w
->IsWidgetLowered(widget
)) {
73 SetObjectToPlace(cursor
, PAL_NONE
, mode
, w
->window_class
, w
->window_number
);
74 w
->LowerWidget(widget
);
79 void CcPlaySound_EXPLOSION(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
)
81 if (result
.Succeeded() && _settings_client
.sound
.confirm
) SndPlayTileFx(SND_12_EXPLOSION
, tile
);
85 * Zooms a viewport in a window in or out.
86 * @param how Zooming direction.
87 * @param w Window owning the viewport.
88 * @return Returns \c true if zooming step could be done, \c false if further zooming is not possible.
89 * @note No button handling or what so ever is done.
91 bool DoZoomInOutWindow(ZoomStateChange how
, Window
*w
)
100 /* On initialisation of the viewport we don't do anything. */
104 if (vp
->zoom
<= _settings_client
.gui
.zoom_min
) return false;
105 vp
->zoom
= (ZoomLevel
)((int)vp
->zoom
- 1);
106 vp
->virtual_width
>>= 1;
107 vp
->virtual_height
>>= 1;
109 w
->viewport
->scrollpos_x
+= vp
->virtual_width
>> 1;
110 w
->viewport
->scrollpos_y
+= vp
->virtual_height
>> 1;
111 w
->viewport
->dest_scrollpos_x
= w
->viewport
->scrollpos_x
;
112 w
->viewport
->dest_scrollpos_y
= w
->viewport
->scrollpos_y
;
113 w
->viewport
->follow_vehicle
= INVALID_VEHICLE
;
116 if (vp
->zoom
>= _settings_client
.gui
.zoom_max
) return false;
117 vp
->zoom
= (ZoomLevel
)((int)vp
->zoom
+ 1);
119 w
->viewport
->scrollpos_x
-= vp
->virtual_width
>> 1;
120 w
->viewport
->scrollpos_y
-= vp
->virtual_height
>> 1;
121 w
->viewport
->dest_scrollpos_x
= w
->viewport
->scrollpos_x
;
122 w
->viewport
->dest_scrollpos_y
= w
->viewport
->scrollpos_y
;
124 vp
->virtual_width
<<= 1;
125 vp
->virtual_height
<<= 1;
126 w
->viewport
->follow_vehicle
= INVALID_VEHICLE
;
129 if (vp
!= nullptr) { // the vp can be null when how == ZOOM_NONE
130 vp
->virtual_left
= w
->viewport
->scrollpos_x
;
131 vp
->virtual_top
= w
->viewport
->scrollpos_y
;
133 /* Update the windows that have zoom-buttons to perhaps disable their buttons */
138 void ZoomInOrOutToCursorWindow(bool in
, Window
*w
)
140 assert(w
!= nullptr);
142 if (_game_mode
!= GM_MENU
) {
143 Viewport
*vp
= w
->viewport
;
144 if ((in
&& vp
->zoom
<= _settings_client
.gui
.zoom_min
) || (!in
&& vp
->zoom
>= _settings_client
.gui
.zoom_max
)) return;
146 Point pt
= GetTileZoomCenterWindow(in
, w
);
148 ScrollWindowTo(pt
.x
, pt
.y
, -1, w
, true);
150 DoZoomInOutWindow(in
? ZOOM_IN
: ZOOM_OUT
, w
);
155 void FixTitleGameZoom()
157 if (_game_mode
!= GM_MENU
) return;
159 Viewport
*vp
= FindWindowByClass(WC_MAIN_WINDOW
)->viewport
;
160 vp
->zoom
= _gui_zoom
;
161 vp
->virtual_width
= ScaleByZoom(vp
->width
, vp
->zoom
);
162 vp
->virtual_height
= ScaleByZoom(vp
->height
, vp
->zoom
);
165 static const struct NWidgetPart _nested_main_window_widgets
[] = {
166 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_M_VIEWPORT
), SetResize(1, 1),
177 GHK_RESET_OBJECT_TO_PLACE
,
179 GHK_DELETE_NONVITAL_WINDOWS
,
180 GHK_DELETE_ALL_MESSAGES
,
185 GHK_TOGGLE_TRANSPARENCY
,
186 GHK_TOGGLE_INVISIBILITY
= GHK_TOGGLE_TRANSPARENCY
+ 9,
187 GHK_TRANSPARENCY_TOOLBAR
= GHK_TOGGLE_INVISIBILITY
+ 8,
197 struct MainWindow
: Window
201 /* Refresh times in milliseconds */
202 static const uint LINKGRAPH_REFRESH_PERIOD
= 7650;
203 static const uint LINKGRAPH_DELAY
= 450;
205 MainWindow(WindowDesc
*desc
) : Window(desc
)
208 CLRBITS(this->flags
, WF_WHITE_BORDER
);
209 ResizeWindow(this, _screen
.width
, _screen
.height
);
211 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_M_VIEWPORT
);
212 nvp
->InitializeViewport(this, TileXY(32, 32), ZOOM_LVL_VIEWPORT
);
214 this->viewport
->overlay
= new LinkGraphOverlay(this, WID_M_VIEWPORT
, 0, 0, 3);
215 this->refresh
.SetInterval(LINKGRAPH_DELAY
);
218 void OnRealtimeTick(uint delta_ms
) override
220 if (!this->refresh
.Elapsed(delta_ms
)) return;
222 this->refresh
.SetInterval(LINKGRAPH_REFRESH_PERIOD
);
224 if (this->viewport
->overlay
->GetCargoMask() == 0 ||
225 this->viewport
->overlay
->GetCompanyMask() == 0) {
229 this->viewport
->overlay
->SetDirty();
230 this->GetWidget
<NWidgetBase
>(WID_M_VIEWPORT
)->SetDirty(this);
233 void OnPaint() override
236 if (_game_mode
== GM_MENU
) {
237 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
};
238 static const uint LETTER_SPACING
= 10;
239 int name_width
= (lengthof(title_sprites
) - 1) * LETTER_SPACING
;
241 for (uint i
= 0; i
< lengthof(title_sprites
); i
++) {
242 name_width
+= GetSpriteSize(title_sprites
[i
]).width
;
244 int off_x
= (this->width
- name_width
) / 2;
246 for (uint i
= 0; i
< lengthof(title_sprites
); i
++) {
247 DrawSprite(title_sprites
[i
], PAL_NONE
, off_x
, 50);
248 off_x
+= GetSpriteSize(title_sprites
[i
]).width
+ LETTER_SPACING
;
253 EventState
OnHotkey(int hotkey
) override
255 if (hotkey
== GHK_QUIT
) {
256 HandleExitGameRequest();
260 /* Disable all key shortcuts, except quit shortcuts when
261 * generating the world, otherwise they create threading
262 * problem during the generating, resulting in random
263 * assertions that are hard to trigger and debug */
264 if (HasModalProgress()) return ES_NOT_HANDLED
;
268 /* No point returning from the main menu to itself */
269 if (_game_mode
== GM_MENU
) return ES_HANDLED
;
270 if (_settings_client
.gui
.autosave_on_exit
) {
272 _switch_mode
= SM_MENU
;
282 case GHK_BOUNDING_BOXES
:
283 ToggleBoundingBoxes();
286 case GHK_DIRTY_BLOCKS
:
291 if (_game_mode
== GM_MENU
) return ES_NOT_HANDLED
;
295 case GHK_CENTER_ZOOM
: {
296 Point pt
= GetTileBelowCursor();
298 bool instant
= (hotkey
== GHK_CENTER_ZOOM
&& this->viewport
->zoom
!= _settings_client
.gui
.zoom_min
);
299 if (hotkey
== GHK_CENTER_ZOOM
) MaxZoomInOut(ZOOM_IN
, this);
300 ScrollMainWindowTo(pt
.x
, pt
.y
, -1, instant
);
305 case GHK_RESET_OBJECT_TO_PLACE
: ResetObjectToPlace(); break;
306 case GHK_DELETE_WINDOWS
: CloseNonVitalWindows(); break;
307 case GHK_DELETE_NONVITAL_WINDOWS
: CloseAllNonVitalWindows(); break;
308 case GHK_DELETE_ALL_MESSAGES
: DeleteAllMessages(); break;
309 case GHK_REFRESH_SCREEN
: MarkWholeScreenDirty(); break;
311 case GHK_CRASH
: // Crash the game
312 *(volatile byte
*)nullptr = 0;
315 case GHK_MONEY
: // Gimme money
316 /* You can only cheat for money in singleplayer mode. */
317 if (!_networking
) DoCommandP(0, 10000000, 0, CMD_MONEY_CHEAT
);
320 case GHK_UPDATE_COORDS
: // Update the coordinates of all station signs
321 UpdateAllVirtCoords();
324 case GHK_TOGGLE_TRANSPARENCY
:
325 case GHK_TOGGLE_TRANSPARENCY
+ 1:
326 case GHK_TOGGLE_TRANSPARENCY
+ 2:
327 case GHK_TOGGLE_TRANSPARENCY
+ 3:
328 case GHK_TOGGLE_TRANSPARENCY
+ 4:
329 case GHK_TOGGLE_TRANSPARENCY
+ 5:
330 case GHK_TOGGLE_TRANSPARENCY
+ 6:
331 case GHK_TOGGLE_TRANSPARENCY
+ 7:
332 case GHK_TOGGLE_TRANSPARENCY
+ 8:
333 /* Transparency toggle hot keys */
334 ToggleTransparency((TransparencyOption
)(hotkey
- GHK_TOGGLE_TRANSPARENCY
));
335 MarkWholeScreenDirty();
338 case GHK_TOGGLE_INVISIBILITY
:
339 case GHK_TOGGLE_INVISIBILITY
+ 1:
340 case GHK_TOGGLE_INVISIBILITY
+ 2:
341 case GHK_TOGGLE_INVISIBILITY
+ 3:
342 case GHK_TOGGLE_INVISIBILITY
+ 4:
343 case GHK_TOGGLE_INVISIBILITY
+ 5:
344 case GHK_TOGGLE_INVISIBILITY
+ 6:
345 case GHK_TOGGLE_INVISIBILITY
+ 7:
346 /* Invisibility toggle hot keys */
347 ToggleInvisibilityWithTransparency((TransparencyOption
)(hotkey
- GHK_TOGGLE_INVISIBILITY
));
348 MarkWholeScreenDirty();
351 case GHK_TRANSPARENCY_TOOLBAR
:
352 ShowTransparencyToolbar();
355 case GHK_TRANSPARANCY
:
356 ResetRestoreAllTransparency();
359 case GHK_CHAT
: // smart chat; send to team if any, otherwise to all
361 const NetworkClientInfo
*cio
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
362 if (cio
== nullptr) break;
364 ShowNetworkChatQueryWindow(NetworkClientPreferTeamChat(cio
) ? DESTTYPE_TEAM
: DESTTYPE_BROADCAST
, cio
->client_playas
);
368 case GHK_CHAT_ALL
: // send text message to all clients
369 if (_networking
) ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST
, 0);
372 case GHK_CHAT_COMPANY
: // send text to all team mates
374 const NetworkClientInfo
*cio
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
375 if (cio
== nullptr) break;
377 ShowNetworkChatQueryWindow(DESTTYPE_TEAM
, cio
->client_playas
);
381 case GHK_CHAT_SERVER
: // send text to the server
382 if (_networking
&& !_network_server
) {
383 ShowNetworkChatQueryWindow(DESTTYPE_CLIENT
, CLIENT_ID_SERVER
);
387 case GHK_CLOSE_NEWS
: // close active news window
388 if (!HideActiveNewsMessage()) return ES_NOT_HANDLED
;
391 case GHK_CLOSE_ERROR
: // close active error window
392 if (!HideActiveErrorMessage()) return ES_NOT_HANDLED
;
395 default: return ES_NOT_HANDLED
;
400 void OnScroll(Point delta
) override
402 this->viewport
->scrollpos_x
+= ScaleByZoom(delta
.x
, this->viewport
->zoom
);
403 this->viewport
->scrollpos_y
+= ScaleByZoom(delta
.y
, this->viewport
->zoom
);
404 this->viewport
->dest_scrollpos_x
= this->viewport
->scrollpos_x
;
405 this->viewport
->dest_scrollpos_y
= this->viewport
->scrollpos_y
;
406 this->refresh
.SetInterval(LINKGRAPH_DELAY
);
409 void OnMouseWheel(int wheel
) override
411 if (_settings_client
.gui
.scrollwheel_scrolling
!= 2) {
412 ZoomInOrOutToCursorWindow(wheel
< 0, this);
416 void OnResize() override
418 if (this->viewport
!= nullptr) {
419 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_M_VIEWPORT
);
420 nvp
->UpdateViewportCoordinates(this);
421 this->refresh
.SetInterval(LINKGRAPH_DELAY
);
426 * Some data on this window has become invalid.
427 * @param data Information about the changed data.
428 * @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.
430 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
432 if (!gui_scope
) return;
433 /* Forward the message to the appropriate toolbar (ingame or scenario editor) */
434 InvalidateWindowData(WC_MAIN_TOOLBAR
, 0, data
, true);
437 static HotkeyList hotkeys
;
440 const uint16 _ghk_quit_keys
[] = {'Q' | WKC_CTRL
, 'Q' | WKC_META
, 0};
441 const uint16 _ghk_abandon_keys
[] = {'W' | WKC_CTRL
, 'W' | WKC_META
, 0};
442 const uint16 _ghk_chat_keys
[] = {WKC_RETURN
, 'T', 0};
443 const uint16 _ghk_chat_all_keys
[] = {WKC_SHIFT
| WKC_RETURN
, WKC_SHIFT
| 'T', 0};
444 const uint16 _ghk_chat_company_keys
[] = {WKC_CTRL
| WKC_RETURN
, WKC_CTRL
| 'T', 0};
445 const uint16 _ghk_chat_server_keys
[] = {WKC_CTRL
| WKC_SHIFT
| WKC_RETURN
, WKC_CTRL
| WKC_SHIFT
| 'T', 0};
447 static Hotkey global_hotkeys
[] = {
448 Hotkey(_ghk_quit_keys
, "quit", GHK_QUIT
),
449 Hotkey(_ghk_abandon_keys
, "abandon", GHK_ABANDON
),
450 Hotkey(WKC_BACKQUOTE
, "console", GHK_CONSOLE
),
451 Hotkey('B' | WKC_CTRL
, "bounding_boxes", GHK_BOUNDING_BOXES
),
452 Hotkey('I' | WKC_CTRL
, "dirty_blocks", GHK_DIRTY_BLOCKS
),
453 Hotkey('C', "center", GHK_CENTER
),
454 Hotkey('Z', "center_zoom", GHK_CENTER_ZOOM
),
455 Hotkey(WKC_ESC
, "reset_object_to_place", GHK_RESET_OBJECT_TO_PLACE
),
456 Hotkey(WKC_DELETE
, "delete_windows", GHK_DELETE_WINDOWS
),
457 Hotkey(WKC_DELETE
| WKC_SHIFT
, "delete_all_windows", GHK_DELETE_NONVITAL_WINDOWS
),
458 Hotkey(WKC_DELETE
| WKC_CTRL
, "delete_all_messages", GHK_DELETE_ALL_MESSAGES
),
459 Hotkey('R' | WKC_CTRL
, "refresh_screen", GHK_REFRESH_SCREEN
),
461 Hotkey('0' | WKC_ALT
, "crash_game", GHK_CRASH
),
462 Hotkey('1' | WKC_ALT
, "money", GHK_MONEY
),
463 Hotkey('2' | WKC_ALT
, "update_coordinates", GHK_UPDATE_COORDS
),
465 Hotkey('1' | WKC_CTRL
, "transparency_signs", GHK_TOGGLE_TRANSPARENCY
),
466 Hotkey('2' | WKC_CTRL
, "transparency_trees", GHK_TOGGLE_TRANSPARENCY
+ 1),
467 Hotkey('3' | WKC_CTRL
, "transparency_houses", GHK_TOGGLE_TRANSPARENCY
+ 2),
468 Hotkey('4' | WKC_CTRL
, "transparency_industries", GHK_TOGGLE_TRANSPARENCY
+ 3),
469 Hotkey('5' | WKC_CTRL
, "transparency_buildings", GHK_TOGGLE_TRANSPARENCY
+ 4),
470 Hotkey('6' | WKC_CTRL
, "transparency_bridges", GHK_TOGGLE_TRANSPARENCY
+ 5),
471 Hotkey('7' | WKC_CTRL
, "transparency_structures", GHK_TOGGLE_TRANSPARENCY
+ 6),
472 Hotkey('8' | WKC_CTRL
, "transparency_catenary", GHK_TOGGLE_TRANSPARENCY
+ 7),
473 Hotkey('9' | WKC_CTRL
, "transparency_loading", GHK_TOGGLE_TRANSPARENCY
+ 8),
474 Hotkey('1' | WKC_CTRL
| WKC_SHIFT
, "invisibility_signs", GHK_TOGGLE_INVISIBILITY
),
475 Hotkey('2' | WKC_CTRL
| WKC_SHIFT
, "invisibility_trees", GHK_TOGGLE_INVISIBILITY
+ 1),
476 Hotkey('3' | WKC_CTRL
| WKC_SHIFT
, "invisibility_houses", GHK_TOGGLE_INVISIBILITY
+ 2),
477 Hotkey('4' | WKC_CTRL
| WKC_SHIFT
, "invisibility_industries", GHK_TOGGLE_INVISIBILITY
+ 3),
478 Hotkey('5' | WKC_CTRL
| WKC_SHIFT
, "invisibility_buildings", GHK_TOGGLE_INVISIBILITY
+ 4),
479 Hotkey('6' | WKC_CTRL
| WKC_SHIFT
, "invisibility_bridges", GHK_TOGGLE_INVISIBILITY
+ 5),
480 Hotkey('7' | WKC_CTRL
| WKC_SHIFT
, "invisibility_structures", GHK_TOGGLE_INVISIBILITY
+ 6),
481 Hotkey('8' | WKC_CTRL
| WKC_SHIFT
, "invisibility_catenary", GHK_TOGGLE_INVISIBILITY
+ 7),
482 Hotkey('X' | WKC_CTRL
, "transparency_toolbar", GHK_TRANSPARENCY_TOOLBAR
),
483 Hotkey('X', "toggle_transparency", GHK_TRANSPARANCY
),
484 Hotkey(_ghk_chat_keys
, "chat", GHK_CHAT
),
485 Hotkey(_ghk_chat_all_keys
, "chat_all", GHK_CHAT_ALL
),
486 Hotkey(_ghk_chat_company_keys
, "chat_company", GHK_CHAT_COMPANY
),
487 Hotkey(_ghk_chat_server_keys
, "chat_server", GHK_CHAT_SERVER
),
488 Hotkey(WKC_SPACE
, "close_news", GHK_CLOSE_NEWS
),
489 Hotkey(WKC_SPACE
, "close_error", GHK_CLOSE_ERROR
),
492 HotkeyList
MainWindow::hotkeys("global", global_hotkeys
);
494 static WindowDesc
_main_window_desc(
495 WDP_MANUAL
, nullptr, 0, 0,
496 WC_MAIN_WINDOW
, WC_NONE
,
498 _nested_main_window_widgets
, lengthof(_nested_main_window_widgets
),
503 * Does the given keycode match one of the keycodes bound to 'quit game'?
504 * @param keycode The keycode that was pressed by the user.
505 * @return True iff the keycode matches one of the hotkeys for 'quit'.
507 bool IsQuitKey(uint16 keycode
)
509 int num
= MainWindow::hotkeys
.CheckMatch(keycode
);
510 return num
== GHK_QUIT
;
514 void ShowSelectGameWindow();
517 * Initialise the default colours (remaps and the likes), and load the main windows.
519 void SetupColoursAndInitialWindow()
521 for (uint i
= 0; i
!= 16; i
++) {
522 const byte
*b
= GetNonSprite(PALETTE_RECOLOUR_START
+ i
, ST_RECOLOUR
);
525 memcpy(_colour_gradient
[i
], b
+ 0xC6, sizeof(_colour_gradient
[i
]));
528 new MainWindow(&_main_window_desc
);
530 /* XXX: these are not done */
531 switch (_game_mode
) {
532 default: NOT_REACHED();
534 ShowSelectGameWindow();
545 * Show the vital in-game windows.
547 void ShowVitalWindows()
551 /* Status bad only for normal games */
552 if (_game_mode
== GM_EDITOR
) return;
558 * Size of the application screen changed.
559 * Adapt the game screen-size, re-allocate the open windows, and repaint everything
561 void GameSizeChanged()
563 _cur_resolution
.width
= _screen
.width
;
564 _cur_resolution
.height
= _screen
.height
;
566 RelocateAllWindows(_screen
.width
, _screen
.height
);
567 MarkWholeScreenDirty();