From 15ec6e26f0ee98a3c8f53cdd7b146e3708467d18 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 17 Mar 2013 15:41:40 +0000 Subject: [PATCH] (svn r25094) -Feature: Introduce dropdown for selecting the sort criterion in the town directory window (sbr) --- src/lang/english.txt | 2 +- src/script/api/game/game_window.hpp.sq | 4 +- src/script/api/script_window.hpp | 4 +- src/town_gui.cpp | 77 +++++++++++++++++--------- src/widgets/town_widget.h | 4 +- 5 files changed, 57 insertions(+), 34 deletions(-) diff --git a/src/lang/english.txt b/src/lang/english.txt index c1f34468d8..2558bb0164 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -256,7 +256,6 @@ STR_MEASURE_AREA_HEIGHTDIFF :{BLACK}Area: {N # 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 @@ -289,6 +288,7 @@ STR_SORT_BY_RUNNING_COST :Running Cost 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 --git a/src/script/api/game/game_window.hpp.sq b/src/script/api/game/game_window.hpp.sq index 057af86ed8..bed536259e 100644 --- a/src/script/api/game/game_window.hpp.sq +++ b/src/script/api/game/game_window.hpp.sq @@ -1163,8 +1163,8 @@ void SQGSWindow_Register(Squirrel *engine) 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 --git a/src/script/api/script_window.hpp b/src/script/api/script_window.hpp index a59013b36c..0972fee9c5 100644 --- a/src/script/api/script_window.hpp +++ b/src/script/api/script_window.hpp @@ -2342,8 +2342,8 @@ public: /* automatically generated from ../../widgets/town_widget.h */ /** 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 --git a/src/town_gui.cpp b/src/town_gui.cpp index 624cc74464..afafc1fa01 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -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 @@ static const NWidgetPart _nested_town_directory_widgets[] = { 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 @@ private: 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 @@ private: /* Always sort the towns. */ this->last_town = NULL; this->towns.Sort(); + this->SetWidgetDirty(WID_TD_LIST); // Force repaint of the displayed towns. } /** Sort by town name */ @@ -706,18 +710,22 @@ public: 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 +772,23 @@ public: 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 +824,13 @@ public: 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 +849,16 @@ public: } } + 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,7 +895,14 @@ public: Listing TownDirectoryWindow::last_sorting = {false, 0}; const Town *TownDirectoryWindow::last_town = NULL; -/* Available town directory sorting functions */ +/** 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, &TownPopulationSorter, diff --git a/src/widgets/town_widget.h b/src/widgets/town_widget.h index 2f2a97e6de..4f5443c36e 100644 --- a/src/widgets/town_widget.h +++ b/src/widgets/town_widget.h @@ -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.