Skip to content
Permalink
Browse files

Add: Town name filtering

  • Loading branch information
telk5093 committed Nov 28, 2018
1 parent 16a36df commit 163fa52c4b370a9020f38b69d63f495172d38e7d
Showing with 41 additions and 3 deletions.
  1. +40 −3 src/town_gui.cpp
  2. +1 −0 src/widgets/town_widget.h
@@ -31,6 +31,7 @@
#include "townname_func.h"
#include "core/geometry_func.hpp"
#include "genworld.h"
#include "stringfilter_type.h"
#include "widgets/dropdown_func.h"

#include "widgets/town_widget.h"
@@ -618,7 +619,7 @@ static const NWidgetPart _nested_town_directory_widgets[] = {
NWidget(NWID_HORIZONTAL),
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(),
NWidget(WWT_EDITBOX, COLOUR_BROWN, WID_TD_FILTER), SetFill(35, 12), SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
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(),
@@ -644,6 +645,10 @@ struct TownDirectoryWindow : public Window {
static const StringID sorter_names[];
static GUITownList::SortFunction * const sorter_funcs[];

StringFilter string_filter; ///< Filter for towns
QueryString townname_editbox; ///< Filter editbox
SmallVector<bool, 32> town_items_shown; ///< Map of the filtered out town names

GUITownList towns;

Scrollbar *vscroll;
@@ -674,7 +679,7 @@ struct TownDirectoryWindow : public Window {
static char buf_cache[64];
const Town *ta = *a;
const Town *tb = *b;
char buf[64];
char buf[MAX_LENGTH_TOWN_NAME_CHARS];

SetDParam(0, ta->index);
GetString(buf, STR_TOWN_NAME, lastof(buf));
@@ -720,7 +725,7 @@ struct TownDirectoryWindow : public Window {
}

public:
TownDirectoryWindow(WindowDesc *desc) : Window(desc)
TownDirectoryWindow(WindowDesc *desc) : Window(desc), townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS)
{
this->CreateNestedTree();

@@ -732,6 +737,9 @@ struct TownDirectoryWindow : public Window {
this->BuildSortTownList();

this->FinishInitNested(0);

this->querystrings[WID_TD_FILTER] = &this->townname_editbox;
this->townname_editbox.cancel_button = QueryString::ACTION_CLEAR;
}

virtual void SetStringParameters(int widget) const
@@ -922,16 +930,45 @@ struct TownDirectoryWindow : public Window {
this->vscroll->SetCapacityFromWidget(this, WID_TD_LIST);
}

virtual void OnEditboxChanged(int wid)
{
this->string_filter.SetFilterTerm(this->townname_editbox.text.buf);
this->InvalidateData(1);
}

/**
* Some data on this window has become invalid.
* @param data Information about the changed data.
* @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
*/
virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
{
char buf[MAX_LENGTH_TOWN_NAME_CHARS];

if (data == 0) {
/* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
this->towns.ForceRebuild();
} else if (data == 1) {
if (this->string_filter.IsEmpty()) {
this->towns.ForceRebuild();
} else {
this->towns.Clear();

const Town *t;
FOR_ALL_TOWNS(t) {
this->string_filter.ResetState();

SetDParam(0, t->index);
GetString(buf, STR_TOWN_NAME, lastof(buf));

this->string_filter.AddLine(buf);
if(this->string_filter.GetState()) *this->towns.Append() = t;
}

this->towns.Compact();
this->towns.RebuildDone();
this->vscroll->SetCount(this->towns.Length()); // Update scrollbar as well.
}
} else {
this->towns.ForceResort();
}
@@ -16,6 +16,7 @@
enum TownDirectoryWidgets {
WID_TD_SORT_ORDER, ///< Direction of sort dropdown.
WID_TD_SORT_CRITERIA, ///< Criteria of sort dropdown.
WID_TD_FILTER, ///< Filter of name.
WID_TD_LIST, ///< List of towns.
WID_TD_SCROLLBAR, ///< Scrollbar for the town list.
WID_TD_WORLD_POPULATION, ///< The world's population.

0 comments on commit 163fa52

Please sign in to comment.
You can’t perform that action at this time.