Files @ r28356:5479dee3539c
Branch filter:

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

Patric Stout
Fix: [CI] patch in SHF_COMPRESSED symbol for our Linux Generic binaries (#11651)
  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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r10238:f2215d7dd22b
r9339:78ad2d03d291
r9339:78ad2d03d291
r19406:fd4cdaea6dc7
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r15393:6dc0cb069557
r9339:78ad2d03d291
r9906:d66b7fd4ccb1
r14248:a9050881acd7
r9339:78ad2d03d291
r20640:cf721180dc1a
r27089:0c098eaacf35
r27089:0c098eaacf35
r9339:78ad2d03d291
r18670:f122c356353c
r18670:f122c356353c
r15393:6dc0cb069557
r15393:6dc0cb069557
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r15175:66e0817dc450
r15175:66e0817dc450
r15175:66e0817dc450
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r26344:08d7d1ca3597
r11082:09fb79759019
r27737:728d55b97775
r9906:d66b7fd4ccb1
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r26383:f90b5ba7430b
r26383:f90b5ba7430b
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r26344:08d7d1ca3597
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r9906:d66b7fd4ccb1
r9339:78ad2d03d291
r9339:78ad2d03d291
r19783:2ab821df0877
r27611:a66527e24f21
r27611:a66527e24f21
r9906:d66b7fd4ccb1
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r16666:196251fd14e5
r16665:b1ac123a9a5a
r9339:78ad2d03d291
r9339:78ad2d03d291
r13024:48c81d0b078a
r9339:78ad2d03d291
r9339:78ad2d03d291
r15366:981e1d95560b
r15366:981e1d95560b
r18632:1741a73d3bc6
r15366:981e1d95560b
r9339:78ad2d03d291
r9339:78ad2d03d291
r14074:8dbb71f1a41e
r9339:78ad2d03d291
r9339:78ad2d03d291
r12572:00b2ff8705ed
r18673:e5a4490b411b
r12572:00b2ff8705ed
r12572:00b2ff8705ed
r28078:5e5d6c0447b1
r23607:36c15679007d
r12572:00b2ff8705ed
r12572:00b2ff8705ed
r27861:6269475166e0
r12572:00b2ff8705ed
r12572:00b2ff8705ed
r9339:78ad2d03d291
r9339:78ad2d03d291
r26344:08d7d1ca3597
r15369:4eefcf058ead
r11636:dba682aa2e1a
r9339:78ad2d03d291
r20280:ca1fc41725ff
r9339:78ad2d03d291
r9339:78ad2d03d291
r12572:00b2ff8705ed
r20280:ca1fc41725ff
r12572:00b2ff8705ed
r9339:78ad2d03d291
r9339:78ad2d03d291
r26575:d5725bf82346
r26575:d5725bf82346
r28167:b14ea36d49e5
r26575:d5725bf82346
r26575:d5725bf82346
r26575:d5725bf82346
r27991:0d8a4a945cd0
r9339:78ad2d03d291
r9339:78ad2d03d291
r21436:38079fc3bcd8
r25564:c875d92c537a
r9339:78ad2d03d291
r9339:78ad2d03d291
r17231:8af772e3e4b6
r17231:8af772e3e4b6
r17231:8af772e3e4b6
r17231:8af772e3e4b6
r17231:8af772e3e4b6
r17231:8af772e3e4b6
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r17231:8af772e3e4b6
r17231:8af772e3e4b6
r17231:8af772e3e4b6
r23499:f9bf6ad58697
r9339:78ad2d03d291
r26575:d5725bf82346
r11431:95d135f68fee
r17647:d4b47933b13c
r17229:f5d1ccc6743f
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26575:d5725bf82346
r17230:f11f2dbf200e
r9339:78ad2d03d291
r9339:78ad2d03d291
r16665:b1ac123a9a5a
r9339:78ad2d03d291
r26575:d5725bf82346
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r20645:0d49e31d617a
r20645:0d49e31d617a
r20645:0d49e31d617a
r11636:dba682aa2e1a
r9339:78ad2d03d291
r11081:725ce4237987
r11636:dba682aa2e1a
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r27089:0c098eaacf35
r27089:0c098eaacf35
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r9906:d66b7fd4ccb1
r9906:d66b7fd4ccb1
r27089:0c098eaacf35
r9906:d66b7fd4ccb1
r23499:f9bf6ad58697
r9339:78ad2d03d291
r19407:8e8cbec18948
r9339:78ad2d03d291
r9339:78ad2d03d291
r27942:f7389062d120
r9339:78ad2d03d291
r11081:725ce4237987
r11081:725ce4237987
r11635:5ea732d7710d
r9339:78ad2d03d291
r9339:78ad2d03d291
r11356:ae9277304002
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9906:d66b7fd4ccb1
r17231:8af772e3e4b6
r9339:78ad2d03d291
r9339:78ad2d03d291
r9906:d66b7fd4ccb1
r17231:8af772e3e4b6
r9339:78ad2d03d291
r9339:78ad2d03d291
r9906:d66b7fd4ccb1
r17231:8af772e3e4b6
r9339:78ad2d03d291
r9339:78ad2d03d291
r9906:d66b7fd4ccb1
r17231:8af772e3e4b6
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r14074:8dbb71f1a41e
r16286:6872e2b14b04
r23838:bfeaabaa7b1d
r16286:6872e2b14b04
r25666:da59d4ce1d64
r14074:8dbb71f1a41e
r14074:8dbb71f1a41e
r9339:78ad2d03d291
r23607:36c15679007d
r15608:7b580ec7448a
r15608:7b580ec7448a
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r20124:2d41107f4aeb
r9906:d66b7fd4ccb1
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r15617:9514cd16007b
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r17232:766ead7cedfd
r28353:bfc4ab63f376
r20641:4d98609601c0
r20654:9830ca4c407b
r20641:4d98609601c0
r20641:4d98609601c0
r20641:4d98609601c0
r20641:4d98609601c0
r20641:4d98609601c0
r20641:4d98609601c0
r27869:dc15a02a4e81
r20651:569557ed35c9
r27869:dc15a02a4e81
r20651:569557ed35c9
r20651:569557ed35c9
r23499:f9bf6ad58697
r20643:19ed64eb09ff
r24597:afde5721a3b6
r20643:19ed64eb09ff
r20643:19ed64eb09ff
r20643:19ed64eb09ff
r20643:19ed64eb09ff
r20643:19ed64eb09ff
r23499:f9bf6ad58697
r20652:9222c445efac
r24597:afde5721a3b6
r20652:9222c445efac
r20652:9222c445efac
r27907:1d59f0f346da
r20652:9222c445efac
r20652:9222c445efac
r20652:9222c445efac
r20652:9222c445efac
r20652:9222c445efac
r27288:52957325547f
r20653:cbfc7f1af4ff
r24597:afde5721a3b6
r20653:cbfc7f1af4ff
r27288:52957325547f
r20653:cbfc7f1af4ff
r20653:cbfc7f1af4ff
r20653:cbfc7f1af4ff
r20653:cbfc7f1af4ff
r23499:f9bf6ad58697
r17232:766ead7cedfd
r17232:766ead7cedfd
r17232:766ead7cedfd
r20640:cf721180dc1a
r23914:c5deff3c5b90
r23914:c5deff3c5b90
r23914:c5deff3c5b90
r23914:c5deff3c5b90
r23914:c5deff3c5b90
r27942:f7389062d120
r20640:cf721180dc1a
r21436:38079fc3bcd8
r20640:cf721180dc1a
r9339:78ad2d03d291
r9339:78ad2d03d291
r26344:08d7d1ca3597
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r18632:1741a73d3bc6
r9339:78ad2d03d291
r9339:78ad2d03d291
r26344:08d7d1ca3597
r9906:d66b7fd4ccb1
r25666:da59d4ce1d64
r25666:da59d4ce1d64
r25668:a808c425ce60
r25666:da59d4ce1d64
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r26344:08d7d1ca3597
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r15369:4eefcf058ead
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r15369:4eefcf058ead
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r11528:f35fda7a3ed2
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r25565:6a5de7df7ea1
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r15366:981e1d95560b
r15366:981e1d95560b
r15366:981e1d95560b
r15366:981e1d95560b
r15366:981e1d95560b
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r14074:8dbb71f1a41e
r9339:78ad2d03d291
r14074:8dbb71f1a41e
r9339:78ad2d03d291
r11998:fc4347de05c4
r11998:fc4347de05c4
r11998:fc4347de05c4
r11998:fc4347de05c4
r23607:36c15679007d
r9339:78ad2d03d291
r11998:fc4347de05c4
r27611:a66527e24f21
r27611:a66527e24f21
r27611:a66527e24f21
r11998:fc4347de05c4
r11998:fc4347de05c4
r11998:fc4347de05c4
r9339:78ad2d03d291
r27611:a66527e24f21
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r27611:a66527e24f21
r27611:a66527e24f21
r9339:78ad2d03d291
r18632:1741a73d3bc6
r19784:f0e8694cfa75
r18632:1741a73d3bc6
r19784:f0e8694cfa75
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r10207:a1fc2f2a33db
r9339:78ad2d03d291
r9339:78ad2d03d291
r9339:78ad2d03d291
r11085:c87a330fb4c2
r13060:e57594b0ca84
r9339:78ad2d03d291
r27555:efb2f3b16d4d
r9339:78ad2d03d291
r26344:08d7d1ca3597
r13024:48c81d0b078a
r9339:78ad2d03d291
r16961:63f09ebd622a
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r28074:fffdbfe84aad
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r26344:08d7d1ca3597
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r17149:9460b4a23572
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
r16961:63f09ebd622a
/*
 * 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 console_gui.cpp Handling the GUI of the in-game console. */

#include "stdafx.h"
#include "textbuf_type.h"
#include "window_gui.h"
#include "console_gui.h"
#include "console_internal.h"
#include "window_func.h"
#include "string_func.h"
#include "strings_func.h"
#include "gfx_func.h"
#include "settings_type.h"
#include "console_func.h"
#include "rev.h"
#include "video/video_driver.hpp"
#include "timer/timer.h"
#include "timer/timer_window.h"

#include "widgets/console_widget.h"

#include "table/strings.h"

#include "safeguards.h"

static const uint ICON_HISTORY_SIZE       = 20;
static const uint ICON_RIGHT_BORDERWIDTH  = 10;
static const uint ICON_BOTTOM_BORDERWIDTH = 12;

/**
 * Container for a single line of console output
 */
struct IConsoleLine {
	std::string buffer;     ///< The data to store.
	TextColour colour;      ///< The colour of the line.
	uint16_t time;            ///< The amount of time the line is in the backlog.

	IConsoleLine() : buffer(), colour(TC_BEGIN), time(0)
	{

	}

	/**
	 * Initialize the console line.
	 * @param buffer the data to print.
	 * @param colour the colour of the line.
	 */
	IConsoleLine(std::string buffer, TextColour colour) :
			buffer(std::move(buffer)),
			colour(colour),
			time(0)
	{
	}

	~IConsoleLine()
	{
	}
};

/** The console backlog buffer. Item index 0 is the newest line. */
static std::deque<IConsoleLine> _iconsole_buffer;

static bool TruncateBuffer();


/* ** main console cmd buffer ** */
static Textbuf _iconsole_cmdline(ICON_CMDLN_SIZE);
static std::deque<std::string> _iconsole_history;
static ptrdiff_t _iconsole_historypos;
IConsoleModes _iconsole_mode;

/* *************** *
 *  end of header  *
 * *************** */

static void IConsoleClearCommand()
{
	memset(_iconsole_cmdline.buf, 0, ICON_CMDLN_SIZE);
	_iconsole_cmdline.chars = _iconsole_cmdline.bytes = 1; // only terminating zero
	_iconsole_cmdline.pixels = 0;
	_iconsole_cmdline.caretpos = 0;
	_iconsole_cmdline.caretxoffs = 0;
	SetWindowDirty(WC_CONSOLE, 0);
}

static inline void IConsoleResetHistoryPos()
{
	_iconsole_historypos = -1;
}


static const char *IConsoleHistoryAdd(const char *cmd);
static void IConsoleHistoryNavigate(int direction);

static const struct NWidgetPart _nested_console_window_widgets[] = {
	NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_BACKGROUND), SetResize(1, 1),
};

