Files @ r26179:ad5479cbfaa1
Branch filter:

Location: cpp/openttd-patchpack/source/src/bridge_gui.cpp - annotation

dP
Change: Deliver cargo to the closest industry first (#9536)
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r6117:d11b4c5c0aea
r5584:545d748cc681
r5584:545d748cc681
r18617:524f2e0f54dc
r8116:df67d3c5e4fd
r15236:52b8c9d62ef9
r23698:1872cc5b7dd7
r8114:866ed489ed98
r8131:7a50db7be0ff
r8157:cf41aa22cd09
r8224:194097dc7288
r8398:cf5cc32ab397
r9283:61eea035568a
r9292:3e908df09326
r14248:a9050881acd7
r17030:fe74bc0da021
r17030:fe74bc0da021
r26102:502baaa2877d
r5584:545d748cc681
r18670:f122c356353c
r18670:f122c356353c
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r9283:61eea035568a
r9283:61eea035568a
r9283:61eea035568a
r9283:61eea035568a
r9283:61eea035568a
r9283:61eea035568a
r9283:61eea035568a
r9283:61eea035568a
r8020:7fa639e03dbc
r16534:df4e8f1343d2
r5584:545d748cc681
r9283:61eea035568a
r9283:61eea035568a
r9283:61eea035568a
r16531:d1a9e93ee01b
r26097:38be59df2287
r17030:fe74bc0da021
r26122:02442b0744ee
r26122:02442b0744ee
r9283:61eea035568a
r26122:02442b0744ee
r5584:545d748cc681
r17030:fe74bc0da021
r24858:bfbc5d118b1d
r17030:fe74bc0da021
r17030:fe74bc0da021
r17030:fe74bc0da021
r17030:fe74bc0da021
r17030:fe74bc0da021
r26114:91e053fa2e46
r26114:91e053fa2e46
r17030:fe74bc0da021
r5584:545d748cc681
r5584:545d748cc681
r16534:df4e8f1343d2
r9283:61eea035568a
r9283:61eea035568a
r9367:77824f88e31c
r16534:df4e8f1343d2
r8020:7fa639e03dbc
r9367:77824f88e31c
r9367:77824f88e31c
r11371:4a7632225c6c
r9367:77824f88e31c
r9367:77824f88e31c
r9283:61eea035568a
r9283:61eea035568a
r26114:91e053fa2e46
r26114:91e053fa2e46
r9292:3e908df09326
r18673:e5a4490b411b
r15769:4a935f419828
r9283:61eea035568a
r9367:77824f88e31c
r23613:a3dc001d74fb
r9367:77824f88e31c
r23613:a3dc001d74fb
r9367:77824f88e31c
r9367:77824f88e31c
r9367:77824f88e31c
r23613:a3dc001d74fb
r9367:77824f88e31c
r23613:a3dc001d74fb
r9367:77824f88e31c
r9367:77824f88e31c
r9367:77824f88e31c
r23613:a3dc001d74fb
r9367:77824f88e31c
r23613:a3dc001d74fb
r9367:77824f88e31c
r9367:77824f88e31c
r9283:61eea035568a
r9283:61eea035568a
r26114:91e053fa2e46
r23530:3c94d05f840c
r23530:3c94d05f840c
r10548:3b5822c2c754
r10548:3b5822c2c754
r26103:316b73a1be08
r26114:91e053fa2e46
r9283:61eea035568a
r8020:7fa639e03dbc
r9292:3e908df09326
r9292:3e908df09326
r9292:3e908df09326
r9367:77824f88e31c
r9292:3e908df09326
r9292:3e908df09326
r18673:e5a4490b411b
r9292:3e908df09326
r9292:3e908df09326
r18673:e5a4490b411b
r18673:e5a4490b411b
r9292:3e908df09326
r9292:3e908df09326
r9283:61eea035568a
r26114:91e053fa2e46
r9283:61eea035568a
r9283:61eea035568a
r26114:91e053fa2e46
r26114:91e053fa2e46
r9283:61eea035568a
r9283:61eea035568a
r20280:ca1fc41725ff
r18673:e5a4490b411b
r12601:4fa7d61dc1cf
r26114:91e053fa2e46
r26114:91e053fa2e46
r12601:4fa7d61dc1cf
r26114:91e053fa2e46
r9367:77824f88e31c
r9367:77824f88e31c
r9480:75fa3be16747
r9292:3e908df09326
r5584:545d748cc681
r23553:9f01781fc43d
r9283:61eea035568a
r9283:61eea035568a
r9283:61eea035568a
r9283:61eea035568a
r9367:77824f88e31c
r9367:77824f88e31c
r9283:61eea035568a
r9283:61eea035568a
r5584:545d748cc681
r23499:f9bf6ad58697
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r18673:e5a4490b411b
r13062:680961bd134a
r21814:76dbe7ca0bbf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r18673:e5a4490b411b
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r18673:e5a4490b411b
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r23520:20bbc807b0eb
r23530:3c94d05f840c
r12601:4fa7d61dc1cf
r9292:3e908df09326
r23530:3c94d05f840c
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r16928:92e564454489
r12601:4fa7d61dc1cf
r12602:1c9d00727f7d
r12602:1c9d00727f7d
r24597:afde5721a3b6
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r9283:61eea035568a
r23499:f9bf6ad58697
r16717:227b400c4b5b
r16717:227b400c4b5b
r18673:e5a4490b411b
r16717:227b400c4b5b
r16717:227b400c4b5b
r16717:227b400c4b5b
r16717:227b400c4b5b
r16717:227b400c4b5b
r16717:227b400c4b5b
r23499:f9bf6ad58697
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r18673:e5a4490b411b
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r9292:3e908df09326
r18673:e5a4490b411b
r12602:1c9d00727f7d
r23520:20bbc807b0eb
r23530:3c94d05f840c
r12601:4fa7d61dc1cf
r23530:3c94d05f840c
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12602:1c9d00727f7d
r16928:92e564454489
r16928:92e564454489
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r12601:4fa7d61dc1cf
r5584:545d748cc681
r9283:61eea035568a
r5584:545d748cc681
r23499:f9bf6ad58697
r9283:61eea035568a
r9283:61eea035568a
r23520:20bbc807b0eb
r9283:61eea035568a
r9283:61eea035568a
r25564:c875d92c537a
r9285:acbf930223e4
r9283:61eea035568a
r9285:acbf930223e4
r9283:61eea035568a
r9283:61eea035568a
r23499:f9bf6ad58697
r9283:61eea035568a
r9292:3e908df09326
r9292:3e908df09326
r18673:e5a4490b411b
r18673:e5a4490b411b
r23520:20bbc807b0eb
r15553:7db2c5732f29
r25564:c875d92c537a
r7983:df865a15383f
r15608:7b580ec7448a
r15608:7b580ec7448a
r9292:3e908df09326
r18673:e5a4490b411b
r9367:77824f88e31c
r9367:77824f88e31c
r9292:3e908df09326
r9292:3e908df09326
r18673:e5a4490b411b
r18673:e5a4490b411b
r9292:3e908df09326
r9292:3e908df09326
r9292:3e908df09326
r9292:3e908df09326
r23499:f9bf6ad58697
r9292:3e908df09326
r18673:e5a4490b411b
r9367:77824f88e31c
r9292:3e908df09326
r9292:3e908df09326
r9283:61eea035568a
r9283:61eea035568a
r7983:df865a15383f
r23499:f9bf6ad58697
r9283:61eea035568a
r18673:e5a4490b411b
r5584:545d748cc681
r9283:61eea035568a
r9283:61eea035568a
r16534:df4e8f1343d2
r16717:227b400c4b5b
r9367:77824f88e31c
r16534:df4e8f1343d2
r10647:62911ec68e89
r9367:77824f88e31c
r9367:77824f88e31c
r9367:77824f88e31c
r9367:77824f88e31c
r9367:77824f88e31c
r16534:df4e8f1343d2
r9367:77824f88e31c
r9367:77824f88e31c
r12474:6d6f8d461c5d
r9367:77824f88e31c
r9367:77824f88e31c
r9367:77824f88e31c
r5584:545d748cc681
r16534:df4e8f1343d2
r11457:9a04ad49bedb
r11457:9a04ad49bedb
r11457:9a04ad49bedb
r13742:180ad925befc
r18673:e5a4490b411b
r20287:5b9a8691769e
r11457:9a04ad49bedb
r12603:73cf823a9613
r11457:9a04ad49bedb
r12603:73cf823a9613
r12603:73cf823a9613
r12603:73cf823a9613
r18673:e5a4490b411b
r18673:e5a4490b411b
r12603:73cf823a9613
r12603:73cf823a9613
r20513:82a59932b307
r12603:73cf823a9613
r12603:73cf823a9613
r12603:73cf823a9613
r11457:9a04ad49bedb
r18673:e5a4490b411b
r13742:180ad925befc
r11457:9a04ad49bedb
r11457:9a04ad49bedb
r11457:9a04ad49bedb
r11457:9a04ad49bedb
r16534:df4e8f1343d2
r20280:ca1fc41725ff
r20283:2a199c78224c
r5584:545d748cc681
r13739:747ed1f003e3
r13554:d1964ead02ee
r11368:058349c3a02c
r5584:545d748cc681
r9283:61eea035568a
r9283:61eea035568a
r9283:61eea035568a
r9283:61eea035568a
r9283:61eea035568a
r13060:e57594b0ca84
r9283:61eea035568a
r9283:61eea035568a
r10140:e2d5edb7fd57
r9283:61eea035568a
r10140:e2d5edb7fd57
r5584:545d748cc681
r25565:6a5de7df7ea1
r5584:545d748cc681
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r19944:25a78576fb5e
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r14944:29599fab3827
r26114:91e053fa2e46
r10548:3b5822c2c754
r10548:3b5822c2c754
r10548:3b5822c2c754
r7983:df865a15383f
r7983:df865a15383f
r7983:df865a15383f
r26114:91e053fa2e46
r5584:545d748cc681
r23607:36c15679007d
r14304:dc9cb5b9e881
r14818:d652bc9ae7bc
r5584:545d748cc681
r10548:3b5822c2c754
r7983:df865a15383f
r5584:545d748cc681
r9365:9bd51de92a64
r9365:9bd51de92a64
r15236:52b8c9d62ef9
r15236:52b8c9d62ef9
r23698:1872cc5b7dd7
r17891:a3152c92ad49
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r23698:1872cc5b7dd7
r17891:a3152c92ad49
r23698:1872cc5b7dd7
r15236:52b8c9d62ef9
r15236:52b8c9d62ef9
r15236:52b8c9d62ef9
r15236:52b8c9d62ef9
r24294:024c3061df55
r24294:024c3061df55
r7983:df865a15383f
r8532:0c31796f7b70
r24294:024c3061df55
r24294:024c3061df55
r7983:df865a15383f
r24434:1f13cf59de8a
r23532:dc91fcd293f5
r23532:dc91fcd293f5
r7983:df865a15383f
r7983:df865a15383f
r23532:dc91fcd293f5
r24294:024c3061df55
r5584:545d748cc681
r5584:545d748cc681
r24294:024c3061df55
r24294:024c3061df55
r24294:024c3061df55
r24294:024c3061df55
r24294:024c3061df55
r5584:545d748cc681
r5584:545d748cc681
r23607:36c15679007d
r26114:91e053fa2e46
r5584:545d748cc681
r11964:7095e7a18762
r14635:a75264d517b7
r5584:545d748cc681
r5584:545d748cc681
/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file bridge_gui.cpp Graphical user interface for bridge construction */

#include "stdafx.h"
#include "error.h"
#include "command_func.h"
#include "rail.h"
#include "road.h"
#include "strings_func.h"
#include "window_func.h"
#include "sound_func.h"
#include "gfx_func.h"
#include "tunnelbridge.h"
#include "sortlist_type.h"
#include "widgets/dropdown_func.h"
#include "core/geometry_func.hpp"
#include "tunnelbridge_map.h"
#include "road_gui.h"
#include "tunnelbridge_cmd.h"

#include "widgets/bridge_widget.h"

#include "table/strings.h"

#include "safeguards.h"

/** The type of the last built rail bridge */
static BridgeType _last_railbridge_type = 0;
/** The type of the last built road bridge */
static BridgeType _last_roadbridge_type = 0;

/**
 * Carriage for the data we need if we want to build a bridge
 */
struct BuildBridgeData {
	BridgeType index;
	const BridgeSpec *spec;
	Money cost;
};

typedef GUIList<BuildBridgeData> GUIBridgeList; ///< List of bridges, used in #BuildBridgeWindow.

/**
 * Callback executed after a build Bridge CMD has been called
 *
 * @param result Whether the build succeeded
 * @param cmd unused
 * @param end_tile End tile of the bridge.
 * @param tile_start start tile
 * @param transport_type transport type.
 */
void CcBuildBridge(Commands cmd, const CommandCost &result, TileIndex end_tile, TileIndex tile_start, TransportType transport_type, BridgeType, byte)
{
	if (result.Failed()) return;
	if (_settings_client.sound.confirm) SndPlayTileFx(SND_27_CONSTRUCTION_BRIDGE, end_tile);

	if (transport_type == TRANSPORT_ROAD) {
		DiagDirection end_direction = ReverseDiagDir(GetTunnelBridgeDirection(end_tile));
		ConnectRoadToStructure(end_tile, end_direction);

		DiagDirection start_direction = ReverseDiagDir(GetTunnelBridgeDirection(tile_start));
		ConnectRoadToStructure(tile_start, start_direction);
	}
}

/** Window class for handling the bridge-build GUI. */
class BuildBridgeWindow : public Window {
private:
	/* Runtime saved values */
	static Listing last_sorting; ///< Last setting of the sort.

	/* Constants for sorting the bridges */
	static const StringID sorter_names[];
	static GUIBridgeList::SortFunction * const sorter_funcs[];

	/* Internal variables */
	TileIndex start_tile;
	TileIndex end_tile;
	TransportType transport_type;
	byte road_rail_type;
	GUIBridgeList *bridges;
	int bridgetext_offset; ///< Horizontal offset of the text describing the bridge properties in #WID_BBS_BRIDGE_LIST relative to the left edge.
	Scrollbar *vscroll;

	/** Sort the bridges by their index */
	static bool BridgeIndexSorter(const BuildBridgeData &a, const BuildBridgeData &b)
	{
		return a.index < b.index;
	}

	/** Sort the bridges by their price */
	static bool BridgePriceSorter(const BuildBridgeData &a, const BuildBridgeData &b)
	{
		return a.cost < b.cost;
	}

	/** Sort the bridges by their maximum speed */
	static bool BridgeSpeedSorter(const BuildBridgeData &a, const BuildBridgeData &b)
	{
		return a.spec->speed < b.spec->speed;
	}

	void BuildBridge(uint8 i)
	{
		switch (this->transport_type) {
			case TRANSPORT_RAIL: _last_railbridge_type = this->bridges->at(i).index; break;
			case TRANSPORT_ROAD: _last_roadbridge_type = this->bridges->at(i).index; break;
			default: break;
		}
		Command<CMD_BUILD_BRIDGE>::Post(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE, CcBuildBridge,
					this->end_tile, this->start_tile, this->transport_type, this->bridges->at(i).index, this->road_rail_type);
	}

	/** Sort the builable bridges */
	void SortBridgeList()
	{
		this->bridges->Sort();

		/* Display the current sort variant */
		this->GetWidget<NWidgetCore>(WID_BBS_DROPDOWN_CRITERIA)->widget_data = this->sorter_names[this->bridges->SortType()];

		/* Set the modified widgets dirty */
		this->SetWidgetDirty(WID_BBS_DROPDOWN_CRITERIA);
		this->SetWidgetDirty(WID_BBS_BRIDGE_LIST);
	}

public:
	BuildBridgeWindow(WindowDesc *desc, TileIndex start, TileIndex end, TransportType transport_type, byte road_rail_type, GUIBridgeList *bl) : Window(desc),
		start_tile(start),
		end_tile(end),
		transport_type(transport_type),
		road_rail_type(road_rail_type),
		bridges(bl)
	{
		this->CreateNestedTree();
		this->vscroll = this->GetScrollbar(WID_BBS_SCROLLBAR);
		/* Change the data, or the caption of the gui. Set it to road or rail, accordingly. */
		this->GetWidget<NWidgetCore>(WID_BBS_CAPTION)->widget_data = (transport_type == TRANSPORT_ROAD) ? STR_SELECT_ROAD_BRIDGE_CAPTION : STR_SELECT_RAIL_BRIDGE_CAPTION;
		this->FinishInitNested(transport_type); // Initializes 'this->bridgetext_offset'.

		this->parent = FindWindowById(WC_BUILD_TOOLBAR, transport_type);
		this->bridges->SetListing(this->last_sorting);
		this->bridges->SetSortFuncs(this->sorter_funcs);
		this->bridges->NeedResort();
		this->SortBridgeList();

		this->vscroll->SetCount((uint)bl->size());
	}

	~BuildBridgeWindow()
	{
		this->last_sorting = this->bridges->GetListing();

		delete bridges;
	}

	void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
	{
		switch (widget) {
			case WID_BBS_DROPDOWN_ORDER: {
				Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
				d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
				d.height += padding.height;
				*size = maxdim(*size, d);
				break;
			}
			case WID_BBS_DROPDOWN_CRITERIA: {
				Dimension d = {0, 0};
				for (const StringID *str = this->sorter_names; *str != INVALID_STRING_ID; str++) {
					d = maxdim(d, GetStringBoundingBox(*str));
				}
				d.width += padding.width;
				d.height += padding.height;
				*size = maxdim(*size, d);
				break;
			}
			case WID_BBS_BRIDGE_LIST: {
				Dimension sprite_dim = {0, 0}; // Biggest bridge sprite dimension
				Dimension text_dim   = {0, 0}; // Biggest text dimension
				for (int i = 0; i < (int)this->bridges->size(); i++) {
					const BridgeSpec *b = this->bridges->at(i).spec;
					sprite_dim = maxdim(sprite_dim, GetSpriteSize(b->sprite));

					SetDParam(2, this->bridges->at(i).cost);
					SetDParam(1, b->speed);
					SetDParam(0, b->material);
					text_dim = maxdim(text_dim, GetStringBoundingBox(_game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_SCENEDIT_INFO : STR_SELECT_BRIDGE_INFO));
				}
				sprite_dim.height++; // Sprite is rendered one pixel down in the matrix field.
				text_dim.height++; // Allowing the bottom row pixels to be rendered on the edge of the matrix field.
				resize->height = std::max(sprite_dim.height, text_dim.height) + 2; // Max of both sizes + account for matrix edges.

				this->bridgetext_offset = WD_MATRIX_LEFT + sprite_dim.width + 1; // Left edge of text, 1 pixel distance from the sprite.
				size->width = this->bridgetext_offset + text_dim.width + WD_MATRIX_RIGHT;
				size->height = 4 * resize->height; // Smallest bridge gui is 4 entries high in the matrix.
				break;
			}
		}
	}

	Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
	{
		/* Position the window so hopefully the first bridge from the list is under the mouse pointer. */
		NWidgetBase *list = this->GetWidget<NWidgetBase>(WID_BBS_BRIDGE_LIST);
		Point corner; // point of the top left corner of the window.
		corner.y = Clamp(_cursor.pos.y - list->pos_y - 5, GetMainViewTop(), GetMainViewBottom() - sm_height);
		corner.x = Clamp(_cursor.pos.x - list->pos_x - 5, 0, _screen.width - sm_width);
		return corner;
	}

	void DrawWidget(const Rect &r, int widget) const override
	{
		switch (widget) {
			case WID_BBS_DROPDOWN_ORDER:
				this->DrawSortButtonState(widget, this->bridges->IsDescSortOrder() ? SBS_DOWN : SBS_UP);
				break;

			case WID_BBS_BRIDGE_LIST: {
				uint y = r.top;
				for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < (int)this->bridges->size(); i++) {
					const BridgeSpec *b = this->bridges->at(i).spec;

					SetDParam(2, this->bridges->at(i).cost);
					SetDParam(1, b->speed);
					SetDParam(0, b->material);

					DrawSprite(b->sprite, b->pal, r.left + WD_MATRIX_LEFT, y + this->resize.step_height - 1 - GetSpriteSize(b->sprite).height);
					DrawStringMultiLine(r.left + this->bridgetext_offset, r.right, y + 2, y + this->resize.step_height,
							_game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_SCENEDIT_INFO : STR_SELECT_BRIDGE_INFO);
					y += this->resize.step_height;
				}
				break;
			}
		}
	}

	EventState OnKeyPress(WChar key, uint16 keycode) override
	{
		const uint8 i = keycode - '1';
		if (i < 9 && i < this->bridges->size()) {
			/* Build the requested bridge */
			this->BuildBridge(i);
			this->Close();
			return ES_HANDLED;
		}
		return ES_NOT_HANDLED;
	}

	void OnClick(Point pt, int widget, int click_count) override
	{
		switch (widget) {
			default: break;
			case WID_BBS_BRIDGE_LIST: {
				uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_BBS_BRIDGE_LIST);
				if (i < this->bridges->size()) {
					this->BuildBridge(i);
					this->Close();
				}
				break;
			}

			case WID_BBS_DROPDOWN_ORDER:
				this->bridges->ToggleSortOrder();
				this->SetDirty();
				break;

			case WID_BBS_DROPDOWN_CRITERIA:
				ShowDropDownMenu(this, this->sorter_names, this->bridges->SortType(), WID_BBS_DROPDOWN_CRITERIA, 0, 0);
				break;
		}
	}

	void OnDropdownSelect(int widget, int index) override
	{
		if (widget == WID_BBS_DROPDOWN_CRITERIA && this->bridges->SortType() != index) {
			this->bridges->SetSortType(index);

			this->SortBridgeList();
		}
	}

	void OnResize() override
	{
		this->vscroll->SetCapacityFromWidget(this, WID_BBS_BRIDGE_LIST);
	}
};

