summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Nelson <peter1138@openttd.org>2019-01-31 21:43:18 +0000
committerCharles Pigott <charlespigott@googlemail.com>2019-02-01 13:25:42 +0000
commit9e9d485713a5441d7170be7c0fea9fb981342acd (patch)
tree831ac7c7653e3610333b2c34ccdae7df4e37c22d
parent12b6fe47c7eeb020f983d2dff813fa0b77ea2a25 (diff)
downloadopenttd-9e9d485713a5441d7170be7c0fea9fb981342acd.tar.xz
Fix 23960d0f2c: Scrollbar was broken for non-group liveries.
Simplify how list position is determined by using existing functions. Also rename livery_height -> rows and SetLiveryHeight() -> SetRows(), as height implies pixels.
-rw-r--r--src/company_gui.cpp40
1 files changed, 19 insertions, 21 deletions
diff --git a/src/company_gui.cpp b/src/company_gui.cpp
index ae7c8d6c6..5317d7089 100644
--- a/src/company_gui.cpp
+++ b/src/company_gui.cpp
@@ -561,7 +561,7 @@ private:
uint32 sel;
LiveryClass livery_class;
Dimension square;
- uint livery_height;
+ uint rows;
uint line_height;
GUIGroupList groups;
SmallVector<int, 32> indents;
@@ -678,20 +678,20 @@ private:
this->groups.RebuildDone();
}
- void SetLiveryHeight()
+ void SetRows()
{
if (this->livery_class < LC_GROUP_RAIL) {
- this->livery_height = 0;
+ this->rows = 0;
for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
if (_livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
- this->livery_height++;
+ this->rows++;
}
}
} else {
- this->livery_height = this->groups.Length();
+ this->rows = this->groups.Length();
}
- this->vscroll->SetCount(this->livery_height);
+ this->vscroll->SetCount(this->rows);
}
public:
@@ -708,7 +708,7 @@ public:
this->sel = 1;
this->LowerWidget(WID_SCL_CLASS_GENERAL);
this->BuildGroupList(company);
- this->SetLiveryHeight();
+ this->SetRows();
} else {
this->SetSelectedGroup(group);
}
@@ -734,10 +734,10 @@ public:
this->groups.ForceRebuild();
this->BuildGroupList((CompanyID)this->window_number);
- this->SetLiveryHeight();
+ this->SetRows();
/* Position scrollbar to selected group */
- for (uint i = 0; i < this->livery_height; i++) {
+ for (uint i = 0; i < this->rows; i++) {
if (this->groups[i]->index == sel) {
this->vscroll->SetPosition(Clamp(i - this->vscroll->GetCapacity() / 2, 0, max(this->vscroll->GetCount() - this->vscroll->GetCapacity(), 0)));
break;
@@ -894,9 +894,11 @@ public:
};
if (livery_class < LC_GROUP_RAIL) {
+ int pos = this->vscroll->GetPosition();
const Company *c = Company::Get((CompanyID)this->window_number);
for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
if (_livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
+ if (pos-- > 0) continue;
draw_livery(STR_LIVERY_DEFAULT + scheme, c->livery[scheme], HasBit(this->sel, scheme), scheme == LS_DEFAULT, 0);
}
}
@@ -946,7 +948,7 @@ public:
}
}
- this->SetLiveryHeight();
+ this->SetRows();
this->SetDirty();
break;
@@ -959,17 +961,16 @@ public:
break;
case WID_SCL_MATRIX: {
- const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SCL_MATRIX);
- if (this->livery_class < LC_GROUP_RAIL) {
- LiveryScheme j = (LiveryScheme)((pt.y - wid->pos_y) / this->line_height);
+ uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SCL_MATRIX, 0, this->line_height);
+ if (row >= this->rows) return;
- if (j >= this->livery_height) return;
+ if (this->livery_class < LC_GROUP_RAIL) {
+ LiveryScheme j = (LiveryScheme)row;
- for (LiveryScheme scheme = LS_BEGIN; scheme <= j; scheme++) {
+ for (LiveryScheme scheme = LS_BEGIN; scheme <= j && scheme < LS_END; scheme++) {
if (_livery_class[scheme] != this->livery_class || !HasBit(_loaded_newgrf_features.used_liveries, scheme)) j++;
- if (scheme >= LS_END) return;
}
- if (j >= LS_END) return;
+ assert(j < LS_END);
if (_ctrl_pressed) {
ToggleBit(this->sel, j);
@@ -977,10 +978,7 @@ public:
this->sel = 1 << j;
}
} else {
- uint id_g = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SCL_MATRIX, 0, this->line_height);
- if (id_g >= this->groups.Length()) return;
-
- this->sel = this->groups[id_g]->index;
+ this->sel = this->groups[row]->index;
}
this->SetDirty();
break;