Skip to content

Commit

Permalink
Centralise management of overlay focus handling
Browse files Browse the repository at this point in the history
Reduce duplication
  • Loading branch information
JGRennison committed Sep 23, 2024
1 parent 1e9aee9 commit e993948
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 202 deletions.
4 changes: 2 additions & 2 deletions src/dropdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,9 @@ int HideDropDownMenu(Window *pw)
return -1;
}

void GetParentWindowInfo(Window *w, WindowClass &parent_wc, WindowNumber &parent_wn)
void GetDropDownParentWindowInfo(const Window *w, WindowClass &parent_wc, WindowNumber &parent_wn)
{
DropdownWindow *dw = dynamic_cast<DropdownWindow*>(w);
const DropdownWindow *dw = dynamic_cast<const DropdownWindow *>(w);
assert(dw != nullptr);
Window *parent = FindWindowByToken(dw->parent_wnd_token);
if (parent != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion src/dropdown_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void ShowDropDownMenu(Window *w, std::span<const StringID> strings, int selected
/* Hide drop down menu of a parent window */
int HideDropDownMenu(Window *pw);

void GetParentWindowInfo(Window *w, WindowClass &parent_wc, WindowNumber &parent_wn);
void GetDropDownParentWindowInfo(const Window *w, WindowClass &parent_wc, WindowNumber &parent_wn);

/* Helper functions for commonly used drop down list items. */
std::unique_ptr<DropDownListItem> MakeDropDownListDividerItem();
Expand Down
2 changes: 1 addition & 1 deletion src/dropdown_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum DropDownSyncFocus {
DDSF_NOTIFY_LOST_FOCUS = 1 << 1,
DDSF_FOCUS_PARENT_ON_SELECT = 1 << 2,

DDSF_SHARED = DDSF_NOTIFY_RECV_FOCUS | DDSF_FOCUS_PARENT_ON_SELECT,
DDSF_SHARED = DDSF_FOCUS_PARENT_ON_SELECT,
};
DECLARE_ENUM_AS_BIT_SET(DropDownSyncFocus)

Expand Down
36 changes: 2 additions & 34 deletions src/order_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ struct CargoTypeOrdersWindow : public Window {

void Close(int data = 0) override
{
if (!FocusWindowById(WC_VEHICLE_ORDERS, this->window_number)) {
MarkDirtyFocusedRoutePaths(this->vehicle);
}
FocusWindowById(WC_VEHICLE_ORDERS, this->window_number);
this->Window::Close();
}

Expand Down Expand Up @@ -294,20 +292,6 @@ struct CargoTypeOrdersWindow : public Window {
}
}

virtual void OnFocus(Window *previously_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, previously_focused_window)) {
MarkDirtyFocusedRoutePaths(this->vehicle);
}
}

virtual void OnFocusLost(bool closing, Window *newly_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, newly_focused_window)) {
MarkDirtyFocusedRoutePaths(this->vehicle);
}
}

