Go to the documentation of this file.
27 #include "table/strings.h"
31 static const uint ICON_HISTORY_SIZE = 20;
32 static const uint ICON_LINE_SPACING = 2;
33 static const uint ICON_RIGHT_BORDERWIDTH = 10;
34 static const uint ICON_BOTTOM_BORDERWIDTH = 12;
80 while (index != 0 && item !=
nullptr) {
98 if (cur ==
nullptr)
return false;
132 static char *_iconsole_history[ICON_HISTORY_SIZE];
133 static int _iconsole_historypos;
140 static void IConsoleClearCommand()
143 _iconsole_cmdline.
chars = _iconsole_cmdline.
bytes = 1;
144 _iconsole_cmdline.
pixels = 0;
150 static inline void IConsoleResetHistoryPos()
152 _iconsole_historypos = -1;
159 static const struct NWidgetPart _nested_console_window_widgets[] = {
167 _nested_console_window_widgets,
lengthof(_nested_console_window_widgets)
184 this->truncate_timer.SetInterval(3000);
201 IConsoleWindow::scroll = Clamp<int>(IConsoleWindow::scroll + amount, 0, max_scroll);
207 const int right = this->
width - 5;
217 int delta = this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH;
228 if (_focused_window ==
this && _iconsole_cmdline.
caret) {
235 if (this->truncate_timer.
CountElapsed(delta_ms) == 0)
return;
265 case WKC_SHIFT | WKC_PAGEDOWN:
266 this->
Scroll(-scroll_height);
269 case WKC_SHIFT | WKC_PAGEUP:
270 this->
Scroll(scroll_height);
273 case WKC_SHIFT | WKC_DOWN:
277 case WKC_SHIFT | WKC_UP:
285 case WKC_RETURN:
case WKC_NUM_ENTER: {
291 IConsoleClearCommand();
297 case WKC_CTRL | WKC_RETURN:
303 case (WKC_CTRL |
'L'):
309 IConsoleWindow::scroll = 0;
310 IConsoleResetHistoryPos();
320 void InsertTextString(
int wid,
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
override
322 if (_iconsole_cmdline.
InsertString(str, marked, caret, insert_location, replacement_end)) {
323 IConsoleWindow::scroll = 0;
324 IConsoleResetHistoryPos();
331 return _iconsole_cmdline.
buf;
336 return _iconsole_cmdline.
buf + _iconsole_cmdline.
caretpos;
341 if (_iconsole_cmdline.
markend == 0)
return nullptr;
344 return _iconsole_cmdline.
buf + _iconsole_cmdline.
markpos;
349 int delta = std::min<int>(this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
350 Point pt = {this->line_offset + delta + _iconsole_cmdline.
caretxoffs, this->
height - this->line_height};
357 int delta = std::min<int>(this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
368 int delta = std::min<int>(this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
370 if (!
IsInsideMM(pt.y, this->height - this->line_height, this->height))
return nullptr;
391 int IConsoleWindow::scroll = 0;
393 void IConsoleGUIInit()
395 IConsoleResetHistoryPos();
399 memset(_iconsole_history, 0,
sizeof(_iconsole_history));
405 IConsoleClearCommand();
408 void IConsoleClearBuffer()
413 void IConsoleGUIFree()
415 IConsoleClearBuffer();
421 switch (_iconsole_mode) {
423 w->
height = _screen.height / 3;
424 w->
width = _screen.width;
427 w->
height = _screen.height - ICON_BOTTOM_BORDERWIDTH;
428 w->
width = _screen.width;
439 switch (_iconsole_mode) {
473 if (_iconsole_history[0] ==
nullptr || strcmp(_iconsole_history[0], cmd) != 0) {
474 free(_iconsole_history[ICON_HISTORY_SIZE - 1]);
475 memmove(&_iconsole_history[1], &_iconsole_history[0],
sizeof(_iconsole_history[0]) * (ICON_HISTORY_SIZE - 1));
476 _iconsole_history[0] =
stredup(cmd);
480 IConsoleResetHistoryPos();
481 return _iconsole_history[0];
490 if (_iconsole_history[0] ==
nullptr)
return;
491 _iconsole_historypos =
Clamp(_iconsole_historypos + direction, -1, ICON_HISTORY_SIZE - 1);
493 if (direction > 0 && _iconsole_history[_iconsole_historypos] ==
nullptr) _iconsole_historypos--;
495 if (_iconsole_historypos == -1) {
498 _iconsole_cmdline.
Assign(_iconsole_history[_iconsole_historypos]);
531 for (uint i = COLOUR_BEGIN; i < COLOUR_END; i++) {
@ ES_HANDLED
The passed event is handled.
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.
static void Reset()
Reset the complete console line backlog.
char32_t WChar
Type for wide characters, i.e.
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
static IConsoleLine * front
The front of the console backlog buffer.
static int size
The amount of items in the backlog.
Container for a single line of console output.
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.
char * buffer
The data to store.
@ SA_LEFT
Left align the text.
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
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
@ SA_BOTTOM
Bottom align the text.
@ SA_FORCE
Force the alignment, i.e. don't swap for RTL languages.
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
static bool Truncate()
Truncate the list removing everything older than/more than the amount as specified in the config file...
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.
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
@ ICONSOLE_FULL
In-game console is closed.
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 opened, whole screen.
@ 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.
@ TC_IS_PALETTE_COLOUR
Colour value is already a real palette colour index, not an index of a StringColour.
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.
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...
~IConsoleLine()
Clear this console line and any further ones.
Coordinates of a point in 2D.
static const IConsoleLine * Get(uint index)
Get the index-ed item in the list.
static const uint8 PC_BLACK
Black palette colour.
void IConsolePrint(TextColour colour_code, const char *string)
Handle the printing of text entered into the console or redirected there by any other means.
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.
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 DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
void OnMouseWheel(int wheel) override
The mouse wheel has been turned.
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
IConsoleLine(char *buffer, TextColour colour)
Initialize the console line.
@ WC_CONSOLE
Console; Window numbers:
IConsoleLine * previous
The previous console message.
EventState
State of handling an event.
#define MAX_UVALUE(type)
The largest value that can be entered in a variable.
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.
#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)
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 const TextColour CC_WARNING
Colour for warning lines.
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.
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.
void CDECL IConsolePrintF(TextColour colour_code, const char *format,...)
Handle the printing of text entered into the console or redirected there by any other means.
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.
uint16 markxoffs
the start position of the marked area in pixels