Skip to content

Commit

Permalink
Allow customising the progress string format
Browse files Browse the repository at this point in the history
Fixup: deal with -Wdeprecated-anon-enum-enum-conversion warning
  • Loading branch information
AaronVanGeffen committed May 20, 2024
1 parent 804f63e commit c9de564
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 22 deletions.
6 changes: 4 additions & 2 deletions data/language/en-GB.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2885,7 +2885,7 @@ STR_5755 :Incorrect Software Version (Server is using {STRING})
STR_5756 :Bad Password
STR_5757 :Server Full
STR_5758 :{OUTLINE}{GREEN}{STRING} has joined the game
STR_5759 :Downloading map … ({INT32} / {INT32}) KiB
STR_5759 :Downloading map …
STR_5760 :Hong Kong Dollars (HK$)
STR_5761 :New Taiwan Dollar (NT$)
STR_5762 :Chinese Yuan (CN¥)
Expand Down Expand Up @@ -3449,7 +3449,7 @@ STR_6374 :C
STR_6375 :Unknown Ride
STR_6376 :{WINDOW_COLOUR_2}Ride vehicle:{NEWLINE}{BLACK}{STRINGID} for {STRINGID}
STR_6377 :{WINDOW_COLOUR_2}Type: {BLACK}{STRINGID} for {STRINGID}
STR_6378 :Receiving objects list: {INT32} / {INT32}
STR_6378 :Receiving objects list
STR_6379 :Received invalid data
STR_6380 :Update available!
STR_6381 :Join OpenRCT2 Discord!
Expand Down Expand Up @@ -3713,6 +3713,8 @@ STR_6638 :Enlarged UI
STR_6639 :Modifies the interface to be more suitable for touch usage
STR_6640 :Edit asset packs…
STR_6641 :Loading/progress window
STR_6642 :{STRING} ({COMMA32} / {COMMA32})
STR_6643 :{STRING} ({COMMA32} / {COMMA32} KiB)

#############
# Scenarios #
Expand Down
3 changes: 2 additions & 1 deletion src/openrct2-ui/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ class WindowManager final : public IWindowManager
{
uint32_t currentProgress = intent->GetUIntExtra(INTENT_EXTRA_PROGRESS_OFFSET);
uint32_t totalCount = intent->GetUIntExtra(INTENT_EXTRA_PROGRESS_TOTAL);
ProgressWindowSet(currentProgress, totalCount);
StringId format = intent->GetUIntExtra(INTENT_EXTRA_STRING_ID);
ProgressWindowSet(currentProgress, totalCount, format);
return nullptr;
}

Expand Down
43 changes: 28 additions & 15 deletions src/openrct2-ui/windows/ProgressWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <openrct2/drawing/Drawing.h>
#include <openrct2/drawing/Text.h>
#include <openrct2/localisation/Localisation.h>
#include <openrct2/localisation/StringIds.h>
#include <openrct2/sprites.h>
#include <random>
#include <sstream>
Expand Down Expand Up @@ -70,8 +71,11 @@ namespace OpenRCT2::Ui::Windows
{
private:
close_callback _onClose = nullptr;
std::string _captionTemplate;

StringId _progressFormat;
std::string _progressTitle;
std::string _currentCaption;

uint32_t _currentProgress;
uint32_t _totalCount;
int8_t style = -1;
Expand Down Expand Up @@ -148,18 +152,22 @@ namespace OpenRCT2::Ui::Windows
{
if (_totalCount > 0)
{
std::stringstream caption;
caption << _captionTemplate;
caption << " (" << _currentProgress << " / " << _totalCount << ")";
_currentCaption = caption.str();
auto ft = Formatter();
ft.Add<const char*>(_progressTitle.c_str());
ft.Add<uint32_t>(_currentProgress);
ft.Add<uint32_t>(_totalCount);

_currentCaption = FormatStringIDLegacy(_progressFormat, ft.Data());
}
else
_currentCaption = _captionTemplate;
_currentCaption = _progressTitle;

// Set window title
auto ft = Formatter::Common();
ft.Add<StringId>(STR_STRING);
ft.Add<const char*>(_currentCaption.c_str());
{
auto ft = Formatter::Common();
ft.Add<StringId>(STR_STRING);
ft.Add<const char*>(_currentCaption.c_str());
}
}

void OnDraw(DrawPixelInfo& dpi) override
Expand Down Expand Up @@ -195,9 +203,9 @@ namespace OpenRCT2::Ui::Windows
GfxDrawSprite(clipDPI, variant.vehicle, ScreenCoordsXY(position, widget.bottom + 1));
}

void SetCaptionTemplate(const std::string& text)
void SetCaption(const std::string& text)
{
_captionTemplate = text;
_progressTitle = text;
_currentProgress = 0;
_totalCount = 0;

Expand All @@ -209,8 +217,13 @@ namespace OpenRCT2::Ui::Windows
_onClose = onClose;
}

