Skip to content

Commit 589e0e5

Browse files
committed
Refactor money display string code
1 parent 12216df commit 589e0e5

File tree

4 files changed

+30
-35
lines changed

4 files changed

+30
-35
lines changed

src/Ext/Building/Body.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,8 @@ void BuildingExt::ExtData::DisplayGrinderRefund()
1212
{
1313
if (this->AccumulatedGrindingRefund && Unsorted::CurrentFrame % 15 == 0)
1414
{
15-
int refundAmount = this->AccumulatedGrindingRefund;
16-
bool isPositive = refundAmount > 0;
17-
auto color = isPositive ? ColorStruct { 0, 255, 0 } : ColorStruct { 255, 0, 0 };
18-
auto coords = this->OwnerObject()->GetRenderCoords();
19-
int width = 0, height = 0;
20-
wchar_t moneyStr[0x20];
21-
swprintf_s(moneyStr, L"%ls%ls%d", isPositive ? L"+" : L"-", Phobos::UI::CostLabel, std::abs(refundAmount));
22-
BitFont::Instance->GetTextDimension(moneyStr, &width, &height, 120);
23-
Point2D pixelOffset = Point2D::Empty;
24-
pixelOffset += this->TypeExtData->Grinding_DisplayRefund_Offset;
25-
pixelOffset.X -= width / 2;
26-
27-
FlyingStrings::Add(moneyStr, coords, color, pixelOffset);
15+
FlyingStrings::AddMoneyString(this->AccumulatedGrindingRefund, this->OwnerObject()->Owner,
16+
this->TypeExtData->Grinding_DisplayRefund_Houses, this->OwnerObject()->GetRenderCoords(), this->TypeExtData->Grinding_DisplayRefund_Offset);
2817

2918
this->AccumulatedGrindingRefund = 0;
3019
}
@@ -256,11 +245,8 @@ bool BuildingExt::DoGrindingExtras(BuildingClass* pBuilding, TechnoClass* pTechn
256245
{
257246
const auto pTypeExt = pExt->TypeExtData;
258247

259-
if (pTypeExt->Grinding_DisplayRefund && (pTypeExt->Grinding_DisplayRefund_Houses == AffectedHouse::All ||
260-
EnumFunctions::CanTargetHouse(pTypeExt->Grinding_DisplayRefund_Houses, pBuilding->Owner, HouseClass::CurrentPlayer)))
261-
{
248+
if (pTypeExt->Grinding_DisplayRefund)
262249
pExt->AccumulatedGrindingRefund += pTechno->GetRefund();
263-
}
264250

265251
if (pTypeExt->Grinding_Weapon.isset()
266252
&& Unsorted::CurrentFrame >= pExt->GrindingWeapon_LastFiredFrame + pTypeExt->Grinding_Weapon.Get()->ROF)

src/Ext/WarheadType/Detonate.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,10 @@ void WarheadTypeExt::ExtData::Detonate(TechnoClass* pOwner, HouseClass* pHouse,
5454
{
5555
pHouse->TransactMoney(this->TransactMoney);
5656

57-
if (this->TransactMoney_Display &&
58-
(this->TransactMoney_Display_Houses == AffectedHouse::All ||
59-
EnumFunctions::CanTargetHouse(this->TransactMoney_Display_Houses, pHouse, HouseClass::CurrentPlayer)))
57+
if (this->TransactMoney_Display)
6058
{
61-
bool isPositive = this->TransactMoney > 0;
62-
auto color = isPositive ? ColorStruct { 0, 255, 0 } : ColorStruct { 255, 0, 0 };
63-
wchar_t moneyStr[0x20];
64-
swprintf_s(moneyStr, L"%ls%ls%d", isPositive ? L"+" : L"-", Phobos::UI::CostLabel, std::abs(this->TransactMoney));
65-
auto displayCoord = this->TransactMoney_Display_AtFirer ? (pOwner ? pOwner->Location : coords) : coords;
66-
67-
int width = 0, height = 0;
68-
BitFont::Instance->GetTextDimension(moneyStr, &width, &height, 120);
69-
Point2D pixelOffset = Point2D::Empty;
70-
pixelOffset += this->TransactMoney_Display_Offset;
71-
pixelOffset.X -= (width / 2);
72-
73-
FlyingStrings::Add(moneyStr, displayCoord, color, pixelOffset);
59+
auto displayCoords = this->TransactMoney_Display_AtFirer ? (pOwner ? pOwner->Location : coords) : coords;
60+
FlyingStrings::AddMoneyString(this->TransactMoney, pHouse, this->TransactMoney_Display_Houses, displayCoords, this->TransactMoney_Display_Offset);
7461
}
7562
}
7663

src/Misc/FlyingStrings.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <Drawing.h>
88
#include <ScenarioClass.h>
99
#include <BitFont.h>
10+
#include <Utilities/EnumFunctions.h>
1011

1112
std::vector<FlyingStrings::Item> FlyingStrings::Data;
1213

@@ -29,6 +30,24 @@ void FlyingStrings::Add(const wchar_t* text, CoordStruct coords, ColorStruct col
2930
Data.push_back(item);
3031
}
3132

33+
void FlyingStrings::AddMoneyString(int amount, HouseClass* owner, AffectedHouse displayToHouses, CoordStruct coords, Point2D pixelOffset)
34+
{
35+
if (displayToHouses == AffectedHouse::All ||
36+
owner && EnumFunctions::CanTargetHouse(displayToHouses, owner, HouseClass::CurrentPlayer))
37+
{
38+
bool isPositive = amount > 0;
39+
ColorStruct color = isPositive ? ColorStruct { 0, 255, 0 } : ColorStruct { 255, 0, 0 };
40+
wchar_t moneyStr[0x20];
41+
swprintf_s(moneyStr, L"%ls%ls%d", isPositive ? L"+" : L"-", Phobos::UI::CostLabel, std::abs(amount));
42+
43+
int width = 0, height = 0;
44+
BitFont::Instance->GetTextDimension(moneyStr, &width, &height, 120);
45+
pixelOffset.X -= (width / 2);
46+
47+
FlyingStrings::Add(moneyStr, coords, color, pixelOffset);
48+
}
49+
}
50+
3251
void FlyingStrings::UpdateAll()
3352
{
3453
if (Data.empty())

src/Misc/FlyingStrings.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ By AlexB and Joshy
77
#pragma once
88
#include <vector>
99
#include <ColorScheme.h>
10+
#include <HouseClass.h>
11+
#include <Utilities/Enum.h>
1012

1113
class FlyingStrings
1214
{
@@ -25,9 +27,10 @@ class FlyingStrings
2527
static const int Duration = 75;
2628
static std::vector<Item> Data;
2729

28-
static bool DrawAllowed(CoordStruct &nCoords);
30+
static bool DrawAllowed(CoordStruct& nCoords);
2931

3032
public:
31-
static void Add(const wchar_t *text, CoordStruct coords, ColorStruct color, Point2D pixelOffset = Point2D::Empty);
33+
static void Add(const wchar_t* text, CoordStruct coords, ColorStruct color, Point2D pixelOffset = Point2D::Empty);
34+
static void AddMoneyString(int amount, HouseClass* owner, AffectedHouse displayToHouses, CoordStruct coords, Point2D pixelOffset = Point2D::Empty);
3235
static void UpdateAll();
3336
};

0 commit comments

Comments
 (0)