static WindowDesc _console_window_desc(__FILE__, __LINE__,
	WDP_MANUAL, nullptr, 0, 0,
	WC_CONSOLE, WC_NONE,
	0,
	std::begin(_nested_console_window_widgets), std::end(_nested_console_window_widgets)
);

struct IConsoleWindow : Window
{
	static size_t scroll;
	int line_height;   ///< Height of one line of text in the console.
	int line_offset;

	IConsoleWindow() : Window(&_console_window_desc)
	{
		_iconsole_mode = ICONSOLE_OPENED;

		this->InitNested(0);
		ResizeWindow(this, _screen.width, _screen.height / 3);
	}

	void OnInit() override
	{
		this->line_height = GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.hsep_normal;
		this->line_offset = GetStringBoundingBox("] ").width + WidgetDimensions::scaled.frametext.left;
	}

	void Close([[maybe_unused]] int data = 0) override
	{
		_iconsole_mode = ICONSOLE_CLOSED;
		VideoDriver::GetInstance()->EditBoxLostFocus();
		this->Window::Close();
	}

	/**
	 * Scroll the content of the console.
	 * @param amount Number of lines to scroll back.
	 */
	void Scroll(int amount)
	{
		if (amount < 0) {
			size_t namount = (size_t) -amount;
			IConsoleWindow::scroll = (namount > IConsoleWindow::scroll) ? 0 : IConsoleWindow::scroll - namount;
		} else {
			assert(this->height >= 0 && this->line_height > 0);
			size_t visible_lines = (size_t)(this->height / this->line_height);
			size_t max_scroll = (visible_lines > _iconsole_buffer.size()) ? 0 : _iconsole_buffer.size() + 1 - visible_lines;
			IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll + amount, max_scroll);
		}
		this->SetDirty();
	}

	void OnPaint() override
	{
		const int right = this->width - WidgetDimensions::scaled.frametext.right;

		GfxFillRect(0, 0, this->width - 1, this->height - 1, PC_BLACK);
		int ypos = this->height - this->line_height;
		for (size_t line_index = IConsoleWindow::scroll; line_index < _iconsole_buffer.size(); line_index++) {
			const IConsoleLine &print = _iconsole_buffer[line_index];
			SetDParamStr(0, print.buffer);
			ypos = DrawStringMultiLine(WidgetDimensions::scaled.frametext.left, right, -this->line_height, ypos, STR_JUST_RAW_STRING, print.colour, SA_LEFT | SA_BOTTOM | SA_FORCE) - WidgetDimensions::scaled.hsep_normal;
			if (ypos < 0) break;
		}
		/* If the text is longer than the window, don't show the starting ']' */
		int delta = this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH;
		if (delta > 0) {
			DrawString(WidgetDimensions::scaled.frametext.left, right, this->height - this->line_height, "]", (TextColour)CC_COMMAND, SA_LEFT | SA_FORCE);
			delta = 0;
		}

		/* If we have a marked area, draw a background highlight. */
		if (_iconsole_cmdline.marklength != 0) GfxFillRect(this->line_offset + delta + _iconsole_cmdline.markxoffs, this->height - this->line_height, this->line_offset + delta + _iconsole_cmdline.markxoffs + _iconsole_cmdline.marklength, this->height - 1, PC_DARK_RED);

		DrawString(this->line_offset + delta, right, this->height - this->line_height, _iconsole_cmdline.buf, (TextColour)CC_COMMAND, SA_LEFT | SA_FORCE);

		if (_focused_window == this && _iconsole_cmdline.caret) {
			DrawString(this->line_offset + delta + _iconsole_cmdline.caretxoffs, right, this->height - this->line_height, "_", TC_WHITE, SA_LEFT | SA_FORCE);
		}
	}

	/** Check on a regular interval if the console buffer needs truncating. */
	IntervalTimer<TimerWindow> truncate_interval = {std::chrono::seconds(3), [this](auto) {
		assert(this->height >= 0 && this->line_height > 0);
		size_t visible_lines = (size_t)(this->height / this->line_height);

		if (TruncateBuffer() && IConsoleWindow::scroll + visible_lines > _iconsole_buffer.size()) {
			size_t max_scroll = (visible_lines > _iconsole_buffer.size()) ? 0 : _iconsole_buffer.size() + 1 - visible_lines;
			IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll, max_scroll);
			this->SetDirty();
		}
	}};

	void OnMouseLoop() override
	{
		if (_iconsole_cmdline.HandleCaret()) this->SetDirty();
	}

	EventState OnKeyPress([[maybe_unused]] char32_t key, uint16_t keycode) override
	{
		if (_focused_window != this) return ES_NOT_HANDLED;

		const int scroll_height = (this->height / this->line_height) - 1;
		switch (keycode) {
			case WKC_UP:
				IConsoleHistoryNavigate(1);
				this->SetDirty();
				break;

			case WKC_DOWN:
				IConsoleHistoryNavigate(-1);
				this->SetDirty();
				break;

			case WKC_SHIFT | WKC_PAGEDOWN:
				this->Scroll(-scroll_height);
				break;

			case WKC_SHIFT | WKC_PAGEUP:
				this->Scroll(scroll_height);
				break;

			case WKC_SHIFT | WKC_DOWN:
				this->Scroll(-1);
				break;

			case WKC_SHIFT | WKC_UP:
				this->Scroll(1);
				break;

			case WKC_BACKQUOTE:
				IConsoleSwitch();
				break;

			case WKC_RETURN: case WKC_NUM_ENTER: {
				/* We always want the ] at the left side; we always force these strings to be left
				 * aligned anyway. So enforce this in all cases by adding a left-to-right marker,
				 * otherwise it will be drawn at the wrong side with right-to-left texts. */
				IConsolePrint(CC_COMMAND, LRM "] {}", _iconsole_cmdline.buf);
				const char *cmd = IConsoleHistoryAdd(_iconsole_cmdline.buf);
				IConsoleClearCommand();

				if (cmd != nullptr) IConsoleCmdExec(cmd);
				break;
			}

			case WKC_CTRL | WKC_RETURN:
				_iconsole_mode = (_iconsole_mode == ICONSOLE_FULL) ? ICONSOLE_OPENED : ICONSOLE_FULL;
				IConsoleResize(this);
				MarkWholeScreenDirty();
				break;

			case (WKC_CTRL | 'L'):
				IConsoleCmdExec("clear");
				break;

			default:
				if (_iconsole_cmdline.HandleKeyPress(key, keycode) != HKPR_NOT_HANDLED) {
					IConsoleWindow::scroll = 0;
					IConsoleResetHistoryPos();
					this->SetDirty();
				} else {
					return ES_NOT_HANDLED;
				}
				break;
		}
		return ES_HANDLED;
	}

	void InsertTextString(WidgetID, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end) override
	{
		if (_iconsole_cmdline.InsertString(str, marked, caret, insert_location, replacement_end)) {
			IConsoleWindow::scroll = 0;
			IConsoleResetHistoryPos();
			this->SetDirty();
		}
	}

	Textbuf *GetFocusedTextbuf() const override
	{
		return &_iconsole_cmdline;
	}

	Point GetCaretPosition() const override
	{
		int delta = std::min<int>(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
		Point pt = {this->line_offset + delta + _iconsole_cmdline.caretxoffs, this->height - this->line_height};

		return pt;
	}

	Rect GetTextBoundingRect(const char *from, const char *to) const override
	{
		int delta = std::min<int>(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);

		Point p1 = GetCharPosInString(_iconsole_cmdline.buf, from, FS_NORMAL);
		Point p2 = from != to ? GetCharPosInString(_iconsole_cmdline.buf, to, FS_NORMAL) : p1;

		Rect r = {this->line_offset + delta + p1.x, this->height - this->line_height, this->line_offset + delta + p2.x, this->height};
		return r;
	}

	ptrdiff_t GetTextCharacterAtPosition(const Point &pt) const override
	{
		int delta = std::min<int>(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);

		if (!IsInsideMM(pt.y, this->height - this->line_height, this->height)) return -1;

		return GetCharAtPosition(_iconsole_cmdline.buf, pt.x - delta);
	}

	void OnMouseWheel(int wheel) override
	{
		this->Scroll(-wheel);
	}

	void OnFocus() override
	{
		VideoDriver::GetInstance()->EditBoxGainedFocus();
	}

	void OnFocusLost(bool) override
	{
		VideoDriver::GetInstance()->EditBoxLostFocus();
	}
};

