OpenTTD Source 20241224-master-gf74b0cf984
console_gui.cpp File Reference

Handling the GUI of the in-game console. More...

#include "stdafx.h"
#include "textbuf_type.h"
#include "window_gui.h"
#include "autocompletion.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 "gfx_layout.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"

Go to the source code of this file.

Data Structures

struct  IConsoleLine
 Container for a single line of console output. More...
 
class  ConsoleAutoCompletion
 
struct  IConsoleWindow
 

Functions

static bool TruncateBuffer ()
 Remove old lines from the backlog buffer.
 
static void IConsoleClearCommand ()
 
static void IConsoleResetHistoryPos ()
 
static const char * IConsoleHistoryAdd (const char *cmd)
 Add the entered line into the history so you can look it back scroll, etc.
 
static void IConsoleHistoryNavigate (int direction)
 Navigate Up/Down in the history of typed commands.
 
void IConsoleGUIInit ()
 
void IConsoleClearBuffer ()
 
void IConsoleGUIFree ()
 
void IConsoleResize (Window *w)
 Change the size of the in-game console window after the screen size changed, or the window state changed.
 
void IConsoleSwitch ()
 Toggle in-game console between opened and closed.
 
void IConsoleClose ()
 Close the in-game console.
 
void IConsoleGUIPrint (TextColour colour_code, const std::string &str)
 Handle the printing of text entered into the console or redirected there by any other means.
 
bool IsValidConsoleColour (TextColour c)
 Check whether the given TextColour is valid for console usage.
 

Variables

static const uint ICON_HISTORY_SIZE = 20
 
static const uint ICON_RIGHT_BORDERWIDTH = 10
 
static const uint ICON_BOTTOM_BORDERWIDTH = 12
 
static std::deque< IConsoleLine_iconsole_buffer
 The console backlog buffer.
 
static Textbuf _iconsole_cmdline (ICON_CMDLN_SIZE)
 
static std::deque< std::string > _iconsole_history
 
static ptrdiff_t _iconsole_historypos
 
IConsoleModes _iconsole_mode
 
static constexpr NWidgetPart _nested_console_window_widgets []
 
static WindowDesc _console_window_desc (WDP_MANUAL, nullptr, 0, 0, WC_CONSOLE, WC_NONE, 0, _nested_console_window_widgets)
 

Detailed Description

Handling the GUI of the in-game console.

Definition in file console_gui.cpp.

Function Documentation

◆ IConsoleClearBuffer()

void IConsoleClearBuffer ( )

Definition at line 400 of file console_gui.cpp.

◆ IConsoleClearCommand()

static void IConsoleClearCommand ( )
static

Definition at line 119 of file console_gui.cpp.

◆ IConsoleClose()

void IConsoleClose ( )

Close the in-game console.

Definition at line 445 of file console_gui.cpp.

References ICONSOLE_OPENED, and IConsoleSwitch().

Referenced by InitWindowSystem().

◆ IConsoleGUIFree()

void IConsoleGUIFree ( )

Definition at line 405 of file console_gui.cpp.

◆ IConsoleGUIInit()

void IConsoleGUIInit ( )

Definition at line 386 of file console_gui.cpp.

◆ IConsoleGUIPrint()

void IConsoleGUIPrint ( TextColour  colour_code,
const std::string &  str 
)

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

Parameters
colour_codethe colour of the command. Red in case of errors, etc.
strthe message entered or output on the console (notice, error, etc.)

Definition at line 501 of file console_gui.cpp.

References _iconsole_buffer, SetWindowDirty(), and WC_CONSOLE.

Referenced by IConsolePrint().

◆ IConsoleHistoryAdd()

static const char * IConsoleHistoryAdd ( const char *  cmd)
static

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

Parameters
cmdText to be entered into the 'history'
Returns
the command to execute

Definition at line 456 of file console_gui.cpp.

References IsWhitespace(), and StrEmpty().

Referenced by IConsoleWindow::OnKeyPress().

◆ IConsoleHistoryNavigate()

static void IConsoleHistoryNavigate ( int  direction)
static

Navigate Up/Down in the history of typed commands.

Parameters
directionGo further back in history (+1), go to recently typed commands (-1)

