This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
avoid_new_delete_prefer_references_to_pointers_use_member_variables_for_sub-components [2017/09/01 18:22] shane |
avoid_new_delete_prefer_references_to_pointers_use_member_variables_for_sub-components [2017/09/01 19:02] (current) shane |
||
---|---|---|---|
Line 6: | Line 6: | ||
- In all of my GUI-related classes, I used '' | - In all of my GUI-related classes, I used '' | ||
- | Regarding the first category, I can only assume that Jules' | + | Regarding the first category, I can only assume that Jules' |
+ | |||
+ | ===== Cleaning up the GUI classes ===== | ||
+ | JUCE Forum user // | ||
+ | <code cpp> | ||
+ | lass EnvelopeComponent : public Component | ||
+ | { | ||
+ | public: | ||
+ | EnvelopeComponent() /* user the initializer list */ | ||
+ | : attack (Slider:: | ||
+ | | ||
+ | | ||
+ | | ||
+ | { | ||
+ | auto initSlider = [this] (Slider& | ||
+ | { | ||
+ | addAndMakeVisible (s): | ||
+ | s.setRange (1.0, 500.0); | ||
+ | s.setValue (25.0); | ||
+ | }; | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | //.... yada yada yada | ||
+ | private: | ||
+ | Slider attack, sustain, decay, release; /* don't need to put them on different lines */ | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | Following // | ||
+ | <code cpp> | ||
+ | class GuiEgTab | ||
+ | public SliderListener | ||
+ | { | ||
+ | public: | ||
+ | GuiEgTab (SynthSound* pSynthSound); | ||
+ | ~GuiEgTab(); | ||
+ | |||
+ | void paint (Graphics& | ||
+ | void resized() override; | ||
+ | void sliderValueChanged (Slider* sliderThatWasMoved) override; | ||
+ | |||
+ | void notify(); | ||
+ | |||
+ | private: | ||
+ | SynthSound* pSound; | ||
+ | |||
+ | ScopedPointer< | ||
+ | ScopedPointer< | ||
+ | ScopedPointer< | ||
+ | ScopedPointer< | ||
+ | ScopedPointer< | ||
+ | ScopedPointer< | ||
+ | ScopedPointer< | ||
+ | ScopedPointer< | ||
+ | |||
+ | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GuiEgTab) | ||
+ | }; | ||
+ | |||
+ | GuiEgTab:: | ||
+ | : pSound(pSynthSound) | ||
+ | { | ||
+ | addAndMakeVisible (attackLabel = new Label (" | ||
+ | TRANS(" | ||
+ | attackLabel-> | ||
+ | attackLabel-> | ||
+ | attackLabel-> | ||
+ | attackLabel-> | ||
+ | attackLabel-> | ||
+ | |||
+ | addAndMakeVisible (attackSlider = new Slider (" | ||
+ | attackSlider-> | ||
+ | attackSlider-> | ||
+ | attackSlider-> | ||
+ | attackSlider-> | ||
+ | |||
+ | addAndMakeVisible (decayLabel = new Label (" | ||
+ | | ||
+ | decayLabel-> | ||
+ | decayLabel-> | ||
+ | decayLabel-> | ||
+ | decayLabel-> | ||
+ | decayLabel-> | ||
+ | |||
+ | addAndMakeVisible (decaySlider = new Slider (" | ||
+ | decaySlider-> | ||
+ | decaySlider-> | ||
+ | decaySlider-> | ||
+ | decaySlider-> | ||
+ | |||
+ | addAndMakeVisible (sustainLabel = new Label (" | ||
+ | | ||
+ | sustainLabel-> | ||
+ | sustainLabel-> | ||
+ | sustainLabel-> | ||
+ | sustainLabel-> | ||
+ | sustainLabel-> | ||
+ | |||
+ | addAndMakeVisible (sustainSlider = new Slider (" | ||
+ | sustainSlider-> | ||
+ | sustainSlider-> | ||
+ | sustainSlider-> | ||
+ | sustainSlider-> | ||
+ | |||
+ | addAndMakeVisible (releaseLabel = new Label (" | ||
+ | TRANS(" | ||
+ | releaseLabel-> | ||
+ | releaseLabel-> | ||
+ | releaseLabel-> | ||
+ | releaseLabel-> | ||
+ | releaseLabel-> | ||
+ | |||
+ | addAndMakeVisible (releaseSlider = new Slider (" | ||
+ | releaseSlider-> | ||
+ | releaseSlider-> | ||
+ | releaseSlider-> | ||
+ | releaseSlider-> | ||
+ | |||
+ | notify(); | ||
+ | } | ||
+ | |||
+ | GuiEgTab:: | ||
+ | { | ||
+ | } | ||
+ | |||
+ | // | ||
+ | void GuiEgTab:: | ||
+ | { | ||
+ | g.fillAll (Colour (0xff323e44)); | ||
+ | } | ||
+ | |||
+ | void GuiEgTab:: | ||
+ | { | ||
+ | const int labelLeft = 16; | ||
+ | const int controlLeft = 144; | ||
+ | const int labelWidth = 120; | ||
+ | const int sliderWidth = 420; | ||
+ | const int controlHeight = 24; | ||
+ | const int gapHeight = 8; | ||
+ | |||
+ | int top = 20; | ||
+ | attackLabel-> | ||
+ | attackSlider-> | ||
+ | top += controlHeight + gapHeight; | ||
+ | decayLabel-> | ||
+ | decaySlider-> | ||
+ | top += controlHeight + gapHeight; | ||
+ | sustainLabel-> | ||
+ | sustainSlider-> | ||
+ | top += controlHeight + gapHeight; | ||
+ | releaseLabel-> | ||
+ | releaseSlider-> | ||
+ | } | ||
+ | |||
+ | void GuiEgTab:: | ||
+ | { | ||
+ | double value = sliderThatWasMoved-> | ||
+ | SynthParameters* pParams = pSound-> | ||
+ | if (sliderThatWasMoved == attackSlider) | ||
+ | { | ||
+ | pParams-> | ||
+ | } | ||
+ | else if (sliderThatWasMoved == decaySlider) | ||
+ | { | ||
+ | pParams-> | ||
+ | } | ||
+ | else if (sliderThatWasMoved == sustainSlider) | ||
+ | { | ||
+ | pParams-> | ||
+ | } | ||
+ | else if (sliderThatWasMoved == releaseSlider) | ||
+ | { | ||
+ | pParams-> | ||
+ | } | ||
+ | pSound-> | ||
+ | } | ||
+ | |||
+ | void GuiEgTab:: | ||
+ | { | ||
+ | SynthParameters* pParams = pSound-> | ||
+ | attackSlider-> | ||
+ | decaySlider-> | ||
+ | sustainSlider-> | ||
+ | releaseSlider-> | ||
+ | } | ||
+ | </ | ||
+ | to this: | ||
+ | <code cpp> | ||
+ | class GuiEgTab | ||
+ | public SliderListener | ||
+ | { | ||
+ | public: | ||
+ | GuiEgTab (SynthSound* pSynthSound); | ||
+ | |||
+ | void paint (Graphics& | ||
+ | void resized() override; | ||
+ | void sliderValueChanged (Slider* sliderThatWasMoved) override; | ||
+ | |||
+ | void notify(); | ||
+ | |||
+ | private: | ||
+ | SynthSound* pSound; | ||
+ | |||
+ | Label attackLabel, | ||
+ | Slider attackSlider, | ||
+ | |||
+ | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GuiEgTab) | ||
+ | }; | ||
+ | |||
+ | GuiEgTab:: | ||
+ | : pSound(pSynthSound) | ||
+ | , attackLabel(" | ||
+ | , decayLabel(" | ||
+ | , sustainLabel(" | ||
+ | , releaseLabel(" | ||
+ | { | ||
+ | auto initLabel = [this](Label& | ||
+ | { | ||
+ | addAndMakeVisible(label); | ||
+ | label.setFont (Font (15.00f, Font:: | ||
+ | label.setJustificationType (Justification:: | ||
+ | label.setEditable (false, false, false); | ||
+ | label.setColour (TextEditor:: | ||
+ | label.setColour (TextEditor:: | ||
+ | }; | ||
+ | |||
+ | initLabel(attackLabel); | ||
+ | initLabel(decayLabel); | ||
+ | initLabel(sustainLabel); | ||
+ | initLabel(releaseLabel); | ||
+ | |||
+ | auto initSlider = [this](Slider& | ||
+ | { | ||
+ | addAndMakeVisible(slider); | ||
+ | slider.setSliderStyle (Slider:: | ||
+ | slider.setTextBoxStyle (Slider:: | ||
+ | slider.addListener (this); | ||
+ | }; | ||
+ | |||
+ | initSlider(attackSlider); | ||
+ | initSlider(decaySlider); | ||
+ | initSlider(sustainSlider); | ||
+ | initSlider(releaseSlider); | ||
+ | |||
+ | notify(); | ||
+ | } | ||
+ | |||
+ | void GuiEgTab:: | ||
+ | { | ||
+ | g.fillAll (Colour (0xff323e44)); | ||
+ | } | ||
+ | |||
+ | void GuiEgTab:: | ||
+ | { | ||
+ | const int labelLeft = 16; | ||
+ | const int controlLeft = 144; | ||
+ | const int labelWidth = 120; | ||
+ | const int sliderWidth = 420; | ||
+ | const int controlHeight = 24; | ||
+ | const int gapHeight = 8; | ||
+ | |||
+ | int top = 20; | ||
+ | attackLabel.setBounds (labelLeft, top, labelWidth, controlHeight); | ||
+ | attackSlider.setBounds (controlLeft, | ||
+ | top += controlHeight + gapHeight; | ||
+ | decayLabel.setBounds (labelLeft, top, labelWidth, controlHeight); | ||
+ | decaySlider.setBounds (controlLeft, | ||
+ | top += controlHeight + gapHeight; | ||
+ | sustainLabel.setBounds (labelLeft, top, labelWidth, controlHeight); | ||
+ | sustainSlider.setBounds (controlLeft, | ||
+ | top += controlHeight + gapHeight; | ||
+ | releaseLabel.setBounds (labelLeft, top, labelWidth, controlHeight); | ||
+ | releaseSlider.setBounds (controlLeft, | ||
+ | } | ||
+ | |||
+ | void GuiEgTab:: | ||
+ | { | ||
+ | double value = sliderThatWasMoved-> | ||
+ | SynthParameters* pParams = pSound-> | ||
+ | if (sliderThatWasMoved == & | ||
+ | else if (sliderThatWasMoved == & | ||
+ | else if (sliderThatWasMoved == & | ||
+ | else if (sliderThatWasMoved == & | ||
+ | pSound-> | ||
+ | } | ||
+ | |||
+ | void GuiEgTab:: | ||
+ | { | ||
+ | SynthParameters* pParams = pSound-> | ||
+ | attackSlider.setValue(pParams-> | ||
+ | decaySlider.setValue(pParams-> | ||
+ | sustainSlider.setValue(100.0 * pParams-> | ||
+ | releaseSlider.setValue(pParams-> | ||
+ | } | ||
+ | </ |