Skip to content

Commit

Permalink
move LumatoneStateEditor setters to Controller class, refactor some f…
Browse files Browse the repository at this point in the history
…ile saving handling
  • Loading branch information
vsicurella committed Jan 15, 2024
1 parent 7315658 commit 0ece893
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 185 deletions.
66 changes: 55 additions & 11 deletions Source/LumatoneEditorState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ juce::RecentlyOpenedFilesList& LumatoneEditorState::getRecentFiles()

const juce::Array<LumatoneEditorColourPalette>& LumatoneEditorState::getColourPalettes()
{
loadColourPalettesFromFile();
return *colourPalettes;
}

Expand Down Expand Up @@ -182,6 +181,12 @@ bool LumatoneEditorState::doSendChangesToDevice() const
return LumatoneApplicationState::doSendChangesToDevice() && editorMode == EditorMode::ONLINE;
}

void LumatoneEditorState::setEditMode(EditorMode editMode)
{
editorMode = editMode;
state.setPropertyExcludingListener(this, LumatoneEditorProperty::EditorMode, (int)editorMode, nullptr);
}

juce::ValueTree LumatoneEditorState::loadStateProperties(juce::ValueTree stateIn)
{
juce::ValueTree newState = (stateIn.hasType(LumatoneEditorProperty::StateTree))
Expand Down Expand Up @@ -236,12 +241,12 @@ void LumatoneEditorState::handleStatePropertyChange(juce::ValueTree stateIn, con
}
}

void LumatoneEditorState::setColourPalettes(const juce::Array<LumatoneEditorColourPalette> &palettesIn)
void LumatoneEditorStateController::setColourPalettes(const juce::Array<LumatoneEditorColourPalette> &palettesIn)
{
*colourPalettes = palettesIn;
}

void LumatoneEditorState::loadColourPalettesFromFile()
void LumatoneEditorStateController::loadColourPalettesFromFile()
{
auto directory = getUserPalettesDirectory();
auto foundPaletteFiles = directory.findChildFiles(juce::File::TypesOfFileToFind::findFiles, true, '*' + juce::String(PALETTEFILEEXTENSION));
Expand All @@ -258,14 +263,20 @@ void LumatoneEditorState::loadColourPalettesFromFile()
setColourPalettes(newPalettes);
}

void LumatoneEditorState::addPalette(const LumatoneEditorColourPalette &newPalette)
const juce::Array<LumatoneEditorColourPalette>& LumatoneEditorStateController::getColourPalettes()
{
loadColourPalettesFromFile();
return LumatoneEditorState::getColourPalettes();
}

void LumatoneEditorStateController::addPalette(const LumatoneEditorColourPalette &newPalette)
{
colourPalettes->add(newPalette);
// TODO
state.setPropertyExcludingListener(this, LumatoneEditorProperty::ColourPalettes, "", nullptr);
}

bool LumatoneEditorState::deletePaletteFile(juce::File pathToPalette)
bool LumatoneEditorStateController::deletePaletteFile(juce::File pathToPalette)
{
bool success = false;

Expand All @@ -278,7 +289,7 @@ bool LumatoneEditorState::deletePaletteFile(juce::File pathToPalette)
}

