From 18704f46ed6acbb4bccf86378cb70e0e621cbb95 Mon Sep 17 00:00:00 2001 From: James Ball Date: Wed, 5 Jul 2023 22:45:51 +0100 Subject: [PATCH] Files can be created within osci-render --- Source/MainComponent.cpp | 73 +++++++++++++++++++++++++++++--------- Source/MainComponent.h | 4 +++ Source/PluginEditor.cpp | 26 ++++++-------- Source/PluginEditor.h | 4 +-- Source/PluginProcessor.cpp | 28 +++++++++++---- Source/PluginProcessor.h | 7 ++-- 6 files changed, 98 insertions(+), 44 deletions(-) diff --git a/Source/MainComponent.cpp b/Source/MainComponent.cpp index c60c11b..1d7d0b4 100644 --- a/Source/MainComponent.cpp +++ b/Source/MainComponent.cpp @@ -22,8 +22,7 @@ MainComponent::MainComponent(OscirenderAudioProcessor& p, OscirenderAudioProcess } } pluginEditor.addCodeEditor(audioProcessor.getCurrentFileIndex()); - pluginEditor.updateCodeEditor(); - pluginEditor.fileUpdated(std::make_unique(audioProcessor.getCurrentFile())); + pluginEditor.fileUpdated(audioProcessor.getCurrentFileName()); }); }; @@ -38,16 +37,46 @@ MainComponent::MainComponent(OscirenderAudioProcessor& p, OscirenderAudioProcess } pluginEditor.removeCodeEditor(audioProcessor.getCurrentFileIndex()); audioProcessor.removeFile(audioProcessor.getCurrentFileIndex()); - pluginEditor.updateCodeEditor(); - std::unique_ptr file; - if (audioProcessor.getCurrentFileIndex() != -1) { - file = std::make_unique(audioProcessor.getCurrentFile()); - } - pluginEditor.fileUpdated(std::move(file)); + pluginEditor.fileUpdated(audioProcessor.getCurrentFileName()); }; addAndMakeVisible(fileLabel); updateFileLabel(); + + + addAndMakeVisible(fileName); + fileType.addItem(".lua", 1); + fileType.addItem(".svg", 2); + fileType.addItem(".obj", 3); + fileType.addItem(".txt", 4); + fileType.setSelectedId(1); + addAndMakeVisible(fileType); + addAndMakeVisible(createFile); + + createFile.onClick = [this] { + juce::SpinLock::ScopedLockType lock(audioProcessor.parsersLock); + auto fileNameText = fileName.getText(); + auto fileTypeText = fileType.getText(); + auto fileName = fileNameText + fileTypeText; + if (fileTypeText == ".lua") { + audioProcessor.addFile(fileNameText + fileTypeText, BinaryData::demo_lua, BinaryData::demo_luaSize); + } else if (fileTypeText == ".svg") { + audioProcessor.addFile(fileNameText + fileTypeText, BinaryData::demo_svg, BinaryData::demo_svgSize); + } else if (fileTypeText == ".obj") { + audioProcessor.addFile(fileNameText + fileTypeText, BinaryData::cube_obj, BinaryData::cube_objSize); + } else if (fileTypeText == ".txt") { + audioProcessor.addFile(fileNameText + fileTypeText, BinaryData::helloworld_txt, BinaryData::helloworld_txtSize); + } else { + return; + } + + pluginEditor.addCodeEditor(audioProcessor.getCurrentFileIndex()); + pluginEditor.fileUpdated(fileName); + }; + + fileName.onReturnKey = [this] { + createFile.triggerClick(); + }; } MainComponent::~MainComponent() { @@ -59,16 +88,28 @@ void MainComponent::updateFileLabel() { return; } - fileLabel.setText(audioProcessor.getCurrentFile().getFileName(), juce::dontSendNotification); + fileLabel.setText(audioProcessor.getCurrentFileName(), juce::dontSendNotification); } void MainComponent::resized() { - auto baseYPadding = 10; - auto xPadding = 10; - auto yPadding = 10; + auto bounds = getLocalBounds().reduced(20); auto buttonWidth = 120; - auto buttonHeight = 40; - fileButton.setBounds(xPadding, baseYPadding + yPadding, buttonWidth, buttonHeight); - closeFileButton.setBounds(xPadding, baseYPadding + yPadding + buttonHeight + yPadding, buttonWidth, buttonHeight); - fileLabel.setBounds(xPadding + buttonWidth + xPadding, baseYPadding + yPadding, getWidth() - xPadding - buttonWidth - xPadding, buttonHeight); + auto buttonHeight = 30; + auto padding = 10; + auto rowPadding = 10; + + auto row = bounds.removeFromTop(buttonHeight); + fileButton.setBounds(row.removeFromLeft(buttonWidth)); + row.removeFromLeft(rowPadding); + fileLabel.setBounds(row); + bounds.removeFromTop(padding); + closeFileButton.setBounds(bounds.removeFromTop(buttonHeight).removeFromLeft(buttonWidth)); + + bounds.removeFromTop(padding); + row = bounds.removeFromTop(buttonHeight); + fileName.setBounds(row.removeFromLeft(buttonWidth)); + row.removeFromLeft(rowPadding); + fileType.setBounds(row.removeFromLeft(buttonWidth / 2)); + row.removeFromLeft(rowPadding); + createFile.setBounds(row.removeFromLeft(buttonWidth)); } diff --git a/Source/MainComponent.h b/Source/MainComponent.h index 6d35db2..ac98211 100644 --- a/Source/MainComponent.h +++ b/Source/MainComponent.h @@ -22,5 +22,9 @@ private: juce::TextButton closeFileButton; juce::Label fileLabel; + juce::TextEditor fileName; + juce::ComboBox fileType; + juce::TextButton createFile{"Create File"}; + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MainComponent) }; \ No newline at end of file diff --git a/Source/PluginEditor.cpp b/Source/PluginEditor.cpp index da3f8d0..452b5f4 100644 --- a/Source/PluginEditor.cpp +++ b/Source/PluginEditor.cpp @@ -46,13 +46,7 @@ OscirenderAudioProcessorEditor::OscirenderAudioProcessorEditor(OscirenderAudioPr for (int i = 0; i < audioProcessor.numFiles(); i++) { addCodeEditor(i); } - updateCodeEditor(); - - std::unique_ptr file; - if (audioProcessor.getCurrentFileIndex() != -1) { - file = std::make_unique(audioProcessor.getCurrentFile()); - } - fileUpdated(std::move(file)); + fileUpdated(audioProcessor.getCurrentFileName()); setSize(1100, 750); setResizable(true, true); @@ -93,7 +87,7 @@ void OscirenderAudioProcessorEditor::resized() { void OscirenderAudioProcessorEditor::addCodeEditor(int index) { std::shared_ptr codeDocument = std::make_shared(); codeDocuments.insert(codeDocuments.begin() + index, codeDocument); - juce::String extension = audioProcessor.getFile(index).getFileExtension(); + juce::String extension = audioProcessor.getFileName(index).fromLastOccurrenceOf(".", true, false); juce::CodeTokeniser* tokeniser = nullptr; if (extension == ".lua") { tokeniser = &luaTokeniser; @@ -137,17 +131,19 @@ void OscirenderAudioProcessorEditor::updateCodeEditor() { } // parsersLock MUST be locked before calling this function -void OscirenderAudioProcessorEditor::fileUpdated(std::unique_ptr file) { +void OscirenderAudioProcessorEditor::fileUpdated(juce::String fileName) { + juce::String extension = fileName.fromLastOccurrenceOf(".", true, false); lua.setVisible(false); obj.setVisible(false); - if (file == nullptr) { - return; - } else if (file->getFileExtension() == ".lua") { + if (fileName.isEmpty()) { + // do nothing + } else if (extension == ".lua") { lua.setVisible(true); - } else if (file->getFileExtension() == ".obj") { + } else if (extension == ".obj") { obj.setVisible(true); } main.updateFileLabel(); + updateCodeEditor(); } // parsersLock AND effectsLock must be locked before calling this function @@ -198,9 +194,7 @@ bool OscirenderAudioProcessorEditor::keyPressed(const juce::KeyPress& key) { if (changedFile) { audioProcessor.changeCurrentFile(currentFile); - fileUpdated(std::make_unique(audioProcessor.getCurrentFile())); - updateCodeEditor(); - main.updateFileLabel(); + fileUpdated(audioProcessor.getCurrentFileName()); } return consumeKey; diff --git a/Source/PluginEditor.h b/Source/PluginEditor.h index e243e0f..abb4211 100644 --- a/Source/PluginEditor.h +++ b/Source/PluginEditor.h @@ -30,8 +30,7 @@ public: void addCodeEditor(int index); void removeCodeEditor(int index); - void updateCodeEditor(); - void fileUpdated(std::unique_ptr file); + void fileUpdated(juce::String fileName); private: OscirenderAudioProcessor& audioProcessor; @@ -48,6 +47,7 @@ private: void codeDocumentTextInserted(const juce::String& newText, int insertIndex) override; void codeDocumentTextDeleted(int startIndex, int endIndex) override; void updateCodeDocument(); + void updateCodeEditor(); bool keyPressed(const juce::KeyPress& key) override; diff --git a/Source/PluginProcessor.cpp b/Source/PluginProcessor.cpp index 3b20a24..e7d3c23 100644 --- a/Source/PluginProcessor.cpp +++ b/Source/PluginProcessor.cpp @@ -238,13 +238,23 @@ void OscirenderAudioProcessor::updateFileBlock(int index, std::shared_ptr()); - files.push_back(file); + fileNames.push_back(file.getFileName()); parsers.push_back(std::make_unique()); file.createInputStream()->readIntoMemoryBlock(*fileBlocks.back()); openFile(fileBlocks.size() - 1); } +// parsersLock AND effectsLock must be locked before calling this function +void OscirenderAudioProcessor::addFile(juce::String fileName, const char* data, const int size) { + fileBlocks.push_back(std::make_shared()); + fileNames.push_back(fileName); + parsers.push_back(std::make_unique()); + fileBlocks.back()->append(data, size); + + openFile(fileBlocks.size() - 1); +} + // parsersLock AND effectsLock must be locked before calling this function void OscirenderAudioProcessor::removeFile(int index) { if (index < 0 || index >= fileBlocks.size()) { @@ -252,7 +262,7 @@ void OscirenderAudioProcessor::removeFile(int index) { } changeCurrentFile(index - 1); fileBlocks.erase(fileBlocks.begin() + index); - files.erase(files.begin() + index); + fileNames.erase(fileNames.begin() + index); parsers.erase(parsers.begin() + index); } @@ -267,7 +277,7 @@ void OscirenderAudioProcessor::openFile(int index) { if (index < 0 || index >= fileBlocks.size()) { return; } - parsers[index]->parse(files[index].getFileExtension(), std::make_unique(*fileBlocks[index], false)); + parsers[index]->parse(fileNames[index].fromLastOccurrenceOf(".", true, false), std::make_unique(*fileBlocks[index], false)); changeCurrentFile(index); } @@ -297,12 +307,16 @@ std::shared_ptr OscirenderAudioProcessor::getCurrentFileParser() { return parsers[currentFile]; } -juce::File OscirenderAudioProcessor::getCurrentFile() { - return files[currentFile]; +juce::String OscirenderAudioProcessor::getCurrentFileName() { + if (currentFile == -1) { + return ""; + } else { + return fileNames[currentFile]; + } } -juce::File OscirenderAudioProcessor::getFile(int index) { - return files[index]; +juce::String OscirenderAudioProcessor::getFileName(int index) { + return fileNames[index]; } std::shared_ptr OscirenderAudioProcessor::getFileBlock(int index) { diff --git a/Source/PluginProcessor.h b/Source/PluginProcessor.h index 76efc63..f3a889d 100644 --- a/Source/PluginProcessor.h +++ b/Source/PluginProcessor.h @@ -162,7 +162,7 @@ public: juce::SpinLock parsersLock; std::vector> parsers; std::vector> fileBlocks; - std::vector files; + std::vector fileNames; std::atomic currentFile = -1; std::unique_ptr producer; @@ -175,13 +175,14 @@ public: void updateEffectPrecedence(); void updateFileBlock(int index, std::shared_ptr block); void addFile(juce::File file); + void addFile(juce::String fileName, const char* data, const int size); void removeFile(int index); int numFiles(); void changeCurrentFile(int index); int getCurrentFileIndex(); std::shared_ptr getCurrentFileParser(); - juce::File getCurrentFile(); - juce::File getFile(int index); + juce::String getCurrentFileName(); + juce::String getFileName(int index); std::shared_ptr getFileBlock(int index); private: double theta = 0.0;