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"
36 #include "timer/timer.h"
37 #include "timer/timer_window.h"
39 #include "saveload/saveload.h"
41 #include "widgets/main_widget.h"
43 #include "network/network.h"
44 #include "network/network_func.h"
45 #include "network/network_gui.h"
46 #include "network/network_base.h"
48 #include "table/sprites.h"
49 #include "table/strings.h"
51 #include "safeguards.h"
54 * This code is shared for the majority of the pushbuttons.
55 * Handles e.g. the pressing of a button (to build things), playing of click sound and sets certain parameters
57 * @param w Window which called the function
58 * @param widget ID of the widget (=button) that called this function
59 * @param cursor How should the cursor image change? E.g. cursor with depot image in it
60 * @param mode Tile highlighting mode, e.g. drawing a rectangle or a dot on the ground
61 * @return true if the button is clicked, false if it's unclicked
63 bool HandlePlacePushButton(Window
*w
, WidgetID widget
, CursorID cursor
, HighLightStyle mode
)
65 if (w
->IsWidgetDisabled(widget
)) return false;
67 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
70 if (w
->IsWidgetLowered(widget
)) {
75 SetObjectToPlace(cursor
, PAL_NONE
, mode
, w
->window_class
, w
->window_number
);
76 w
->LowerWidget(widget
);
81 void CcPlaySound_EXPLOSION(Commands
, const CommandCost
&result
, TileIndex tile
)
83 if (result
.Succeeded() && _settings_client
.sound
.confirm
) SndPlayTileFx(SND_12_EXPLOSION
, tile
);
87 * Zooms a viewport in a window in or out.
88 * @param how Zooming direction.
89 * @param w Window owning the viewport.
90 * @return Returns \c true if zooming step could be done, \c false if further zooming is not possible.
91 * @note No button handling or what so ever is done.
93 bool DoZoomInOutWindow(ZoomStateChange how
, Window
*w
)
102 /* On initialisation of the viewport we don't do anything. */
106 if (vp
->zoom
<= _settings_client
.gui
.zoom_min
) return false;
107 vp
->zoom
= (ZoomLevel
)((int)vp
->zoom
- 1);
108 vp
->virtual_width
>>= 1;
109 vp
->virtual_height
>>= 1;
111 w
->viewport
->scrollpos_x
+= vp
->virtual_width
>> 1;
112 w
->viewport
->scrollpos_y
+= vp
->virtual_height
>> 1;
113 w
->viewport
->dest_scrollpos_x
= w
->viewport
->scrollpos_x
;
114 w
->viewport
->dest_scrollpos_y
= w
->viewport
->scrollpos_y
;
117 if (vp
->zoom
>= _settings_client
.gui
.zoom_max
) return false;
118 vp
->zoom
= (ZoomLevel
)((int)vp
->zoom
+ 1);
120 w
->viewport
->scrollpos_x
-= vp
->virtual_width
>> 1;
121 w
->viewport
->scrollpos_y
-= vp
->virtual_height
>> 1;
122 w
->viewport
->dest_scrollpos_x
= w
->viewport
->scrollpos_x
;
123 w
->viewport
->dest_scrollpos_y
= w
->viewport
->scrollpos_y
;
125 vp
->virtual_width
<<= 1;
126 vp
->virtual_height
<<= 1;
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(int zoom_adjust
)
157 if (_game_mode
!= GM_MENU
) return;
159 Viewport
*vp
= GetMainWindow()->viewport
;
161 /* Adjust the zoom in/out.
162 * Can't simply add, since operator+ is not defined on the ZoomLevel type. */
163 vp
->zoom
= _gui_zoom
;
164 while (zoom_adjust
< 0 && vp
->zoom
!= _settings_client
.gui
.zoom_min
) {
168 while (zoom_adjust
> 0 && vp
->zoom
!= _settings_client
.gui
.zoom_max
) {
173 vp
->virtual_width
= ScaleByZoom(vp
->width
, vp
->zoom
);
174 vp
->virtual_height
= ScaleByZoom(vp
->height
, vp
->zoom
);
177 static constexpr NWidgetPart _nested_main_window_widgets
[] = {
178 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_M_VIEWPORT
), SetResize(1, 1),
190 GHK_RESET_OBJECT_TO_PLACE
,
192 GHK_DELETE_NONVITAL_WINDOWS
,
193 GHK_DELETE_ALL_MESSAGES
,
198 GHK_TOGGLE_TRANSPARENCY
,
199 GHK_TOGGLE_INVISIBILITY
= GHK_TOGGLE_TRANSPARENCY
+ 9,
200 GHK_TRANSPARENCY_TOOLBAR
= GHK_TOGGLE_INVISIBILITY
+ 8,
210 struct MainWindow
: Window
212 MainWindow(WindowDesc
&desc
) : Window(desc
)
215 CLRBITS(this->flags
, WF_WHITE_BORDER
);
216 ResizeWindow(this, _screen
.width
, _screen
.height
);
218 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_M_VIEWPORT
);
219 nvp
->InitializeViewport(this, TileXY(32, 32), ScaleZoomGUI(ZOOM_LVL_VIEWPORT
));
221 this->viewport
->overlay
= std::make_shared
<LinkGraphOverlay
>(this, WID_M_VIEWPORT
, 0, 0, 2);
222 this->refresh_timeout
.Reset();
225 /** Refresh the link-graph overlay. */
226 void RefreshLinkGraph()
228 if (this->viewport
->overlay
->GetCargoMask() == 0 ||
229 this->viewport
->overlay
->GetCompanyMask() == 0) {
233 this->viewport
->overlay
->SetDirty();
234 this->GetWidget
<NWidgetBase
>(WID_M_VIEWPORT
)->SetDirty(this);
237 /** Refresh the link-graph overlay on a regular interval. */
238 IntervalTimer
<TimerWindow
> refresh_interval
= {std::chrono::milliseconds(7650), [this](auto) {
243 * Sometimes when something happened, force an update to the link-graph a bit sooner.
245 * We don't do it instantly on those changes, as for example when you are scrolling,
246 * constantly refreshing the link-graph would be very slow. So we delay it a bit,
247 * and only draw it once the scrolling settles down.
249 TimeoutTimer
<TimerWindow
> refresh_timeout
= {std::chrono::milliseconds(450), [this]() {
253 void OnPaint() override
256 if (_game_mode
== GM_MENU
) {
257 static const std::initializer_list
<SpriteID
> title_sprites
= {SPR_OTTD_O
, SPR_OTTD_P
, SPR_OTTD_E
, SPR_OTTD_N
, SPR_OTTD_T
, SPR_OTTD_T
, SPR_OTTD_D
};
258 uint letter_spacing
= ScaleGUITrad(10);
259 int name_width
= static_cast<int>(std::size(title_sprites
) - 1) * letter_spacing
;
261 for (const SpriteID
&sprite
: title_sprites
) {
262 name_width
+= GetSpriteSize(sprite
).width
;
264 int off_x
= (this->width
- name_width
) / 2;
266 for (const SpriteID
&sprite
: title_sprites
) {
267 DrawSprite(sprite
, PAL_NONE
, off_x
, ScaleGUITrad(50));
268 off_x
+= GetSpriteSize(sprite
).width
+ letter_spacing
;
273 EventState
OnHotkey(int hotkey
) override
275 if (hotkey
== GHK_QUIT
) {
276 HandleExitGameRequest();
280 /* Disable all key shortcuts, except quit shortcuts when
281 * generating the world, otherwise they create threading
282 * problem during the generating, resulting in random
283 * assertions that are hard to trigger and debug */
284 if (HasModalProgress()) return ES_NOT_HANDLED
;
288 /* No point returning from the main menu to itself */
289 if (_game_mode
== GM_MENU
) return ES_HANDLED
;
290 if (_settings_client
.gui
.autosave_on_exit
) {
292 _switch_mode
= SM_MENU
;
302 case GHK_BOUNDING_BOXES
:
303 ToggleBoundingBoxes();
306 case GHK_DIRTY_BLOCKS
:
310 case GHK_WIDGET_OUTLINES
:
311 ToggleWidgetOutlines();
315 if (_game_mode
== GM_MENU
) return ES_NOT_HANDLED
;
319 case GHK_CENTER_ZOOM
: {
320 Point pt
= GetTileBelowCursor();
322 bool instant
= (hotkey
== GHK_CENTER_ZOOM
&& this->viewport
->zoom
!= _settings_client
.gui
.zoom_min
);
323 if (hotkey
== GHK_CENTER_ZOOM
) MaxZoomInOut(ZOOM_IN
, this);
324 ScrollMainWindowTo(pt
.x
, pt
.y
, -1, instant
);
329 case GHK_RESET_OBJECT_TO_PLACE
: ResetObjectToPlace(); break;
330 case GHK_DELETE_WINDOWS
: CloseNonVitalWindows(); break;
331 case GHK_DELETE_NONVITAL_WINDOWS
: CloseAllNonVitalWindows(); break;
332 case GHK_DELETE_ALL_MESSAGES
: DeleteAllMessages(); break;
333 case GHK_REFRESH_SCREEN
: MarkWholeScreenDirty(); break;
335 case GHK_CRASH
: // Crash the game
336 *(volatile uint8_t *)nullptr = 0;
339 case GHK_MONEY
: // Gimme money
340 /* You can only cheat for money in singleplayer mode. */
341 if (!_networking
) Command
<CMD_MONEY_CHEAT
>::Post(10000000);
344 case GHK_UPDATE_COORDS
: // Update the coordinates of all station signs
345 UpdateAllVirtCoords();
348 case GHK_TOGGLE_TRANSPARENCY
:
349 case GHK_TOGGLE_TRANSPARENCY
+ 1:
350 case GHK_TOGGLE_TRANSPARENCY
+ 2:
351 case GHK_TOGGLE_TRANSPARENCY
+ 3:
352 case GHK_TOGGLE_TRANSPARENCY
+ 4:
353 case GHK_TOGGLE_TRANSPARENCY
+ 5:
354 case GHK_TOGGLE_TRANSPARENCY
+ 6:
355 case GHK_TOGGLE_TRANSPARENCY
+ 7:
356 case GHK_TOGGLE_TRANSPARENCY
+ 8:
357 /* Transparency toggle hot keys */
358 ToggleTransparency((TransparencyOption
)(hotkey
- GHK_TOGGLE_TRANSPARENCY
));
359 MarkWholeScreenDirty();
362 case GHK_TOGGLE_INVISIBILITY
:
363 case GHK_TOGGLE_INVISIBILITY
+ 1:
364 case GHK_TOGGLE_INVISIBILITY
+ 2:
365 case GHK_TOGGLE_INVISIBILITY
+ 3:
366 case GHK_TOGGLE_INVISIBILITY
+ 4:
367 case GHK_TOGGLE_INVISIBILITY
+ 5:
368 case GHK_TOGGLE_INVISIBILITY
+ 6:
369 case GHK_TOGGLE_INVISIBILITY
+ 7:
370 /* Invisibility toggle hot keys */
371 ToggleInvisibilityWithTransparency((TransparencyOption
)(hotkey
- GHK_TOGGLE_INVISIBILITY
));
372 MarkWholeScreenDirty();
375 case GHK_TRANSPARENCY_TOOLBAR
:
376 ShowTransparencyToolbar();
379 case GHK_TRANSPARANCY
:
380 ResetRestoreAllTransparency();
383 case GHK_CHAT
: // smart chat; send to team if any, otherwise to all
385 const NetworkClientInfo
*cio
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
386 if (cio
== nullptr) break;
388 ShowNetworkChatQueryWindow(NetworkClientPreferTeamChat(cio
) ? DESTTYPE_TEAM
: DESTTYPE_BROADCAST
, cio
->client_playas
);
392 case GHK_CHAT_ALL
: // send text message to all clients
393 if (_networking
) ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST
, 0);
396 case GHK_CHAT_COMPANY
: // send text to all team mates
398 const NetworkClientInfo
*cio
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
399 if (cio
== nullptr) break;
401 ShowNetworkChatQueryWindow(DESTTYPE_TEAM
, cio
->client_playas
);
405 case GHK_CHAT_SERVER
: // send text to the server
406 if (_networking
&& !_network_server
) {
407 ShowNetworkChatQueryWindow(DESTTYPE_CLIENT
, CLIENT_ID_SERVER
);
411 case GHK_CLOSE_NEWS
: // close active news window
412 if (!HideActiveNewsMessage()) return ES_NOT_HANDLED
;
415 case GHK_CLOSE_ERROR
: // close active error window
416 if (!HideActiveErrorMessage()) return ES_NOT_HANDLED
;
419 default: return ES_NOT_HANDLED
;
424 void OnScroll(Point delta
) override
426 this->viewport
->scrollpos_x
+= ScaleByZoom(delta
.x
, this->viewport
->zoom
);
427 this->viewport
->scrollpos_y
+= ScaleByZoom(delta
.y
, this->viewport
->zoom
);
428 this->viewport
->dest_scrollpos_x
= this->viewport
->scrollpos_x
;
429 this->viewport
->dest_scrollpos_y
= this->viewport
->scrollpos_y
;
430 this->refresh_timeout
.Reset();
433 void OnMouseWheel(int wheel
) override
435 if (_settings_client
.gui
.scrollwheel_scrolling
!= SWS_OFF
) {
438 /* When following, only change zoom - otherwise zoom to the cursor. */
439 if (this->viewport
->follow_vehicle
!= INVALID_VEHICLE
) {
440 DoZoomInOutWindow(in
? ZOOM_IN
: ZOOM_OUT
, this);
442 ZoomInOrOutToCursorWindow(in
, this);
447 void OnResize() override
449 if (this->viewport
!= nullptr) {
450 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_M_VIEWPORT
);
451 nvp
->UpdateViewportCoordinates(this);
452 this->refresh_timeout
.Reset();
456 bool OnTooltip([[maybe_unused
]] Point pt
, WidgetID widget
, TooltipCloseCondition close_cond
) override
458 if (widget
!= WID_M_VIEWPORT
) return false;
459 return this->viewport
->overlay
->ShowTooltip(pt
, close_cond
);
463 * Some data on this window has become invalid.
464 * @param data Information about the changed data.
465 * @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.
467 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
469 if (!gui_scope
) return;
470 /* Forward the message to the appropriate toolbar (ingame or scenario editor) */
471 InvalidateWindowData(WC_MAIN_TOOLBAR
, 0, data
, true);
474 static inline HotkeyList hotkeys
{"global", {
475 Hotkey({'Q' | WKC_CTRL
, 'Q' | WKC_META
}, "quit", GHK_QUIT
),
476 Hotkey({'W' | WKC_CTRL
, 'W' | WKC_META
}, "abandon", GHK_ABANDON
),
477 Hotkey(WKC_BACKQUOTE
, "console", GHK_CONSOLE
),
478 Hotkey('B' | WKC_CTRL
, "bounding_boxes", GHK_BOUNDING_BOXES
),
479 Hotkey('I' | WKC_CTRL
, "dirty_blocks", GHK_DIRTY_BLOCKS
),
480 Hotkey('O' | WKC_CTRL
, "widget_outlines", GHK_WIDGET_OUTLINES
),
481 Hotkey('C', "center", GHK_CENTER
),
482 Hotkey('Z', "center_zoom", GHK_CENTER_ZOOM
),
483 Hotkey(WKC_ESC
, "reset_object_to_place", GHK_RESET_OBJECT_TO_PLACE
),
484 Hotkey(WKC_DELETE
, "delete_windows", GHK_DELETE_WINDOWS
),
485 Hotkey(WKC_DELETE
| WKC_SHIFT
, "delete_all_windows", GHK_DELETE_NONVITAL_WINDOWS
),
486 Hotkey(WKC_DELETE
| WKC_CTRL
, "delete_all_messages", GHK_DELETE_ALL_MESSAGES
),
487 Hotkey('R' | WKC_CTRL
, "refresh_screen", GHK_REFRESH_SCREEN
),
489 Hotkey('0' | WKC_ALT
, "crash_game", GHK_CRASH
),
490 Hotkey('1' | WKC_ALT
, "money", GHK_MONEY
),
491 Hotkey('2' | WKC_ALT
, "update_coordinates", GHK_UPDATE_COORDS
),
493 Hotkey('1' | WKC_CTRL
, "transparency_signs", GHK_TOGGLE_TRANSPARENCY
),
494 Hotkey('2' | WKC_CTRL
, "transparency_trees", GHK_TOGGLE_TRANSPARENCY
+ 1),
495 Hotkey('3' | WKC_CTRL
, "transparency_houses", GHK_TOGGLE_TRANSPARENCY
+ 2),
496 Hotkey('4' | WKC_CTRL
, "transparency_industries", GHK_TOGGLE_TRANSPARENCY
+ 3),
497 Hotkey('5' | WKC_CTRL
, "transparency_buildings", GHK_TOGGLE_TRANSPARENCY
+ 4),
498 Hotkey('6' | WKC_CTRL
, "transparency_bridges", GHK_TOGGLE_TRANSPARENCY
+ 5),
499 Hotkey('7' | WKC_CTRL
, "transparency_structures", GHK_TOGGLE_TRANSPARENCY
+ 6),
500 Hotkey('8' | WKC_CTRL
, "transparency_catenary", GHK_TOGGLE_TRANSPARENCY
+ 7),
501 Hotkey('9' | WKC_CTRL
, "transparency_loading", GHK_TOGGLE_TRANSPARENCY
+ 8),
502 Hotkey('1' | WKC_CTRL
| WKC_SHIFT
, "invisibility_signs", GHK_TOGGLE_INVISIBILITY
),
503 Hotkey('2' | WKC_CTRL
| WKC_SHIFT
, "invisibility_trees", GHK_TOGGLE_INVISIBILITY
+ 1),
504 Hotkey('3' | WKC_CTRL
| WKC_SHIFT
, "invisibility_houses", GHK_TOGGLE_INVISIBILITY
+ 2),
505 Hotkey('4' | WKC_CTRL
| WKC_SHIFT
, "invisibility_industries", GHK_TOGGLE_INVISIBILITY
+ 3),
506 Hotkey('5' | WKC_CTRL
| WKC_SHIFT
, "invisibility_buildings", GHK_TOGGLE_INVISIBILITY
+ 4),
507 Hotkey('6' | WKC_CTRL
| WKC_SHIFT
, "invisibility_bridges", GHK_TOGGLE_INVISIBILITY
+ 5),
508 Hotkey('7' | WKC_CTRL
| WKC_SHIFT
, "invisibility_structures", GHK_TOGGLE_INVISIBILITY
+ 6),
509 Hotkey('8' | WKC_CTRL
| WKC_SHIFT
, "invisibility_catenary", GHK_TOGGLE_INVISIBILITY
+ 7),
510 Hotkey('X' | WKC_CTRL
, "transparency_toolbar", GHK_TRANSPARENCY_TOOLBAR
),
511 Hotkey('X', "toggle_transparency", GHK_TRANSPARANCY
),
512 Hotkey({WKC_RETURN
, 'T'}, "chat", GHK_CHAT
),
513 Hotkey({WKC_SHIFT
| WKC_RETURN
, WKC_SHIFT
| 'T'}, "chat_all", GHK_CHAT_ALL
),
514 Hotkey({WKC_CTRL
| WKC_RETURN
, WKC_CTRL
| 'T'}, "chat_company", GHK_CHAT_COMPANY
),
515 Hotkey({WKC_CTRL
| WKC_SHIFT
| WKC_RETURN
, WKC_CTRL
| WKC_SHIFT
| 'T'}, "chat_server", GHK_CHAT_SERVER
),
516 Hotkey(WKC_SPACE
, "close_news", GHK_CLOSE_NEWS
),
517 Hotkey(WKC_SPACE
, "close_error", GHK_CLOSE_ERROR
),
521 static WindowDesc
_main_window_desc(
522 WDP_MANUAL
, nullptr, 0, 0,
523 WC_MAIN_WINDOW
, WC_NONE
,
525 _nested_main_window_widgets
,
530 * Does the given keycode match one of the keycodes bound to 'quit game'?
531 * @param keycode The keycode that was pressed by the user.
532 * @return True iff the keycode matches one of the hotkeys for 'quit'.
534 bool IsQuitKey(uint16_t keycode
)
536 int num
= MainWindow::hotkeys
.CheckMatch(keycode
);
537 return num
== GHK_QUIT
;
541 void ShowSelectGameWindow();
544 * Initialise the default colours (remaps and the likes), and load the main windows.
546 void SetupColoursAndInitialWindow()
548 for (Colours i
= COLOUR_BEGIN
; i
!= COLOUR_END
; i
++) {
549 const uint8_t *b
= GetNonSprite(GENERAL_SPRITE_COLOUR(i
), SpriteType::Recolour
) + 1;
550 assert(b
!= nullptr);
551 for (ColourShade j
= SHADE_BEGIN
; j
< SHADE_END
; j
++) {
552 SetColourGradient(i
, j
, b
[0xC6 + j
]);
556 new MainWindow(_main_window_desc
);
558 /* XXX: these are not done */
559 switch (_game_mode
) {
560 default: NOT_REACHED();
562 ShowSelectGameWindow();
573 * Show the vital in-game windows.
575 void ShowVitalWindows()
579 /* Status bad only for normal games */
580 if (_game_mode
== GM_EDITOR
) return;
586 * Size of the application screen changed.
587 * Adapt the game screen-size, re-allocate the open windows, and repaint everything
589 void GameSizeChanged()
591 _cur_resolution
.width
= _screen
.width
;
592 _cur_resolution
.height
= _screen
.height
;
594 RelocateAllWindows(_screen
.width
, _screen
.height
);
595 MarkWholeScreenDirty();