Go to the documentation of this file.
29 #include "table/strings.h"
33 static const uint ICON_HISTORY_SIZE = 20;
34 static const uint ICON_RIGHT_BORDERWIDTH = 10;
35 static const uint ICON_BOTTOM_BORDERWIDTH = 12;
75 static char *_iconsole_history[ICON_HISTORY_SIZE];
76 static int _iconsole_historypos;
83 static void IConsoleClearCommand()
86 _iconsole_cmdline.
chars = _iconsole_cmdline.
bytes = 1;
87 _iconsole_cmdline.
pixels = 0;
93 static inline void IConsoleResetHistoryPos()
95 _iconsole_historypos = -1;
102 static const struct NWidgetPart _nested_console_window_widgets[] = {
110 _nested_console_window_widgets,
lengthof(_nested_console_window_widgets)
115 static size_t scroll;
125 this->truncate_timer.SetInterval(3000);
149 size_t namount = (size_t) -amount;
150 IConsoleWindow::scroll = (namount > IConsoleWindow::scroll) ? 0 : IConsoleWindow::scroll - namount;
152 assert(this->
height >= 0 && this->line_height > 0);
153 size_t visible_lines = (size_t)(this->
height / this->line_height);
155 IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll + amount, max_scroll);
166 for (
size_t line_index = IConsoleWindow::scroll; line_index <
_iconsole_buffer.size(); line_index++) {
173 int delta = this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH;
184 if (_focused_window ==
this && _iconsole_cmdline.
caret) {
191 if (this->truncate_timer.
CountElapsed(delta_ms) == 0)
return;
193 assert(this->
height >= 0 && this->line_height > 0);
194 size_t visible_lines = (size_t)(this->
height / this->line_height);
198 IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll, max_scroll);
224 case WKC_SHIFT | WKC_PAGEDOWN:
225 this->
Scroll(-scroll_height);
228 case WKC_SHIFT | WKC_PAGEUP:
229 this->
Scroll(scroll_height);
232 case WKC_SHIFT | WKC_DOWN:
236 case WKC_SHIFT | WKC_UP:
244 case WKC_RETURN:
case WKC_NUM_ENTER: {
250 IConsoleClearCommand();
256 case WKC_CTRL | WKC_RETURN:
262 case (WKC_CTRL |
'L'):
268 IConsoleWindow::scroll = 0;
269 IConsoleResetHistoryPos();
279 void InsertTextString(
int wid,
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
override
281 if (_iconsole_cmdline.
InsertString(str, marked, caret, insert_location, replacement_end)) {
282 IConsoleWindow::scroll = 0;
283 IConsoleResetHistoryPos();
290 return _iconsole_cmdline.
buf;
295 return _iconsole_cmdline.
buf + _iconsole_cmdline.
caretpos;
300 if (_iconsole_cmdline.
markend == 0)
return nullptr;
303 return _iconsole_cmdline.
buf + _iconsole_cmdline.
markpos;
308 int delta = std::min<int>(this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
309 Point pt = {this->line_offset + delta + _iconsole_cmdline.
caretxoffs, this->
height - this->line_height};
316 int delta = std::min<int>(this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
327 int delta = std::min<int>(this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
329 if (!
IsInsideMM(pt.y, this->height - this->line_height, this->height))
return nullptr;
350 size_t IConsoleWindow::scroll = 0;
352 void IConsoleGUIInit()
354 IConsoleResetHistoryPos();
357 IConsoleClearBuffer();
358 memset(_iconsole_history, 0,
sizeof(_iconsole_history));
360 IConsolePrint(TC_LIGHT_BLUE,
"OpenTTD Game Console Revision 7 - {}", _openttd_revision);
364 IConsoleClearCommand();
367 void IConsoleClearBuffer()
372 void IConsoleGUIFree()
374 IConsoleClearBuffer();
380 switch (_iconsole_mode) {
382 w->
height = _screen.height / 3;
383 w->
width = _screen.width;
386 w->
height = _screen.height - ICON_BOTTOM_BORDERWIDTH;
387 w->
width = _screen.width;
398 switch (_iconsole_mode) {
432 if (_iconsole_history[0] ==
nullptr || strcmp(_iconsole_history[0], cmd) != 0) {
433 free(_iconsole_history[ICON_HISTORY_SIZE - 1]);
434 memmove(&_iconsole_history[1], &_iconsole_history[0],
sizeof(_iconsole_history[0]) * (ICON_HISTORY_SIZE - 1));
435 _iconsole_history[0] =
stredup(cmd);
439 IConsoleResetHistoryPos();
440 return _iconsole_history[0];
449 if (_iconsole_history[0] ==
nullptr)
return;
450 _iconsole_historypos =
Clamp(_iconsole_historypos + direction, -1, ICON_HISTORY_SIZE - 1);
452 if (direction > 0 && _iconsole_history[_iconsole_historypos] ==
nullptr) _iconsole_historypos--;
454 if (_iconsole_historypos == -1) {
457 _iconsole_cmdline.
Assign(_iconsole_history[_iconsole_historypos]);
485 bool need_truncation =
false;
492 need_truncation =
true;
497 if (need_truncation) {
501 return need_truncation;
518 for (uint i = COLOUR_BEGIN; i < COLOUR_END; i++) {
@ ES_HANDLED
The passed event is handled.
static constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
const char * GetMarkedText(size_t *length) const override
Get the range of the currently marked input text.
@ ICONSOLE_OPENED
In-game console is opened, upper 1/3 of the screen.
char32_t WChar
Type for wide characters, i.e.
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Container for a single line of console output.
static std::deque< IConsoleLine > _iconsole_buffer
The console backlog buffer.
static const uint8 PC_DARK_RED
Dark red palette colour.
uint16 time
The amount of time the line is in the backlog.
static const uint ICON_CMDLN_SIZE
maximum length of a typed in command
void Assign(StringID string)
Render a string into the textbuffer.
void OnRealtimeTick(uint delta_ms) override
Called periodically.
static const char * IConsoleHistoryAdd(const char *cmd)
Add the entered line into the history so you can look it back scroll, etc.
uint16 caretpos
the current position of the caret in the buffer, in bytes
const char * GetCaret() const override
Get the string at the caret if an edit box has the focus.
void Scroll(int amount)
Scroll the content of the console.
void InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end) override
Insert a text string at the cursor position into the edit box widget.
void OnFocus() override
Called when window gains focus.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
uint16 caretxoffs
the current position of the caret in pixels
@ SA_BOTTOM
Bottom align the text.
ClientSettings _settings_client
The current settings for this game.
Rect GetTextBoundingRect(const char *from, const char *to) const override
Get the bounding rectangle for a text range if an edit box has the focus.
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
IConsoleModes
Modes of the in-game console.
uint16 markpos
the start position of the marked area in the buffer, in bytes
void IConsoleGUIPrint(TextColour colour_code, char *str)
Handle the printing of text entered into the console or redirected there by any other means.
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
bool InsertString(const char *str, bool marked, const char *caret=nullptr, const char *insert_location=nullptr, const char *replacement_end=nullptr)
Insert a string into the text buffer.
uint16 console_backlog_timeout
the minimum amount of time items should be in the console backlog before they will be removed in ~3 s...
uint16 pixels
the current size of the string in pixels
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
char *const buf
buffer in which text is saved
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
@ ICONSOLE_FULL
In-game console is opened, whole screen.
High level window description.
bool HandleCaret()
Handle the flashing of the caret.
const char * GetFocusedText() const override
Get the current input text if an edit box has the focus.
@ ICONSOLE_CLOSED
In-game console is closed.
@ FS_NORMAL
Index of the normal font in the font tables.
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
uint16 console_backlog_length
the minimum amount of items in the console backlog before items will be removed.
int height
Height of the window (number of pixels down in y direction)
uint16 marklength
the length of the marked area in pixels
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
static void IConsoleHistoryNavigate(int direction)
Navigate Up/Down in the history of typed commands.
int line_height
Height of one line of text in the console.
Point GetCaretPosition() const override
Get the current caret position if an edit box has the focus.
@ ES_NOT_HANDLED
The passed event is not handled.
void Close() override
Hide the window and all its child windows, and mark them for a later deletion.
@ TC_IS_PALETTE_COLOUR
Colour value is already a real palette colour index, not an index of a StringColour.
@ SA_FORCE
Force the alignment, i.e. don't swap for RTL languages.
const char * GetCharAtPosition(const char *str, int x, FontSize start_fontsize)
Get the character from a string that is drawn at a specific position.
void OnMouseLoop() override
Called for every mouse loop run, which is at least once per (game) tick.
IConsoleLine(std::string buffer, TextColour colour)
Initialize the console line.
TextColour colour
The colour of the line.
bool caret
is the caret ("_") visible or not
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
void IConsoleResize(Window *w)
Change the size of the in-game console window after the screen size changed, or the window state chan...
Coordinates of a point in 2D.
static const uint8 PC_BLACK
Black palette colour.
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
static const TextColour CC_COMMAND
Colour for the console's commands.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
void IConsoleClose()
Close the in-game console.
const char * GetTextCharacterAtPosition(const Point &pt) const override
Get the character that is rendered at a position by the focused edit box.
#define LRM
A left-to-right marker, marks the next character as left-to-right.
std::string buffer
The data to store.
uint16 markend
the end position of the marked area in the buffer, in bytes
uint16 bytes
the current size of the string in bytes (including terminating '\0')
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
void OnMouseWheel(int wheel) override
The mouse wheel has been turned.
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
@ WC_CONSOLE
Console; Window numbers:
EventState
State of handling an event.
uint CountElapsed(uint delta)
Count how many times the interval has elapsed.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
@ SA_LEFT
Left align the text.
#define lengthof(x)
Return the length of an fixed size array.
int width
width of the window (number of pixels to the right in x direction)
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
void CloseWindowById(WindowClass cls, WindowNumber number, bool force)
Close a window by its class and window number (if it is open).
uint16 chars
the current size of the string in characters (including terminating '\0')
static bool IsWhitespace(WChar c)
Check whether UNICODE character is whitespace or not, i.e.
void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
Execute a given command passed to us.
virtual void EditBoxLostFocus()
An edit box lost the input focus.
Data structure for an opened window.
void IConsoleSwitch()
Toggle in-game console between opened and closed.
void OnFocusLost() override
Called when window loses focus.
bool IsValidConsoleColour(TextColour c)
Check whether the given TextColour is valid for console usage.
@ HKPR_NOT_HANDLED
Key does not affect editboxes.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
void OnPaint() override
The window must be repainted.
Specification of a rectangle with absolute coordinates of all edges.
void OnInit() override
Notification that the nested widget tree gets initialized.
static const TextColour CC_WHITE
White console lines for various things such as the welcome.
Point GetCharPosInString(const char *str, const char *ch, FontSize start_fontsize)
Get the leading corner of a character in a single-line string relative to the start of the string.
EventState OnKeyPress(WChar key, uint16 keycode) override
A key has been pressed.
void CDECL void DeleteAll()
Delete every character in the textbuffer.
static bool TruncateBuffer()
Remove old lines from the backlog buffer.
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
GUISettings gui
settings related to the GUI
virtual void EditBoxGainedFocus()
An edit box gained the input focus.
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
Resize the window.
Helper/buffer for input fields.
virtual void Close()
Hide the window and all its child windows, and mark them for a later deletion.
void IConsolePrint(TextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.
uint16 markxoffs
the start position of the marked area in pixels