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

377 lines
12 KiB
C++
Raw Normal View History

2018-06-15 14:07:34 +02:00
/*****************************************************************************
* Copyright (c) 2014-2024 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 "../UiStringIds.h"
2018-06-22 23:20:47 +02:00
#include "Theme.h"
#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
2024-05-10 12:10:21 +02:00
using namespace OpenRCT2;
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()
{
2024-05-10 12:10:21 +02:00
return (Config::Get().interface.ConsoleSmallFont ? FontStyle::Small : FontStyle::Medium);
}
static int32_t InGameConsoleGetLineHeight()
{
return FontGetLineHeight(InGameConsoleGetFontStyle());
}
void InGameConsole::WriteInitial()
2018-03-11 19:55:52 +00:00
{
InteractiveConsole::WriteLine(OPENRCT2_NAME " " OPENRCT2_VERSION);
InteractiveConsole::WriteLine(LanguageGetString(STR_CONSOLE_HELPER_TEXT));
2018-03-11 19:55:52 +00:00
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--;
_consoleCurrentLine = _consoleHistory[_consoleHistoryIndex];
2018-06-22 23:20:47 +02:00
}
_consoleTextInputSession->Length = UTF8Length(_consoleCurrentLine.c_str());
_consoleTextInputSession->SelectionStart = _consoleCurrentLine.size();
RefreshCaret(_consoleTextInputSession->SelectionStart);
2018-06-22 23:20:47 +02:00
break;
case ConsoleInput::HistoryNext:
if (_consoleHistoryIndex + 1 < _consoleHistory.size())
2018-06-22 23:20:47 +02:00
{
_consoleHistoryIndex++;
_consoleCurrentLine = _consoleHistory[_consoleHistoryIndex];
_consoleTextInputSession->Length = UTF8Length(_consoleCurrentLine.c_str());
_consoleTextInputSession->SelectionStart = _consoleCurrentLine.size();
2018-06-22 23:20:47 +02:00
}
else
{
_consoleHistoryIndex = _consoleHistory.size();
2018-06-22 23:20:47 +02:00
ClearInput();
}
RefreshCaret(_consoleTextInputSession->SelectionStart);
2018-06-22 23:20:47 +02:00
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.clear();
2018-06-22 23:20:47 +02:00
if (_isOpen)
{
_consoleTextInputSession = ContextStartTextInput(_consoleCurrentLine, CONSOLE_INPUT_SIZE);
2018-03-11 19:55:52 +00:00
}
}
void InGameConsole::HistoryAdd(const u8string& src)
2018-03-11 19:55:52 +00:00
{
if (_consoleHistory.size() >= CONSOLE_HISTORY_SIZE)
2018-06-22 23:20:47 +02:00
{
_consoleHistory.pop_front();
2018-03-11 19:55:52 +00:00
}
_consoleHistory.push_back(src);
_consoleHistoryIndex = _consoleHistory.size();
2018-03-11 19:55:52 +00:00
}
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;
auto text = u8string_view{ _consoleCurrentLine }.substr(0, _selectionStart);
_caretScreenPosX = GfxGetStringWidthNoFormatting(text, 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()
{
if (!_isInitialised)
{
WriteInitial();
_isInitialised = true;
}
2018-03-11 21:18:12 +00:00
_isOpen = true;
2018-03-11 19:55:52 +00:00
ScrollToEnd();
RefreshCaret();
_consoleTextInputSession = ContextStartTextInput(_consoleCurrentLine, CONSOLE_INPUT_SIZE);
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
{
GfxSetDirtyBlocks({ _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
WindowBase* mainWindow = WindowGetMain();
2018-06-22 23:20:47 +02:00
if (mainWindow != nullptr)
{
Viewport* mainViewport = WindowGetViewport(mainWindow);
2018-06-22 23:20:47 +02:00
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
GfxInvalidateScreen();
2018-03-11 19:55:52 +00:00
}
}
}
}
// Flash the caret
_consoleCaretTicks = (_consoleCaretTicks + 1) % 30;
}
void InGameConsole::Draw(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
2024-05-09 02:45:07 +02:00
ColourWithFlags textColour = { ThemeGetColour(WindowClass::Console, 1).colour, 0 };
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();
2024-05-09 02:45:07 +02:00
if (textColour.colour == COLOUR_BLACK)
2018-03-11 19:55:52 +00:00
{
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
{
2024-05-09 02:45:07 +02:00
textColour.setFlag(ColourFlag::withOutline, true);
2018-03-11 19:55:52 +00:00
}
Invalidate();
// Give console area a translucent effect.
GfxFilterRect(dpi, { _consoleTopLeft, _consoleBottomRight }, FilterPaletteID::Palette51);
2018-03-11 19:55:52 +00:00
// Make input area more opaque.
GfxFilterRect(
dpi, { { _consoleTopLeft.x, _consoleBottomRight.y - lineHeight - 10 }, _consoleBottomRight - ScreenCoordsXY{ 0, 1 } },
FilterPaletteID::Palette51);
2018-03-11 19:55:52 +00:00
// Paint background colour.
2024-05-09 02:45:07 +02:00
auto backgroundColour = ThemeGetColour(WindowClass::Console, 0);
GfxFillRectInset(dpi, { _consoleTopLeft, _consoleBottomRight }, backgroundColour, INSET_RECT_FLAG_FILL_NONE);
GfxFillRectInset(
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];
2024-05-10 10:56:03 +01:00
DrawText(dpi, screenCoords, { textColour, InGameConsoleGetFontStyle() }, lineBuffer.c_str());
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;
DrawText(dpi, screenCoords, { TEXT_COLOUR_255, InGameConsoleGetFontStyle() }, lineBuffer.c_str(), true);
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 };
2024-05-09 02:45:07 +02:00
uint8_t caretColour = ColourMapA[textColour.colour].lightest;
GfxFillRect(dpi, { caret, caret + ScreenCoordsXY{ CONSOLE_CARET_WIDTH, 1 } }, caretColour);
2018-03-11 19:55:52 +00:00
}
// What about border colours?
2024-05-09 02:45:07 +02:00
uint8_t borderColour1 = ColourMapA[backgroundColour.colour].light;
uint8_t borderColour2 = ColourMapA[backgroundColour.colour].mid_dark;
2018-03-11 19:55:52 +00:00
// Input area top border
GfxFillRect(
dpi,
{ { _consoleTopLeft.x, _consoleBottomRight.y - lineHeight - 11 },
{ _consoleBottomRight.x, _consoleBottomRight.y - lineHeight - 11 } },
borderColour1);
GfxFillRect(
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
GfxFillRect(
dpi, { { _consoleTopLeft.x, _consoleBottomRight.y - 1 }, { _consoleBottomRight.x, _consoleBottomRight.y - 1 } },
borderColour1);
GfxFillRect(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;
}