Files
OpenRCT2-Web/src/openrct2-ui/interface/InGameConsole.cpp
T

371 lines
12 KiB
C++
Raw Normal View History

2018-06-15 14:07:34 +02:00
/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 OpenRCT2 developers
2018-06-15 14:07:34 +02:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-06-22 23:20:47 +02:00
#include "InGameConsole.h"
#include "Theme.h"
2018-03-11 19:55:52 +00:00
#include <algorithm>
2018-03-20 08:24:41 +01:00
#include <cstring>
2018-03-11 22:25:34 +00:00
#include <openrct2/Context.h>
2018-06-22 23:20:47 +02:00
#include <openrct2/Version.h>
#include <openrct2/config/Config.h>
2018-03-20 08:24:41 +01:00
#include <openrct2/drawing/Drawing.h>
2018-03-11 22:25:34 +00:00
#include <openrct2/interface/Colour.h>
#include <openrct2/interface/Window.h>
#include <openrct2/localisation/Language.h>
2018-04-26 23:03:02 +01:00
#include <openrct2/localisation/LocalisationService.h>
2018-03-11 22:25:34 +00:00
using namespace OpenRCT2::Ui;
2018-03-11 19:55:52 +00:00
static InGameConsole _inGameConsole;
static FontStyle InGameConsoleGetFontStyle()
{
return (gConfigInterface.ConsoleSmallFont ? FontStyle::Small : FontStyle::Medium);
}
static int32_t InGameConsoleGetLineHeight()
{
return font_get_line_height(InGameConsoleGetFontStyle());
}
2018-03-11 19:55:52 +00:00
InGameConsole::InGameConsole()
{
InteractiveConsole::WriteLine(OPENRCT2_NAME " " OPENRCT2_VERSION);
InteractiveConsole::WriteLine("Type 'help' for a list of available commands. Type 'hide' to hide the console.");
InteractiveConsole::WriteLine("");
WritePrompt();
}
void InGameConsole::WritePrompt()
{
InteractiveConsole::WriteLine("> ");
}
void InGameConsole::Input(ConsoleInput input)
2018-03-11 19:55:52 +00:00
{
2018-06-22 23:20:47 +02:00
switch (input)
{
case ConsoleInput::LineClear:
2018-03-11 19:55:52 +00:00
ClearInput();
RefreshCaret();
2018-06-22 23:20:47 +02:00
break;
case ConsoleInput::LineExecute:
2018-06-22 23:20:47 +02:00
if (_consoleCurrentLine[0] != '\0')
{
HistoryAdd(_consoleCurrentLine);
// Append text we are executing to prompt line
_consoleLines.back().append(_consoleCurrentLine);
Execute(_consoleCurrentLine);
WritePrompt();
ClearInput();
RefreshCaret();
}
ScrollToEnd();
break;
case ConsoleInput::HistoryPrevious:
2018-06-22 23:20:47 +02:00
if (_consoleHistoryIndex > 0)
{
_consoleHistoryIndex--;
std::memcpy(_consoleCurrentLine, _consoleHistory[_consoleHistoryIndex], CONSOLE_INPUT_SIZE);
2018-06-22 23:20:47 +02:00
}
2018-03-11 19:55:52 +00:00
_consoleTextInputSession->Size = strlen(_consoleTextInputSession->Buffer);
_consoleTextInputSession->Length = utf8_length(_consoleTextInputSession->Buffer);
_consoleTextInputSession->SelectionStart = strlen(_consoleCurrentLine);
2018-06-22 23:20:47 +02:00
break;
case ConsoleInput::HistoryNext:
2018-06-22 23:20:47 +02:00
if (_consoleHistoryIndex < _consoleHistoryCount - 1)
{
_consoleHistoryIndex++;
std::memcpy(_consoleCurrentLine, _consoleHistory[_consoleHistoryIndex], CONSOLE_INPUT_SIZE);
2018-06-22 23:20:47 +02:00
_consoleTextInputSession->Size = strlen(_consoleTextInputSession->Buffer);
_consoleTextInputSession->Length = utf8_length(_consoleTextInputSession->Buffer);
_consoleTextInputSession->SelectionStart = strlen(_consoleCurrentLine);
}
else
{
_consoleHistoryIndex = _consoleHistoryCount;
ClearInput();
}
break;
case ConsoleInput::ScrollPrevious:
2018-06-22 23:20:47 +02:00
{
int32_t scrollAmt = GetNumVisibleLines() - 1;
Scroll(scrollAmt);
break;
2018-03-11 19:55:52 +00:00
}
case ConsoleInput::ScrollNext:
2018-06-22 23:20:47 +02:00
{
int32_t scrollAmt = GetNumVisibleLines() - 1;
Scroll(-scrollAmt);
break;
}
default:
break;
2018-03-11 19:55:52 +00:00
}
}
void InGameConsole::ClearInput()
{
_consoleCurrentLine[0] = 0;
2018-06-22 23:20:47 +02:00
if (_isOpen)
{
2022-11-06 20:49:07 +00:00
ContextStartTextInput(_consoleCurrentLine, sizeof(_consoleCurrentLine));
2018-03-11 19:55:52 +00:00
}
}
2018-06-22 23:20:47 +02:00
void InGameConsole::HistoryAdd(const utf8* src)
2018-03-11 19:55:52 +00:00
{
2018-06-22 23:20:47 +02:00
if (_consoleHistoryCount >= CONSOLE_HISTORY_SIZE)
{
for (int32_t i = 0; i < _consoleHistoryCount - 1; i++)
std::memcpy(_consoleHistory[i], _consoleHistory[i + 1], CONSOLE_INPUT_SIZE);
2018-03-11 19:55:52 +00:00
_consoleHistoryCount--;
}
std::memcpy(_consoleHistory[_consoleHistoryCount++], src, CONSOLE_INPUT_SIZE);
2018-03-11 19:55:52 +00:00
_consoleHistoryIndex = _consoleHistoryCount;
}
void InGameConsole::ScrollToEnd()
{
const int32_t maxLines = GetNumVisibleLines();
if (maxLines == 0)
_consoleScrollPos = 0;
else
2020-03-28 16:36:51 -03:00
_consoleScrollPos = std::max<int32_t>(0, static_cast<int32_t>(_consoleLines.size()) - maxLines);
2018-03-11 19:55:52 +00:00
}
2020-10-08 01:45:57 +01:00
void InGameConsole::RefreshCaret(size_t position)
2018-03-11 19:55:52 +00:00
{
_consoleCaretTicks = 0;
2020-10-08 01:45:57 +01:00
_selectionStart = position;
char tempString[TEXT_INPUT_SIZE] = { 0 };
std::memcpy(tempString, &_consoleCurrentLine, _selectionStart);
_caretScreenPosX = gfx_get_string_width_no_formatting(tempString, InGameConsoleGetFontStyle());
2018-03-11 19:55:52 +00:00
}
void InGameConsole::Scroll(int32_t linesToScroll)
2018-03-11 19:55:52 +00:00
{
const int32_t maxVisibleLines = GetNumVisibleLines();
2020-03-28 16:36:51 -03:00
const int32_t numLines = static_cast<int32_t>(_consoleLines.size());
2018-03-11 19:55:52 +00:00
if (numLines > maxVisibleLines)
{
int32_t maxScrollValue = numLines - maxVisibleLines;
2018-08-12 12:50:40 +01:00
_consoleScrollPos = std::clamp<int32_t>(_consoleScrollPos - linesToScroll, 0, maxScrollValue);
2018-03-11 19:55:52 +00:00
}
}
void InGameConsole::Clear()
{
_consoleLines.clear();
ScrollToEnd();
}
void InGameConsole::ClearLine()
{
_consoleCurrentLine[0] = 0;
RefreshCaret();
}
void InGameConsole::Open()
{
2018-03-11 21:18:12 +00:00
_isOpen = true;
2018-03-11 19:55:52 +00:00
ScrollToEnd();
RefreshCaret();
2022-11-06 20:49:07 +00:00
_consoleTextInputSession = ContextStartTextInput(_consoleCurrentLine, sizeof(_consoleCurrentLine));
2018-03-11 19:55:52 +00:00
}
void InGameConsole::Close()
{
2018-03-11 21:12:22 +00:00
_consoleTextInputSession = nullptr;
2018-03-11 21:18:12 +00:00
_isOpen = false;
2018-03-11 19:55:52 +00:00
Invalidate();
2022-11-06 20:49:07 +00:00
ContextStopTextInput();
2018-03-11 19:55:52 +00:00
}
2018-03-22 20:29:41 +00:00
void InGameConsole::Hide()
{
Close();
}
2018-03-11 19:55:52 +00:00
void InGameConsole::Toggle()
{
2018-03-11 21:18:12 +00:00
if (_isOpen)
{
Close();
}
2018-03-11 19:55:52 +00:00
else
2018-03-11 21:18:12 +00:00
{
Open();
}
2018-03-11 19:55:52 +00:00
}
2020-10-16 00:13:52 +01:00
void InGameConsole::WriteLine(const std::string& input, FormatToken colourFormat)
2018-03-11 19:55:52 +00:00
{
// Include text colour format only for special cases
// The draw function handles the default text colour differently
2020-10-16 00:13:52 +01:00
auto colourCodepoint = "";
if (colourFormat != FormatToken::ColourWindow2)
2020-11-27 18:54:51 +00:00
colourCodepoint = "{WINDOW_COLOUR_2}";
2018-03-11 19:55:52 +00:00
std::string line;
2018-06-22 23:20:47 +02:00
std::size_t splitPos = 0;
2018-03-11 19:55:52 +00:00
std::size_t stringOffset = 0;
while (splitPos != std::string::npos)
{
splitPos = input.find('\n', stringOffset);
2018-06-22 23:20:47 +02:00
line = input.substr(stringOffset, splitPos - stringOffset);
_consoleLines.emplace_back(colourCodepoint + line);
2018-03-11 19:55:52 +00:00
stringOffset = splitPos + 1;
}
if (_consoleLines.size() > CONSOLE_MAX_LINES)
{
const std::size_t linesToErase = _consoleLines.size() - CONSOLE_MAX_LINES;
_consoleLines.erase(_consoleLines.begin(), _consoleLines.begin() + linesToErase);
}
}
2018-03-11 21:18:12 +00:00
void InGameConsole::Invalidate() const
2018-03-11 19:55:52 +00:00
{
2021-03-27 01:00:09 -03:00
gfx_set_dirty_blocks({ _consoleTopLeft, _consoleBottomRight });
2018-03-11 19:55:52 +00:00
}
void InGameConsole::Update()
{
2021-03-27 01:00:09 -03:00
_consoleTopLeft = { 0, 0 };
2022-11-06 20:49:07 +00:00
_consoleBottomRight = { ContextGetWidth(), 322 };
2018-03-11 19:55:52 +00:00
2018-06-22 23:20:47 +02:00
if (_isOpen)
{
2018-03-11 19:55:52 +00:00
// When scrolling the map, the console pixels get copied... therefore invalidate the screen
2018-06-22 23:20:47 +02:00
rct_window* mainWindow = window_get_main();
if (mainWindow != nullptr)
{
rct_viewport* mainViewport = window_get_viewport(mainWindow);
if (mainViewport != nullptr)
{
2020-03-01 10:34:58 -03:00
if (_lastMainViewport != mainViewport->viewPos)
2018-06-22 23:20:47 +02:00
{
2020-03-01 10:34:58 -03:00
_lastMainViewport = mainViewport->viewPos;
2018-03-11 19:55:52 +00:00
gfx_invalidate_screen();
}
}
}
}
// Flash the caret
_consoleCaretTicks = (_consoleCaretTicks + 1) % 30;
}
2018-06-22 23:20:47 +02:00
void InGameConsole::Draw(rct_drawpixelinfo* dpi) const
2018-03-11 19:55:52 +00:00
{
2018-03-11 21:18:12 +00:00
if (!_isOpen)
2018-03-11 19:55:52 +00:00
return;
// Set font
uint8_t textColour = NOT_TRANSLUCENT(ThemeGetColour(WindowClass::Console, 1));
const int32_t lineHeight = InGameConsoleGetLineHeight();
2018-06-22 23:20:47 +02:00
const int32_t maxLines = GetNumVisibleLines();
2018-03-11 19:55:52 +00:00
// This is something of a hack to ensure the text is actually black
// as opposed to a desaturated grey
2021-11-24 17:08:41 +00:00
thread_local std::string _colourFormatStr;
_colourFormatStr.clear();
2018-03-11 19:55:52 +00:00
if (textColour == COLOUR_BLACK)
{
2021-11-24 17:08:41 +00:00
_colourFormatStr = "{BLACK}";
2018-03-11 19:55:52 +00:00
}
// TTF looks far better without the outlines
2018-04-26 23:03:02 +01:00
if (!LocalisationService_UseTrueTypeFont())
2018-03-11 19:55:52 +00:00
{
textColour |= COLOUR_FLAG_OUTLINE;
}
Invalidate();
// Give console area a translucent effect.
2021-03-27 01:00:09 -03:00
gfx_filter_rect(dpi, { _consoleTopLeft, _consoleBottomRight }, FilterPaletteID::Palette51);
2018-03-11 19:55:52 +00:00
// Make input area more opaque.
gfx_filter_rect(
dpi, { { _consoleTopLeft.x, _consoleBottomRight.y - lineHeight - 10 }, _consoleBottomRight - ScreenCoordsXY{ 0, 1 } },
FilterPaletteID::Palette51);
2018-03-11 19:55:52 +00:00
// Paint background colour.
uint8_t backgroundColour = ThemeGetColour(WindowClass::Console, 0);
2021-03-27 01:00:09 -03:00
gfx_fill_rect_inset(dpi, { _consoleTopLeft, _consoleBottomRight }, backgroundColour, INSET_RECT_FLAG_FILL_NONE);
2018-06-22 23:20:47 +02:00
gfx_fill_rect_inset(
2021-03-27 01:00:09 -03:00
dpi, { _consoleTopLeft + ScreenCoordsXY{ 1, 1 }, _consoleBottomRight - ScreenCoordsXY{ 1, 1 } }, backgroundColour,
2018-06-22 23:20:47 +02:00
INSET_RECT_FLAG_BORDER_INSET);
2018-03-11 19:55:52 +00:00
std::string lineBuffer;
2021-03-27 01:00:09 -03:00
auto screenCoords = _consoleTopLeft + ScreenCoordsXY{ CONSOLE_EDGE_PADDING, CONSOLE_EDGE_PADDING };
2018-03-11 19:55:52 +00:00
// Draw text inside console
2020-03-28 16:36:51 -03:00
for (std::size_t i = 0; i < _consoleLines.size() && i < static_cast<size_t>(maxLines); i++)
2018-06-22 23:20:47 +02:00
{
2018-03-11 19:55:52 +00:00
const size_t index = i + _consoleScrollPos;
2021-11-24 17:08:41 +00:00
lineBuffer = _colourFormatStr + _consoleLines[index];
gfx_draw_string(dpi, screenCoords, lineBuffer.c_str(), { textColour, InGameConsoleGetFontStyle() });
screenCoords.y += lineHeight;
2018-03-11 19:55:52 +00:00
}
2021-03-27 01:00:09 -03:00
screenCoords.y = _consoleBottomRight.y - lineHeight - CONSOLE_EDGE_PADDING - 1;
2018-03-11 19:55:52 +00:00
// Draw current line
2021-11-24 17:08:41 +00:00
lineBuffer = _colourFormatStr + _consoleCurrentLine;
gfx_draw_string_no_formatting(dpi, screenCoords, lineBuffer.c_str(), { TEXT_COLOUR_255, InGameConsoleGetFontStyle() });
2018-03-11 19:55:52 +00:00
// Draw caret
2018-06-22 23:20:47 +02:00
if (_consoleCaretTicks < CONSOLE_CARET_FLASH_THRESHOLD)
{
2020-10-08 01:45:57 +01:00
auto caret = screenCoords + ScreenCoordsXY{ _caretScreenPosX, lineHeight };
uint8_t caretColour = ColourMapA[BASE_COLOUR(textColour)].lightest;
2020-10-08 01:45:57 +01:00
gfx_fill_rect(dpi, { caret, caret + ScreenCoordsXY{ CONSOLE_CARET_WIDTH, 1 } }, caretColour);
2018-03-11 19:55:52 +00:00
}
// What about border colours?
uint8_t borderColour1 = ColourMapA[BASE_COLOUR(backgroundColour)].light;
uint8_t borderColour2 = ColourMapA[BASE_COLOUR(backgroundColour)].mid_dark;
2018-03-11 19:55:52 +00:00
// Input area top border
2018-06-22 23:20:47 +02:00
gfx_fill_rect(
2021-03-27 01:00:09 -03:00
dpi,
{ { _consoleTopLeft.x, _consoleBottomRight.y - lineHeight - 11 },
{ _consoleBottomRight.x, _consoleBottomRight.y - lineHeight - 11 } },
borderColour1);
2018-06-22 23:20:47 +02:00
gfx_fill_rect(
2021-03-27 01:00:09 -03:00
dpi,
{ { _consoleTopLeft.x, _consoleBottomRight.y - lineHeight - 10 },
{ _consoleBottomRight.x, _consoleBottomRight.y - lineHeight - 10 } },
borderColour2);
2018-03-11 19:55:52 +00:00
// Input area bottom border
2021-03-27 01:00:09 -03:00
gfx_fill_rect(
dpi, { { _consoleTopLeft.x, _consoleBottomRight.y - 1 }, { _consoleBottomRight.x, _consoleBottomRight.y - 1 } },
borderColour1);
gfx_fill_rect(dpi, { { _consoleTopLeft.x, _consoleBottomRight.y }, _consoleBottomRight }, borderColour2);
2018-03-11 19:55:52 +00:00
}
// Calculates the amount of visible lines, based on the console size, excluding the input line.
int32_t InGameConsole::GetNumVisibleLines() const
2018-03-11 19:55:52 +00:00
{
const int32_t lineHeight = InGameConsoleGetLineHeight();
2021-03-27 01:00:09 -03:00
const int32_t consoleHeight = _consoleBottomRight.y - _consoleTopLeft.y;
if (consoleHeight == 0)
return 0;
const int32_t drawableHeight = consoleHeight - 2 * lineHeight - 4; // input line, separator - padding
2018-03-11 19:55:52 +00:00
return drawableHeight / lineHeight;
}