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 console_gui.cpp Handling the GUI of the in-game console. */
13 #include "textbuf_type.h"
14 #include "window_gui.h"
15 #include "console_gui.h"
16 #include "console_internal.h"
17 #include "window_func.h"
19 #include "strings_func.h"
21 #include "settings_type.h"
22 #include "console_func.h"
24 #include "video/video_driver.hpp"
26 #include "widgets/console_widget.h"
28 #include "table/strings.h"
30 static const uint ICON_HISTORY_SIZE
= 20;
31 static const uint ICON_LINE_SPACING
= 2;
32 static const uint ICON_RIGHT_BORDERWIDTH
= 10;
33 static const uint ICON_BOTTOM_BORDERWIDTH
= 12;
36 * Container for a single line of console output
39 static IConsoleLine
*front
; ///< The front of the console backlog buffer
40 static int size
; ///< The amount of items in the backlog
42 IConsoleLine
*previous
; ///< The previous console message.
43 char *buffer
; ///< The data to store.
44 TextColour colour
; ///< The colour of the line.
45 uint16 time
; ///< The amount of time the line is in the backlog.
48 * Initialize the console line.
49 * @param buffer the data to print.
50 * @param colour the colour of the line.
52 IConsoleLine(char *buffer
, TextColour colour
) :
53 previous(IConsoleLine::front
),
58 IConsoleLine::front
= this;
63 * Clear this console line and any further ones.
74 * Get the index-ed item in the list.
76 static const IConsoleLine
*Get(uint index
)
78 const IConsoleLine
*item
= IConsoleLine::front
;
79 while (index
!= 0 && item
!= NULL
) {
81 item
= item
->previous
;
88 * Truncate the list removing everything older than/more than the amount
89 * as specified in the config file.
90 * As a side effect also increase the time the other lines have been in
92 * @return true if and only if items got removed.
94 static bool Truncate()
96 IConsoleLine
*cur
= IConsoleLine::front
;
97 if (cur
== NULL
) return false;
100 for (IConsoleLine
*item
= cur
->previous
; item
!= NULL
; count
++, cur
= item
, item
= item
->previous
) {
101 if (item
->time
> _settings_client
.gui
.console_backlog_timeout
&&
102 count
> _settings_client
.gui
.console_backlog_length
) {
104 cur
->previous
= NULL
;
108 if (item
->time
!= MAX_UVALUE(uint16
)) item
->time
++;
115 * Reset the complete console line backlog.
119 delete IConsoleLine::front
;
120 IConsoleLine::front
= NULL
;
121 IConsoleLine::size
= 0;
125 /* static */ IConsoleLine
*IConsoleLine::front
= NULL
;
126 /* static */ int IConsoleLine::size
= 0;
129 /* ** main console cmd buffer ** */
130 static char _iconsole_buffer
[ICON_CMDLN_SIZE
];
131 static Textbuf
_iconsole_cmdline (lengthof(_iconsole_buffer
), _iconsole_buffer
);
132 static char *_iconsole_history
[ICON_HISTORY_SIZE
];
133 static int _iconsole_historypos
;
134 IConsoleModes _iconsole_mode
;
140 static void IConsoleClearCommand()
142 _iconsole_cmdline
.zerofill();
143 _iconsole_cmdline
.chars
= 1; // only terminating zero
144 _iconsole_cmdline
.pixels
= 0;
145 _iconsole_cmdline
.caretpos
= 0;
146 _iconsole_cmdline
.caretxoffs
= 0;
147 SetWindowDirty(WC_CONSOLE
, 0);
150 static inline void IConsoleResetHistoryPos()
152 _iconsole_historypos
= -1;
156 static const char *IConsoleHistoryAdd(const char *cmd
);
157 static void IConsoleHistoryNavigate(int direction
);
159 static const struct NWidgetPart _nested_console_window_widgets
[] = {
160 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_C_BACKGROUND
), SetResize(1, 1),
163 static const WindowDesc
_console_window_desc(
167 _nested_console_window_widgets
, lengthof(_nested_console_window_widgets
)
170 struct IConsoleWindow
: Window
173 int line_height
; ///< Height of one line of text in the console.
176 IConsoleWindow() : Window (&_console_window_desc
),
177 line_height (FONT_HEIGHT_NORMAL
+ ICON_LINE_SPACING
),
178 line_offset (GetStringBoundingBox("] ").width
+ 5)
180 _iconsole_mode
= ICONSOLE_OPENED
;
183 ResizeWindow (this, _screen_width
, _screen_height
/ 3);
186 void OnDelete (void) FINAL_OVERRIDE
188 _iconsole_mode
= ICONSOLE_CLOSED
;
189 VideoDriver::GetActiveDriver()->EditBoxLostFocus();
193 * Scroll the content of the console.
194 * @param amount Number of lines to scroll back.
196 void Scroll(int amount
)
198 int max_scroll
= max
<int>(0, IConsoleLine::size
+ 1 - this->height
/ this->line_height
);
199 IConsoleWindow::scroll
= Clamp
<int>(IConsoleWindow::scroll
+ amount
, 0, max_scroll
);
203 void OnPaint (BlitArea
*dpi
) OVERRIDE
205 const int right
= this->width
- 5;
207 GfxFillRect (dpi
, 0, 0, this->width
- 1, this->height
- 1, PC_BLACK
);
208 int ypos
= this->height
- this->line_height
;
209 for (const IConsoleLine
*print
= IConsoleLine::Get(IConsoleWindow::scroll
); print
!= NULL
; print
= print
->previous
) {
210 SetDParamStr(0, print
->buffer
);
211 ypos
= DrawStringMultiLine (dpi
, 5, right
, -this->line_height
, ypos
, STR_JUST_RAW_STRING
, print
->colour
, SA_LEFT
| SA_BOTTOM
| SA_FORCE
) - ICON_LINE_SPACING
;
214 /* If the text is longer than the window, don't show the starting ']' */
215 int delta
= this->width
- this->line_offset
- _iconsole_cmdline
.pixels
- ICON_RIGHT_BORDERWIDTH
;
217 DrawString (dpi
, 5, right
, this->height
- this->line_height
, "]", (TextColour
)CC_COMMAND
, SA_LEFT
| SA_FORCE
);
221 /* If we have a marked area, draw a background highlight. */
222 if (_iconsole_cmdline
.marklength
!= 0) GfxFillRect (dpi
, 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
);
224 DrawString (dpi
, this->line_offset
+ delta
, right
, this->height
- this->line_height
, _iconsole_cmdline
.GetText(), (TextColour
)CC_COMMAND
, SA_LEFT
| SA_FORCE
);
226 if (_focused_window
== this && _iconsole_cmdline
.caret
) {
227 DrawString (dpi
, this->line_offset
+ delta
+ _iconsole_cmdline
.caretxoffs
, right
, this->height
- this->line_height
, "_", TC_WHITE
, SA_LEFT
| SA_FORCE
);
231 virtual void OnHundredthTick()
233 if (IConsoleLine::Truncate() &&
234 (IConsoleWindow::scroll
> IConsoleLine::size
)) {
235 IConsoleWindow::scroll
= max(0, IConsoleLine::size
- (this->height
/ this->line_height
) + 1);
240 virtual void OnMouseLoop()
242 if (_iconsole_cmdline
.HandleCaret()) this->SetDirty();
245 virtual EventState
OnKeyPress(WChar key
, uint16 keycode
)
247 if (_focused_window
!= this) return ES_NOT_HANDLED
;
249 const int scroll_height
= (this->height
/ this->line_height
) - 1;
252 IConsoleHistoryNavigate(1);
257 IConsoleHistoryNavigate(-1);
261 case WKC_SHIFT
| WKC_PAGEDOWN
:
262 this->Scroll(-scroll_height
);
265 case WKC_SHIFT
| WKC_PAGEUP
:
266 this->Scroll(scroll_height
);
269 case WKC_SHIFT
| WKC_DOWN
:
273 case WKC_SHIFT
| WKC_UP
:
281 case WKC_RETURN
: case WKC_NUM_ENTER
: {
282 /* We always want the ] at the left side; we always force these strings to be left
283 * aligned anyway. So enforce this in all cases by addding a left-to-right marker,
284 * otherwise it will be drawn at the wrong side with right-to-left texts. */
285 IConsolePrintF(CC_COMMAND
, LRM
"] %s", _iconsole_cmdline
.GetText());
286 const char *cmd
= IConsoleHistoryAdd(_iconsole_cmdline
.GetText());
287 IConsoleClearCommand();
289 if (cmd
!= NULL
) IConsoleCmdExec(cmd
);
293 case WKC_CTRL
| WKC_RETURN
:
294 _iconsole_mode
= (_iconsole_mode
== ICONSOLE_FULL
) ? ICONSOLE_OPENED
: ICONSOLE_FULL
;
295 IConsoleResize(this);
296 MarkWholeScreenDirty();
299 case (WKC_CTRL
| 'L'):
300 IConsoleCmdExec("clear");
304 if (_iconsole_cmdline
.HandleKeyPress(key
, keycode
) != HKPR_NOT_HANDLED
) {
305 IConsoleWindow::scroll
= 0;
306 IConsoleResetHistoryPos();
309 return ES_NOT_HANDLED
;
316 virtual void InsertTextString(int wid
, const char *str
, bool marked
, const char *caret
, const char *insert_location
, const char *replacement_end
)
318 if (_iconsole_cmdline
.InsertString(str
, marked
, caret
, insert_location
, replacement_end
)) {
319 IConsoleWindow::scroll
= 0;
320 IConsoleResetHistoryPos();
325 virtual const char *GetFocusedText() const
327 return _iconsole_cmdline
.GetText();
330 virtual const char *GetCaret() const
332 return _iconsole_cmdline
.GetCaret();
335 virtual const char *GetMarkedText(size_t *length
) const
337 return _iconsole_cmdline
.GetMarkedText (length
);
340 virtual Point
GetCaretPosition() const
342 int delta
= min(this->width
- this->line_offset
- _iconsole_cmdline
.pixels
- ICON_RIGHT_BORDERWIDTH
, 0);
343 Point pt
= {this->line_offset
+ delta
+ _iconsole_cmdline
.caretxoffs
, this->height
- this->line_height
};
348 virtual Rect
GetTextBoundingRect(const char *from
, const char *to
) const
350 int delta
= min(this->width
- this->line_offset
- _iconsole_cmdline
.pixels
- ICON_RIGHT_BORDERWIDTH
, 0);
353 _iconsole_cmdline
.GetCharPositions (from
, &x1
, to
, &x2
);
355 Rect r
= { this->line_offset
+ delta
+ x1
, this->height
- this->line_height
, this->line_offset
+ delta
+ x2
, this->height
};
359 virtual const char *GetTextCharacterAtPosition(const Point
&pt
) const
361 int delta
= min(this->width
- this->line_offset
- _iconsole_cmdline
.pixels
- ICON_RIGHT_BORDERWIDTH
, 0);
363 if (!IsInsideMM(pt
.y
, this->height
- this->line_height
, this->height
)) return NULL
;
365 return _iconsole_cmdline
.GetCharAtPosition (pt
.x
- delta
);
368 virtual void OnMouseWheel(int wheel
)
370 this->Scroll(-wheel
);
373 virtual void OnFocusLost()
375 VideoDriver::GetActiveDriver()->EditBoxLostFocus();
379 int IConsoleWindow::scroll
= 0;
381 void IConsoleGUIInit()
383 IConsoleResetHistoryPos();
384 _iconsole_mode
= ICONSOLE_CLOSED
;
386 IConsoleLine::Reset();
387 memset(_iconsole_history
, 0, sizeof(_iconsole_history
));
389 IConsolePrintF(CC_WARNING
, "OpenTTD Game Console Revision 7 - %s", _openttd_revision
);
390 IConsolePrint(CC_WHITE
, "------------------------------------");
391 IConsolePrint(CC_WHITE
, "use \"help\" for more information");
392 IConsolePrint(CC_WHITE
, "");
393 IConsoleClearCommand();
396 void IConsoleClearBuffer()
398 IConsoleLine::Reset();
401 void IConsoleGUIFree()
403 IConsoleClearBuffer();
406 /** Change the size of the in-game console window after the screen size changed, or the window state changed. */
407 void IConsoleResize(Window
*w
)
409 switch (_iconsole_mode
) {
410 case ICONSOLE_OPENED
:
411 w
->height
= _screen_height
/ 3;
412 w
->width
= _screen_width
;
415 w
->height
= _screen_height
- ICON_BOTTOM_BORDERWIDTH
;
416 w
->width
= _screen_width
;
421 MarkWholeScreenDirty();
424 /** Toggle in-game console between opened and closed. */
425 void IConsoleSwitch()
427 switch (_iconsole_mode
) {
428 case ICONSOLE_CLOSED
:
429 new IConsoleWindow();
432 case ICONSOLE_OPENED
: case ICONSOLE_FULL
:
433 DeleteWindowById(WC_CONSOLE
, 0);
437 MarkWholeScreenDirty();
440 /** Close the in-game console. */
443 if (_iconsole_mode
== ICONSOLE_OPENED
) IConsoleSwitch();
447 * Add the entered line into the history so you can look it back
448 * scroll, etc. Put it to the beginning as it is the latest text
449 * @param cmd Text to be entered into the 'history'
450 * @return the command to execute
452 static const char *IConsoleHistoryAdd(const char *cmd
)
454 /* Strip all spaces at the begin */
455 while (IsWhitespace(*cmd
)) cmd
++;
457 /* Do not put empty command in history */
458 if (StrEmpty(cmd
)) return NULL
;
460 /* Do not put in history if command is same as previous */
461 if (_iconsole_history
[0] == NULL
|| strcmp(_iconsole_history
[0], cmd
) != 0) {
462 free(_iconsole_history
[ICON_HISTORY_SIZE
- 1]);
463 memmove(&_iconsole_history
[1], &_iconsole_history
[0], sizeof(_iconsole_history
[0]) * (ICON_HISTORY_SIZE
- 1));
464 _iconsole_history
[0] = xstrdup(cmd
);
467 /* Reset the history position */
468 IConsoleResetHistoryPos();
469 return _iconsole_history
[0];
473 * Navigate Up/Down in the history of typed commands
474 * @param direction Go further back in history (+1), go to recently typed commands (-1)
476 static void IConsoleHistoryNavigate(int direction
)
478 if (_iconsole_history
[0] == NULL
) return; // Empty history
479 _iconsole_historypos
= Clamp(_iconsole_historypos
+ direction
, -1, ICON_HISTORY_SIZE
- 1);
481 if (direction
> 0 && _iconsole_history
[_iconsole_historypos
] == NULL
) _iconsole_historypos
--;
483 if (_iconsole_historypos
== -1) {
484 _iconsole_cmdline
.DeleteAll();
486 _iconsole_cmdline
.Assign(_iconsole_history
[_iconsole_historypos
]);
491 * Handle the printing of text entered into the console or redirected there
492 * by any other means. Text can be redirected to other clients in a network game
493 * as well as to a logfile. If the network server is a dedicated server, all activities
494 * are also logged. All lines to print are added to a temporary buffer which can be
495 * used as a history to print them onscreen
496 * @param colour_code the colour of the command. Red in case of errors, etc.
497 * @param str the message entered or output on the console (notice, error, etc.)
499 void IConsoleGUIPrint(TextColour colour_code
, char *str
)
501 new IConsoleLine(str
, colour_code
);
502 SetWindowDirty(WC_CONSOLE
, 0);
507 * Check whether the given TextColour is valid for console usage.
508 * @param c The text colour to compare to.
509 * @return true iff the TextColour is valid for console usage.
511 bool IsValidConsoleColour(TextColour c
)
513 /* A normal text colour is used. */
514 if (!(c
& TC_IS_PALETTE_COLOUR
)) return TC_BEGIN
<= c
&& c
< TC_END
;
516 /* A text colour from the palette is used; must be the company
517 * colour gradient, so it must be one of those. */
518 c
&= ~TC_IS_PALETTE_COLOUR
;
519 for (uint i
= COLOUR_BEGIN
; i
< COLOUR_END
; i
++) {
520 if (_colour_gradient
[i
][4] == c
) return true;