size_t IConsoleWindow::scroll = 0;

void IConsoleGUIInit()
{
	IConsoleResetHistoryPos();
	_iconsole_mode = ICONSOLE_CLOSED;

	IConsoleClearBuffer();

	IConsolePrint(TC_LIGHT_BLUE, "OpenTTD Game Console Revision 7 - {}", _openttd_revision);
	IConsolePrint(CC_WHITE, "------------------------------------");
	IConsolePrint(CC_WHITE, "use \"help\" for more information.");
	IConsolePrint(CC_WHITE, "");
	IConsoleClearCommand();
}

void IConsoleClearBuffer()
{
	_iconsole_buffer.clear();
}

void IConsoleGUIFree()
{
	IConsoleClearBuffer();
}

/** Change the size of the in-game console window after the screen size changed, or the window state changed. */
void IConsoleResize(Window *w)
{
	switch (_iconsole_mode) {
		case ICONSOLE_OPENED:
			w->height = _screen.height / 3;
			w->width = _screen.width;
			break;
		case ICONSOLE_FULL:
			w->height = _screen.height - ICON_BOTTOM_BORDERWIDTH;
			w->width = _screen.width;
			break;
		default: return;
	}

	MarkWholeScreenDirty();
}

/** Toggle in-game console between opened and closed. */
void IConsoleSwitch()
{
	switch (_iconsole_mode) {
		case ICONSOLE_CLOSED:
			new IConsoleWindow();
			break;

		case ICONSOLE_OPENED: case ICONSOLE_FULL:
			CloseWindowById(WC_CONSOLE, 0);
			break;
	}

	MarkWholeScreenDirty();
}

