From de5c9ca2af7df9fbe6b0cfef9dc311d614f0ce42 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sat, 12 Aug 2017 03:38:10 -0700 Subject: [PATCH] Added Scene::createMenu(), renamed arch macros to ARCH_* --- Makefile | 4 +- Makefile-plugin.inc | 3 +- Makefile.inc | 7 ++-- include/app.hpp | 1 - include/widgets.hpp | 1 + src/app/ModuleWidget.cpp | 87 +++++++++++++++++++--------------------- src/app/ParamWidget.cpp | 4 +- src/app/RackScene.cpp | 1 - src/app/RackWidget.cpp | 50 ++++++++++------------- src/main.cpp | 4 +- src/plugin.cpp | 51 +++++++++++------------ src/widgets/Scene.cpp | 12 ++++++ 12 files changed, 110 insertions(+), 115 deletions(-) diff --git a/Makefile b/Makefile index 88b21f7d..e5e9dfb4 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ ARCH ?= lin -FLAGS = -g -Wall -O2 -msse -mfpmath=sse -ffast-math -fno-finite-math-only \ +FLAGS += -g -Wall -O3 -march=core2 -ffast-math \ -I./ext -I./include -CXXFLAGS = -fno-exceptions +CXXFLAGS += -std=c++11 -fno-exceptions SOURCES = $(wildcard src/*.cpp src/*/*.cpp) \ ext/nanovg/src/nanovg.c diff --git a/Makefile-plugin.inc b/Makefile-plugin.inc index 42b772ee..b453fb25 100644 --- a/Makefile-plugin.inc +++ b/Makefile-plugin.inc @@ -1,8 +1,9 @@ ARCH ?= lin -FLAGS += -fPIC -g -Wall -O3 -msse -mfpmath=sse -ffast-math \ +FLAGS += -fPIC -g -Wall -O3 -march=core2 -ffast-math \ -I../../include +CXXFLAGS += -std=c++11 -fno-exceptions LDFLAGS += diff --git a/Makefile.inc b/Makefile.inc index 37af6b75..66aa19ce 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -1,17 +1,16 @@ FLAGS += -MMD -CXXFLAGS += -std=c++11 ifeq ($(ARCH), lin) CC ?= gcc CXX ?= g++ -FLAGS += -DLINUX +FLAGS += -DARCH_LIN endif ifeq ($(ARCH), mac) CC ?= clang CXX ?= clang++ -FLAGS += -DAPPLE +FLAGS += -DARCH_MAC CXXFLAGS += -stdlib=libc++ LDFLAGS += -stdlib=libc++ endif @@ -19,7 +18,7 @@ endif ifeq ($(ARCH), win) CC ?= x86_64-w64-mingw32-gcc CXX ?= x86_64-w64-mingw32-g++ -FLAGS += -DWINDOWS -D_USE_MATH_DEFINES +FLAGS += -DARCH_WIN -D_USE_MATH_DEFINES endif diff --git a/include/app.hpp b/include/app.hpp index 646bc7e9..2d6ab15d 100644 --- a/include/app.hpp +++ b/include/app.hpp @@ -289,7 +289,6 @@ struct RackScene : Scene { extern std::string gApplicationName; extern std::string gApplicationVersion; -extern Scene *gScene; extern RackWidget *gRackWidget; void sceneInit(); diff --git a/include/widgets.hpp b/include/widgets.hpp index f4a5c459..63bf2fa9 100644 --- a/include/widgets.hpp +++ b/include/widgets.hpp @@ -387,6 +387,7 @@ struct Tooltip : Widget { struct Scene : OpaqueWidget { Widget *overlay = NULL; void setOverlay(Widget *w); + Menu *createMenu(); void step(); }; diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index 93ac7e0d..93d7bfaa 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -40,40 +40,40 @@ void ModuleWidget::addParam(ParamWidget *param) { } json_t *ModuleWidget::toJson() { - json_t *root = json_object(); + json_t *rootJ = json_object(); // plugin - json_object_set_new(root, "plugin", json_string(model->plugin->slug.c_str())); + json_object_set_new(rootJ, "plugin", json_string(model->plugin->slug.c_str())); // model - json_object_set_new(root, "model", json_string(model->slug.c_str())); + json_object_set_new(rootJ, "model", json_string(model->slug.c_str())); // pos json_t *pos = json_pack("[f, f]", (double) box.pos.x, (double) box.pos.y); - json_object_set_new(root, "pos", pos); + json_object_set_new(rootJ, "pos", pos); // params json_t *paramsJ = json_array(); for (ParamWidget *paramWidget : params) { json_t *paramJ = paramWidget->toJson(); json_array_append_new(paramsJ, paramJ); } - json_object_set_new(root, "params", paramsJ); + json_object_set_new(rootJ, "params", paramsJ); // data json_t *dataJ = module ? module->toJsonData() : NULL; if (dataJ) { - json_object_set_new(root, "data", dataJ); + json_object_set_new(rootJ, "data", dataJ); } - return root; + return rootJ; } -void ModuleWidget::fromJson(json_t *root) { +void ModuleWidget::fromJson(json_t *rootJ) { // pos - json_t *pos = json_object_get(root, "pos"); + json_t *pos = json_object_get(rootJ, "pos"); double x, y; json_unpack(pos, "[F, F]", &x, &y); box.pos = Vec(x, y); // params - json_t *paramsJ = json_object_get(root, "params"); + json_t *paramsJ = json_object_get(rootJ, "params"); size_t paramId; json_t *paramJ; json_array_foreach(paramsJ, paramId, paramJ) { @@ -83,7 +83,7 @@ void ModuleWidget::fromJson(json_t *root) { } // data - json_t *dataJ = json_object_get(root, "data"); + json_t *dataJ = json_object_get(rootJ, "data"); if (dataJ && module) { module->fromJsonData(dataJ); } @@ -197,41 +197,36 @@ struct DeleteModuleMenuItem : MenuItem { void ModuleWidget::onMouseDown(int button) { if (button == 1) { - MenuOverlay *overlay = new MenuOverlay(); - Menu *menu = new Menu(); - menu->box.pos = gMousePos; - { - MenuLabel *menuLabel = new MenuLabel(); - menuLabel->text = model->plugin->name + ": " + model->name; - menu->pushChild(menuLabel); - - ResetParamsMenuItem *resetItem = new ResetParamsMenuItem(); - resetItem->text = "Initialize"; - resetItem->moduleWidget = this; - menu->pushChild(resetItem); - - RandomizeParamsMenuItem *randomizeParams = new RandomizeParamsMenuItem(); - randomizeParams->text = "Randomize"; - randomizeParams->moduleWidget = this; - menu->pushChild(randomizeParams); - - DisconnectPortsMenuItem *disconnectItem = new DisconnectPortsMenuItem(); - disconnectItem->text = "Disconnect cables"; - disconnectItem->moduleWidget = this; - menu->pushChild(disconnectItem); - - CloneModuleMenuItem *cloneItem = new CloneModuleMenuItem(); - cloneItem->text = "Clone"; - cloneItem->moduleWidget = this; - menu->pushChild(cloneItem); - - DeleteModuleMenuItem *deleteItem = new DeleteModuleMenuItem(); - deleteItem->text = "Delete"; - deleteItem->moduleWidget = this; - menu->pushChild(deleteItem); - } - overlay->addChild(menu); - gScene->setOverlay(overlay); + Menu *menu = gScene->createMenu(); + + MenuLabel *menuLabel = new MenuLabel(); + menuLabel->text = model->plugin->name + ": " + model->name; + menu->pushChild(menuLabel); + + ResetParamsMenuItem *resetItem = new ResetParamsMenuItem(); + resetItem->text = "Initialize"; + resetItem->moduleWidget = this; + menu->pushChild(resetItem); + + RandomizeParamsMenuItem *randomizeParams = new RandomizeParamsMenuItem(); + randomizeParams->text = "Randomize"; + randomizeParams->moduleWidget = this; + menu->pushChild(randomizeParams); + + DisconnectPortsMenuItem *disconnectItem = new DisconnectPortsMenuItem(); + disconnectItem->text = "Disconnect cables"; + disconnectItem->moduleWidget = this; + menu->pushChild(disconnectItem); + + CloneModuleMenuItem *cloneItem = new CloneModuleMenuItem(); + cloneItem->text = "Clone"; + cloneItem->moduleWidget = this; + menu->pushChild(cloneItem); + + DeleteModuleMenuItem *deleteItem = new DeleteModuleMenuItem(); + deleteItem->text = "Delete"; + deleteItem->moduleWidget = this; + menu->pushChild(deleteItem); } } diff --git a/src/app/ParamWidget.cpp b/src/app/ParamWidget.cpp index 7f708809..6f83574d 100644 --- a/src/app/ParamWidget.cpp +++ b/src/app/ParamWidget.cpp @@ -9,8 +9,8 @@ json_t *ParamWidget::toJson() { return paramJ; } -void ParamWidget::fromJson(json_t *root) { - setValue(json_number_value(root)); +void ParamWidget::fromJson(json_t *rootJ) { + setValue(json_number_value(rootJ)); } void ParamWidget::onMouseDown(int button) { diff --git a/src/app/RackScene.cpp b/src/app/RackScene.cpp index 9c984052..a8dc8401 100644 --- a/src/app/RackScene.cpp +++ b/src/app/RackScene.cpp @@ -30,5 +30,4 @@ void RackScene::draw(NVGcontext *vg) { } - } // namespace rack diff --git a/src/app/RackWidget.cpp b/src/app/RackWidget.cpp index 8e7ec62f..db3eac18 100644 --- a/src/app/RackWidget.cpp +++ b/src/app/RackWidget.cpp @@ -33,10 +33,10 @@ void RackWidget::savePatch(std::string filename) { if (!file) return; - json_t *root = toJson(); - if (root) { - json_dumpf(root, file, JSON_INDENT(2)); - json_decref(root); + json_t *rootJ = toJson(); + if (rootJ) { + json_dumpf(rootJ, file, JSON_INDENT(2)); + json_decref(rootJ); } fclose(file); @@ -49,11 +49,11 @@ void RackWidget::loadPatch(std::string filename) { return; json_error_t error; - json_t *root = json_loadf(file, 0, &error); - if (root) { + json_t *rootJ = json_loadf(file, 0, &error); + if (rootJ) { clear(); - fromJson(root); - json_decref(root); + fromJson(rootJ); + json_decref(rootJ); } else { printf("JSON parsing error at %s %d:%d %s\n", error.source, error.line, error.column, error.text); @@ -64,19 +64,19 @@ void RackWidget::loadPatch(std::string filename) { json_t *RackWidget::toJson() { // root - json_t *root = json_object(); + json_t *rootJ = json_object(); // version json_t *versionJ = json_string(gApplicationVersion.c_str()); - json_object_set_new(root, "version", versionJ); + json_object_set_new(rootJ, "version", versionJ); // wireOpacity json_t *wireOpacityJ = json_real(dynamic_cast(gScene)->toolbar->wireOpacitySlider->value); - json_object_set_new(root, "wireOpacity", wireOpacityJ); + json_object_set_new(rootJ, "wireOpacity", wireOpacityJ); // wireTension json_t *wireTensionJ = json_real(dynamic_cast(gScene)->toolbar->wireTensionSlider->value); - json_object_set_new(root, "wireTension", wireTensionJ); + json_object_set_new(rootJ, "wireTension", wireTensionJ); // modules json_t *modulesJ = json_array(); @@ -91,7 +91,7 @@ json_t *RackWidget::toJson() { json_t *moduleJ = moduleWidget->toJson(); json_array_append_new(modulesJ, moduleJ); } - json_object_set_new(root, "modules", modulesJ); + json_object_set_new(rootJ, "modules", modulesJ); // wires json_t *wires = json_array(); @@ -129,14 +129,14 @@ json_t *RackWidget::toJson() { } json_array_append_new(wires, wire); } - json_object_set_new(root, "wires", wires); + json_object_set_new(rootJ, "wires", wires); - return root; + return rootJ; } -void RackWidget::fromJson(json_t *root) { +void RackWidget::fromJson(json_t *rootJ) { // version - json_t *versionJ = json_object_get(root, "version"); + json_t *versionJ = json_object_get(rootJ, "version"); if (versionJ) { const char *version = json_string_value(versionJ); if (gApplicationVersion != version) @@ -144,18 +144,18 @@ void RackWidget::fromJson(json_t *root) { } // wireOpacity - json_t *wireOpacityJ = json_object_get(root, "wireOpacity"); + json_t *wireOpacityJ = json_object_get(rootJ, "wireOpacity"); if (wireOpacityJ) dynamic_cast(gScene)->toolbar->wireOpacitySlider->value = json_number_value(wireOpacityJ); // wireTension - json_t *wireTensionJ = json_object_get(root, "wireTension"); + json_t *wireTensionJ = json_object_get(rootJ, "wireTension"); if (wireTensionJ) dynamic_cast(gScene)->toolbar->wireTensionSlider->value = json_number_value(wireTensionJ); // modules std::map moduleWidgets; - json_t *modulesJ = json_object_get(root, "modules"); + json_t *modulesJ = json_object_get(rootJ, "modules"); if (!modulesJ) return; size_t moduleId; json_t *moduleJ; @@ -193,7 +193,7 @@ void RackWidget::fromJson(json_t *root) { } // wires - json_t *wiresJ = json_object_get(root, "wires"); + json_t *wiresJ = json_object_get(rootJ, "wires"); if (!wiresJ) return; size_t wireId; json_t *wireJ; @@ -320,12 +320,8 @@ struct AddModuleMenuItem : MenuItem { void RackWidget::onMouseDown(int button) { if (button == 1) { - // Get relative position of the click Vec modulePos = gMousePos.minus(getAbsolutePos()); - - MenuOverlay *overlay = new MenuOverlay(); - Menu *menu = new Menu(); - menu->box.pos = gMousePos; + Menu *menu = gScene->createMenu(); MenuLabel *menuLabel = new MenuLabel(); menuLabel->text = "Add Module"; @@ -339,8 +335,6 @@ void RackWidget::onMouseDown(int button) { menu->pushChild(item); } } - overlay->addChild(menu); - gScene->setOverlay(overlay); } } diff --git a/src/main.cpp b/src/main.cpp index 719a1096..e37d3d14 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,7 @@ #include "plugin.hpp" -#if defined(APPLE) +#if ARCH_MAC #include #include // for chdir and access #include // for dirname @@ -81,7 +81,7 @@ void fixCwd() { using namespace rack; int main() { -#if defined(APPLE) +#if ARCH_MAC fixCwd(); #endif diff --git a/src/plugin.cpp b/src/plugin.cpp index f9eb09c5..25054dc1 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -12,7 +12,7 @@ #include #include -#if defined(WINDOWS) +#if ARCH_WIN #include #include #include @@ -43,22 +43,22 @@ Plugin::~Plugin() { static int loadPlugin(std::string slug) { - #if defined(LINUX) + #if ARCH_LIN std::string path = "./plugins/" + slug + "/plugin.so"; - #elif defined(WINDOWS) + #elif ARCH_WIN std::string path = "./plugins/" + slug + "/plugin.dll"; - #elif defined(APPLE) + #elif ARCH_MAC std::string path = "./plugins/" + slug + "/plugin.dylib"; #endif // Load dynamic/shared library - #if defined(WINDOWS) + #if ARCH_WIN HINSTANCE handle = LoadLibrary(path.c_str()); if (!handle) { fprintf(stderr, "Failed to load library %s\n", path.c_str()); return -1; } - #elif defined(LINUX) || defined(APPLE) + #elif ARCH_LIN || ARCH_MAC void *handle = dlopen(path.c_str(), RTLD_NOW | RTLD_GLOBAL); if (!handle) { fprintf(stderr, "Failed to load library %s: %s\n", path.c_str(), dlerror()); @@ -69,9 +69,9 @@ static int loadPlugin(std::string slug) { // Call plugin init() function typedef Plugin *(*InitCallback)(); InitCallback initCallback; - #if defined(WINDOWS) + #if ARCH_WIN initCallback = (InitCallback) GetProcAddress(handle, "init"); - #elif defined(LINUX) || defined(APPLE) + #elif ARCH_LIN || ARCH_MAC initCallback = (InitCallback) dlsym(handle, "init"); #endif if (!initCallback) { @@ -173,10 +173,8 @@ static void extract_zip(const char *dir, int zipfd) { if (err) goto cleanup; int nameLen = strlen(zs.name); - char path[MAXPATHLEN] = "\0"; - strncat(path, dir, MAXPATHLEN); - strncat(path, "/", MAXPATHLEN); - strncat(path, zs.name, MAXPATHLEN); + char path[MAXPATHLEN]; + snprintf(path, sizeof(path), "%s/%s", dir, zs.name); if (zs.name[nameLen - 1] == '/') { err = mkdir(path, 0755); @@ -213,15 +211,15 @@ cleanup: void pluginOpenBrowser(std::string url) { // shell injection is possible, so make sure the URL is trusted -#if defined(LINUX) +#if ARCH_LIN std::string command = "xdg-open " + url; system(command.c_str()); #endif -#if defined(APPLE) +#if ARCH_MAC std::string command = "open " + url; system(command.c_str()); #endif -#if defined(WINDOWS) +#if ARCH_WIN ShellExecute(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL); #endif } @@ -245,14 +243,14 @@ void pluginLogIn(std::string email, std::string password) { if (res == CURLE_OK) { // Parse JSON response json_error_t error; - json_t *root = json_loads(resText.c_str(), 0, &error); - if (root) { - json_t *tokenJ = json_object_get(root, "token"); + json_t *rootJ = json_loads(resText.c_str(), 0, &error); + if (rootJ) { + json_t *tokenJ = json_object_get(rootJ, "token"); if (tokenJ) { // Set the token, which logs the user in token = json_string_value(tokenJ); } - json_decref(root); + json_decref(rootJ); } } } @@ -287,11 +285,8 @@ static void pluginRefreshPlugin(json_t *pluginJ) { downloadProgress = 0.0; const char *dir = "plugins"; - char path[MAXPATHLEN] = "\0"; - strncat(path, dir, MAXPATHLEN); - strncat(path, "/", MAXPATHLEN); - strncat(path, slug.c_str(), MAXPATHLEN); - strncat(path, ".zip", MAXPATHLEN); + char path[MAXPATHLEN]; + snprintf(path, sizeof(path), "%s/%s.zip", dir, slug.c_str()); int zip = open(path, O_RDWR | O_TRUNC | O_CREAT, 0644); // Download zip download_file(zip, url.c_str()); @@ -329,9 +324,9 @@ void pluginRefresh() { if (res == CURLE_OK) { // Parse JSON response json_error_t error; - json_t *root = json_loads(resText.c_str(), 0, &error); - if (root) { - json_t *pluginsJ = json_object_get(root, "plugins"); + json_t *rootJ = json_loads(resText.c_str(), 0, &error); + if (rootJ) { + json_t *pluginsJ = json_object_get(rootJ, "plugins"); if (pluginsJ) { // Iterate through each plugin object size_t index; @@ -340,7 +335,7 @@ void pluginRefresh() { pluginRefreshPlugin(pluginJ); } } - json_decref(root); + json_decref(rootJ); } } diff --git a/src/widgets/Scene.cpp b/src/widgets/Scene.cpp index e215b9eb..a5de343a 100644 --- a/src/widgets/Scene.cpp +++ b/src/widgets/Scene.cpp @@ -16,6 +16,18 @@ void Scene::setOverlay(Widget *w) { } } +Menu *Scene::createMenu() { + // Get relative position of the click + MenuOverlay *overlay = new MenuOverlay(); + Menu *menu = new Menu(); + menu->box.pos = gMousePos; + + overlay->addChild(menu); + gScene->setOverlay(overlay); + + return menu; +} + void Scene::step() { if (overlay) { overlay->box.size = box.size;