Skip to content

Commit

Permalink
[viewer] Add dynamic background colour to script canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Oct 24, 2023
1 parent a1d9e5a commit 6810183
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
22 changes: 20 additions & 2 deletions tools/jml-viewer/Viewer/LuaScriptViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<juce::PropertyComponent*>{
std::make_unique<ColourPropertyComponent>(_viewportColour, "Background").release(),
});

_viewportColour.addListener(this);

addAndMakeVisible(_viewport);
addAndMakeVisible(_componentTree);
addAndMakeVisible(_viewportProperties);
startTimer(2000);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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<juce::Colour>(_viewportColour));
}
}

auto LuaScriptViewer::reloadLuaState() -> void
{
_lua = std::make_unique<LuaState>();
Expand Down
6 changes: 6 additions & 0 deletions tools/jml-viewer/Viewer/LuaScriptViewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace jml::viewer {
struct LuaScriptViewer final
: juce::Component
, juce::Timer
, juce::Value::Listener
{
LuaScriptViewer();
~LuaScriptViewer() override = default;
Expand All @@ -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
Expand All @@ -37,8 +39,12 @@ struct LuaScriptViewer final
static auto handleLuaError(sol::error const& error) -> void;

std::unique_ptr<LuaState> _lua;

ScriptViewport _viewport;
ComponentTree _componentTree;
juce::PropertyPanel _viewportProperties;

juce::Value _viewportColour;

juce::File _scriptFile;
std::unique_ptr<FileChangeListener> _fileListener;
Expand Down
9 changes: 7 additions & 2 deletions tools/jml-viewer/Viewer/ScriptViewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace jml::viewer {

ScriptViewport::ScriptViewport() : _background{getSchemeWidgetBackgroundColour()} {}

auto ScriptViewport::setViewedComponent(juce::Component* component) -> void
{
if (_component != nullptr) {
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion tools/jml-viewer/Viewer/ScriptViewport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6810183

Please sign in to comment.