Codechange: use int32_t instead of uint16_t for scroll bar position/size/capacity

This commit is contained in:
Rubidium 2024-02-25 19:42:28 +01:00 committed by rubidium42
parent c01bf06ee1
commit d09b5aaeba
11 changed files with 57 additions and 53 deletions

View File

@ -495,8 +495,8 @@ public:
break; break;
case WID_AP_AIRPORT_LIST: { case WID_AP_AIRPORT_LIST: {
int num_clicked = this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height); int32_t num_clicked = this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
if (num_clicked == INT_MAX) break; if (num_clicked == INT32_MAX) break;
const AirportSpec *as = AirportClass::Get(_selected_airport_class)->GetSpec(num_clicked); const AirportSpec *as = AirportClass::Get(_selected_airport_class)->GetSpec(num_clicked);
if (as->IsAvailable()) this->SelectOtherAirport(num_clicked); if (as->IsAvailable()) this->SelectOtherAirport(num_clicked);
break; break;

View File

@ -459,10 +459,10 @@ struct DepotWindow : Window {
} }
ym = (y - matrix_widget->pos_y) % this->resize.step_height; ym = (y - matrix_widget->pos_y) % this->resize.step_height;
int row = this->vscroll->GetScrolledRowFromWidget(y, this, WID_D_MATRIX); int32_t row = this->vscroll->GetScrolledRowFromWidget(y, this, WID_D_MATRIX);
uint pos = (row * this->num_columns) + xt; uint pos = (row * this->num_columns) + xt;
if (row == INT_MAX || this->vehicle_list.size() + this->wagon_list.size() <= pos) { if (row == INT32_MAX || this->vehicle_list.size() + this->wagon_list.size() <= pos) {
/* Clicking on 'line' / 'block' without a vehicle */ /* Clicking on 'line' / 'block' without a vehicle */
if (this->type == VEH_TRAIN) { if (this->type == VEH_TRAIN) {
/* End the dragging */ /* End the dragging */

View File

@ -601,7 +601,7 @@ struct FramerateWindow : Window {
void DrawElementTimesColumn(const Rect &r, StringID heading_str, const CachedDecimal *values) const void DrawElementTimesColumn(const Rect &r, StringID heading_str, const CachedDecimal *values) const
{ {
const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR); const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
uint16_t skip = sb->GetPosition(); int32_t skip = sb->GetPosition();
int drawable = this->num_displayed; int drawable = this->num_displayed;
int y = r.top; int y = r.top;
DrawString(r.left, r.right, y, heading_str, TC_FROMSTRING, SA_CENTER, true); DrawString(r.left, r.right, y, heading_str, TC_FROMSTRING, SA_CENTER, true);
@ -623,7 +623,7 @@ struct FramerateWindow : Window {
void DrawElementAllocationsColumn(const Rect &r) const void DrawElementAllocationsColumn(const Rect &r) const
{ {
const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR); const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
uint16_t skip = sb->GetPosition(); int32_t skip = sb->GetPosition();
int drawable = this->num_displayed; int drawable = this->num_displayed;
int y = r.top; int y = r.top;
DrawString(r.left, r.right, y, STR_FRAMERATE_MEMORYUSE, TC_FROMSTRING, SA_CENTER, true); DrawString(r.left, r.right, y, STR_FRAMERATE_MEMORYUSE, TC_FROMSTRING, SA_CENTER, true);
@ -657,7 +657,7 @@ struct FramerateWindow : Window {
case WID_FRW_TIMES_NAMES: { case WID_FRW_TIMES_NAMES: {
/* Render a column of titles for performance element names */ /* Render a column of titles for performance element names */
const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR); const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
uint16_t skip = sb->GetPosition(); int32_t skip = sb->GetPosition();
int drawable = this->num_displayed; int drawable = this->num_displayed;
int y = r.top + GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal; // first line contains headings in the value columns int y = r.top + GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal; // first line contains headings in the value columns
for (PerformanceElement e : DISPLAY_ORDER_PFE) { for (PerformanceElement e : DISPLAY_ORDER_PFE) {
@ -701,8 +701,8 @@ struct FramerateWindow : Window {
case WID_FRW_TIMES_AVERAGE: { case WID_FRW_TIMES_AVERAGE: {
/* Open time graph windows when clicking detail measurement lines */ /* Open time graph windows when clicking detail measurement lines */
const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR); const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
int line = sb->GetScrolledRowFromWidget(pt.y, this, widget, WidgetDimensions::scaled.vsep_normal + GetCharacterHeight(FS_NORMAL)); int32_t line = sb->GetScrolledRowFromWidget(pt.y, this, widget, WidgetDimensions::scaled.vsep_normal + GetCharacterHeight(FS_NORMAL));
if (line != INT_MAX) { if (line != INT32_MAX) {
line++; line++;
/* Find the visible line that was clicked */ /* Find the visible line that was clicked */
for (PerformanceElement e : DISPLAY_ORDER_PFE) { for (PerformanceElement e : DISPLAY_ORDER_PFE) {

View File

@ -589,8 +589,8 @@ struct NewGRFInspectWindow : Window {
if (nif->variables == nullptr) return; if (nif->variables == nullptr) return;
/* Get the line, make sure it's within the boundaries. */ /* Get the line, make sure it's within the boundaries. */
int line = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NGRFI_MAINPANEL, WidgetDimensions::scaled.frametext.top); int32_t line = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NGRFI_MAINPANEL, WidgetDimensions::scaled.frametext.top);
if (line == INT_MAX) return; if (line == INT32_MAX) return;
/* Find the variable related to the line */ /* Find the variable related to the line */
for (const NIVariable *niv = nif->variables; niv->name != nullptr; niv++, line--) { for (const NIVariable *niv = nif->variables; niv->name != nullptr; niv++, line--) {

View File

@ -146,11 +146,11 @@ static void ShowNewGRFInfo(const GRFConfig *c, const Rect &r, bool show_params)
struct NewGRFParametersWindow : public Window { struct NewGRFParametersWindow : public Window {
static GRFParameterInfo dummy_parameter_info; ///< Dummy info in case a newgrf didn't provide info about some parameter. static GRFParameterInfo dummy_parameter_info; ///< Dummy info in case a newgrf didn't provide info about some parameter.
GRFConfig *grf_config; ///< Set the parameters of this GRFConfig. GRFConfig *grf_config; ///< Set the parameters of this GRFConfig.
uint clicked_button; ///< The row in which a button was clicked or UINT_MAX. int32_t clicked_button; ///< The row in which a button was clicked or INT_MAX when none is selected.
bool clicked_increase; ///< True if the increase button was clicked, false for the decrease button. bool clicked_increase; ///< True if the increase button was clicked, false for the decrease button.
bool clicked_dropdown; ///< Whether the dropdown is open. bool clicked_dropdown; ///< Whether the dropdown is open.
bool closing_dropdown; ///< True, if the dropdown list is currently closing. bool closing_dropdown; ///< True, if the dropdown list is currently closing.
uint clicked_row; ///< The selected parameter int32_t clicked_row; ///< The selected parameter, or INT_MAX when none is selected.
int line_height; ///< Height of a row in the matrix widget. int line_height; ///< Height of a row in the matrix widget.
Scrollbar *vscroll; Scrollbar *vscroll;
bool action14present; ///< True if action14 information is present. bool action14present; ///< True if action14 information is present.
@ -158,10 +158,10 @@ struct NewGRFParametersWindow : public Window {
NewGRFParametersWindow(WindowDesc *desc, bool is_baseset, GRFConfig *c, bool editable) : Window(desc), NewGRFParametersWindow(WindowDesc *desc, bool is_baseset, GRFConfig *c, bool editable) : Window(desc),
grf_config(c), grf_config(c),
clicked_button(UINT_MAX), clicked_button(INT32_MAX),
clicked_dropdown(false), clicked_dropdown(false),
closing_dropdown(false), closing_dropdown(false),
clicked_row(UINT_MAX), clicked_row(INT32_MAX),
editable(editable) editable(editable)
{ {
this->action14present = (c->num_valid_params != c->param.size() || !c->param_info.empty()); this->action14present = (c->num_valid_params != c->param.size() || !c->param_info.empty());
@ -282,7 +282,7 @@ struct NewGRFParametersWindow : public Window {
int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2; int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2; int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2;
for (uint i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) { for (int32_t i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
GRFParameterInfo &par_info = this->GetParameterInfo(i); GRFParameterInfo &par_info = this->GetParameterInfo(i);
uint32_t current_value = par_info.GetValue(this->grf_config); uint32_t current_value = par_info.GetValue(this->grf_config);
bool selected = (i == this->clicked_row); bool selected = (i == this->clicked_row);
@ -354,7 +354,7 @@ struct NewGRFParametersWindow : public Window {
case WID_NP_BACKGROUND: { case WID_NP_BACKGROUND: {
if (!this->editable) break; if (!this->editable) break;
uint num = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NP_BACKGROUND); int32_t num = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NP_BACKGROUND);
if (num >= this->vscroll->GetCount()) break; if (num >= this->vscroll->GetCount()) break;
if (this->clicked_row != num) { if (this->clicked_row != num) {
@ -493,15 +493,15 @@ struct NewGRFParametersWindow : public Window {
} }
this->vscroll->SetCount(this->action14present ? this->grf_config->num_valid_params : this->grf_config->num_params); this->vscroll->SetCount(this->action14present ? this->grf_config->num_valid_params : this->grf_config->num_params);
if (this->clicked_row != UINT_MAX && this->clicked_row >= this->vscroll->GetCount()) { if (this->clicked_row != INT32_MAX && this->clicked_row >= this->vscroll->GetCount()) {
this->clicked_row = UINT_MAX; this->clicked_row = INT32_MAX;
this->CloseChildWindows(WC_QUERY_STRING); this->CloseChildWindows(WC_QUERY_STRING);
} }
} }
/** When reset, unclick the button after a small timeout. */ /** When reset, unclick the button after a small timeout. */
TimeoutTimer<TimerWindow> unclick_timeout = {std::chrono::milliseconds(150), [this]() { TimeoutTimer<TimerWindow> unclick_timeout = {std::chrono::milliseconds(150), [this]() {
this->clicked_button = UINT_MAX; this->clicked_button = INT32_MAX;
this->SetDirty(); this->SetDirty();
}}; }};
}; };

View File

@ -600,8 +600,8 @@ private:
*/ */
VehicleOrderID GetOrderFromPt(int y) VehicleOrderID GetOrderFromPt(int y)
{ {
int sel = this->vscroll->GetScrolledRowFromWidget(y, this, WID_O_ORDER_LIST, WidgetDimensions::scaled.framerect.top); int32_t sel = this->vscroll->GetScrolledRowFromWidget(y, this, WID_O_ORDER_LIST, WidgetDimensions::scaled.framerect.top);
if (sel == INT_MAX) return INVALID_VEH_ORDER_ID; if (sel == INT32_MAX) return INVALID_VEH_ORDER_ID;
/* One past the orders is the 'End of Orders' line. */ /* One past the orders is the 'End of Orders' line. */
assert(IsInsideBS(sel, 0, vehicle->GetNumOrders() + 1)); assert(IsInsideBS(sel, 0, vehicle->GetNumOrders() + 1));
return sel; return sel;

View File

@ -2560,8 +2560,8 @@ struct GameSettingsWindow : Window {
if (widget != WID_GS_OPTIONSPANEL) return; if (widget != WID_GS_OPTIONSPANEL) return;
uint btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GS_OPTIONSPANEL, WidgetDimensions::scaled.framerect.top); int32_t btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GS_OPTIONSPANEL, WidgetDimensions::scaled.framerect.top);
if (btn == INT_MAX || (int)btn < this->warn_lines) return; if (btn == INT32_MAX || btn < this->warn_lines) return;
btn -= this->warn_lines; btn -= this->warn_lines;
uint cur_row = 0; uint cur_row = 0;

View File

@ -495,12 +495,12 @@ protected:
* Get the total height of the content displayed in this window. * Get the total height of the content displayed in this window.
* @return the height in pixels * @return the height in pixels
*/ */
uint GetContentHeight() int32_t GetContentHeight()
{ {
this->EnsureStoryPageElementLayout(); this->EnsureStoryPageElementLayout();
/* The largest bottom coordinate of any element is the height of the content */ /* The largest bottom coordinate of any element is the height of the content */
uint max_y = std::accumulate(this->layout_cache.begin(), this->layout_cache.end(), 0, [](uint max_y, const LayoutCacheElement &ce) -> uint { return std::max<uint>(max_y, ce.bounds.bottom); }); int32_t max_y = std::accumulate(this->layout_cache.begin(), this->layout_cache.end(), 0, [](int32_t max_y, const LayoutCacheElement &ce) -> int32_t { return std::max<int32_t>(max_y, ce.bounds.bottom); });
return max_y; return max_y;
} }

View File

@ -277,8 +277,8 @@ struct TimetableWindow : Window {
int GetOrderFromTimetableWndPt(int y, [[maybe_unused]] const Vehicle *v) int GetOrderFromTimetableWndPt(int y, [[maybe_unused]] const Vehicle *v)
{ {
int sel = this->vscroll->GetScrolledRowFromWidget(y, this, WID_VT_TIMETABLE_PANEL, WidgetDimensions::scaled.framerect.top); int32_t sel = this->vscroll->GetScrolledRowFromWidget(y, this, WID_VT_TIMETABLE_PANEL, WidgetDimensions::scaled.framerect.top);
if (sel == INT_MAX) return INVALID_ORDER; if (sel == INT32_MAX) return INVALID_ORDER;
assert(IsInsideBS(sel, 0, v->GetNumOrders() * 2)); assert(IsInsideBS(sel, 0, v->GetNumOrders() * 2));
return sel; return sel;
} }

View File

@ -2262,13 +2262,13 @@ void NWidgetViewport::UpdateViewportCoordinates(Window *w)
* @param widget Widget number of the widget clicked in. * @param widget Widget number of the widget clicked in.
* @param padding Amount of empty space between the widget edge and the top of the first row. Default value is \c 0. * @param padding Amount of empty space between the widget edge and the top of the first row. Default value is \c 0.
* @param line_height Height of a single row. A negative value means using the vertical resize step of the widget. * @param line_height Height of a single row. A negative value means using the vertical resize step of the widget.
* @return Row number clicked at. If clicked at a wrong position, #INT_MAX is returned. * @return Row number clicked at. If clicked at a wrong position, #Scrollbar::npos is returned.
*/ */
int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, WidgetID widget, int padding, int line_height) const Scrollbar::size_type Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, WidgetID widget, int padding, int line_height) const
{ {
uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height); int pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
if (pos != INT_MAX) pos += this->GetPosition(); if (pos != INT_MAX) pos += this->GetPosition();
return (pos >= this->GetCount()) ? INT_MAX : pos; return (pos < 0 || pos >= this->GetCount()) ? Scrollbar::npos : pos;
} }
/** /**

View File

@ -678,12 +678,16 @@ public:
* Scrollbar data structure * Scrollbar data structure
*/ */
class Scrollbar { class Scrollbar {
public:
using size_type = int32_t;
static constexpr size_type max_size_type = std::numeric_limits<size_type>::max();
static constexpr size_type npos = max_size_type;
private: private:
const bool is_vertical; ///< Scrollbar has vertical orientation. const bool is_vertical; ///< Scrollbar has vertical orientation.
uint16_t count; ///< Number of elements in the list. size_type count; ///< Number of elements in the list.
uint16_t cap; ///< Number of visible elements of the scroll bar. size_type cap; ///< Number of visible elements of the scroll bar.
uint16_t pos; ///< Index of first visible item of the list. size_type pos; ///< Index of first visible item of the list.
uint16_t stepsize; ///< Distance to scroll, when pressing the buttons or using the wheel. size_type stepsize; ///< Distance to scroll, when pressing the buttons or using the wheel.
public: public:
/** Stepping sizes when scrolling */ /** Stepping sizes when scrolling */
@ -701,7 +705,7 @@ public:
* Gets the number of elements in the list * Gets the number of elements in the list
* @return the number of elements * @return the number of elements
*/ */
inline uint16_t GetCount() const inline size_type GetCount() const
{ {
return this->count; return this->count;
} }
@ -710,7 +714,7 @@ public:
* Gets the number of visible elements of the scrollbar * Gets the number of visible elements of the scrollbar
* @return the number of visible elements * @return the number of visible elements
*/ */
inline uint16_t GetCapacity() const inline size_type GetCapacity() const
{ {
return this->cap; return this->cap;
} }
@ -719,7 +723,7 @@ public:
* Gets the position of the first visible element in the list * Gets the position of the first visible element in the list
* @return the position of the element * @return the position of the element
*/ */
inline uint16_t GetPosition() const inline size_type GetPosition() const
{ {
return this->pos; return this->pos;
} }
@ -729,7 +733,7 @@ public:
* @param item to check * @param item to check
* @return true iff the item is visible * @return true iff the item is visible
*/ */
inline bool IsVisible(uint16_t item) const inline bool IsVisible(size_type item) const
{ {
return IsInsideBS(item, this->GetPosition(), this->GetCapacity()); return IsInsideBS(item, this->GetPosition(), this->GetCapacity());
} }
@ -751,7 +755,7 @@ public:
{ {
assert(stepsize > 0); assert(stepsize > 0);
this->stepsize = ClampTo<uint16_t>(stepsize); this->stepsize = ClampTo<size_type>(stepsize);
} }
/** /**
@ -761,9 +765,9 @@ public:
*/ */
void SetCount(size_t num) void SetCount(size_t num)
{ {
assert(num <= MAX_UVALUE(uint16_t)); assert(num < Scrollbar::max_size_type);
this->count = ClampTo<uint16_t>(num); this->count = ClampTo<size_type>(num);
/* Ensure position is within bounds */ /* Ensure position is within bounds */
this->SetPosition(this->pos); this->SetPosition(this->pos);
} }
@ -775,9 +779,9 @@ public:
*/ */
void SetCapacity(size_t capacity) void SetCapacity(size_t capacity)
{ {
assert(capacity <= MAX_UVALUE(uint16_t)); assert(capacity < Scrollbar::max_size_type);
this->cap = ClampTo<uint16_t>(capacity); this->cap = ClampTo<size_type>(capacity);
/* Ensure position is within bounds */ /* Ensure position is within bounds */
this->SetPosition(this->pos); this->SetPosition(this->pos);
} }
@ -789,9 +793,9 @@ public:
* @param position the position of the element * @param position the position of the element
* @return true iff the position has changed * @return true iff the position has changed
*/ */
bool SetPosition(int position) bool SetPosition(size_type position)
{ {
uint16_t old_pos = this->pos; size_type old_pos = this->pos;
this->pos = Clamp(position, 0, std::max(this->count - this->cap, 0)); this->pos = Clamp(position, 0, std::max(this->count - this->cap, 0));
return this->pos != old_pos; return this->pos != old_pos;
} }
@ -820,7 +824,7 @@ public:
* the window depending on where in the list it was. * the window depending on where in the list it was.
* @param position the position to scroll towards. * @param position the position to scroll towards.
*/ */
void ScrollTowards(int position) void ScrollTowards(size_type position)
{ {
if (position < this->GetPosition()) { if (position < this->GetPosition()) {
/* scroll up to the item */ /* scroll up to the item */
@ -831,7 +835,7 @@ public:
} }
} }
int GetScrolledRowFromWidget(int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const; size_type GetScrolledRowFromWidget(int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const;
/** /**
* Get a pair of iterators for the range of visible elements in a container. * Get a pair of iterators for the range of visible elements in a container.
@ -841,7 +845,7 @@ public:
template <typename Tcontainer> template <typename Tcontainer>
auto GetVisibleRangeIterators(Tcontainer &container) const auto GetVisibleRangeIterators(Tcontainer &container) const
{ {
assert(this->GetCount() == container.size()); // Scrollbar and container size must match. assert((size_t)this->GetCount() == container.size()); // Scrollbar and container size must match.
auto first = std::next(std::begin(container), this->GetPosition()); auto first = std::next(std::begin(container), this->GetPosition());
auto last = std::next(first, std::min<size_t>(this->GetCapacity(), this->GetCount() - this->GetPosition())); auto last = std::next(first, std::min<size_t>(this->GetCapacity(), this->GetCount() - this->GetPosition()));
return std::make_pair(first, last); return std::make_pair(first, last);
@ -860,9 +864,9 @@ public:
template <typename Tcontainer> template <typename Tcontainer>
typename Tcontainer::iterator GetScrolledItemFromWidget(Tcontainer &container, int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const typename Tcontainer::iterator GetScrolledItemFromWidget(Tcontainer &container, int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const
{ {
assert(this->GetCount() == container.size()); // Scrollbar and container size must match. assert((size_t)this->GetCount() == container.size()); // Scrollbar and container size must match.
int row = this->GetScrolledRowFromWidget(clickpos, w, widget, padding, line_height); size_type row = this->GetScrolledRowFromWidget(clickpos, w, widget, padding, line_height);
if (row == INT_MAX) return std::end(container); if (row == Scrollbar::npos) return std::end(container);
typename Tcontainer::iterator it = std::begin(container); typename Tcontainer::iterator it = std::begin(container);
std::advance(it, row); std::advance(it, row);