Skip to content

Commit

Permalink
Change: Show company finances column if it has any values in it. (#13…
Browse files Browse the repository at this point in the history
…112)

This solves finances not being show if the company inauguration date is in the future.

Current period is now always shown in the same position instead of moving for the first 2 years.
  • Loading branch information
PeterN authored Nov 24, 2024
1 parent 0c04966 commit 0446123
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/cheat_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ static int32_t ClickChangeDateCheat(int32_t new_value, int32_t)
InvalidateWindowClassesData(WC_BUS_STATION, 0);
InvalidateWindowClassesData(WC_TRUCK_STATION, 0);
InvalidateWindowClassesData(WC_BUILD_OBJECT, 0);
InvalidateWindowClassesData(WC_FINANCES, 0);
ResetSignalVariant();
return TimerGameCalendar::year.base();
}
Expand Down
2 changes: 1 addition & 1 deletion src/company_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ static IntervalTimer<TimerGameEconomy> _economy_companies_yearly({TimerGameEcono
/* Move expenses to previous years. */
std::rotate(std::rbegin(c->yearly_expenses), std::rbegin(c->yearly_expenses) + 1, std::rend(c->yearly_expenses));
c->yearly_expenses[0] = {};
SetWindowDirty(WC_FINANCES, c->index);
InvalidateWindowData(WC_FINANCES, c->index);
}

if (_settings_client.gui.show_finances && _local_company != COMPANY_SPECTATOR) {
Expand Down
32 changes: 27 additions & 5 deletions src/company_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,11 @@ static constexpr NWidgetPart _nested_company_finances_widgets[] = {

/** Window class displaying the company finances. */
struct CompanyFinancesWindow : Window {
static constexpr int NUM_PERIODS = WID_CF_EXPS_PRICE3 - WID_CF_EXPS_PRICE1 + 1;

static Money max_money; ///< The maximum amount of money a company has had this 'run'
bool small; ///< Window is toggled to 'small'.
uint8_t first_visible = NUM_PERIODS - 1; ///< First visible expenses column. The last column (current) is always visible.

CompanyFinancesWindow(WindowDesc &desc, CompanyID company) : Window(desc)
{
Expand All @@ -344,6 +347,7 @@ struct CompanyFinancesWindow : Window {
this->FinishInitNested(company);

this->owner = (Owner)this->window_number;
this->InvalidateData();
}

void SetStringParameters(WidgetID widget) const override
Expand Down Expand Up @@ -426,12 +430,12 @@ struct CompanyFinancesWindow : Window {
case WID_CF_EXPS_PRICE1:
case WID_CF_EXPS_PRICE2:
case WID_CF_EXPS_PRICE3: {
int period = widget - WID_CF_EXPS_PRICE1;
if (period < this->first_visible) break;

const Company *c = Company::Get((CompanyID)this->window_number);
auto age = std::min(TimerGameEconomy::year - c->inaugurated_year, TimerGameEconomy::Year(2));
int wid_offset = widget - WID_CF_EXPS_PRICE1;
if (wid_offset <= age) {
DrawYearColumn(r, TimerGameEconomy::year - (age - wid_offset), c->yearly_expenses[(age - wid_offset).base()]);
}
const auto &expenses = c->yearly_expenses[NUM_PERIODS - period - 1];
DrawYearColumn(r, TimerGameEconomy::year - (NUM_PERIODS - period - 1), expenses);
break;
}

Expand Down Expand Up @@ -514,6 +518,24 @@ struct CompanyFinancesWindow : Window {
}
}

void RefreshVisibleColumns()
{
for (uint period = 0; period < this->first_visible; ++period) {
const Company *c = Company::Get((CompanyID)this->window_number);
const Expenses &expenses = c->yearly_expenses[NUM_PERIODS - period - 1];
/* Show expenses column if it has any non-zero value in it. */
if (std::ranges::any_of(expenses, [](const Money &value) { return value != 0; })) {
this->first_visible = period;
break;
}
}
}

void OnInvalidateData(int, bool) override
{
this->RefreshVisibleColumns();
}

/**
* Check on a regular interval if the maximum amount of money has changed.
* If it has, rescale the window to fit the new amount.
Expand Down

0 comments on commit 0446123

Please sign in to comment.