26 #include "table/strings.h" 30 static const uint ICON_HISTORY_SIZE = 20;
31 static const uint ICON_LINE_SPACING = 2;
32 static const uint ICON_RIGHT_BORDERWIDTH = 10;
33 static const uint ICON_BOTTOM_BORDERWIDTH = 12;
79 while (index != 0 && item !=
nullptr) {
97 if (cur ==
nullptr)
return false;
131 static char *_iconsole_history[ICON_HISTORY_SIZE];
132 static int _iconsole_historypos;
139 static void IConsoleClearCommand()
142 _iconsole_cmdline.
chars = _iconsole_cmdline.
bytes = 1;
143 _iconsole_cmdline.
pixels = 0;
149 static inline void IConsoleResetHistoryPos()
151 _iconsole_historypos = -1;
158 static const struct NWidgetPart _nested_console_window_widgets[] = {
166 _nested_console_window_widgets,
lengthof(_nested_console_window_widgets)
197 int max_scroll = max<int>(0,
IConsoleLine::size + 1 - this->height / this->line_height);
198 IConsoleWindow::scroll = Clamp<int>(IConsoleWindow::scroll + amount, 0, max_scroll);
204 const int right = this->width - 5;
207 int ypos = this->height - this->line_height;
214 int delta = this->width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH;
225 if (_focused_window ==
this && _iconsole_cmdline.
caret) {
241 if (_iconsole_cmdline.
HandleCaret()) this->SetDirty();
248 const int scroll_height = (this->height / this->line_height) - 1;
260 case WKC_SHIFT | WKC_PAGEDOWN:
261 this->Scroll(-scroll_height);
264 case WKC_SHIFT | WKC_PAGEUP:
265 this->Scroll(scroll_height);
268 case WKC_SHIFT | WKC_DOWN:
272 case WKC_SHIFT | WKC_UP:
280 case WKC_RETURN:
case WKC_NUM_ENTER: {
286 IConsoleClearCommand();
292 case WKC_CTRL | WKC_RETURN:
298 case (WKC_CTRL |
'L'):
304 IConsoleWindow::scroll = 0;
305 IConsoleResetHistoryPos();
315 void InsertTextString(
int wid,
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
override 317 if (_iconsole_cmdline.
InsertString(str, marked, caret, insert_location, replacement_end)) {
318 IConsoleWindow::scroll = 0;
319 IConsoleResetHistoryPos();
326 return _iconsole_cmdline.
buf;
331 return _iconsole_cmdline.
buf + _iconsole_cmdline.
caretpos;
336 if (_iconsole_cmdline.
markend == 0)
return nullptr;
339 return _iconsole_cmdline.
buf + _iconsole_cmdline.
markpos;
344 int delta =
min(this->width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
345 Point pt = {this->line_offset + delta + _iconsole_cmdline.
caretxoffs, this->height - this->line_height};
352 int delta =
min(this->width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
357 Rect r = {this->line_offset + delta + p1.x, this->height - this->line_height, this->line_offset + delta + p2.x, this->height};
363 int delta =
min(this->width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
365 if (!
IsInsideMM(pt.y, this->height - this->line_height, this->height))
return nullptr;
372 this->Scroll(-wheel);
386 int IConsoleWindow::scroll = 0;
388 void IConsoleGUIInit()
390 IConsoleResetHistoryPos();
394 memset(_iconsole_history, 0,
sizeof(_iconsole_history));
400 IConsoleClearCommand();
403 void IConsoleClearBuffer()
408 void IConsoleGUIFree()
410 IConsoleClearBuffer();
416 switch (_iconsole_mode) {
418 w->
height = _screen.height / 3;
419 w->
width = _screen.width;
422 w->
height = _screen.height - ICON_BOTTOM_BORDERWIDTH;
423 w->
width = _screen.width;
434 switch (_iconsole_mode) {
468 if (_iconsole_history[0] ==
nullptr || strcmp(_iconsole_history[0], cmd) != 0) {
469 free(_iconsole_history[ICON_HISTORY_SIZE - 1]);
470 memmove(&_iconsole_history[1], &_iconsole_history[0],
sizeof(_iconsole_history[0]) * (ICON_HISTORY_SIZE - 1));
471 _iconsole_history[0] =
stredup(cmd);
475 IConsoleResetHistoryPos();
476 return _iconsole_history[0];
485 if (_iconsole_history[0] ==
nullptr)
return;
486 _iconsole_historypos =
Clamp(_iconsole_historypos + direction, -1, ICON_HISTORY_SIZE - 1);
488 if (direction > 0 && _iconsole_history[_iconsole_historypos] ==
nullptr) _iconsole_historypos--;
490 if (_iconsole_historypos == -1) {
493 _iconsole_cmdline.
Assign(_iconsole_history[_iconsole_historypos]);
525 c &= ~TC_IS_PALETTE_COLOUR;
526 for (uint i = COLOUR_BEGIN; i < COLOUR_END; i++) {
EventState
State of handling an event.
Functions related to OTTD's strings.
uint16 markend
the end position of the marked area in the buffer, in bytes
Base of all video drivers.
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 chars
the current size of the string in characters (including terminating '\0')
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...
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
High level window description.
static const char * IConsoleHistoryAdd(const char *cmd)
Add the entered line into the history so you can look it back scroll, etc.
virtual void EditBoxGainedFocus()
An edit box gained the input focus.
The passed event is not handled.
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Stuff related to text buffers.
static bool IsWhitespace(WChar c)
Check whether UNICODE character is whitespace or not, i.e.
void CDECL void DeleteAll()
Delete every character in the textbuffer.
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.
char * buffer
The data to store.
IConsoleModes
Modes of the in-game console.
void IConsoleResize(Window *w)
Change the size of the in-game console window after the screen size changed, or the window state chan...
Helper/buffer for input fields.
uint16 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.
void IConsoleGUIPrint(TextColour colour_code, char *str)
Handle the printing of text entered into the console or redirected there by any other means...
static T max(const T a, const T b)
Returns the maximum of two values.
In-game console is opened, whole screen.
Functions, definitions and such used only by the GUI.
In-game console is closed.
Force the alignment, i.e. don't swap for RTL languages.
Data structure for an opened window.
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Functions related to low-level strings.
bool caret
is the caret ("_") visible or not
Container for a single line of console output.
Internally used functions for the console.
void IConsoleClose()
Close the in-game console.
uint16 markxoffs
the start position of the marked area in pixels
void IConsolePrint(TextColour colour_code, const char *string)
Handle the printing of text entered into the console or redirected there by any other means...
uint16 pixels
the current size of the string in pixels
void OnPaint() override
The window must be repainted.
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
void IConsoleCmdExec(const char *cmdstr)
Execute a given command passed to us.
Functions related to the gfx engine.
ClientSettings _settings_client
The current settings for this game.
Types related to global configuration settings.
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...
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
const char * GetCaret() const override
Get the string at the caret if an edit box has the focus.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
void Scroll(int amount)
Scroll the content of the console.
~IConsoleLine()
Clear this console line and any further ones.
static void Reset()
Reset the complete console line backlog.
Key does not affect editboxes.
static int size
The amount of items in the backlog.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Console functions used outside of the console code.
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.
TextColour colour
The colour of the line.
GUI related functions in the console.
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...
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.
#define lengthof(x)
Return the length of an fixed size array.
static T min(const T a, const T b)
Returns the minimum of two values.
#define MAX_UVALUE(type)
The largest value that can be entered in a variable.
uint16 caretpos
the current position of the caret in the buffer, in bytes
void OnMouseLoop() override
Called for every mouse loop run, which is at least once per (game) tick.
static IConsoleLine * front
The front of the console backlog buffer.
static const uint8 PC_BLACK
Black palette colour.
static const IConsoleLine * Get(uint index)
Get the index-ed item in the list.
void OnHundredthTick() override
Called once every 100 (game) ticks.
static const TextColour CC_COMMAND
Colour for the console's commands.
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
void Assign(StringID string)
Render a string into the textbuffer.
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
No window, redirects to WC_MAIN_WINDOW.
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
uint16 console_backlog_length
the minimum amount of items in the console backlog before items will be removed.
static const uint8 PC_DARK_RED
Dark red palette colour.
IConsoleLine(char *buffer, TextColour colour)
Initialize the console line.
char *const buf
buffer in which text is saved
EventState OnKeyPress(WChar key, uint16 keycode) override
A key has been pressed.
const char * GetTextCharacterAtPosition(const Point &pt) const override
Get the character that is rendered at a position by the focused edit box.
GUISettings gui
settings related to the GUI
uint16 time
The amount of time the line is in the backlog.
static bool Truncate()
Truncate the list removing everything older than/more than the amount as specified in the config file...
bool HandleCaret()
Handle the flashing of the caret.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
int line_height
Height of one line of text in the console.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
uint16 caretxoffs
the current position of the caret in pixels
uint16 console_backlog_timeout
the minimum amount of time items should be in the console backlog before they will be removed in ~3 s...
Index of the normal font in the font tables.
const char * GetMarkedText(size_t *length) const override
Get the range of the currently marked input text.
virtual void EditBoxLostFocus()
An edit box lost the input focus.
Coordinates of a point in 2D.
const char * GetFocusedText() const override
Get the current input text if an edit box has the focus.
static const uint ICON_CMDLN_SIZE
maximum length of a typed in command
Colour value is already a real palette colour index, not an index of a StringColour.
#define LRM
A left-to-right marker, marks the next character as left-to-right.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
declaration of OTTD revision dependent variables
int width
width of the window (number of pixels to the right in x direction)
IConsoleLine * previous
The previous console message.
Point GetCaretPosition() const override
Get the current caret position if an edit box has the focus.
void IConsoleSwitch()
Toggle in-game console between opened and closed.
Specification of a rectangle with absolute coordinates of all edges.
The passed event is handled.
bool IsValidConsoleColour(TextColour c)
Check whether the given TextColour is valid for console usage.
const char * GetCharAtPosition(const char *str, int x, FontSize start_fontsize)
Get the character from a string that is drawn at a specific position.
Window functions not directly related to making/drawing windows.
Manually align the window (so no automatic location finding)
uint16 marklength
the length of the marked area in pixels
In-game console is opened, upper 1/3 of the screen.
uint32 WChar
Type for wide characters, i.e.
void OnMouseWheel(int wheel) override
The mouse wheel has been turned.
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
Resize the window.
static const TextColour CC_WARNING
Colour for warning lines.
void OnFocus() override
Called when window gains focus.
static const TextColour CC_WHITE
White console lines for various things such as the welcome.
uint16 markpos
the start position of the marked area in the buffer, in bytes
void OnFocusLost() override
Called when window loses focus.
int height
Height of the window (number of pixels down in y direction)
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
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.