1
0
Fork 0

(svn r22428) -Codechange: Reduce code duplication in the smallmap legend.

release/1.2
michi_cc 2011-05-06 20:57:02 +00:00
parent ddd4981f52
commit 4b0a2fbe29
1 changed files with 43 additions and 55 deletions

View File

@ -1312,9 +1312,7 @@ public:
break; break;
case SM_WIDGET_LEGEND: // Legend case SM_WIDGET_LEGEND: // Legend
/* If industry type small map*/ if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER) {
if (this->map_type == SMT_INDUSTRY) {
/* If click on industries label, find right industry type and enable/disable it */
const NWidgetBase *wi = this->GetWidget<NWidgetBase>(SM_WIDGET_LEGEND); // Label panel const NWidgetBase *wi = this->GetWidget<NWidgetBase>(SM_WIDGET_LEGEND); // Label panel
uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL; uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
uint columns = this->GetNumberColumnsLegend(wi->current_x); uint columns = this->GetNumberColumnsLegend(wi->current_x);
@ -1326,65 +1324,55 @@ public:
if (rtl) x = wi->current_x - x; if (rtl) x = wi->current_x - x;
uint column = (x - WD_FRAMERECT_LEFT) / this->column_width; uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
/* Check if click is on industry label*/ /* If industry type small map*/
int industry_pos = (column * number_of_rows) + line; if (this->map_type == SMT_INDUSTRY) {
if (industry_pos < _smallmap_industry_count) { /* If click on industries label, find right industry type and enable/disable it. */
if (_ctrl_pressed) { int industry_pos = (column * number_of_rows) + line;
/* Disable all, except the clicked one */ if (industry_pos < _smallmap_industry_count) {
bool changes = false; if (_ctrl_pressed) {
for (int i = 0; i != _smallmap_industry_count; i++) { /* Disable all, except the clicked one. */
bool new_state = i == industry_pos; bool changes = false;
if (_legend_from_industries[i].show_on_map != new_state) {
changes = true;
_legend_from_industries[i].show_on_map = new_state;
}
}
if (!changes) {
/* Nothing changed? Then show all (again). */
for (int i = 0; i != _smallmap_industry_count; i++) { for (int i = 0; i != _smallmap_industry_count; i++) {
_legend_from_industries[i].show_on_map = true; bool new_state = i == industry_pos;
if (_legend_from_industries[i].show_on_map != new_state) {
changes = true;
_legend_from_industries[i].show_on_map = new_state;
}
} }
if (!changes) {
/* Nothing changed? Then show all (again). */
for (int i = 0; i != _smallmap_industry_count; i++) {
_legend_from_industries[i].show_on_map = true;
}
}
} else {
_legend_from_industries[industry_pos].show_on_map = !_legend_from_industries[industry_pos].show_on_map;
} }
} else {
_legend_from_industries[industry_pos].show_on_map = !_legend_from_industries[industry_pos].show_on_map;
} }
} } else if (this->map_type == SMT_OWNER) {
this->SetDirty(); /* If click on companies label, find right company and enable/disable it. */
} else if (this->map_type == SMT_OWNER) { int company_pos = (column * number_of_rows) + line;
/* If click on companies label, find right company and enable/disable it. */ if (company_pos < NUM_NO_COMPANY_ENTRIES) break;
const NWidgetBase *wi = this->GetWidget<NWidgetBase>(SM_WIDGET_LEGEND); if (company_pos < _smallmap_company_count) {
uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL; if (_ctrl_pressed) {
uint columns = this->GetNumberColumnsLegend(wi->current_x); /* Disable all, except the clicked one */
uint number_of_rows = max(CeilDiv(max(_smallmap_company_count, _smallmap_industry_count), columns), this->min_number_of_fixed_rows); bool changes = false;
if (line >= number_of_rows) break;
bool rtl = _current_text_dir == TD_RTL;
int x = pt.x - wi->pos_x;
if (rtl) x = wi->current_x - x;
uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
/* Check if click is on company label. */
int company_pos = (column * number_of_rows) + line;
if (company_pos < NUM_NO_COMPANY_ENTRIES) break;
if (company_pos < _smallmap_company_count) {
if (_ctrl_pressed) {
/* Disable all, except the clicked one */
bool changes = false;
for (int i = NUM_NO_COMPANY_ENTRIES; i != _smallmap_company_count; i++) {
bool new_state = i == company_pos;
if (_legend_land_owners[i].show_on_map != new_state) {
changes = true;
_legend_land_owners[i].show_on_map = new_state;
}
}
if (!changes) {
/* Nothing changed? Then show all (again). */
for (int i = NUM_NO_COMPANY_ENTRIES; i != _smallmap_company_count; i++) { for (int i = NUM_NO_COMPANY_ENTRIES; i != _smallmap_company_count; i++) {
_legend_land_owners[i].show_on_map = true; bool new_state = i == company_pos;
if (_legend_land_owners[i].show_on_map != new_state) {
changes = true;
_legend_land_owners[i].show_on_map = new_state;
}
} }
if (!changes) {
/* Nothing changed? Then show all (again). */
for (int i = NUM_NO_COMPANY_ENTRIES; i != _smallmap_company_count; i++) {
_legend_land_owners[i].show_on_map = true;
}
}
} else {
_legend_land_owners[company_pos].show_on_map = !_legend_land_owners[company_pos].show_on_map;
} }
} else {
_legend_land_owners[company_pos].show_on_map = !_legend_land_owners[company_pos].show_on_map;
} }
} }
this->SetDirty(); this->SetDirty();