30 #include "table/strings.h"
34 static const uint ICON_HISTORY_SIZE = 20;
35 static const uint ICON_RIGHT_BORDERWIDTH = 10;
36 static const uint ICON_BOTTOM_BORDERWIDTH = 12;
75 using AutoCompletion::AutoCompletion;
78 std::vector<std::string> GetSuggestions(std::string_view
prefix, std::string_view
query)
override
81 std::vector<std::string> suggestions;
88 for (
const auto &[_, command] : IConsole::Commands()) {
89 if (command.name.starts_with(
query)) {
90 suggestions.push_back(command.name);
93 for (
const auto &[_, alias] : IConsole::Aliases()) {
94 if (alias.name.starts_with(
query)) {
95 suggestions.push_back(alias.name);
102 void ApplySuggestion(std::string_view
prefix, std::string_view suggestion)
override
104 this->textbuf->
Assign(fmt::format(
"{}{} ",
prefix, suggestion));
111 static std::deque<std::string> _iconsole_history;
112 static ptrdiff_t _iconsole_historypos;
119 static void IConsoleClearCommand()
122 _iconsole_cmdline.
chars = _iconsole_cmdline.
bytes = 1;
123 _iconsole_cmdline.
pixels = 0;
126 _iconsole_tab_completion.Reset();
130 static inline void IConsoleResetHistoryPos()
132 _iconsole_historypos = -1;
139 static constexpr
NWidgetPart _nested_console_window_widgets[] = {
147 _nested_console_window_widgets
152 static size_t scroll;
172 void Close([[maybe_unused]]
int data = 0)
override
186 size_t namount =
static_cast<size_t>(-amount);
187 IConsoleWindow::scroll = (namount > IConsoleWindow::scroll) ? 0 : IConsoleWindow::scroll - namount;
189 assert(this->
height >= 0 && this->line_height > 0);
192 IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll + amount, max_scroll);
203 for (
size_t line_index = IConsoleWindow::scroll; line_index <
_iconsole_buffer.size(); line_index++) {
221 if (_focused_window ==
this && _iconsole_cmdline.
caret) {
228 assert(this->
height >= 0 && this->line_height > 0);
233 IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll, max_scroll);
243 EventState OnKeyPress([[maybe_unused]] char32_t key, uint16_t keycode)
override
259 case WKC_SHIFT | WKC_PAGEDOWN:
260 this->
Scroll(-scroll_height);
263 case WKC_SHIFT | WKC_PAGEUP:
264 this->
Scroll(scroll_height);
267 case WKC_SHIFT | WKC_DOWN:
271 case WKC_SHIFT | WKC_UP:
279 case WKC_RETURN:
case WKC_NUM_ENTER: {
285 IConsoleClearCommand();
291 case WKC_CTRL | WKC_RETURN:
297 case (WKC_CTRL |
'L'):
302 if (_iconsole_tab_completion.AutoComplete()) {
311 _iconsole_tab_completion.Reset();
313 IConsoleWindow::scroll = 0;
314 IConsoleResetHistoryPos();
325 void InsertTextString(
WidgetID,
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
override
327 if (_iconsole_cmdline.
InsertString(str, marked, caret, insert_location, replacement_end)) {
328 _iconsole_tab_completion.Reset();
329 IConsoleWindow::scroll = 0;
330 IConsoleResetHistoryPos();
337 return &_iconsole_cmdline;
342 int delta = std::min<int>(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};
350 int delta = std::min<int>(this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
355 Rect r = {this->line_offset + delta + p1.left, this->
height - this->
line_height, this->line_offset + delta + p2.right, this->
height};
361 int delta = std::min<int>(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 -1;
368 void OnMouseWheel(
int wheel)
override
384 size_t IConsoleWindow::scroll = 0;
386 void IConsoleGUIInit()
388 IConsoleResetHistoryPos();
391 IConsoleClearBuffer();
393 IConsolePrint(TC_LIGHT_BLUE,
"OpenTTD Game Console Revision 7 - {}", _openttd_revision);
397 IConsoleClearCommand();
400 void IConsoleClearBuffer()
405 void IConsoleGUIFree()
407 IConsoleClearBuffer();
413 switch (_iconsole_mode) {
415 w->
height = _screen.height / 3;
416 w->
width = _screen.width;
419 w->
height = _screen.height - ICON_BOTTOM_BORDERWIDTH;
420 w->
width = _screen.width;
431 switch (_iconsole_mode) {
465 if (_iconsole_history.empty() || _iconsole_history.front() != cmd) {
466 _iconsole_history.emplace_front(cmd);
467 while (_iconsole_history.size() > ICON_HISTORY_SIZE) _iconsole_history.pop_back();
471 IConsoleResetHistoryPos();
472 return _iconsole_history.front().c_str();
481 if (_iconsole_history.empty())
return;
482 _iconsole_historypos = Clamp<ptrdiff_t>(_iconsole_historypos + direction, -1, _iconsole_history.size() - 1);
484 if (_iconsole_historypos == -1) {
487 _iconsole_cmdline.
Assign(_iconsole_history[_iconsole_historypos]);
489 _iconsole_tab_completion.Reset();
516 bool need_truncation =
false;
523 need_truncation =
true;
528 if (need_truncation) {
532 return need_truncation;
549 for (Colours i = COLOUR_BEGIN; i < COLOUR_END; i++) {
Generic auto-completion engine.
std::string_view query
Last token of the text. This is used to based the suggestions on.
std::string_view prefix
Prefix of the text before the last space.
virtual void EditBoxLostFocus()
An edit box lost the input focus.
virtual void EditBoxGainedFocus()
An edit box gained the input focus.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
void IConsoleCmdExec(const std::string &command_string, const uint recurse_count)
Execute a given command passed to us.
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.
Console functions used outside of the console code.
void IConsoleSwitch()
Toggle in-game console between opened and closed.
void IConsoleClose()
Close the in-game console.
bool IsValidConsoleColour(TextColour c)
Check whether the given TextColour is valid for console usage.
static bool TruncateBuffer()
Remove old lines from the backlog buffer.
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.
static std::deque< IConsoleLine > _iconsole_buffer
The console backlog buffer.
void IConsoleResize(Window *w)
Change the size of the in-game console window after the screen size changed, or the window state chan...
static void IConsoleHistoryNavigate(int direction)
Navigate Up/Down in the history of typed commands.
static const char * IConsoleHistoryAdd(const char *cmd)
Add the entered line into the history so you can look it back scroll, etc.
GUI related functions in the console.
Internally used functions for the console.
static const uint ICON_CMDLN_SIZE
maximum length of a typed in command
static const TextColour CC_WHITE
White console lines for various things such as the welcome.
static const TextColour CC_COMMAND
Colour for the console's commands.
IConsoleModes
Modes of the in-game console.
@ ICONSOLE_CLOSED
In-game console is closed.
@ ICONSOLE_OPENED
In-game console is opened, upper 1/3 of the screen.
@ ICONSOLE_FULL
In-game console is opened, whole screen.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
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.
uint8_t GetCharacterWidth(FontSize size, char32_t key)
Return width of character glyph.
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Functions related to the gfx engine.
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize)
Get the character from a string that is drawn at a specific position.
ParagraphLayouter::Position GetCharPosInString(std::string_view 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.
Functions related to laying out the texts.
@ SA_LEFT
Left align the text.
@ SA_FORCE
Force the alignment, i.e. don't swap for RTL languages.
@ SA_BOTTOM
Bottom align the text.
@ FS_NORMAL
Index of the normal font in the font tables.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
@ TC_IS_PALETTE_COLOUR
Colour value is already a real palette colour index, not an index of a StringColour.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
uint8_t GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
static const uint8_t PC_BLACK
Black palette colour.
static const uint8_t PC_DARK_RED
Dark red palette colour.
declaration of OTTD revision dependent variables
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.
bool StrEmpty(const char *s)
Check if a string buffer is empty.
bool IsWhitespace(char32_t c)
Check whether UNICODE character is whitespace or not, i.e.
#define LRM
A left-to-right marker, marks the next character as left-to-right.
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Functions related to OTTD's strings.
GUISettings gui
settings related to the GUI
uint16_t console_backlog_length
the minimum amount of items in the console backlog before items will be removed.
uint16_t console_backlog_timeout
the minimum amount of time items should be in the console backlog before they will be removed in ~3 s...
Container for a single line of console output.
TextColour colour
The colour of the line.
uint16_t time
The amount of time the line is in the backlog.
std::string buffer
The data to store.
IConsoleLine(std::string buffer, TextColour colour)
Initialize the console line.
void OnFocusLost(bool) override
The window has lost focus.
void Scroll(int amount)
Scroll the content of the console.
void InsertTextString(WidgetID, 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.
IntervalTimer< TimerWindow > truncate_interval
Check on a regular interval if the console buffer needs truncating.
ptrdiff_t GetTextCharacterAtPosition(const Point &pt) const override
Get the character that is rendered at a position by the focused edit box.
const Textbuf * GetFocusedTextbuf() const override
Get the current input text buffer.
void OnMouseLoop() override
Called for every mouse loop run, which is at least once per (game) tick.
void OnInit() override
Notification that the nested widget tree gets initialized.
int line_height
Height of one line of text in the console.
void OnPaint() override
The window must be repainted.
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.
Point GetCaretPosition() const override
Get the current caret position if an edit box has the focus.
void OnFocus() override
The window has gained focus.
Coordinates of a point in 2D.
Specification of a rectangle with absolute coordinates of all edges.
Helper/buffer for input fields.
uint16_t pixels
the current size of the string in pixels
uint16_t caretpos
the current position of the caret in the buffer, in bytes
uint16_t markxoffs
the start position of the marked area in pixels
void DeleteAll()
Delete every character in the textbuffer.
uint16_t chars
the current size of the string in characters (including terminating '\0')
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.
void Assign(StringID string)
Render a string into the textbuffer.
uint16_t bytes
the current size of the string in bytes (including terminating '\0')
uint16_t caretxoffs
the current position of the caret in pixels
uint16_t marklength
the length of the marked area in pixels
bool caret
is the caret ("_") visible or not
char *const buf
buffer in which text is saved
bool HandleCaret()
Handle the flashing of the caret.
High level window description.
Data structure for an opened window.
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
int height
Height of the window (number of pixels down in y direction)
int width
width of the window (number of pixels to the right in x direction)
Stuff related to text buffers.
HandleKeyPressResult
Return values for Textbuf::HandleKeypress.
@ HKPR_NOT_HANDLED
Key does not affect editboxes.
@ HKPR_EDITING
Textbuf content changed.
Definition of Interval and OneShot timers.
Definition of the Window system.
Base of all video drivers.
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen, bool schedule_resize)
Resize the window.
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
EventState
State of handling an event.
@ ES_HANDLED
The passed event is handled.
@ ES_NOT_HANDLED
The passed event is not handled.
@ WC_CONSOLE
Console; Window numbers:
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.