| 1 |
/* |
| 2 |
* MetronomeController.cpp |
| 3 |
* |
| 4 |
* Created on: 2 Oct 2016 |
| 5 |
* Author: jeremy |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "MetronomeController.h" |
| 9 |
|
| 10 |
#include <algorithm> |
| 11 |
#include <iterator> |
| 12 |
|
| 13 |
using namespace eXaDrumsApi; |
| 14 |
|
| 15 |
namespace Controllers |
| 16 |
{ |
| 17 |
|
| 18 |
MetronomeController::MetronomeController(Glib::RefPtr<Gtk::Builder> builder, std::shared_ptr<eXaDrums> drumKit) |
| 19 |
: builder(builder), drumKit(drumKit) |
| 20 |
{ |
| 21 |
|
| 22 |
// Get all widgets |
| 23 |
{ |
| 24 |
this->builder->get_widget("MetronomeConfig", metronomeWindow); |
| 25 |
this->builder->get_widget("MetronomeConfigSave", metronomeConfigSave); |
| 26 |
this->builder->get_widget("MetronomeSoundType", clickTypes); |
| 27 |
this->builder->get_widget("ClickTempoScale", clickTempoScale); |
| 28 |
this->builder->get_widget("ClickVolumeScale", clickVolumeScale); |
| 29 |
this->builder->get_widget("EnableClickButton", enableClickButton); |
| 30 |
this->builder->get_widget("RhythmList", rhythmList); |
| 31 |
this->builder->get_widget("BpmeasList", bpmeasList); |
| 32 |
} |
| 33 |
|
| 34 |
// Connect all signals |
| 35 |
{ |
| 36 |
metronomeConfigSave->signal_clicked().connect([&] { SaveMetronomeConfig(); }); |
| 37 |
clickTempoScale->signal_value_changed().connect([&] { ChangeTempo(); }); |
| 38 |
clickVolumeScale->signal_value_changed().connect([&] { ChangeClickVolume(); }); |
| 39 |
enableClickButton->signal_clicked().connect([&] { EnableClick(); }); |
| 40 |
} |
| 41 |
|
| 42 |
// Set tempo value |
| 43 |
clickTempoScale->set_value(drumKit->GetTempo()); |
| 44 |
// Metronome not running by defautl |
| 45 |
clickVolumeScale->property_sensitive() = false; |
| 46 |
|
| 47 |
// Configure Metronome Window |
| 48 |
{ |
| 49 |
// Append clicks to list |
| 50 |
std::vector<std::string> clicks = drumKit->GetClicksTypes(); |
| 51 |
std::for_each(clicks.cbegin(), clicks.cend(), [this](const std::string& s){ clickTypes->append(s); }); |
| 52 |
|
| 53 |
// Get current click type |
| 54 |
int clickTypeId = drumKit->GetClickTypeId(); |
| 55 |
|
| 56 |
// Select current click type |
| 57 |
clickTypes->set_active(clickTypeId); |
| 58 |
} |
| 59 |
|
| 60 |
// Populate rhythm list |
| 61 |
this->rhythms = drumKit->GetRhythms(); |
| 62 |
{ |
| 63 |
// Append rhythms to list |
| 64 |
std::for_each(rhythms.cbegin(), rhythms.cend(), [this](int x) { rhythmList->append(std::to_string(x)); }); |
| 65 |
|
| 66 |
// Get current rhythm index in vector |
| 67 |
auto it = std::find(rhythms.cbegin(), rhythms.cend(), this->drumKit->GetRhythm()); |
| 68 |
int index = std::distance(rhythms.cbegin(), it); |
| 69 |
|
| 70 |
// Select current rhythm |
| 71 |
rhythmList->set_active(index); |
| 72 |
} |
| 73 |
|
| 74 |
// Populate beats per measure list |
| 75 |
this->bpmeasValues = drumKit->GetBpms(); |
| 76 |
{ |
| 77 |
// Append values to list |
| 78 |
std::for_each(bpmeasValues.cbegin(), bpmeasValues.cend(), [this](int x) { bpmeasList->append(std::to_string(x));}); |
| 79 |
|
| 80 |
// Get current nb of beats per measure's index in vector |
| 81 |
auto it = std::find(bpmeasValues.cbegin(), bpmeasValues.cend(), this->drumKit->GetBpmeas()); |
| 82 |
int index = std::distance(bpmeasValues.cbegin(), it); |
| 83 |
|
| 84 |
// Select current number of beats per measure |
| 85 |
bpmeasList->set_active(index); |
| 86 |
} |
| 87 |
|
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
MetronomeController::~MetronomeController() |
| 92 |
{ |
| 93 |
|
| 94 |
// Delete all windows and dialogs |
| 95 |
delete metronomeWindow; |
| 96 |
|
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
// Private methods |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
void MetronomeController::EnableClick() const |
| 106 |
{ |
| 107 |
|
| 108 |
bool enable = enableClickButton->get_active(); |
| 109 |
|
| 110 |
drumKit->EnableMetronome(enable); |
| 111 |
|
| 112 |
clickVolumeScale->property_sensitive() = enable; |
| 113 |
clickVolumeScale->set_value(drumKit->GetClickVolume()); |
| 114 |
|
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
void MetronomeController::ChangeTempo() const |
| 119 |
{ |
| 120 |
|
| 121 |
int tempo = (int) this->clickTempoScale->get_value(); |
| 122 |
drumKit->ChangeTempo(tempo); |
| 123 |
|
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
void MetronomeController::ChangeClickVolume() const |
| 128 |
{ |
| 129 |
|
| 130 |
int volume = (int) this->clickVolumeScale->get_value(); |
| 131 |
drumKit->ChangeClickVolume(volume); |
| 132 |
clickVolumeScale->property_sensitive() = true; |
| 133 |
|
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
void MetronomeController::ShowMetronomePrefs() |
| 138 |
{ |
| 139 |
|
| 140 |
metronomeWindow->show(); |
| 141 |
|
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
void MetronomeController::SaveMetronomeConfig() |
| 146 |
{ |
| 147 |
|
| 148 |
// Set new parameters values |
| 149 |
drumKit->SetClickType(clickTypes->get_active_row_number()); |
| 150 |
drumKit->SetRhythm(rhythms[rhythmList->get_active_row_number()]); |
| 151 |
drumKit->SetBpmeas(bpmeasValues[bpmeasList->get_active_row_number()]); |
| 152 |
|
| 153 |
drumKit->RestartMetronome(); |
| 154 |
drumKit->SaveMetronomeConfig(); |
| 155 |
|
| 156 |
metronomeWindow->hide(); |
| 157 |
|
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
} /* namespace Gui */ |