Merge commit '73bed054b87399484e39f6972d30f91a404daba8'
This commit is contained in:
@@ -12,7 +12,6 @@
|
||||
#include "window_gui.h"
|
||||
#include "console_gui.h"
|
||||
#include "console_internal.h"
|
||||
#include "guitimer_func.h"
|
||||
#include "window_func.h"
|
||||
#include "string_func.h"
|
||||
#include "strings_func.h"
|
||||
@@ -22,8 +21,8 @@
|
||||
#include "rev.h"
|
||||
#include "video/video_driver.hpp"
|
||||
#include "textbuf_gui.h"
|
||||
#include <deque>
|
||||
#include <string>
|
||||
#include "timer/timer.h"
|
||||
#include "timer/timer_window.h"
|
||||
|
||||
#include "widgets/console_widget.h"
|
||||
|
||||
@@ -48,7 +47,7 @@ static const uint ICON_BOTTOM_BORDERWIDTH = 12;
|
||||
struct IConsoleLine {
|
||||
std::string buffer; ///< The data to store.
|
||||
TextColour colour; ///< The colour of the line.
|
||||
uint16 time; ///< The amount of time the line is in the backlog.
|
||||
uint16_t time; ///< The amount of time the line is in the backlog.
|
||||
|
||||
IConsoleLine() : buffer(), colour(TC_BEGIN), time(0)
|
||||
{
|
||||
@@ -80,8 +79,8 @@ static bool TruncateBuffer();
|
||||
|
||||
/* ** main console cmd buffer ** */
|
||||
static Textbuf _iconsole_cmdline(ICON_CMDLN_SIZE);
|
||||
static char *_iconsole_history[ICON_HISTORY_SIZE];
|
||||
static int _iconsole_historypos;
|
||||
static std::deque<std::string> _iconsole_history;
|
||||
static ptrdiff_t _iconsole_historypos;
|
||||
IConsoleModes _iconsole_mode;
|
||||
|
||||
/* *************** *
|
||||
@@ -107,15 +106,15 @@ static inline void IConsoleResetHistoryPos()
|
||||
static const char *IConsoleHistoryAdd(const char *cmd);
|
||||
static void IConsoleHistoryNavigate(int direction);
|
||||
|
||||
static const struct NWidgetPart _nested_console_window_widgets[] = {
|
||||
static constexpr NWidgetPart _nested_console_window_widgets[] = {
|
||||
NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_BACKGROUND), SetResize(1, 1),
|
||||
};
|
||||
|
||||
static WindowDesc _console_window_desc(
|
||||
static WindowDesc _console_window_desc(__FILE__, __LINE__,
|
||||
WDP_MANUAL, nullptr, 0, 0,
|
||||
WC_CONSOLE, WC_NONE,
|
||||
0,
|
||||
_nested_console_window_widgets, lengthof(_nested_console_window_widgets)
|
||||
std::begin(_nested_console_window_widgets), std::end(_nested_console_window_widgets)
|
||||
);
|
||||
|
||||
struct IConsoleWindow : Window
|
||||
@@ -123,32 +122,23 @@ struct IConsoleWindow : Window
|
||||
static size_t scroll;
|
||||
int line_height; ///< Height of one line of text in the console.
|
||||
int line_offset;
|
||||
GUITimer truncate_timer;
|
||||
|
||||
IConsoleWindow() : Window(&_console_window_desc)
|
||||
{
|
||||
_iconsole_mode = ICONSOLE_OPENED;
|
||||
|
||||
this->InitNested(0);
|
||||
this->truncate_timer.SetInterval(3000);
|
||||
ResizeWindow(this, _screen.width - GetMinButtonSize() * 2, _screen.height / 3);
|
||||
this->left = GetMinButtonSize();
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
// TODO: move this code to SDL2 Emscripten backend
|
||||
EM_ASM({
|
||||
Module.startTextInput(UTF8ToString($0));
|
||||
}, "");
|
||||
#endif // __EMSCRIPTEN__
|
||||
}
|
||||
|
||||
void OnInit() override
|
||||
{
|
||||
this->line_height = FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.hsep_normal;
|
||||
this->line_height = GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.hsep_normal;
|
||||
this->line_offset = GetStringBoundingBox("] ").width + WidgetDimensions::scaled.frametext.left;
|
||||
}
|
||||
|
||||
void Close() override
|
||||
void Close([[maybe_unused]] int data = 0) override
|
||||
{
|
||||
_iconsole_mode = ICONSOLE_CLOSED;
|
||||
VideoDriver::GetInstance()->EditBoxLostFocus();
|
||||
@@ -200,25 +190,6 @@ struct IConsoleWindow : Window
|
||||
if (_focused_window == this && _iconsole_cmdline.caret) {
|
||||
DrawString(this->line_offset + delta + _iconsole_cmdline.caretxoffs, right, this->height - this->line_height, "_", TC_WHITE, SA_LEFT | SA_FORCE);
|
||||
}
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
// TODO: move this code to SDL2 Emscripten backend
|
||||
char buf[512] = "";
|
||||
int status = EM_ASM_INT({
|
||||
var value = Module.getTextInput();
|
||||
if (value == null) {
|
||||
return 0;
|
||||
}
|
||||
var lengthBytes = Math.min(512, lengthBytesUTF8(value) + 1);
|
||||
stringToUTF8(value, $0, lengthBytes);
|
||||
return 1;
|
||||
}, buf);
|
||||
if (status) {
|
||||
this->OnQueryTextFinished(buf);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void OnQueryTextFinished(char *str) override
|
||||
{
|
||||
_focused_window = this;
|
||||
@@ -229,10 +200,8 @@ struct IConsoleWindow : Window
|
||||
this->OnKeyPress(0, WKC_RETURN);
|
||||
}
|
||||
|
||||
void OnRealtimeTick(uint delta_ms) override
|
||||
{
|
||||
if (this->truncate_timer.CountElapsed(delta_ms) == 0) return;
|
||||
|
||||
/** Check on a regular interval if the console buffer needs truncating. */
|
||||
IntervalTimer<TimerWindow> truncate_interval = {std::chrono::seconds(3), [this](auto) {
|
||||
assert(this->height >= 0 && this->line_height > 0);
|
||||
size_t visible_lines = (size_t)(this->height / this->line_height);
|
||||
|
||||
@@ -241,24 +210,14 @@ struct IConsoleWindow : Window
|
||||
IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll, max_scroll);
|
||||
this->SetDirty();
|
||||
}
|
||||
}
|
||||
}};
|
||||
|
||||
void OnMouseLoop() override
|
||||
{
|
||||
if (_iconsole_cmdline.HandleCaret()) this->SetDirty();
|
||||
}
|
||||
|
||||
void OnClick(Point pt, int widget, int click_count) override
|
||||
{
|
||||
#ifdef __EMSCRIPTEN__
|
||||
// TODO: move this code to SDL2 Emscripten backend
|
||||
EM_ASM({
|
||||
Module.startTextInput(UTF8ToString($0));
|
||||
}, "");
|
||||
#endif // __EMSCRIPTEN__
|
||||
}
|
||||
|
||||
EventState OnKeyPress(WChar key, uint16 keycode) override
|
||||
EventState OnKeyPress([[maybe_unused]] char32_t key, uint16_t keycode) override
|
||||
{
|
||||
if (_focused_window != this) return ES_NOT_HANDLED;
|
||||
|
||||
@@ -329,7 +288,7 @@ struct IConsoleWindow : Window
|
||||
return ES_HANDLED;
|
||||
}
|
||||
|
||||
void InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end) override
|
||||
void InsertTextString(WidgetID, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end) override
|
||||
{
|
||||
if (_iconsole_cmdline.InsertString(str, marked, caret, insert_location, replacement_end)) {
|
||||
IConsoleWindow::scroll = 0;
|
||||
@@ -338,22 +297,9 @@ struct IConsoleWindow : Window
|
||||
}
|
||||
}
|
||||
|
||||
const char *GetFocusedText() const override
|
||||
Textbuf *GetFocusedTextbuf() const override
|
||||
{
|
||||
return _iconsole_cmdline.buf;
|
||||
}
|
||||
|
||||
const char *GetCaret() const override
|
||||
{
|
||||
return _iconsole_cmdline.buf + _iconsole_cmdline.caretpos;
|
||||
}
|
||||
|
||||
const char *GetMarkedText(size_t *length) const override
|
||||
{
|
||||
if (_iconsole_cmdline.markend == 0) return nullptr;
|
||||
|
||||
*length = _iconsole_cmdline.markend - _iconsole_cmdline.markpos;
|
||||
return _iconsole_cmdline.buf + _iconsole_cmdline.markpos;
|
||||
return &_iconsole_cmdline;
|
||||
}
|
||||
|
||||
Point GetCaretPosition() const override
|
||||
@@ -369,17 +315,17 @@ struct IConsoleWindow : Window
|
||||
int delta = std::min<int>(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
|
||||
|
||||
Point p1 = GetCharPosInString(_iconsole_cmdline.buf, from, FS_NORMAL);
|
||||
Point p2 = from != to ? GetCharPosInString(_iconsole_cmdline.buf, from) : p1;
|
||||
Point p2 = from != to ? GetCharPosInString(_iconsole_cmdline.buf, to, FS_NORMAL) : p1;
|
||||
|
||||
Rect r = {this->line_offset + delta + p1.x, this->height - this->line_height, this->line_offset + delta + p2.x, this->height};
|
||||
return r;
|
||||
}
|
||||
|
||||
const char *GetTextCharacterAtPosition(const Point &pt) const override
|
||||
ptrdiff_t GetTextCharacterAtPosition(const Point &pt) const override
|
||||
{
|
||||
int delta = std::min<int>(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
|
||||
|
||||
if (!IsInsideMM(pt.y, this->height - this->line_height, this->height)) return nullptr;
|
||||
if (!IsInsideMM(pt.y, this->height - this->line_height, this->height)) return -1;
|
||||
|
||||
return GetCharAtPosition(_iconsole_cmdline.buf, pt.x - delta);
|
||||
}
|
||||
@@ -394,7 +340,7 @@ struct IConsoleWindow : Window
|
||||
VideoDriver::GetInstance()->EditBoxGainedFocus();
|
||||
}
|
||||
|
||||
void OnFocusLost() override
|
||||
void OnFocusLost(bool) override
|
||||
{
|
||||
VideoDriver::GetInstance()->EditBoxLostFocus();
|
||||
}
|
||||
@@ -408,7 +354,6 @@ void IConsoleGUIInit()
|
||||
_iconsole_mode = ICONSOLE_CLOSED;
|
||||
|
||||
IConsoleClearBuffer();
|
||||
memset(_iconsole_history, 0, sizeof(_iconsole_history));
|
||||
|
||||
IConsolePrint(TC_LIGHT_BLUE, "OpenTTD Game Console Revision 7 - {}", _openttd_revision);
|
||||
IConsolePrint(CC_WHITE, "------------------------------------");
|
||||
@@ -499,15 +444,14 @@ static const char *IConsoleHistoryAdd(const char *cmd)
|
||||
if (StrEmpty(cmd)) return nullptr;
|
||||
|
||||
/* Do not put in history if command is same as previous */
|
||||
if (_iconsole_history[0] == nullptr || strcmp(_iconsole_history[0], cmd) != 0) {
|
||||
free(_iconsole_history[ICON_HISTORY_SIZE - 1]);
|
||||
memmove(&_iconsole_history[1], &_iconsole_history[0], sizeof(_iconsole_history[0]) * (ICON_HISTORY_SIZE - 1));
|
||||
_iconsole_history[0] = stredup(cmd);
|
||||
if (_iconsole_history.empty() || _iconsole_history.front() != cmd) {
|
||||
_iconsole_history.emplace_front(cmd);
|
||||
while (_iconsole_history.size() > ICON_HISTORY_SIZE) _iconsole_history.pop_back();
|
||||
}
|
||||
|
||||
/* Reset the history position */
|
||||
IConsoleResetHistoryPos();
|
||||
return _iconsole_history[0];
|
||||
return _iconsole_history.front().c_str();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -516,10 +460,8 @@ static const char *IConsoleHistoryAdd(const char *cmd)
|
||||
*/
|
||||
static void IConsoleHistoryNavigate(int direction)
|
||||
{
|
||||
if (_iconsole_history[0] == nullptr) return; // Empty history
|
||||
_iconsole_historypos = Clamp(_iconsole_historypos + direction, -1, ICON_HISTORY_SIZE - 1);
|
||||
|
||||
if (direction > 0 && _iconsole_history[_iconsole_historypos] == nullptr) _iconsole_historypos--;
|
||||
if (_iconsole_history.empty()) return; // Empty history
|
||||
_iconsole_historypos = Clamp<ptrdiff_t>(_iconsole_historypos + direction, -1, _iconsole_history.size() - 1);
|
||||
|
||||
if (_iconsole_historypos == -1) {
|
||||
_iconsole_cmdline.DeleteAll();
|
||||
@@ -537,7 +479,7 @@ static void IConsoleHistoryNavigate(int direction)
|
||||
* @param colour_code the colour of the command. Red in case of errors, etc.
|
||||
* @param str the message entered or output on the console (notice, error, etc.)
|
||||
*/
|
||||
void IConsoleGUIPrint(TextColour colour_code, char *str)
|
||||
void IConsoleGUIPrint(TextColour colour_code, const std::string &str)
|
||||
{
|
||||
_iconsole_buffer.push_front(IConsoleLine(str, colour_code));
|
||||
SetWindowDirty(WC_CONSOLE, 0);
|
||||
@@ -549,7 +491,7 @@ void IConsoleGUIPrint(TextColour colour_code, char *str)
|
||||
* 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;
|
||||
|
||||
Reference in New Issue
Block a user