|
OpenTTD Source
14.0-beta3
|
Go to the documentation of this file.
28 #include "table/strings.h"
32 static const uint ICON_HISTORY_SIZE = 20;
33 static const uint ICON_RIGHT_BORDERWIDTH = 10;
34 static const uint ICON_BOTTOM_BORDERWIDTH = 12;
74 static std::deque<std::string> _iconsole_history;
75 static ptrdiff_t _iconsole_historypos;
82 static void IConsoleClearCommand()
85 _iconsole_cmdline.
chars = _iconsole_cmdline.
bytes = 1;
86 _iconsole_cmdline.
pixels = 0;
92 static inline void IConsoleResetHistoryPos()
94 _iconsole_historypos = -1;
101 static constexpr
NWidgetPart _nested_console_window_widgets[] = {
105 static WindowDesc _console_window_desc(__FILE__, __LINE__,
109 std::begin(_nested_console_window_widgets), std::end(_nested_console_window_widgets)
114 static size_t scroll;
132 void Close([[maybe_unused]]
int data = 0)
override
146 size_t namount = (size_t) -amount;
147 IConsoleWindow::scroll = (namount > IConsoleWindow::scroll) ? 0 : IConsoleWindow::scroll - namount;
149 assert(this->
height >= 0 && this->line_height > 0);
150 size_t visible_lines = (size_t)(this->
height / this->line_height);
152 IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll + amount, max_scroll);
163 for (
size_t line_index = IConsoleWindow::scroll; line_index <
_iconsole_buffer.size(); line_index++) {
170 int delta = this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH;
181 if (_focused_window ==
this && _iconsole_cmdline.
caret) {
188 assert(this->
height >= 0 && this->line_height > 0);
189 size_t visible_lines = (size_t)(this->
height / this->line_height);
193 IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll, max_scroll);
203 EventState OnKeyPress([[maybe_unused]] char32_t key, uint16_t keycode)
override
219 case WKC_SHIFT | WKC_PAGEDOWN:
220 this->
Scroll(-scroll_height);
223 case WKC_SHIFT | WKC_PAGEUP:
224 this->
Scroll(scroll_height);
227 case WKC_SHIFT | WKC_DOWN:
231 case WKC_SHIFT | WKC_UP:
239 case WKC_RETURN:
case WKC_NUM_ENTER: {
245 IConsoleClearCommand();
251 case WKC_CTRL | WKC_RETURN:
257 case (WKC_CTRL |
'L'):
263 IConsoleWindow::scroll = 0;
264 IConsoleResetHistoryPos();
274 void InsertTextString(
WidgetID,
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
override
276 if (_iconsole_cmdline.
InsertString(str, marked, caret, insert_location, replacement_end)) {
277 IConsoleWindow::scroll = 0;
278 IConsoleResetHistoryPos();
285 return &_iconsole_cmdline;
290 int delta = std::min<int>(this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
291 Point pt = {this->line_offset + delta + _iconsole_cmdline.
caretxoffs, this->
height - this->line_height};
298 int delta = std::min<int>(this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
309 int delta = std::min<int>(this->
width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
311 if (!
IsInsideMM(pt.y, this->height - this->line_height, this->height))
return -1;
316 void OnMouseWheel(
int wheel)
override
332 size_t IConsoleWindow::scroll = 0;
334 void IConsoleGUIInit()
336 IConsoleResetHistoryPos();
339 IConsoleClearBuffer();
341 IConsolePrint(TC_LIGHT_BLUE,
"OpenTTD Game Console Revision 7 - {}", _openttd_revision);
345 IConsoleClearCommand();
348 void IConsoleClearBuffer()
353 void IConsoleGUIFree()
355 IConsoleClearBuffer();
361 switch (_iconsole_mode) {
363 w->
height = _screen.height / 3;
364 w->
width = _screen.width;
367 w->
height = _screen.height - ICON_BOTTOM_BORDERWIDTH;
368 w->
width = _screen.width;
379 switch (_iconsole_mode) {
413 if (_iconsole_history.empty() || _iconsole_history.front() != cmd) {
414 _iconsole_history.emplace_front(cmd);
415 while (_iconsole_history.size() > ICON_HISTORY_SIZE) _iconsole_history.pop_back();
419 IConsoleResetHistoryPos();
420 return _iconsole_history.front().c_str();
429 if (_iconsole_history.empty())
return;
430 _iconsole_historypos = Clamp<ptrdiff_t>(_iconsole_historypos + direction, -1, _iconsole_history.size() - 1);
432 if (_iconsole_historypos == -1) {
435 _iconsole_cmdline.
Assign(_iconsole_history[_iconsole_historypos]);
463 bool need_truncation =
false;
470 need_truncation =
true;
475 if (need_truncation) {
479 return need_truncation;
496 for (uint i = COLOUR_BEGIN; i < COLOUR_END; i++) {
@ ES_HANDLED
The passed event is handled.
@ ICONSOLE_OPENED
In-game console is opened, upper 1/3 of the screen.
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
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...
constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Container for a single line of console output.
static std::deque< IConsoleLine > _iconsole_buffer
The console backlog buffer.
static const uint ICON_CMDLN_SIZE
maximum length of a typed in command
void Assign(StringID string)
Render a string into the textbuffer.
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
static const char * IConsoleHistoryAdd(const char *cmd)
Add the entered line into the history so you can look it back scroll, etc.
uint16_t pixels
the current size of the string in pixels
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
void Scroll(int amount)
Scroll the content of the console.
void OnFocus() override
The window has gained focus.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
@ 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.
uint16_t caretpos
the current position of the caret in the buffer, in bytes
IConsoleModes
Modes of the in-game 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.
bool StrEmpty(const char *s)
Check if a string buffer is empty.
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.
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
char *const buf
buffer in which text is saved
void OnFocusLost(bool) override
The window has lost focus.
@ ICONSOLE_FULL
In-game console is opened, whole screen.
High level window description.
bool HandleCaret()
Handle the flashing of the caret.
uint16_t markxoffs
the start position of the marked area in pixels
@ 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.
int height
Height of the window (number of pixels down in y direction)
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
uint16_t bytes
the current size of the string in bytes (including terminating '\0')
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.
void IConsoleCmdExec(const std::string &command_string, const uint recurse_count)
Execute a given command passed to us.
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.
@ SA_FORCE
Force the alignment, i.e. don't swap for RTL languages.
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
void IConsoleResize(Window *w)
Change the size of the in-game console window after the screen size changed, or the window state chan...
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.
Coordinates of a point in 2D.
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.
#define LRM
A left-to-right marker, marks the next character as left-to-right.
std::string buffer
The data to store.
ptrdiff_t GetTextCharacterAtPosition(const Point &pt) const override
Get the character that is rendered at a position by the focused edit box.
Textbuf * GetFocusedTextbuf() const override
Get the current input text buffer.
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.
static const uint8_t PC_DARK_RED
Dark red palette colour.
@ WC_CONSOLE
Console; Window numbers:
uint16_t marklength
the length of the marked area in pixels
EventState
State of handling an event.
uint16_t console_backlog_length
the minimum amount of items in the console backlog before items will be removed.
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
static const uint8_t PC_BLACK
Black palette colour.
bool IsWhitespace(char32_t c)
Check whether UNICODE character is whitespace or not, i.e.
@ SA_LEFT
Left align the text.
uint16_t chars
the current size of the string in characters (including terminating '\0')
uint16_t time
The amount of time the line is in the backlog.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
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)
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.
IntervalTimer< TimerWindow > truncate_interval
Check on a regular interval if the console buffer needs truncating.
Point 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.
virtual void EditBoxLostFocus()
An edit box lost the input focus.
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.
Data structure for an opened window.
void IConsoleSwitch()
Toggle in-game console between opened and closed.
bool IsValidConsoleColour(TextColour c)
Check whether the given TextColour is valid for console usage.
@ HKPR_NOT_HANDLED
Key does not affect editboxes.
void OnPaint() override
The window must be repainted.
Specification of a rectangle with absolute coordinates of all edges.
uint16_t caretxoffs
the current position of the caret in pixels
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.
void DeleteAll()
Delete every character in the textbuffer.
static bool TruncateBuffer()
Remove old lines from the backlog buffer.
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
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.
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.