/** Close the in-game console. */
void IConsoleClose()
{
	if (_iconsole_mode == ICONSOLE_OPENED) IConsoleSwitch();
}

/**
 * Add the entered line into the history so you can look it back
 * scroll, etc. Put it to the beginning as it is the latest text
 * @param cmd Text to be entered into the 'history'
 * @return the command to execute
 */
static const char *IConsoleHistoryAdd(const char *cmd)
{
	/* Strip all spaces at the begin */
	while (IsWhitespace(*cmd)) cmd++;

	/* Do not put empty command in history */
	if (StrEmpty(cmd)) return nullptr;

	/* Do not put in history if command is same as previous */
	if (_iconsole_history.empty() || _iconsole_history.front() != cmd) {
		_iconsole_history.emplace_front(cmd);
		while (_iconsole_history.size() > ICON_HISTORY_SIZE) _iconsole_history.pop_back();
	}

	/* Reset the history position */
	IConsoleResetHistoryPos();
	return _iconsole_history.front().c_str();
}

/**
 * Navigate Up/Down in the history of typed commands
 * @param direction Go further back in history (+1), go to recently typed commands (-1)
 */
static void IConsoleHistoryNavigate(int direction)
{
	if (_iconsole_history.empty()) return; // Empty history
	_iconsole_historypos = Clamp<ptrdiff_t>(_iconsole_historypos + direction, -1, _iconsole_history.size() - 1);

	if (_iconsole_historypos == -1) {
		_iconsole_cmdline.DeleteAll();
	} else {
		_iconsole_cmdline.Assign(_iconsole_history[_iconsole_historypos]);
	}
}