/** Set the default sorting for the bridges */
Listing BuildBridgeWindow::last_sorting = {true, 2};

/** Available bridge sorting functions. */
GUIBridgeList::SortFunction * const BuildBridgeWindow::sorter_funcs[] = {
	&BridgeIndexSorter,
	&BridgePriceSorter,
	&BridgeSpeedSorter
};

/** Names of the sorting functions. */
const StringID BuildBridgeWindow::sorter_names[] = {
	STR_SORT_BY_NUMBER,
	STR_SORT_BY_COST,
	STR_SORT_BY_MAX_SPEED,
	INVALID_STRING_ID
};

/** Widgets of the bridge gui. */
static const NWidgetPart _nested_build_bridge_widgets[] = {
	/* Header */
	NWidget(NWID_HORIZONTAL),
		NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
		NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_BBS_CAPTION), SetDataTip(STR_SELECT_RAIL_BRIDGE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
		NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
	EndContainer(),

	NWidget(NWID_HORIZONTAL),
		NWidget(NWID_VERTICAL),
			/* Sort order + criteria buttons */
			NWidget(NWID_HORIZONTAL),
				NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, WID_BBS_DROPDOWN_ORDER), SetFill(1, 0), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
				NWidget(WWT_DROPDOWN, COLOUR_DARK_GREEN, WID_BBS_DROPDOWN_CRITERIA), SetFill(1, 0), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
			EndContainer(),
			/* Matrix. */
			NWidget(WWT_MATRIX, COLOUR_DARK_GREEN, WID_BBS_BRIDGE_LIST), SetFill(1, 0), SetResize(0, 22), SetMatrixDataTip(1, 0, STR_SELECT_BRIDGE_SELECTION_TOOLTIP), SetScrollbar(WID_BBS_SCROLLBAR),
		EndContainer(),

		/* scrollbar + resize button */
		NWidget(NWID_VERTICAL),
			NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BBS_SCROLLBAR),
			NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
		EndContainer(),
	EndContainer(),
};

