diff --git a/src/group_gui.cpp b/src/group_gui.cpp --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -28,53 +28,39 @@ #include "table/strings.h" #include "table/sprites.h" -struct grouplist_d { - const Group **sort_list; - list_d l; // General list struct -}; -assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(grouplist_d)); +typedef GUIList GUIGroupList; struct groupveh_d : vehiclelist_d { GroupID group_sel; VehicleID vehicle_sel; - grouplist_d gl; + GUIGroupList groups; }; assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(groupveh_d)); - -struct Sorting { - Listing aircraft; - Listing roadveh; - Listing ship; - Listing train; -}; - -static Sorting _sorting; +assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(vehiclelist_d)); -static void BuildGroupList(grouplist_d* gl, PlayerID owner, VehicleType vehicle_type) +static void BuildGroupList(GUIGroupList *gl, PlayerID owner, VehicleType vehicle_type) { - const Group** list; - const Group *g; uint n = 0; - if (!(gl->l.flags & VL_REBUILD)) return; + if (!(gl->flags & VL_REBUILD)) return; - list = MallocT(GetGroupArraySize()); + const Group **list = MallocT(GetGroupArraySize()); + const Group *g; FOR_ALL_GROUPS(g) { if (g->owner == owner && g->vehicle_type == vehicle_type) list[n++] = g; } - free((void*)gl->sort_list); - gl->sort_list = MallocT(n); - gl->l.list_length = n; + gl->sort_list = ReallocT(gl->sort_list, n); + gl->list_length = n; for (uint i = 0; i < n; ++i) gl->sort_list[i] = list[i]; - free((void*)list); + free(list); - gl->l.flags &= ~VL_REBUILD; - gl->l.flags |= VL_RESORT; + gl->flags &= ~VL_REBUILD; + gl->flags |= VL_RESORT; } @@ -107,14 +93,14 @@ static int CDECL GroupNameSorter(const v } -static void SortGroupList(grouplist_d *gl) +static void SortGroupList(GUIGroupList *gl) { - if (!(gl->l.flags & VL_RESORT)) return; + if (!(gl->flags & VL_RESORT)) return; - qsort((void*)gl->sort_list, gl->l.list_length, sizeof(gl->sort_list[0]), GroupNameSorter); + qsort((void*)gl->sort_list, gl->list_length, sizeof(gl->sort_list[0]), GroupNameSorter); - gl->l.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; - gl->l.flags &= ~VL_RESORT; + gl->resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; + gl->flags &= ~VL_RESORT; } @@ -180,8 +166,9 @@ static const Widget _group_widgets[] = { static void CreateVehicleGroupWindow(Window *w) { const PlayerID owner = (PlayerID)GB(w->window_number, 0, 8); - groupveh_d *gv = &WP(w, groupveh_d); - grouplist_d *gl = &WP(w, groupveh_d).gl; + groupveh_d *gv = &WP(w, groupveh_d); + GUIVehicleList *vl = &gv->vehicles; + GUIGroupList *gl = &gv->groups; w->caption_color = owner; w->hscroll.cap = 224; @@ -208,21 +195,22 @@ static void CreateVehicleGroupWindow(Win switch (gv->vehicle_type) { default: NOT_REACHED(); - case VEH_TRAIN: gv->_sorting = &_sorting.train; break; - case VEH_ROAD: gv->_sorting = &_sorting.roadveh; break; - case VEH_SHIP: gv->_sorting = &_sorting.ship; break; - case VEH_AIRCRAFT: gv->_sorting = &_sorting.aircraft; break; + case VEH_TRAIN: gv->sorting = &_sorting.train; break; + case VEH_ROAD: gv->sorting = &_sorting.roadveh; break; + case VEH_SHIP: gv->sorting = &_sorting.ship; break; + case VEH_AIRCRAFT: gv->sorting = &_sorting.aircraft; break; } - gv->sort_list = NULL; gv->vehicle_type = (VehicleType)GB(w->window_number, 11, 5); - gv->l.sort_type = gv->_sorting->criteria; - gv->l.flags = VL_REBUILD | (gv->_sorting->order ? VL_DESC : VL_NONE); - gv->l.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer + + vl->sort_list = NULL; + vl->sort_type = gv->sorting->criteria; + vl->flags = VL_REBUILD | (gv->sorting->order ? VL_DESC : VL_NONE); + vl->resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer gl->sort_list = NULL; - gl->l.flags = VL_REBUILD | VL_NONE; - gl->l.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer + gl->flags = VL_REBUILD | VL_NONE; + gl->resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer gv->group_sel = ALL_GROUP; gv->vehicle_sel = INVALID_VEHICLE; @@ -305,15 +293,16 @@ static void ShowGroupActionDropdown(Wind static void GroupWndProc(Window *w, WindowEvent *e) { const PlayerID owner = (PlayerID)GB(w->window_number, 0, 8); - groupveh_d *gv = &WP(w, groupveh_d); - grouplist_d *gl = &WP(w, groupveh_d).gl; + groupveh_d *gv = &WP(w, groupveh_d); + GUIVehicleList *vl = &gv->vehicles; + GUIGroupList *gl = &gv->groups; gv->vehicle_type = (VehicleType)GB(w->window_number, 11, 5); switch(e->event) { case WE_INVALIDATE_DATA: - gv->l.flags |= VL_REBUILD; - gl->l.flags |= VL_REBUILD; + vl->flags |= VL_REBUILD; + gl->flags |= VL_REBUILD; if (!(IsAllGroupID(gv->group_sel) || IsDefaultGroupID(gv->group_sel) || IsValidGroupID(gv->group_sel))) { gv->group_sel = ALL_GROUP; HideDropDownMenu(w); @@ -341,17 +330,17 @@ static void GroupWndProc(Window *w, Wind BuildGroupList(gl, owner, gv->vehicle_type); SortGroupList(gl); - SetVScrollCount(w, gl->l.list_length); - SetVScroll2Count(w, gv->l.list_length); + SetVScrollCount(w, gl->list_length); + SetVScroll2Count(w, vl->list_length); /* The drop down menu is out, *but* it may not be used, retract it. */ - if (gv->l.list_length == 0 && w->IsWidgetLowered(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN)) { + if (vl->list_length == 0 && w->IsWidgetLowered(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN)) { w->RaiseWidget(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN); HideDropDownMenu(w); } /* Disable all lists management button when the list is empty */ - w->SetWidgetsDisabledState(gv->l.list_length == 0 || _local_player != owner, + w->SetWidgetsDisabledState(vl->list_length == 0 || _local_player != owner, GRP_WIDGET_STOP_ALL, GRP_WIDGET_START_ALL, GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN, @@ -380,7 +369,7 @@ static void GroupWndProc(Window *w, Wind We list all vehicles or ungrouped vehicles */ if (IsDefaultGroupID(gv->group_sel) || IsAllGroupID(gv->group_sel)) { SetDParam(0, owner); - SetDParam(1, gv->l.list_length); + SetDParam(1, vl->list_length); switch (gv->vehicle_type) { case VEH_TRAIN: @@ -429,7 +418,7 @@ static void GroupWndProc(Window *w, Wind } /* Set text of sort by dropdown */ - w->widget[GRP_WIDGET_SORT_BY_DROPDOWN].data = _vehicle_sort_listing[gv->l.sort_type]; + w->widget[GRP_WIDGET_SORT_BY_DROPDOWN].data = _vehicle_sort_listing[vl->sort_type]; DrawWindowWidgets(w); @@ -462,7 +451,7 @@ static void GroupWndProc(Window *w, Wind DrawString(10, y1, str_no_group_veh, IsDefaultGroupID(gv->group_sel) ? TC_WHITE : TC_BLACK); - max = min(w->vscroll.pos + w->vscroll.cap, gl->l.list_length); + max = min(w->vscroll.pos + w->vscroll.cap, gl->list_length); for (i = w->vscroll.pos ; i < max ; ++i) { const Group *g = gl->sort_list[i]; @@ -479,12 +468,12 @@ static void GroupWndProc(Window *w, Wind DrawStringRightAligned(187, y1 + 1, STR_GROUP_TINY_NUM, (gv->group_sel == g->index) ? TC_WHITE : TC_BLACK); } - DrawSortButtonState(w, GRP_WIDGET_SORT_BY_ORDER, gv->l.flags & VL_DESC ? SBS_DOWN : SBS_UP); + DrawSortButtonState(w, GRP_WIDGET_SORT_BY_ORDER, vl->flags & VL_DESC ? SBS_DOWN : SBS_UP); /* Draw Matrix Vehicle according to the vehicle list built before */ - max = min(w->vscroll2.pos + w->vscroll2.cap, gv->l.list_length); + max = min(w->vscroll2.pos + w->vscroll2.cap, vl->list_length); for (i = w->vscroll2.pos ; i < max ; ++i) { - const Vehicle* v = gv->sort_list[i]; + const Vehicle* v = vl->sort_list[i]; assert(v->type == gv->vehicle_type && v->owner == owner); @@ -516,21 +505,21 @@ static void GroupWndProc(Window *w, Wind switch(e->we.click.widget) { case GRP_WIDGET_SORT_BY_ORDER: // Flip sorting method ascending/descending - gv->l.flags ^= VL_DESC; - gv->l.flags |= VL_RESORT; + vl->flags ^= VL_DESC; + vl->flags |= VL_RESORT; - gv->_sorting->order = !!(gv->l.flags & VL_DESC); + gv->sorting->order = !!(vl->flags & VL_DESC); w->SetDirty(); break; case GRP_WIDGET_SORT_BY_DROPDOWN: // Select sorting criteria dropdown menu - ShowDropDownMenu(w, _vehicle_sort_listing, gv->l.sort_type, GRP_WIDGET_SORT_BY_DROPDOWN, 0, (gv->vehicle_type == VEH_TRAIN || gv->vehicle_type == VEH_ROAD) ? 0 : (1 << 10)); + ShowDropDownMenu(w, _vehicle_sort_listing, vl->sort_type, GRP_WIDGET_SORT_BY_DROPDOWN, 0, (gv->vehicle_type == VEH_TRAIN || gv->vehicle_type == VEH_ROAD) ? 0 : (1 << 10)); return; case GRP_WIDGET_ALL_VEHICLES: // All vehicles button if (!IsAllGroupID(gv->group_sel)) { gv->group_sel = ALL_GROUP; - gv->l.flags |= VL_REBUILD; + vl->flags |= VL_REBUILD; w->SetDirty(); } break; @@ -538,7 +527,7 @@ static void GroupWndProc(Window *w, Wind case GRP_WIDGET_DEFAULT_VEHICLES: // Ungrouped vehicles button if (!IsDefaultGroupID(gv->group_sel)) { gv->group_sel = DEFAULT_GROUP; - gv->l.flags |= VL_REBUILD; + vl->flags |= VL_REBUILD; w->SetDirty(); } break; @@ -550,11 +539,11 @@ static void GroupWndProc(Window *w, Wind id_g += w->vscroll.pos; - if (id_g >= gl->l.list_length) return; + if (id_g >= gl->list_length) return; gv->group_sel = gl->sort_list[id_g]->index;; - gv->l.flags |= VL_REBUILD; + vl->flags |= VL_REBUILD; w->SetDirty(); break; } @@ -567,9 +556,9 @@ static void GroupWndProc(Window *w, Wind id_v += w->vscroll2.pos; - if (id_v >= gv->l.list_length) return; // click out of list bound + if (id_v >= vl->list_length) return; // click out of list bound - v = gv->sort_list[id_v]; + v = vl->sort_list[id_v]; gv->vehicle_sel = v->index; @@ -657,7 +646,7 @@ static void GroupWndProc(Window *w, Wind id_g += w->vscroll.pos; - if (id_g >= gl->l.list_length) return; + if (id_g >= gl->list_length) return; DoCommandP(0, gl->sort_list[id_g]->index, vindex, NULL, CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_GROUP_CAN_T_ADD_VEHICLE)); @@ -677,9 +666,9 @@ static void GroupWndProc(Window *w, Wind id_v += w->vscroll2.pos; - if (id_v >= gv->l.list_length) return; // click out of list bound + if (id_v >= vl->list_length) return; // click out of list bound - v = gv->sort_list[id_v]; + v = vl->sort_list[id_v]; if (vindex == v->index) { ShowVehicleViewWindow(v); @@ -713,15 +702,15 @@ static void GroupWndProc(Window *w, Wind case WE_DROPDOWN_SELECT: // we have selected a dropdown item in the list switch (e->we.dropdown.button) { case GRP_WIDGET_SORT_BY_DROPDOWN: - if (gv->l.sort_type != e->we.dropdown.index) { - gv->l.flags |= VL_RESORT; - gv->l.sort_type = e->we.dropdown.index; - gv->_sorting->criteria = gv->l.sort_type; + if (vl->sort_type != e->we.dropdown.index) { + vl->flags |= VL_RESORT; + vl->sort_type = e->we.dropdown.index; + gv->sorting->criteria = vl->sort_type; } break; case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN: - assert(gv->l.list_length != 0); + assert(vl->list_length != 0); switch (e->we.dropdown.index) { case GALF_REPLACE: // Replace window @@ -758,21 +747,21 @@ static void GroupWndProc(Window *w, Wind case WE_DESTROY: - free((void*)gv->sort_list); + free((void*)vl->sort_list); free((void*)gl->sort_list); break; case WE_TICK: // resort the lists every 20 seconds orso (10 days) if (_pause_game != 0) break; - if (--gv->l.resort_timer == 0) { - gv->l.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; - gv->l.flags |= VL_RESORT; + if (--vl->resort_timer == 0) { + vl->resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; + vl->flags |= VL_RESORT; w->SetDirty(); } - if (--gl->l.resort_timer == 0) { - gl->l.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; - gl->l.flags |= VL_RESORT; + if (--gl->resort_timer == 0) { + gl->resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; + gl->flags |= VL_RESORT; w->SetDirty(); } break; diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -168,11 +168,12 @@ enum NetworkGameWindowWidgets { NGWW_CANCEL, ///< 'Cancel' button }; +typedef GUIList GUIGameServerList; + struct NetworkGameWindow : public QueryStringBaseWindow { byte field; ///< selected text-field NetworkGameList *server; ///< selected server - NetworkGameList **sort_list; ///< list of games (sorted) - list_d ld; ///< accompanying list-administration + GUIGameServerList servers; ///< list with game servers. NetworkGameWindow(const WindowDesc *desc) : QueryStringBaseWindow(desc) { @@ -188,16 +189,16 @@ struct NetworkGameWindow : public QueryS this->field = NGWW_PLAYER; this->server = NULL; - this->sort_list = NULL; - this->ld.flags = VL_REBUILD | (_ng_sorting.order ? VL_DESC : VL_NONE); - this->ld.sort_type = _ng_sorting.criteria; + this->servers.sort_list = NULL; + this->servers.flags = VL_REBUILD | (_ng_sorting.order ? VL_DESC : VL_NONE); + this->servers.sort_type = _ng_sorting.criteria; this->FindWindowPlacementAndResize(desc); } ~NetworkGameWindow() { - free(this->sort_list); + free(this->servers.sort_list); } /** @@ -209,23 +210,23 @@ struct NetworkGameWindow : public QueryS NetworkGameList *ngl_temp; uint n = 0; - if (!(this->ld.flags & VL_REBUILD)) return; + if (!(this->servers.flags & VL_REBUILD)) return; /* Count the number of games in the list */ for (ngl_temp = _network_game_list; ngl_temp != NULL; ngl_temp = ngl_temp->next) n++; if (n == 0) return; /* Create temporary array of games to use for listing */ - this->sort_list = ReallocT(this->sort_list, n); - this->ld.list_length = n; + this->servers.sort_list = ReallocT(this->servers.sort_list, n); + this->servers.list_length = n; for (n = 0, ngl_temp = _network_game_list; ngl_temp != NULL; ngl_temp = ngl_temp->next) { - this->sort_list[n++] = ngl_temp; + this->servers.sort_list[n++] = ngl_temp; } /* Force resort */ - this->ld.flags &= ~VL_REBUILD; - this->ld.flags |= VL_RESORT; + this->servers.flags &= ~VL_REBUILD; + this->servers.flags |= VL_RESORT; } void SortNetworkGameList() @@ -239,23 +240,23 @@ struct NetworkGameWindow : public QueryS NetworkGameList *item; uint i; - if (!(this->ld.flags & VL_RESORT)) return; - if (this->ld.list_length == 0) return; + if (!(this->servers.flags & VL_RESORT)) return; + if (this->servers.list_length == 0) return; - _internal_sort_order = !!(this->ld.flags & VL_DESC); - qsort(this->sort_list, this->ld.list_length, sizeof(this->sort_list[0]), ngame_sorter[this->ld.sort_type]); + _internal_sort_order = !!(this->servers.flags & VL_DESC); + qsort(this->servers.sort_list, this->servers.list_length, sizeof(this->servers.sort_list[0]), ngame_sorter[this->servers.sort_type]); /* After sorting ngl->sort_list contains the sorted items. Put these back * into the original list. Basically nothing has changed, we are only * shuffling the ->next pointers */ - _network_game_list = this->sort_list[0]; - for (item = _network_game_list, i = 1; i != this->ld.list_length; i++) { - item->next = this->sort_list[i]; + _network_game_list = this->servers.sort_list[0]; + for (item = _network_game_list, i = 1; i != this->servers.list_length; i++) { + item->next = this->servers.sort_list[i]; item = item->next; } item->next = NULL; - this->ld.flags &= ~VL_RESORT; + this->servers.flags &= ~VL_RESORT; } /** @@ -294,13 +295,13 @@ struct NetworkGameWindow : public QueryS virtual void OnPaint() { const NetworkGameList *sel = this->server; - const SortButtonState arrow = (this->ld.flags & VL_DESC) ? SBS_DOWN : SBS_UP; + const SortButtonState arrow = (this->servers.flags & VL_DESC) ? SBS_DOWN : SBS_UP; - if (this->ld.flags & VL_REBUILD) { + if (this->servers.flags & VL_REBUILD) { this->BuildNetworkGameList(); - SetVScrollCount(this, this->ld.list_length); + SetVScrollCount(this, this->servers.list_length); } - if (this->ld.flags & VL_RESORT) this->SortNetworkGameList(); + if (this->servers.flags & VL_RESORT) this->SortNetworkGameList(); /* 'Refresh' button invisible if no server selected */ this->SetWidgetDisabledState(NGWW_REFRESH, sel == NULL); @@ -325,7 +326,7 @@ struct NetworkGameWindow : public QueryS DrawString(this->widget[NGWW_PLAYER].left - 100, 23, STR_NETWORK_PLAYER_NAME, TC_GOLD); /* Sort based on widgets: name, clients, compatibility */ - switch (this->ld.sort_type) { + switch (this->servers.sort_type) { case NGWW_NAME - NGWW_NAME: DrawSortButtonState(this, NGWW_NAME, arrow); break; case NGWW_CLIENTS - NGWW_NAME: DrawSortButtonState(this, NGWW_CLIENTS, arrow); break; case NGWW_INFO - NGWW_NAME: DrawSortButtonState(this, NGWW_INFO, arrow); break; @@ -446,12 +447,12 @@ struct NetworkGameWindow : public QueryS case NGWW_NAME: // Sort by name case NGWW_CLIENTS: // Sort by connected clients case NGWW_INFO: // Connectivity (green dot) - if (this->ld.sort_type == widget - NGWW_NAME) this->ld.flags ^= VL_DESC; - this->ld.flags |= VL_RESORT; - this->ld.sort_type = widget - NGWW_NAME; + if (this->servers.sort_type == widget - NGWW_NAME) this->servers.flags ^= VL_DESC; + this->servers.flags |= VL_RESORT; + this->servers.sort_type = widget - NGWW_NAME; - _ng_sorting.order = !!(this->ld.flags & VL_DESC); - _ng_sorting.criteria = this->ld.sort_type; + _ng_sorting.order = !!(this->servers.flags & VL_DESC); + _ng_sorting.criteria = this->servers.sort_type; this->SetDirty(); break; @@ -537,7 +538,7 @@ struct NetworkGameWindow : public QueryS virtual void OnInvalidateData(int data) { if (data != 0) this->server = NULL; - this->ld.flags |= VL_REBUILD; + this->servers.flags |= VL_REBUILD; this->SetDirty(); } @@ -580,7 +581,7 @@ struct NetworkGameWindow : public QueryS this->widget[NGWW_MATRIX].data = (this->vscroll.cap << 8) + 1; - SetVScrollCount(this, this->ld.list_length); + SetVScrollCount(this, this->servers.list_length); int widget_width = this->widget[NGWW_FIND].right - this->widget[NGWW_FIND].left; int space = (this->width - 4 * widget_width - 25) / 3; diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -54,15 +54,7 @@ struct refit_d { }; assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(refit_d)); -struct Sorting { - Listing aircraft; - Listing roadveh; - Listing ship; - Listing train; -}; - -static Sorting _sorting; - +Sorting _sorting; static bool _internal_sort_order; // descending/ascending typedef int CDECL VehicleSortListingTypeFunction(const void*, const void*); @@ -124,7 +116,7 @@ static void SetVehicleListsFlag(SortList case WC_ROADVEH_LIST: case WC_SHIPS_LIST: case WC_AIRCRAFT_LIST: - WP(w, vehiclelist_d).l.flags |= sl_flag; + WP(w, vehiclelist_d).vehicles.flags |= sl_flag; w->SetDirty(); break; @@ -151,14 +143,14 @@ void ResortVehicleLists() void BuildVehicleList(vehiclelist_d *vl, PlayerID owner, uint16 index, uint16 window_type) { - if (!(vl->l.flags & VL_REBUILD)) return; + if (!(vl->vehicles.flags & VL_REBUILD)) return; DEBUG(misc, 3, "Building vehicle list for player %d at station %d", owner, index); - vl->l.list_length = GenerateVehicleSortList(&vl->sort_list, &vl->length_of_sort_list, vl->vehicle_type, owner, index, window_type); + vl->vehicles.list_length = GenerateVehicleSortList(&vl->vehicles.sort_list, &vl->vehicles.list_length, vl->vehicle_type, owner, index, window_type); - vl->l.flags &= ~VL_REBUILD; - vl->l.flags |= VL_RESORT; + vl->vehicles.flags &= ~VL_REBUILD; + vl->vehicles.flags |= VL_RESORT; } /* cached values for VehicleNameSorter to spare many GetString() calls */ @@ -167,17 +159,17 @@ static char _last_name[2][64] void SortVehicleList(vehiclelist_d *vl) { - if (!(vl->l.flags & VL_RESORT)) return; + if (!(vl->vehicles.flags & VL_RESORT)) return; /* invalidate cached values for name sorter - vehicle names could change */ _last_vehicle[0] = _last_vehicle[1] = NULL; - _internal_sort_order = (vl->l.flags & VL_DESC) != 0; - qsort((void*)vl->sort_list, vl->l.list_length, sizeof(vl->sort_list[0]), - _vehicle_sorter[vl->l.sort_type]); + _internal_sort_order = (vl->vehicles.flags & VL_DESC) != 0; + qsort((void*)vl->vehicles.sort_list, vl->vehicles.list_length, sizeof(vl->vehicles.sort_list[0]), + _vehicle_sorter[vl->vehicles.sort_type]); - vl->l.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; - vl->l.flags &= ~VL_RESORT; + vl->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; + vl->vehicles.flags &= ~VL_RESORT; } void DepotSortList(Vehicle **v, uint16 length) @@ -822,8 +814,8 @@ static void CreateVehicleListWindow(Wind PlayerID player = (PlayerID)GB(w->window_number, 0, 8); vl->vehicle_type = (VehicleType)GB(w->window_number, 11, 5); - vl->length_of_sort_list = 0; - vl->sort_list = NULL; + vl->vehicles.list_length = 0; + vl->vehicles.sort_list = NULL; w->caption_color = player; /* Hide the widgets that we will not use in this window @@ -924,17 +916,16 @@ static void CreateVehicleListWindow(Wind * point to the correct global _sorting struct so we are freed * from having conditionals during window operation */ switch (vl->vehicle_type) { - case VEH_TRAIN: vl->_sorting = &_sorting.train; break; - case VEH_ROAD: vl->_sorting = &_sorting.roadveh; break; - case VEH_SHIP: vl->_sorting = &_sorting.ship; break; - case VEH_AIRCRAFT: vl->_sorting = &_sorting.aircraft; break; + case VEH_TRAIN: vl->sorting = &_sorting.train; break; + case VEH_ROAD: vl->sorting = &_sorting.roadveh; break; + case VEH_SHIP: vl->sorting = &_sorting.ship; break; + case VEH_AIRCRAFT: vl->sorting = &_sorting.aircraft; break; default: NOT_REACHED(); break; } - vl->l.flags = VL_REBUILD | (vl->_sorting->order ? VL_DESC : VL_NONE); - vl->l.sort_type = vl->_sorting->criteria; - vl->sort_list = NULL; - vl->l.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer + vl->vehicles.flags = VL_REBUILD | (vl->sorting->order ? VL_DESC : VL_NONE); + vl->vehicles.sort_type = vl->sorting->criteria; + vl->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer } void DrawSmallOrderList(const Vehicle *v, int x, int y) @@ -973,12 +964,12 @@ static void DrawVehicleListWindow(Window BuildVehicleList(vl, owner, index, window_type); SortVehicleList(vl); - SetVScrollCount(w, vl->l.list_length); + SetVScrollCount(w, vl->vehicles.list_length); /* draw the widgets */ switch (window_type) { case VLW_SHARED_ORDERS: /* Shared Orders */ - if (vl->l.list_length == 0) { + if (vl->vehicles.list_length == 0) { /* We can't open this window without vehicles using this order * and we should close the window when deleting the order */ NOT_REACHED(); @@ -1014,7 +1005,7 @@ static void DrawVehicleListWindow(Window default: NOT_REACHED(); break; } - w->SetWidgetsDisabledState(vl->l.list_length == 0, + w->SetWidgetsDisabledState(vl->vehicles.list_length == 0, VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN, VLW_WIDGET_STOP_ALL, VLW_WIDGET_START_ALL, @@ -1023,13 +1014,13 @@ static void DrawVehicleListWindow(Window DrawWindowWidgets(w); /* draw sorting criteria string */ - DrawString(85, 15, _vehicle_sort_listing[vl->l.sort_type], TC_BLACK); + DrawString(85, 15, _vehicle_sort_listing[vl->vehicles.sort_type], TC_BLACK); /* draw arrow pointing up/down for ascending/descending sorting */ - DrawSortButtonState(w, VLW_WIDGET_SORT_ORDER, vl->l.flags & VL_DESC ? SBS_DOWN : SBS_UP); + DrawSortButtonState(w, VLW_WIDGET_SORT_ORDER, vl->vehicles.flags & VL_DESC ? SBS_DOWN : SBS_UP); - max = min(w->vscroll.pos + w->vscroll.cap, vl->l.list_length); + max = min(w->vscroll.pos + w->vscroll.cap, vl->vehicles.list_length); for (i = w->vscroll.pos; i < max; ++i) { - const Vehicle *v = vl->sort_list[i]; + const Vehicle *v = vl->vehicles.sort_list[i]; StringID str; SetDParam(0, v->GetDisplayProfitThisYear()); @@ -1084,14 +1075,14 @@ void PlayerVehWndProc(Window *w, WindowE case WE_CLICK: { switch (e->we.click.widget) { case VLW_WIDGET_SORT_ORDER: /* Flip sorting method ascending/descending */ - vl->l.flags ^= VL_DESC; - vl->l.flags |= VL_RESORT; + vl->vehicles.flags ^= VL_DESC; + vl->vehicles.flags |= VL_RESORT; - vl->_sorting->order = !!(vl->l.flags & VL_DESC); + vl->sorting->order = !!(vl->vehicles.flags & VL_DESC); w->SetDirty(); break; case VLW_WIDGET_SORT_BY_PULLDOWN:/* Select sorting criteria dropdown menu */ - ShowDropDownMenu(w, _vehicle_sort_listing, vl->l.sort_type, VLW_WIDGET_SORT_BY_PULLDOWN, 0, (vl->vehicle_type == VEH_TRAIN || vl->vehicle_type == VEH_ROAD) ? 0 : (1 << 10)); + ShowDropDownMenu(w, _vehicle_sort_listing, vl->vehicles.sort_type, VLW_WIDGET_SORT_BY_PULLDOWN, 0, (vl->vehicle_type == VEH_TRAIN || vl->vehicle_type == VEH_ROAD) ? 0 : (1 << 10)); return; case VLW_WIDGET_LIST: { /* Matrix to show vehicles */ uint32 id_v = (e->we.click.pt.y - PLY_WND_PRC__OFFSET_TOP_WIDGET) / w->resize.step_height; @@ -1101,9 +1092,9 @@ void PlayerVehWndProc(Window *w, WindowE id_v += w->vscroll.pos; - if (id_v >= vl->l.list_length) return; // click out of list bound + if (id_v >= vl->vehicles.list_length) return; // click out of list bound - v = vl->sort_list[id_v]; + v = vl->vehicles.sort_list[id_v]; ShowVehicleViewWindow(v); } break; @@ -1143,15 +1134,15 @@ void PlayerVehWndProc(Window *w, WindowE case WE_DROPDOWN_SELECT: /* we have selected a dropdown item in the list */ switch (e->we.dropdown.button) { case VLW_WIDGET_SORT_BY_PULLDOWN: - if (vl->l.sort_type != e->we.dropdown.index) { + if (vl->vehicles.sort_type != e->we.dropdown.index) { /* value has changed -> resort */ - vl->l.flags |= VL_RESORT; - vl->l.sort_type = e->we.dropdown.index; - vl->_sorting->criteria = vl->l.sort_type; + vl->vehicles.flags |= VL_RESORT; + vl->vehicles.sort_type = e->we.dropdown.index; + vl->sorting->criteria = vl->vehicles.sort_type; } break; case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN: - assert(vl->l.list_length != 0); + assert(vl->vehicles.list_length != 0); switch (e->we.dropdown.index) { case 0: /* Replace window */ @@ -1179,18 +1170,18 @@ void PlayerVehWndProc(Window *w, WindowE break; case WE_DESTROY: - free((void*)vl->sort_list); + free((void*)vl->vehicles.sort_list); break; case WE_TICK: /* resort the list every 20 seconds orso (10 days) */ if (_pause_game != 0) break; - if (--vl->l.resort_timer == 0) { + if (--vl->vehicles.resort_timer == 0) { StationID station = ((w->window_number & VLW_MASK) == VLW_STATION_LIST) ? GB(w->window_number, 16, 16) : INVALID_STATION; PlayerID owner = (PlayerID)w->caption_color; DEBUG(misc, 3, "Periodic resort %d list player %d at station %d", vl->vehicle_type, owner, station); - vl->l.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; - vl->l.flags |= VL_RESORT; + vl->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; + vl->vehicles.flags |= VL_RESORT; w->SetDirty(); } break; diff --git a/src/vehicle_gui.h b/src/vehicle_gui.h --- a/src/vehicle_gui.h +++ b/src/vehicle_gui.h @@ -15,12 +15,6 @@ void DrawVehicleProfitButton(const Vehicle *v, int x, int y); void ShowVehicleRefitWindow(const Vehicle *v, VehicleOrderID order); -/* sorter stuff */ -void RebuildVehicleLists(); -void ResortVehicleLists(); -void SortVehicleList(vehiclelist_d *vl); -void BuildVehicleList(vehiclelist_d *vl, PlayerID owner, uint16 index, uint16 window_type); - #define PERIODIC_RESORT_DAYS 10 extern const StringID _vehicle_sort_listing[]; @@ -69,7 +63,7 @@ static inline bool ValidVLWFlags(uint16 return (flags == VLW_STANDARD || flags == VLW_SHARED_ORDERS || flags == VLW_STATION_LIST || flags == VLW_DEPOT_LIST || flags == VLW_GROUP_LIST); } -void PlayerVehWndProc(Window *w, WindowEvent *e); +void PlayerVehWndProc(Window *w, struct WindowEvent *e); int DrawVehiclePurchaseInfo(int x, int y, uint w, EngineID engine_number); @@ -136,6 +130,29 @@ static inline WindowClass GetWindowClass /* Unified window procedure */ void ShowVehicleViewWindow(const Vehicle *v); -Vehicle *CheckClickOnVehicle(const ViewPort *vp, int x, int y); +Vehicle *CheckClickOnVehicle(const struct ViewPort *vp, int x, int y); + +typedef GUIList GUIVehicleList; + +struct vehiclelist_d { + GUIVehicleList vehicles; ///< The list of vehicles + Listing *sorting; ///< Pointer to the vehicle type related sorting. + VehicleType vehicle_type; ///< The vehicle type that is sorted +}; + +struct Sorting { + Listing aircraft; + Listing roadveh; + Listing ship; + Listing train; +}; + +extern Sorting _sorting; + +/* sorter stuff */ +void RebuildVehicleLists(); +void ResortVehicleLists(); +void SortVehicleList(vehiclelist_d *vl); +void BuildVehicleList(vehiclelist_d *vl, PlayerID owner, uint16 index, uint16 window_type); #endif /* VEHICLE_GUI_H */ diff --git a/src/window_gui.h b/src/window_gui.h --- a/src/window_gui.h +++ b/src/window_gui.h @@ -557,7 +557,6 @@ enum SortListFlags { VL_REBUILD = 1 << 2, ///< create sort-listing to use for qsort and friends VL_END = 1 << 3, }; - DECLARE_ENUM_AS_BIT_SET(SortListFlags); struct Listing { @@ -565,22 +564,14 @@ struct Listing { byte criteria; ///< Sorting criteria }; -struct list_d { +template +struct GUIList { + T* sort_list; ///< The items to sort. + SortListFlags flags; ///< used to control sorting/resorting/etc. uint16 list_length; ///< length of the list being sorted + uint16 resort_timer; ///< resort list after a given amount of ticks if set byte sort_type; ///< what criteria to sort on - SortListFlags flags; ///< used to control sorting/resorting/etc. - uint16 resort_timer; ///< resort list after a given amount of ticks if set }; -assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(list_d)); - -struct vehiclelist_d { - const Vehicle** sort_list; // List of vehicles (sorted) - Listing *_sorting; // pointer to the appropiate subcategory of _sorting - uint16 length_of_sort_list; // Keeps track of how many vehicle pointers sort list got space for - VehicleType vehicle_type; // The vehicle type that is sorted - list_d l; // General list struct -}; -assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(vehiclelist_d)); /****************** THESE ARE NOT WIDGET TYPES!!!!! *******************/ enum WindowWidgetBehaviours {