Skip to content

Commit

Permalink
Feature: text filter for the industry directory
Browse files Browse the repository at this point in the history
  • Loading branch information
perezdidac committed Apr 9, 2021
1 parent 2b86d42 commit 7096488
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/industry_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include "widgets/dropdown_type.h"
#include "widgets/industry_widget.h"
#include "clear_map.h"
#include "querystring_gui.h"
#include "stringfilter_type.h"

#include "table/strings.h"

Expand Down Expand Up @@ -1215,7 +1217,7 @@ static const NWidgetPart _nested_industry_directory_widgets[] = {
NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_ID_DROPDOWN_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_ID_FILTER_BY_ACC_CARGO), SetMinimalSize(225, 12), SetFill(0, 1), SetDataTip(STR_INDUSTRY_DIRECTORY_ACCEPTED_CARGO_FILTER, STR_TOOLTIP_FILTER_CRITERIA),
NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_ID_FILTER_BY_PROD_CARGO), SetMinimalSize(225, 12), SetFill(0, 1), SetDataTip(STR_INDUSTRY_DIRECTORY_PRODUCED_CARGO_FILTER, STR_TOOLTIP_FILTER_CRITERIA),
NWidget(WWT_PANEL, COLOUR_BROWN), SetResize(1, 0), EndContainer(),
NWidget(WWT_EDITBOX, COLOUR_BROWN, WID_ID_FILTER), SetMinimalSize(96, 0), SetFill(1, 0), SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
EndContainer(),
NWidget(WWT_PANEL, COLOUR_BROWN, WID_ID_INDUSTRY_LIST), SetDataTip(0x0, STR_INDUSTRY_DIRECTORY_LIST_CAPTION), SetResize(1, 1), SetScrollbar(WID_ID_SCROLLBAR), EndContainer(),
EndContainer(),
Expand Down Expand Up @@ -1310,6 +1312,10 @@ class IndustryDirectoryWindow : public Window {
byte produced_cargo_filter_criteria; ///< Selected produced cargo filter index
byte accepted_cargo_filter_criteria; ///< Selected accepted cargo filter index

const int MAX_FILTER_LENGTH = 64; ///< The max length of the filter, in bytes
StringFilter string_filter; ///< Filter for industries
QueryString industry_editbox; ///< Filter editbox

/**
* Set cargo filter list item index.
* @param index The index of the cargo to be set
Expand Down Expand Up @@ -1388,11 +1394,18 @@ class IndustryDirectoryWindow : public Window {
this->industries.clear();

for (const Industry *i : Industry::Iterate()) {
this->industries.push_back(i);
if (this->string_filter.IsEmpty()) {
this->industries.push_back(i);
continue;
}
this->string_filter.ResetState();
this->string_filter.AddLine(i->GetCachedName());
if (this->string_filter.GetState()) this->industries.push_back(i);
}

this->industries.shrink_to_fit();
this->industries.RebuildDone();
this->vscroll->SetCount((uint)this->industries.size()); // Update scrollbar as well.
}

auto filter = std::make_pair(this->cargo_filter[this->accepted_cargo_filter_criteria],
Expand All @@ -1401,8 +1414,6 @@ class IndustryDirectoryWindow : public Window {
this->industries.Filter(filter);
this->industries.Sort();

this->vscroll->SetCount((uint)this->industries.size()); // Update scrollbar as well.

this->SetDirty();
}

Expand Down Expand Up @@ -1544,7 +1555,7 @@ class IndustryDirectoryWindow : public Window {
}

public:
IndustryDirectoryWindow(WindowDesc *desc, WindowNumber number) : Window(desc)
IndustryDirectoryWindow(WindowDesc *desc, WindowNumber number) : Window(desc), industry_editbox(MAX_FILTER_LENGTH)
{
this->CreateNestedTree();
this->vscroll = this->GetScrollbar(WID_ID_SCROLLBAR);
Expand All @@ -1555,6 +1566,9 @@ class IndustryDirectoryWindow : public Window {
this->BuildSortIndustriesList();

this->FinishInitNested(0);

this->querystrings[WID_ID_FILTER] = &this->industry_editbox;
this->industry_editbox.cancel_button = QueryString::ACTION_CLEAR;
}

~IndustryDirectoryWindow()
Expand Down Expand Up @@ -1720,6 +1734,14 @@ class IndustryDirectoryWindow : public Window {
this->vscroll->SetCapacityFromWidget(this, WID_ID_INDUSTRY_LIST);
}

void OnEditboxChanged(int wid) override
{
if (wid == WID_ID_FILTER) {
this->string_filter.SetFilterTerm(this->industry_editbox.text.buf);
this->InvalidateData(IDIWD_FORCE_REBUILD);
}
}

void OnPaint() override
{
if (this->industries.NeedRebuild()) this->BuildSortIndustriesList();
Expand Down
1 change: 1 addition & 0 deletions src/widgets/industry_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ enum IndustryDirectoryWidgets {
WID_ID_DROPDOWN_CRITERIA, ///< Dropdown for the criteria of the sort.
WID_ID_FILTER_BY_ACC_CARGO, ///< Accepted cargo filter dropdown list.
WID_ID_FILTER_BY_PROD_CARGO, ///< Produced cargo filter dropdown list.
WID_ID_FILTER, ///< Filter of name.
WID_ID_INDUSTRY_LIST, ///< Industry list.
WID_ID_SCROLLBAR, ///< Scrollbar of the list.
};
Expand Down

0 comments on commit 7096488

Please sign in to comment.