Songbook search improvements

This commit is contained in:
Kae 2024-03-15 21:29:14 +11:00
parent 6fa0afd758
commit 13f91aa195
3 changed files with 73 additions and 62 deletions

View File

@ -9,7 +9,6 @@
"hint" : "Search",
"maxWidth" : 50
},
"lblBandInput" : {
"position" : [3, 68]
},

View File

@ -27,57 +27,25 @@ SongbookInterface::SongbookInterface(PlayerPtr player) {
reader.construct(assets->json("/interface/windowconfig/songbook.config:paneLayout"), this);
auto songList = fetchChild<ListWidget>("songs.list");
auto search = fetchChild<TextBoxWidget>("search")->getText();
Root::singleton().registerReloadListener(
m_reloadListener = make_shared<CallbackListener>([this]() {
refresh(true);
})
);
if (m_searchValue != search)
m_searchValue = search;
m_files = assets->scan(".abc");
sort(m_files, [](String const& a, String const& b) -> bool { return b.compare(a, String::CaseInsensitive) > 0; });
for (auto s : m_files) {
auto song = s.substr(7, s.length() - (7 + 4));
if (song.contains(m_searchValue, String::CaseInsensitive)) {
auto widget = songList->addItem();
widget->setData(s);
auto songName = widget->fetchChild<LabelWidget>("songName");
songName->setText(song);
widget->show();
}
}
refresh(true);
}
void SongbookInterface::update(float dt) {
Pane::update(dt);
auto search = fetchChild<TextBoxWidget>("search")->getText();
if (m_searchValue != search) {
m_searchValue = search;
auto songList = fetchChild<ListWidget>("songs.list");
songList->clear();
for (auto s : m_files) {
auto song = s.substr(7, s.length() - (7 + 4));
if (song.contains(m_searchValue, String::CaseInsensitive)) {
auto widget = songList->addItem();
widget->setData(s);
auto songName = widget->fetchChild<LabelWidget>("songName");
songName->setText(song);
widget->show();
}
}
}
refresh();
}
bool SongbookInterface::play() {
auto songList = fetchChild<ListWidget>("songs.list");
auto songWidget = songList->selectedWidget();
if (!songWidget)
return false;
auto songName = songWidget->data().toString();
if (!songWidget) return false;
auto& songName = m_files.at(songWidget->data().toUInt());
auto group = fetchChild<TextBoxWidget>("group")->getText();
JsonObject song;
@ -89,4 +57,45 @@ bool SongbookInterface::play() {
return true;
}
void SongbookInterface::refresh(bool reloadFiles) {
if (reloadFiles) {
m_files = Root::singleton().assets()->scanExtension(".abc").values();
sort(m_files, [](String const& a, String const& b) -> bool { return b.compare(a, String::CaseInsensitive) > 0; });
}
auto search = fetchChild<TextBoxWidget>("search")->getText();
if (m_lastSearch != search || reloadFiles) {
m_lastSearch = search;
auto songList = fetchChild<ListWidget>("songs.list");
songList->clear();
if (search.empty()) {
for (size_t i = 0; i != m_files.size(); ++i) {
auto widget = songList->addItem();
widget->setData(i);
auto songName = widget->fetchChild<LabelWidget>("songName");
songName->setText(m_files[i]);
widget->show();
}
} else {
for (size_t i = 0; i != m_files.size(); ++i) {
StringView song = m_files[i];
auto find = song.find(search, 0, String::CaseInsensitive);
if (find != NPos) {
auto widget = songList->addItem();
widget->setData(i);
String text = "";
size_t last = 0;
do {
text += strf("^#bbb;{}^#7f7;{}", song.substr(last, find - last), song.substr(find, search.size()));
last = find + search.size();
find = song.find(search, last, String::CaseInsensitive);
} while (find != NPos);
auto songName = widget->fetchChild<LabelWidget>("songName");
songName->setText(text + strf("^#bbb;{}", song.substr(last)));
widget->show();
}
}
}
}
}
}

View File

@ -2,6 +2,7 @@
#include "StarSongbook.hpp"
#include "StarPane.hpp"
#include "StarListener.hpp"
namespace Star {
@ -18,8 +19,10 @@ public:
private:
PlayerPtr m_player;
StringList m_files;
String m_searchValue;
String m_lastSearch;
CallbackListenerPtr m_reloadListener;
bool play();
void refresh(bool reloadFiles = false);
};
}