Skip to content

Commit

Permalink
plugin: add padding customization to theme file
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Aug 12, 2024
1 parent bc5bd05 commit ea42e0b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 13 deletions.
21 changes: 12 additions & 9 deletions plugin/components/parameters_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "lookandfeel.h"
#include "parameters_panel.h"
#include "../parameter.h"

#define LOOKANDFEEL dynamic_cast<YsfxLookAndFeel&>(this->getLookAndFeel())

class YsfxParameterListener : private YsfxParameter::Listener,
private juce::Timer {
public:
Expand Down Expand Up @@ -91,8 +94,8 @@ class YsfxBooleanParameterComponent final : public juce::Component, private Ysfx
void resized() override
{
auto area = getLocalBounds();
area.removeFromLeft(8);
button.setBounds(area.reduced(0, 10));
area.removeFromLeft(LOOKANDFEEL.m_pad + 2);
button.setBounds(area.reduced(LOOKANDFEEL.m_pad, LOOKANDFEEL.m_gap));
}

private:
Expand Down Expand Up @@ -153,8 +156,8 @@ class YsfxSwitchParameterComponent final : public juce::Component, private YsfxP

void resized() override
{
auto area = getLocalBounds().reduced(0, 8);
area.removeFromLeft(8);
auto area = getLocalBounds().reduced(LOOKANDFEEL.m_pad, LOOKANDFEEL.m_gap);
area.removeFromLeft(LOOKANDFEEL.m_pad + 2);

for (auto &button : buttons)
button.setBounds(area.removeFromLeft(80));
Expand Down Expand Up @@ -216,8 +219,8 @@ class YsfxChoiceParameterComponent final : public juce::Component, private YsfxP
void resized() override
{
auto area = getLocalBounds();
area.removeFromLeft(8);
box.setBounds(area.reduced(0, 10));
area.removeFromLeft(LOOKANDFEEL.m_pad + 2);
box.setBounds(area.reduced(LOOKANDFEEL.m_pad, LOOKANDFEEL.m_gap));
}

private:
Expand Down Expand Up @@ -303,11 +306,11 @@ class YsfxSliderParameterComponent final : public juce::Component, private YsfxP

void resized() override
{
auto area = getLocalBounds().reduced(10, 10);
auto area = getLocalBounds().reduced(LOOKANDFEEL.m_pad, LOOKANDFEEL.m_gap);

valueLabel.setBounds(area.removeFromRight(80));

area.removeFromLeft(6);
//area.removeFromLeft(LOOKANDFEEL.m_pad);
slider.setBounds(area.withTrimmedRight(15));
}

Expand Down Expand Up @@ -388,7 +391,7 @@ class YsfxParameterDisplayComponent : public juce::Component {
addAndMakeVisible(parameterName);
addAndMakeVisible(*(parameterComp = createParameterComp()));

setSize(400, 40);
setSize(400, 20 + 2 * LOOKANDFEEL.m_gap);
}

void paint(juce::Graphics &) override
Expand Down
36 changes: 32 additions & 4 deletions plugin/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ YsfxEditor::YsfxEditor(YsfxProcessor &proc)
readTheme();
}

void writeThemeFile(juce::File file, std::map<std::string, std::array<uint8_t, 3>> colors, std::map<std::string, float> params)
{
juce::FileOutputStream stream(file);
stream.setPosition(0);
stream.truncate();
nlohmann::json json_colors{colors};
nlohmann::json json_params{params};

nlohmann::json json_file;
json_file["version"] = 1;
json_file["colors"] = json_colors;
json_file["params"] = json_params;

stream.writeString(juce::String{json_file.dump(4)});
}

void YsfxEditor::readTheme()
{
if (!m_impl) return;
Expand All @@ -144,10 +160,9 @@ void YsfxEditor::readTheme()

if (!file.existsAsFile()) {
try {
juce::FileOutputStream stream(file);
nlohmann::json theme{getDefaultColors()};
stream.writeString(juce::String{theme.dump(4)});
writeThemeFile(file, getDefaultColors(), getDefaultParams());
setColors(getLookAndFeel(), {});
setParams(getLookAndFeel(), {});
} catch (nlohmann::json::exception e) {
// Log: std::cout << "Failed to write theme: " << e.what() << std::endl;
}
Expand All @@ -157,8 +172,21 @@ void YsfxEditor::readTheme()

try {
auto jsonFile = nlohmann::json::parse(text.toStdString());
auto readTheme = jsonFile[0].get<std::map<std::string, std::array<uint8_t, 3>>>();
// Fallback for version 1 files (upconvert the file)
if (!jsonFile.contains("version")) {
auto readTheme = jsonFile[0].get<std::map<std::string, std::array<uint8_t, 3>>>();
writeThemeFile(file, readTheme, getDefaultParams());

// Reread it!
stream.setPosition(0);
text = stream.readEntireStreamAsString();
jsonFile = nlohmann::json::parse(text.toStdString());
}

auto readTheme = jsonFile.at("colors")[0].get<std::map<std::string, std::array<uint8_t, 3>>>();
auto readParams = jsonFile.at("params")[0].get<std::map<std::string, float>>();
setColors(getLookAndFeel(), readTheme);
setParams(getLookAndFeel(), readParams);
} catch (nlohmann::json::exception e) {
// Log: std::cout << "Failed to read theme: " << e.what() << std::endl;
}
Expand Down
31 changes: 31 additions & 0 deletions plugin/lookandfeel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,37 @@ std::map<std::string, std::array<uint8_t, 3>> getDefaultColors()
};
}

std::map<std::string, float> getDefaultParams()
{
return std::map<std::string, float>{
{"vertical_pad", 5},
{"left_pad", 3},
};
}

void setParams(juce::LookAndFeel& lnf, std::map<std::string, float> params)
{
std::map<std::string, float> currentParams = getDefaultParams();
for (auto it = params.begin(); it != params.end(); ++it) {
currentParams[it->first] = it->second;
}

auto get = [currentParams](std::string key) {
auto it = currentParams.find(key);
jassert(it != currentParams.end()); // This color doesn't have a default!

if (it != currentParams.end()) {
return it->second;
} else {
return 1.0f;
}
};

YsfxLookAndFeel& ysfx_lnf = dynamic_cast<YsfxLookAndFeel&>(lnf);
ysfx_lnf.m_gap = static_cast<int>(get("vertical_pad"));
ysfx_lnf.m_pad = static_cast<int>(get("left_pad"));
}

void setColors(juce::LookAndFeel& lnf, std::map<std::string, std::array<uint8_t, 3>> colormap)
{
std::map<std::string, std::array<uint8_t, 3>> currentColorMap = getDefaultColors();
Expand Down
5 changes: 5 additions & 0 deletions plugin/lookandfeel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@


void setColors(juce::LookAndFeel& lnf, std::map<std::string, std::array<uint8_t, 3>> colormap);
void setParams(juce::LookAndFeel& lnf, std::map<std::string, float> params);
std::map<std::string, std::array<uint8_t, 3>> getDefaultColors();
std::map<std::string, float> getDefaultParams();


class YsfxLookAndFeel : public juce::LookAndFeel_V4 {
public:
int m_gap{3};
int m_pad{3};

YsfxLookAndFeel()
{
setColors(*this, {});
Expand Down

0 comments on commit ea42e0b

Please sign in to comment.