/**
* Some data on this window has become invalid.
* @param data Information about the changed data.
Expand Down Expand Up @@ -1994,9 +1978,7 @@ struct OrdersWindow : public GeneralVehicleWindow {
{
CloseWindowById(WC_VEHICLE_CARGO_TYPE_LOAD_ORDERS, this->window_number, false);
CloseWindowById(WC_VEHICLE_CARGO_TYPE_UNLOAD_ORDERS, this->window_number, false);
if (!FocusWindowById(WC_VEHICLE_VIEW, this->window_number)) {
MarkDirtyFocusedRoutePaths(this->vehicle);
}
FocusWindowById(WC_VEHICLE_VIEW, this->window_number);
this->GeneralVehicleWindow::Close();
}

Expand Down Expand Up @@ -3797,20 +3779,6 @@ struct OrdersWindow : public GeneralVehicleWindow {
this->vscroll->SetCapacityFromWidget(this, WID_O_ORDER_LIST, WidgetDimensions::scaled.framerect.Vertical());
}

virtual void OnFocus(Window *previously_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, previously_focused_window)) {
MarkDirtyFocusedRoutePaths(this->vehicle);
}
}

virtual void OnFocusLost(bool closing, Window *newly_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, newly_focused_window)) {
MarkDirtyFocusedRoutePaths(this->vehicle);
}
}

bool OnTooltip(Point pt, WidgetID widget, TooltipCloseCondition close_cond) override
{
switch (widget) {
Expand Down
18 changes: 1 addition & 17 deletions src/schdispatch_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,7 @@ struct SchdispatchWindow : GeneralVehicleWindow {

void Close(int data = 0) override
{
if (!FocusWindowById(WC_VEHICLE_VIEW, this->window_number)) {
MarkDirtyFocusedRoutePaths(this->vehicle);
}
FocusWindowById(WC_VEHICLE_VIEW, this->window_number);
this->GeneralVehicleWindow::Close();
}

Expand Down Expand Up @@ -1430,20 +1428,6 @@ struct SchdispatchWindow : GeneralVehicleWindow {
this->num_columns = nwi->current_x / nwi->resize_x;
}

virtual void OnFocus(Window *previously_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, previously_focused_window)) {
MarkDirtyFocusedRoutePaths(this->vehicle);
}
}

virtual void OnFocusLost(bool closing, Window *newly_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, newly_focused_window)) {
MarkDirtyFocusedRoutePaths(this->vehicle);
}
}

bool OnVehicleSelect(const Vehicle *v) override
{
if (v->orders == nullptr || v->orders->GetScheduledDispatchScheduleCount() == 0) return false;
Expand Down
18 changes: 1 addition & 17 deletions src/timetable_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,7 @@ struct TimetableWindow : GeneralVehicleWindow {

void Close(int data = 0) override
{
if (!FocusWindowById(WC_VEHICLE_VIEW, this->window_number)) {
MarkDirtyFocusedRoutePaths(this->vehicle);
}
FocusWindowById(WC_VEHICLE_VIEW, this->window_number);
this->GeneralVehicleWindow::Close();
}

Expand Down Expand Up @@ -1188,20 +1186,6 @@ struct TimetableWindow : GeneralVehicleWindow {
this->GetWidget<NWidgetStacked>(WID_VT_SEL_SHARED)->SetDisplayedPlane(this->vehicle->owner == _local_company && _ctrl_pressed ? 1 : 0);
}

virtual void OnFocus(Window *previously_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, previously_focused_window)) {
MarkDirtyFocusedRoutePaths(this->vehicle);
}
}

virtual void OnFocusLost(bool closing, Window *newly_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, newly_focused_window)) {
MarkDirtyFocusedRoutePaths(this->vehicle);
}
}

const Vehicle *GetVehicle()
{
return this->vehicle;
Expand Down
97 changes: 2 additions & 95 deletions src/vehicle_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -959,36 +959,11 @@ struct RefitWindow : public Window {
void Close(int data = 0) override
{
if (this->window_number != INVALID_VEHICLE) {
if (!FocusWindowById(WC_VEHICLE_VIEW, this->window_number)) {
if (this->window_number != INVALID_VEHICLE) {
const Vehicle *v = Vehicle::Get(this->window_number);
MarkDirtyFocusedRoutePaths(v);
}
}
FocusWindowById(WC_VEHICLE_VIEW, this->window_number);
}
this->Window::Close();
}

void OnFocus(Window *previously_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, previously_focused_window)) {
if (this->window_number != INVALID_VEHICLE) {
const Vehicle *v = Vehicle::Get(this->window_number);
MarkDirtyFocusedRoutePaths(v);
}
}
}

void OnFocusLost(bool closing, Window *newly_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, newly_focused_window)) {
if (this->window_number != INVALID_VEHICLE) {
const Vehicle *v = Vehicle::Get(this->window_number);
MarkDirtyFocusedRoutePaths(v);
}
}
}

void OnInit() override
{
/* (Re)build the refit list */
Expand Down Expand Up @@ -2278,14 +2253,6 @@ struct VehicleListWindow : public BaseVehicleListWindow {
BP_SHARED_ORDERS, ///< Show the normal caption.
};

