From 385b25df63d5214f857e212f74372e7a7617beb7 Mon Sep 17 00:00:00 2001 From: PeterN Date: Sat, 13 May 2023 23:18:36 +0100 Subject: [PATCH] Fix #10819, Fix #10811: GetVehicleFromDepotWndPt used widget- instead of window-relative positions. (#10820) Pass window-relative positions instead, and adjust for relative positions where needed within the function itself. This simplifies calling code. --- src/depot_gui.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index 7a20022b5b..49bec452ea 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -445,6 +445,8 @@ struct DepotWindow : Window { DepotGUIAction GetVehicleFromDepotWndPt(int x, int y, const Vehicle **veh, GetDepotVehiclePtData *d) const { const NWidgetCore *matrix_widget = this->GetWidget(WID_D_MATRIX); + /* Make X relative to widget. Y is left alone for GetScrolledRowFromWidget(). */ + x -= matrix_widget->pos_x; /* In case of RTL the widgets are swapped as a whole */ if (_current_text_dir == TD_RTL) x = matrix_widget->current_x - x; @@ -456,7 +458,7 @@ struct DepotWindow : Window { xm = x % this->resize.step_width; if (xt >= this->num_columns) return MODE_ERROR; } - ym = 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); if (row == INT_MAX) return MODE_ERROR; @@ -762,11 +764,9 @@ struct DepotWindow : Window { void OnClick(Point pt, int widget, int click_count) override { switch (widget) { - case WID_D_MATRIX: { // List - NWidgetBase *nwi = this->GetWidget(WID_D_MATRIX); - this->DepotClick(pt.x - nwi->pos_x, pt.y - nwi->pos_y); + case WID_D_MATRIX: // List + this->DepotClick(pt.x, pt.y); break; - } case WID_D_BUILD: // Build vehicle ResetObjectToPlace(); @@ -849,8 +849,7 @@ struct DepotWindow : Window { GetDepotVehiclePtData gdvp = { nullptr, nullptr }; const Vehicle *v = nullptr; - NWidgetBase *nwi = this->GetWidget(WID_D_MATRIX); - DepotGUIAction mode = this->GetVehicleFromDepotWndPt(pt.x - nwi->pos_x, pt.y - nwi->pos_y, &v, &gdvp); + DepotGUIAction mode = this->GetVehicleFromDepotWndPt(pt.x, pt.y, &v, &gdvp); if (this->type == VEH_TRAIN) v = gdvp.wagon; @@ -1005,11 +1004,10 @@ struct DepotWindow : Window { return; } - NWidgetBase *matrix = this->GetWidget(widget); const Vehicle *v = nullptr; GetDepotVehiclePtData gdvp = {nullptr, nullptr}; - if (this->GetVehicleFromDepotWndPt(pt.x - matrix->pos_x, pt.y - matrix->pos_y, &v, &gdvp) != MODE_DRAG_VEHICLE) return; + if (this->GetVehicleFromDepotWndPt(pt.x, pt.y, &v, &gdvp) != MODE_DRAG_VEHICLE) return; VehicleID new_vehicle_over = INVALID_VEHICLE; if (gdvp.head != nullptr) { @@ -1042,11 +1040,10 @@ struct DepotWindow : Window { this->sel = INVALID_VEHICLE; this->SetDirty(); - NWidgetBase *nwi = this->GetWidget(WID_D_MATRIX); if (this->type == VEH_TRAIN) { GetDepotVehiclePtData gdvp = { nullptr, nullptr }; - if (this->GetVehicleFromDepotWndPt(pt.x - nwi->pos_x, pt.y - nwi->pos_y, &v, &gdvp) == MODE_DRAG_VEHICLE && sel != INVALID_VEHICLE) { + if (this->GetVehicleFromDepotWndPt(pt.x, pt.y, &v, &gdvp) == MODE_DRAG_VEHICLE && sel != INVALID_VEHICLE) { if (gdvp.wagon != nullptr && gdvp.wagon->index == sel && _ctrl_pressed) { Command::Post(STR_ERROR_CAN_T_REVERSE_DIRECTION_RAIL_VEHICLE, Vehicle::Get(sel)->tile, Vehicle::Get(sel)->index, true); } else if (gdvp.wagon == nullptr || gdvp.wagon->index != sel) { @@ -1056,7 +1053,7 @@ struct DepotWindow : Window { ShowVehicleViewWindow(gdvp.head); } } - } else if (this->GetVehicleFromDepotWndPt(pt.x - nwi->pos_x, pt.y - nwi->pos_y, &v, nullptr) == MODE_DRAG_VEHICLE && v != nullptr && sel == v->index) { + } else if (this->GetVehicleFromDepotWndPt(pt.x, pt.y, &v, nullptr) == MODE_DRAG_VEHICLE && v != nullptr && sel == v->index) { ShowVehicleViewWindow(v); } break;