28 #include "table/strings.h" 32 static const uint ICON_HISTORY_SIZE = 20;
33 static const uint ICON_LINE_SPACING = 2;
34 static const uint ICON_RIGHT_BORDERWIDTH = 10;
35 static const uint ICON_BOTTOM_BORDERWIDTH = 12;
81 while (index != 0 && item != NULL) {
99 if (cur == NULL)
return false;
133 static char *_iconsole_history[ICON_HISTORY_SIZE];
134 static int _iconsole_historypos;
141 static void IConsoleClearCommand()
144 _iconsole_cmdline.
chars = _iconsole_cmdline.
bytes = 1;
145 _iconsole_cmdline.
pixels = 0;
151 static inline void IConsoleResetHistoryPos()
153 _iconsole_historypos = -1;
160 static const struct NWidgetPart _nested_console_window_widgets[] = {
168 _nested_console_window_widgets,
lengthof(_nested_console_window_widgets)
199 int max_scroll = max<int>(0,
IConsoleLine::size + 1 - this->height / this->line_height);
200 IConsoleWindow::scroll = Clamp<int>(IConsoleWindow::scroll + amount, 0, max_scroll);
206 const int right = this->width - 5;
209 int ypos = this->height - this->line_height;
216 int delta = this->width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH;
227 if (_focused_window ==
this && _iconsole_cmdline.
caret) {
243 if (_iconsole_cmdline.
HandleCaret()) this->SetDirty();
250 const int scroll_height = (this->height / this->line_height) - 1;
262 case WKC_SHIFT | WKC_PAGEDOWN:
263 this->Scroll(-scroll_height);
266 case WKC_SHIFT | WKC_PAGEUP:
267 this->Scroll(scroll_height);
270 case WKC_SHIFT | WKC_DOWN:
274 case WKC_SHIFT | WKC_UP:
282 case WKC_RETURN:
case WKC_NUM_ENTER: {
288 IConsoleClearCommand();
294 case WKC_CTRL | WKC_RETURN:
300 case (WKC_CTRL |
'L'):
306 IConsoleWindow::scroll = 0;
307 IConsoleResetHistoryPos();
317 virtual void InsertTextString(
int wid,
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
319 if (_iconsole_cmdline.
InsertString(str, marked, caret, insert_location, replacement_end)) {
320 IConsoleWindow::scroll = 0;
321 IConsoleResetHistoryPos();
328 return _iconsole_cmdline.
buf;
333 return _iconsole_cmdline.
buf + _iconsole_cmdline.
caretpos;
338 if (_iconsole_cmdline.
markend == 0)
return NULL;
341 return _iconsole_cmdline.
buf + _iconsole_cmdline.
markpos;
346 int delta =
min(this->width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
347 Point pt = {this->line_offset + delta + _iconsole_cmdline.
caretxoffs, this->height - this->line_height};
354 int delta =
min(this->width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
359 Rect r = {this->line_offset + delta + p1.x, this->height - this->line_height, this->line_offset + delta + p2.x, this->height};
365 int delta =
min(this->width - this->line_offset - _iconsole_cmdline.
pixels - ICON_RIGHT_BORDERWIDTH, 0);
367 if (!
IsInsideMM(pt.y, this->height - this->line_height, this->height))
return NULL;
374 this->Scroll(-wheel);
383 int IConsoleWindow::scroll = 0;
385 void IConsoleGUIInit()
387 IConsoleResetHistoryPos();
391 memset(_iconsole_history, 0,
sizeof(_iconsole_history));
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[0] == NULL || strcmp(_iconsole_history[0], cmd) != 0) {
466 free(_iconsole_history[ICON_HISTORY_SIZE - 1]);
467 memmove(&_iconsole_history[1], &_iconsole_history[0],
sizeof(_iconsole_history[0]) * (ICON_HISTORY_SIZE - 1));
468 _iconsole_history[0] =
stredup(cmd);
472 IConsoleResetHistoryPos();
473 return _iconsole_history[0];
482 if (_iconsole_history[0] == NULL)
return;
483 _iconsole_historypos =
Clamp(_iconsole_historypos + direction, -1, ICON_HISTORY_SIZE - 1);
485 if (direction > 0 && _iconsole_history[_iconsole_historypos] == NULL) _iconsole_historypos--;
487 if (_iconsole_historypos == -1) {
490 _iconsole_cmdline.
Assign(_iconsole_history[_iconsole_historypos]);
522 c &= ~TC_IS_PALETTE_COLOUR;
523 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.
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.
virtual const char * GetFocusedText() const
Get the current input text if an edit box has the focus.
static bool IsInsideMM(const T x, const uint min, const uint max)
Checks if a value is in an interval.
static const char * IConsoleHistoryAdd(const char *cmd)
Add the entered line into the history so you can look it back scroll, etc.
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.
char * buffer
The data to store.
virtual const char * GetTextCharacterAtPosition(const Point &pt) const
Get the character that is rendered at a position by the focused edit box.
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.
virtual void OnHundredthTick()
Called once every 100 (game) ticks.
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.
bool InsertString(const char *str, bool marked, const char *caret=NULL, const char *insert_location=NULL, const char *replacement_end=NULL)
Insert a string into the text buffer.
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.
virtual void InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
Insert a text string at the cursor position into the edit box widget.
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
#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.
virtual void OnMouseLoop()
Called for every mouse loop run, which is at least once per (game) tick.
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.
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.
virtual void OnMouseWheel(int wheel)
The mouse wheel has been turned.
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
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.
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.
virtual void OnFocusLost()
Called when window looses focus.
char *const buf
buffer in which text is saved
virtual void OnPaint()
The window must be repainted.
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.
virtual void EditBoxLostFocus()
An edit box lost the input focus.
Coordinates of a point in 2D.
static const uint ICON_CMDLN_SIZE
maximum length of a typed in command
virtual Rect GetTextBoundingRect(const char *from, const char *to) const
Get the bounding rectangle for a text range if an edit box has the focus.
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.
virtual const char * GetCaret() const
Get the string at the caret 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.
virtual const char * GetMarkedText(size_t *length) const
Get the range of the currently marked input text.
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 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.
virtual EventState OnKeyPress(WChar key, uint16 keycode)
A key has been pressed.
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
virtual Point GetCaretPosition() const
Get the current caret position if an edit box has the 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.