31#include "table/strings.h"
35static const uint ICON_HISTORY_SIZE = 20;
36static const uint ICON_RIGHT_BORDERWIDTH = 10;
37static const uint ICON_BOTTOM_BORDERWIDTH = 12;
72 using AutoCompletion::AutoCompletion;
78 std::vector<std::string> suggestions;
85 for (
const auto &[_, command] : IConsole::Commands()) {
86 if (command.name.starts_with(
query)) {
87 suggestions.push_back(command.name);
90 for (
const auto &[_, alias] : IConsole::Aliases()) {
91 if (alias.name.starts_with(
query)) {
92 suggestions.push_back(alias.name);
101 this->textbuf->Assign(fmt::format(
"{}{} ",
prefix, suggestion));
108static std::deque<std::string> _iconsole_history;
109static ptrdiff_t _iconsole_historypos;
116static void IConsoleClearCommand()
119 _iconsole_tab_completion.Reset();
123static inline void IConsoleResetHistoryPos()
125 _iconsole_historypos = -1;
132static constexpr std::initializer_list<NWidgetPart> _nested_console_window_widgets = {
139 WindowClass::Console, WindowClass::None,
141 _nested_console_window_widgets
146 static size_t scroll;
149 int cursor_width = 0;
166 void Close([[maybe_unused]]
int data = 0)
override
180 size_t namount =
static_cast<size_t>(-amount);
181 IConsoleWindow::scroll = (namount > IConsoleWindow::scroll) ? 0 : IConsoleWindow::scroll - namount;
183 assert(this->
height >= 0 && this->line_height > 0);
184 size_t visible_lines =
static_cast<size_t>(this->
height / this->line_height);
186 IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll + amount, max_scroll);
197 for (
size_t line_index = IConsoleWindow::scroll; line_index <
_iconsole_buffer.size(); line_index++) {
221 assert(this->
height >= 0 && this->line_height > 0);
222 size_t visible_lines =
static_cast<size_t>(this->
height / this->line_height);
226 IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll, max_scroll);
240 const int scroll_height = (this->
height / this->line_height) - 1;
252 case WKC_SHIFT | WKC_PAGEDOWN:
253 this->
Scroll(-scroll_height);
256 case WKC_SHIFT | WKC_PAGEUP:
257 this->
Scroll(scroll_height);
260 case WKC_SHIFT | WKC_DOWN:
264 case WKC_SHIFT | WKC_UP:
272 case WKC_RETURN:
case WKC_NUM_ENTER: {
278 IConsoleClearCommand();
284 case WKC_CTRL | WKC_RETURN:
290 case (WKC_CTRL |
'L'):
295 if (_iconsole_tab_completion.AutoComplete()) {
304 _iconsole_tab_completion.Reset();
306 IConsoleWindow::scroll = 0;
307 IConsoleResetHistoryPos();
318 void InsertTextString(
WidgetID, std::string_view str,
bool marked, std::optional<size_t> caret, std::optional<size_t> insert_location, std::optional<size_t> replacement_end)
override
320 if (
_iconsole_cmdline.InsertString(str, marked, caret, insert_location, replacement_end)) {
321 _iconsole_tab_completion.Reset();
322 IConsoleWindow::scroll = 0;
323 IConsoleResetHistoryPos();
335 int delta = std::min<int>(this->
width - this->line_offset -
_iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
343 int delta = std::min<int>(this->
width - this->line_offset -
_iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
348 Rect r = {this->line_offset + delta + p1.left, this->
height - this->line_height, this->line_offset + delta + p2.right, this->
height};
354 int delta = std::min<int>(this->
width - this->line_offset -
_iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
356 if (!
IsInsideMM(pt.
y, this->height - this->line_height, this->height))
return -1;
378size_t IConsoleWindow::scroll = 0;
380void IConsoleGUIInit()
382 IConsoleResetHistoryPos();
385 IConsoleClearBuffer();
391 IConsoleClearCommand();
394void IConsoleClearBuffer()
399void IConsoleGUIFree()
401 IConsoleClearBuffer();
412 w->
height = _screen.height / 3;
413 w->
width = _screen.width;
416 w->
height = _screen.height - ICON_BOTTOM_BORDERWIDTH;
417 w->
width = _screen.width;
457 while (!cmd.empty() &&
IsWhitespace(cmd[0])) cmd.remove_prefix(1);
460 if (cmd.empty())
return std::nullopt;
463 if (_iconsole_history.empty() || _iconsole_history.front() != cmd) {
464 _iconsole_history.emplace_front(cmd);
465 while (_iconsole_history.size() > ICON_HISTORY_SIZE) _iconsole_history.pop_back();
469 IConsoleResetHistoryPos();
470 return _iconsole_history.front();
479 if (_iconsole_history.empty())
return;
480 _iconsole_historypos =
Clamp<ptrdiff_t>(_iconsole_historypos + direction, -1, _iconsole_history.size() - 1);
482 if (_iconsole_historypos == -1) {
487 _iconsole_tab_completion.Reset();
514 bool need_truncation =
false;
521 need_truncation =
true;
526 if (need_truncation) {
530 return need_truncation;
Generic auto-completion engine.
@ Begin
Used for iteration.
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.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
std::vector< std::string > GetSuggestions(std::string_view prefix, std::string_view query) override
Get suggestions for auto completion with the given 'query' input text.
void ApplySuggestion(std::string_view prefix, std::string_view suggestion) override
Format the given suggestion after the given prefix, and write that to the buffer.
Iterate a range of enum values.
An interval timer will fire every interval, and will continue to fire until it is deleted.
static const std::string_view WHITESPACE_NO_NEWLINE
ASCII whitespace characters, excluding new-line.
virtual void EditBoxLostFocus()
An edit box lost the input focus.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
virtual void EditBoxGainedFocus()
An edit box gained the input focus.
void IConsolePrint(ExtendedTextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.
void IConsoleCmdExec(std::string_view command_string, const uint recurse_count)
Execute a given command passed to us.
Console functions used outside of the console code.
IConsoleMode _iconsole_mode
Console modes.
static Textbuf _iconsole_cmdline(ICON_CMDLN_SIZE)
Main console cmd buffer.
void IConsoleGUIPrint(ExtendedTextColour colour_code, const std::string &str)
Handle the printing of text entered into the console or redirected there by any other means.
static std::optional< std::string_view > IConsoleHistoryAdd(std::string_view cmd)
Add the entered line into the history so you can look it back scroll, etc.
void IConsoleSwitch()
Toggle in-game console between opened and closed.
void IConsoleClose()
Close the in-game console.
static bool TruncateBuffer()
Remove old lines from the backlog buffer.
bool IsValidConsoleColour(ExtendedTextColour c)
Check whether the given TextColour is valid for console usage.
IConsoleMode _iconsole_mode
Console modes.
static WindowDesc _console_window_desc(WindowPosition::Manual, {}, 0, 0, WindowClass::Console, WindowClass::None, {}, _nested_console_window_widgets)
Window definition for the console window.
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.
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.
IConsoleMode
Modes of the in-game console.
@ Closed
In-game console is closed.
@ Opened
In-game console is opened, upper 1/3 of the screen.
@ Full
In-game console is opened, whole screen.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
@ ForceLeft
Force align to the left.
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
uint8_t GetCharacterWidth(FontSize size, char32_t key)
Return width of character glyph.
void GfxFillRect(int left, int top, int right, int bottom, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
int DrawString(int left, int right, int top, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Functions related to the gfx engine.
ParagraphLayouter::Position GetCharPosInString(std::string_view str, size_t pos, FontSize start_fontsize)
Get the leading corner of a character in a single-line string relative to the start of the string.
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.
Functions related to laying out the texts.
@ Normal
Index of the normal font in the font tables.
Colours
One of 16 base colours used for companies and windows/widgets.
@ End
End-of-array marker.
@ IsPaletteColour
Colour value is already a real palette colour index, not an index of a StringColour.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
@ Begin
Marker for the begin of the range.
@ LightBlue
Light blue colour.
@ End
Marker for the end of the range.
void SetDirty() const
Mark entire window as dirty (in need of re-paint).
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
#define Point
Macro that prevents name conflicts between included headers.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
PixelColour GetColourGradient(Colours colour, Shade shade)
Get colour gradient palette index.
@ Normal
Normal colour shade.
static constexpr PixelColour PC_DARK_RED
Dark red palette colour.
static constexpr PixelColour PC_BLACK
Black 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 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.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Functions related to OTTD's strings.
value_type width
The width of the rectangle.
Container for the text colour and some text colour related flags for drawing.
ExtendedTextColourFlags flags
The flags.
TextColour colour
The colour.
Container for a single line of console output.
ExtendedTextColour colour
The colour of the line.
IConsoleLine(std::string buffer, ExtendedTextColour colour)
Initialize the console line.
uint16_t time
The amount of time the line is in the backlog.
std::string buffer
The data to store.
void OnFocusLost(bool) override
The window has lost focus.
EventState OnKeyPress(char32_t key, uint16_t keycode) override
A key has been pressed.
void Scroll(int amount)
Scroll the content of the console.
const Textbuf * GetFocusedTextbuf() const override
Get the current input text buffer.
ptrdiff_t GetTextCharacterAtPosition(const Point &pt) const override
Get the character that is rendered at a position by the focused edit box.
Rect GetTextBoundingRect(size_t from, size_t to) const override
Get the bounding rectangle for a text range if an edit box has the focus.
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.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
int line_height
Height of one line of text in the console.
void InsertTextString(WidgetID, std::string_view str, bool marked, std::optional< size_t > caret, std::optional< size_t > insert_location, std::optional< size_t > replacement_end) override
Insert a text string at the cursor position into the edit box widget.
void OnPaint() override
The window must be repainted.
const IntervalTimer< TimerWindow > truncate_interval
Check on a regular interval if the console buffer needs truncating.
Point GetCaretPosition() const override
Get the current caret position if an edit box has the focus.
void OnMouseWheel(int wheel, WidgetID widget) override
The mouse wheel has been turned.
void OnFocus() override
The window has gained focus.
Specification of a rectangle with absolute coordinates of all edges.
Helper/buffer for input fields.
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.
Window(WindowDesc &desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
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.
@ Editing
Textbuf content changed.
@ NotHandled
Key does not affect editboxes.
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).
Window * _focused_window
Window that currently has focus.
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.
@ Manual
Manually align the window (so no automatic location finding).
EventState
State of handling an event.
@ Handled
The passed event is handled.
@ NotHandled
The passed event is not handled.