From 681018322c6cb9a5c54e0bc0e71d1507de99c866 Mon Sep 17 00:00:00 2001 From: Tobias Hienzsch Date: Tue, 24 Oct 2023 05:13:52 +0200 Subject: [PATCH] [viewer] Add dynamic background colour to script canvas --- tools/jml-viewer/Viewer/LuaScriptViewer.cpp | 22 +++++++++++++++++++-- tools/jml-viewer/Viewer/LuaScriptViewer.hpp | 6 ++++++ tools/jml-viewer/Viewer/ScriptViewport.cpp | 9 +++++++-- tools/jml-viewer/Viewer/ScriptViewport.hpp | 4 +++- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/tools/jml-viewer/Viewer/LuaScriptViewer.cpp b/tools/jml-viewer/Viewer/LuaScriptViewer.cpp index 253ea9d..39ea579 100644 --- a/tools/jml-viewer/Viewer/LuaScriptViewer.cpp +++ b/tools/jml-viewer/Viewer/LuaScriptViewer.cpp @@ -9,8 +9,16 @@ constexpr auto const* DefaultScriptPath = R"(~/Developer/neo-sonar/jml/examples/ LuaScriptViewer::LuaScriptViewer() : _scriptFile(DefaultScriptPath) { + _viewportColour.setValue(toVar(getSchemeWidgetBackgroundColour())); + _viewportProperties.addProperties(juce::Array{ + std::make_unique(_viewportColour, "Background").release(), + }); + + _viewportColour.addListener(this); + addAndMakeVisible(_viewport); addAndMakeVisible(_componentTree); + addAndMakeVisible(_viewportProperties); startTimer(2000); } @@ -64,8 +72,11 @@ auto LuaScriptViewer::paint(juce::Graphics& g) -> void auto LuaScriptViewer::resized() -> void { - auto area = getLocalBounds(); - _componentTree.setBounds(area.removeFromRight(area.proportionOfWidth(0.2))); + auto area = getLocalBounds(); + auto treeArea = area.removeFromRight(area.proportionOfWidth(0.2)); + + _viewportProperties.setBounds(treeArea.removeFromBottom(treeArea.proportionOfHeight(0.15))); + _componentTree.setBounds(treeArea); _viewport.setBounds(area); } @@ -77,6 +88,13 @@ auto LuaScriptViewer::timerCallback() -> void _lua->state.collect_garbage(); } +auto LuaScriptViewer::valueChanged(juce::Value& value) -> void +{ + if (value.refersToSameSourceAs(_viewportColour)) { + _viewport.setBackgroundColour(fromVar(_viewportColour)); + } +} + auto LuaScriptViewer::reloadLuaState() -> void { _lua = std::make_unique(); diff --git a/tools/jml-viewer/Viewer/LuaScriptViewer.hpp b/tools/jml-viewer/Viewer/LuaScriptViewer.hpp index 655e28a..614c04f 100644 --- a/tools/jml-viewer/Viewer/LuaScriptViewer.hpp +++ b/tools/jml-viewer/Viewer/LuaScriptViewer.hpp @@ -13,6 +13,7 @@ namespace jml::viewer { struct LuaScriptViewer final : juce::Component , juce::Timer + , juce::Value::Listener { LuaScriptViewer(); ~LuaScriptViewer() override = default; @@ -24,6 +25,7 @@ struct LuaScriptViewer final auto paint(juce::Graphics& g) -> void override; auto resized() -> void override; auto timerCallback() -> void override; + auto valueChanged(juce::Value& value) -> void override; private: struct LuaState @@ -37,8 +39,12 @@ struct LuaScriptViewer final static auto handleLuaError(sol::error const& error) -> void; std::unique_ptr _lua; + ScriptViewport _viewport; ComponentTree _componentTree; + juce::PropertyPanel _viewportProperties; + + juce::Value _viewportColour; juce::File _scriptFile; std::unique_ptr _fileListener; diff --git a/tools/jml-viewer/Viewer/ScriptViewport.cpp b/tools/jml-viewer/Viewer/ScriptViewport.cpp index 5aab1c5..8b4c347 100644 --- a/tools/jml-viewer/Viewer/ScriptViewport.cpp +++ b/tools/jml-viewer/Viewer/ScriptViewport.cpp @@ -4,6 +4,8 @@ namespace jml::viewer { +ScriptViewport::ScriptViewport() : _background{getSchemeWidgetBackgroundColour()} {} + auto ScriptViewport::setViewedComponent(juce::Component* component) -> void { if (_component != nullptr) { @@ -15,11 +17,14 @@ auto ScriptViewport::setViewedComponent(juce::Component* component) -> void } } -auto ScriptViewport::paint(juce::Graphics& g) -> void +auto ScriptViewport::setBackgroundColour(juce::Colour colour) -> void { - g.fillAll(getSchemeWidgetBackgroundColour()); + _background = colour; + repaint(); } +auto ScriptViewport::paint(juce::Graphics& g) -> void { g.fillAll(_background); } + auto ScriptViewport::resized() -> void { if (_component == nullptr) { diff --git a/tools/jml-viewer/Viewer/ScriptViewport.hpp b/tools/jml-viewer/Viewer/ScriptViewport.hpp index 8d87019..d7b0b17 100644 --- a/tools/jml-viewer/Viewer/ScriptViewport.hpp +++ b/tools/jml-viewer/Viewer/ScriptViewport.hpp @@ -6,16 +6,18 @@ namespace jml::viewer { struct ScriptViewport final : juce::Component { - ScriptViewport() = default; + ScriptViewport(); ~ScriptViewport() override = default; auto setViewedComponent(juce::Component* component) -> void; + auto setBackgroundColour(juce::Colour colour) -> void; auto paint(juce::Graphics& g) -> void override; auto resized() -> void override; private: juce::Component* _component{nullptr}; + juce::Colour _background; }; } // namespace jml::viewer