From 92951206b65ada191a172df167c89d9ae2e6dfad Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Tue, 16 Oct 2018 05:26:20 -0400 Subject: [PATCH] Rename WidgetState to event::Context, move color.hpp implementations to source --- include/WidgetState.hpp | 38 ------------ include/app/AudioWidget.hpp | 4 +- include/app/CircularShadow.hpp | 2 +- include/app/Component.hpp | 2 +- include/app/Knob.hpp | 4 +- include/app/LedDisplay.hpp | 2 +- include/app/LightWidget.hpp | 2 +- include/app/MidiWidget.hpp | 4 +- include/app/ModuleLightWidget.hpp | 4 +- include/app/ModuleWidget.hpp | 2 +- include/app/MomentarySwitch.hpp | 2 +- include/app/MultiLightWidget.hpp | 4 +- include/app/ParamWidget.hpp | 2 +- include/app/PluginManagerWidget.hpp | 2 +- include/app/Port.hpp | 2 +- include/app/RackRail.hpp | 2 +- include/app/RackScene.hpp | 2 +- include/app/RackScrollWidget.hpp | 2 +- include/app/RackWidget.hpp | 6 +- include/app/SVGButton.hpp | 2 +- include/app/SVGKnob.hpp | 4 +- include/app/SVGPanel.hpp | 2 +- include/app/SVGPort.hpp | 4 +- include/app/SVGSlider.hpp | 2 +- include/app/SVGSwitch.hpp | 2 +- include/app/ToggleSwitch.hpp | 2 +- include/app/Toolbar.hpp | 2 +- include/app/WireContainer.hpp | 4 +- include/app/WireWidget.hpp | 2 +- include/color.hpp | 85 ++++----------------------- include/event.hpp | 26 ++++++++ include/helpers.hpp | 4 +- include/rack.hpp | 2 - include/ui/Button.hpp | 2 +- include/ui/ChoiceButton.hpp | 2 +- include/ui/IconButton.hpp | 2 +- include/ui/Label.hpp | 2 +- include/ui/List.hpp | 2 +- include/ui/Menu.hpp | 2 +- include/ui/MenuEntry.hpp | 2 +- include/ui/MenuItem.hpp | 6 +- include/ui/MenuLabel.hpp | 2 +- include/ui/MenuOverlay.hpp | 2 +- include/ui/MenuSeparator.hpp | 2 +- include/ui/PasswordField.hpp | 4 +- include/ui/ProgressBar.hpp | 2 +- include/ui/RadioButton.hpp | 2 +- include/ui/Scene.hpp | 2 +- include/ui/ScrollWidget.hpp | 6 +- include/ui/SequentialLayout.hpp | 2 +- include/ui/Slider.hpp | 2 +- include/ui/TextField.hpp | 10 ++-- include/ui/Tooltip.hpp | 2 +- include/ui/TooltipOverlay.hpp | 2 +- include/ui/WindowOverlay.hpp | 2 +- include/ui/WindowWidget.hpp | 2 +- src/Core/MIDICCToCVInterface.cpp | 10 ++-- src/Core/MIDITriggerToCVInterface.cpp | 6 +- src/app/LedDisplay.cpp | 4 +- src/app/ModuleBrowser.cpp | 4 +- src/app/WireWidget.cpp | 4 +- src/app/app.cpp | 8 +-- src/asset.cpp | 4 +- src/color.cpp | 82 ++++++++++++++++++++++++++ src/{WidgetState.cpp => event.cpp} | 25 ++++---- src/widgets/Widget.cpp | 4 +- src/window.cpp | 24 ++++---- 67 files changed, 239 insertions(+), 231 deletions(-) delete mode 100644 include/WidgetState.hpp create mode 100644 src/color.cpp rename src/{WidgetState.cpp => event.cpp} (88%) diff --git a/include/WidgetState.hpp b/include/WidgetState.hpp deleted file mode 100644 index 4be926d8..00000000 --- a/include/WidgetState.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -#include "math.hpp" -#include - - -namespace rack { - - -struct Widget; - - -struct WidgetState { - Widget *rootWidget = NULL; - Widget *hoveredWidget = NULL; - Widget *draggedWidget = NULL; - Widget *dragHoveredWidget = NULL; - Widget *selectedWidget = NULL; - /** For middle-click dragging */ - Widget *scrollWidget = NULL; - - void handleButton(Vec pos, int button, int action, int mods); - void handleHover(Vec pos, Vec mouseDelta); - void handleLeave(); - void handleScroll(Vec pos, Vec scrollDelta); - void handleText(Vec pos, int codepoint); - void handleKey(Vec pos, int key, int scancode, int action, int mods); - void handleDrop(Vec pos, std::vector paths); - void handleZoom(); - /** Prepares a widget for deletion */ - void finalizeWidget(Widget *w); -}; - - -// TODO Move this elsewhere -extern WidgetState *gWidgetState; - - -} // namespace rack diff --git a/include/app/AudioWidget.hpp b/include/app/AudioWidget.hpp index 142793a5..ef885d1f 100644 --- a/include/app/AudioWidget.hpp +++ b/include/app/AudioWidget.hpp @@ -1,6 +1,6 @@ #pragma once -#include "common.hpp" -#include "LedDisplay.hpp" +#include "app/common.hpp" +#include "app/LedDisplay.hpp" namespace rack { diff --git a/include/app/CircularShadow.hpp b/include/app/CircularShadow.hpp index 68971f39..c3d189c3 100644 --- a/include/app/CircularShadow.hpp +++ b/include/app/CircularShadow.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/Component.hpp b/include/app/Component.hpp index ad9ed176..b499dd4e 100644 --- a/include/app/Component.hpp +++ b/include/app/Component.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/Knob.hpp b/include/app/Knob.hpp index 54c12862..8ea9c796 100644 --- a/include/app/Knob.hpp +++ b/include/app/Knob.hpp @@ -1,6 +1,6 @@ #pragma once -#include "common.hpp" -#include "ParamWidget.hpp" +#include "app/common.hpp" +#include "app/ParamWidget.hpp" namespace rack { diff --git a/include/app/LedDisplay.hpp b/include/app/LedDisplay.hpp index c4208b0a..30e5ebb0 100644 --- a/include/app/LedDisplay.hpp +++ b/include/app/LedDisplay.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/LightWidget.hpp b/include/app/LightWidget.hpp index 1aadc31c..536a6439 100644 --- a/include/app/LightWidget.hpp +++ b/include/app/LightWidget.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/MidiWidget.hpp b/include/app/MidiWidget.hpp index 57aab5f7..ec3c93b5 100644 --- a/include/app/MidiWidget.hpp +++ b/include/app/MidiWidget.hpp @@ -1,6 +1,6 @@ #pragma once -#include "common.hpp" -#include "LedDisplay.hpp" +#include "app/common.hpp" +#include "app/LedDisplay.hpp" namespace rack { diff --git a/include/app/ModuleLightWidget.hpp b/include/app/ModuleLightWidget.hpp index d6b53599..ff5f76aa 100644 --- a/include/app/ModuleLightWidget.hpp +++ b/include/app/ModuleLightWidget.hpp @@ -1,6 +1,6 @@ #pragma once -#include "common.hpp" -#include "MultiLightWidget.hpp" +#include "app/common.hpp" +#include "app/MultiLightWidget.hpp" namespace rack { diff --git a/include/app/ModuleWidget.hpp b/include/app/ModuleWidget.hpp index d6986da0..de272153 100644 --- a/include/app/ModuleWidget.hpp +++ b/include/app/ModuleWidget.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" #include "plugin.hpp" #include "engine.hpp" diff --git a/include/app/MomentarySwitch.hpp b/include/app/MomentarySwitch.hpp index 205ebdcf..a56e3075 100644 --- a/include/app/MomentarySwitch.hpp +++ b/include/app/MomentarySwitch.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/MultiLightWidget.hpp b/include/app/MultiLightWidget.hpp index 16f2536f..b995d615 100644 --- a/include/app/MultiLightWidget.hpp +++ b/include/app/MultiLightWidget.hpp @@ -1,6 +1,6 @@ #pragma once -#include "common.hpp" -#include "LightWidget.hpp" +#include "app/common.hpp" +#include "app/LightWidget.hpp" namespace rack { diff --git a/include/app/ParamWidget.hpp b/include/app/ParamWidget.hpp index 02452301..03ebad64 100644 --- a/include/app/ParamWidget.hpp +++ b/include/app/ParamWidget.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" #include "engine.hpp" diff --git a/include/app/PluginManagerWidget.hpp b/include/app/PluginManagerWidget.hpp index e6cb2614..72a1d313 100644 --- a/include/app/PluginManagerWidget.hpp +++ b/include/app/PluginManagerWidget.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/Port.hpp b/include/app/Port.hpp index 7e1b10e0..4390cdaa 100644 --- a/include/app/Port.hpp +++ b/include/app/Port.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/RackRail.hpp b/include/app/RackRail.hpp index b072a3ec..eb689c28 100644 --- a/include/app/RackRail.hpp +++ b/include/app/RackRail.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/RackScene.hpp b/include/app/RackScene.hpp index 97930322..b02bb44d 100644 --- a/include/app/RackScene.hpp +++ b/include/app/RackScene.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/RackScrollWidget.hpp b/include/app/RackScrollWidget.hpp index 5c01b095..60aa1876 100644 --- a/include/app/RackScrollWidget.hpp +++ b/include/app/RackScrollWidget.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/RackWidget.hpp b/include/app/RackWidget.hpp index cc6100ba..bf3319d3 100644 --- a/include/app/RackWidget.hpp +++ b/include/app/RackWidget.hpp @@ -1,7 +1,7 @@ #pragma once -#include "common.hpp" -#include "WireWidget.hpp" -#include "WireContainer.hpp" +#include "app/common.hpp" +#include "app/WireWidget.hpp" +#include "app/WireContainer.hpp" #include "engine.hpp" diff --git a/include/app/SVGButton.hpp b/include/app/SVGButton.hpp index 63b3633f..267fbd25 100644 --- a/include/app/SVGButton.hpp +++ b/include/app/SVGButton.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" #include "engine.hpp" diff --git a/include/app/SVGKnob.hpp b/include/app/SVGKnob.hpp index 7afe32e1..d73ef852 100644 --- a/include/app/SVGKnob.hpp +++ b/include/app/SVGKnob.hpp @@ -1,6 +1,6 @@ #pragma once -#include "common.hpp" -#include "CircularShadow.hpp" +#include "app/common.hpp" +#include "app/CircularShadow.hpp" namespace rack { diff --git a/include/app/SVGPanel.hpp b/include/app/SVGPanel.hpp index 624c94da..59bcf260 100644 --- a/include/app/SVGPanel.hpp +++ b/include/app/SVGPanel.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/SVGPort.hpp b/include/app/SVGPort.hpp index fd4d41c6..22738e98 100644 --- a/include/app/SVGPort.hpp +++ b/include/app/SVGPort.hpp @@ -1,6 +1,6 @@ #pragma once -#include "common.hpp" -#include "Port.hpp" +#include "app/common.hpp" +#include "app/Port.hpp" namespace rack { diff --git a/include/app/SVGSlider.hpp b/include/app/SVGSlider.hpp index 714ea9e2..e8e82fe6 100644 --- a/include/app/SVGSlider.hpp +++ b/include/app/SVGSlider.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/SVGSwitch.hpp b/include/app/SVGSwitch.hpp index 2052d459..5d056041 100644 --- a/include/app/SVGSwitch.hpp +++ b/include/app/SVGSwitch.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/ToggleSwitch.hpp b/include/app/ToggleSwitch.hpp index ba37f31b..7bc1c6ca 100644 --- a/include/app/ToggleSwitch.hpp +++ b/include/app/ToggleSwitch.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/Toolbar.hpp b/include/app/Toolbar.hpp index 9e6f914b..c1662aca 100644 --- a/include/app/Toolbar.hpp +++ b/include/app/Toolbar.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/app/WireContainer.hpp b/include/app/WireContainer.hpp index 4cb59323..bb81e8f9 100644 --- a/include/app/WireContainer.hpp +++ b/include/app/WireContainer.hpp @@ -1,6 +1,6 @@ #pragma once -#include "common.hpp" -#include "WireWidget.hpp" +#include "app/common.hpp" +#include "app/WireWidget.hpp" #include "engine.hpp" diff --git a/include/app/WireWidget.hpp b/include/app/WireWidget.hpp index 8647dc6c..c182ff02 100644 --- a/include/app/WireWidget.hpp +++ b/include/app/WireWidget.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "app/common.hpp" namespace rack { diff --git a/include/color.hpp b/include/color.hpp index 5203d29c..48616079 100644 --- a/include/color.hpp +++ b/include/color.hpp @@ -1,5 +1,4 @@ #pragma once - #include "common.hpp" #include "string.hpp" #include "nanovg.h" @@ -11,86 +10,26 @@ namespace color { static const NVGcolor BLACK_TRANSPARENT = nvgRGBA(0x00, 0x00, 0x00, 0x00); static const NVGcolor BLACK = nvgRGB(0x00, 0x00, 0x00); +static const NVGcolor WHITE_TRANSPARENT = nvgRGBA(0xff, 0xff, 0xff, 0x00); static const NVGcolor WHITE = nvgRGB(0xff, 0xff, 0xff); -static const NVGcolor WHITE_TRANSPARENT = nvgRGB(0xff, 0xff, 0xff); static const NVGcolor RED = nvgRGB(0xff, 0x00, 0x00); static const NVGcolor GREEN = nvgRGB(0x00, 0xff, 0x00); static const NVGcolor BLUE = nvgRGB(0x00, 0x00, 0xff); -static const NVGcolor YELLOW = nvgRGB(0xff, 0xff, 0x00); -static const NVGcolor MAGENTA = nvgRGB(0xff, 0x00, 0xff); static const NVGcolor CYAN = nvgRGB(0x00, 0xff, 0xff); +static const NVGcolor MAGENTA = nvgRGB(0xff, 0x00, 0xff); +static const NVGcolor YELLOW = nvgRGB(0xff, 0xff, 0x00); -inline NVGcolor clip(NVGcolor a) { - for (int i = 0; i < 4; i++) - a.rgba[i] = clamp(a.rgba[i], 0.f, 1.f); - return a; -} - -inline NVGcolor minus(NVGcolor a, NVGcolor b) { - for (int i = 0; i < 3; i++) - a.rgba[i] -= b.rgba[i]; - return a; -} - -inline NVGcolor plus(NVGcolor a, NVGcolor b) { - for (int i = 0; i < 3; i++) - a.rgba[i] += b.rgba[i]; - return a; -} - -inline NVGcolor mult(NVGcolor a, NVGcolor b) { - for (int i = 0; i < 3; i++) - a.rgba[i] *= b.rgba[i]; - return a; -} - -inline NVGcolor mult(NVGcolor a, float x) { - for (int i = 0; i < 3; i++) - a.rgba[i] *= x; - return a; -} - +NVGcolor clip(NVGcolor a); +NVGcolor minus(NVGcolor a, NVGcolor b); +NVGcolor plus(NVGcolor a, NVGcolor b); +NVGcolor mult(NVGcolor a, NVGcolor b); +NVGcolor mult(NVGcolor a, float x); /** Screen blending with alpha compositing */ -inline NVGcolor screen(NVGcolor a, NVGcolor b) { - if (a.a == 0.0) - return b; - if (b.a == 0.0) - return a; - - a = mult(a, a.a); - b = mult(b, b.a); - NVGcolor c = minus(plus(a, b), mult(a, b)); - c.a = a.a + b.a - a.a * b.a; - c = mult(c, 1.f / c.a); - c = clip(c); - return c; -} - -inline NVGcolor alpha(NVGcolor a, float alpha) { - a.a *= alpha; - return a; -} - -inline NVGcolor fromHexString(std::string s) { - uint8_t r = 0; - uint8_t g = 0; - uint8_t b = 0; - uint8_t a = 255; - sscanf(s.c_str(), "#%2hhx%2hhx%2hhx%2hhx", &r, &g, &b, &a); - return nvgRGBA(r, g, b, a); -} - -inline std::string toHexString(NVGcolor c) { - uint8_t r = std::round(c.r * 255); - uint8_t g = std::round(c.g * 255); - uint8_t b = std::round(c.b * 255); - uint8_t a = std::round(c.a * 255); - if (a == 255) - return string::f("#%02x%02x%02x", r, g, b); - else - return string::f("#%02x%02x%02x%02x", r, g, b, a); -} +NVGcolor screen(NVGcolor a, NVGcolor b); +NVGcolor alpha(NVGcolor a, float alpha); +NVGcolor fromHexString(std::string s); +std::string toHexString(NVGcolor c); } // namespace color diff --git a/include/event.hpp b/include/event.hpp index 7922ba4e..a05a5fdf 100644 --- a/include/event.hpp +++ b/include/event.hpp @@ -207,5 +207,31 @@ struct Zoom : Event { }; +struct Context { + Widget *rootWidget = NULL; + Widget *hoveredWidget = NULL; + Widget *draggedWidget = NULL; + Widget *dragHoveredWidget = NULL; + Widget *selectedWidget = NULL; + /** For middle-click dragging */ + Widget *scrollWidget = NULL; + + void handleButton(Vec pos, int button, int action, int mods); + void handleHover(Vec pos, Vec mouseDelta); + void handleLeave(); + void handleScroll(Vec pos, Vec scrollDelta); + void handleText(Vec pos, int codepoint); + void handleKey(Vec pos, int key, int scancode, int action, int mods); + void handleDrop(Vec pos, std::vector paths); + void handleZoom(); + /** Prepares a widget for deletion */ + void finalizeWidget(Widget *w); +}; + + +// TODO Move this into a global context class +extern Context *gContext; + + } // namespace event } // namespace rack diff --git a/include/helpers.hpp b/include/helpers.hpp index ed291af1..5132dad4 100644 --- a/include/helpers.hpp +++ b/include/helpers.hpp @@ -1,7 +1,7 @@ #include "plugin.hpp" #include "engine.hpp" #include "app.hpp" -#include "WidgetState.hpp" +#include "event.hpp" namespace rack { @@ -145,7 +145,7 @@ inline Menu *createMenu() { MenuOverlay *menuOverlay = new MenuOverlay; menuOverlay->addChild(o); - gWidgetState->rootWidget->addChild(menuOverlay); + event::gContext->rootWidget->addChild(menuOverlay); return o; } diff --git a/include/rack.hpp b/include/rack.hpp index 397bf06e..7b2676a3 100644 --- a/include/rack.hpp +++ b/include/rack.hpp @@ -1,6 +1,4 @@ #pragma once - -// Include headers that plugins will likely use, for convenience #include "common.hpp" #include "math.hpp" #include "string.hpp" diff --git a/include/ui/Button.hpp b/include/ui/Button.hpp index a6a3721c..04d3c007 100644 --- a/include/ui/Button.hpp +++ b/include/ui/Button.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/ChoiceButton.hpp b/include/ui/ChoiceButton.hpp index 1be9533f..8012c6e2 100644 --- a/include/ui/ChoiceButton.hpp +++ b/include/ui/ChoiceButton.hpp @@ -1,5 +1,5 @@ #pragma once -#include "Button.hpp" +#include "ui/Button.hpp" namespace rack { diff --git a/include/ui/IconButton.hpp b/include/ui/IconButton.hpp index f2f62fb6..f8f42d50 100644 --- a/include/ui/IconButton.hpp +++ b/include/ui/IconButton.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/Label.hpp b/include/ui/Label.hpp index 1bd39455..f427c587 100644 --- a/include/ui/Label.hpp +++ b/include/ui/Label.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/List.hpp b/include/ui/List.hpp index 74d79189..76ffee03 100644 --- a/include/ui/List.hpp +++ b/include/ui/List.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/Menu.hpp b/include/ui/Menu.hpp index 6935d4d6..581abbf6 100644 --- a/include/ui/Menu.hpp +++ b/include/ui/Menu.hpp @@ -1,5 +1,5 @@ #pragma once -#include "MenuEntry.hpp" +#include "ui/MenuEntry.hpp" namespace rack { diff --git a/include/ui/MenuEntry.hpp b/include/ui/MenuEntry.hpp index 0521eb51..911da47a 100644 --- a/include/ui/MenuEntry.hpp +++ b/include/ui/MenuEntry.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/MenuItem.hpp b/include/ui/MenuItem.hpp index 1fa26de1..75f84c2e 100644 --- a/include/ui/MenuItem.hpp +++ b/include/ui/MenuItem.hpp @@ -1,6 +1,6 @@ #pragma once -#include "common.hpp" -#include "MenuOverlay.hpp" +#include "ui/common.hpp" +#include "ui/MenuOverlay.hpp" namespace rack { @@ -15,7 +15,7 @@ struct MenuItem : MenuEntry { void draw(NVGcontext *vg) override { // Get state - BNDwidgetState state = (gWidgetState->hoveredWidget == this) ? BND_HOVER : BND_DEFAULT; + BNDwidgetState state = (event::gContext->hoveredWidget == this) ? BND_HOVER : BND_DEFAULT; Menu *parentMenu = dynamic_cast(parent); if (parentMenu && parentMenu->activeEntry == this) { state = BND_ACTIVE; diff --git a/include/ui/MenuLabel.hpp b/include/ui/MenuLabel.hpp index ee6b79a7..f5c0fc58 100644 --- a/include/ui/MenuLabel.hpp +++ b/include/ui/MenuLabel.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/MenuOverlay.hpp b/include/ui/MenuOverlay.hpp index b66a83f5..295fcca1 100644 --- a/include/ui/MenuOverlay.hpp +++ b/include/ui/MenuOverlay.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/MenuSeparator.hpp b/include/ui/MenuSeparator.hpp index 8c46d5e4..4406ba42 100644 --- a/include/ui/MenuSeparator.hpp +++ b/include/ui/MenuSeparator.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/PasswordField.hpp b/include/ui/PasswordField.hpp index 9e1243a9..8cf17e73 100644 --- a/include/ui/PasswordField.hpp +++ b/include/ui/PasswordField.hpp @@ -1,6 +1,6 @@ #pragma once -#include "common.hpp" -#include "TextField.hpp" +#include "ui/common.hpp" +#include "ui/TextField.hpp" namespace rack { diff --git a/include/ui/ProgressBar.hpp b/include/ui/ProgressBar.hpp index 20e73fe6..11e58a4f 100644 --- a/include/ui/ProgressBar.hpp +++ b/include/ui/ProgressBar.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/RadioButton.hpp b/include/ui/RadioButton.hpp index c4cb4d42..bca5502a 100644 --- a/include/ui/RadioButton.hpp +++ b/include/ui/RadioButton.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/Scene.hpp b/include/ui/Scene.hpp index d25e27e8..0779144a 100644 --- a/include/ui/Scene.hpp +++ b/include/ui/Scene.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/ScrollWidget.hpp b/include/ui/ScrollWidget.hpp index 4628dc5f..5e45a4aa 100644 --- a/include/ui/ScrollWidget.hpp +++ b/include/ui/ScrollWidget.hpp @@ -1,6 +1,6 @@ #pragma once -#include "common.hpp" -#include "WidgetState.hpp" +#include "ui/common.hpp" +#include "event.hpp" namespace rack { @@ -110,7 +110,7 @@ struct ScrollWidget : OpaqueWidget { void onHover(event::Hover &e) override { // Scroll with arrow keys - if (!gWidgetState->selectedWidget) { + if (!event::gContext->selectedWidget) { float arrowSpeed = 30.0; if (windowIsShiftPressed() && windowIsModPressed()) arrowSpeed /= 16.0; diff --git a/include/ui/SequentialLayout.hpp b/include/ui/SequentialLayout.hpp index f7391fc5..5b0103fc 100644 --- a/include/ui/SequentialLayout.hpp +++ b/include/ui/SequentialLayout.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/Slider.hpp b/include/ui/Slider.hpp index 533552e1..ac27e66c 100644 --- a/include/ui/Slider.hpp +++ b/include/ui/Slider.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/TextField.hpp b/include/ui/TextField.hpp index 2d4e0860..ba9c7107 100644 --- a/include/ui/TextField.hpp +++ b/include/ui/TextField.hpp @@ -1,6 +1,6 @@ #pragma once -#include "common.hpp" -#include "WidgetState.hpp" +#include "ui/common.hpp" +#include "event.hpp" namespace rack { @@ -25,9 +25,9 @@ struct TextField : OpaqueWidget { nvgScissor(vg, 0, 0, box.size.x, box.size.y); BNDwidgetState state; - if (this == gWidgetState->selectedWidget) + if (this == event::gContext->selectedWidget) state = BND_ACTIVE; - else if (this == gWidgetState->hoveredWidget) + else if (this == event::gContext->hoveredWidget) state = BND_HOVER; else state = BND_DEFAULT; @@ -51,7 +51,7 @@ struct TextField : OpaqueWidget { } void onHover(event::Hover &e) override { - if (this == gWidgetState->draggedWidget) { + if (this == event::gContext->draggedWidget) { int pos = getTextPosition(e.pos); if (pos != selection) { cursor = pos; diff --git a/include/ui/Tooltip.hpp b/include/ui/Tooltip.hpp index d04c8412..0162c56f 100644 --- a/include/ui/Tooltip.hpp +++ b/include/ui/Tooltip.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/TooltipOverlay.hpp b/include/ui/TooltipOverlay.hpp index 9835f9d2..6ace069d 100644 --- a/include/ui/TooltipOverlay.hpp +++ b/include/ui/TooltipOverlay.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/WindowOverlay.hpp b/include/ui/WindowOverlay.hpp index eb763904..ee20cf55 100644 --- a/include/ui/WindowOverlay.hpp +++ b/include/ui/WindowOverlay.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/include/ui/WindowWidget.hpp b/include/ui/WindowWidget.hpp index 14ca61fb..991a6be4 100644 --- a/include/ui/WindowWidget.hpp +++ b/include/ui/WindowWidget.hpp @@ -1,5 +1,5 @@ #pragma once -#include "common.hpp" +#include "ui/common.hpp" namespace rack { diff --git a/src/Core/MIDICCToCVInterface.cpp b/src/Core/MIDICCToCVInterface.cpp index 7eb742b6..d649faab 100644 --- a/src/Core/MIDICCToCVInterface.cpp +++ b/src/Core/MIDICCToCVInterface.cpp @@ -2,7 +2,7 @@ #include "midi.hpp" #include "dsp/filter.hpp" #include "window.hpp" -#include "WidgetState.hpp" +#include "event.hpp" struct MIDICCToCVInterface : Module { @@ -130,8 +130,8 @@ struct MidiCcChoice : GridChoice { else { text = string::f("%d", module->learnedCcs[id]); color.a = 1.0; - if (gWidgetState->selectedWidget == this) - gWidgetState->selectedWidget = NULL; + if (event::gContext->selectedWidget == this) + event::gContext->selectedWidget = NULL; } } @@ -159,11 +159,11 @@ struct MidiCcChoice : GridChoice { } void onSelectKey(event::SelectKey &e) override { - if (gWidgetState->selectedWidget == this) { + if (event::gContext->selectedWidget == this) { if (e.key == GLFW_KEY_ENTER || e.key == GLFW_KEY_KP_ENTER) { event::Deselect eDeselect; onDeselect(eDeselect); - gWidgetState->selectedWidget = NULL; + event::gContext->selectedWidget = NULL; e.target = this; } } diff --git a/src/Core/MIDITriggerToCVInterface.cpp b/src/Core/MIDITriggerToCVInterface.cpp index 1a2fbc0a..80d3e29f 100644 --- a/src/Core/MIDITriggerToCVInterface.cpp +++ b/src/Core/MIDITriggerToCVInterface.cpp @@ -1,6 +1,6 @@ #include "Core.hpp" #include "midi.hpp" -#include "WidgetState.hpp" +#include "event.hpp" struct MIDITriggerToCVInterface : Module { @@ -172,8 +172,8 @@ struct MidiTrigChoice : GridChoice { text = string::f("%s%d", noteNames[semi], oct); color.a = 1.0; - if (gWidgetState->selectedWidget == this) - gWidgetState->selectedWidget = NULL; + if (event::gContext->selectedWidget == this) + event::gContext->selectedWidget = NULL; } } diff --git a/src/app/LedDisplay.cpp b/src/app/LedDisplay.cpp index 9ab4a719..5df111d8 100644 --- a/src/app/LedDisplay.cpp +++ b/src/app/LedDisplay.cpp @@ -1,7 +1,7 @@ #include "app.hpp" #include "asset.hpp" #include "window.hpp" -#include "WidgetState.hpp" +#include "event.hpp" namespace rack { @@ -85,7 +85,7 @@ void LedDisplayTextField::draw(NVGcontext *vg) { NVGcolor highlightColor = color; highlightColor.a = 0.5; int begin = std::min(cursor, selection); - int end = (this == gWidgetState->selectedWidget) ? std::max(cursor, selection) : -1; + int end = (this == event::gContext->selectedWidget) ? std::max(cursor, selection) : -1; bndIconLabelCaret(vg, textOffset.x, textOffset.y, box.size.x - 2*textOffset.x, box.size.y - 2*textOffset.y, -1, color, 12, text.c_str(), highlightColor, begin, end); diff --git a/src/app/ModuleBrowser.cpp b/src/app/ModuleBrowser.cpp index 9cecb728..bd73d591 100644 --- a/src/app/ModuleBrowser.cpp +++ b/src/app/ModuleBrowser.cpp @@ -2,7 +2,7 @@ #include "plugin.hpp" #include "window.hpp" #include "helpers.hpp" -#include "WidgetState.hpp" +#include "event.hpp" #include #include @@ -430,7 +430,7 @@ struct ModuleBrowser : OpaqueWidget { moduleScroll->box.size.y = std::min(box.size.y - moduleScroll->box.pos.y, moduleList->box.size.y); box.size.y = std::min(box.size.y, moduleScroll->box.getBottomRight().y); - gWidgetState->selectedWidget = searchField; + event::gContext->selectedWidget = searchField; Widget::step(); } }; diff --git a/src/app/WireWidget.cpp b/src/app/WireWidget.cpp index 0f3e007a..ebec5340 100644 --- a/src/app/WireWidget.cpp +++ b/src/app/WireWidget.cpp @@ -2,7 +2,7 @@ #include "engine.hpp" #include "componentlibrary.hpp" #include "window.hpp" -#include "WidgetState.hpp" +#include "event.hpp" namespace rack { @@ -172,7 +172,7 @@ void WireWidget::draw(NVGcontext *vg) { opacity = 1.0; } else { - Port *hoveredPort = dynamic_cast(gWidgetState->hoveredWidget); + Port *hoveredPort = dynamic_cast(event::gContext->hoveredWidget); if (hoveredPort && (hoveredPort == outputPort || hoveredPort == inputPort)) opacity = 1.0; } diff --git a/src/app/app.cpp b/src/app/app.cpp index c4f19e81..c87c4089 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -38,8 +38,8 @@ static void checkVersion() { void appInit(bool devMode) { gRackScene = new RackScene; - gWidgetState = new WidgetState; - gWidgetState->rootWidget = gRackScene; + event::gContext = new event::Context; + event::gContext->rootWidget = gRackScene; // Request latest version from server if (!devMode && gCheckVersion) { @@ -53,8 +53,8 @@ void appDestroy() { delete gRackScene; gRackScene = NULL; - delete gWidgetState; - gWidgetState = NULL; + delete event::gContext; + event::gContext = NULL; } diff --git a/src/asset.cpp b/src/asset.cpp index acc89ed0..c0e6b0a5 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -15,8 +15,8 @@ #include #endif -#include "rack.hpp" -#include "osdialog.h" +#include "asset.hpp" +#include "system.hpp" namespace rack { diff --git a/src/color.cpp b/src/color.cpp new file mode 100644 index 00000000..eb18daeb --- /dev/null +++ b/src/color.cpp @@ -0,0 +1,82 @@ +#include "color.hpp" +#include "math.hpp" + + +namespace rack { +namespace color { + + +NVGcolor clip(NVGcolor a) { + for (int i = 0; i < 4; i++) + a.rgba[i] = clamp(a.rgba[i], 0.f, 1.f); + return a; +} + +NVGcolor minus(NVGcolor a, NVGcolor b) { + for (int i = 0; i < 3; i++) + a.rgba[i] -= b.rgba[i]; + return a; +} + +NVGcolor plus(NVGcolor a, NVGcolor b) { + for (int i = 0; i < 3; i++) + a.rgba[i] += b.rgba[i]; + return a; +} + +NVGcolor mult(NVGcolor a, NVGcolor b) { + for (int i = 0; i < 3; i++) + a.rgba[i] *= b.rgba[i]; + return a; +} + +NVGcolor mult(NVGcolor a, float x) { + for (int i = 0; i < 3; i++) + a.rgba[i] *= x; + return a; +} + +/** Screen blending with alpha compositing */ +NVGcolor screen(NVGcolor a, NVGcolor b) { + if (a.a == 0.f) + return b; + if (b.a == 0.f) + return a; + + a = mult(a, a.a); + b = mult(b, b.a); + NVGcolor c = minus(plus(a, b), mult(a, b)); + c.a = a.a + b.a - a.a * b.a; + c = mult(c, 1.f / c.a); + c = clip(c); + return c; +} + +NVGcolor alpha(NVGcolor a, float alpha) { + a.a *= alpha; + return a; +} + +NVGcolor fromHexString(std::string s) { + uint8_t r = 0; + uint8_t g = 0; + uint8_t b = 0; + uint8_t a = 255; + sscanf(s.c_str(), "#%2hhx%2hhx%2hhx%2hhx", &r, &g, &b, &a); + return nvgRGBA(r, g, b, a); +} + +std::string toHexString(NVGcolor c) { + uint8_t r = std::round(c.r * 255); + uint8_t g = std::round(c.g * 255); + uint8_t b = std::round(c.b * 255); + uint8_t a = std::round(c.a * 255); + if (a == 255) + return string::f("#%02x%02x%02x", r, g, b); + else + return string::f("#%02x%02x%02x%02x", r, g, b, a); +} + + +} // namespace network +} // namespace rack diff --git a/src/WidgetState.cpp b/src/event.cpp similarity index 88% rename from src/WidgetState.cpp rename to src/event.cpp index f0127556..01d3783d 100644 --- a/src/WidgetState.cpp +++ b/src/event.cpp @@ -1,13 +1,13 @@ -#include "WidgetState.hpp" #include "event.hpp" -#include "widgets.hpp" +#include "widgets/Widget.hpp" #include "logger.hpp" namespace rack { +namespace event { -void WidgetState::handleButton(Vec pos, int button, int action, int mods) { +void Context::handleButton(Vec pos, int button, int action, int mods) { // event::Button event::Button eButton; eButton.pos = pos; @@ -74,7 +74,7 @@ void WidgetState::handleButton(Vec pos, int button, int action, int mods) { } -void WidgetState::handleHover(Vec pos, Vec mouseDelta) { +void Context::handleHover(Vec pos, Vec mouseDelta) { if (draggedWidget) { // event::DragMove event::DragMove eDragMove; @@ -139,7 +139,7 @@ void WidgetState::handleHover(Vec pos, Vec mouseDelta) { } } -void WidgetState::handleLeave() { +void Context::handleLeave() { if (hoveredWidget) { // event::Leave event::Leave eLeave; @@ -148,7 +148,7 @@ void WidgetState::handleLeave() { hoveredWidget = NULL; } -void WidgetState::handleScroll(Vec pos, Vec scrollDelta) { +void Context::handleScroll(Vec pos, Vec scrollDelta) { // event::HoverScroll event::HoverScroll eHoverScroll; eHoverScroll.pos = pos; @@ -156,7 +156,7 @@ void WidgetState::handleScroll(Vec pos, Vec scrollDelta) { rootWidget->onHoverScroll(eHoverScroll); } -void WidgetState::handleDrop(Vec pos, std::vector paths) { +void Context::handleDrop(Vec pos, std::vector paths) { // event::PathDrop event::PathDrop ePathDrop; ePathDrop.pos = pos; @@ -164,7 +164,7 @@ void WidgetState::handleDrop(Vec pos, std::vector paths) { rootWidget->onPathDrop(ePathDrop); } -void WidgetState::handleText(Vec pos, int codepoint) { +void Context::handleText(Vec pos, int codepoint) { if (selectedWidget) { // event::SelectText event::SelectText eSelectText; @@ -181,7 +181,7 @@ void WidgetState::handleText(Vec pos, int codepoint) { rootWidget->onHoverText(eHoverText); } -void WidgetState::handleKey(Vec pos, int key, int scancode, int action, int mods) { +void Context::handleKey(Vec pos, int key, int scancode, int action, int mods) { if (selectedWidget) { // event::SelectKey event::SelectKey eSelectKey; @@ -204,7 +204,7 @@ void WidgetState::handleKey(Vec pos, int key, int scancode, int action, int mods rootWidget->onHoverKey(eHoverKey); } -void WidgetState::finalizeWidget(Widget *w) { +void Context::finalizeWidget(Widget *w) { if (hoveredWidget == w) hoveredWidget = NULL; if (draggedWidget == w) draggedWidget = NULL; if (dragHoveredWidget == w) dragHoveredWidget = NULL; @@ -212,7 +212,7 @@ void WidgetState::finalizeWidget(Widget *w) { if (scrollWidget == w) scrollWidget = NULL; } -void WidgetState::handleZoom() { +void Context::handleZoom() { // event::Zoom event::Zoom eZoom; rootWidget->onZoom(eZoom); @@ -220,7 +220,8 @@ void WidgetState::handleZoom() { // TODO Move this elsewhere -WidgetState *gWidgetState = NULL; +Context *gContext = NULL; +} // namespace event } // namespace rack diff --git a/src/widgets/Widget.cpp b/src/widgets/Widget.cpp index b7e7e929..1e10b198 100644 --- a/src/widgets/Widget.cpp +++ b/src/widgets/Widget.cpp @@ -1,6 +1,6 @@ #include "widgets.hpp" #include "app.hpp" -#include "WidgetState.hpp" +#include "event.hpp" #include @@ -9,7 +9,7 @@ namespace rack { Widget::~Widget() { // You should only delete orphaned widgets assert(!parent); - gWidgetState->finalizeWidget(this); + event::gContext->finalizeWidget(this); clearChildren(); } diff --git a/src/window.cpp b/src/window.cpp index 7eb4faec..565ea27d 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -13,7 +13,7 @@ #include "rack.hpp" #include "keyboard.hpp" #include "gamepad.hpp" -#include "WidgetState.hpp" +#include "event.hpp" #define NANOVG_GL2_IMPLEMENTATION 1 // #define NANOVG_GL3_IMPLEMENTATION 1 @@ -60,7 +60,7 @@ static void mouseButtonCallback(GLFWwindow *window, int button, int action, int } #endif - gWidgetState->handleButton(gMousePos, button, action, mods); + event::gContext->handleButton(gMousePos, button, action, mods); } struct MouseButtonArguments { @@ -107,12 +107,12 @@ void cursorPosCallback(GLFWwindow* window, double xpos, double ypos) { gMousePos = mousePos; - gWidgetState->handleHover(mousePos, mouseDelta); + event::gContext->handleHover(mousePos, mouseDelta); } void cursorEnterCallback(GLFWwindow* window, int entered) { if (!entered) { - gWidgetState->handleLeave(); + event::gContext->handleLeave(); } } @@ -124,15 +124,15 @@ void scrollCallback(GLFWwindow *window, double x, double y) { #endif scrollDelta = scrollDelta.mult(50.0); - gWidgetState->handleScroll(gMousePos, scrollDelta); + event::gContext->handleScroll(gMousePos, scrollDelta); } void charCallback(GLFWwindow *window, unsigned int codepoint) { - gWidgetState->handleText(gMousePos, codepoint); + event::gContext->handleText(gMousePos, codepoint); } void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { - gWidgetState->handleKey(gMousePos, key, scancode, action, mods); + event::gContext->handleKey(gMousePos, key, scancode, action, mods); // Keyboard MIDI driver if (!(mods & (GLFW_MOD_SHIFT | GLFW_MOD_CONTROL | GLFW_MOD_ALT | GLFW_MOD_SUPER))) { @@ -150,7 +150,7 @@ void dropCallback(GLFWwindow *window, int count, const char **paths) { for (int i = 0; i < count; i++) { pathsVec.push_back(paths[i]); } - gWidgetState->handleDrop(gMousePos, pathsVec); + event::gContext->handleDrop(gMousePos, pathsVec); } void errorCallback(int error, const char *description) { @@ -166,7 +166,7 @@ void renderGui() { nvgReset(gVg); nvgScale(gVg, gPixelRatio, gPixelRatio); - gWidgetState->rootWidget->draw(gVg); + event::gContext->rootWidget->draw(gVg); glViewport(0, 0, width, height); glClearColor(0.0, 0.0, 0.0, 1.0); @@ -319,7 +319,7 @@ void windowRun() { glfwGetWindowContentScale(gWindow, &pixelRatio, NULL); pixelRatio = roundf(pixelRatio); if (pixelRatio != gPixelRatio) { - gWidgetState->handleZoom(); + event::gContext->handleZoom(); gPixelRatio = pixelRatio; } @@ -330,10 +330,10 @@ void windowRun() { glfwGetWindowSize(gWindow, &windowWidth, &windowHeight); gWindowRatio = (float)width / windowWidth; - gWidgetState->rootWidget->box.size = Vec(width, height).div(gPixelRatio); + event::gContext->rootWidget->box.size = Vec(width, height).div(gPixelRatio); // Step scene - gWidgetState->rootWidget->step(); + event::gContext->rootWidget->step(); // Render bool visible = glfwGetWindowAttrib(gWindow, GLFW_VISIBLE) && !glfwGetWindowAttrib(gWindow, GLFW_ICONIFIED);