Alternative approach to custom module widget behaviour

Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
falkTX 2022-11-30 21:52:32 +00:00
parent 377cf01ddc
commit dca76207e7
21 changed files with 1727 additions and 340 deletions

View file

@ -40,94 +40,11 @@
#include <settings.hpp>
#include <system.hpp>
#undef ModuleWidget
namespace rack {
namespace app {
struct CardinalModuleWidget : ModuleWidget {
CardinalModuleWidget() : ModuleWidget() {}
DEPRECATED CardinalModuleWidget(engine::Module* module) : ModuleWidget() {
setModule(module);
}
void onButton(const ButtonEvent& e) override;
};
struct ModuleWidget::Internal {
math::Vec dragOffset;
math::Vec dragRackPos;
bool dragEnabled;
widget::Widget* panel;
};
static void CardinalModuleWidget__loadDialog(ModuleWidget* const w)
{
std::string presetDir = w->model->getUserPresetDirectory();
system::createDirectories(presetDir);
WeakPtr<ModuleWidget> weakThis = w;
async_dialog_filebrowser(false, nullptr, presetDir.c_str(), "Load preset", [=](char* pathC) {
// Delete directories if empty
DEFER({
try {
system::remove(presetDir);
system::remove(system::getDirectory(presetDir));
}
catch (Exception& e) {
// Ignore exceptions if directory cannot be removed.
}
});
if (!weakThis)
return;
if (!pathC)
return;
try {
weakThis->loadAction(pathC);
}
catch (Exception& e) {
async_dialog_message(e.what());
}
std::free(pathC);
});
}
void CardinalModuleWidget__saveDialog(ModuleWidget* const w)
{
const std::string presetDir = w->model->getUserPresetDirectory();
system::createDirectories(presetDir);
WeakPtr<ModuleWidget> weakThis = w;
async_dialog_filebrowser(true, "preset.vcvm", presetDir.c_str(), "Save preset", [=](char* pathC) {
// Delete directories if empty
DEFER({
try {
system::remove(presetDir);
system::remove(system::getDirectory(presetDir));
}
catch (Exception& e) {
// Ignore exceptions if directory cannot be removed.
}
});
if (!weakThis)
return;
if (!pathC)
return;
std::string path = pathC;
std::free(pathC);
// Automatically append .vcvm extension
if (system::getExtension(path) != ".vcvm")
path += ".vcvm";
weakThis->save(path);
});
}
// Create ModulePresetPathItems for each patch in a directory.
static void appendPresetItems(ui::Menu* menu, WeakPtr<ModuleWidget> moduleWidget, std::string presetDir) {
bool foundPresets = false;
@ -165,117 +82,6 @@ static void appendPresetItems(ui::Menu* menu, WeakPtr<ModuleWidget> moduleWidget
}
};
static void CardinalModuleWidget__createContextMenu(ModuleWidget* const w,
plugin::Model* const model,
engine::Module* const module) {
DISTRHO_SAFE_ASSERT_RETURN(model != nullptr,);
ui::Menu* menu = createMenu();
WeakPtr<ModuleWidget> weakThis = w;
// Brand and module name
menu->addChild(createMenuLabel(model->name));
menu->addChild(createMenuLabel(model->plugin->brand));
// Info
menu->addChild(createSubmenuItem("Info", "", [model](ui::Menu* menu) {
model->appendContextMenu(menu);
}));
// Preset
menu->addChild(createSubmenuItem("Preset", "", [weakThis](ui::Menu* menu) {
menu->addChild(createMenuItem("Copy", RACK_MOD_CTRL_NAME "+C", [weakThis]() {
if (!weakThis)
return;
weakThis->copyClipboard();
}));
menu->addChild(createMenuItem("Paste", RACK_MOD_CTRL_NAME "+V", [weakThis]() {
if (!weakThis)
return;
weakThis->pasteClipboardAction();
}));
menu->addChild(createMenuItem("Open", "", [weakThis]() {
if (!weakThis)
return;
CardinalModuleWidget__loadDialog(weakThis);
}));
/* TODO requires setting up user dir
menu->addChild(createMenuItem("Save as", "", [weakThis]() {
if (!weakThis)
return;
CardinalModuleWidget__saveDialog(weakThis);
}));
// Scan `<user dir>/presets/<plugin slug>/<module slug>` for presets.
menu->addChild(new ui::MenuSeparator);
menu->addChild(createMenuLabel("User presets"));
appendPresetItems(menu, weakThis, weakThis->model->getUserPresetDirectory());
*/
// Scan `<plugin dir>/presets/<module slug>` for presets.
appendPresetItems(menu, weakThis, weakThis->model->getFactoryPresetDirectory());
}));
// Initialize
menu->addChild(createMenuItem("Initialize", RACK_MOD_CTRL_NAME "+I", [weakThis]() {
if (!weakThis)
return;
weakThis->resetAction();
}));
// Randomize
menu->addChild(createMenuItem("Randomize", RACK_MOD_CTRL_NAME "+R", [weakThis]() {
if (!weakThis)
return;
weakThis->randomizeAction();
}));
// Disconnect cables
menu->addChild(createMenuItem("Disconnect cables", RACK_MOD_CTRL_NAME "+U", [weakThis]() {
if (!weakThis)
return;
weakThis->disconnectAction();
}));
// Bypass
std::string bypassText = RACK_MOD_CTRL_NAME "+E";
bool bypassed = module && module->isBypassed();
if (bypassed)
bypassText += " " CHECKMARK_STRING;
menu->addChild(createMenuItem("Bypass", bypassText, [weakThis, bypassed]() {
if (!weakThis)
return;
weakThis->bypassAction(!bypassed);
}));
// Duplicate
menu->addChild(createMenuItem("Duplicate", RACK_MOD_CTRL_NAME "+D", [weakThis]() {
if (!weakThis)
return;
weakThis->cloneAction(false);
}));
// Duplicate with cables
menu->addChild(createMenuItem("└ with cables", RACK_MOD_SHIFT_NAME "+" RACK_MOD_CTRL_NAME "+D", [weakThis]() {
if (!weakThis)
return;
weakThis->cloneAction(true);
}));
// Delete
menu->addChild(createMenuItem("Delete", "Backspace/Delete", [weakThis]() {
if (!weakThis)
return;
weakThis->removeAction();
}, false, true));
w->appendContextMenu(menu);
}
static void CardinalModuleWidget__saveSelectionDialog(RackWidget* const w)
{
std::string selectionDir = asset::user("selections");
@ -304,75 +110,6 @@ static void CardinalModuleWidget__saveSelectionDialog(RackWidget* const w)
});
}
void CardinalModuleWidget::onButton(const ButtonEvent& e)
{
const bool selected = APP->scene->rack->isSelected(this);
if (selected) {
if (e.button == GLFW_MOUSE_BUTTON_RIGHT) {
if (e.action == GLFW_PRESS) {
// Open selection context menu on right-click
ui::Menu* menu = createMenu();
patchUtils::appendSelectionContextMenu(menu);
}
e.consume(this);
}
if (e.button == GLFW_MOUSE_BUTTON_LEFT) {
if (e.action == GLFW_PRESS) {
// Toggle selection on Shift-click
if ((e.mods & RACK_MOD_MASK) == GLFW_MOD_SHIFT) {
APP->scene->rack->select(this, false);
e.consume(NULL);
return;
}
// If module positions are locked, don't consume left-click
if (settings::lockModules) {
return;
}
internal->dragOffset = e.pos;
}
e.consume(this);
}
return;
}
// Dispatch event to children
Widget::onButton(e);
e.stopPropagating();
if (e.isConsumed())
return;
if (e.button == GLFW_MOUSE_BUTTON_LEFT) {
if (e.action == GLFW_PRESS) {
// Toggle selection on Shift-click
if ((e.mods & RACK_MOD_MASK) == GLFW_MOD_SHIFT) {
APP->scene->rack->select(this, true);
e.consume(NULL);
return;
}
// If module positions are locked, don't consume left-click
if (settings::lockModules) {
return;
}
internal->dragOffset = e.pos;
}
e.consume(this);
}
// Open context menu on right-click
if (e.button == GLFW_MOUSE_BUTTON_RIGHT && e.action == GLFW_PRESS) {
CardinalModuleWidget__createContextMenu(this, model, module);
e.consume(this);
}
}
}
}