|
1 | 1 | #include <iostream> |
2 | 2 | #include <string> |
3 | 3 |
|
| 4 | +#include <ncurses.h> |
| 5 | + |
4 | 6 | #include "system/reader.h" |
5 | 7 | #include "system/settings.h" |
| 8 | + |
6 | 9 | #include "theatre/management.h" |
7 | 10 |
|
| 11 | +#include "uistate/uistate.h" |
| 12 | + |
8 | 13 | namespace glight { |
9 | 14 |
|
| 15 | +using uistate::FaderControlType; |
| 16 | +using uistate::FaderSetState; |
| 17 | +using uistate::FaderState; |
| 18 | +using uistate::UIState; |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +struct TextFader { |
| 23 | + std::string label; |
| 24 | + theatre::SourceValue* source; |
| 25 | + size_t line; |
| 26 | +}; |
| 27 | + |
| 28 | +std::string Trim(const std::string& input, size_t width) { |
| 29 | + if (input.size() < width) return input; |
| 30 | + if (width > 5) { |
| 31 | + return input.substr(0, width - 2) + ".."; |
| 32 | + } else { |
| 33 | + return input.substr(0, width); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +std::vector<TextFader> InitializeFaders(const UIState& state) { |
| 38 | + std::vector<TextFader> result; |
| 39 | + size_t n_lines = 0; |
| 40 | + for (const std::unique_ptr<FaderSetState>& fader_set : state.FaderSets()) { |
| 41 | + mvprintw(n_lines, 0, fader_set->name.c_str()); // TODO trim |
| 42 | + ++n_lines; |
| 43 | + |
| 44 | + for (const std::unique_ptr<FaderState>& fader : fader_set->faders) { |
| 45 | + if (fader->GetFaderType() == FaderControlType::Fader) { |
| 46 | + theatre::SourceValue* source = nullptr; |
| 47 | + if (!fader->GetSourceValues().empty()) |
| 48 | + source = fader->GetSourceValues()[0]; |
| 49 | + if (source) { |
| 50 | + TextFader& new_fader = result.emplace_back(); |
| 51 | + new_fader.label = source->Name(); |
| 52 | + new_fader.source = source; |
| 53 | + new_fader.line = n_lines; |
| 54 | + ++n_lines; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return result; |
| 60 | +} |
| 61 | + |
| 62 | +void PrintState(const std::vector<TextFader>& faders, size_t width, |
| 63 | + size_t height) { |
| 64 | + for (const TextFader& fader : faders) { |
| 65 | + if (fader.line >= height) break; |
| 66 | + const theatre::ControlValue value = fader.source->A().Value(); |
| 67 | + const std::string check_box = value ? "[X] " : "[ ] "; |
| 68 | + mvprintw(fader.line, 1, (check_box + Trim(fader.label, width - 5)).c_str()); |
| 69 | + } |
| 70 | +} |
| 71 | + |
10 | 72 | void RunCli(const std::string filename) { |
11 | 73 | const glight::system::Settings settings = glight::system::LoadSettings(); |
12 | 74 | glight::theatre::Management management(settings); |
13 | | - glight::system::Read(filename, management); |
| 75 | + UIState state; |
| 76 | + glight::system::Read(filename, management, &state); |
14 | 77 | management.GetUniverses().Open(); |
15 | 78 | management.Run(); |
16 | | - std::cout << "Press enter to exit.\n"; |
17 | | - std::cin.get(); |
| 79 | + |
| 80 | + initscr(); |
| 81 | + |
| 82 | + int height; |
| 83 | + int width; |
| 84 | + getmaxyx(stdscr, height, width); |
| 85 | + keypad(stdscr, TRUE); |
| 86 | + noecho(); |
| 87 | + |
| 88 | + std::vector<TextFader> faders = InitializeFaders(state); |
| 89 | + PrintState(faders, width, height); |
| 90 | + |
| 91 | + size_t selected_fader = 0; |
| 92 | + bool running = true; |
| 93 | + do { |
| 94 | + move(faders[selected_fader].line, 2); |
| 95 | + const int c = getch(); |
| 96 | + switch (c) { |
| 97 | + case KEY_UP: |
| 98 | + if (selected_fader > 0) --selected_fader; |
| 99 | + break; |
| 100 | + case KEY_DOWN: |
| 101 | + if (selected_fader + 1 < faders.size()) ++selected_fader; |
| 102 | + break; |
| 103 | + case ' ': { |
| 104 | + TextFader& fader = faders[selected_fader]; |
| 105 | + theatre::ControlValue value = fader.source->A().Value(); |
| 106 | + if (value) { |
| 107 | + value = theatre::ControlValue::Zero(); |
| 108 | + printw(" "); |
| 109 | + } else { |
| 110 | + value = theatre::ControlValue::Max(); |
| 111 | + printw("X"); |
| 112 | + } |
| 113 | + fader.source->A().Set(value); |
| 114 | + } break; |
| 115 | + case 'q': |
| 116 | + running = false; |
| 117 | + break; |
| 118 | + } |
| 119 | + } while (running); |
| 120 | + |
| 121 | + endwin(); |
| 122 | + |
18 | 123 | management.BlackOut(false, 0.0f); |
19 | | - std::cout << "Stopping...\n"; |
20 | 124 | // There is some time required for the black out to take effect. |
21 | 125 | usleep(400000); |
22 | 126 | } |
23 | 127 |
|
| 128 | +} // namespace |
24 | 129 | } // namespace glight |
25 | 130 |
|
26 | 131 | int main(int argc, char* argv[]) { |
27 | 132 | if (argc <= 1) { |
28 | | - std::cout << "Syntax: glight-cli <show-file>\n\n" |
29 | | - "glight-cli can output a previously created gshow file " |
| 133 | + std::cout << "Syntax: glight-player <show-file>\n\n" |
| 134 | + "glight-player can play a previously created gshow file " |
30 | 135 | "without requiring a graphical desktop.\n"; |
31 | 136 | return 0; |
32 | 137 | } |
|
0 commit comments