/**
 * Handle the printing of text entered into the console or redirected there
 * by any other means. Text can be redirected to other clients in a network game
 * as well as to a logfile. If the network server is a dedicated server, all activities
 * are also logged. All lines to print are added to a temporary buffer which can be
 * used as a history to print them onscreen
 * @param colour_code the colour of the command. Red in case of errors, etc.
 * @param str the message entered or output on the console (notice, error, etc.)
 */
void IConsoleGUIPrint(TextColour colour_code, const std::string &str)
{
	_iconsole_buffer.push_front(IConsoleLine(str, colour_code));
	SetWindowDirty(WC_CONSOLE, 0);
}

/**
 * Remove old lines from the backlog buffer.
 * The buffer is limited by a maximum size and a minimum age. Every time truncation runs,
 * all lines in the buffer are aged by one. When a line exceeds both the maximum position
 * and also the maximum age, it gets removed.
 * @return true if any lines were removed
 */
static bool TruncateBuffer()
{
	bool need_truncation = false;
	size_t count = 0;
	for (IConsoleLine &line : _iconsole_buffer) {
		count++;
		line.time++;
		if (line.time > _settings_client.gui.console_backlog_timeout && count > _settings_client.gui.console_backlog_length) {
			/* Any messages after this are older and need to be truncated */
			need_truncation = true;
			break;
		}
	}

	if (need_truncation) {
		_iconsole_buffer.resize(count - 1);
	}

	return need_truncation;
}


/**
 * Check whether the given TextColour is valid for console usage.
 * @param c The text colour to compare to.
 * @return true iff the TextColour is valid for console usage.
 */
bool IsValidConsoleColour(TextColour c)
{
	/* A normal text colour is used. */
	if (!(c & TC_IS_PALETTE_COLOUR)) return TC_BEGIN <= c && c < TC_END;

	/* A text colour from the palette is used; must be the company
	 * colour gradient, so it must be one of those. */
	c &= ~TC_IS_PALETTE_COLOUR;
	for (uint i = COLOUR_BEGIN; i < COLOUR_END; i++) {
		if (_colour_gradient[i][4] == c) return true;
	}

	return false;
}