Skip to content

Commit

Permalink
Merge pull request #1467 from ayrtondenner/issue-1380-format-value-field
Browse files Browse the repository at this point in the history
Issue #1380: display credit labels in currency format
  • Loading branch information
FilmBoy84 authored Jun 7, 2024
2 parents 777dc4b + b77e83f commit ae5d789
Showing 7 changed files with 91 additions and 17 deletions.
5 changes: 3 additions & 2 deletions forms/label.cpp
Original file line number Diff line number Diff line change
@@ -85,11 +85,12 @@ void Label::setText(const UString &Text)
{
if (text == Text)
return;

text = Text;

if (scroller)
{
scroller->setValue(0);
}

this->setDirty();
}

9 changes: 7 additions & 2 deletions game/state/gamestate.cpp
Original file line number Diff line number Diff line change
@@ -114,9 +114,14 @@ GameState::~GameState()
}

// Just a handy shortcut since it's shown on every single screen
UString GameState::getPlayerBalance() const
UString GameState::getPlayerBalance(const bool formatAsCurrency) const
{
return Strings::fromInteger(this->getPlayer()->balance);
auto playerBalance = Strings::fromInteger(this->getPlayer()->balance);

if (formatAsCurrency)
playerBalance = Strings::formatTextAsCurrency(playerBalance);

return playerBalance;
}

StateRef<Organisation> GameState::getOrganisation(const UString &orgID)
2 changes: 1 addition & 1 deletion game/state/gamestate.h
Original file line number Diff line number Diff line change
@@ -170,7 +170,7 @@ class GameState : public std::enable_shared_from_this<GameState>

Xorshift128Plus<uint32_t> rng;

UString getPlayerBalance() const;
UString getPlayerBalance(const bool formatAsCurrency = true) const;
StateRef<Organisation> getOrganisation(const UString &orgID);
const StateRef<Organisation> &getPlayer() const;
StateRef<Organisation> getPlayer();
7 changes: 4 additions & 3 deletions game/ui/base/buyandsellscreen.cpp
Original file line number Diff line number Diff line change
@@ -91,10 +91,11 @@ void BuyAndSellScreen::updateFormValues(bool queueHighlightUpdate)
TransactionScreen::updateFormValues(queueHighlightUpdate);

// Update money
int balance = state->getPlayer()->balance + moneyDelta;
form->findControlTyped<Label>("TEXT_FUNDS")->setText(Strings::fromInteger(balance));
const auto balance = state->getPlayer()->balance + moneyDelta;
form->findControlTyped<Label>("TEXT_FUNDS")->setText(Strings::fromInteger(balance, true));
form->findControlTyped<Label>("TEXT_FUNDS_DELTA")
->setText(format("%s%s", moneyDelta > 0 ? "+" : "", Strings::fromInteger(moneyDelta)));
->setText(
format("%s%s", moneyDelta > 0 ? "+" : "", Strings::fromInteger(moneyDelta, true)));
}

void BuyAndSellScreen::closeScreen()
7 changes: 4 additions & 3 deletions game/ui/base/recruitscreen.cpp
Original file line number Diff line number Diff line change
@@ -335,10 +335,11 @@ void RecruitScreen::updateFormValues()
}

// Update money
int balance = state->getPlayer()->balance + moneyDelta;
form->findControlTyped<Label>("TEXT_FUNDS")->setText(Strings::fromInteger(balance));
const auto balance = state->getPlayer()->balance + moneyDelta;
form->findControlTyped<Label>("TEXT_FUNDS")->setText(Strings::fromInteger(balance, true));
form->findControlTyped<Label>("TEXT_FUNDS_DELTA")
->setText(format("%s%s", moneyDelta > 0 ? "+" : "", Strings::fromInteger(moneyDelta)));
->setText(
format("%s%s", moneyDelta > 0 ? "+" : "", Strings::fromInteger(moneyDelta, true)));

updateBaseHighlight();
}
70 changes: 67 additions & 3 deletions library/strings.cpp
Original file line number Diff line number Diff line change
@@ -134,16 +134,80 @@ bool Strings::isFloat(const UStringView s)
return (endpos != u8str.c_str());
}

UString Strings::fromInteger(int i) { return format("%d", i); }
UString Strings::fromInteger(int i, const bool formatAsCurrency)
{
auto result = format("%d", i);

if (formatAsCurrency)
result = formatTextAsCurrency(result);

UString Strings::fromFloat(float f) { return format("%f", f); }
return result;
}

UString Strings::fromFloat(float f, const bool formatAsCurrency)
{
auto result = format("%f", f);

if (formatAsCurrency)
result = formatTextAsCurrency(result);

return result;
}

bool Strings::isWhiteSpace(char32_t c)
{
// FIXME: Only works on ASCII whitespace
return isspace(c) != 0;
}

UString Strings::fromU64(uint64_t i) { return format("%llu", i); }
UString Strings::fromU64(uint64_t i, const bool formatAsCurrency)
{
auto result = format("%llu", i);

if (formatAsCurrency)
result = formatTextAsCurrency(result);

return result;
}

UString Strings::formatTextAsCurrency(const UString &Text)
{
try
{
const auto textLenght = Text.length();

if (!Text.empty() && textLenght <= 3)
return Text;

auto isNumber = true;

for (const auto &ch : Text)
{
// Allowed chars in strings containing monetary values
if (!std::isdigit(ch) && ch != '.' && ch != ',' && ch != '-')
{
isNumber = false;
break;
}
}

if (!isNumber)
return Text;

auto formattedText = Text; // copy text for formatting

for (auto i = textLenght - 1; i > 0; i--)
{
if ((textLenght - i) % 3 == 0 && formattedText[i - 1] != '-')
formattedText.insert(i, ",");
}

return formattedText;
}
catch (const std::exception &err)
{
return Text;
}
}

}; // namespace OpenApoc
8 changes: 5 additions & 3 deletions library/strings.h
Original file line number Diff line number Diff line change
@@ -51,10 +51,12 @@ class Strings
[[nodiscard]] static int toInteger(const UStringView s);
[[nodiscard]] static uint8_t toU8(const UStringView s);
[[nodiscard]] static float toFloat(const UStringView s);
[[nodiscard]] static UString fromInteger(int i);
[[nodiscard]] static UString fromU64(uint64_t i);
[[nodiscard]] static UString fromFloat(float f);
[[nodiscard]] static UString fromInteger(int i, const bool formatAsCurrency = false);
[[nodiscard]] static UString fromU64(uint64_t i, const bool formatAsCurrency = false);
[[nodiscard]] static UString fromFloat(float f, const bool formatAsCurrency = false);
[[nodiscard]] static bool isWhiteSpace(char32_t c);

static UString formatTextAsCurrency(const UString &Text);
};

}; // namespace OpenApoc

0 comments on commit ae5d789

Please sign in to comment.