void RefreshRouteOverlay() const
{
if (this->vli.type == VL_SHARED_ORDERS) {
const Vehicle *v = Vehicle::GetIfValid(this->vli.index);
MarkDirtyFocusedRoutePaths(v);
}
}

public:
VehicleListWindow(WindowDesc &desc, WindowNumber window_number) : BaseVehicleListWindow(desc, window_number)
{
Expand Down Expand Up @@ -2321,20 +2288,9 @@ struct VehicleListWindow : public BaseVehicleListWindow {
void Close(int data = 0) override
{
*this->sorting = this->vehgroups.GetListing();
this->RefreshRouteOverlay();
this->Window::Close();
}

virtual void OnFocus(Window *previously_focused_window) override
{
this->RefreshRouteOverlay();
}

virtual void OnFocusLost(bool closing, Window *newly_focused_window) override
{
this->RefreshRouteOverlay();
}

void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
{
switch (widget) {
Expand Down Expand Up @@ -2966,12 +2922,7 @@ struct VehicleDetailsWindow : Window {
void Close(int data = 0) override
{
if (this->window_number != INVALID_VEHICLE) {
if (!FocusWindowById(WC_VEHICLE_VIEW, this->window_number)) {
if (this->window_number != INVALID_VEHICLE) {
const Vehicle *v = Vehicle::Get(this->window_number);
MarkDirtyFocusedRoutePaths(v);
}
}
FocusWindowById(WC_VEHICLE_VIEW, this->window_number);
}
this->Window::Close();
}
Expand Down Expand Up @@ -3600,26 +3551,6 @@ struct VehicleDetailsWindow : Window {
this->vscroll->SetCapacityFromWidget(this, WID_VD_MATRIX);
}
}

virtual void OnFocus(Window *previously_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, previously_focused_window)) {
if (this->window_number != INVALID_VEHICLE) {
const Vehicle *v = Vehicle::Get(this->window_number);
MarkDirtyFocusedRoutePaths(v);
}
}
}

virtual void OnFocusLost(bool closing, Window *newly_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, newly_focused_window)) {
if (this->window_number != INVALID_VEHICLE) {
const Vehicle *v = Vehicle::Get(this->window_number);
MarkDirtyFocusedRoutePaths(v);
}
}
}
};

/** Vehicle details window descriptor. */
Expand Down Expand Up @@ -3916,10 +3847,6 @@ struct VehicleViewWindow : Window {

void Close([[maybe_unused]] int data = 0) override
{
if (this->window_number != INVALID_VEHICLE) {
const Vehicle *v = Vehicle::Get(this->window_number);
MarkDirtyFocusedRoutePaths(v);
}
CloseWindowById(WC_VEHICLE_ORDERS, this->window_number, false);
CloseWindowById(WC_VEHICLE_REFIT, this->window_number, false);
CloseWindowById(WC_VEHICLE_DETAILS, this->window_number, false);
Expand All @@ -3933,26 +3860,6 @@ struct VehicleViewWindow : Window {
this->Window::Close();
}

virtual void OnFocus(Window *previously_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, previously_focused_window)) {
if (this->window_number != INVALID_VEHICLE) {
const Vehicle *v = Vehicle::Get(this->window_number);
MarkDirtyFocusedRoutePaths(v);
}
}
}

virtual void OnFocusLost(bool closing, Window *newly_focused_window) override
{
if (HasFocusedVehicleChanged(this->window_number, newly_focused_window)) {
if (this->window_number != INVALID_VEHICLE) {
const Vehicle *v = Vehicle::Get(this->window_number);
MarkDirtyFocusedRoutePaths(v);
}
}
}

void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
{
const Vehicle *v = Vehicle::Get(this->window_number);
Expand Down
32 changes: 0 additions & 32 deletions src/vehicle_gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,36 +124,4 @@ void StopGlobalFollowVehicle(const Vehicle *v);
void DrawVehicleImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type, int skip);
void SetMouseCursorVehicle(const Vehicle *v, EngineImageType image_type);

