Create SvgButton component to reduce duplication

pull/170/head
James Ball 2023-07-22 13:44:13 +01:00
rodzic 3701157440
commit 90ce1a73a2
5 zmienionych plików z 38 dodań i 32 usunięć

Wyświetl plik

@ -1,7 +1,6 @@
#include "ObjComponent.h"
#include "PluginEditor.h"
#include <numbers>
#include "Util.h"
ObjComponent::ObjComponent(OscirenderAudioProcessor& p, OscirenderAudioProcessorEditor& editor) : audioProcessor(p), pluginEditor(editor) {
setText("3D .obj File Settings");
@ -67,21 +66,6 @@ ObjComponent::ObjComponent(OscirenderAudioProcessor& p, OscirenderAudioProcessor
}
};
auto doc = juce::XmlDocument::parse(BinaryData::fixed_rotate_svg);
Util::changeSvgColour(doc.get(), "white");
fixedRotateWhite = juce::Drawable::createFromSVG(*doc);
Util::changeSvgColour(doc.get(), "red");
fixedRotateRed = juce::Drawable::createFromSVG(*doc);
// TODO: any way of removing this duplication?
getLookAndFeel().setColour(juce::DrawableButton::backgroundOnColourId, juce::Colours::transparentWhite);
fixedRotateX->setClickingTogglesState(true);
fixedRotateY->setClickingTogglesState(true);
fixedRotateZ->setClickingTogglesState(true);
fixedRotateX->setImages(fixedRotateWhite.get(), nullptr, nullptr, nullptr, fixedRotateRed.get());
fixedRotateY->setImages(fixedRotateWhite.get(), nullptr, nullptr, nullptr, fixedRotateRed.get());
fixedRotateZ->setImages(fixedRotateWhite.get(), nullptr, nullptr, nullptr, fixedRotateRed.get());
fixedRotateX->onClick = onRotationChange;
fixedRotateY->onClick = onRotationChange;
fixedRotateZ->onClick = onRotationChange;

Wyświetl plik

@ -3,6 +3,7 @@
#include <JuceHeader.h>
#include "PluginProcessor.h"
#include "components/EffectComponent.h"
#include "components/SvgButton.h"
class OscirenderAudioProcessorEditor;
class ObjComponent : public juce::GroupComponent, public juce::MouseListener {
@ -26,11 +27,9 @@ private:
juce::TextButton resetRotation{"Reset Rotation"};
juce::ToggleButton mouseRotate{"Rotate with Mouse (Esc to disable)"};
std::unique_ptr<juce::Drawable> fixedRotateWhite;
std::unique_ptr<juce::Drawable> fixedRotateRed;
std::shared_ptr<juce::DrawableButton> fixedRotateX = std::make_shared<juce::DrawableButton>("fixedRotateX", juce::DrawableButton::ButtonStyle::ImageFitted);
std::shared_ptr<juce::DrawableButton> fixedRotateY = std::make_shared<juce::DrawableButton>("fixedRotateY", juce::DrawableButton::ButtonStyle::ImageFitted);
std::shared_ptr<juce::DrawableButton> fixedRotateZ = std::make_shared<juce::DrawableButton>("fixedRotateZ", juce::DrawableButton::ButtonStyle::ImageFitted);
std::shared_ptr<SvgButton> fixedRotateX = std::make_shared<SvgButton>("fixedRotateX", juce::String(BinaryData::fixed_rotate_svg), "white", "red");
std::shared_ptr<SvgButton> fixedRotateY = std::make_shared<SvgButton>("fixedRotateY", juce::String(BinaryData::fixed_rotate_svg), "white", "red");
std::shared_ptr<SvgButton> fixedRotateZ = std::make_shared<SvgButton>("fixedRotateZ", juce::String(BinaryData::fixed_rotate_svg), "white", "red");
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ObjComponent)
};

Wyświetl plik

@ -1,10 +0,0 @@
#pragma once
#include <JuceHeader.h>
namespace Util {
void changeSvgColour(juce::XmlElement* xml, juce::String colour) {
forEachXmlChildElement(*xml, xmlnode) {
xmlnode->setAttribute("fill", colour);
}
}
}

Wyświetl plik

@ -0,0 +1,33 @@
#pragma once
#include <JuceHeader.h>
class SvgButton : public juce::DrawableButton {
public:
SvgButton(juce::String name, juce::String svg, juce::String colour, juce::String colourOn) : juce::DrawableButton(name, juce::DrawableButton::ButtonStyle::ImageFitted) {
auto doc = juce::XmlDocument::parse(svg);
changeSvgColour(doc.get(), colour);
normalImage = juce::Drawable::createFromSVG(*doc);
changeSvgColour(doc.get(), colourOn);
normalImageOn = juce::Drawable::createFromSVG(*doc);
getLookAndFeel().setColour(juce::DrawableButton::backgroundOnColourId, juce::Colours::transparentWhite);
if (colour != colourOn) {
setClickingTogglesState(true);
}
setImages(normalImage.get(), nullptr, nullptr, nullptr, normalImageOn.get());
}
SvgButton(juce::String name, juce::String svg, juce::String colour) : SvgButton(name, svg, colour, colour) {}
~SvgButton() override {}
private:
std::unique_ptr<juce::Drawable> normalImage;
std::unique_ptr<juce::Drawable> normalImageOn;
void changeSvgColour(juce::XmlElement* xml, juce::String colour) {
forEachXmlChildElement(*xml, xmlnode) {
xmlnode->setAttribute("fill", colour);
}
}
};

Wyświetl plik

@ -112,6 +112,7 @@
<FILE id="x0Syav" name="LuaListComponent.h" compile="0" resource="0"
file="Source/components/LuaListComponent.h"/>
<FILE id="QQzSwh" name="SliderTextBox.h" compile="0" resource="0" file="Source/components/SliderTextBox.h"/>
<FILE id="QrDKRZ" name="SvgButton.h" compile="0" resource="0" file="Source/components/SvgButton.h"/>
<FILE id="y3UiR0" name="VisualiserComponent.cpp" compile="1" resource="0"
file="Source/components/VisualiserComponent.cpp"/>
<FILE id="ZueyNl" name="VisualiserComponent.h" compile="0" resource="0"
@ -438,7 +439,6 @@
<FILE id="vIYWRG" name="TextParser.cpp" compile="1" resource="0" file="Source/txt/TextParser.cpp"/>
<FILE id="LlefOK" name="TextParser.h" compile="0" resource="0" file="Source/txt/TextParser.h"/>
</GROUP>
<FILE id="JceyXh" name="Util.h" compile="0" resource="0" file="Source/Util.h"/>
<GROUP id="{022CB910-9A16-C4AE-4C3B-9CB57BE87FC2}" name="xml">
<FILE id="pW7WRh" name="pugiconfig.hpp" compile="0" resource="0" file="Source/xml/pugiconfig.hpp"/>
<FILE id="CnkgyF" name="pugixml.cpp" compile="1" resource="0" file="Source/xml/pugixml.cpp"/>