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
;
115 w
->viewport
->follow_vehicle
= INVALID_VEHICLE
;
118 if (vp
->zoom
>= _settings_client
.gui
.zoom_max
) return false;
119 vp
->zoom
= (ZoomLevel
)((int)vp
->zoom
+ 1);
121 w
->viewport
->scrollpos_x
-= vp
->virtual_width
>> 1;
122 w
->viewport
->scrollpos_y
-= vp
->virtual_height
>> 1;
123 w
->viewport
->dest_scrollpos_x
= w
->viewport
->scrollpos_x
;
124 w
->viewport
->dest_scrollpos_y
= w
->viewport
->scrollpos_y
;
126 vp
->virtual_width
<<= 1;
127 vp
->virtual_height
<<= 1;
128 w
->viewport
->follow_vehicle
= INVALID_VEHICLE
;
131 if (vp
!= nullptr) { // the vp can be null when how == ZOOM_NONE
132 vp
->virtual_left
= w
->viewport
->scrollpos_x
;
133 vp
->virtual_top
= w
->viewport
->scrollpos_y
;
135 /* Update the windows that have zoom-buttons to perhaps disable their buttons */
140 void ZoomInOrOutToCursorWindow(bool in
, Window
*w
)
142 assert(w
!= nullptr);
144 if (_game_mode
!= GM_MENU
) {
145 Viewport
*vp
= w
->viewport
;
146 if ((in
&& vp
->zoom
<= _settings_client
.gui
.zoom_min
) || (!in
&& vp
->zoom
>= _settings_client
.gui
.zoom_max
)) return;
148 Point pt
= GetTileZoomCenterWindow(in
, w
);
150 ScrollWindowTo(pt
.x
, pt
.y
, -1, w
, true);
152 DoZoomInOutWindow(in
? ZOOM_IN
: ZOOM_OUT
, w
);
157 void FixTitleGameZoom(int zoom_adjust
)
159 if (_game_mode
!= GM_MENU
) return;
161 Viewport
*vp
= GetMainWindow()->viewport
;
163 /* Adjust the zoom in/out.
164 * Can't simply add, since operator+ is not defined on the ZoomLevel type. */
165 vp
->zoom
= _gui_zoom
;
166 while (zoom_adjust
< 0 && vp
->zoom
!= _settings_client
.gui
.zoom_min
) {
170 while (zoom_adjust
> 0 && vp
->zoom
!= _settings_client
.gui
.zoom_max
) {
175 vp
->virtual_width
= ScaleByZoom(vp
->width
, vp
->zoom
);
176 vp
->virtual_height
= ScaleByZoom(vp
->height
, vp
->zoom
);
179 static constexpr NWidgetPart _nested_main_window_widgets
[] = {
180 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_M_VIEWPORT
), SetResize(1, 1),
192 GHK_RESET_OBJECT_TO_PLACE
,
194 GHK_DELETE_NONVITAL_WINDOWS
,
195 GHK_DELETE_ALL_MESSAGES
,
200 GHK_TOGGLE_TRANSPARENCY
,
201 GHK_TOGGLE_INVISIBILITY
= GHK_TOGGLE_TRANSPARENCY
+ 9,
202 GHK_TRANSPARENCY_TOOLBAR
= GHK_TOGGLE_INVISIBILITY
+ 8,
212 struct MainWindow
: Window
214 MainWindow(WindowDesc
*desc
) : Window(desc
)
217 CLRBITS(this->flags
, WF_WHITE_BORDER
);
218 ResizeWindow(this, _screen
.width
, _screen
.height
);
220 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_M_VIEWPORT
);
221 nvp
->InitializeViewport(this, TileXY(32, 32), ScaleZoomGUI(ZOOM_LVL_VIEWPORT
));
223 this->viewport
->overlay
= std::make_shared
<LinkGraphOverlay
>(this, WID_M_VIEWPORT
, 0, 0, 2);
224 this->refresh_timeout
.Reset();
227 /** Refresh the link-graph overlay. */
228 void RefreshLinkGraph()
230 if (this->viewport
->overlay
->GetCargoMask() == 0 ||
231 this->viewport
->overlay
->GetCompanyMask() == 0) {
235 this->viewport
->overlay
->SetDirty();
236 this->GetWidget
<NWidgetBase
>(WID_M_VIEWPORT
)->SetDirty(this);
239 /** Refresh the link-graph overlay on a regular interval. */
240 IntervalTimer
<TimerWindow
> refresh_interval
= {std::chrono::milliseconds(7650), [this](auto) {
245 * Sometimes when something happened, force an update to the link-graph a bit sooner.
247 * We don't do it instantly on those changes, as for example when you are scrolling,
248 * constantly refreshing the link-graph would be very slow. So we delay it a bit,
249 * and only draw it once the scrolling settles down.
251 TimeoutTimer
<TimerWindow
> refresh_timeout
= {std::chrono::milliseconds(450), [this]() {
255 void OnPaint() override
258 if (_game_mode
== GM_MENU
) {
259 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
};
260 uint letter_spacing
= ScaleGUITrad(10);
261 int name_width
= (lengthof(title_sprites
) - 1) * letter_spacing
;
263 for (uint i
= 0; i
< lengthof(title_sprites
); i
++) {
264 name_width
+= GetSpriteSize(title_sprites
[i
]).width
;
266 int off_x
= (this->width
- name_width
) / 2;
268 for (uint i
= 0; i
< lengthof(title_sprites
); i
++) {
269 DrawSprite(title_sprites
[i
], PAL_NONE
, off_x
, ScaleGUITrad(50));
270 off_x
+= GetSpriteSize(title_sprites
[i
]).width
+ letter_spacing
;
275 EventState
OnHotkey(int hotkey
) override
277 if (hotkey
== GHK_QUIT
) {
278 HandleExitGameRequest();
282 /* Disable all key shortcuts, except quit shortcuts when
283 * generating the world, otherwise they create threading
284 * problem during the generating, resulting in random
285 * assertions that are hard to trigger and debug */
286 if (HasModalProgress()) return ES_NOT_HANDLED
;
290 /* No point returning from the main menu to itself */
291 if (_game_mode
== GM_MENU
) return ES_HANDLED
;
292 if (_settings_client
.gui
.autosave_on_exit
) {
294 _switch_mode
= SM_MENU
;
304 case GHK_BOUNDING_BOXES
:
305 ToggleBoundingBoxes();
308 case GHK_DIRTY_BLOCKS
:
312 case GHK_WIDGET_OUTLINES
:
313 ToggleWidgetOutlines();
317 if (_game_mode
== GM_MENU
) return ES_NOT_HANDLED
;
321 case GHK_CENTER_ZOOM
: {
322 Point pt
= GetTileBelowCursor();
324 bool instant
= (hotkey
== GHK_CENTER_ZOOM
&& this->viewport
->zoom
!= _settings_client
.gui
.zoom_min
);
325 if (hotkey
== GHK_CENTER_ZOOM
) MaxZoomInOut(ZOOM_IN
, this);
326 ScrollMainWindowTo(pt
.x
, pt
.y
, -1, instant
);
331 case GHK_RESET_OBJECT_TO_PLACE
: ResetObjectToPlace(); break;
332 case GHK_DELETE_WINDOWS
: CloseNonVitalWindows(); break;
333 case GHK_DELETE_NONVITAL_WINDOWS
: CloseAllNonVitalWindows(); break;
334 case GHK_DELETE_ALL_MESSAGES
: DeleteAllMessages(); break;
335 case GHK_REFRESH_SCREEN
: MarkWholeScreenDirty(); break;
337 case GHK_CRASH
: // Crash the game
338 *(volatile byte
*)nullptr = 0;
341 case GHK_MONEY
: // Gimme money
342 /* You can only cheat for money in singleplayer mode. */
343 if (!_networking
) Command
<CMD_MONEY_CHEAT
>::Post(10000000);
346 case GHK_UPDATE_COORDS
: // Update the coordinates of all station signs
347 UpdateAllVirtCoords();
350 case GHK_TOGGLE_TRANSPARENCY
:
351 case GHK_TOGGLE_TRANSPARENCY
+ 1:
352 case GHK_TOGGLE_TRANSPARENCY
+ 2:
353 case GHK_TOGGLE_TRANSPARENCY
+ 3:
354 case GHK_TOGGLE_TRANSPARENCY
+ 4:
355 case GHK_TOGGLE_TRANSPARENCY
+ 5:
356 case GHK_TOGGLE_TRANSPARENCY
+ 6:
357 case GHK_TOGGLE_TRANSPARENCY
+ 7:
358 case GHK_TOGGLE_TRANSPARENCY
+ 8:
359 /* Transparency toggle hot keys */
360 ToggleTransparency((TransparencyOption
)(hotkey
- GHK_TOGGLE_TRANSPARENCY
));
361 MarkWholeScreenDirty();
364 case GHK_TOGGLE_INVISIBILITY
:
365 case GHK_TOGGLE_INVISIBILITY
+ 1:
366 case GHK_TOGGLE_INVISIBILITY
+ 2:
367 case GHK_TOGGLE_INVISIBILITY
+ 3:
368 case GHK_TOGGLE_INVISIBILITY
+ 4:
369 case GHK_TOGGLE_INVISIBILITY
+ 5:
370 case GHK_TOGGLE_INVISIBILITY
+ 6:
371 case GHK_TOGGLE_INVISIBILITY
+ 7:
372 /* Invisibility toggle hot keys */
373 ToggleInvisibilityWithTransparency((TransparencyOption
)(hotkey
- GHK_TOGGLE_INVISIBILITY
));
374 MarkWholeScreenDirty();
377 case GHK_TRANSPARENCY_TOOLBAR
:
378 ShowTransparencyToolbar();
381 case GHK_TRANSPARANCY
:
382 ResetRestoreAllTransparency();
385 case GHK_CHAT
: // smart chat; send to team if any, otherwise to all
387 const NetworkClientInfo
*cio
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
388 if (cio
== nullptr) break;
390 ShowNetworkChatQueryWindow(NetworkClientPreferTeamChat(cio
) ? DESTTYPE_TEAM
: DESTTYPE_BROADCAST
, cio
->client_playas
);
394 case GHK_CHAT_ALL
: // send text message to all clients
395 if (_networking
) ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST
, 0);
398 case GHK_CHAT_COMPANY
: // send text to all team mates
400 const NetworkClientInfo
*cio
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
401 if (cio
== nullptr) break;
403 ShowNetworkChatQueryWindow(DESTTYPE_TEAM
, cio
->client_playas
);
407 case GHK_CHAT_SERVER
: // send text to the server
408 if (_networking
&& !_network_server
) {
409 ShowNetworkChatQueryWindow(DESTTYPE_CLIENT
, CLIENT_ID_SERVER
);
413 case GHK_CLOSE_NEWS
: // close active news window
414 if (!HideActiveNewsMessage()) return ES_NOT_HANDLED
;
417 case GHK_CLOSE_ERROR
: // close active error window
418 if (!HideActiveErrorMessage()) return ES_NOT_HANDLED
;
421 default: return ES_NOT_HANDLED
;
426 void OnScroll(Point delta
) override
428 this->viewport
->scrollpos_x
+= ScaleByZoom(delta
.x
, this->viewport
->zoom
);
429 this->viewport
->scrollpos_y
+= ScaleByZoom(delta
.y
, this->viewport
->zoom
);
430 this->viewport
->dest_scrollpos_x
= this->viewport
->scrollpos_x
;
431 this->viewport
->dest_scrollpos_y
= this->viewport
->scrollpos_y
;
432 this->refresh_timeout
.Reset();
435 void OnMouseWheel(int wheel
) override
437 if (_settings_client
.gui
.scrollwheel_scrolling
!= 2) {
438 ZoomInOrOutToCursorWindow(wheel
< 0, this);
442 void OnResize() override
444 if (this->viewport
!= nullptr) {
445 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_M_VIEWPORT
);
446 nvp
->UpdateViewportCoordinates(this);
447 this->refresh_timeout
.Reset();
451 bool OnTooltip([[maybe_unused
]] Point pt
, WidgetID widget
, TooltipCloseCondition close_cond
) override
453 if (widget
!= WID_M_VIEWPORT
) return false;
454 return this->viewport
->overlay
->ShowTooltip(pt
, close_cond
);
458 * Some data on this window has become invalid.
459 * @param data Information about the changed data.
460 * @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.
462 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
464 if (!gui_scope
) return;
465 /* Forward the message to the appropriate toolbar (ingame or scenario editor) */
466 InvalidateWindowData(WC_MAIN_TOOLBAR
, 0, data
, true);
469 static inline HotkeyList hotkeys
{"global", {
470 Hotkey({'Q' | WKC_CTRL
, 'Q' | WKC_META
}, "quit", GHK_QUIT
),
471 Hotkey({'W' | WKC_CTRL
, 'W' | WKC_META
}, "abandon", GHK_ABANDON
),
472 Hotkey(WKC_BACKQUOTE
, "console", GHK_CONSOLE
),
473 Hotkey('B' | WKC_CTRL
, "bounding_boxes", GHK_BOUNDING_BOXES
),
474 Hotkey('I' | WKC_CTRL
, "dirty_blocks", GHK_DIRTY_BLOCKS
),
475 Hotkey('O' | WKC_CTRL
, "widget_outlines", GHK_WIDGET_OUTLINES
),
476 Hotkey('C', "center", GHK_CENTER
),
477 Hotkey('Z', "center_zoom", GHK_CENTER_ZOOM
),
478 Hotkey(WKC_ESC
, "reset_object_to_place", GHK_RESET_OBJECT_TO_PLACE
),
479 Hotkey(WKC_DELETE
, "delete_windows", GHK_DELETE_WINDOWS
),
480 Hotkey(WKC_DELETE
| WKC_SHIFT
, "delete_all_windows", GHK_DELETE_NONVITAL_WINDOWS
),
481 Hotkey(WKC_DELETE
| WKC_CTRL
, "delete_all_messages", GHK_DELETE_ALL_MESSAGES
),
482 Hotkey('R' | WKC_CTRL
, "refresh_screen", GHK_REFRESH_SCREEN
),
484 Hotkey('0' | WKC_ALT
, "crash_game", GHK_CRASH
),
485 Hotkey('1' | WKC_ALT
, "money", GHK_MONEY
),
486 Hotkey('2' | WKC_ALT
, "update_coordinates", GHK_UPDATE_COORDS
),
488 Hotkey('1' | WKC_CTRL
, "transparency_signs", GHK_TOGGLE_TRANSPARENCY
),
489 Hotkey('2' | WKC_CTRL
, "transparency_trees", GHK_TOGGLE_TRANSPARENCY
+ 1),
490 Hotkey('3' | WKC_CTRL
, "transparency_houses", GHK_TOGGLE_TRANSPARENCY
+ 2),
491 Hotkey('4' | WKC_CTRL
, "transparency_industries", GHK_TOGGLE_TRANSPARENCY
+ 3),
492 Hotkey('5' | WKC_CTRL
, "transparency_buildings", GHK_TOGGLE_TRANSPARENCY
+ 4),
493 Hotkey('6' | WKC_CTRL
, "transparency_bridges", GHK_TOGGLE_TRANSPARENCY
+ 5),
494 Hotkey('7' | WKC_CTRL
, "transparency_structures", GHK_TOGGLE_TRANSPARENCY
+ 6),
495 Hotkey('8' | WKC_CTRL
, "transparency_catenary", GHK_TOGGLE_TRANSPARENCY
+ 7),
496 Hotkey('9' | WKC_CTRL
, "transparency_loading", GHK_TOGGLE_TRANSPARENCY
+ 8),
497 Hotkey('1' | WKC_CTRL
| WKC_SHIFT
, "invisibility_signs", GHK_TOGGLE_INVISIBILITY
),
498 Hotkey('2' | WKC_CTRL
| WKC_SHIFT
, "invisibility_trees", GHK_TOGGLE_INVISIBILITY
+ 1),
499 Hotkey('3' | WKC_CTRL
| WKC_SHIFT
, "invisibility_houses", GHK_TOGGLE_INVISIBILITY
+ 2),
500 Hotkey('4' | WKC_CTRL
| WKC_SHIFT
, "invisibility_industries", GHK_TOGGLE_INVISIBILITY
+ 3),
501 Hotkey('5' | WKC_CTRL
| WKC_SHIFT
, "invisibility_buildings", GHK_TOGGLE_INVISIBILITY
+ 4),
502 Hotkey('6' | WKC_CTRL
| WKC_SHIFT
, "invisibility_bridges", GHK_TOGGLE_INVISIBILITY
+ 5),
503 Hotkey('7' | WKC_CTRL
| WKC_SHIFT
, "invisibility_structures", GHK_TOGGLE_INVISIBILITY
+ 6),
504 Hotkey('8' | WKC_CTRL
| WKC_SHIFT
, "invisibility_catenary", GHK_TOGGLE_INVISIBILITY
+ 7),
505 Hotkey('X' | WKC_CTRL
, "transparency_toolbar", GHK_TRANSPARENCY_TOOLBAR
),
506 Hotkey('X', "toggle_transparency", GHK_TRANSPARANCY
),
507 Hotkey({WKC_RETURN
, 'T'}, "chat", GHK_CHAT
),
508 Hotkey({WKC_SHIFT
| WKC_RETURN
, WKC_SHIFT
| 'T'}, "chat_all", GHK_CHAT_ALL
),
509 Hotkey({WKC_CTRL
| WKC_RETURN
, WKC_CTRL
| 'T'}, "chat_company", GHK_CHAT_COMPANY
),
510 Hotkey({WKC_CTRL
| WKC_SHIFT
| WKC_RETURN
, WKC_CTRL
| WKC_SHIFT
| 'T'}, "chat_server", GHK_CHAT_SERVER
),
511 Hotkey(WKC_SPACE
, "close_news", GHK_CLOSE_NEWS
),
512 Hotkey(WKC_SPACE
, "close_error", GHK_CLOSE_ERROR
),
516 static WindowDesc
_main_window_desc(__FILE__
, __LINE__
,
517 WDP_MANUAL
, nullptr, 0, 0,
518 WC_MAIN_WINDOW
, WC_NONE
,
520 std::begin(_nested_main_window_widgets
), std::end(_nested_main_window_widgets
),
525 * Does the given keycode match one of the keycodes bound to 'quit game'?
526 * @param keycode The keycode that was pressed by the user.
527 * @return True iff the keycode matches one of the hotkeys for 'quit'.
529 bool IsQuitKey(uint16_t keycode
)
531 int num
= MainWindow::hotkeys
.CheckMatch(keycode
);
532 return num
== GHK_QUIT
;
536 void ShowSelectGameWindow();
539 * Initialise the default colours (remaps and the likes), and load the main windows.
541 void SetupColoursAndInitialWindow()
543 for (uint i
= 0; i
!= 16; i
++) {
544 const byte
*b
= GetNonSprite(GENERAL_SPRITE_COLOUR(i
), SpriteType::Recolour
);
547 memcpy(_colour_gradient
[i
], b
+ 0xC6, sizeof(_colour_gradient
[i
]));
550 new MainWindow(&_main_window_desc
);
552 /* XXX: these are not done */
553 switch (_game_mode
) {
554 default: NOT_REACHED();
556 ShowSelectGameWindow();
567 * Show the vital in-game windows.
569 void ShowVitalWindows()
573 /* Status bad only for normal games */
574 if (_game_mode
== GM_EDITOR
) return;
580 * Size of the application screen changed.
581 * Adapt the game screen-size, re-allocate the open windows, and repaint everything
583 void GameSizeChanged()
585 _cur_resolution
.width
= _screen
.width
;
586 _cur_resolution
.height
= _screen
.height
;
588 RelocateAllWindows(_screen
.width
, _screen
.height
);
589 MarkWholeScreenDirty();