// Open a SysEx mapping from the file specified in currentFile
bool LumatoneEditorState::resetToCurrentFile()
bool LumatoneEditorStateController::resetToCurrentFile()
{
if (getCurrentFile().getFullPathName().isEmpty())
{
Expand Down Expand Up @@ -325,23 +336,56 @@ bool LumatoneEditorState::resetToCurrentFile()
return false;
}

bool LumatoneEditorState::setCurrentFile(File fileToOpen)
bool LumatoneEditorStateController::setCurrentFile(File fileToOpen)
{
currentFile = fileToOpen;
state.setPropertyExcludingListener(this, LumatoneEditorProperty::CurrentFile, currentFile.getFullPathName(), nullptr);
return resetToCurrentFile();
}

// open a file from the "recent files" menu
bool LumatoneEditorState::openRecentFile(int recentFileIndex)
bool LumatoneEditorStateController::openRecentFile(int recentFileIndex)
{
jassert(recentFileIndex >= 0 && recentFileIndex < recentFiles->getNumFiles());
return setCurrentFile(recentFiles->getFile(recentFileIndex));
}

bool LumatoneEditorStateController::saveMappingToFile(juce::File fileToSave)
{
juce::StringArray stringArray = getMappingData()->toStringArray();
juce::String fileText = stringArray.joinIntoString("\n");

void LumatoneEditorState::setEditMode(EditorMode editMode)
bool success = false;

if (fileToSave.existsAsFile())
success = fileToSave.replaceWithText(fileText, false, false);

else if (fileToSave.create().ok)
{
success = fileToSave.appendText(fileText, false, false);
}

if (success && getCurrentFile() != fileToSave)
{
// TODO skip certain updates?
setCurrentFile(fileToSave);
}

return success;
}

bool LumatoneEditorStateController::savePropertiesFile() const
{
editorMode = editMode;
state.setPropertyExcludingListener(this, LumatoneEditorProperty::EditorMode, (int)editorMode, nullptr);
// TODO Save documents directories (Future: provide option to change them and save after changed by user)
//propertiesFile->setValue(LumatoneEditorProperty::UserDocumentsDirectory, getUserDocumentsDirectory().getFullPathName());
//propertiesFile->setValue(LumatoneEditorProperty::UserMappingsDirectory, getUserMappingsDirectory().getFullPathName());
//propertiesFile->setValue(LumatoneEditorProperty::UserPalettesDirectory, getUserPalettesDirectory().getFullPathName());

// Save recent files list
recentFiles->removeNonExistentFiles();
jassert(propertiesFile != nullptr);
propertiesFile->setValue(LumatoneEditorProperty::RecentFiles, recentFiles->toString());

return propertiesFile->saveIfNeeded();
}

76 changes: 42 additions & 34 deletions Source/LumatoneEditorState.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class LumatoneEditorLookAndFeel;
class LumatoneController;
class LumatoneEditorColourPalette;

class TerpstraSysExApplication;

namespace LumatoneEditorProperty
{
static const juce::Identifier StateTree = juce::Identifier("LumatoneEditorState");
Expand Down Expand Up @@ -61,7 +59,7 @@ namespace LumatoneEditorProperty
static const juce::Identifier IsomorphicMassAssign = juce::Identifier("IsomorphicMassAssign");

static const juce::Identifier LastSettingsPanel = juce::Identifier("LastSettingsPanel");

static const juce::Identifier LastColourWindowTab = juce::Identifier("LastColourWindowTab");
}

enum class EditorMode
Expand All @@ -72,13 +70,15 @@ enum class EditorMode

static juce::Array<juce::Identifier> GetLumatoneEditorProperties();

class LumatoneEditorStateController;

class LumatoneEditorState : public LumatoneApplicationState
{
public:

LumatoneEditorState(juce::String name, LumatoneFirmwareDriver& driverIn, juce::UndoManager* undoManagerIn);
LumatoneEditorState(juce::String name, const LumatoneEditorState& stateIn);

protected:
LumatoneEditorState(juce::String name, LumatoneFirmwareDriver& driverIn, juce::UndoManager* undoManagerIn);
public:
~LumatoneEditorState() override;

const juce::String getApplicationName() const { return ProjectInfo::projectName; }
Expand All @@ -93,14 +93,12 @@ class LumatoneEditorState : public LumatoneApplicationState

LumatoneEditorLookAndFeel& getEditorLookAndFeel() { return *lookAndFeel; }

//const juce::Array<LumatoneEditorColourPalette>& getColourPalettes() { return colourPalettes; }
const juce::Array<LumatoneEditorColourPalette>& getColourPalettes();
virtual const juce::Array<LumatoneEditorColourPalette>& getColourPalettes();

const LumatoneEditorFontLibrary& getAppFonts() const { return *appFonts; }

juce::String getProperty(juce::Identifier propertyId, juce::String fallbackValue=juce::String()) const;

// juce::PropertiesFile& getPropertiesFile() { return *propertiesFile; }
juce::File getCurrentFile() const { return currentFile; }

juce::RecentlyOpenedFilesList& getRecentFiles();
Expand All @@ -109,8 +107,6 @@ class LumatoneEditorState : public LumatoneApplicationState
juce::File getUserMappingsDirectory() const;
juce::File getUserPalettesDirectory() const;

// RecentlyOpenedFilesList& getRecentFileList() { return *recentFiles; }
// Font getAppFont(LumatoneEditorFont fontIdIn, float height = 12.0f) { return appFonts.getFont(fontIdIn, height); }

public:
bool doSendChangesToDevice() const override;
Expand All @@ -119,28 +115,10 @@ class LumatoneEditorState : public LumatoneApplicationState
juce::ValueTree loadStateProperties(juce::ValueTree stateIn) override;
void handleStatePropertyChange(juce::ValueTree stateIn, const juce::Identifier& property) override;

bool resetToCurrentFile();
bool openRecentFile(int recentFileIndex);

void addPalette(const LumatoneEditorColourPalette& newPalette);
bool deletePaletteFile(juce::File pathToPalette);

public:



// Only main app should access these
private:
void setColourPalettes(const juce::Array<LumatoneEditorColourPalette>& palettesIn);
void loadColourPalettesFromFile();

bool setCurrentFile(juce::File fileToOpen);

void setEditMode(EditorMode editMode);

void setHasChangesToSave(bool hasChangesToSave);
void setCalibrationMode(bool calibrationModeOn);
void setDeveloperMode(bool developerModeOn);
virtual void setHasChangesToSave(bool hasChangesToSave);
virtual void setCalibrationMode(bool calibrationModeOn);
virtual void setDeveloperMode(bool developerModeOn);
virtual void setEditMode(EditorMode editMode);

protected:
bool hasChangesToSave = false;
Expand All @@ -161,7 +139,37 @@ class LumatoneEditorState : public LumatoneApplicationState

std::shared_ptr<juce::PropertiesFile> propertiesFile;

friend class TerpstraSysExApplication;
friend class LumatoneEditorStateController;
};

class LumatoneEditorStateController : public LumatoneEditorState
{
public:
LumatoneEditorStateController(juce::String name, LumatoneFirmwareDriver& driverIn, juce::UndoManager* undoManagerIn)
: LumatoneEditorState(name, driverIn, undoManagerIn) {}
LumatoneEditorStateController(juce::String name, const LumatoneEditorState& stateIn)
: LumatoneEditorState(name, stateIn) {}

const juce::Array<LumatoneEditorColourPalette>& getColourPalettes() override;

bool resetToCurrentFile();
bool openRecentFile(int recentFileIndex);

void addPalette(const LumatoneEditorColourPalette& newPalette);
bool deletePaletteFile(juce::File pathToPalette);

void setColourPalettes(const juce::Array<LumatoneEditorColourPalette>& palettesIn);
void loadColourPalettesFromFile();

bool setCurrentFile(juce::File fileToOpen);
bool saveMappingToFile(juce::File fileToSave);

juce::PropertiesFile* getPropertiesFile() const { return propertiesFile.get(); }
bool savePropertiesFile() const;

void setDeveloperMode(bool developerModeOn) override { LumatoneEditorState::setDeveloperMode(developerModeOn); }
void setEditMode(EditorMode editMode) override { LumatoneEditorState::setEditMode(editMode); }
};


#endif LUMATONE_EDITOR_STATE_H
5 changes: 2 additions & 3 deletions Source/LumatoneMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace Lumatone {
namespace Menu {

MainMenuModel::MainMenuModel(const LumatoneEditorState& state, ApplicationCommandManager* commandManager)
: LumatoneEditorState("MainMenu", state)
MainMenuModel::MainMenuModel(const LumatoneEditorState& state, juce::ApplicationCommandManager* commandManager)
: LumatoneEditorStateController("MainMenu", state)
{
theManager = commandManager;
setApplicationCommandManagerToWatch(commandManager);
Expand Down Expand Up @@ -81,7 +81,6 @@ namespace Lumatone {
if (menuItemID >= recentFilesBaseID && menuItemID < recentFilesBaseID + 100)
{
// open a file from the "recent files" menu
// TerpstraSysExApplication::getApp().openRecentFile(menuItemID - recentFilesBaseID);
openRecentFile(menuItemID - recentFilesBaseID);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/LumatoneMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Lumatone {
};

class MainMenuModel : public juce::MenuBarModel
, public LumatoneEditorState
, public LumatoneEditorStateController
{
public:
MainMenuModel(const LumatoneEditorState& stateIn, ApplicationCommandManager* commandManager);
Expand Down
Loading

0 comments on commit 0ece893

Please sign in to comment.