[JUCE API] Preset ComboBox To Xml File (1)

SangHoon You·2025년 3월 27일
0

JUCE API

목록 보기
1/17
post-thumbnail

Signal Flow

  1. Implementing the preset combo box window
  2. Trigger the onChange function immediately upon click
  1. Implementing a file chooser with flag using launchAsync function
  1. Trigger the callback when the window is closed

1. Setting the Preset Directory

mDirPreset(juce::File::getSpecialLocation
(juce::File::userDocumentsDirectory).
getChildFile(JucePlugin_Name))
  • To provide an appropriate path based on the operating system, it returns the user's 'Documents' folder path using the plugin name as a subdirectory


    const juce::File& getDirPreset() const noexcept;

The const at the beginning prevents the referenced object from being modified, and the const at the end ensures that member variables are not modified within the function


2. FileChooser Allocation

onChange = [this]()
{
    mFileChooser = std::make_unique<juce::FileChooser>(id == IdComboBox::Save ? "Save" : "Load",
                                                       mPresetManager.getDirPreset(),
                                                       "*.xml",
                                                       false);
    mFileChooser->launchAsync(flag, callback);
}
  • The reason for using a smart pointer is to ensure the FileChooser object is automatically deallocated each time the combobox is triggered
  • The reason for making it a member variable is to keep the object alive until the launchAsync() callback is executed
onChange = [this]()
{
    juce::FileChooser chooser(...);      // local variable
    chooser.launchAsync(flag, callback); // the callback will be called later
}; // ⚠️ chooser is already destroyed here
  • Since launchAsync is an asynchronous function, it can execute even after the function has returned

3. Flag Definition

int flag = juce::FileBrowserComponent::canSelectFiles;
flag |= id==IdComboBox::Save ? juce::FileBrowserComponent::saveMode :
                               juce::FileBrowserComponent::openMode;
  • The flag is initialized with the option to allow file selection, and either save mode or open mode is added using a bitwise OR operation based on the id value

bitwise OR operation

It is commonly used to combine multiple options (flags) into one, and at each bit position, the result is 1 if either bit is 1

ex)

// Define flags as binary values
constexpr int canSelectFiles = 0b100;  // 4
constexpr int saveMode       = 0b010;  // 2
constexpr int openMode       = 0b001;  // 1

// Example 1: Combine for "Open File" mode
int flag1 = canSelectFiles | openMode; // 0b100 | 0b001 = 0b101 (5)

// Example 2: Combine for "Save File" mode
int flag2 = canSelectFiles | saveMode; // 0b100 | 0b010 = 0b110 (6)

4. Callback Definition

auto callback = [this,id](const juce::FileChooser& inFileChooser){}
  • The result selected in the file chooser is passed through the callback as const juce::FileChooser&, and we handle it inside the callback function

switch(id)
{
    case IdComboBox::Save:
    {
        DBG("Save");
        mPresetManager.setStateCopied(file.withFileExtension("xml"));
        break;
    }
    case IdComboBox::Load:
    {
        DBG("Load");
        break;
    }
}
  • It’s a structure that branches behavior based on whether the user selected "Save" or "Load" from the combo box

yPresetComboBox::MyPresetComboBox()
{
    resetByXmlPreset();
    
	onChange = [this]()
    {	resetByXmlPrreset();
}
  • Since resetByXmlPreset() initializes the combo box and displays the currently selected preset, it should be called for initial state setup (in the constructor) and state update after user actions (in onChange)
profile
Audio Plugin Developer

0개의 댓글