| 1 |
/* |
| 2 |
* VirtualInstrument.h |
| 3 |
* |
| 4 |
* Created on: 13 Jan 2019 |
| 5 |
* Author: jeremy |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef EXADRUMS_SOURCE_WIDGETS_VIRTUALPAD_H |
| 9 |
#define EXADRUMS_SOURCE_WIDGETS_VIRTUALPAD_H |
| 10 |
|
| 11 |
#include <gtkmm/grid.h> |
| 12 |
#include <gtkmm/button.h> |
| 13 |
#include <gtkmm/entry.h> |
| 14 |
#include <gtkmm/label.h> |
| 15 |
|
| 16 |
#include <string> |
| 17 |
#include <functional> |
| 18 |
#include <algorithm> |
| 19 |
#include <memory> |
| 20 |
|
| 21 |
namespace Widgets |
| 22 |
{ |
| 23 |
|
| 24 |
class VirtualPad : public Gtk::Grid |
| 25 |
{ |
| 26 |
|
| 27 |
public: |
| 28 |
VirtualPad(std::string const& name, std::vector<std::function<void()>>&& soundsVec) |
| 29 |
{ |
| 30 |
|
| 31 |
// Register functions that play sounds |
| 32 |
for(auto&& f : soundsVec) |
| 33 |
{ |
| 34 |
sounds.emplace_back(std::move(f)); |
| 35 |
} |
| 36 |
|
| 37 |
const size_t nbTrigs = sounds.size(); |
| 38 |
|
| 39 |
// Prepare keys vector |
| 40 |
keys.resize(nbTrigs); |
| 41 |
|
| 42 |
|
| 43 |
// Create buttons and entries |
| 44 |
for(size_t i = 0; i < nbTrigs; ++i) |
| 45 |
{ |
| 46 |
// Add button to vector |
| 47 |
if(nbTrigs > 1) |
| 48 |
{ |
| 49 |
buttons.emplace_back(name + " (Trig." + std::to_string(i) + ")"); |
| 50 |
} |
| 51 |
else |
| 52 |
{ |
| 53 |
buttons.emplace_back(name); |
| 54 |
} |
| 55 |
|
| 56 |
// Connect button signal |
| 57 |
buttons.back().signal_clicked().connect(sounds[i], false); |
| 58 |
|
| 59 |
// Add entry to vector |
| 60 |
shortcuts.emplace_back(); |
| 61 |
|
| 62 |
// Connect entry signal |
| 63 |
shortcuts.back().signal_changed().connect([&, i]{ keys[i] = shortcuts[i].get_text(); }); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
// Attach buttons and entries |
| 68 |
for(size_t i = 0; i < buttons.size(); ++i) |
| 69 |
{ |
| 70 |
buttons[i].set_hexpand(); |
| 71 |
shortcuts[i].set_tooltip_text("Keyboard shortcuts (2 max) for this trigger."); |
| 72 |
shortcuts[i].set_max_width_chars(3); |
| 73 |
shortcuts[i].set_max_length(2); |
| 74 |
shortcuts[i].set_width_chars(3); |
| 75 |
|
| 76 |
if(i == 0) |
| 77 |
{ |
| 78 |
this->attach(buttons[i], 0, 0, 1, 1); |
| 79 |
this->attach_next_to(shortcuts[i], buttons[i], Gtk::PositionType::POS_RIGHT, 1, 1); |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
this->attach_next_to(buttons[i], buttons[i-1], Gtk::PositionType::POS_BOTTOM, 1, 1); |
| 84 |
this->attach_next_to(shortcuts[i], buttons[i], Gtk::PositionType::POS_RIGHT, 1, 1); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
this->set_halign(Gtk::Align::ALIGN_FILL); |
| 89 |
this->show_all(); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
virtual ~VirtualPad() = default; |
| 94 |
|
| 95 |
void PlaySound(char k) const |
| 96 |
{ |
| 97 |
auto kIt = std::find_if(keys.begin(), keys.end(), [&] (const auto& key) { return key.find(k) != std::string::npos; }); |
| 98 |
|
| 99 |
if(kIt != keys.end()) |
| 100 |
{ |
| 101 |
const size_t index = kIt - keys.begin(); |
| 102 |
if(sounds[index]) |
| 103 |
{ |
| 104 |
sounds[index](); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
private: |
| 112 |
|
| 113 |
std::vector<std::function<void()>> sounds; |
| 114 |
std::vector<Gtk::Button> buttons; |
| 115 |
std::vector<Gtk::Entry> shortcuts; |
| 116 |
std::vector<std::string> keys; |
| 117 |
|
| 118 |
}; |
| 119 |
|
| 120 |
using VirtualPadPtr = std::unique_ptr<VirtualPad>; |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
#endif /* EXADRUMS_SOURCE_WIDGETS_VIRTUALPAD_H */ |