sm-26/Source/PluginEditor.cpp

491 lines
18 KiB
C++

#include "PluginEditor.h"
#include "BuildInfo_generated.h"
static juce::String padRight (const juce::String& s, int width)
{
if (s.length() >= width) return s.substring (0, width);
return s + juce::String::repeatedString (" ", width - s.length());
}
SM26AudioProcessorEditor::SM26AudioProcessorEditor (SM26AudioProcessor& p)
: AudioProcessorEditor (p), proc (p)
{
setSize (920, 680);
setResizable (true, false);
setResizeLimits (760, 620, 1500, 1300);
addAndMakeVisible (lcd);
lcd.onPressed = [](int) {};
setupKnobs();
setupNavButtons();
footer.setText (buildGitFooter(), juce::dontSendNotification);
footer.setJustificationType (juce::Justification::centredLeft);
footer.setColour (juce::Label::textColourId, juce::Colour (0xFFA0A0A0));
footer.setFont (juce::Font (juce::FontOptions { juce::Font::getDefaultMonospacedFontName(), 11.0f, juce::Font::plain }));
addAndMakeVisible (footer);
buildRootMenu();
startTimerHz (20);
}
SM26AudioProcessorEditor::~SM26AudioProcessorEditor()
{
stopTimer();
}
juce::String SM26AudioProcessorEditor::buildGitFooter()
{
return juce::String ("SM-26 v") + SM26_GIT_VERSION
+ " | branch " + SM26_GIT_BRANCH
+ " | built " + SM26_BUILD_TIME
+ " | arm64";
}
void SM26AudioProcessorEditor::setupKnobs()
{
for (int i = 0; i < 4; ++i)
{
knobSliders[i].setSliderStyle (juce::Slider::RotaryVerticalDrag);
knobSliders[i].setTextBoxStyle (juce::Slider::NoTextBox, false, 0, 0);
knobSliders[i].setRange (0.0, 1.0, 0.001);
knobSliders[i].setColour (juce::Slider::rotarySliderFillColourId, juce::Colour (0xFFC8C8C8));
knobSliders[i].setColour (juce::Slider::rotarySliderOutlineColourId, juce::Colour (0xFF2A2A2A));
knobSliders[i].setColour (juce::Slider::thumbColourId, juce::Colours::transparentBlack);
knobSliders[i].addListener (this);
addAndMakeVisible (knobSliders[i]);
knobLabels[i].setJustificationType (juce::Justification::centred);
knobLabels[i].setColour (juce::Label::textColourId, juce::Colour (0xFFA0A0A0));
knobLabels[i].setFont (juce::Font (juce::FontOptions { juce::Font::getDefaultMonospacedFontName(), 13.0f, juce::Font::plain }));
addAndMakeVisible (knobLabels[i]);
knobValueLabels[i].setJustificationType (juce::Justification::centred);
knobValueLabels[i].setColour (juce::Label::textColourId, juce::Colour (0xFFD0D0D0));
knobValueLabels[i].setFont (juce::Font (juce::FontOptions { juce::Font::getDefaultMonospacedFontName(), 14.0f, juce::Font::bold }));
addAndMakeVisible (knobValueLabels[i]);
knobSliders[i].setValue (0.5, juce::dontSendNotification);
}
}
void SM26AudioProcessorEditor::setupNavButtons()
{
addAndMakeVisible (navEnter);
addAndMakeVisible (navEsc);
addAndMakeVisible (navStick);
navStick.setRepeatSpeed (250, 50);
navStick.onAction = [this](int dir) {
switch (dir)
{
case JoystickPad::Up:
if (menu.isOpen()) menu.up();
else cyclePreset (-1);
break;
case JoystickPad::Down:
if (menu.isOpen()) menu.down();
else cyclePreset (+1);
break;
case JoystickPad::Left: menu.left(); break;
case JoystickPad::Right: menu.right(); break;
default: break;
}
};
navEnter.onClick = [this] { if (menu.isOpen()) menu.enter(); else menu.open(); };
navEsc.onClick = [this] { if (menu.isOpen()) menu.escape(); else menu.close(); };
}
void SM26AudioProcessorEditor::cyclePreset (int delta)
{
const int count = sm26::PresetStore::count();
if (count <= 0) return;
const int idx = proc.engine.presetIndex();
proc.setPresetByIndex ((idx + delta + count) % count);
}
void SM26AudioProcessorEditor::buildRootMenu()
{
auto builder = [this]() -> MenuScreen
{
MenuScreen s;
s.title = "SM-26 MAIN MENU";
s.selected = 0;
auto addValue = [&](const char* n, int pid) -> MenuItem
{
MenuItem mi;
mi.label = n;
mi.kind = MenuItem::Value;
mi.paramId = pid;
mi.valueText = [this, pid]() {
if (pid == sm26::PDTIME && proc.engine.getParam (sm26::PDSYNC) > 0.5f)
return sm26::formatTimeMs ((float) proc.engine.delayTimeMs());
return sm26::formatParamReal (pid, proc.engine.getParam (pid));
};
mi.onAdjust = [this, pid](float d) {
float delta = (pid == sm26::PDSTEPS) ? d / 0.02f * (1.0f / 15.0f) : d;
float cur = proc.engine.getParam (pid);
proc.engine.setParam (pid, sm26::clamp01 (cur + delta));
for (int k = 0; k < 4; ++k)
{
auto kv = proc.engine.knobView (k);
if (kv.param == pid)
proc.setKnob (k, sm26::clamp01 ((proc.engine.getParam (pid) - kv.lo) / (kv.hi - kv.lo + 1e-9f)));
}
};
return mi;
};
auto addHeader = [&](const char* n) {
MenuItem h;
h.label = "-- " + juce::String (n) + " --";
h.kind = MenuItem::Separator;
s.items.push_back (h);
};
{
MenuItem m;
m.label = "PRESETS (" + juce::String (sm26::PresetStore::count()) + ")";
m.kind = MenuItem::Submenu;
m.onEnter = [this]() { openPresetMenu(); };
s.items.push_back (m);
}
addHeader ("OSCILLATORS");
s.items.push_back (addValue ("OSC1 WAVE", sm26::PO1WAVE));
s.items.push_back (addValue ("OSC2 WAVE", sm26::PO2WAVE));
s.items.push_back (addValue ("O1 OCTAVE", sm26::PO1OCT));
s.items.push_back (addValue ("O1 DETUNE", sm26::PO1CT));
s.items.push_back (addValue ("O2 OCTAVE", sm26::PO2OCT));
s.items.push_back (addValue ("O2 DETUNE", sm26::PO2CT));
s.items.push_back (addValue ("MIX", sm26::PMIX));
s.items.push_back (addValue ("NOISE", sm26::PNOISE));
addHeader ("FILTER");
s.items.push_back (addValue ("CUTOFF", sm26::PCUTOFF));
s.items.push_back (addValue ("RES", sm26::PRES));
s.items.push_back (addValue ("F-ENV", sm26::PFENV));
s.items.push_back (addValue ("F-TYPE", sm26::PFTYPE));
addHeader ("ENVELOPE");
s.items.push_back (addValue ("ATTACK", sm26::PATK));
s.items.push_back (addValue ("DECAY", sm26::PDEC));
s.items.push_back (addValue ("SUSTAIN", sm26::PSUS));
s.items.push_back (addValue ("RELEASE", sm26::PREL));
addHeader ("DELAY");
s.items.push_back (addValue ("TIME/STEPS", sm26::PDTIME));
s.items.push_back (addValue ("STEPS", sm26::PDSTEPS));
s.items.push_back (addValue ("SYNC", sm26::PDSYNC));
s.items.push_back (addValue ("FEEDBACK", sm26::PDFB));
s.items.push_back (addValue ("MIX", sm26::PDMIX));
addHeader ("SYSTEM");
s.items.push_back (addValue ("DRIVE", sm26::PDRIVE));
s.items.push_back (addValue ("GLIDE", sm26::PGLIDE));
s.items.push_back (addValue ("MASTER", sm26::PMASTER));
addHeader ("INFO");
{
MenuItem mi;
mi.label = "ABOUT";
mi.kind = MenuItem::Action;
mi.onEnter = [this]() {
MenuScreen as;
as.title = "ABOUT";
as.selected = 0;
{
MenuItem m2; m2.label = "SM-26 Sound Module by A. Jenewein"; m2.kind = MenuItem::Action;
as.items.push_back (m2);
}
{
MenuItem m2;
m2.label = "Version: " + juce::String (SM26_GIT_VERSION);
m2.kind = MenuItem::Action;
as.items.push_back (m2);
}
{
MenuItem m2;
m2.label = "Built: " + juce::String (SM26_BUILD_TIME);
m2.kind = MenuItem::Action;
as.items.push_back (m2);
}
{
MenuItem m2;
m2.label = "Branch: " + juce::String (SM26_GIT_BRANCH);
m2.kind = MenuItem::Action;
as.items.push_back (m2);
}
{
MenuItem m2; m2.label = "---"; m2.kind = MenuItem::Separator;
as.items.push_back (m2);
}
{
MenuItem m2;
m2.label = juce::String (sm26::PresetStore::count()) + " presets | VST3 + AU";
m2.kind = MenuItem::Action;
as.items.push_back (m2);
}
menu.pushScreen (as);
};
s.items.push_back (mi);
}
return s;
};
menu.setRootScreenBuilder (builder);
}
void SM26AudioProcessorEditor::openPresetMenu()
{
struct Cat { juce::String name; int first; int count; };
std::vector<Cat> cats;
const int total = sm26::PresetStore::count();
for (int i = 0; i < total; ++i)
{
const auto* p = sm26::PresetStore::get (i);
if (cats.empty() || cats.back().name != p->category)
cats.push_back ({ juce::String (p->category), i, 0 });
cats.back().count++;
}
MenuScreen s;
s.title = "SELECT PATCH CATEGORY";
s.selected = 0;
const juce::String curCat = sm26::PresetStore::findByIndex (proc.engine.presetIndex())->category;
for (int c = 0; c < (int) cats.size(); ++c)
{
if (cats[(size_t) c].name == curCat)
s.selected = c;
MenuItem mi;
mi.label = cats[(size_t) c].name + " (" + juce::String (cats[(size_t) c].count) + ")";
mi.kind = MenuItem::Submenu;
mi.onEnter = [this, cats, c]() {
const Cat& cat = cats[(size_t) c];
MenuScreen catScreen;
catScreen.title = "PATCHES: " + cat.name;
catScreen.selected = 0;
const juce::String curName = sm26::PresetStore::findByIndex (proc.engine.presetIndex())->name;
for (int i = cat.first; i < cat.first + cat.count; ++i)
{
const auto* p = sm26::PresetStore::get (i);
if (juce::String (p->name) == curName)
catScreen.selected = i - cat.first;
MenuItem pi;
pi.label = p->name;
pi.kind = MenuItem::Action;
pi.onEnter = [this, i]() {
proc.setPresetByIndex (i);
menu.close();
};
catScreen.items.push_back (pi);
}
menu.pushScreen (catScreen);
};
s.items.push_back (mi);
}
if (! menu.isOpen())
menu.open();
menu.pushScreen (s);
}
void SM26AudioProcessorEditor::paint (juce::Graphics& g)
{
g.fillAll (juce::Colour (0xFF000000));
const float totalW = (float) getWidth();
const float totalH = (float) getHeight();
g.setColour (juce::Colour (0xFF000000));
g.fillRoundedRectangle (8.0f, 8.0f, totalW - 16.0f, totalH - 16.0f, 6.0f);
g.setColour (juce::Colour (0xFF000000));
g.drawRoundedRectangle (8.0f, 8.0f, totalW - 16.0f, totalH - 16.0f, 6.0f, 1.5f);
const float lcdBottom = 16.0f + 302.0f;
g.setColour (juce::Colour (0xFF000000));
g.fillRoundedRectangle (8.0f, lcdBottom, totalW - 16.0f,
juce::jmax (0.0f, totalH - 16.0f - lcdBottom), 6.0f);
g.setColour (juce::Colour (0xFFC8C8C8));
g.setFont (juce::Font (juce::FontOptions { juce::Font::getDefaultMonospacedFontName(), 14.0f, juce::Font::bold }));
g.drawText ("SM-26", 20.0f, 16.0f, 100.0f, 22.0f, juce::Justification::centredLeft);
}
void SM26AudioProcessorEditor::resized()
{
const float totalW = (float) getWidth();
const float totalH = (float) getHeight();
const float footerTop = totalH - 22.0f;
// ---- LCD -----------------------------------------------------------
lcd.setBounds (14, 16, (int) totalW - 28, 302);
const float lcdBottom = 16.0f + 302.0f;
// ---- navigation joystick: directly under the LCD --------------------
const float navSize = 30.0f;
const float navGap = 3.0f;
const float navCenterX = totalW / 2.0f;
const float navTop = lcdBottom + 12.0f;
const float stickS = juce::jlimit (64.0f, 116.0f, juce::jmin (116.0f, totalW * 0.35f));
const float stickLeft = navCenterX - stickS * 0.5f;
navStick.setBounds ((int) stickLeft, (int) navTop, (int) stickS, (int) stickS);
navEnter.setBounds ((int) (stickLeft - navSize - navGap),
(int) (navTop + stickS * 0.5f - navSize * 0.5f),
(int) navSize, (int) navSize);
navEsc.setBounds ((int) (stickLeft + stickS + navGap),
(int) (navTop + stickS * 0.5f - navSize * 0.5f),
(int) navSize, (int) navSize);
// ---- knobs: below the navigation -----------------------------------
const float knobSize = 62.0f;
const float knobGap = 14.0f;
const float knobsW = 4.0f * knobSize + 3.0f * knobGap;
const float knobsX = (totalW - knobsW) / 2.0f;
float knobsY = navTop + stickS + 26.0f;
knobsY = juce::jmin (knobsY, footerTop - (knobSize + 31.0f) - 10.0f);
for (int i = 0; i < 4; ++i)
{
float x = knobsX + (float) i * (knobSize + knobGap);
knobSliders[i].setBounds ((int) x, (int) knobsY, (int) knobSize, (int) knobSize);
knobLabels[i].setBounds ((int) x, (int) (knobsY + knobSize + 2), (int) knobSize, 14);
knobValueLabels[i].setBounds ((int) x, (int) (knobsY + knobSize + 16), (int) knobSize, 15);
}
footer.setBounds (8, (int) footerTop, (int) totalW - 16, 16);
}
void SM26AudioProcessorEditor::timerCallback()
{
updateLCD();
for (int i = 0; i < 4; ++i)
{
auto kv = proc.engine.knobView (i);
float val = proc.engine.getParam (kv.param);
if (! knobDragging[i])
{
float pos = sm26::clamp01 ((val - kv.lo) / (kv.hi - kv.lo + 1e-9f));
knobSliders[i].setValue (pos, juce::dontSendNotification);
}
knobLabels[i].setText (kv.name, juce::dontSendNotification);
knobValueLabels[i].setText (sm26::formatParamReal (kv.param, val), juce::dontSendNotification);
}
if (! menu.isOpen() && menuWasOpen)
buildRootMenu();
menuWasOpen = menu.isOpen();
repaint();
}
void SM26AudioProcessorEditor::sliderValueChanged (juce::Slider* s)
{
for (int i = 0; i < 4; ++i)
{
if (s == &knobSliders[i])
{
float v = (float) s->getValue();
auto kv = proc.engine.knobView (i);
float paramVal = sm26::clamp01 (kv.lo + v * (kv.hi - kv.lo));
proc.engine.setParam (kv.param, paramVal);
knobValueLabels[i].setText (sm26::formatParamReal (kv.param, paramVal),
juce::dontSendNotification);
break;
}
}
}
void SM26AudioProcessorEditor::sliderDragStarted (juce::Slider* s)
{
for (int i = 0; i < 4; ++i)
if (s == &knobSliders[i])
knobDragging[i] = true;
}
void SM26AudioProcessorEditor::sliderDragEnded (juce::Slider* s)
{
for (int i = 0; i < 4; ++i)
if (s == &knobSliders[i])
knobDragging[i] = false;
}
void SM26AudioProcessorEditor::updateLCD()
{
if (! menu.isOpen())
{
int presetIdx = proc.engine.presetIndex();
const auto* p = sm26::PresetStore::findByIndex (presetIdx);
lcd.setLine (0, juce::String (presetIdx + 1) + "/" + juce::String (sm26::PresetStore::count())
+ " " + sm26::PresetStore::fullLabel (*p));
for (int k = 0; k < 4; ++k)
{
auto kv = proc.engine.knobView (k);
lcd.setLine (1 + k, padRight (kv.name, 20)
+ sm26::formatParamReal (kv.param, proc.engine.getParam (kv.param)));
}
double bpm = proc.engine.lastBpm();
juce::String bpmStr = juce::String (bpm, 1) + " BPM";
int notes = proc.engine.notesPlaying();
juce::String notesStr = notes > 0 ? "NOTES:" + juce::String (notes) : "";
lcd.setLine (6, bpmStr + (notesStr.isNotEmpty() ? " " + notesStr : ""));
lcd.setLine (7, juce::String ("SM-26") + " UP/DN=PATCH CLICK=BROWSE OK=MENU");
for (int r = 0; r < LcdDisplay::kRows; ++r)
lcd.setInverted (r, false);
lcd.showCursor (false);
}
else
{
const auto& cs = menu.current();
lcd.setLine (0, "== " + cs.title + " ==");
for (int r = 0; r < LcdDisplay::kRows; ++r)
lcd.setInverted (r, false);
std::vector<int> shown;
for (int i = 0; i < (int) cs.items.size(); ++i)
{
const auto& it = cs.items[i];
if (it.kind == MenuItem::Separator && it.label.isEmpty())
continue;
shown.push_back (i);
}
int pos = 0;
for (int i = 0; i < (int) shown.size(); ++i)
if (shown[i] == cs.selected) { pos = i; break; }
int top = juce::jlimit (0, juce::jmax (0, (int) shown.size() - 6), pos - 2);
for (int row = 0; row < 6 && top + row < (int) shown.size(); ++row)
{
int idx = shown[top + row];
const auto& item = cs.items[idx];
juce::String line = " " + item.label;
if (item.kind == MenuItem::Value && item.valueText)
line = padRight (line, 50) + item.valueText();
else if (item.kind == MenuItem::Submenu)
line = line + " [ENTER]";
else if (item.kind == MenuItem::Separator)
line = " " + item.label;
lcd.setLine (1 + row, line);
lcd.setInverted (1 + row, idx == cs.selected);
}
lcd.setLine (7, "UP/DN=SEL L/R=VAL OK=OK ESC=BACK");
lcd.showCursor (false);
}
}