1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-26 16:24:35 +01:00

Merge pull request #20700 from ZehMatt/string-refactor

String refactor
This commit is contained in:
Matt
2023-09-28 20:36:45 +03:00
committed by GitHub
10 changed files with 34 additions and 67 deletions

View File

@@ -106,7 +106,6 @@ static TrackDesign* _trackDesign;
static std::vector<LoadSaveListItem> _listItems;
static char _directory[MAX_PATH];
static char _shortenedDirectory[MAX_PATH];
static char _parentDirectory[MAX_PATH];
static u8string _extensionPattern;
static u8string _defaultPath;
@@ -508,7 +507,6 @@ public:
SafeStrCpy(_directory, absoluteDirectory.c_str(), std::size(_directory));
// Note: This compares the pointers, not values
_extensionPattern = extensionPattern;
_shortenedDirectory[0] = '\0';
_listItems.clear();
@@ -741,18 +739,15 @@ public:
{
DrawWidgets(dpi);
if (_shortenedDirectory[0] == '\0')
{
ShortenPath(_shortenedDirectory, sizeof(_shortenedDirectory), _directory, width - 8, FontStyle::Medium);
}
const auto shortPath = ShortenPath(_directory, width - 8, FontStyle::Medium);
// Format text
thread_local std::string _buffer;
_buffer.assign("{BLACK}");
_buffer += _shortenedDirectory;
std::string buffer;
buffer.assign("{BLACK}");
buffer += shortPath;
// Draw path text
const auto normalisedPath = Platform::StrDecompToPrecomp(_buffer.data());
const auto normalisedPath = Platform::StrDecompToPrecomp(buffer.data());
const auto* normalisedPathC = normalisedPath.c_str();
auto ft = Formatter();
ft.Add<const char*>(normalisedPathC);

View File

@@ -367,7 +367,7 @@ public:
SetPage(_currentTab);
break;
case WIDX_FILTER_TEXT_BOX:
WindowStartTextbox(*this, widgetIndex, STR_STRING, _filter.data(), MAX_PATH);
WindowStartTextbox(*this, widgetIndex, STR_STRING, _filter.data(), TEXT_INPUT_SIZE);
break;
case WIDX_FILTER_CLEAR_BUTTON:
_filter.clear();

View File

@@ -218,13 +218,10 @@ public:
// Scenario path
if (gConfigGeneral.DebuggingTools)
{
utf8 path[MAX_PATH];
const auto shortPath = ShortenPath(scenario->Path, width - 6 - TabWidth, FontStyle::Medium);
ShortenPath(path, sizeof(path), scenario->Path, width - 6 - TabWidth, FontStyle::Medium);
const utf8* pathPtr = path;
auto ft = Formatter();
ft.Add<const char*>(pathPtr);
ft.Add<utf8*>(shortPath.c_str());
DrawTextBasic(dpi, windowPos + ScreenCoordsXY{ TabWidth + 3, height - 3 - 11 }, STR_STRING, ft, { colours[1] });
}

View File

@@ -271,7 +271,7 @@ public:
Invalidate();
break;
case WIDX_FILTER_TEXT_BOX:
WindowStartTextbox(*this, widgetIndex, STR_STRING, _filteredSceneryTab.Filter.data(), MAX_PATH);
WindowStartTextbox(*this, widgetIndex, STR_STRING, _filteredSceneryTab.Filter.data(), TEXT_INPUT_SIZE);
break;
case WIDX_FILTER_CLEAR_BUTTON:
_tabEntries[_activeTabIndex].Filter.clear();

View File

@@ -470,11 +470,9 @@ public:
// Show track file path (in debug mode)
if (gConfigGeneral.DebuggingTools)
{
utf8 pathBuffer[MAX_PATH];
const utf8* pathPtr = pathBuffer;
ShortenPath(pathBuffer, sizeof(pathBuffer), path.c_str(), width, FontStyle::Medium);
const auto shortPath = ShortenPath(path, width, FontStyle::Medium);
auto ft = Formatter();
ft.Add<utf8*>(pathPtr);
ft.Add<utf8*>(shortPath.c_str());
DrawTextBasic(
dpi, windowPos + ScreenCoordsXY{ 0, height - DEBUG_PATH_HEIGHT - 3 }, STR_STRING, ft,
{ colours[1] }); // TODO Check dpi

View File

@@ -26,12 +26,6 @@
#include <cstddef>
#include <cstdint>
// Define MAX_PATH for various headers that don't want to include system headers
// just for MAX_PATH
#ifndef MAX_PATH
# define MAX_PATH 260
#endif
using colour_t = uint8_t;
// Gets the name of a symbol as a C string

View File

@@ -1075,45 +1075,28 @@ void GfxDrawStringWithYOffsets(
dpi.lastStringPos = { info.x, info.y };
}
void ShortenPath(utf8* buffer, size_t bufferSize, const utf8* path, int32_t availableWidth, FontStyle fontStyle)
u8string ShortenPath(const u8string& path, int32_t availableWidth, FontStyle fontStyle)
{
size_t length = strlen(path);
// Return full string if it fits
if (GfxGetStringWidth(const_cast<char*>(path), fontStyle) <= availableWidth)
if (GfxGetStringWidth(path, fontStyle) <= availableWidth)
{
SafeStrCpy(buffer, path, bufferSize);
return;
return path;
}
// Count path separators
int32_t path_separators = 0;
for (size_t x = 0; x < length; x++)
u8string shortenedPath = u8"...";
size_t begin = 0;
while (begin < path.size())
{
if (path[x] == *PATH_SEPARATOR || path[x] == '/')
begin = path.find_first_of(*PATH_SEPARATOR, begin + 1);
if (begin == path.npos)
break;
shortenedPath = u8"..." + path.substr(begin);
if (GfxGetStringWidth(shortenedPath, fontStyle) <= availableWidth)
{
path_separators++;
return shortenedPath;
}
}
// TODO: Replace with unicode ellipsis when supported
SafeStrCpy(buffer, "...", bufferSize);
// Abbreviate beginning with xth separator
int32_t begin = -1;
for (int32_t x = 0; x < path_separators; x++)
{
do
{
begin++;
} while (path[begin] != *PATH_SEPARATOR && path[begin] != '/');
SafeStrCpy(buffer + 3, path + begin, bufferSize - 3);
if (GfxGetStringWidth(buffer, fontStyle) <= availableWidth)
{
return;
}
}
SafeStrCpy(buffer, path, bufferSize);
return shortenedPath;
}

View File

@@ -572,7 +572,7 @@ int32_t GfxGetStringWidthNewLined(std::string_view text, FontStyle fontStyle);
int32_t GfxGetStringWidthNoFormatting(std::string_view text, FontStyle fontStyle);
int32_t StringGetHeightRaw(std::string_view text, FontStyle fontStyle);
int32_t GfxClipString(char* buffer, int32_t width, FontStyle fontStyle);
void ShortenPath(utf8* buffer, size_t bufferSize, const utf8* path, int32_t availableWidth, FontStyle fontStyle);
u8string ShortenPath(const u8string& path, int32_t availableWidth, FontStyle fontStyle);
void TTFDrawString(
DrawPixelInfo& dpi, const_utf8string text, int32_t colour, const ScreenCoordsXY& coords, bool noFormatting,
FontStyle fontStyle, TextDarkness darkness);

View File

@@ -204,10 +204,10 @@ private:
{
auto& objRepository = OpenRCT2::GetContext()->GetObjectRepository();
auto importer = ParkImporter::CreateParkFile(objRepository);
importer->LoadScenario(path.c_str(), true);
importer->LoadScenario(path, true);
if (importer->GetDetails(entry))
{
String::Set(entry->Path, sizeof(entry->Path), path.c_str());
entry->Path = path;
entry->Timestamp = timestamp;
result = true;
}
@@ -225,10 +225,10 @@ private:
try
{
auto s4Importer = ParkImporter::CreateS4();
s4Importer->LoadScenario(path.c_str(), true);
s4Importer->LoadScenario(path, true);
if (s4Importer->GetDetails(entry))
{
String::Set(entry->Path, sizeof(entry->Path), path.c_str());
entry->Path = path;
entry->Timestamp = timestamp;
result = true;
}
@@ -273,7 +273,7 @@ private:
ScenarioIndexEntry entry = {};
// Set new entry
String::Set(entry.Path, sizeof(entry.Path), path.c_str());
entry.Path = path;
entry.Timestamp = timestamp;
entry.Category = s6Info->Category;
entry.ObjectiveType = s6Info->ObjectiveType;
@@ -562,7 +562,7 @@ private:
if (existingEntry->Timestamp > entry.Timestamp)
{
// Existing entry is more recent
conflictPath = String::ToStd(existingEntry->Path);
conflictPath = existingEntry->Path;
// Overwrite existing entry with this one
*existingEntry = entry;

View File

@@ -42,7 +42,7 @@ enum class ScenarioSource : uint8_t
struct ScenarioIndexEntry
{
utf8 Path[MAX_PATH];
u8string Path;
uint64_t Timestamp;
// Category / sequence