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"
23 #include "sound_func.h"
24 #include "transparency.h"
25 #include "strings_func.h"
26 #include "zoom_func.h"
27 #include "company_base.h"
28 #include "company_func.h"
29 #include "toolbar_gui.h"
30 #include "statusbar_gui.h"
31 #include "linkgraph/linkgraph_gui.h"
32 #include "tilehighlight_func.h"
35 #include "saveload/saveload.h"
37 #include "widgets/main_widget.h"
39 #include "network/network.h"
40 #include "network/network_func.h"
41 #include "network/network_gui.h"
42 #include "network/network_base.h"
44 #include "table/sprites.h"
45 #include "table/strings.h"
47 static int _rename_id
= 1;
48 static int _rename_what
= -1;
50 void CcGiveMoney(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
53 if (result
.Failed() || !_settings_game
.economy
.give_money
) return;
55 /* Inform the company of the action of one of its clients (controllers). */
58 GetString(msg
, STR_COMPANY_NAME
, lastof(msg
));
60 if (!_network_server
) {
61 NetworkClientSendChat(NETWORK_ACTION_GIVE_MONEY
, DESTTYPE_TEAM
, p2
, msg
, p1
);
63 NetworkServerSendChat(NETWORK_ACTION_GIVE_MONEY
, DESTTYPE_TEAM
, p2
, msg
, CLIENT_ID_SERVER
, p1
);
65 #endif /* ENABLE_NETWORK */
68 void HandleOnEditText(const char *str
)
70 switch (_rename_what
) {
72 case 3: { // Give money, you can only give money in excess of loan
73 const Company
*c
= Company::GetIfValid(_local_company
);
75 Money money
= min(c
->money
- c
->current_loan
, (Money
)(atoi(str
) / _currency
->rate
));
77 uint32 money_c
= Clamp(ClampToI32(money
), 0, 20000000); // Clamp between 20 million and 0
79 /* Give 'id' the money, and subtract it from ourself */
80 DoCommandP(0, money_c
, _rename_id
, CMD_GIVE_MONEY
| CMD_MSG(STR_ERROR_INSUFFICIENT_FUNDS
), CcGiveMoney
, str
);
83 #endif /* ENABLE_NETWORK */
84 default: NOT_REACHED();
87 _rename_id
= _rename_what
= -1;
91 * This code is shared for the majority of the pushbuttons.
92 * Handles e.g. the pressing of a button (to build things), playing of click sound and sets certain parameters
94 * @param w Window which called the function
95 * @param widget ID of the widget (=button) that called this function
96 * @param cursor How should the cursor image change? E.g. cursor with depot image in it
97 * @param mode Tile highlighting mode, e.g. drawing a rectangle or a dot on the ground
98 * @return true if the button is clicked, false if it's unclicked
100 bool HandlePlacePushButton(Window
*w
, int widget
, CursorID cursor
, HighLightStyle mode
)
102 if (w
->IsWidgetDisabled(widget
)) return false;
104 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
107 if (w
->IsWidgetLowered(widget
)) {
108 ResetObjectToPlace();
112 SetObjectToPlace(cursor
, PAL_NONE
, mode
, w
->window_class
, w
->window_number
);
113 w
->LowerWidget(widget
);
118 void CcPlaySound10(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
120 if (result
.Succeeded() && _settings_client
.sound
.confirm
) SndPlayTileFx(SND_12_EXPLOSION
, tile
);
123 #ifdef ENABLE_NETWORK
124 void ShowNetworkGiveMoneyWindow(CompanyID company
)
126 _rename_id
= company
;
128 ShowQueryString(STR_EMPTY
, STR_NETWORK_GIVE_MONEY_CAPTION
, 30, NULL
, CS_NUMERAL
, QSF_NONE
);
130 #endif /* ENABLE_NETWORK */
134 * Zooms a viewport in a window in or out.
135 * @param how Zooming direction.
136 * @param w Window owning the viewport.
137 * @return Returns \c true if zooming step could be done, \c false if further zooming is not possible.
138 * @note No button handling or what so ever is done.
140 bool DoZoomInOutWindow(ZoomStateChange how
, Window
*w
)
149 /* On initialisation of the viewport we don't do anything. */
153 if (vp
->zoom
<= _settings_client
.gui
.zoom_min
) return false;
154 vp
->zoom
= (ZoomLevel
)((int)vp
->zoom
- 1);
155 vp
->virtual_width
>>= 1;
156 vp
->virtual_height
>>= 1;
158 w
->viewport
->scrollpos_x
+= vp
->virtual_width
>> 1;
159 w
->viewport
->scrollpos_y
+= vp
->virtual_height
>> 1;
160 w
->viewport
->dest_scrollpos_x
= w
->viewport
->scrollpos_x
;
161 w
->viewport
->dest_scrollpos_y
= w
->viewport
->scrollpos_y
;
162 w
->viewport
->follow_vehicle
= INVALID_VEHICLE
;
165 if (vp
->zoom
>= _settings_client
.gui
.zoom_max
) return false;
166 vp
->zoom
= (ZoomLevel
)((int)vp
->zoom
+ 1);
168 w
->viewport
->scrollpos_x
-= vp
->virtual_width
>> 1;
169 w
->viewport
->scrollpos_y
-= vp
->virtual_height
>> 1;
170 w
->viewport
->dest_scrollpos_x
= w
->viewport
->scrollpos_x
;
171 w
->viewport
->dest_scrollpos_y
= w
->viewport
->scrollpos_y
;
173 vp
->virtual_width
<<= 1;
174 vp
->virtual_height
<<= 1;
175 w
->viewport
->follow_vehicle
= INVALID_VEHICLE
;
178 if (vp
!= NULL
) { // the vp can be null when how == ZOOM_NONE
179 vp
->virtual_left
= w
->viewport
->scrollpos_x
;
180 vp
->virtual_top
= w
->viewport
->scrollpos_y
;
182 /* Update the windows that have zoom-buttons to perhaps disable their buttons */
187 void ZoomInOrOutToCursorWindow(bool in
, Window
*w
)
191 if (_game_mode
!= GM_MENU
) {
192 ViewPort
*vp
= w
->viewport
;
193 if ((in
&& vp
->zoom
<= _settings_client
.gui
.zoom_min
) || (!in
&& vp
->zoom
>= _settings_client
.gui
.zoom_max
)) return;
195 Point pt
= GetTileZoomCenterWindow(in
, w
);
197 ScrollWindowTo(pt
.x
, pt
.y
, -1, w
, true);
199 DoZoomInOutWindow(in
? ZOOM_IN
: ZOOM_OUT
, w
);
204 static const struct NWidgetPart _nested_main_window_widgets
[] = {
205 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_M_VIEWPORT
), SetResize(1, 1),
216 GHK_RESET_OBJECT_TO_PLACE
,
218 GHK_DELETE_NONVITAL_WINDOWS
,
223 GHK_TOGGLE_TRANSPARENCY
,
224 GHK_TOGGLE_INVISIBILITY
= GHK_TOGGLE_TRANSPARENCY
+ 9,
225 GHK_TRANSPARENCY_TOOLBAR
= GHK_TOGGLE_INVISIBILITY
+ 8,
233 struct MainWindow
: Window
237 static const uint LINKGRAPH_REFRESH_PERIOD
= 0xff;
238 static const uint LINKGRAPH_DELAY
= 0xf;
240 MainWindow(WindowDesc
*desc
) : Window(desc
)
243 CLRBITS(this->flags
, WF_WHITE_BORDER
);
244 ResizeWindow(this, _screen
.width
, _screen
.height
);
246 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_M_VIEWPORT
);
247 nvp
->InitializeViewport(this, TileXY(32, 32), ZOOM_LVL_VIEWPORT
);
249 this->viewport
->overlay
= new LinkGraphOverlay(this, WID_M_VIEWPORT
, 0, 0, 3);
250 this->refresh
= LINKGRAPH_DELAY
;
253 virtual void OnTick()
255 if (--this->refresh
> 0) return;
257 this->refresh
= LINKGRAPH_REFRESH_PERIOD
;
259 if (this->viewport
->overlay
->GetCargoMask() == 0 ||
260 this->viewport
->overlay
->GetCompanyMask() == 0) {
264 this->viewport
->overlay
->RebuildCache();
265 this->GetWidget
<NWidgetBase
>(WID_M_VIEWPORT
)->SetDirty(this);
268 virtual void OnPaint()
271 if (_game_mode
== GM_MENU
) {
272 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
};
273 static const uint LETTER_SPACING
= 10;
274 int name_width
= (lengthof(title_sprites
) - 1) * LETTER_SPACING
;
276 for (uint i
= 0; i
< lengthof(title_sprites
); i
++) {
277 name_width
+= GetSpriteSize(title_sprites
[i
]).width
;
279 int off_x
= (this->width
- name_width
) / 2;
281 for (uint i
= 0; i
< lengthof(title_sprites
); i
++) {
282 DrawSprite(title_sprites
[i
], PAL_NONE
, off_x
, 50);
283 off_x
+= GetSpriteSize(title_sprites
[i
]).width
+ LETTER_SPACING
;
288 virtual EventState
OnHotkey(int hotkey
)
290 if (hotkey
== GHK_QUIT
) {
291 HandleExitGameRequest();
295 /* Disable all key shortcuts, except quit shortcuts when
296 * generating the world, otherwise they create threading
297 * problem during the generating, resulting in random
298 * assertions that are hard to trigger and debug */
299 if (HasModalProgress()) return ES_NOT_HANDLED
;
303 /* No point returning from the main menu to itself */
304 if (_game_mode
== GM_MENU
) return ES_HANDLED
;
305 if (_settings_client
.gui
.autosave_on_exit
) {
307 _switch_mode
= SM_MENU
;
317 case GHK_BOUNDING_BOXES
:
318 ToggleBoundingBoxes();
321 case GHK_DIRTY_BLOCKS
:
326 if (_game_mode
== GM_MENU
) return ES_NOT_HANDLED
;
330 case GHK_CENTER_ZOOM
: {
331 Point pt
= GetTileBelowCursor();
333 bool instant
= (hotkey
== GHK_CENTER_ZOOM
&& this->viewport
->zoom
!= _settings_client
.gui
.zoom_min
);
334 if (hotkey
== GHK_CENTER_ZOOM
) MaxZoomInOut(ZOOM_IN
, this);
335 ScrollMainWindowTo(pt
.x
, pt
.y
, -1, instant
);
340 case GHK_RESET_OBJECT_TO_PLACE
: ResetObjectToPlace(); break;
341 case GHK_DELETE_WINDOWS
: DeleteNonVitalWindows(); break;
342 case GHK_DELETE_NONVITAL_WINDOWS
: DeleteAllNonVitalWindows(); break;
343 case GHK_REFRESH_SCREEN
: MarkWholeScreenDirty(); break;
345 case GHK_CRASH
: // Crash the game
346 *(volatile byte
*)0 = 0;
349 case GHK_MONEY
: // Gimme money
350 /* You can only cheat for money in single player. */
351 if (!_networking
) DoCommandP(0, 10000000, 0, CMD_MONEY_CHEAT
);
354 case GHK_UPDATE_COORDS
: // Update the coordinates of all station signs
355 UpdateAllVirtCoords();
358 case GHK_TOGGLE_TRANSPARENCY
:
359 case GHK_TOGGLE_TRANSPARENCY
+ 1:
360 case GHK_TOGGLE_TRANSPARENCY
+ 2:
361 case GHK_TOGGLE_TRANSPARENCY
+ 3:
362 case GHK_TOGGLE_TRANSPARENCY
+ 4:
363 case GHK_TOGGLE_TRANSPARENCY
+ 5:
364 case GHK_TOGGLE_TRANSPARENCY
+ 6:
365 case GHK_TOGGLE_TRANSPARENCY
+ 7:
366 case GHK_TOGGLE_TRANSPARENCY
+ 8:
367 /* Transparency toggle hot keys */
368 ToggleTransparency((TransparencyOption
)(hotkey
- GHK_TOGGLE_TRANSPARENCY
));
369 MarkWholeScreenDirty();
372 case GHK_TOGGLE_INVISIBILITY
:
373 case GHK_TOGGLE_INVISIBILITY
+ 1:
374 case GHK_TOGGLE_INVISIBILITY
+ 2:
375 case GHK_TOGGLE_INVISIBILITY
+ 3:
376 case GHK_TOGGLE_INVISIBILITY
+ 4:
377 case GHK_TOGGLE_INVISIBILITY
+ 5:
378 case GHK_TOGGLE_INVISIBILITY
+ 6:
379 case GHK_TOGGLE_INVISIBILITY
+ 7:
380 /* Invisibility toggle hot keys */
381 ToggleInvisibilityWithTransparency((TransparencyOption
)(hotkey
- GHK_TOGGLE_INVISIBILITY
));
382 MarkWholeScreenDirty();
385 case GHK_TRANSPARENCY_TOOLBAR
:
386 ShowTransparencyToolbar();
389 case GHK_TRANSPARANCY
:
390 ResetRestoreAllTransparency();
393 #ifdef ENABLE_NETWORK
394 case GHK_CHAT
: // smart chat; send to team if any, otherwise to all
396 const NetworkClientInfo
*cio
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
397 if (cio
== NULL
) break;
399 ShowNetworkChatQueryWindow(NetworkClientPreferTeamChat(cio
) ? DESTTYPE_TEAM
: DESTTYPE_BROADCAST
, cio
->client_playas
);
403 case GHK_CHAT_ALL
: // send text message to all clients
404 if (_networking
) ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST
, 0);
407 case GHK_CHAT_COMPANY
: // send text to all team mates
409 const NetworkClientInfo
*cio
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
410 if (cio
== NULL
) break;
412 ShowNetworkChatQueryWindow(DESTTYPE_TEAM
, cio
->client_playas
);
416 case GHK_CHAT_SERVER
: // send text to the server
417 if (_networking
&& !_network_server
) {
418 ShowNetworkChatQueryWindow(DESTTYPE_CLIENT
, CLIENT_ID_SERVER
);
423 default: return ES_NOT_HANDLED
;
428 virtual void OnScroll(Point delta
)
430 this->viewport
->scrollpos_x
+= ScaleByZoom(delta
.x
, this->viewport
->zoom
);
431 this->viewport
->scrollpos_y
+= ScaleByZoom(delta
.y
, this->viewport
->zoom
);
432 this->viewport
->dest_scrollpos_x
= this->viewport
->scrollpos_x
;
433 this->viewport
->dest_scrollpos_y
= this->viewport
->scrollpos_y
;
434 this->refresh
= LINKGRAPH_DELAY
;
437 virtual void OnMouseWheel(int wheel
)
439 if (_settings_client
.gui
.scrollwheel_scrolling
== 0) {
440 ZoomInOrOutToCursorWindow(wheel
< 0, this);
444 virtual void OnResize()
446 if (this->viewport
!= NULL
) {
447 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_M_VIEWPORT
);
448 nvp
->UpdateViewportCoordinates(this);
449 this->refresh
= LINKGRAPH_DELAY
;
454 * Some data on this window has become invalid.
455 * @param data Information about the changed data.
456 * @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.
458 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
460 if (!gui_scope
) return;
461 /* Forward the message to the appropriate toolbar (ingame or scenario editor) */
462 InvalidateWindowData(WC_MAIN_TOOLBAR
, 0, data
, true);
465 static HotkeyList hotkeys
;
468 const uint16 _ghk_quit_keys
[] = {'Q' | WKC_CTRL
, 'Q' | WKC_META
, 0};
469 const uint16 _ghk_abandon_keys
[] = {'W' | WKC_CTRL
, 'W' | WKC_META
, 0};
470 const uint16 _ghk_chat_keys
[] = {WKC_RETURN
, 'T', 0};
471 const uint16 _ghk_chat_all_keys
[] = {WKC_SHIFT
| WKC_RETURN
, WKC_SHIFT
| 'T', 0};
472 const uint16 _ghk_chat_company_keys
[] = {WKC_CTRL
| WKC_RETURN
, WKC_CTRL
| 'T', 0};
473 const uint16 _ghk_chat_server_keys
[] = {WKC_CTRL
| WKC_SHIFT
| WKC_RETURN
, WKC_CTRL
| WKC_SHIFT
| 'T', 0};
475 static Hotkey global_hotkeys
[] = {
476 Hotkey(_ghk_quit_keys
, "quit", GHK_QUIT
),
477 Hotkey(_ghk_abandon_keys
, "abandon", GHK_ABANDON
),
478 Hotkey(WKC_BACKQUOTE
, "console", GHK_CONSOLE
),
479 Hotkey('B' | WKC_CTRL
, "bounding_boxes", GHK_BOUNDING_BOXES
),
480 Hotkey('I' | WKC_CTRL
, "dirty_blocks", GHK_DIRTY_BLOCKS
),
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('R' | WKC_CTRL
, "refresh_screen", GHK_REFRESH_SCREEN
),
488 Hotkey('0' | WKC_ALT
, "crash_game", GHK_CRASH
),
489 Hotkey('1' | WKC_ALT
, "money", GHK_MONEY
),
490 Hotkey('2' | WKC_ALT
, "update_coordinates", GHK_UPDATE_COORDS
),
492 Hotkey('1' | WKC_CTRL
, "transparency_signs", GHK_TOGGLE_TRANSPARENCY
),
493 Hotkey('2' | WKC_CTRL
, "transparency_trees", GHK_TOGGLE_TRANSPARENCY
+ 1),
494 Hotkey('3' | WKC_CTRL
, "transparency_houses", GHK_TOGGLE_TRANSPARENCY
+ 2),
495 Hotkey('4' | WKC_CTRL
, "transparency_industries", GHK_TOGGLE_TRANSPARENCY
+ 3),
496 Hotkey('5' | WKC_CTRL
, "transparency_buildings", GHK_TOGGLE_TRANSPARENCY
+ 4),
497 Hotkey('6' | WKC_CTRL
, "transparency_bridges", GHK_TOGGLE_TRANSPARENCY
+ 5),
498 Hotkey('7' | WKC_CTRL
, "transparency_structures", GHK_TOGGLE_TRANSPARENCY
+ 6),
499 Hotkey('8' | WKC_CTRL
, "transparency_catenary", GHK_TOGGLE_TRANSPARENCY
+ 7),
500 Hotkey('9' | WKC_CTRL
, "transparency_loading", GHK_TOGGLE_TRANSPARENCY
+ 8),
501 Hotkey('1' | WKC_CTRL
| WKC_SHIFT
, "invisibility_signs", GHK_TOGGLE_INVISIBILITY
),
502 Hotkey('2' | WKC_CTRL
| WKC_SHIFT
, "invisibility_trees", GHK_TOGGLE_INVISIBILITY
+ 1),
503 Hotkey('3' | WKC_CTRL
| WKC_SHIFT
, "invisibility_houses", GHK_TOGGLE_INVISIBILITY
+ 2),
504 Hotkey('4' | WKC_CTRL
| WKC_SHIFT
, "invisibility_industries", GHK_TOGGLE_INVISIBILITY
+ 3),
505 Hotkey('5' | WKC_CTRL
| WKC_SHIFT
, "invisibility_buildings", GHK_TOGGLE_INVISIBILITY
+ 4),
506 Hotkey('6' | WKC_CTRL
| WKC_SHIFT
, "invisibility_bridges", GHK_TOGGLE_INVISIBILITY
+ 5),
507 Hotkey('7' | WKC_CTRL
| WKC_SHIFT
, "invisibility_structures", GHK_TOGGLE_INVISIBILITY
+ 6),
508 Hotkey('8' | WKC_CTRL
| WKC_SHIFT
, "invisibility_catenary", GHK_TOGGLE_INVISIBILITY
+ 7),
509 Hotkey('X' | WKC_CTRL
, "transparency_toolbar", GHK_TRANSPARENCY_TOOLBAR
),
510 Hotkey('X', "toggle_transparency", GHK_TRANSPARANCY
),
511 #ifdef ENABLE_NETWORK
512 Hotkey(_ghk_chat_keys
, "chat", GHK_CHAT
),
513 Hotkey(_ghk_chat_all_keys
, "chat_all", GHK_CHAT_ALL
),
514 Hotkey(_ghk_chat_company_keys
, "chat_company", GHK_CHAT_COMPANY
),
515 Hotkey(_ghk_chat_server_keys
, "chat_server", GHK_CHAT_SERVER
),
519 HotkeyList
MainWindow::hotkeys("global", global_hotkeys
);
521 static WindowDesc
_main_window_desc(
522 WDP_MANUAL
, NULL
, 0, 0,
523 WC_MAIN_WINDOW
, WC_NONE
,
525 _nested_main_window_widgets
, lengthof(_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 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 (uint i
= 0; i
!= 16; i
++) {
549 const byte
*b
= GetNonSprite(PALETTE_RECOLOUR_START
+ i
, ST_RECOLOUR
);
552 memcpy(_colour_gradient
[i
], b
+ 0xC6, sizeof(_colour_gradient
[i
]));
555 new MainWindow(&_main_window_desc
);
557 /* XXX: these are not done */
558 switch (_game_mode
) {
559 default: NOT_REACHED();
561 ShowSelectGameWindow();
572 * Show the vital in-game windows.
574 void ShowVitalWindows()
578 /* Status bad only for normal games */
579 if (_game_mode
== GM_EDITOR
) return;
585 * Size of the application screen changed.
586 * Adapt the game screen-size, re-allocate the open windows, and repaint everything
588 void GameSizeChanged()
590 _cur_resolution
.width
= _screen
.width
;
591 _cur_resolution
.height
= _screen
.height
;
593 RelocateAllWindows(_screen
.width
, _screen
.height
);
594 MarkWholeScreenDirty();