diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index b4b89030..22ae806a 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -2,7 +2,7 @@ Search before opening an issue to make sure your topic is not a duplicate. Delet ## For bug reports: -Operating system, or "all" if known to exist on all three: +Operating system(s): Version if official Rack release, commit hash and/or branch if from source: ## For feature requests: diff --git a/dep/Makefile b/dep/Makefile index f3f2b4d0..b49bb6f7 100755 --- a/dep/Makefile +++ b/dep/Makefile @@ -145,7 +145,7 @@ ifeq ($(ARCH),win) RTAUDIO_FLAGS += -DAUDIO_WINDOWS_DS=ON -DAUDIO_WINDOWS_WASAPI=ON -DAUDIO_WINDOWS_ASIO=ON endif ifeq ($(ARCH),lin) -RTAUDIO_FLAGS += -DAUDIO_LINUX_ALSA=ON -DAUDIO_LINUX_PULSE=ON +RTAUDIO_FLAGS += -DAUDIO_LINUX_ALSA=ON endif ifdef RTAUDIO_ALL_APIS @@ -153,7 +153,7 @@ ifeq ($(ARCH),mac) RTAUDIO_FLAGS += -DAUDIO_UNIX_JACK=ON endif ifeq ($(ARCH),lin) -RTAUDIO_FLAGS += -DAUDIO_LINUX_JACK=ON +RTAUDIO_FLAGS += -DAUDIO_LINUX_PULSE=ON -DAUDIO_LINUX_JACK=ON endif endif diff --git a/include/app.hpp b/include/app.hpp index 4e136f8b..859723ee 100644 --- a/include/app.hpp +++ b/include/app.hpp @@ -158,6 +158,8 @@ struct RackWidget : OpaqueWidget { void openDialog(); void saveDialog(); void saveAsDialog(); + /** If `lastPath` is defined, ask the user to reload it */ + void revert(); void savePatch(std::string filename); void loadPatch(std::string filename); json_t *toJson(); @@ -171,6 +173,7 @@ struct RackWidget : OpaqueWidget { bool requestModuleBox(ModuleWidget *m, Rect box); /** Moves a module to the closest non-colliding position */ bool requestModuleBoxNearest(ModuleWidget *m, Rect box); + void step() override; void draw(NVGcontext *vg) override; @@ -183,13 +186,6 @@ struct RackRail : TransparentWidget { void draw(NVGcontext *vg) override; }; -struct AddModuleWindow : Window { - Vec modulePos; - - AddModuleWindow(); - void step() override; -}; - struct Panel : TransparentWidget { NVGcolor backgroundColor; std::shared_ptr backgroundImage; @@ -524,8 +520,12 @@ extern RackScene *gRackScene; extern RackWidget *gRackWidget; extern Toolbar *gToolbar; -void sceneInit(); -void sceneDestroy(); +void appInit(); +void appDestroy(); +void appModuleBrowserCreate(); +json_t *appModuleBrowserToJson(); +void appModuleBrowserFromJson(json_t *root); + json_t *colorToJson(NVGcolor color); NVGcolor jsonToColor(json_t *colorJ); diff --git a/include/engine.hpp b/include/engine.hpp index 1c3ebcd6..5806c271 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -18,7 +18,10 @@ struct Light { void setBrightness(float brightness) { value = (brightness > 0.f) ? brightness * brightness : 0.f; } - void setBrightnessSmooth(float brightness); + /** Emulates slow fall (but immediate rise) of LED brightness. + `frames` rescales the timestep. For example, if your module calls this method every 16 frames, use 16.0. + */ + void setBrightnessSmooth(float brightness, float frames = 1.f); }; struct Input { diff --git a/include/ui.hpp b/include/ui.hpp index 482fbcf7..6f731e14 100644 --- a/include/ui.hpp +++ b/include/ui.hpp @@ -14,6 +14,11 @@ struct Label : Widget { void draw(NVGcontext *vg) override; }; +struct List : OpaqueWidget { + void step() override; + void draw(NVGcontext *vg) override; +}; + /** Deletes itself from parent when clicked */ struct MenuOverlay : OpaqueWidget { void step() override; @@ -33,7 +38,7 @@ struct Menu : OpaqueWidget { box.size = Vec(0, 0); } ~Menu(); - // Resizes menu and calls addChild() + /** Deprecated. Just use addChild(child) instead */ void pushChild(Widget *child) DEPRECATED { addChild(child); } @@ -44,10 +49,6 @@ struct Menu : OpaqueWidget { }; struct MenuEntry : OpaqueWidget { - MenuEntry() { - box.size = Vec(0, BND_WIDGET_HEIGHT); - } - template static T *create() { T *o = Widget::create(Vec()); @@ -57,6 +58,9 @@ struct MenuEntry : OpaqueWidget { struct MenuLabel : MenuEntry { std::string text; + MenuLabel() { + box.size = Vec(0, BND_WIDGET_HEIGHT); + } void draw(NVGcontext *vg) override; void step() override; @@ -71,6 +75,9 @@ struct MenuLabel : MenuEntry { struct MenuItem : MenuEntry { std::string text; std::string rightText; + MenuItem() { + box.size = Vec(0, BND_WIDGET_HEIGHT); + } void draw(NVGcontext *vg) override; void step() override; virtual Menu *createChildMenu() {return NULL;} @@ -89,7 +96,7 @@ struct MenuItem : MenuEntry { struct WindowOverlay : OpaqueWidget { }; -struct Window : OpaqueWidget { +struct WindowWidget : OpaqueWidget { std::string title; void draw(NVGcontext *vg) override; void onDragMove(EventDragMove &e) override; @@ -139,22 +146,7 @@ struct Slider : OpaqueWidget, QuantityWidget { void onMouseDown(EventMouseDown &e) override; }; -/** Parent must be a ScrollWidget */ -struct ScrollBar : OpaqueWidget { - enum { VERTICAL, HORIZONTAL } orientation; - BNDwidgetState state = BND_DEFAULT; - float offset = 0.0; - float size = 0.0; - - ScrollBar() { - box.size = Vec(BND_SCROLLBAR_WIDTH, BND_SCROLLBAR_HEIGHT); - } - void draw(NVGcontext *vg) override; - void onDragStart(EventDragStart &e) override; - void onDragMove(EventDragMove &e) override; - void onDragEnd(EventDragEnd &e) override; -}; - +struct ScrollBar; /** Handles a container with ScrollBar */ struct ScrollWidget : OpaqueWidget { Widget *container; @@ -186,7 +178,10 @@ struct TextField : OpaqueWidget { void onFocus(EventFocus &e) override; void onText(EventText &e) override; void onKey(EventKey &e) override; - void insertText(std::string newText); + /** Inserts text at the cursor, replacing the selection if necessary */ + void insertText(std::string text); + /** Replaces the entire text */ + void setText(std::string text); virtual int getTextPosition(Vec mousePos); virtual void onTextChange() {} }; diff --git a/include/widgets.hpp b/include/widgets.hpp index 98757813..6fd6fd18 100644 --- a/include/widgets.hpp +++ b/include/widgets.hpp @@ -257,6 +257,7 @@ struct FramebufferWidget : virtual Widget { void onZoom(EventZoom &e) override; }; +/** A Widget representing a float value */ struct QuantityWidget : virtual Widget { float value = 0.0; float minValue = 0.0; diff --git a/src/core/AudioInterface.cpp b/src/Core/AudioInterface.cpp similarity index 99% rename from src/core/AudioInterface.cpp rename to src/Core/AudioInterface.cpp index a9f2c39d..c7f4ab78 100644 --- a/src/core/AudioInterface.cpp +++ b/src/Core/AudioInterface.cpp @@ -4,7 +4,7 @@ #include #include #include -#include "core.hpp" +#include "Core.hpp" #include "audio.hpp" #include "dsp/samplerate.hpp" #include "dsp/ringbuffer.hpp" diff --git a/src/core/Blank.cpp b/src/Core/Blank.cpp similarity index 99% rename from src/core/Blank.cpp rename to src/Core/Blank.cpp index 2069accd..13fa5d08 100644 --- a/src/core/Blank.cpp +++ b/src/Core/Blank.cpp @@ -1,4 +1,4 @@ -#include "core.hpp" +#include "Core.hpp" using namespace rack; diff --git a/src/core/core.cpp b/src/Core/Core.cpp similarity index 94% rename from src/core/core.cpp rename to src/Core/Core.cpp index 178ddef1..89ead4ec 100644 --- a/src/core/core.cpp +++ b/src/Core/Core.cpp @@ -1,4 +1,4 @@ -#include "core.hpp" +#include "Core.hpp" void init(rack::Plugin *p) { diff --git a/src/core/core.hpp b/src/Core/Core.hpp similarity index 100% rename from src/core/core.hpp rename to src/Core/Core.hpp diff --git a/src/core/MIDICCToCVInterface.cpp b/src/Core/MIDICCToCVInterface.cpp similarity index 99% rename from src/core/MIDICCToCVInterface.cpp rename to src/Core/MIDICCToCVInterface.cpp index 8b280cdb..e01b7bd2 100644 --- a/src/core/MIDICCToCVInterface.cpp +++ b/src/Core/MIDICCToCVInterface.cpp @@ -1,4 +1,4 @@ -#include "core.hpp" +#include "Core.hpp" #include "midi.hpp" #include "dsp/filter.hpp" diff --git a/src/core/MIDIToCVInterface.cpp b/src/Core/MIDIToCVInterface.cpp similarity index 99% rename from src/core/MIDIToCVInterface.cpp rename to src/Core/MIDIToCVInterface.cpp index 0df5eec0..cbd5405e 100644 --- a/src/core/MIDIToCVInterface.cpp +++ b/src/Core/MIDIToCVInterface.cpp @@ -1,4 +1,4 @@ -#include "core.hpp" +#include "Core.hpp" #include "midi.hpp" #include "dsp/filter.hpp" diff --git a/src/core/MIDITriggerToCVInterface.cpp b/src/Core/MIDITriggerToCVInterface.cpp similarity index 99% rename from src/core/MIDITriggerToCVInterface.cpp rename to src/Core/MIDITriggerToCVInterface.cpp index de4bb272..54eef226 100644 --- a/src/core/MIDITriggerToCVInterface.cpp +++ b/src/Core/MIDITriggerToCVInterface.cpp @@ -1,4 +1,4 @@ -#include "core.hpp" +#include "Core.hpp" #include "midi.hpp" #include "dsp/filter.hpp" diff --git a/src/core/MidiClockToCV.cpp b/src/Core/MidiClockToCV.cpp similarity index 100% rename from src/core/MidiClockToCV.cpp rename to src/Core/MidiClockToCV.cpp diff --git a/src/core/MidiIO.cpp b/src/Core/MidiIO.cpp similarity index 100% rename from src/core/MidiIO.cpp rename to src/Core/MidiIO.cpp diff --git a/src/core/MidiIO.hpp b/src/Core/MidiIO.hpp similarity index 100% rename from src/core/MidiIO.hpp rename to src/Core/MidiIO.hpp diff --git a/src/core/Notes.cpp b/src/Core/Notes.cpp similarity index 98% rename from src/core/Notes.cpp rename to src/Core/Notes.cpp index a113f265..7dc1c08f 100644 --- a/src/core/Notes.cpp +++ b/src/Core/Notes.cpp @@ -1,4 +1,4 @@ -#include "core.hpp" +#include "Core.hpp" using namespace rack; diff --git a/src/core/QuadMIDIToCVInterface.cpp b/src/Core/QuadMIDIToCVInterface.cpp similarity index 99% rename from src/core/QuadMIDIToCVInterface.cpp rename to src/Core/QuadMIDIToCVInterface.cpp index 1466d2d3..c39d05af 100644 --- a/src/core/QuadMIDIToCVInterface.cpp +++ b/src/Core/QuadMIDIToCVInterface.cpp @@ -1,4 +1,4 @@ -#include "core.hpp" +#include "Core.hpp" #include "midi.hpp" diff --git a/src/app/AddModuleWindow.cpp b/src/app/AddModuleWindow.cpp deleted file mode 100644 index f80a4c73..00000000 --- a/src/app/AddModuleWindow.cpp +++ /dev/null @@ -1,292 +0,0 @@ -#include "app.hpp" -#include "plugin.hpp" -#include -#include -#include - - -namespace rack { - - -static std::string sManufacturer; -static Model *sModel = NULL; -static std::string sFilter; - - -struct ListMenu : OpaqueWidget { - void draw(NVGcontext *vg) override { - Widget::draw(vg); - } - - void step() override { - Widget::step(); - - box.size.y = 0; - for (Widget *child : children) { - if (!child->visible) - continue; - // Increase height, set position of child - child->box.pos = Vec(0, box.size.y); - box.size.y += child->box.size.y; - child->box.size.x = box.size.x; - } - } -}; - - -struct UrlItem : MenuItem { - std::string url; - void onAction(EventAction &e) override { - std::thread t(openBrowser, url); - t.detach(); - } -}; - - -struct MetadataMenu : ListMenu { - Model *model = NULL; - - void step() override { - if (model != sModel) { - model = sModel; - clearChildren(); - - if (model) { - // Tag list - if (!model->tags.empty()) { - for (ModelTag tag : model->tags) { - addChild(construct(&MenuLabel::text, gTagNames[tag])); - } - addChild(construct()); - } - - // Plugin name - std::string pluginName = model->plugin->slug; - if (!model->plugin->version.empty()) { - pluginName += " v"; - pluginName += model->plugin->version; - } - addChild(construct(&MenuLabel::text, pluginName)); - - // Plugin metadata - if (!model->plugin->website.empty()) { - addChild(construct(&MenuItem::text, "Website", &UrlItem::url, model->plugin->website)); - } - if (!model->plugin->manual.empty()) { - addChild(construct(&MenuItem::text, "Manual", &UrlItem::url, model->plugin->manual)); - } - if (!model->plugin->path.empty()) { - addChild(construct(&MenuItem::text, "Browse directory", &UrlItem::url, model->plugin->path)); - } - } - } - - ListMenu::step(); - } -}; - - -static bool isModelMatch(Model *model, std::string search) { - // Build content string - std::string str; - str += model->manufacturer; - str += " "; - str += model->name; - str += " "; - str += model->slug; - for (ModelTag tag : model->tags) { - str += " "; - str += gTagNames[tag]; - } - str = lowercase(str); - search = lowercase(search); - return (str.find(search) != std::string::npos); -} - - -struct ModelItem : MenuItem { - Model *model; - void onAction(EventAction &e) override { - ModuleWidget *moduleWidget = model->createModuleWidget(); - gRackWidget->moduleContainer->addChild(moduleWidget); - // Move module nearest to the mouse position - Rect box; - box.size = moduleWidget->box.size; - AddModuleWindow *w = getAncestorOfType(); - box.pos = w->modulePos.minus(box.getCenter()); - gRackWidget->requestModuleBoxNearest(moduleWidget, box); - } - void onMouseEnter(EventMouseEnter &e) override { - sModel = model; - MenuItem::onMouseEnter(e); - } -}; - - -struct ModelMenu : ListMenu { - std::string manufacturer; - std::string filter; - - void step() override { - if (manufacturer != sManufacturer) { - manufacturer = sManufacturer; - filter = ""; - clearChildren(); - addChild(construct(&MenuLabel::text, manufacturer)); - // Add models for the selected manufacturer - for (Plugin *plugin : gPlugins) { - for (Model *model : plugin->models) { - if (model->manufacturer == manufacturer) { - addChild(construct(&MenuItem::text, model->name, &ModelItem::model, model)); - } - } - } - } - - if (filter != sFilter) { - filter = sFilter; - // Make all children invisible - for (Widget *child : children) { - child->visible = false; - } - // Make children with a matching model visible - for (Widget *child : children) { - ModelItem *item = dynamic_cast(child); - if (!item) - continue; - - if (isModelMatch(item->model, filter)) { - item->visible = true; - } - } - } - - ListMenu::step(); - } -}; - - -struct ManufacturerItem : MenuItem { - Model *model; - void onAction(EventAction &e) override { - sManufacturer = text; - e.consumed = false; - } -}; - - -struct ManufacturerMenu : ListMenu { - std::string filter; - - ManufacturerMenu() { - addChild(construct(&MenuLabel::text, "Manufacturers")); - - // Collect manufacturer names - std::set manufacturers; - for (Plugin *plugin : gPlugins) { - for (Model *model : plugin->models) { - manufacturers.insert(model->manufacturer); - } - } - // Add menu item for each manufacturer name - for (std::string manufacturer : manufacturers) { - addChild(construct(&MenuItem::text, manufacturer)); - } - } - - void step() override { - if (filter != sFilter) { - // Make all children invisible - for (Widget *child : children) { - child->visible = false; - } - // Make children with a matching model visible - for (Widget *child : children) { - MenuItem *item = dynamic_cast(child); - if (!item) - continue; - - std::string manufacturer = item->text; - for (Plugin *plugin : gPlugins) { - for (Model *model : plugin->models) { - if (model->manufacturer == manufacturer) { - if (isModelMatch(model, sFilter)) { - item->visible = true; - } - } - } - } - } - filter = sFilter; - } - - ListMenu::step(); - } -}; - - -struct SearchModuleField : TextField { - void onTextChange() override { - sFilter = text; - } -}; - - -AddModuleWindow::AddModuleWindow() { - box.size = Vec(600, 300); - title = "Add module"; - - float posY = BND_NODE_TITLE_HEIGHT; - - // Search - SearchModuleField *searchField = new SearchModuleField(); - searchField->box.pos.y = posY; - posY += searchField->box.size.y; - searchField->box.size.x = box.size.x; - searchField->text = sFilter; - gFocusedWidget = searchField; - { - EventFocus eFocus; - searchField->onFocus(eFocus); - searchField->onTextChange(); - } - addChild(searchField); - - // Manufacturers - ManufacturerMenu *manufacturerMenu = new ManufacturerMenu(); - manufacturerMenu->box.size.x = 200; - - ScrollWidget *manufacturerScroll = new ScrollWidget(); - manufacturerScroll->container->addChild(manufacturerMenu); - manufacturerScroll->box.pos = Vec(0, posY); - manufacturerScroll->box.size = Vec(200, box.size.y - posY); - addChild(manufacturerScroll); - - // Models - ModelMenu *modelMenu = new ModelMenu(); - modelMenu->box.size.x = 200; - - ScrollWidget *modelScroll = new ScrollWidget(); - modelScroll->container->addChild(modelMenu); - modelScroll->box.pos = Vec(200, posY); - modelScroll->box.size = Vec(200, box.size.y - posY); - addChild(modelScroll); - - // Metadata - MetadataMenu *metadataMenu = new MetadataMenu(); - metadataMenu->box.size.x = 200; - - ScrollWidget *metadataScroll = new ScrollWidget(); - metadataScroll->container->addChild(metadataMenu); - metadataScroll->box.pos = Vec(400, posY); - metadataScroll->box.size = Vec(200, box.size.y - posY); - addChild(metadataScroll); -} - - -void AddModuleWindow::step() { - Widget::step(); -} - - -} // namespace rack diff --git a/src/app/RackScene.cpp b/src/app/RackScene.cpp index e893d7c7..2e2ff042 100644 --- a/src/app/RackScene.cpp +++ b/src/app/RackScene.cpp @@ -94,35 +94,40 @@ void RackScene::onHoverKey(EventHoverKey &e) { if (windowIsModPressed() && !windowIsShiftPressed()) { gRackWidget->reset(); e.consumed = true; - return; } } break; case GLFW_KEY_Q: { if (windowIsModPressed() && !windowIsShiftPressed()) { windowClose(); e.consumed = true; - return; } } break; case GLFW_KEY_O: { if (windowIsModPressed() && !windowIsShiftPressed()) { gRackWidget->openDialog(); e.consumed = true; - return; } } break; case GLFW_KEY_S: { if (windowIsModPressed() && !windowIsShiftPressed()) { gRackWidget->saveDialog(); e.consumed = true; - return; } if (windowIsModPressed() && windowIsShiftPressed()) { gRackWidget->saveAsDialog(); e.consumed = true; - return; } } break; + case GLFW_KEY_R: { + if (windowIsModPressed() && !windowIsShiftPressed()) { + gRackWidget->revert(); + e.consumed = true; + } + } break; + case GLFW_KEY_ENTER: { + appModuleBrowserCreate(); + e.consumed = true; + } break; } } } diff --git a/src/app/RackWidget.cpp b/src/app/RackWidget.cpp index 8eb6b09d..c5877142 100644 --- a/src/app/RackWidget.cpp +++ b/src/app/RackWidget.cpp @@ -141,6 +141,14 @@ void RackWidget::loadPatch(std::string path) { fclose(file); } +void RackWidget::revert() { + if (lastPath.empty()) + return; + if (osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, "Revert your patch to the last saved state?")) { + loadPatch(lastPath); + } +} + json_t *RackWidget::toJson() { // root json_t *rootJ = json_object(); @@ -438,15 +446,7 @@ void RackWidget::onMouseDown(EventMouseDown &e) { return; if (e.button == 1) { - MenuOverlay *overlay = new MenuOverlay(); - - AddModuleWindow *window = new AddModuleWindow(); - // Set center position - window->box.pos = gMousePos.minus(window->box.getCenter()); - window->modulePos = lastMousePos; - - overlay->addChild(window); - gScene->setOverlay(overlay); + appModuleBrowserCreate(); } e.consumed = true; e.target = this; diff --git a/src/app/Toolbar.cpp b/src/app/Toolbar.cpp index 951ad2d1..d403c68d 100644 --- a/src/app/Toolbar.cpp +++ b/src/app/Toolbar.cpp @@ -30,6 +30,12 @@ struct SaveAsItem : MenuItem { } }; +struct RevertItem : MenuItem { + void onAction(EventAction &e) override { + gRackWidget->revert(); + } +}; + struct QuitItem : MenuItem { void onAction(EventAction &e) override { windowClose(); @@ -42,13 +48,12 @@ struct FileChoice : ChoiceButton { menu->box.pos = getAbsoluteOffset(Vec(0, box.size.y)); menu->box.size.x = box.size.x; - { - menu->addChild(construct(&MenuItem::text, "New", &MenuItem::rightText, WINDOW_MOD_KEY_NAME "+N")); - menu->addChild(construct(&MenuItem::text, "Open", &MenuItem::rightText, WINDOW_MOD_KEY_NAME "+O")); - menu->addChild(construct(&MenuItem::text, "Save", &MenuItem::rightText, WINDOW_MOD_KEY_NAME "+S")); - menu->addChild(construct(&MenuItem::text, "Save as", &MenuItem::rightText, WINDOW_MOD_KEY_NAME "+Shift+S")); - menu->addChild(construct(&MenuItem::text, "Quit", &MenuItem::rightText, WINDOW_MOD_KEY_NAME "+Q")); - } + menu->addChild(construct(&MenuItem::text, "New", &MenuItem::rightText, WINDOW_MOD_KEY_NAME "+N")); + menu->addChild(construct(&MenuItem::text, "Open", &MenuItem::rightText, WINDOW_MOD_KEY_NAME "+O")); + menu->addChild(construct(&MenuItem::text, "Save", &MenuItem::rightText, WINDOW_MOD_KEY_NAME "+S")); + menu->addChild(construct(&MenuItem::text, "Save as", &MenuItem::rightText, WINDOW_MOD_KEY_NAME "+Shift+S")); + menu->addChild(construct(&MenuItem::text, "Revert", &MenuItem::rightText, WINDOW_MOD_KEY_NAME "+R")); + menu->addChild(construct(&MenuItem::text, "Quit", &MenuItem::rightText, WINDOW_MOD_KEY_NAME "+Q")); } }; diff --git a/src/app.cpp b/src/app/app.cpp similarity index 93% rename from src/app.cpp rename to src/app/app.cpp index c8dc1ad8..c49ee550 100644 --- a/src/app.cpp +++ b/src/app/app.cpp @@ -13,14 +13,13 @@ Toolbar *gToolbar = NULL; RackScene *gRackScene = NULL; -void sceneInit() { +void appInit() { gRackScene = new RackScene(); gScene = gRackScene; } -void sceneDestroy() { - delete gScene; - gScene = NULL; +void appDestroy() { + delete gRackScene; } diff --git a/src/app/moduleBrowser.cpp b/src/app/moduleBrowser.cpp new file mode 100644 index 00000000..73d23172 --- /dev/null +++ b/src/app/moduleBrowser.cpp @@ -0,0 +1,212 @@ +#include "app.hpp" +#include "plugin.hpp" +#include "window.hpp" +#include + + +#define BND_LABEL_FONT_SIZE 13 + + +namespace rack { + + +static std::string sSearch; +static std::set sFavoriteModels; + + +struct FavoriteRadioButton : RadioButton { + Model *model = NULL; + void onChange(EventChange &e) override { + debug("HI"); + if (!model) + return; + if (value) { + sFavoriteModels.insert(model); + } + else { + auto it = sFavoriteModels.find(model); + if (it != sFavoriteModels.end()) + sFavoriteModels.erase(it); + } + } +}; + + +struct ModuleListItem : OpaqueWidget { + bool selected = false; + FavoriteRadioButton *favoriteButton; + + ModuleListItem() { + box.size.y = 3 * BND_WIDGET_HEIGHT; + + favoriteButton = new FavoriteRadioButton(); + favoriteButton->box.pos = Vec(7, BND_WIDGET_HEIGHT); + favoriteButton->box.size.x = 20; + favoriteButton->label = "★"; + addChild(favoriteButton); + } + + void draw(NVGcontext *vg) override { + BNDwidgetState state = selected ? BND_HOVER : BND_DEFAULT; + bndMenuItem(vg, 0.0, 0.0, box.size.x, box.size.y, state, -1, ""); + Widget::draw(vg); + } + + void onDragDrop(EventDragDrop &e) override { + if (e.origin != this) + return; + + EventAction eAction; + eAction.consumed = true; + onAction(eAction); + if (eAction.consumed) { + // deletes `this` + gScene->setOverlay(NULL); + } + } +}; + +struct ModelItem : ModuleListItem { + Model *model; + + void setModel(Model *model) { + this->model = model; + auto it = sFavoriteModels.find(model); + if (it != sFavoriteModels.end()) + favoriteButton->setValue(1); + favoriteButton->model = model; + } + + void draw(NVGcontext *vg) override { + ModuleListItem::draw(vg); + + // bndMenuItem(vg, 0.0, 0.0, box.size.x, box.size.y, BND_DEFAULT, -1, model->name.c_str()); + + float x = box.size.x - bndLabelWidth(vg, -1, model->manufacturer.c_str()); + NVGcolor rightColor = bndGetTheme()->menuTheme.textColor; + bndIconLabelValue(vg, x, 0.0, box.size.x, box.size.y, -1, rightColor, BND_LEFT, BND_LABEL_FONT_SIZE, model->manufacturer.c_str(), NULL); + } + + void onAction(EventAction &e) override { + ModuleWidget *moduleWidget = model->createModuleWidget(); + gRackWidget->moduleContainer->addChild(moduleWidget); + // Move module nearest to the mouse position + // Rect box; + // box.size = moduleWidget->box.size; + // AddModuleWindow *w = getAncestorOfType(); + // box.pos = w->modulePos.minus(box.getCenter()); + // gRackWidget->requestModuleBoxNearest(moduleWidget, box); + } +}; + + +struct ModuleBrowser; + +struct SearchModuleField : TextField { + ModuleBrowser *moduleBrowser; + void onTextChange() override; + void onKey(EventKey &e) override; +}; + + +struct ModuleBrowser : OpaqueWidget { + SearchModuleField *searchField; + ScrollWidget *moduleScroll; + List *moduleList; + + ModuleBrowser() { + box.size.x = 300; + + // Search + searchField = new SearchModuleField(); + searchField->box.size.x = box.size.x; + searchField->moduleBrowser = this; + addChild(searchField); + + moduleList = new List(); + moduleList->box.size = Vec(box.size.x, 0.0); + + // Module Scroll + moduleScroll = new ScrollWidget(); + moduleScroll->box.pos.y = searchField->box.size.y; + moduleScroll->box.size.x = box.size.x; + moduleScroll->container->addChild(moduleList); + addChild(moduleScroll); + + // Focus search + searchField->setText(sSearch); + EventFocus eFocus; + searchField->onFocus(eFocus); + } + + void setSearch(std::string search) { + moduleList->clearChildren(); + + // Favorites + for (Model *model : sFavoriteModels) { + ModelItem *item = new ModelItem(); + item->setModel(model); + moduleList->addChild(item); + } + + // Models + for (Plugin *plugin : gPlugins) { + for (Model *model : plugin->models) { + ModelItem *item = new ModelItem(); + item->setModel(model); + moduleList->addChild(item); + } + } + } + + void step() override { + box.pos = parent->box.size.minus(box.size).div(2).round(); + box.pos.y = 40; + box.size.y = parent->box.size.y - 2 * box.pos.y; + + moduleScroll->box.size.y = box.size.y - moduleScroll->box.pos.y; + gFocusedWidget = searchField; + Widget::step(); + } +}; + + +void SearchModuleField::onTextChange() { + sSearch = text; + moduleBrowser->setSearch(text); +} + +void SearchModuleField::onKey(EventKey &e) { + switch (e.key) { + case GLFW_KEY_ESCAPE: { + gScene->setOverlay(NULL); + e.consumed = true; + return; + } break; + } + + if (!e.consumed) { + TextField::onKey(e); + } +} + + +void appModuleBrowserCreate() { + MenuOverlay *overlay = new MenuOverlay(); + + ModuleBrowser *moduleBrowser = new ModuleBrowser(); + overlay->addChild(moduleBrowser); + gScene->setOverlay(overlay); +} + +json_t *appModuleBrowserToJson() { + // TODO + return json_object(); +} + +void appModuleBrowserFromJson(json_t *root) { + // TODO +} + + +} // namespace rack diff --git a/src/engine.cpp b/src/engine.cpp index 9976373d..6dd620ae 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -38,11 +38,11 @@ float Light::getBrightness() { return sqrtf(fmaxf(0.f, value)); } -void Light::setBrightnessSmooth(float brightness) { +void Light::setBrightnessSmooth(float brightness, float frames) { float v = (brightness > 0.f) ? brightness * brightness : 0.f; if (v < value) { // Fade out light with lambda = framerate - value += (v - value) * sampleTime * (60.f * 1.f); + value += (v - value) * sampleTime * frames * 60.f; } else { // Immediately illuminate light diff --git a/src/main.cpp b/src/main.cpp index 37b32a8b..a1f1a157 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,20 +34,22 @@ int main(int argc, char* argv[]) { pluginInit(); engineInit(); windowInit(); - sceneInit(); + appInit(); settingsLoad(assetLocal("settings.json")); + std::string oldLastPath = gRackWidget->lastPath; // To prevent launch crashes, if Rack crashes between now and 15 seconds from now, the "skipAutosaveOnLaunch" property will remain in settings.json, so that in the next launch, the broken autosave will not be loaded. bool oldSkipAutosaveOnLaunch = skipAutosaveOnLaunch; skipAutosaveOnLaunch = true; settingsSave(assetLocal("settings.json")); skipAutosaveOnLaunch = false; - if (oldSkipAutosaveOnLaunch && osdialog_message(OSDIALOG_INFO, OSDIALOG_YES_NO, "Rack has recovered from a crash, likely caused by a faulty module in your patch. Would you like to clear your patch and start over?")) { + if (oldSkipAutosaveOnLaunch && osdialog_message(OSDIALOG_INFO, OSDIALOG_YES_NO, "Rack has recovered from a crash, possibly caused by a faulty module in your patch. Would you like to clear your patch and start over?")) { // Do nothing. Empty patch is already loaded. } else { gRackWidget->loadPatch(assetLocal("autosave.vcv")); } + gRackWidget->lastPath = oldLastPath; engineStart(); windowRun(); @@ -55,7 +57,7 @@ int main(int argc, char* argv[]) { gRackWidget->savePatch(assetLocal("autosave.vcv")); settingsSave(assetLocal("settings.json")); - sceneDestroy(); + appDestroy(); windowDestroy(); engineDestroy(); pluginDestroy(); diff --git a/src/settings.cpp b/src/settings.cpp index b12121da..78737b0f 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -64,6 +64,9 @@ static json_t *settingsToJson() { json_object_set_new(rootJ, "skipAutosaveOnLaunch", json_true()); } + // moduleBrowser + json_object_set_new(rootJ, "moduleBrowser", appModuleBrowserToJson()); + return rootJ; } @@ -123,9 +126,15 @@ static void settingsFromJson(json_t *rootJ) { if (lastPathJ) gRackWidget->lastPath = json_string_value(lastPathJ); + // skipAutosaveOnLaunch json_t *skipAutosaveOnLaunchJ = json_object_get(rootJ, "skipAutosaveOnLaunch"); if (skipAutosaveOnLaunchJ) skipAutosaveOnLaunch = json_boolean_value(skipAutosaveOnLaunchJ); + + // moduleBrowser + json_t * moduleBrowserJ = json_object_get(rootJ, "moduleBrowser"); + if (moduleBrowserJ) + appModuleBrowserFromJson(moduleBrowserJ); } diff --git a/src/ui/List.cpp b/src/ui/List.cpp new file mode 100644 index 00000000..791a226d --- /dev/null +++ b/src/ui/List.cpp @@ -0,0 +1,30 @@ +#include "ui.hpp" + + +namespace rack { + + +void List::step() { + Widget::step(); + + // Set positions of children + box.size.y = 0.0; + for (Widget *child : children) { + if (!child->visible) + continue; + // Increment height, set position of child + child->box.pos = Vec(0.0, box.size.y); + box.size.y += child->box.size.y; + // Resize width of child + child->box.size.x = box.size.x - BND_SCROLLBAR_WIDTH; + } +} + +void List::draw(NVGcontext *vg) { + bndBackground(vg, 0.0, 0.0, box.size.x, box.size.y); + bndBevel(vg, 0.0, 0.0, box.size.x, box.size.y); + Widget::draw(vg); +} + + +} // namespace rack diff --git a/src/ui/Menu.cpp b/src/ui/Menu.cpp index cc53b5fc..580f3afc 100644 --- a/src/ui/Menu.cpp +++ b/src/ui/Menu.cpp @@ -29,7 +29,7 @@ void Menu::step() { for (Widget *child : children) { if (!child->visible) continue; - // Increase height, set position of child + // Increment height, set position of child child->box.pos = Vec(0, box.size.y); box.size.y += child->box.size.y; // Increase width based on maximum width of child diff --git a/src/ui/MenuItem.cpp b/src/ui/MenuItem.cpp index 5bc46499..6cc976fe 100644 --- a/src/ui/MenuItem.cpp +++ b/src/ui/MenuItem.cpp @@ -24,7 +24,7 @@ void MenuItem::draw(NVGcontext *vg) { } void MenuItem::step() { - // Add 10 more pixels because Retina measurements are sometimes too small + // Add 10 more pixels because measurements on high-DPI screens are sometimes too small for some reason const float rightPadding = 10.0; // HACK use gVg from the window. // All this does is inspect the font, so it shouldn't modify gVg and should work when called from a FramebufferWidget for example. @@ -53,7 +53,7 @@ void MenuItem::onDragDrop(EventDragDrop &e) { return; EventAction eAction; - // TODO Perhaps remove this? It would require all onAction() methods to call this explicitly, which might be too annoying to change. + // Consume event by default, but allow action to un-consume it to prevent the menu from being removed. eAction.consumed = true; onAction(eAction); if (eAction.consumed) { diff --git a/src/ui/MenuOverlay.cpp b/src/ui/MenuOverlay.cpp index 621cff0e..92ed66af 100644 --- a/src/ui/MenuOverlay.cpp +++ b/src/ui/MenuOverlay.cpp @@ -1,4 +1,5 @@ #include "ui.hpp" +#include "window.hpp" namespace rack { @@ -23,9 +24,19 @@ void MenuOverlay::onMouseDown(EventMouseDown &e) { } void MenuOverlay::onHoverKey(EventHoverKey &e) { - // Recurse children but consume the event - Widget::onHoverKey(e); - e.consumed = true; + switch (e.key) { + case GLFW_KEY_ESCAPE: { + gScene->setOverlay(NULL); + e.consumed = true; + return; + } break; + } + + if (!e.consumed) { + // Recurse children but consume the event + Widget::onHoverKey(e); + e.consumed = true; + } } diff --git a/src/ui/RadioButton.cpp b/src/ui/RadioButton.cpp index 18d4220e..e00fbdd9 100644 --- a/src/ui/RadioButton.cpp +++ b/src/ui/RadioButton.cpp @@ -17,10 +17,10 @@ void RadioButton::onMouseLeave(EventMouseLeave &e) { void RadioButton::onDragDrop(EventDragDrop &e) { if (e.origin == this) { - if (value == 0.0) - value = 1.0; + if (value) + setValue(0.0); else - value = 0.0; + setValue(1.0); EventAction eAction; onAction(eAction); diff --git a/src/ui/ScrollBar.cpp b/src/ui/ScrollBar.cpp deleted file mode 100644 index a4676837..00000000 --- a/src/ui/ScrollBar.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "ui.hpp" -#include "window.hpp" - - -namespace rack { - - -void ScrollBar::draw(NVGcontext *vg) { - bndScrollBar(vg, 0.0, 0.0, box.size.x, box.size.y, state, offset, size); -} - -void ScrollBar::onDragStart(EventDragStart &e) { - state = BND_ACTIVE; - windowCursorLock(); -} - -void ScrollBar::onDragMove(EventDragMove &e) { - ScrollWidget *scrollWidget = dynamic_cast(parent); - assert(scrollWidget); - if (orientation == HORIZONTAL) - scrollWidget->offset.x += e.mouseRel.x; - else - scrollWidget->offset.y += e.mouseRel.y; -} - -void ScrollBar::onDragEnd(EventDragEnd &e) { - state = BND_DEFAULT; - windowCursorUnlock(); -} - - -} // namespace rack diff --git a/src/ui/ScrollWidget.cpp b/src/ui/ScrollWidget.cpp index 7c59caa7..f494a371 100644 --- a/src/ui/ScrollWidget.cpp +++ b/src/ui/ScrollWidget.cpp @@ -4,6 +4,47 @@ namespace rack { + +/** Parent must be a ScrollWidget */ +struct ScrollBar : OpaqueWidget { + enum Orientation { + VERTICAL, + HORIZONTAL + }; + Orientation orientation; + BNDwidgetState state = BND_DEFAULT; + float offset = 0.0; + float size = 0.0; + + ScrollBar() { + box.size = Vec(BND_SCROLLBAR_WIDTH, BND_SCROLLBAR_HEIGHT); + } + + void draw(NVGcontext *vg) override { + bndScrollBar(vg, 0.0, 0.0, box.size.x, box.size.y, state, offset, size); + } + + void onDragStart(EventDragStart &e) override { + state = BND_ACTIVE; + windowCursorLock(); + } + + void onDragMove(EventDragMove &e) override { + ScrollWidget *scrollWidget = dynamic_cast(parent); + assert(scrollWidget); + if (orientation == HORIZONTAL) + scrollWidget->offset.x += e.mouseRel.x; + else + scrollWidget->offset.y += e.mouseRel.y; + } + + void onDragEnd(EventDragEnd &e) override { + state = BND_DEFAULT; + windowCursorUnlock(); + } +}; + + ScrollWidget::ScrollWidget() { container = new Widget(); addChild(container); @@ -21,9 +62,7 @@ ScrollWidget::ScrollWidget() { void ScrollWidget::draw(NVGcontext *vg) { nvgScissor(vg, 0, 0, box.size.x, box.size.y); - Widget::draw(vg); - nvgResetScissor(vg); } @@ -32,13 +71,6 @@ void ScrollWidget::step() { Vec containerCorner = container->getChildrenBoundingBox().getBottomRight(); offset = offset.clamp(Rect(Vec(0, 0), containerCorner.minus(box.size))); - // Resize scroll bars - Vec inner = Vec(box.size.x - verticalScrollBar->box.size.x, box.size.y - horizontalScrollBar->box.size.y); - horizontalScrollBar->box.pos.y = inner.y; - horizontalScrollBar->box.size.x = inner.x; - verticalScrollBar->box.pos.x = inner.x; - verticalScrollBar->box.size.y = inner.y; - // Update the container's positions from the offset container->box.pos = offset.neg().round(); @@ -47,12 +79,19 @@ void ScrollWidget::step() { Vec scrollbarOffset = offset.div(viewportSize.minus(box.size)); Vec scrollbarSize = box.size.div(viewportSize); - horizontalScrollBar->offset = scrollbarOffset.x; - horizontalScrollBar->size = scrollbarSize.x; horizontalScrollBar->visible = (0.0 < scrollbarSize.x && scrollbarSize.x < 1.0); + verticalScrollBar->visible = (0.0 < scrollbarSize.y && scrollbarSize.y < 1.0); + horizontalScrollBar->offset = scrollbarOffset.x; verticalScrollBar->offset = scrollbarOffset.y; + horizontalScrollBar->size = scrollbarSize.x; verticalScrollBar->size = scrollbarSize.y; - verticalScrollBar->visible = (0.0 < scrollbarSize.y && scrollbarSize.y < 1.0); + + // Resize scroll bars + Vec inner = Vec(box.size.x - verticalScrollBar->box.size.x, box.size.y - horizontalScrollBar->box.size.y); + horizontalScrollBar->box.pos.y = inner.y; + verticalScrollBar->box.pos.x = inner.x; + horizontalScrollBar->box.size.x = verticalScrollBar->visible ? inner.x : box.size.x; + verticalScrollBar->box.size.y = horizontalScrollBar->visible ? inner.y : box.size.y; Widget::step(); } diff --git a/src/ui/TextField.cpp b/src/ui/TextField.cpp index be9d314b..9278ef51 100644 --- a/src/ui/TextField.cpp +++ b/src/ui/TextField.cpp @@ -155,15 +155,20 @@ void TextField::onKey(EventKey &e) { e.consumed = true; } -void TextField::insertText(std::string newText) { +void TextField::insertText(std::string text) { if (begin < end) - text.erase(begin, end - begin); - text.insert(begin, newText); - begin += newText.size(); + this->text.erase(begin, end - begin); + this->text.insert(begin, text); + begin += text.size(); end = begin; onTextChange(); } +void TextField::setText(std::string text) { + this->text = text; + onTextChange(); +} + int TextField::getTextPosition(Vec mousePos) { return bndTextFieldTextPosition(gVg, 0.0, 0.0, box.size.x, box.size.y, -1, text.c_str(), mousePos.x, mousePos.y); } diff --git a/src/ui/Window.cpp b/src/ui/WindowWidget.cpp similarity index 72% rename from src/ui/Window.cpp rename to src/ui/WindowWidget.cpp index b95f94c7..a282fbde 100644 --- a/src/ui/Window.cpp +++ b/src/ui/WindowWidget.cpp @@ -4,12 +4,12 @@ namespace rack { -void Window::draw(NVGcontext *vg) { +void WindowWidget::draw(NVGcontext *vg) { bndNodeBackground(vg, 0.0, 0.0, box.size.x, box.size.y, BND_DEFAULT, -1, title.c_str(), bndGetTheme()->backgroundColor); Widget::draw(vg); } -void Window::onDragMove(EventDragMove &e) { +void WindowWidget::onDragMove(EventDragMove &e) { box.pos = box.pos.plus(e.mouseRel); } diff --git a/src/ui.cpp b/src/ui/ui.cpp similarity index 100% rename from src/ui.cpp rename to src/ui/ui.cpp diff --git a/src/util.cpp b/src/util.cpp deleted file mode 100644 index 8b000aab..00000000 --- a/src/util.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include -#include -#include -#include // for dirname and basename -#include - -#if ARCH_WIN -#include -#include -#endif diff --git a/src/widgets.cpp b/src/widgets/widgets.cpp similarity index 100% rename from src/widgets.cpp rename to src/widgets/widgets.cpp