osb/source/frontend/StarSongbookInterface.cpp

92 lines
2.6 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#include "StarSongbookInterface.hpp"
#include "StarGuiReader.hpp"
#include "StarRoot.hpp"
#include "StarListWidget.hpp"
#include "StarLabelWidget.hpp"
#include "StarTextBoxWidget.hpp"
#include "StarPlayer.hpp"
#include "StarAssets.hpp"
namespace Star {
SongbookInterface::SongbookInterface(PlayerPtr player) {
m_player = std::move(player);
2023-06-20 04:33:09 +00:00
auto assets = Root::singleton().assets();
GuiReader reader;
reader.registerCallback("close", [=](Widget*) { dismiss(); });
reader.registerCallback("btnPlay",
[=](Widget*) {
if (play())
dismiss();
});
reader.registerCallback("group", [=](Widget*) {});
2024-03-08 16:39:39 +00:00
reader.registerCallback("search", [=](Widget*) {});
2023-06-20 04:33:09 +00:00
reader.construct(assets->json("/interface/windowconfig/songbook.config:paneLayout"), this);
auto songList = fetchChild<ListWidget>("songs.list");
2024-03-08 16:39:39 +00:00
auto search = fetchChild<TextBoxWidget>("search")->getText();
2023-06-20 04:33:09 +00:00
2024-03-08 16:39:39 +00:00
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) {
2023-06-20 04:33:09 +00:00
auto song = s.substr(7, s.length() - (7 + 4));
2024-03-08 16:39:39 +00:00
if (song.contains(m_searchValue, String::CaseInsensitive)) {
auto widget = songList->addItem();
widget->setData(s);
auto songName = widget->fetchChild<LabelWidget>("songName");
songName->setText(song);
2023-06-20 04:33:09 +00:00
2024-03-08 16:39:39 +00:00
widget->show();
}
}
}
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();
}
}
2023-06-20 04:33:09 +00:00
}
}
bool SongbookInterface::play() {
auto songList = fetchChild<ListWidget>("songs.list");
auto songWidget = songList->selectedWidget();
if (!songWidget)
return false;
auto songName = songWidget->data().toString();
auto group = fetchChild<TextBoxWidget>("group")->getText();
JsonObject song;
song["resource"] = songName;
auto buffer = Root::singleton().assets()->bytes(songName);
song["abc"] = String(buffer->ptr(), buffer->size());
m_player->songbook()->play(song, group);
return true;
}
2024-03-08 16:39:39 +00:00
}