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

summaryrefslogtreecommitdiff
path: root/source/frontend/StarSongbookInterface.cpp
blob: cc6a17da281b6daeaba57107ba526b23c8224770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#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);

  auto assets = Root::singleton().assets();

  GuiReader reader;

  reader.registerCallback("close", [=](Widget*) { dismiss(); });
  reader.registerCallback("btnPlay",
      [=](Widget*) {
        if (play())
          dismiss();
      });
  reader.registerCallback("group", [=](Widget*) {});
  reader.registerCallback("search", [=](Widget*) {});

  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);

      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();
      }
    }
  }
}

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;
}

}