Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/frontend/StarSongbookInterface.cpp
diff options
context:
space:
mode:
authorKae <80987908+Novaenia@users.noreply.github.com>2024-03-15 21:29:14 +1100
committerKae <80987908+Novaenia@users.noreply.github.com>2024-03-15 21:29:14 +1100
commit13f91aa1959ef18410ea5b8afdede13ba128bbdd (patch)
tree8e4be3db0845d5982a447c490f736ff2bca3cdc0 /source/frontend/StarSongbookInterface.cpp
parent6fa0afd758a6351873df813cd7e70b1904714ed6 (diff)
Songbook search improvements
Diffstat (limited to 'source/frontend/StarSongbookInterface.cpp')
-rw-r--r--source/frontend/StarSongbookInterface.cpp91
1 files changed, 50 insertions, 41 deletions
diff --git a/source/frontend/StarSongbookInterface.cpp b/source/frontend/StarSongbookInterface.cpp
index cc6a17d..28145b6 100644
--- a/source/frontend/StarSongbookInterface.cpp
+++ b/source/frontend/StarSongbookInterface.cpp
@@ -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();
-
- 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);
+ Root::singleton().registerReloadListener(
+ m_reloadListener = make_shared<CallbackListener>([this]() {
+ refresh(true);
+ })
+ );
- 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();
+ }
+ }
+ }
+ }
+}
+
} \ No newline at end of file