gpt4 book ai didi

c++ - 自定义子组件已添加但不可见

转载 作者:太空宇宙 更新时间:2023-11-04 12:31:33 25 4
gpt4 key购买 nike

我正在使用 JUCE 框架创建一个音频插件。我创建了一个继承自 Component 类的 Knob 类。我的 Knob 类包含对 SliderLabel 的引用。

在我的 AudioProcessorEditor 类中,我初始化了几个 Knob 对象。但是,没有一个 Component 在运行时是可见的。我做错了什么或错过了一步吗?

我已经尝试在我的 AudioProcessorEditor 类中直接声明 SliderLabel 对象。当我这样做时,我能够在运行时成功地看到 SliderLabel 对象。所以我觉得这个问题涉及到我的 Knob 类。

旋钮.h

#pragma once
#include "../JuceLibraryCode/JuceHeader.h"

class Knob : public Component
{
public:
Knob(String, AudioProcessorEditor*);
~Knob();

void paint (Graphics&) override;
void resized() override;

String Name;
Label *KnobLabel;
Slider *KnobSlider;
AudioProcessorEditor *APE;

private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Knob)
};

旋钮.cpp

#include "Knob.h"

Knob::Knob(String name, AudioProcessorEditor *ape)
{
// In your constructor, you should add any child components, and
// initialise any special settings that your component needs.

this->Name = name;

APE = ape;

KnobLabel = new Label(name);
KnobLabel->setColour(0x1000281, Colours::antiquewhite);
KnobLabel->setAlwaysOnTop(true);
KnobLabel->setFont(10);

KnobSlider = new Slider();
KnobSlider->setAlwaysOnTop(true);

addAndMakeVisible(KnobLabel);
addAndMakeVisible(KnobSlider);
}

void Knob::paint (Graphics& g)
{
/* This demo code just fills the component's background and
draws some placeholder text to get you started.

You should replace everything in this method with your own
drawing code..
*/
g.setColour(Colours::white);
g.fillAll();
}

void Knob::resized()
{
// This method is where you should set the bounds of any child
// components that your component contains..

auto bounds = getLocalBounds();

KnobSlider->setBounds(bounds.removeFromTop(getHeight()*8));
KnobLabel->setBounds(bounds);
}

插件编辑器.h

#pragma once

#include "../JuceLibraryCode/JuceHeader.h"
#include "PluginProcessor.h"
#include "Knob.h"
#include "Model.h"

class MoonlightAudioProcessorEditor : public AudioProcessorEditor
{
public:
MoonlightAudioProcessorEditor (MoonlightAudioProcessor&);
~MoonlightAudioProcessorEditor();

void paint (Graphics&) override;
void resized() override;

Knob *OrbitKnob;
Knob *SpaceKnob;
Knob *InertiaKnob;

void ConfigureUI();

private:
OwnedArray<Knob> Knobs;
ComponentBoundsConstrainer ResizeBounds;
ResizableCornerComponent *Resizer;
MoonlightAudioProcessor& processor;
};

插件编辑器.cpp

#include "PluginProcessor.h"
#include "PluginEditor.h"

MoonlightAudioProcessorEditor::MoonlightAudioProcessorEditor (MoonlightAudioProcessor& p)
: AudioProcessorEditor (&p), processor (p)
{
setSize (400, 300);
ConfigureUI();
}

void MoonlightAudioProcessorEditor::ConfigureUI()
{
OrbitKnob = new Knob("Orbit", this);
SpaceKnob = new Knob("Space", this);
InertiaKnob = new Knob("Inertia", this);

Knobs.add(OrbitKnob);
Knobs.add(SpaceKnob);
Knobs.add(InertiaKnob);

for (Knob *knob : Knobs)
{
knob->KnobSlider->addListener(this);
addAndMakeVisible(knob);
knob->setAlwaysOnTop(true);
}

ResizeBounds.setSizeLimits(DEFAULT_WIDTH,
DEFAULT_HEIGHT,
MAX_WIDTH,
MAX_HEIGHT);
Resizer = new ResizableCornerComponent(this, &ResizeBounds);
addAndMakeVisible(Resizer);

setSize(processor._UIWidth, processor._UIHeight);
}

void MoonlightAudioProcessorEditor::paint (Graphics& g)
{
g.setColour (Colours::black);
g.fillAll();
}

void MoonlightAudioProcessorEditor::resized()
{
int width = getWidth();
int height = getHeight();

auto bounds = getLocalBounds();
auto graphicBounds = bounds.removeFromTop(height*.8);
auto orbitBounds = bounds.removeFromLeft(width/3);
auto spaceBounds = bounds.removeFromLeft(width/3);

if (OrbitKnob != nullptr)
{
OrbitKnob->setBounds(orbitBounds);
}

if (SpaceKnob != nullptr)
{
SpaceKnob->setBounds(spaceBounds);
}

if (InertiaKnob != nullptr)
{
InertiaKnob->setBounds(bounds);
}

if (Resizer != nullptr)
{
Resizer->setBounds(width - 16, height - 16, 16, 16);
}

processor._UIWidth = width;
processor._UIHeight = height;
}

此外,我一直在使用 JUCE 提供的 AudioPluginHost 应用程序来测试我的插件。很多时候,应用程序会因段错误而崩溃。然后我在不做任何更改的情况下重建插件,它就会工作。

最佳答案

我终于明白了。

我的组件在设置大小后正在初始化。很多显示逻辑都在 resize() 函数中,所以我需要在组件初始化后设置尺寸。

关于c++ - 自定义子组件已添加但不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58458189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com