# HG changeset patch # Parent 17aa310af0f6f5d30a622c6401967b1aaf351bb4 # User sbr # Date 1346577585 -7200 Use a 'sort order' button and a 'sort criteria' dropdown to sort towns in the TownDirectoryWindow. diff -r 17aa310af0f6 src/lang/english.txt --- a/src/lang/english.txt Fri Sep 21 21:00:32 2012 +0000 +++ b/src/lang/english.txt Sat Sep 22 11:23:46 2012 +0200 @@ -255,7 +255,6 @@ # These are used in buttons -STR_SORT_BY_CAPTION_POPULATION :{BLACK}Population STR_SORT_BY_CAPTION_NAME :{BLACK}Name STR_SORT_BY_CAPTION_DATE :{BLACK}Date # These are used in dropdowns @@ -288,6 +287,7 @@ STR_SORT_BY_POWER_VS_RUNNING_COST :Power/Running Cost STR_SORT_BY_CARGO_CAPACITY :Cargo Capacity STR_SORT_BY_RANGE :Range +STR_SORT_BY_POPULATION :Population # Tooltips for the main toolbar STR_TOOLBAR_TOOLTIP_PAUSE_GAME :{BLACK}Pause game diff -r 17aa310af0f6 src/script/api/game/game_window.hpp.sq --- a/src/script/api/game/game_window.hpp.sq Fri Sep 21 21:00:32 2012 +0000 +++ b/src/script/api/game/game_window.hpp.sq Sat Sep 22 11:23:46 2012 +0200 @@ -1165,8 +1165,8 @@ SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TE_MUSIC_SOUND, "WID_TE_MUSIC_SOUND"); SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TE_HELP, "WID_TE_HELP"); SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TE_SWITCH_BAR, "WID_TE_SWITCH_BAR"); - SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TD_SORT_NAME, "WID_TD_SORT_NAME"); - SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TD_SORT_POPULATION, "WID_TD_SORT_POPULATION"); + SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TD_SORT_ORDER, "WID_TD_SORT_ORDER"); + SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TD_SORT_CRITERIA, "WID_TD_SORT_CRITERIA"); SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TD_LIST, "WID_TD_LIST"); SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TD_SCROLLBAR, "WID_TD_SCROLLBAR"); SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TD_WORLD_POPULATION, "WID_TD_WORLD_POPULATION"); diff -r 17aa310af0f6 src/script/api/script_window.hpp --- a/src/script/api/script_window.hpp Fri Sep 21 21:00:32 2012 +0000 +++ b/src/script/api/script_window.hpp Sat Sep 22 11:23:46 2012 +0200 @@ -2224,8 +2224,8 @@ /** Widgets of the #TownDirectoryWindow class. */ enum TownDirectoryWidgets { - WID_TD_SORT_NAME = ::WID_TD_SORT_NAME, ///< Sort by town name. - WID_TD_SORT_POPULATION = ::WID_TD_SORT_POPULATION, ///< Sort by town population. + WID_TD_SORT_ORDER = ::WID_TD_SORT_ORDER, ///< Direction of sort dropdown. + WID_TD_SORT_CRITERIA = ::WID_TD_SORT_CRITERIA, ///< Criteria of sort dropdown. WID_TD_LIST = ::WID_TD_LIST, ///< List of towns. WID_TD_SCROLLBAR = ::WID_TD_SCROLLBAR, ///< Scrollbar for the town list. WID_TD_WORLD_POPULATION = ::WID_TD_WORLD_POPULATION, ///< The world's population. diff -r 17aa310af0f6 src/town_gui.cpp --- a/src/town_gui.cpp Fri Sep 21 21:00:32 2012 +0000 +++ b/src/town_gui.cpp Sat Sep 22 11:23:46 2012 +0200 @@ -31,6 +31,7 @@ #include "townname_func.h" #include "core/geometry_func.hpp" #include "genworld.h" +#include "widgets/dropdown_func.h" #include "widgets/town_widget.h" @@ -606,8 +607,9 @@ NWidget(NWID_HORIZONTAL), NWidget(NWID_VERTICAL), NWidget(NWID_HORIZONTAL), - NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TD_SORT_NAME), SetMinimalSize(99, 12), SetDataTip(STR_SORT_BY_CAPTION_NAME, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), - NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TD_SORT_POPULATION), SetMinimalSize(97, 12), SetDataTip(STR_SORT_BY_CAPTION_POPULATION, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), + NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TD_SORT_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER), + NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_TD_SORT_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA), + NWidget(WWT_PANEL, COLOUR_BROWN), SetResize(1, 0), EndContainer(), EndContainer(), NWidget(WWT_PANEL, COLOUR_BROWN, WID_TD_LIST), SetMinimalSize(196, 0), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP), SetFill(1, 0), SetResize(0, 10), SetScrollbar(WID_TD_SCROLLBAR), EndContainer(), @@ -630,6 +632,7 @@ static const Town *last_town; /* Constants for sorting towns */ + static const StringID sorter_names[]; static GUITownList::SortFunction * const sorter_funcs[]; GUITownList towns; @@ -653,6 +656,7 @@ /* Always sort the towns. */ this->last_town = NULL; this->towns.Sort(); + this->SetWidgetDirty(WID_TD_LIST); // Set the modified widget dirty } /** Sort by town name */ @@ -706,18 +710,17 @@ virtual void SetStringParameters(int widget) const { - if (widget == WID_TD_WORLD_POPULATION) SetDParam(0, GetWorldPopulation()); + switch (widget) { + case WID_TD_WORLD_POPULATION: SetDParam(0, GetWorldPopulation()); break; + case WID_TD_SORT_CRITERIA: SetDParam(0, TownDirectoryWindow::sorter_names[this->towns.SortType()]); break; + } } virtual void DrawWidget(const Rect &r, int widget) const { switch (widget) { - case WID_TD_SORT_NAME: - if (this->towns.SortType() == 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP); - break; - - case WID_TD_SORT_POPULATION: - if (this->towns.SortType() != 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP); + case WID_TD_SORT_ORDER: + this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP); break; case WID_TD_LIST: { @@ -764,14 +767,23 @@ virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) { switch (widget) { - case WID_TD_SORT_NAME: - case WID_TD_SORT_POPULATION: { + case WID_TD_SORT_ORDER: { Dimension d = GetStringBoundingBox(this->GetWidget(widget)->widget_data); d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better. d.height += padding.height; *size = maxdim(*size, d); break; } + case WID_TD_SORT_CRITERIA: { + Dimension d = {0, 0}; + for (uint i = 0; TownDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) { + d = maxdim(d, GetStringBoundingBox(TownDirectoryWindow::sorter_names[i])); + } + d.width += padding.width; + d.height += padding.height; + *size = maxdim(*size, d); + break; + } case WID_TD_LIST: { Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE); for (uint i = 0; i < this->towns.Length(); i++) { @@ -807,24 +819,13 @@ virtual void OnClick(Point pt, int widget, int click_count) { switch (widget) { - case WID_TD_SORT_NAME: // Sort by Name ascending/descending - if (this->towns.SortType() == 0) { - this->towns.ToggleSortOrder(); - } else { - this->towns.SetSortType(0); - } - this->BuildSortTownList(); + case WID_TD_SORT_ORDER: // Click on sort order button + this->towns.ToggleSortOrder(); this->SetDirty(); break; - case WID_TD_SORT_POPULATION: // Sort by Population ascending/descending - if (this->towns.SortType() == 1) { - this->towns.ToggleSortOrder(); - } else { - this->towns.SetSortType(1); - } - this->BuildSortTownList(); - this->SetDirty(); + case WID_TD_SORT_CRITERIA: // Click on sort criteria dropdown + ShowDropDownMenu(this, TownDirectoryWindow::sorter_names, this->towns.SortType(), WID_TD_SORT_CRITERIA, 0, 0); break; case WID_TD_LIST: { // Click on Town Matrix @@ -843,6 +844,16 @@ } } + virtual void OnDropdownSelect(int widget, int index) + { + if (widget != WID_TD_SORT_CRITERIA) return; + + if (this->towns.SortType() != index) { + this->towns.SetSortType(index); + this->BuildSortTownList(); + } + } + virtual void OnPaint() { if (this->towns.NeedRebuild()) this->BuildSortTownList(); @@ -879,6 +890,13 @@ Listing TownDirectoryWindow::last_sorting = {false, 0}; const Town *TownDirectoryWindow::last_town = NULL; +/* Names of the sorting functions */ +const StringID TownDirectoryWindow::sorter_names[] = { + STR_SORT_BY_NAME, + STR_SORT_BY_POPULATION, + INVALID_STRING_ID +}; + /* Available town directory sorting functions */ GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = { &TownNameSorter, diff -r 17aa310af0f6 src/widgets/town_widget.h --- a/src/widgets/town_widget.h Fri Sep 21 21:00:32 2012 +0000 +++ b/src/widgets/town_widget.h Sat Sep 22 11:23:46 2012 +0200 @@ -14,8 +14,8 @@ /** Widgets of the #TownDirectoryWindow class. */ enum TownDirectoryWidgets { - WID_TD_SORT_NAME, ///< Sort by town name. - WID_TD_SORT_POPULATION, ///< Sort by town population. + WID_TD_SORT_ORDER, ///< Direction of sort dropdown. + WID_TD_SORT_CRITERIA, ///< Criteria of sort dropdown. WID_TD_LIST, ///< List of towns. WID_TD_SCROLLBAR, ///< Scrollbar for the town list. WID_TD_WORLD_POPULATION, ///< The world's population.