Codechange: Use `GetVisibleRangeIterators()` in more places. (#12190)

This replaces more first/last index calculation, along with indexed array/vector access, with iterator access instead.
pull/661/head
Peter Nelson 4 months ago committed by GitHub
parent 529d813496
commit d4f0f0e2c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -231,11 +231,11 @@ public:
case WID_BBS_BRIDGE_LIST: { case WID_BBS_BRIDGE_LIST: {
Rect tr = r.WithHeight(this->resize.step_height).Shrink(WidgetDimensions::scaled.matrix); Rect tr = r.WithHeight(this->resize.step_height).Shrink(WidgetDimensions::scaled.matrix);
bool rtl = _current_text_dir == TD_RTL; bool rtl = _current_text_dir == TD_RTL;
for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < (int)this->bridges.size(); i++) { auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->bridges);
const BuildBridgeData &bridge_data = this->bridges.at(i); for (auto it = first; it != last; ++it) {
const BridgeSpec *b = bridge_data.spec; const BridgeSpec *b = it->spec;
DrawSpriteIgnorePadding(b->sprite, b->pal, tr.WithWidth(this->icon_width, rtl), SA_HOR_CENTER | SA_BOTTOM); DrawSpriteIgnorePadding(b->sprite, b->pal, tr.WithWidth(this->icon_width, rtl), SA_HOR_CENTER | SA_BOTTOM);
DrawStringMultiLine(tr.Indent(this->icon_width + WidgetDimensions::scaled.hsep_normal, rtl), GetBridgeSelectString(bridge_data)); DrawStringMultiLine(tr.Indent(this->icon_width + WidgetDimensions::scaled.hsep_normal, rtl), GetBridgeSelectString(*it));
tr = tr.Translate(0, this->resize.step_height); tr = tr.Translate(0, this->resize.step_height);
} }
break; break;

@ -441,8 +441,8 @@ public:
GfxFillRect(br, PC_BLACK); GfxFillRect(br, PC_BLACK);
Rect tr = r.Shrink(WidgetDimensions::scaled.inset).WithHeight(this->resize.step_height); Rect tr = r.Shrink(WidgetDimensions::scaled.inset).WithHeight(this->resize.step_height);
uint scroll_pos = this->vscroll->GetPosition(); auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->display_list);
for (auto it = this->display_list.begin() + scroll_pos; it != this->display_list.end() && tr.top < br.bottom; ++it) { for (auto it = first; it != last; ++it) {
const FiosItem *item = *it; const FiosItem *item = *it;
if (item == this->selected) { if (item == this->selected) {

@ -1023,13 +1023,11 @@ struct PaymentRatesGraphWindow : BaseGraphWindow {
bool rtl = _current_text_dir == TD_RTL; bool rtl = _current_text_dir == TD_RTL;
int pos = this->vscroll->GetPosition(); auto [first, last] = this->vscroll->GetVisibleRangeIterators(_sorted_standard_cargo_specs);
int max = pos + this->vscroll->GetCapacity();
Rect line = r.WithHeight(this->line_height); Rect line = r.WithHeight(this->line_height);
for (const CargoSpec *cs : _sorted_standard_cargo_specs) { for (auto it = first; it != last; ++it) {
if (pos-- > 0) continue; const CargoSpec *cs = *it;
if (--max < 0) break;
bool lowered = !HasBit(_legend_excluded_cargo, cs->Index()); bool lowered = !HasBit(_legend_excluded_cargo, cs->Index());

@ -626,9 +626,9 @@ public:
if (this->vli.index != ALL_GROUP && this->grouping == GB_NONE) { if (this->vli.index != ALL_GROUP && this->grouping == GB_NONE) {
/* Mark vehicles which are in sub-groups (only if we are not using shared order coalescing) */ /* Mark vehicles which are in sub-groups (only if we are not using shared order coalescing) */
Rect mr = r.WithHeight(this->resize.step_height); Rect mr = r.WithHeight(this->resize.step_height);
size_t max = std::min<size_t>(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->vehgroups.size()); auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->vehgroups);
for (size_t i = this->vscroll->GetPosition(); i < max; ++i) { for (auto it = first; it != last; ++it) {
const Vehicle *v = this->vehgroups[i].GetSingleVehicle(); const Vehicle *v = it->GetSingleVehicle();
if (v->group_id != this->vli.index) { if (v->group_id != this->vli.index) {
GfxFillRect(mr.Shrink(WidgetDimensions::scaled.bevel), GetColourGradient(COLOUR_GREY, SHADE_DARK), FILLRECT_CHECKER); GfxFillRect(mr.Shrink(WidgetDimensions::scaled.bevel), GetColourGradient(COLOUR_GREY, SHADE_DARK), FILLRECT_CHECKER);
} }

@ -528,8 +528,9 @@ public:
icon.top = r.top + (this->resize.step_height - this->legend.height + 1) / 2; icon.top = r.top + (this->resize.step_height - this->legend.height + 1) / 2;
icon.bottom = icon.top + this->legend.height - 1; icon.bottom = icon.top + this->legend.height - 1;
for (uint16_t i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) { auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->list);
IndustryType type = this->list[i]; for (auto it = first; it != last; ++it) {
IndustryType type = *it;
bool selected = this->selected_type == type; bool selected = this->selected_type == type;
const IndustrySpec *indsp = GetIndustrySpec(type); const IndustrySpec *indsp = GetIndustrySpec(type);
@ -1699,20 +1700,19 @@ public:
DrawString(ir, STR_INDUSTRY_DIRECTORY_NONE); DrawString(ir, STR_INDUSTRY_DIRECTORY_NONE);
break; break;
} }
int n = 0;
const CargoID acf_cid = this->accepted_cargo_filter_criteria; const CargoID acf_cid = this->accepted_cargo_filter_criteria;
for (uint i = this->vscroll->GetPosition(); i < this->industries.size(); i++) { auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->industries);
for (auto it = first; it != last; ++it) {
TextColour tc = TC_FROMSTRING; TextColour tc = TC_FROMSTRING;
if (acf_cid != CargoFilterCriteria::CF_ANY && acf_cid != CargoFilterCriteria::CF_NONE) { if (acf_cid != CargoFilterCriteria::CF_ANY && acf_cid != CargoFilterCriteria::CF_NONE) {
Industry *ind = const_cast<Industry *>(this->industries[i]); Industry *ind = const_cast<Industry *>(*it);
if (IndustryTemporarilyRefusesCargo(ind, acf_cid)) { if (IndustryTemporarilyRefusesCargo(ind, acf_cid)) {
tc = TC_GREY | TC_FORCED; tc = TC_GREY | TC_FORCED;
} }
} }
DrawString(ir, this->GetIndustryString(this->industries[i]), tc); DrawString(ir, this->GetIndustryString(*it), tc);
ir.top += this->resize.step_height; ir.top += this->resize.step_height;
if (++n == this->vscroll->GetCapacity()) break; // max number of industries in 1 window
} }
break; break;
} }

@ -652,11 +652,8 @@ public:
int text_y_offset = (this->resize.step_height - GetCharacterHeight(FS_NORMAL)) / 2; int text_y_offset = (this->resize.step_height - GetCharacterHeight(FS_NORMAL)) / 2;
Rect mr = r.WithHeight(this->resize.step_height); Rect mr = r.WithHeight(this->resize.step_height);
auto iter = this->content.begin() + this->vscroll->GetPosition(); auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->content);
size_t last = this->vscroll->GetPosition() + this->vscroll->GetCapacity(); for (auto iter = first; iter != last; iter++) {
auto end = (last < this->content.size()) ? this->content.begin() + last : this->content.end();
for (/**/; iter != end; iter++) {
const ContentInfo *ci = *iter; const ContentInfo *ci = *iter;
if (ci == this->selected) GfxFillRect(mr.Shrink(WidgetDimensions::scaled.bevel), PC_GREY); if (ci == this->selected) GfxFillRect(mr.Shrink(WidgetDimensions::scaled.bevel), PC_GREY);

@ -527,10 +527,9 @@ public:
case WID_NG_MATRIX: { case WID_NG_MATRIX: {
uint16_t y = r.top; uint16_t y = r.top;
const int max = std::min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), (int)this->servers.size()); auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->servers);
for (auto it = first; it != last; ++it) {
for (int i = this->vscroll->GetPosition(); i < max; ++i) { const NetworkGameList *ngl = *it;
const NetworkGameList *ngl = this->servers[i];
this->DrawServerLine(ngl, y, ngl == this->server); this->DrawServerLine(ngl, y, ngl == this->server);
y += this->resize.step_height; y += this->resize.step_height;
} }

@ -933,13 +933,13 @@ struct SpriteAlignerWindow : Window {
const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget); const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
int step_size = nwid->resize_y; int step_size = nwid->resize_y;
std::vector<SpriteID> &list = _newgrf_debug_sprite_picker.sprites; const std::vector<SpriteID> &list = _newgrf_debug_sprite_picker.sprites;
int max = std::min<int>(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), (uint)list.size());
Rect ir = r.Shrink(WidgetDimensions::scaled.matrix); Rect ir = r.Shrink(WidgetDimensions::scaled.matrix);
for (int i = this->vscroll->GetPosition(); i < max; i++) { auto [first, last] = this->vscroll->GetVisibleRangeIterators(list);
SetDParam(0, list[i]); for (auto it = first; it != last; ++it) {
DrawString(ir, STR_JUST_COMMA, list[i] == this->current_sprite ? TC_WHITE : TC_BLACK, SA_RIGHT | SA_FORCE); SetDParam(0, *it);
DrawString(ir, STR_JUST_COMMA, *it == this->current_sprite ? TC_WHITE : TC_BLACK, SA_RIGHT | SA_FORCE);
ir.top += step_size; ir.top += step_size;
} }
break; break;

@ -909,11 +909,10 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
Rect tr = r.Shrink(WidgetDimensions::scaled.framerect); Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
uint step_height = this->GetWidget<NWidgetBase>(WID_NS_AVAIL_LIST)->resize_y; uint step_height = this->GetWidget<NWidgetBase>(WID_NS_AVAIL_LIST)->resize_y;
int offset_y = (step_height - GetCharacterHeight(FS_NORMAL)) / 2; int offset_y = (step_height - GetCharacterHeight(FS_NORMAL)) / 2;
uint min_index = this->vscroll2->GetPosition();
uint max_index = std::min(min_index + this->vscroll2->GetCapacity(), (uint)this->avails.size());
for (uint i = min_index; i < max_index; i++) { auto [first, last] = this->vscroll2->GetVisibleRangeIterators(this->avails);
const GRFConfig *c = this->avails[i]; for (auto it = first; it != last; ++it) {
const GRFConfig *c = *it;
bool h = (c == this->avail_sel); bool h = (c == this->avail_sel);
const char *text = c->GetName(); const char *text = c->GetName();
@ -2127,13 +2126,13 @@ struct SavePresetWindow : public Window {
uint step_height = this->GetWidget<NWidgetBase>(WID_SVP_PRESET_LIST)->resize_y; uint step_height = this->GetWidget<NWidgetBase>(WID_SVP_PRESET_LIST)->resize_y;
int offset_y = (step_height - GetCharacterHeight(FS_NORMAL)) / 2; int offset_y = (step_height - GetCharacterHeight(FS_NORMAL)) / 2;
Rect tr = r.Shrink(WidgetDimensions::scaled.framerect); Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
uint min_index = this->vscroll->GetPosition();
uint max_index = std::min(min_index + this->vscroll->GetCapacity(), (uint)this->presets.size());
for (uint i = min_index; i < max_index; i++) { auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->presets);
if ((int)i == this->selected) GfxFillRect(br.left, tr.top, br.right, tr.top + step_height - 1, PC_DARK_BLUE); for (auto it = first; it != last; ++it) {
int row = static_cast<int>(std::distance(std::begin(this->presets), it));
if (row == this->selected) GfxFillRect(br.left, tr.top, br.right, tr.top + step_height - 1, PC_DARK_BLUE);
DrawString(tr.left, tr.right, tr.top + offset_y, this->presets[i], ((int)i == this->selected) ? TC_WHITE : TC_SILVER); DrawString(tr.left, tr.right, tr.top + offset_y, *it, (row == this->selected) ? TC_WHITE : TC_SILVER);
tr.top += step_height; tr.top += step_height;
} }
break; break;

@ -927,7 +927,7 @@ struct ScriptDebugWindow : public Window {
{ {
if (this->filter.script_debug_company == INVALID_COMPANY) return; if (this->filter.script_debug_company == INVALID_COMPANY) return;
ScriptLogTypes::LogData &log = this->GetLogData(); const ScriptLogTypes::LogData &log = this->GetLogData();
if (log.empty()) return; if (log.empty()) return;
Rect fr = r.Shrink(WidgetDimensions::scaled.framerect); Rect fr = r.Shrink(WidgetDimensions::scaled.framerect);
@ -943,8 +943,9 @@ struct ScriptDebugWindow : public Window {
fr.left -= this->hscroll->GetPosition(); fr.left -= this->hscroll->GetPosition();
for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && (size_t)i < log.size(); i++) { auto [first, last] = this->vscroll->GetVisibleRangeIterators(log);
const ScriptLogTypes::LogLine &line = log[i]; for (auto it = first; it != last; ++it) {
const ScriptLogTypes::LogLine &line = *it;
TextColour colour; TextColour colour;
switch (line.type) { switch (line.type) {
@ -957,7 +958,7 @@ struct ScriptDebugWindow : public Window {
} }
/* Check if the current line should be highlighted */ /* Check if the current line should be highlighted */
if (i == this->highlight_row) { if (std::distance(std::begin(log), it) == this->highlight_row) {
fr.bottom = fr.top + this->resize.step_height - 1; fr.bottom = fr.top + this->resize.step_height - 1;
GfxFillRect(fr, PC_BLACK); GfxFillRect(fr, PC_BLACK);
if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white. if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white.

@ -211,9 +211,9 @@ struct SignListWindow : Window, SignList {
tr = tr.Indent(this->text_offset, rtl); tr = tr.Indent(this->text_offset, rtl);
/* At least one sign available. */ /* At least one sign available. */
for (uint16_t i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->signs);
{ for (auto it = first; it != last; ++it) {
const Sign *si = this->signs[i]; const Sign *si = *it;
if (si->owner != OWNER_NONE) DrawCompanyIcon(si->owner, icon_left, tr.top + sprite_offset_y); if (si->owner != OWNER_NONE) DrawCompanyIcon(si->owner, icon_left, tr.top + sprite_offset_y);

@ -455,7 +455,6 @@ public:
case WID_STL_LIST: { case WID_STL_LIST: {
bool rtl = _current_text_dir == TD_RTL; bool rtl = _current_text_dir == TD_RTL;
size_t max = std::min<size_t>(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->stations.size());
Rect tr = r.Shrink(WidgetDimensions::scaled.framerect); Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
uint line_height = this->GetWidget<NWidgetBase>(widget)->resize_y; uint line_height = this->GetWidget<NWidgetBase>(widget)->resize_y;
/* Spacing between station name and first rating graph. */ /* Spacing between station name and first rating graph. */
@ -463,8 +462,9 @@ public:
/* Spacing between additional rating graphs. */ /* Spacing between additional rating graphs. */
int rating_spacing = WidgetDimensions::scaled.hsep_normal; int rating_spacing = WidgetDimensions::scaled.hsep_normal;
for (size_t i = this->vscroll->GetPosition(); i < max; ++i) { // do until max number of stations of owner auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->stations);
const Station *st = this->stations[i]; for (auto it = first; it != last; ++it) {
const Station *st = *it;
assert(st->xy != INVALID_TILE); assert(st->xy != INVALID_TILE);
/* Do not do the complex check HasStationInUse here, it may be even false /* Do not do the complex check HasStationInUse here, it may be even false
@ -2320,11 +2320,12 @@ struct SelectStationWindow : Window {
if (widget != WID_JS_PANEL) return; if (widget != WID_JS_PANEL) return;
Rect tr = r.Shrink(WidgetDimensions::scaled.framerect); Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
for (uint i = this->vscroll->GetPosition(); i < _stations_nearby_list.size(); ++i, tr.top += this->resize.step_height) { auto [first, last] = this->vscroll->GetVisibleRangeIterators(_stations_nearby_list);
if (_stations_nearby_list[i] == NEW_STATION) { for (auto it = first; it != last; ++it, tr.top += this->resize.step_height) {
if (*it == NEW_STATION) {
DrawString(tr, T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_JOIN_WAYPOINT_CREATE_SPLITTED_WAYPOINT : STR_JOIN_STATION_CREATE_SPLITTED_STATION); DrawString(tr, T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_JOIN_WAYPOINT_CREATE_SPLITTED_WAYPOINT : STR_JOIN_STATION_CREATE_SPLITTED_STATION);
} else { } else {
const T *st = T::Get(_stations_nearby_list[i]); const T *st = T::Get(*it);
SetDParam(0, st->index); SetDParam(0, st->index);
SetDParam(1, st->facilities); SetDParam(1, st->facilities);
DrawString(tr, T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_STATION_LIST_WAYPOINT : STR_STATION_LIST_STATION); DrawString(tr, T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_STATION_LIST_WAYPOINT : STR_STATION_LIST_STATION);

@ -840,7 +840,6 @@ public:
break; break;
case WID_TD_LIST: { case WID_TD_LIST: {
int n = 0;
Rect tr = r.Shrink(WidgetDimensions::scaled.framerect); Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
if (this->towns.empty()) { // No towns available. if (this->towns.empty()) { // No towns available.
DrawString(tr, STR_TOWN_DIRECTORY_NONE); DrawString(tr, STR_TOWN_DIRECTORY_NONE);
@ -853,8 +852,9 @@ public:
int icon_x = tr.WithWidth(icon_size.width, rtl).left; int icon_x = tr.WithWidth(icon_size.width, rtl).left;
tr = tr.Indent(icon_size.width + WidgetDimensions::scaled.hsep_normal, rtl); tr = tr.Indent(icon_size.width + WidgetDimensions::scaled.hsep_normal, rtl);
for (uint i = this->vscroll->GetPosition(); i < this->towns.size(); i++) { auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->towns);
const Town *t = this->towns[i]; for (auto it = first; it != last; ++it) {
const Town *t = *it;
assert(t->xy != INVALID_TILE); assert(t->xy != INVALID_TILE);
/* Draw rating icon. */ /* Draw rating icon. */
@ -872,7 +872,6 @@ public:
DrawString(tr.left, tr.right, tr.top + (this->resize.step_height - GetCharacterHeight(FS_NORMAL)) / 2, GetTownString(t)); DrawString(tr.left, tr.right, tr.top + (this->resize.step_height - GetCharacterHeight(FS_NORMAL)) / 2, GetTownString(t));
tr.top += this->resize.step_height; tr.top += this->resize.step_height;
if (++n == this->vscroll->GetCapacity()) break; // max number of towns in 1 window
} }
break; break;
} }

@ -1704,10 +1704,9 @@ void BaseVehicleListWindow::DrawVehicleListItems(VehicleID selected_vehicle, int
int vehicle_button_x = rtl ? ir.right - profit.width : ir.left; int vehicle_button_x = rtl ? ir.right - profit.width : ir.left;
uint max = static_cast<uint>(std::min<size_t>(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->vehgroups.size())); auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->vehgroups);
for (uint i = this->vscroll->GetPosition(); i < max; ++i) { for (auto it = first; it != last; ++it) {
const GUIVehicleGroup &vehgroup = *it;
const GUIVehicleGroup &vehgroup = this->vehgroups[i];
SetDParam(0, vehgroup.GetDisplayProfitThisYear()); SetDParam(0, vehgroup.GetDisplayProfitThisYear());
SetDParam(1, vehgroup.GetDisplayProfitLastYear()); SetDParam(1, vehgroup.GetDisplayProfitLastYear());

Loading…
Cancel
Save