/** Window definition for the rail bridge selection window. */
static WindowDesc _build_bridge_desc(
	WDP_AUTO, "build_bridge", 200, 114,
	WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
	WDF_CONSTRUCTION,
	_nested_build_bridge_widgets, lengthof(_nested_build_bridge_widgets)
);

/**
 * Prepare the data for the build a bridge window.
 *  If we can't build a bridge under the given conditions
 *  show an error message.
 *
 * @param start The start tile of the bridge
 * @param end The end tile of the bridge
 * @param transport_type The transport type
 * @param road_rail_type The road/rail type
 */
void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, byte road_rail_type)
{
	CloseWindowByClass(WC_BUILD_BRIDGE);

	/* The bridge length without ramps. */
	const uint bridge_len = GetTunnelBridgeLength(start, end);

	/* If Ctrl is being pressed, check whether the last bridge built is available
	 * If so, return this bridge type. Otherwise continue normally.
	 * We store bridge types for each transport type, so we have to check for
	 * the transport type beforehand.
	 */
	BridgeType last_bridge_type = 0;
	switch (transport_type) {
		case TRANSPORT_ROAD: last_bridge_type = _last_roadbridge_type; break;
		case TRANSPORT_RAIL: last_bridge_type = _last_railbridge_type; break;
		default: break; // water ways and air routes don't have bridge types
	}
	if (_ctrl_pressed && CheckBridgeAvailability(last_bridge_type, bridge_len).Succeeded()) {
		Command<CMD_BUILD_BRIDGE>::Post(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE, CcBuildBridge, end, start, transport_type, last_bridge_type, road_rail_type);
		return;
	}

	/* only query bridge building possibility once, result is the same for all bridges!
	 * returns CMD_ERROR on failure, and price on success */
	StringID errmsg = INVALID_STRING_ID;
	CommandCost ret = Command<CMD_BUILD_BRIDGE>::Do(CommandFlagsToDCFlags(GetCommandFlags<CMD_BUILD_BRIDGE>()) | DC_QUERY_COST, end, start, transport_type, 0, road_rail_type);

	GUIBridgeList *bl = nullptr;
	if (ret.Failed()) {
		errmsg = ret.GetErrorMessage();
	} else {
		/* check which bridges can be built */
		const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);

		bl = new GUIBridgeList();

		Money infra_cost = 0;
		switch (transport_type) {
			case TRANSPORT_ROAD: {
				/* In case we add a new road type as well, we must be aware of those costs. */
				RoadType road_rt = INVALID_ROADTYPE;
				RoadType tram_rt = INVALID_ROADTYPE;
				if (IsBridgeTile(start)) {
					road_rt = GetRoadTypeRoad(start);
					tram_rt = GetRoadTypeTram(start);
				}
				if (RoadTypeIsRoad((RoadType)road_rail_type)) {
					road_rt = (RoadType)road_rail_type;
				} else {
					tram_rt = (RoadType)road_rail_type;
				}

				if (road_rt != INVALID_ROADTYPE) infra_cost += (bridge_len + 2) * 2 * RoadBuildCost(road_rt);
				if (tram_rt != INVALID_ROADTYPE) infra_cost += (bridge_len + 2) * 2 * RoadBuildCost(tram_rt);

				break;
			}
			case TRANSPORT_RAIL: infra_cost = (bridge_len + 2) * RailBuildCost((RailType)road_rail_type); break;
			default: break;
		}

		bool any_available = false;
		CommandCost type_check;
		/* loop for all bridgetypes */
		for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) {
			type_check = CheckBridgeAvailability(brd_type, bridge_len);
			if (type_check.Succeeded()) {
				/* bridge is accepted, add to list */
				BuildBridgeData &item = bl->emplace_back();
				item.index = brd_type;
				item.spec = GetBridgeSpec(brd_type);
				/* Add to terraforming & bulldozing costs the cost of the
				 * bridge itself (not computed with DC_QUERY_COST) */
				item.cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price[PR_BUILD_BRIDGE] * item.spec->price) >> 8) + infra_cost;
				any_available = true;
			}
		}
		/* give error cause if no bridges available here*/
		if (!any_available)
		{
			errmsg = type_check.GetErrorMessage();
		}
	}

	if (bl != nullptr && bl->size() != 0) {
		new BuildBridgeWindow(&_build_bridge_desc, start, end, transport_type, road_rail_type, bl);
	} else {
		delete bl;
		ShowErrorMessage(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE, errmsg, WL_INFO, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
	}
}