/**
* Tell if the focused window concerns the specified vehicle.
* @param vid Vehicle id to check.
* @param ref_window The window to check against.
* @return True if the focused window is about specified vehicle.
*/
inline bool HasFocusedVehicleChanged(const VehicleID vid, Window *ref_window)
{
if (ref_window != nullptr) {
WindowClass wc = ref_window->window_class;
WindowNumber wn = ref_window->window_number;

if (wc == WC_DROPDOWN_MENU) GetParentWindowInfo(ref_window, wc, wn);

switch (wc) {
default:
break;
case WC_VEHICLE_DETAILS:
case WC_VEHICLE_REFIT:
case WC_VEHICLE_ORDERS:
case WC_VEHICLE_TIMETABLE:
case WC_VEHICLE_VIEW:
case WC_VEHICLE_CARGO_TYPE_LOAD_ORDERS:
case WC_VEHICLE_CARGO_TYPE_UNLOAD_ORDERS:
case WC_SCHDISPATCH_SLOTS:
return ((uint32_t) wn != vid);
}
}

return true;
}

#endif /* VEHICLE_GUI_H */
15 changes: 12 additions & 3 deletions src/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2506,13 +2506,13 @@ static void ViewportDrawStrings(ViewportDrawerDynamic *vdd, ZoomLevel zoom, cons
}
}

static inline Vehicle *GetVehicleFromWindow(Window *w)
static inline Vehicle *GetVehicleFromWindow(const Window *w)
{
if (w != nullptr) {
WindowClass wc = w->window_class;
WindowNumber wn = w->window_number;

if (wc == WC_DROPDOWN_MENU) GetParentWindowInfo(w, wc, wn);
if (wc == WC_DROPDOWN_MENU) GetDropDownParentWindowInfo(w, wc, wn);

switch (wc) {
case WC_VEHICLE_VIEW:
Expand All @@ -2523,7 +2523,7 @@ static inline Vehicle *GetVehicleFromWindow(Window *w)
case WC_VEHICLE_CARGO_TYPE_LOAD_ORDERS:
case WC_VEHICLE_CARGO_TYPE_UNLOAD_ORDERS:
case WC_SCHDISPATCH_SLOTS:
if (wn != INVALID_VEHICLE) return Vehicle::Get(wn);
if (wn != INVALID_VEHICLE) return Vehicle::GetIfValid(wn);
break;
case WC_TRAINS_LIST:
case WC_ROADVEH_LIST:
Expand Down Expand Up @@ -4889,6 +4889,15 @@ void CheckMarkDirtyViewportRoutePaths()
}
}

void HandleViewportRoutePathFocusChange(const Window *old, const Window *focused)
{
const Vehicle *old_v = (old != nullptr) ? GetVehicleFromWindow(old) : nullptr;
const Vehicle *new_v = (focused != nullptr) ? GetVehicleFromWindow(focused) : nullptr;
if (old_v != new_v) {
_vp_focused_window_route_overlay.MarkAllDirty(new_v);
}
}

void AddFixedViewportRoutePath(VehicleID veh)
{
FixedVehicleViewportRouteOverlay &overlay = _vp_fixed_route_overlays.emplace_back();
Expand Down
1 change: 1 addition & 0 deletions src/viewport_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void MarkTileLineDirty(const TileIndex from_tile, const TileIndex to_tile, Viewp
void MarkDirtyFocusedRoutePaths(const Vehicle *veh);
void CheckMarkDirtyViewportRoutePaths(const Vehicle *veh);
void CheckMarkDirtyViewportRoutePaths();
void HandleViewportRoutePathFocusChange(const Window *old, const Window *focused);
void AddFixedViewportRoutePath(VehicleID veh);
void RemoveFixedViewportRoutePath(VehicleID veh);
void ChangeFixedViewportRoutePath(VehicleID from, VehicleID to);
Expand Down
Loading

0 comments on commit e993948

Please sign in to comment.