Skip to content

Commit

Permalink
Merge pull request #438 from asb2m10/379-cartmanager_refactor
Browse files Browse the repository at this point in the history
379 Cartridge Manager refractor (see original cartridge name)
  • Loading branch information
asb2m10 authored Jul 24, 2024
2 parents a061968 + 223a866 commit 8fdafa3
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 101 deletions.
109 changes: 60 additions & 49 deletions Source/CartManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2015-2017 Pascal Gauthier.
* Copyright (c) 2015-2024 Pascal Gauthier.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -25,7 +25,6 @@
#include "PluginData.h"

#include <fstream>
using namespace ::std;

class SyxFileFilter : public FileFilter {
public:
Expand All @@ -41,26 +40,26 @@ class SyxFileFilter : public FileFilter {
class FileTreeDrop : public FileTreeComponent {
public :
FileTreeDrop(DirectoryContentsList &listToShow) : FileTreeComponent(listToShow) {}

bool isInterestedInFileDrag (const StringArray &files) override {
bool found = false;

for(int i=0; i<files.size(); i++) {
String filename = files[i].toLowerCase();
found |= filename.endsWith(".syx");
}
return found;
}

void filesDropped(const StringArray &files, int x, int y) override {
File targetDir = getSelectedFile();

if ( ! targetDir.exists() )
targetDir = DexedAudioProcessor::dexedCartDir;

if ( ! targetDir.isDirectory() )
targetDir = targetDir.getParentDirectory();

for(int i=0; i<files.size(); i++) {
if ( files[i].toLowerCase().endsWith(".syx") ) {
File src(files[i]);
Expand All @@ -74,65 +73,61 @@ public :

CartManager::CartManager(DexedAudioProcessorEditor *editor) : Component("CartManager") {
mainWindow = editor;
cartDir = DexedAudioProcessor::dexedCartDir;
cartDir = DexedAudioProcessor::dexedCartDir;

activeCart.reset(new ProgramListBox("activepgm", 8));
addAndMakeVisible(activeCart.get());
activeCart->setBounds(28, 441, 800, 96);
activeCart->addListener(this);

browserCart.reset(new ProgramListBox("browserpgm", 2));
addAndMakeVisible(browserCart.get());
browserCart->setBounds(635, 18, 200, 384);
browserCart->addListener(this);

// -------------------------
syxFileFilter.reset(new SyxFileFilter());
timeSliceThread.reset(new TimeSliceThread("Cartridge Directory Scanner"));
timeSliceThread->startThread();

cartBrowserList.reset(new DirectoryContentsList(syxFileFilter.get(), *timeSliceThread.get()));
cartBrowserList->setDirectory(cartDir, true, true);
cartBrowser.reset(new FileTreeDrop(*cartBrowserList));
cartBrowser->addKeyListener(this);
addAndMakeVisible(cartBrowser.get());

cartBrowser->setBounds(23, 18, 590, 384);

cartBrowser->setDragAndDropDescription("Sysex Browser");
cartBrowser->addListener(this);

closeButton.reset(new TextButton("CLOSE"));
addAndMakeVisible(closeButton.get());
closeButton->setBounds(4, 545, 50, 30);
closeButton->addListener(this);

loadButton.reset(new TextButton("LOAD"));
addAndMakeVisible(loadButton.get());
loadButton->setBounds(52, 545, 50, 30);
loadButton->addListener(this);

saveButton.reset(new TextButton("SAVE"));
addAndMakeVisible(saveButton.get());
saveButton->setBounds(100, 545, 50, 30);
saveButton->addListener(this);

fileMgrButton.reset(new TextButton("SHOW DIR"));
addAndMakeVisible(fileMgrButton.get());
fileMgrButton->setBounds(148, 545, 70, 30);
fileMgrButton->addListener(this);

activeCartName.reset(new CartridgeFileDisplay());
addAndMakeVisible(activeCartName.get());

/*
*
* I've removed this since it only works on the DX7 II. TBC.
*
addAndMakeVisible(getDXPgmButton = new TextButton("GET DX7 PGM"));
getDXPgmButton->setBounds(656, 545, 100, 30);
getDXPgmButton->addListener(this);
addAndMakeVisible(getDXCartButton = new TextButton("GET DX7 CART"));
getDXCartButton->setBounds(755, 545, 100, 30);
getDXCartButton->addListener(this);
*/
*/
}

CartManager::~CartManager() {
Expand All @@ -141,12 +136,25 @@ CartManager::~CartManager() {
cartBrowserList.reset(NULL);
}

void CartManager::resized() {
float activeSize = ((float) getWidth() - 30) / 8;

activeCart->setBounds(15, 402, activeSize * 8, 96);
browserCart->setBounds(activeSize * 6 + 15, 10, activeSize * 2, 384);
cartBrowser->setBounds(15, 10, activeSize * 6 - 1, 383);
closeButton->setBounds(4, getHeight() - 40, 70, 30);
saveButton->setBounds(144, getHeight() - 40, 70, 30);
loadButton->setBounds(74, getHeight() - 40, 70, 30);
fileMgrButton->setBounds(214, getHeight() - 40, 90, 30);
activeCartName->setBounds(getWidth() - 8 - 300, getHeight() - 40, 300, 30);
}

void CartManager::paint(Graphics &g) {
g.fillAll(DXLookNFeel::lightBackground);
g.setColour(DXLookNFeel::roundBackground);
g.fillRoundedRectangle(8, 418, 843, 126, 15);
g.setColour(Colours::whitesmoke);
g.drawText("currently loaded cartridge", 38, 410, 150, 40, Justification::left);
}

void CartManager::updateCartFilename() {
activeCartName->setCartrdigeFile(mainWindow->processor->activeFileCartridge);
}

void CartManager::programSelected(ProgramListBox *source, int pos) {
Expand All @@ -168,22 +176,24 @@ void CartManager::programSelected(ProgramListBox *source, int pos) {
void CartManager::buttonClicked(juce::Button *buttonThatWasClicked) {
if ( buttonThatWasClicked == closeButton.get() ) {
mainWindow->startTimer(100);
setVisible(false);
getParentComponent()->setVisible(false);
return;
}

if ( buttonThatWasClicked == loadButton.get() ) {
FileChooser fc ("Import original DX sysex...", File::getSpecialLocation(File::SpecialLocationType::userDocumentsDirectory), "*.syx;*.SYX;*.*", 1);
if ( fc.browseForFileToOpen())

if ( fc.browseForFileToOpen()) {
mainWindow->loadCart(fc.getResult());
updateCartFilename();
}
return;
}

if ( buttonThatWasClicked == saveButton.get() ) {
mainWindow->saveCart();
}

if ( buttonThatWasClicked == fileMgrButton.get() ) {
cartDir.revealToUser();
return;
Expand All @@ -199,7 +209,7 @@ void CartManager::buttonClicked(juce::Button *buttonThatWasClicked) {
}
return;
}

if ( buttonThatWasClicked == getDXCartButton.get() ) {
if ( mainWindow->processor->sysexComm.isInputActive() && mainWindow->processor->sysexComm.isOutputActive() ) {
unsigned char msg[] = { 0xF0, 0x43, 0x20, 0x00, 0xF7 };
Expand All @@ -216,19 +226,20 @@ void CartManager::fileDoubleClicked(const File& file) {
return;
mainWindow->loadCart(file);
activeCart->setCartridge(mainWindow->processor->currentCart);
updateCartFilename();
}

void CartManager::fileClicked(const File& file, const MouseEvent& e) {
if ( e.mods.isPopupMenu()) {
PopupMenu menu;

menu.addItem(1000, "Open location");
if ( ! file.isDirectory() ) {
menu.addItem(1010, "Send sysex cartridge to DX7");
}
menu.addSeparator();
menu.addItem(1020, "Refresh");

switch(menu.show()) {
case 1000:
file.revealToUser();
Expand Down Expand Up @@ -261,17 +272,17 @@ void CartManager::selectionChanged() {

if ( ! file.exists() )
return;

if ( file.isDirectory() )
return;

Cartridge browserSysex;
int rc = browserSysex.load(file);
if ( rc < 0 ) {
AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon, "Error", "Unable to open file");
return;
}

if ( rc != 0 ) {
browserCart->readOnly = true;
} else {
Expand All @@ -283,30 +294,30 @@ void CartManager::selectionChanged() {

void CartManager::programRightClicked(ProgramListBox *source, int pos) {
PopupMenu menu;

menu.addItem(1000, "Send program '" + source->programNames[pos] + "' to DX7");

if ( source == activeCart.get() )
menu.addItem(1010, "Send current sysex cartridge to DX7");

switch(menu.show()) {
case 1000:
uint8_t unpackPgm[161];

if ( source == activeCart.get() ) {
mainWindow->processor->currentCart.unpackProgram(unpackPgm, pos);
} else {
source->getCurrentCart().unpackProgram(unpackPgm, pos);
}

if ( mainWindow->processor->sysexComm.isOutputActive() ) {
uint8_t msg[163];
exportSysexPgm(msg, unpackPgm);
msg[2] |= mainWindow->processor->sysexComm.getChl();
mainWindow->processor->sysexComm.send(MidiMessage(msg, 163));
}
break;

case 1010:
mainWindow->processor->sendCurrentSysexCartridge();
break;
Expand All @@ -321,10 +332,10 @@ void CartManager::programDragged(ProgramListBox *destListBox, int dest, char *pa
mainWindow->updateUI();
} else {
File file = cartBrowser->getSelectedFile();

if ( ! file.exists() )
return;

if ( file.isDirectory() )
return;
if ( file.getSize() != 4104 && file.getSize() != 4096 )
Expand Down
55 changes: 45 additions & 10 deletions Source/CartManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@
#include "ProgramListBox.h"
#include "PluginData.h"

class CartridgeFileDisplay : public Component {
File file;
float clickableArea = 0;
public:
void setCartrdigeFile(File &file) {
this->file = file;
repaint();
}

void paint(Graphics& g) override {
g.setColour(Colours::whitesmoke);
if ( file.exists() ) {
g.setFont(g.getCurrentFont().withStyle(Font::underlined));
clickableArea = g.getCurrentFont().getStringWidthFloat(file.getFileName());
g.drawText(file.getFileName(), 0, 0, getWidth(), getHeight(), Justification::right);
g.setFont(g.getCurrentFont().withStyle(Font::plain));
g.drawText("Based on cartridge: ", 0, 0, getWidth() - (clickableArea + 2), getHeight(), Justification::right);
} else {
g.drawText("[No reference to original cartridge]", 0, 0, getWidth(), getHeight(), Justification::right);
clickableArea = 0;
}
}

void mouseDown(const MouseEvent &event) override {
if ( event.getMouseDownX() > getWidth() - clickableArea ) {
if ( file.exists() )
file.revealToUser();
}
}
};

class CartManager : public Component, public Button::Listener, public DragAndDropContainer, public FileBrowserListener
, public ProgramListBoxListener, public KeyListener {
std::unique_ptr<TextButton> newButton;
Expand All @@ -35,18 +66,19 @@ class CartManager : public Component, public Button::Listener, public DragAndDr
std::unique_ptr<TextButton> fileMgrButton;
std::unique_ptr<TextButton> getDXPgmButton;
std::unique_ptr<TextButton> getDXCartButton;

std::unique_ptr<ProgramListBox> activeCart;
std::unique_ptr<ProgramListBox> browserCart;

std::unique_ptr<FileFilter> syxFileFilter;

std::unique_ptr<FileTreeComponent> cartBrowser;
std::unique_ptr<TimeSliceThread> timeSliceThread;
std::unique_ptr<DirectoryContentsList> cartBrowserList;

std::unique_ptr<CartridgeFileDisplay> activeCartName;

File cartDir;

DexedAudioProcessorEditor *mainWindow;

void showSysexConfigMsg();
Expand All @@ -56,22 +88,25 @@ class CartManager : public Component, public Button::Listener, public DragAndDr
virtual ~CartManager();
void paint(Graphics& g) override;
void buttonClicked (Button* buttonThatWasClicked) override;

void selectionChanged() override;
void fileClicked (const File& file, const MouseEvent& e) override;
void fileDoubleClicked (const File& file) override;
void browserRootChanged (const File& newRoot) override;

void setActiveProgram(int idx, String activeName);
void resetActiveSysex();


void updateCartFilename();

void resized() override;

virtual void programSelected(ProgramListBox *source, int pos) override;
virtual void programRightClicked(ProgramListBox *source, int pos) override;
virtual void programDragged(ProgramListBox *destListBox, int dest, char *packedPgm) override;
virtual bool keyPressed(const KeyPress& key, Component* originatingComponent) override;

void initialFocus();
};


#endif // CARTMANAGER_H_INCLUDED
Loading

0 comments on commit 8fdafa3

Please sign in to comment.