(svn r13269) -Codechange: build bridge window uses GUILists Sort() now

-Cleanup: make some globals part of the build bridge window
pull/155/head
skidd13 16 years ago
parent f202f1dda6
commit c3dd9d864e

@ -22,9 +22,6 @@
#include "table/strings.h" #include "table/strings.h"
/* Save the sorting during runtime */
static Listing _bridge_sorting = {false, 0};
/** /**
* Carriage for the data we need if we want to build a bridge * Carriage for the data we need if we want to build a bridge
*/ */
@ -36,53 +33,6 @@ struct BuildBridgeData {
typedef GUIList<BuildBridgeData> GUIBridgeList; typedef GUIList<BuildBridgeData> GUIBridgeList;
/** Sort the bridges by their index */
static int CDECL BridgeIndexSorter(const void *a, const void *b)
{
const BuildBridgeData* ba = (BuildBridgeData*)a;
const BuildBridgeData* bb = (BuildBridgeData*)b;
int r = ba->index - bb->index;
return (_bridge_sorting.order) ? -r : r;
}
/** Sort the bridges by their price */
static int CDECL BridgePriceSorter(const void *a, const void *b)
{
const BuildBridgeData* ba = (BuildBridgeData*)a;
const BuildBridgeData* bb = (BuildBridgeData*)b;
int r = ba->cost - bb->cost;
return (_bridge_sorting.order) ? -r : r;
}
/** Sort the bridges by their maximum speed */
static int CDECL BridgeSpeedSorter(const void *a, const void *b)
{
const BuildBridgeData* ba = (BuildBridgeData*)a;
const BuildBridgeData* bb = (BuildBridgeData*)b;
int r = ba->spec->speed - bb->spec->speed;
return (_bridge_sorting.order) ? -r : r;
}
typedef int CDECL BridgeSortListingTypeFunction(const void*, const void*);
/* Availible bridge sorting functions */
static BridgeSortListingTypeFunction* const _bridge_sorter[] = {
&BridgeIndexSorter,
&BridgePriceSorter,
&BridgeSpeedSorter
};
/* Names of the sorting functions */
static const StringID _bridge_sort_listing[] = {
STR_SORT_BY_NUMBER,
STR_ENGINE_SORT_COST,
STR_SORT_BY_MAX_SPEED,
INVALID_STRING_ID
};
/** /**
* Callback executed after a build Bridge CMD has been called * Callback executed after a build Bridge CMD has been called
* *
@ -109,15 +59,38 @@ enum BuildBridgeSelectionWidgets {
class BuildBridgeWindow : public Window { class BuildBridgeWindow : public Window {
private: private:
/* The last size of the build bridge window /* Runtime saved values */
* is saved during runtime */
static uint last_size; static uint last_size;
static Listing last_sorting;
/* Constants for sorting the bridges */
static const StringID sorter_names[];
static const GUIBridgeList::SortFunction *const sorter_funcs[];
/* Internal variables */
TileIndex start_tile; TileIndex start_tile;
TileIndex end_tile; TileIndex end_tile;
uint32 type; uint32 type;
GUIBridgeList *bridges; GUIBridgeList *bridges;
/** Sort the bridges by their index */
static int BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b)
{
return a->index - b->index;
}
/** Sort the bridges by their price */
static int BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b)
{
return a->cost - b->cost;
}
/** Sort the bridges by their maximum speed */
static int BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b)
{
return a->spec->speed - b->spec->speed;
}
void BuildBridge(uint8 i) void BuildBridge(uint8 i)
{ {
DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->Get(i)->index, DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->Get(i)->index,
@ -127,15 +100,10 @@ private:
/** Sort the builable bridges */ /** Sort the builable bridges */
void SortBridgeList() void SortBridgeList()
{ {
/* Skip sorting if resort bit is not set */ this->bridges->Sort();
if (!(bridges->flags & VL_RESORT)) return;
qsort(this->bridges->Begin(), this->bridges->Length(), sizeof(this->bridges->Begin()), _bridge_sorter[_bridge_sorting.criteria]);
/* Display the current sort variant */ /* Display the current sort variant */
this->widget[BBSW_DROPDOWN_CRITERIA].data = _bridge_sort_listing[this->bridges->sort_type]; this->widget[BBSW_DROPDOWN_CRITERIA].data = this->sorter_names[this->bridges->SortType()];
bridges->flags &= ~VL_RESORT;
/* Set the modified widgets dirty */ /* Set the modified widgets dirty */
this->InvalidateWidget(BBSW_DROPDOWN_CRITERIA); this->InvalidateWidget(BBSW_DROPDOWN_CRITERIA);
@ -149,6 +117,8 @@ public:
type(br_type), type(br_type),
bridges(bl) bridges(bl)
{ {
this->bridges->SetListing(this->last_sorting);
this->bridges->SetSortFuncs(this->sorter_funcs);
this->SortBridgeList(); this->SortBridgeList();
/* Change the data, or the caption of the gui. Set it to road or rail, accordingly */ /* Change the data, or the caption of the gui. Set it to road or rail, accordingly */
@ -171,6 +141,8 @@ public:
~BuildBridgeWindow() ~BuildBridgeWindow()
{ {
this->last_sorting = this->bridges->GetListing();
delete bridges; delete bridges;
} }
@ -224,27 +196,21 @@ public:
} break; } break;
case BBSW_DROPDOWN_ORDER: case BBSW_DROPDOWN_ORDER:
/* Revers the sort order */ this->bridges->ToggleSortOrder();
this->bridges->flags ^= VL_DESC; this->SetDirty();
_bridge_sorting.order = !_bridge_sorting.order;
this->bridges->flags |= VL_RESORT;
this->SortBridgeList();
break; break;
case BBSW_DROPDOWN_CRITERIA: case BBSW_DROPDOWN_CRITERIA:
ShowDropDownMenu(this, _bridge_sort_listing, bridges->sort_type, BBSW_DROPDOWN_CRITERIA, 0, 0); ShowDropDownMenu(this, this->sorter_names, this->bridges->SortType(), BBSW_DROPDOWN_CRITERIA, 0, 0);
break; break;
} }
} }
virtual void OnDropdownSelect(int widget, int index) virtual void OnDropdownSelect(int widget, int index)
{ {
if (widget == BBSW_DROPDOWN_CRITERIA && this->bridges->sort_type != index) { if (widget == BBSW_DROPDOWN_CRITERIA && this->bridges->SortType() != index) {
this->bridges->sort_type = index; this->bridges->SetSortType(index);
_bridge_sorting.criteria = index;
this->bridges->flags |= VL_RESORT;
this->SortBridgeList(); this->SortBridgeList();
} }
} }
@ -261,6 +227,23 @@ public:
/* Set the default size of the Build Bridge Window */ /* Set the default size of the Build Bridge Window */
uint BuildBridgeWindow::last_size = 4; uint BuildBridgeWindow::last_size = 4;
/* Set the default sorting for the bridges */
Listing BuildBridgeWindow::last_sorting = {false, 0};
/* Availible bridge sorting functions */
GUIBridgeList::SortFunction* const BuildBridgeWindow::sorter_funcs[] = {
&BridgeIndexSorter,
&BridgePriceSorter,
&BridgeSpeedSorter
};
/* Names of the sorting functions */
const StringID BuildBridgeWindow::sorter_names[] = {
STR_SORT_BY_NUMBER,
STR_ENGINE_SORT_COST,
STR_SORT_BY_MAX_SPEED,
INVALID_STRING_ID
};
/* Widget definition for the rail bridge selection window */ /* Widget definition for the rail bridge selection window */
static const Widget _build_bridge_widgets[] = { static const Widget _build_bridge_widgets[] = {

Loading…
Cancel
Save