Skip to content

Commit

Permalink
Add basic search for Artist, Title, Genre
Browse files Browse the repository at this point in the history
  • Loading branch information
samsta committed Jul 12, 2022
1 parent f7ce8d5 commit 40a0582
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
19 changes: 19 additions & 0 deletions Browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ def __init__(self, browser, log):
self._bpm_tolerance_percent = 5.0
self._filter_by_bpm = True
self._filter_by_key = True
self._filter_artist = ""
self._filter_title = ""
self._filter_genre = ""
self._start_ui()
self._apply_filter()
self._update()
Expand Down Expand Up @@ -259,6 +262,13 @@ def tempo(self, bpm):
self._update()

def _filter(self, item):
if self._filter_artist != "" and not self._filter_artist in item.artist.lower():
return False
if self._filter_title != "" and not self._filter_title in item.title.lower():
return False
if self._filter_genre != "" and not self._filter_genre in item.genre.lower():
return False

return (not self._filter_by_bpm or (item.bpm > self._bpm_lower and item.bpm < self._bpm_upper)) and \
(not self._filter_by_key or item.keydistance < 4)

Expand Down Expand Up @@ -380,6 +390,15 @@ def poll(self):
self.set_current_index(data["load_ix"])
self.load()
self._update()
elif "filter_artist" in data:
self._filter_artist = data["filter_artist"].lower()
filter_changed = filter_changed or True
elif "filter_title" in data:
self._filter_title = data["filter_title"].lower()
filter_changed = filter_changed or True
elif "filter_genre" in data:
self._filter_genre = data["filter_genre"].lower()
filter_changed = filter_changed or True

if filter_changed:
self._apply_filter()
Expand Down
41 changes: 40 additions & 1 deletion src/LiveMusicBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ static bool filter_bpm = false;
static bool filter_key = false;
static float bpm_percentage = 5;

static std::map<std::string, std::string> filters;

void drawFilters(const json11::Json& data, json11::Json& send_data)
{
filter_key = data["key_filter"].bool_value();
Expand Down Expand Up @@ -112,9 +114,46 @@ void drawBrowserList(const json11::Json& data, json11::Json& send_data)
continue;
}
ImGui::TableSetupColumn(col_name.string_value().c_str());

col_ix++;
}
// Instead of calling TableHeadersRow() we'll submit custom headers ourselves
ImGui::TableNextRow(ImGuiTableRowFlags_Headers);
col_ix = 0;
for (const auto& col_name: data["cols"].array_items())
{
if (col_name.string_value() == "KeyDistance")
{
continue;
}

ImGui::TableSetColumnIndex(col_ix);
ImGui::TableHeader(col_name.string_value().c_str());
if (col_name.string_value() == "Artist" or
col_name.string_value() == "Title" or
col_name.string_value() == "Genre")
{
ImGui::PushID(col_ix);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
char filter[128] = "";
strncpy(filter, filters[col_name.string_value()].c_str(), sizeof(filter));
ImGui::InputTextWithHint(
(std::string("##search") + col_name.string_value()).c_str(),
"search",
filter, IM_ARRAYSIZE(filter));

if (filters[col_name.string_value()] != filter)
{
std::string col_name_lower = col_name.string_value();
col_name_lower[0] = std::tolower(col_name_lower[0]);
send_data = json11::Json::object{{std::string("filter_") + col_name_lower, json11::Json(filter)}};
filters[col_name.string_value()] = filter;
}
ImGui::PopStyleVar();
ImGui::PopID();
}
col_ix++;
}
ImGui::TableHeadersRow();

ImGui::PushStyleColor(ImGuiCol_Header, ROW_HIGHLIGHT_COLOR);

Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsClassic();

// Setup Platform/Renderer backends
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);

Expand Down

0 comments on commit 40a0582

Please sign in to comment.