2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-10-14 19:27:08 +00:00
|
|
|
/** @file console_gui.cpp Handling the GUI of the in-game console. */
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
2012-06-04 15:29:37 +00:00
|
|
|
#include "textbuf_type.h"
|
2008-05-24 11:55:25 +00:00
|
|
|
#include "window_gui.h"
|
|
|
|
#include "console_gui.h"
|
|
|
|
#include "console_internal.h"
|
|
|
|
#include "window_func.h"
|
|
|
|
#include "string_func.h"
|
2010-07-02 13:59:27 +00:00
|
|
|
#include "strings_func.h"
|
2008-05-24 11:55:25 +00:00
|
|
|
#include "gfx_func.h"
|
2008-08-12 20:49:27 +00:00
|
|
|
#include "settings_type.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "console_func.h"
|
2008-05-24 11:55:25 +00:00
|
|
|
#include "rev.h"
|
2013-08-05 20:37:02 +00:00
|
|
|
#include "video/video_driver.hpp"
|
2023-04-13 15:18:27 +00:00
|
|
|
#include "timer/timer.h"
|
|
|
|
#include "timer/timer_window.h"
|
2022-08-26 07:38:35 +00:00
|
|
|
#include <deque>
|
2008-05-24 11:55:25 +00:00
|
|
|
|
2011-12-15 22:22:55 +00:00
|
|
|
#include "widgets/console_widget.h"
|
|
|
|
|
2010-07-02 13:59:27 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2010-05-13 10:14:29 +00:00
|
|
|
static const uint ICON_HISTORY_SIZE = 20;
|
|
|
|
static const uint ICON_RIGHT_BORDERWIDTH = 10;
|
|
|
|
static const uint ICON_BOTTOM_BORDERWIDTH = 12;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
/**
|
|
|
|
* Container for a single line of console output
|
|
|
|
*/
|
|
|
|
struct IConsoleLine {
|
2022-08-26 07:38:35 +00:00
|
|
|
std::string buffer; ///< The data to store.
|
2009-02-09 02:09:47 +00:00
|
|
|
TextColour colour; ///< The colour of the line.
|
|
|
|
uint16 time; ///< The amount of time the line is in the backlog.
|
2008-08-12 20:49:27 +00:00
|
|
|
|
2022-08-26 07:38:35 +00:00
|
|
|
IConsoleLine() : buffer(), colour(TC_BEGIN), time(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
/**
|
|
|
|
* Initialize the console line.
|
|
|
|
* @param buffer the data to print.
|
|
|
|
* @param colour the colour of the line.
|
|
|
|
*/
|
2022-09-09 16:40:50 +00:00
|
|
|
IConsoleLine(std::string buffer, TextColour colour) :
|
|
|
|
buffer(std::move(buffer)),
|
2008-08-12 20:49:27 +00:00
|
|
|
colour(colour),
|
|
|
|
time(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~IConsoleLine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-26 07:38:35 +00:00
|
|
|
/** The console backlog buffer. Item index 0 is the newest line. */
|
|
|
|
static std::deque<IConsoleLine> _iconsole_buffer;
|
|
|
|
|
|
|
|
static bool TruncateBuffer();
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* ** main console cmd buffer ** */
|
2012-11-14 22:50:17 +00:00
|
|
|
static Textbuf _iconsole_cmdline(ICON_CMDLN_SIZE);
|
2008-05-24 11:55:25 +00:00
|
|
|
static char *_iconsole_history[ICON_HISTORY_SIZE];
|
2011-12-11 11:37:03 +00:00
|
|
|
static int _iconsole_historypos;
|
2008-08-12 20:49:27 +00:00
|
|
|
IConsoleModes _iconsole_mode;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
/* *************** *
|
|
|
|
* end of header *
|
|
|
|
* *************** */
|
|
|
|
|
|
|
|
static void IConsoleClearCommand()
|
|
|
|
{
|
|
|
|
memset(_iconsole_cmdline.buf, 0, ICON_CMDLN_SIZE);
|
2010-12-05 22:22:54 +00:00
|
|
|
_iconsole_cmdline.chars = _iconsole_cmdline.bytes = 1; // only terminating zero
|
2010-12-05 22:21:37 +00:00
|
|
|
_iconsole_cmdline.pixels = 0;
|
2008-05-24 11:55:25 +00:00
|
|
|
_iconsole_cmdline.caretpos = 0;
|
|
|
|
_iconsole_cmdline.caretxoffs = 0;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_CONSOLE, 0);
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
|
2010-06-26 14:51:27 +00:00
|
|
|
static inline void IConsoleResetHistoryPos()
|
|
|
|
{
|
2011-12-11 11:37:03 +00:00
|
|
|
_iconsole_historypos = -1;
|
2010-06-26 14:51:27 +00:00
|
|
|
}
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
|
2009-12-25 19:17:36 +00:00
|
|
|
static const char *IConsoleHistoryAdd(const char *cmd);
|
2008-05-24 11:55:25 +00:00
|
|
|
static void IConsoleHistoryNavigate(int direction);
|
|
|
|
|
2009-08-01 12:42:58 +00:00
|
|
|
static const struct NWidgetPart _nested_console_window_widgets[] = {
|
2011-12-16 16:27:45 +00:00
|
|
|
NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_BACKGROUND), SetResize(1, 1),
|
2009-08-01 12:42:58 +00:00
|
|
|
};
|
|
|
|
|
2013-05-26 19:23:42 +00:00
|
|
|
static WindowDesc _console_window_desc(
|
2019-04-10 21:07:06 +00:00
|
|
|
WDP_MANUAL, nullptr, 0, 0,
|
2009-08-01 12:42:58 +00:00
|
|
|
WC_CONSOLE, WC_NONE,
|
|
|
|
0,
|
2009-11-15 10:26:01 +00:00
|
|
|
_nested_console_window_widgets, lengthof(_nested_console_window_widgets)
|
2009-08-01 12:42:58 +00:00
|
|
|
);
|
|
|
|
|
2008-05-24 11:55:25 +00:00
|
|
|
struct IConsoleWindow : Window
|
|
|
|
{
|
2022-08-26 07:38:35 +00:00
|
|
|
static size_t scroll;
|
2010-06-26 15:22:10 +00:00
|
|
|
int line_height; ///< Height of one line of text in the console.
|
2009-04-10 18:55:38 +00:00
|
|
|
int line_offset;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
2013-05-26 19:23:42 +00:00
|
|
|
IConsoleWindow() : Window(&_console_window_desc)
|
2008-05-24 11:55:25 +00:00
|
|
|
{
|
|
|
|
_iconsole_mode = ICONSOLE_OPENED;
|
2009-08-01 12:42:58 +00:00
|
|
|
|
2013-05-26 19:23:42 +00:00
|
|
|
this->InitNested(0);
|
2009-08-01 12:42:58 +00:00
|
|
|
ResizeWindow(this, _screen.width, _screen.height / 3);
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 08:36:22 +00:00
|
|
|
void OnInit() override
|
|
|
|
{
|
|
|
|
this->line_height = FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.hsep_normal;
|
|
|
|
this->line_offset = GetStringBoundingBox("] ").width + WidgetDimensions::scaled.frametext.left;
|
|
|
|
}
|
|
|
|
|
2021-05-15 21:12:25 +00:00
|
|
|
void Close() override
|
2008-05-24 11:55:25 +00:00
|
|
|
{
|
|
|
|
_iconsole_mode = ICONSOLE_CLOSED;
|
2014-04-28 21:06:51 +00:00
|
|
|
VideoDriver::GetInstance()->EditBoxLostFocus();
|
2021-05-15 21:12:25 +00:00
|
|
|
this->Window::Close();
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
|
2011-02-05 17:53:29 +00:00
|
|
|
/**
|
|
|
|
* Scroll the content of the console.
|
|
|
|
* @param amount Number of lines to scroll back.
|
|
|
|
*/
|
|
|
|
void Scroll(int amount)
|
|
|
|
{
|
2022-08-26 07:38:35 +00:00
|
|
|
if (amount < 0) {
|
|
|
|
size_t namount = (size_t) -amount;
|
|
|
|
IConsoleWindow::scroll = (namount > IConsoleWindow::scroll) ? 0 : IConsoleWindow::scroll - namount;
|
|
|
|
} else {
|
|
|
|
assert(this->height >= 0 && this->line_height > 0);
|
|
|
|
size_t visible_lines = (size_t)(this->height / this->line_height);
|
|
|
|
size_t max_scroll = (visible_lines > _iconsole_buffer.size()) ? 0 : _iconsole_buffer.size() + 1 - visible_lines;
|
|
|
|
IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll + amount, max_scroll);
|
|
|
|
}
|
2011-02-05 17:53:29 +00:00
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnPaint() override
|
2008-05-24 11:55:25 +00:00
|
|
|
{
|
2022-09-23 08:36:22 +00:00
|
|
|
const int right = this->width - WidgetDimensions::scaled.frametext.right;
|
2009-03-21 22:46:17 +00:00
|
|
|
|
2011-05-06 21:13:29 +00:00
|
|
|
GfxFillRect(0, 0, this->width - 1, this->height - 1, PC_BLACK);
|
2011-02-05 17:41:05 +00:00
|
|
|
int ypos = this->height - this->line_height;
|
2022-08-26 07:38:35 +00:00
|
|
|
for (size_t line_index = IConsoleWindow::scroll; line_index < _iconsole_buffer.size(); line_index++) {
|
|
|
|
const IConsoleLine &print = _iconsole_buffer[line_index];
|
|
|
|
SetDParamStr(0, print.buffer);
|
2022-09-23 08:36:22 +00:00
|
|
|
ypos = DrawStringMultiLine(WidgetDimensions::scaled.frametext.left, right, -this->line_height, ypos, STR_JUST_RAW_STRING, print.colour, SA_LEFT | SA_BOTTOM | SA_FORCE) - WidgetDimensions::scaled.hsep_normal;
|
2011-02-05 17:44:44 +00:00
|
|
|
if (ypos < 0) break;
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
/* If the text is longer than the window, don't show the starting ']' */
|
2010-12-05 22:21:37 +00:00
|
|
|
int delta = this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH;
|
2008-05-24 11:55:25 +00:00
|
|
|
if (delta > 0) {
|
2022-09-23 08:36:22 +00:00
|
|
|
DrawString(WidgetDimensions::scaled.frametext.left, right, this->height - this->line_height, "]", (TextColour)CC_COMMAND, SA_LEFT | SA_FORCE);
|
2008-05-24 11:55:25 +00:00
|
|
|
delta = 0;
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:37:22 +00:00
|
|
|
/* If we have a marked area, draw a background highlight. */
|
|
|
|
if (_iconsole_cmdline.marklength != 0) GfxFillRect(this->line_offset + delta + _iconsole_cmdline.markxoffs, this->height - this->line_height, this->line_offset + delta + _iconsole_cmdline.markxoffs + _iconsole_cmdline.marklength, this->height - 1, PC_DARK_RED);
|
|
|
|
|
2009-04-10 18:55:38 +00:00
|
|
|
DrawString(this->line_offset + delta, right, this->height - this->line_height, _iconsole_cmdline.buf, (TextColour)CC_COMMAND, SA_LEFT | SA_FORCE);
|
2008-05-24 11:55:25 +00:00
|
|
|
|
2009-02-09 01:22:29 +00:00
|
|
|
if (_focused_window == this && _iconsole_cmdline.caret) {
|
2009-04-10 18:55:38 +00:00
|
|
|
DrawString(this->line_offset + delta + _iconsole_cmdline.caretxoffs, right, this->height - this->line_height, "_", TC_WHITE, SA_LEFT | SA_FORCE);
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 15:18:27 +00:00
|
|
|
/** Check on a regular interval if the console buffer needs truncating. */
|
|
|
|
IntervalTimer<TimerWindow> truncate_interval = {std::chrono::seconds(3), [this](auto) {
|
2022-08-26 07:38:35 +00:00
|
|
|
assert(this->height >= 0 && this->line_height > 0);
|
|
|
|
size_t visible_lines = (size_t)(this->height / this->line_height);
|
|
|
|
|
|
|
|
if (TruncateBuffer() && IConsoleWindow::scroll + visible_lines > _iconsole_buffer.size()) {
|
|
|
|
size_t max_scroll = (visible_lines > _iconsole_buffer.size()) ? 0 : _iconsole_buffer.size() + 1 - visible_lines;
|
|
|
|
IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll, max_scroll);
|
2008-08-12 20:49:27 +00:00
|
|
|
this->SetDirty();
|
|
|
|
}
|
2023-04-13 15:18:27 +00:00
|
|
|
}};
|
2008-08-12 20:49:27 +00:00
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnMouseLoop() override
|
2008-05-24 11:55:25 +00:00
|
|
|
{
|
2012-06-04 15:30:29 +00:00
|
|
|
if (_iconsole_cmdline.HandleCaret()) this->SetDirty();
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
EventState OnKeyPress(WChar key, uint16 keycode) override
|
2008-05-24 11:55:25 +00:00
|
|
|
{
|
2009-02-09 01:22:29 +00:00
|
|
|
if (_focused_window != this) return ES_NOT_HANDLED;
|
|
|
|
|
2009-04-10 18:21:40 +00:00
|
|
|
const int scroll_height = (this->height / this->line_height) - 1;
|
2008-05-24 11:55:25 +00:00
|
|
|
switch (keycode) {
|
|
|
|
case WKC_UP:
|
2009-03-14 18:16:29 +00:00
|
|
|
IConsoleHistoryNavigate(1);
|
2008-05-24 11:55:25 +00:00
|
|
|
this->SetDirty();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WKC_DOWN:
|
|
|
|
IConsoleHistoryNavigate(-1);
|
|
|
|
this->SetDirty();
|
|
|
|
break;
|
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
case WKC_SHIFT | WKC_PAGEDOWN:
|
2011-02-05 17:53:29 +00:00
|
|
|
this->Scroll(-scroll_height);
|
2008-05-24 11:55:25 +00:00
|
|
|
break;
|
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
case WKC_SHIFT | WKC_PAGEUP:
|
2011-02-05 17:53:29 +00:00
|
|
|
this->Scroll(scroll_height);
|
2008-05-24 11:55:25 +00:00
|
|
|
break;
|
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
case WKC_SHIFT | WKC_DOWN:
|
2011-02-05 17:53:29 +00:00
|
|
|
this->Scroll(-1);
|
2008-05-24 11:55:25 +00:00
|
|
|
break;
|
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
case WKC_SHIFT | WKC_UP:
|
2011-02-05 17:53:29 +00:00
|
|
|
this->Scroll(1);
|
2008-05-24 11:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WKC_BACKQUOTE:
|
|
|
|
IConsoleSwitch();
|
|
|
|
break;
|
|
|
|
|
2009-12-25 19:17:36 +00:00
|
|
|
case WKC_RETURN: case WKC_NUM_ENTER: {
|
2010-10-21 20:12:48 +00:00
|
|
|
/* We always want the ] at the left side; we always force these strings to be left
|
2019-09-29 20:27:32 +00:00
|
|
|
* aligned anyway. So enforce this in all cases by adding a left-to-right marker,
|
2010-10-21 20:12:48 +00:00
|
|
|
* otherwise it will be drawn at the wrong side with right-to-left texts. */
|
2021-06-12 19:33:01 +00:00
|
|
|
IConsolePrint(CC_COMMAND, LRM "] {}", _iconsole_cmdline.buf);
|
2009-12-25 19:17:36 +00:00
|
|
|
const char *cmd = IConsoleHistoryAdd(_iconsole_cmdline.buf);
|
2008-05-24 11:55:25 +00:00
|
|
|
IConsoleClearCommand();
|
2009-12-25 19:17:36 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (cmd != nullptr) IConsoleCmdExec(cmd);
|
2010-08-01 18:53:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
case WKC_CTRL | WKC_RETURN:
|
|
|
|
_iconsole_mode = (_iconsole_mode == ICONSOLE_FULL) ? ICONSOLE_OPENED : ICONSOLE_FULL;
|
|
|
|
IConsoleResize(this);
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (WKC_CTRL | 'L'):
|
|
|
|
IConsoleCmdExec("clear");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-03-17 13:05:45 +00:00
|
|
|
if (_iconsole_cmdline.HandleKeyPress(key, keycode) != HKPR_NOT_HANDLED) {
|
2008-08-12 20:49:27 +00:00
|
|
|
IConsoleWindow::scroll = 0;
|
2008-05-24 11:55:25 +00:00
|
|
|
IConsoleResetHistoryPos();
|
|
|
|
this->SetDirty();
|
|
|
|
} else {
|
|
|
|
return ES_NOT_HANDLED;
|
|
|
|
}
|
2010-08-01 20:58:35 +00:00
|
|
|
break;
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
return ES_HANDLED;
|
|
|
|
}
|
2011-02-05 17:54:36 +00:00
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end) override
|
2013-08-05 20:37:06 +00:00
|
|
|
{
|
2013-08-05 20:37:57 +00:00
|
|
|
if (_iconsole_cmdline.InsertString(str, marked, caret, insert_location, replacement_end)) {
|
2013-08-05 20:37:06 +00:00
|
|
|
IConsoleWindow::scroll = 0;
|
|
|
|
IConsoleResetHistoryPos();
|
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
const char *GetFocusedText() const override
|
2013-08-05 20:37:44 +00:00
|
|
|
{
|
|
|
|
return _iconsole_cmdline.buf;
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
const char *GetCaret() const override
|
2013-08-05 20:37:44 +00:00
|
|
|
{
|
|
|
|
return _iconsole_cmdline.buf + _iconsole_cmdline.caretpos;
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
const char *GetMarkedText(size_t *length) const override
|
2013-08-05 20:37:44 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (_iconsole_cmdline.markend == 0) return nullptr;
|
2013-08-05 20:37:44 +00:00
|
|
|
|
|
|
|
*length = _iconsole_cmdline.markend - _iconsole_cmdline.markpos;
|
|
|
|
return _iconsole_cmdline.buf + _iconsole_cmdline.markpos;
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
Point GetCaretPosition() const override
|
2013-08-05 20:37:14 +00:00
|
|
|
{
|
2021-01-08 10:16:18 +00:00
|
|
|
int delta = std::min<int>(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
|
2013-08-05 20:37:14 +00:00
|
|
|
Point pt = {this->line_offset + delta + _iconsole_cmdline.caretxoffs, this->height - this->line_height};
|
|
|
|
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
Rect GetTextBoundingRect(const char *from, const char *to) const override
|
2013-08-05 20:37:48 +00:00
|
|
|
{
|
2021-01-08 10:16:18 +00:00
|
|
|
int delta = std::min<int>(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
|
2013-08-05 20:37:48 +00:00
|
|
|
|
|
|
|
Point p1 = GetCharPosInString(_iconsole_cmdline.buf, from, FS_NORMAL);
|
2013-08-05 20:37:53 +00:00
|
|
|
Point p2 = from != to ? GetCharPosInString(_iconsole_cmdline.buf, from) : p1;
|
2013-08-05 20:37:48 +00:00
|
|
|
|
|
|
|
Rect r = {this->line_offset + delta + p1.x, this->height - this->line_height, this->line_offset + delta + p2.x, this->height};
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2023-05-06 17:53:02 +00:00
|
|
|
ptrdiff_t GetTextCharacterAtPosition(const Point &pt) const override
|
2013-08-05 20:37:53 +00:00
|
|
|
{
|
2021-01-08 10:16:18 +00:00
|
|
|
int delta = std::min<int>(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
|
2013-08-05 20:37:53 +00:00
|
|
|
|
2023-05-06 17:53:02 +00:00
|
|
|
if (!IsInsideMM(pt.y, this->height - this->line_height, this->height)) return -1;
|
2013-08-05 20:37:53 +00:00
|
|
|
|
|
|
|
return GetCharAtPosition(_iconsole_cmdline.buf, pt.x - delta);
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnMouseWheel(int wheel) override
|
2011-02-05 17:54:36 +00:00
|
|
|
{
|
|
|
|
this->Scroll(-wheel);
|
|
|
|
}
|
2013-08-05 20:37:02 +00:00
|
|
|
|
2019-11-04 18:09:24 +00:00
|
|
|
void OnFocus() override
|
|
|
|
{
|
|
|
|
VideoDriver::GetInstance()->EditBoxGainedFocus();
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnFocusLost() override
|
2013-08-05 20:37:02 +00:00
|
|
|
{
|
2014-04-28 21:06:51 +00:00
|
|
|
VideoDriver::GetInstance()->EditBoxLostFocus();
|
2013-08-05 20:37:02 +00:00
|
|
|
}
|
2008-05-24 11:55:25 +00:00
|
|
|
};
|
|
|
|
|
2022-08-26 07:38:35 +00:00
|
|
|
size_t IConsoleWindow::scroll = 0;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
void IConsoleGUIInit()
|
|
|
|
{
|
2011-12-11 11:37:03 +00:00
|
|
|
IConsoleResetHistoryPos();
|
2008-05-24 11:55:25 +00:00
|
|
|
_iconsole_mode = ICONSOLE_CLOSED;
|
|
|
|
|
2022-08-26 07:38:35 +00:00
|
|
|
IConsoleClearBuffer();
|
2008-05-24 11:55:25 +00:00
|
|
|
memset(_iconsole_history, 0, sizeof(_iconsole_history));
|
2008-08-12 20:49:27 +00:00
|
|
|
|
2021-06-12 19:33:01 +00:00
|
|
|
IConsolePrint(TC_LIGHT_BLUE, "OpenTTD Game Console Revision 7 - {}", _openttd_revision);
|
|
|
|
IConsolePrint(CC_WHITE, "------------------------------------");
|
2021-06-13 12:55:11 +00:00
|
|
|
IConsolePrint(CC_WHITE, "use \"help\" for more information.");
|
2021-06-12 19:33:01 +00:00
|
|
|
IConsolePrint(CC_WHITE, "");
|
2008-05-24 11:55:25 +00:00
|
|
|
IConsoleClearCommand();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IConsoleClearBuffer()
|
|
|
|
{
|
2022-08-26 07:38:35 +00:00
|
|
|
_iconsole_buffer.clear();
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IConsoleGUIFree()
|
|
|
|
{
|
|
|
|
IConsoleClearBuffer();
|
|
|
|
}
|
|
|
|
|
2010-06-26 15:22:10 +00:00
|
|
|
/** Change the size of the in-game console window after the screen size changed, or the window state changed. */
|
2008-05-24 11:55:25 +00:00
|
|
|
void IConsoleResize(Window *w)
|
|
|
|
{
|
|
|
|
switch (_iconsole_mode) {
|
|
|
|
case ICONSOLE_OPENED:
|
|
|
|
w->height = _screen.height / 3;
|
|
|
|
w->width = _screen.width;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_FULL:
|
|
|
|
w->height = _screen.height - ICON_BOTTOM_BORDERWIDTH;
|
|
|
|
w->width = _screen.width;
|
|
|
|
break;
|
|
|
|
default: return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2010-06-26 15:22:10 +00:00
|
|
|
/** Toggle in-game console between opened and closed. */
|
2008-05-24 11:55:25 +00:00
|
|
|
void IConsoleSwitch()
|
|
|
|
{
|
|
|
|
switch (_iconsole_mode) {
|
|
|
|
case ICONSOLE_CLOSED:
|
2009-03-30 01:15:51 +00:00
|
|
|
new IConsoleWindow();
|
2008-05-24 11:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ICONSOLE_OPENED: case ICONSOLE_FULL:
|
2021-05-17 13:46:38 +00:00
|
|
|
CloseWindowById(WC_CONSOLE, 0);
|
2008-05-24 11:55:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2010-06-26 14:51:27 +00:00
|
|
|
/** Close the in-game console. */
|
|
|
|
void IConsoleClose()
|
|
|
|
{
|
|
|
|
if (_iconsole_mode == ICONSOLE_OPENED) IConsoleSwitch();
|
|
|
|
}
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the entered line into the history so you can look it back
|
|
|
|
* scroll, etc. Put it to the beginning as it is the latest text
|
|
|
|
* @param cmd Text to be entered into the 'history'
|
2009-12-25 19:17:36 +00:00
|
|
|
* @return the command to execute
|
2008-05-24 11:55:25 +00:00
|
|
|
*/
|
2009-12-25 19:17:36 +00:00
|
|
|
static const char *IConsoleHistoryAdd(const char *cmd)
|
2008-05-24 11:55:25 +00:00
|
|
|
{
|
2009-05-23 22:40:14 +00:00
|
|
|
/* Strip all spaces at the begin */
|
|
|
|
while (IsWhitespace(*cmd)) cmd++;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
2009-05-23 22:40:14 +00:00
|
|
|
/* Do not put empty command in history */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (StrEmpty(cmd)) return nullptr;
|
2009-05-23 22:40:14 +00:00
|
|
|
|
|
|
|
/* Do not put in history if command is same as previous */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (_iconsole_history[0] == nullptr || strcmp(_iconsole_history[0], cmd) != 0) {
|
2009-05-23 22:40:14 +00:00
|
|
|
free(_iconsole_history[ICON_HISTORY_SIZE - 1]);
|
|
|
|
memmove(&_iconsole_history[1], &_iconsole_history[0], sizeof(_iconsole_history[0]) * (ICON_HISTORY_SIZE - 1));
|
2014-04-25 15:40:32 +00:00
|
|
|
_iconsole_history[0] = stredup(cmd);
|
2009-05-23 22:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the history position */
|
2008-05-24 11:55:25 +00:00
|
|
|
IConsoleResetHistoryPos();
|
2009-12-25 19:17:36 +00:00
|
|
|
return _iconsole_history[0];
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate Up/Down in the history of typed commands
|
|
|
|
* @param direction Go further back in history (+1), go to recently typed commands (-1)
|
|
|
|
*/
|
|
|
|
static void IConsoleHistoryNavigate(int direction)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (_iconsole_history[0] == nullptr) return; // Empty history
|
2011-12-11 11:37:03 +00:00
|
|
|
_iconsole_historypos = Clamp(_iconsole_historypos + direction, -1, ICON_HISTORY_SIZE - 1);
|
2008-05-24 11:55:25 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (direction > 0 && _iconsole_history[_iconsole_historypos] == nullptr) _iconsole_historypos--;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
2011-12-11 11:37:03 +00:00
|
|
|
if (_iconsole_historypos == -1) {
|
2012-11-14 22:50:21 +00:00
|
|
|
_iconsole_cmdline.DeleteAll();
|
2011-12-11 11:37:03 +00:00
|
|
|
} else {
|
2012-11-14 22:50:21 +00:00
|
|
|
_iconsole_cmdline.Assign(_iconsole_history[_iconsole_historypos]);
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the printing of text entered into the console or redirected there
|
2008-09-30 20:39:50 +00:00
|
|
|
* by any other means. Text can be redirected to other clients in a network game
|
2008-05-24 11:55:25 +00:00
|
|
|
* as well as to a logfile. If the network server is a dedicated server, all activities
|
|
|
|
* are also logged. All lines to print are added to a temporary buffer which can be
|
|
|
|
* used as a history to print them onscreen
|
2009-02-09 02:57:15 +00:00
|
|
|
* @param colour_code the colour of the command. Red in case of errors, etc.
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param str the message entered or output on the console (notice, error, etc.)
|
2008-05-24 11:55:25 +00:00
|
|
|
*/
|
2011-01-03 12:01:41 +00:00
|
|
|
void IConsoleGUIPrint(TextColour colour_code, char *str)
|
2008-05-24 11:55:25 +00:00
|
|
|
{
|
2022-08-26 07:38:35 +00:00
|
|
|
_iconsole_buffer.push_front(IConsoleLine(str, colour_code));
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_CONSOLE, 0);
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
2011-01-03 20:54:20 +00:00
|
|
|
|
2022-08-26 07:38:35 +00:00
|
|
|
/**
|
|
|
|
* Remove old lines from the backlog buffer.
|
|
|
|
* The buffer is limited by a maximum size and a minimum age. Every time truncation runs,
|
|
|
|
* all lines in the buffer are aged by one. When a line exceeds both the maximum position
|
|
|
|
* and also the maximum age, it gets removed.
|
|
|
|
* @return true if any lines were removed
|
|
|
|
*/
|
|
|
|
static bool TruncateBuffer()
|
|
|
|
{
|
|
|
|
bool need_truncation = false;
|
|
|
|
size_t count = 0;
|
|
|
|
for (IConsoleLine &line : _iconsole_buffer) {
|
|
|
|
count++;
|
|
|
|
line.time++;
|
|
|
|
if (line.time > _settings_client.gui.console_backlog_timeout && count > _settings_client.gui.console_backlog_length) {
|
|
|
|
/* Any messages after this are older and need to be truncated */
|
|
|
|
need_truncation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_truncation) {
|
|
|
|
_iconsole_buffer.resize(count - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return need_truncation;
|
|
|
|
}
|
|
|
|
|
2011-01-03 20:54:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the given TextColour is valid for console usage.
|
|
|
|
* @param c The text colour to compare to.
|
|
|
|
* @return true iff the TextColour is valid for console usage.
|
|
|
|
*/
|
|
|
|
bool IsValidConsoleColour(TextColour c)
|
|
|
|
{
|
|
|
|
/* A normal text colour is used. */
|
|
|
|
if (!(c & TC_IS_PALETTE_COLOUR)) return TC_BEGIN <= c && c < TC_END;
|
|
|
|
|
|
|
|
/* A text colour from the palette is used; must be the company
|
|
|
|
* colour gradient, so it must be one of those. */
|
|
|
|
c &= ~TC_IS_PALETTE_COLOUR;
|
2011-01-23 00:11:15 +00:00
|
|
|
for (uint i = COLOUR_BEGIN; i < COLOUR_END; i++) {
|
2011-01-03 20:54:20 +00:00
|
|
|
if (_colour_gradient[i][4] == c) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|