(svn r19535) -Feature: Sort cargos at the cargo payment rates graph.

pull/155/head
terkhen 14 years ago
parent f7327dc7d7
commit 4bb33b9ddd

@ -810,9 +810,11 @@ enum CargoPaymentRatesWidgets {
}; };
struct PaymentRatesGraphWindow : BaseGraphWindow { struct PaymentRatesGraphWindow : BaseGraphWindow {
bool first_init; ///< This value is true until the first initialization of the window has finished.
PaymentRatesGraphWindow(const WindowDesc *desc, WindowNumber window_number) : PaymentRatesGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
BaseGraphWindow(CPW_GRAPH, false, STR_JUST_CURRCOMPACT) BaseGraphWindow(CPW_GRAPH, false, STR_JUST_CURRCOMPACT)
{ {
this->first_init = true;
this->num_on_x_axis = 20; this->num_on_x_axis = 20;
this->num_vert_lines = 20; this->num_vert_lines = 20;
this->month = 0xFF; this->month = 0xFF;
@ -824,11 +826,34 @@ struct PaymentRatesGraphWindow : BaseGraphWindow {
this->InitNested(desc, window_number); this->InitNested(desc, window_number);
int i = 0; this->UpdateLoweredWidgets();
const CargoSpec *cs; }
FOR_ALL_CARGOSPECS(cs) {
this->SetWidgetLoweredState(CPW_CARGO_FIRST + cs->Index(), !HasBit(_legend_excluded_cargo, i)); virtual void OnInit()
i++; {
/* UpdateLoweredWidgets needs to be called after a language or NewGRF change, but it can't be called before
* InitNested is done. On the first init these functions are called in the correct order by the constructor. */
if (!this->first_init) {
/* Initialise the dataset */
this->OnHundredthTick();
this->UpdateLoweredWidgets();
}
this->first_init = false;
}
void UpdateExcludedData()
{
this->excluded_data = 0;
for (int i = 0; i < _sorted_cargo_specs_size; i++) {
if (HasBit(_legend_excluded_cargo, _sorted_cargo_specs[i]->Index())) SetBit(this->excluded_data, i);
}
}
void UpdateLoweredWidgets()
{
for (int i = 0; i < _sorted_cargo_specs_size; i++) {
this->SetWidgetLoweredState(CPW_CARGO_FIRST + i, !HasBit(this->excluded_data, i));
} }
} }
@ -839,7 +864,7 @@ struct PaymentRatesGraphWindow : BaseGraphWindow {
return; return;
} }
const CargoSpec *cs = CargoSpec::Get(widget - CPW_CARGO_FIRST); const CargoSpec *cs = _sorted_cargo_specs[widget - CPW_CARGO_FIRST];
SetDParam(0, cs->name); SetDParam(0, cs->name);
Dimension d = GetStringBoundingBox(STR_GRAPH_CARGO_PAYMENT_CARGO); Dimension d = GetStringBoundingBox(STR_GRAPH_CARGO_PAYMENT_CARGO);
d.width += 14; // colour field d.width += 14; // colour field
@ -855,7 +880,7 @@ struct PaymentRatesGraphWindow : BaseGraphWindow {
return; return;
} }
const CargoSpec *cs = CargoSpec::Get(widget - CPW_CARGO_FIRST); const CargoSpec *cs = _sorted_cargo_specs[widget - CPW_CARGO_FIRST];
bool rtl = _dynlang.text_dir == TD_RTL; bool rtl = _dynlang.text_dir == TD_RTL;
/* Since the buttons have no text, no images, /* Since the buttons have no text, no images,
@ -877,16 +902,10 @@ struct PaymentRatesGraphWindow : BaseGraphWindow {
virtual void OnClick(Point pt, int widget, int click_count) virtual void OnClick(Point pt, int widget, int click_count)
{ {
if (widget >= CPW_CARGO_FIRST) { if (widget >= CPW_CARGO_FIRST) {
int i = 0; int i = widget - CPW_CARGO_FIRST;
const CargoSpec *cs; ToggleBit(_legend_excluded_cargo, _sorted_cargo_specs[i]->Index());
FOR_ALL_CARGOSPECS(cs) {
if (cs->Index() + CPW_CARGO_FIRST == widget) break;
i++;
}
ToggleBit(_legend_excluded_cargo, i);
this->ToggleWidgetLoweredState(widget); this->ToggleWidgetLoweredState(widget);
this->excluded_data = _legend_excluded_cargo; this->UpdateExcludedData();
this->SetDirty(); this->SetDirty();
} }
} }
@ -898,17 +917,14 @@ struct PaymentRatesGraphWindow : BaseGraphWindow {
virtual void OnHundredthTick() virtual void OnHundredthTick()
{ {
this->excluded_data = _legend_excluded_cargo; this->UpdateExcludedData();
int i = 0; int i;
const CargoSpec *cs; for (i = 0; i < _sorted_cargo_specs_size; i++) {
FOR_ALL_CARGOSPECS(cs) { this->colours[i] = _sorted_cargo_specs[i]->legend_colour;
this->colours[i] = cs->legend_colour;
for (uint j = 0; j != 20; j++) { for (uint j = 0; j != 20; j++) {
this->cost[i][j] = GetTransportedGoodsIncome(10, 20, j * 4 + 4, cs->Index()); this->cost[i][j] = GetTransportedGoodsIncome(10, 20, j * 4 + 4, _sorted_cargo_specs[i]->Index());
} }
i++;
} }
this->num_dataset = i; this->num_dataset = i;
} }
@ -919,15 +935,14 @@ static NWidgetBase *MakeCargoButtons(int *biggest_index)
{ {
NWidgetVertical *ver = new NWidgetVertical; NWidgetVertical *ver = new NWidgetVertical;
const CargoSpec *cs; for (int i = 0; i < _sorted_cargo_specs_size; i++) {
FOR_ALL_CARGOSPECS(cs) { NWidgetBackground *leaf = new NWidgetBackground(WWT_PANEL, COLOUR_ORANGE, CPW_CARGO_FIRST + i, NULL);
*biggest_index = CPW_CARGO_FIRST + cs->Index();
NWidgetBackground *leaf = new NWidgetBackground(WWT_PANEL, COLOUR_ORANGE, *biggest_index, NULL);
leaf->tool_tip = STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO; leaf->tool_tip = STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO;
leaf->SetFill(1, 0); leaf->SetFill(1, 0);
leaf->SetLowered(true); leaf->SetLowered(true);
ver->Add(leaf); ver->Add(leaf);
} }
*biggest_index = CPW_CARGO_FIRST + _sorted_cargo_specs_size - 1;
return ver; return ver;
} }

Loading…
Cancel
Save