void SetProgress(uint32_t currentProgress, uint32_t totalCount)
void SetProgress(uint32_t currentProgress, uint32_t totalCount, StringId format)
{
if (format == STR_NONE)
_progressFormat = STR_STRING_M_OF_N;
else
_progressFormat = format;

_currentProgress = currentProgress;
_totalCount = totalCount;
Invalidate();
Expand All @@ -233,20 +246,20 @@ namespace OpenRCT2::Ui::Windows
WF_10 | WF_TRANSPARENT | WF_CENTRE_SCREEN | WF_STICK_TO_FRONT);
}

window->SetCaptionTemplate(text);
window->SetCaption(text);
window->SetCloseCallback(onClose);
return window;
}

void ProgressWindowSet(uint32_t currentProgress, uint32_t totalCount)
void ProgressWindowSet(uint32_t currentProgress, uint32_t totalCount, StringId format)
{
auto window = WindowFindByClass(WindowClass::ProgressWindow);
if (window == nullptr)
{
return;
}
auto progressWindow = static_cast<ProgressWindow*>(window);
progressWindow->SetProgress(currentProgress, totalCount);
progressWindow->SetProgress(currentProgress, totalCount, format);
}

// Closes the window, deliberately *without* executing the callback.
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2-ui/windows/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ namespace OpenRCT2::Ui::Windows
void WindowNetworkStatusClose();

WindowBase* ProgressWindowOpen(const std::string& text, close_callback onClose = nullptr);
void ProgressWindowSet(uint32_t currentProgress, uint32_t totalCount);
void ProgressWindowSet(uint32_t currentProgress, uint32_t totalCount, StringId format = STR_NONE);
void ProgressWindowClose();

void WindowTextInputKey(WindowBase* w, uint32_t keycode);
Expand Down
3 changes: 2 additions & 1 deletion src/openrct2/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,12 @@ namespace OpenRCT2
ContextOpenIntent(&intent);
}

void SetProgress(uint32_t currentProgress, uint32_t totalCount) override
void SetProgress(uint32_t currentProgress, uint32_t totalCount, StringId format = STR_NONE) override
{
auto intent = Intent(INTENT_ACTION_PROGRESS_SET);
intent.PutExtra(INTENT_EXTRA_PROGRESS_OFFSET, currentProgress);
intent.PutExtra(INTENT_EXTRA_PROGRESS_TOTAL, totalCount);
intent.PutExtra(INTENT_EXTRA_STRING_ID, format);
ContextOpenIntent(&intent);
}

Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ namespace OpenRCT2
virtual void DisposeDrawingEngine() abstract;

virtual void OpenProgress(StringId captionStringId) abstract;
virtual void SetProgress(uint32_t currentProgress, uint32_t totalCount) abstract;
virtual void SetProgress(uint32_t currentProgress, uint32_t totalCount, StringId format = STR_NONE) abstract;
virtual void CloseProgress() abstract;

virtual bool LoadParkFromFile(
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/localisation/StringIds.h
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,8 @@ enum : StringId
STR_LOADING_TITLE_SEQUENCE = 6637,

STR_THEME_LOADING_WINDOW = 6641,
STR_STRING_M_OF_N = 6642,
STR_STRING_M_OF_N_KIB = 6643,

// Have to include resource strings (from scenarios and objects) for the time being now that language is partially working
/* MAX_STR_COUNT = 32768 */ // MAX_STR_COUNT - upper limit for number of strings, not the current count strings
Expand Down
5 changes: 4 additions & 1 deletion src/openrct2/network/NetworkBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2782,8 +2782,11 @@ void NetworkBase::Client_Handle_MAP([[maybe_unused]] NetworkConnection& connecti
chunk_buffer.resize(size);
}

const auto currentProgressKiB = (offset + chunksize) / 1024;
const auto totalSizeKiB = size / 1024;

OpenNetworkProgress(STR_MULTIPLAYER_DOWNLOADING_MAP);
GetContext().SetProgress((offset + chunksize) / 1024, size / 1024);
GetContext().SetProgress(currentProgressKiB, totalSizeKiB, STR_STRING_M_OF_N_KIB);

std::memcpy(&chunk_buffer[offset], const_cast<void*>(static_cast<const void*>(packet.Read(chunksize))), chunksize);
if (offset + chunksize == size)
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/windows/Intent.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,5 @@ enum
INTENT_EXTRA_SCENERY_GROUP_ENTRY_INDEX,
INTENT_EXTRA_PROGRESS_OFFSET,
INTENT_EXTRA_PROGRESS_TOTAL,
INTENT_EXTRA_STRING_ID,
};

0 comments on commit c9de564

Please sign in to comment.