Definition at line 479 of file console_gui.cpp.

References Textbuf::Assign(), and Textbuf::DeleteAll().

Referenced by IConsoleWindow::OnKeyPress().

◆ IConsoleResetHistoryPos()

static void IConsoleResetHistoryPos ( )
inlinestatic

Definition at line 130 of file console_gui.cpp.

◆ IConsoleResize()

void IConsoleResize ( Window w)

Change the size of the in-game console window after the screen size changed, or the window state changed.

Definition at line 411 of file console_gui.cpp.

References Window::height, ICONSOLE_FULL, ICONSOLE_OPENED, MarkWholeScreenDirty(), and Window::width.

Referenced by IConsoleWindow::OnKeyPress(), and RelocateAllWindows().

◆ IConsoleSwitch()

void IConsoleSwitch ( )

Toggle in-game console between opened and closed.

Definition at line 429 of file console_gui.cpp.

References CloseWindowById(), ICONSOLE_CLOSED, ICONSOLE_FULL, ICONSOLE_OPENED, MarkWholeScreenDirty(), and WC_CONSOLE.

Referenced by IConsoleClose(), MenuClickHelp(), MainWindow::OnHotkey(), and IConsoleWindow::OnKeyPress().

◆ IsValidConsoleColour()

bool IsValidConsoleColour ( TextColour  c)

Check whether the given TextColour is valid for console usage.

Parameters
cThe text colour to compare to.
Returns
true iff the TextColour is valid for console usage.

Definition at line 541 of file console_gui.cpp.

References GetColourGradient(), and TC_IS_PALETTE_COLOUR.

Referenced by IConsolePrint(), ServerNetworkAdminSocketHandler::Receive_ADMIN_EXTERNAL_CHAT(), ClientNetworkGameSocketHandler::Receive_SERVER_EXTERNAL_CHAT(), and ClientNetworkGameSocketHandler::Receive_SERVER_RCON().

◆ TruncateBuffer()

static bool TruncateBuffer ( )
static

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.

Returns
true if any lines were removed

Definition at line 514 of file console_gui.cpp.

References _iconsole_buffer, _settings_client, GUISettings::console_backlog_length, GUISettings::console_backlog_timeout, and ClientSettings::gui.

Variable Documentation

◆ _iconsole_buffer

std::deque<IConsoleLine> _iconsole_buffer
static

The console backlog buffer.

Item index 0 is the newest line.

Definition at line 69 of file console_gui.cpp.

Referenced by IConsoleGUIPrint(), IConsoleWindow::OnPaint(), IConsoleWindow::Scroll(), and TruncateBuffer().

◆ _iconsole_cmdline

static ConsoleAutoCompletion _iconsole_tab_completion & _iconsole_cmdline ( ICON_CMDLN_SIZE  )
static

Definition at line 110 of file console_gui.cpp.

◆ _iconsole_history

std::deque<std::string> _iconsole_history
static

Definition at line 111 of file console_gui.cpp.

◆ _iconsole_historypos

ptrdiff_t _iconsole_historypos
static

Definition at line 112 of file console_gui.cpp.

◆ _iconsole_mode

IConsoleModes _iconsole_mode

Definition at line 113 of file console_gui.cpp.

◆ _nested_console_window_widgets

constexpr NWidgetPart _nested_console_window_widgets[]
staticconstexpr
Initial value:
= {
NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_BACKGROUND), SetResize(1, 1),
}
@ WID_C_BACKGROUND
Background of the console.
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
@ WWT_EMPTY
Empty widget, place holder to reserve space in widget tree.
Definition widget_type.h:48

Definition at line 139 of file console_gui.cpp.

◆ ICON_BOTTOM_BORDERWIDTH

const uint ICON_BOTTOM_BORDERWIDTH = 12
static

Definition at line 36 of file console_gui.cpp.

◆ ICON_HISTORY_SIZE

const uint ICON_HISTORY_SIZE = 20
static

Definition at line 34 of file console_gui.cpp.

◆ ICON_RIGHT_BORDERWIDTH

const uint ICON_RIGHT_BORDERWIDTH = 10
static

Definition at line 35 of file console_gui.cpp.