From 4140b3b85b4cbf6dee59e5ee69f053b6782dbdf1 Mon Sep 17 00:00:00 2001 From: Alex Birch Date: Sat, 13 Jul 2019 22:37:26 +0100 Subject: [PATCH] progress integrating table with store, decoupling from fluidsynth model --- Source/FluidSynthModel.cpp | 26 ++++----- Source/FluidSynthModel.h | 22 +++---- Source/TableComponent.cpp | 116 +++++++++++++++++++++++++------------ Source/TableComponent.h | 29 +++++++--- Source/TablesComponent.cpp | 43 +++++++------- Source/TablesComponent.h | 6 +- Source/Util.h | 8 +++ 7 files changed, 158 insertions(+), 92 deletions(-) diff --git a/Source/FluidSynthModel.cpp b/Source/FluidSynthModel.cpp index 2d33629..c11ba06 100644 --- a/Source/FluidSynthModel.cpp +++ b/Source/FluidSynthModel.cpp @@ -324,7 +324,7 @@ void FluidSynthModel::onFileNameChanged(const String &absPath, int bank, int pre valueTree.setPropertyExcludingListener(this, "path", absPath, nullptr); // valueTree.setPropertyExcludingListener(this, "soundFontPath", absPath, nullptr); // sharedParams.setSoundFontPath(absPath); - eventListeners.call(&FluidSynthModel::Listener::fontChanged, this, absPath); + // eventListeners.call(&FluidSynthModel::Listener::fontChanged, this, absPath); } void FluidSynthModel::unloadAndLoadFont(const String &absPath) { @@ -377,8 +377,8 @@ void FluidSynthModel::loadFont(const String &absPath) { valueTreeState.state.getChildWithName("presets") = presets; } -FluidSynthModel::Listener::~Listener() { -} +// FluidSynthModel::Listener::~Listener() { +// } bool FluidSynthModel::shouldLoadFont(const String &absPath) { if (absPath.isEmpty()) { @@ -390,23 +390,23 @@ bool FluidSynthModel::shouldLoadFont(const String &absPath) { return true; } -void FluidSynthModel::Listener::fontChanged(FluidSynthModel * model, const String &absPath) { -} +// void FluidSynthModel::Listener::fontChanged(FluidSynthModel * model, const String &absPath) { +// } const String& FluidSynthModel::getCurrentSoundFontAbsPath() { return currentSoundFontAbsPath; } //============================================================================== -void FluidSynthModel::addListener (FluidSynthModel::Listener* const newListener) -{ - eventListeners.add(newListener); -} +// void FluidSynthModel::addListener (FluidSynthModel::Listener* const newListener) +// { +// eventListeners.add(newListener); +// } -void FluidSynthModel::removeListener (FluidSynthModel::Listener* const listener) -{ - eventListeners.remove(listener); -} +// void FluidSynthModel::removeListener (FluidSynthModel::Listener* const listener) +// { +// eventListeners.remove(listener); +// } void FluidSynthModel::setSampleRate(float sampleRate) { currentSampleRate = sampleRate; diff --git a/Source/FluidSynthModel.h b/Source/FluidSynthModel.h index 9f08a4b..89dd7ef 100644 --- a/Source/FluidSynthModel.h +++ b/Source/FluidSynthModel.h @@ -43,26 +43,26 @@ public: @see Button::addListener, Button::removeListener */ - class Listener - { - public: - /** Destructor. */ - virtual ~Listener(); + // class Listener + // { + // public: + // /** Destructor. */ + // virtual ~Listener(); - /** Called when the button is clicked. */ - virtual void fontChanged (FluidSynthModel*, const String &absPath); - }; + // /** Called when the button is clicked. */ + // virtual void fontChanged (FluidSynthModel*, const String &absPath); + // }; /** Registers a listener to receive events when this button's state changes. If the listener is already registered, this will not register it again. @see removeListener */ - void addListener (Listener* newListener); + // void addListener (Listener* newListener); /** Removes a previously-registered button listener @see addListener */ - void removeListener (Listener* listener); + // void removeListener (Listener* listener); void setSampleRate(float sampleRate); @@ -137,7 +137,7 @@ private: // fluid_mod_t* mod; - ListenerList eventListeners; + // ListenerList eventListeners; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FluidSynthModel) }; diff --git a/Source/TableComponent.cpp b/Source/TableComponent.cpp index 219aad4..8888c18 100644 --- a/Source/TableComponent.cpp +++ b/Source/TableComponent.cpp @@ -7,8 +7,10 @@ // #include "TableComponent.h" +#include "Util.h" using namespace std; +using namespace Util; //============================================================================== /** @@ -17,15 +19,16 @@ using namespace std; TableComponent::TableComponent( AudioProcessorValueTreeState& valueTreeState, // const vector &columns, - const vector> &rows, +// const vector &rows, const function &onRowSelected, // const function&)> &rowToIDMapper, int initiallySelectedRow ) -: font (14.0f) -, columns(columns) -, rows(rows) -, onRowSelected(onRowSelected)/*, +: valueTreeState{valueTreeState} +, font{14.0f} +//, columns{columns} +//, rows{rows} +, onRowSelected{onRowSelected}/*, rowToIDMapper(rowToIDMapper)*/ { // Create our table component and add it to this component.. @@ -104,26 +107,34 @@ TableComponent::~TableComponent() { void TableComponent::valueTreePropertyChanged( ValueTree& treeWhosePropertyHasChanged, const Identifier& property) { - if (treeWhosePropertyHasChanged.getType() == StringRef("soundFont")) { + if (treeWhosePropertyHasChanged.getType() == StringRef("presets")) { + rows.clear(); + int numChildren{treeWhosePropertyHasChanged.getNumChildren()}; + for(int i{0}; i>& rows, int initiallySelectedRow) { - this->rows = rows; - table.deselectAllRows(); - table.updateContent(); - table.getHeader().setSortColumnId(0, true); - table.selectRow(initiallySelectedRow); - table.repaint(); -} +// void TableComponent::setRows(const vector>& rows, int initiallySelectedRow) { +// this->rows = rows; +// table.deselectAllRows(); +// table.updateContent(); +// table.getHeader().setSortColumnId(0, true); +// table.selectRow(initiallySelectedRow); +// table.repaint(); +// } // This is overloaded from TableListBoxModel, and must return the total number of rows in our table int TableComponent::getNumRows() @@ -160,7 +171,14 @@ void TableComponent::paintCell ( g.setColour (getLookAndFeel().findColour (ListBox::textColourId)); g.setFont (font); - g.drawText (rows[rowNumber][columnId-1], 2, 0, width - 4, height, Justification::centredLeft, true); + TableRow& row{rows[rowNumber]}; + String text; + if (columnId <= 1) { + text = String(row.preset); + } else { + text = row.name; + } + g.drawText (text, 2, 0, width - 4, height, Justification::centredLeft, true); g.setColour (getLookAndFeel().findColour (ListBox::backgroundColourId)); g.fillRect (width - 1, 0, 1, height); @@ -174,20 +192,25 @@ void TableComponent::sortOrderChanged ( ) { if (newSortColumnId != 0) { int selectedRowIx = table.getSelectedRow(); - vector selectedRow; - if (selectedRowIx >= 0) { - selectedRow = rows[selectedRowIx]; - } + // TableRow* selectedRow; + // if (selectedRowIx >= 0) { + // selectedRow = &rows[selectedRowIx]; + // } TableComponent::DataSorter sorter (newSortColumnId, isForwards); sort(rows.begin(), rows.end(), sorter); table.updateContent(); + RangedAudioParameter *param {valueTreeState.getParameter("preset")}; + jassert(dynamic_cast (param) != nullptr); + AudioParameterInt* castParam {dynamic_cast (param)}; + int value{castParam->get()}; + if (selectedRowIx >= 0) { for (auto it = rows.begin(); it != rows.end(); ++it) { - if(*it == selectedRow) { - int index = static_cast(std::distance(rows.begin(), it)); + if(it->preset == value) { + int index {static_cast(std::distance(rows.begin(), it))}; table.selectRow(index); break; } @@ -206,8 +229,15 @@ int TableComponent::getColumnAutoSizeWidth (int columnId) { int widest = 32; // find the widest bit of text in this column.. - for (int i = getNumRows(); --i >= 0;) { - widest = jmax (widest, font.getStringWidth (rows[i][columnId-1])); + for (int i{getNumRows()}; --i >= 0;) { + TableRow& row{rows[i]}; + String text; + if (columnId <= 1) { + text = String(row.preset); + } else { + text = row.name; + } + widest = jmax (widest, font.getStringWidth (text)); } return widest + 8; @@ -226,20 +256,24 @@ TableComponent::DataSorter::DataSorter ( int columnByWhichToSort, bool forwards ) - : columnByWhichToSort (columnByWhichToSort), - direction (forwards ? 1 : -1) +: columnByWhichToSort (columnByWhichToSort) +, direction (forwards ? 1 : -1) {} bool TableComponent::DataSorter::operator ()( - vector first, - vector second + TableRow first, + TableRow second ) { - int result = String(first[columnByWhichToSort-1]) - .compareNatural (String(second[columnByWhichToSort-1])); - - if (result == 0) - result = String(first[0]) - .compareNatural (String(second[0])); + int result; + if (columnByWhichToSort <= 1) { + result = compare(first.preset, second.preset); + } else { + result = first.name + .compareNatural (second.name); + if (result == 0) { + result = compare(first.preset, second.preset); + } + } result *= direction; @@ -252,7 +286,7 @@ void TableComponent::selectedRowsChanged (int row) { } // onRowSelected(rowToIDMapper(rows[row])); // onRowSelected(stoi(rows[row][0])); - int newPreset{stoi(rows[row][0])}; + int newPreset{rows[row].preset}; RangedAudioParameter *param {valueTreeState.getParameter("preset")}; jassert(dynamic_cast (param) != nullptr); AudioParameterInt* castParam {dynamic_cast (param)}; @@ -262,3 +296,11 @@ void TableComponent::selectedRowsChanged (int row) { bool TableComponent::keyPressed(const KeyPress &key) { return table.keyPressed(key); } + +TableRow::TableRow( + int preset, + String name +) +: preset{preset} +: name{name} +{} diff --git a/Source/TableComponent.h b/Source/TableComponent.h index 7445e44..7153923 100644 --- a/Source/TableComponent.h +++ b/Source/TableComponent.h @@ -15,6 +15,21 @@ using namespace std; +class TableRow { +public: + TableRow( + int preset, + String name + ); +private: + int preset; + String name; + + friend class TableComponent; + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TableRow) +}; + + class TableComponent : public Component, public TableListBoxModel, public ValueTree::Listener/*, @@ -23,7 +38,7 @@ public: TableComponent( AudioProcessorValueTreeState& valueTreeState, // const vector &columns, - const vector> &rows, + // const vector &rows, const function &onRowSelected, // const function&)> &rowToIDMapper, int initiallySelectedRow @@ -56,7 +71,7 @@ public: void resized() override; - void setRows(const vector>& rows, int initiallySelectedRow); +// void setRows(const vector& rows, int initiallySelectedRow); bool keyPressed(const KeyPress &key) override; @@ -79,8 +94,8 @@ private: TableListBox table; // the table component itself Font font; - vector columns; - vector> rows; +// vector columns; + vector rows; function onRowSelected; // function&)> rowToIDMapper; @@ -94,8 +109,8 @@ private: ); bool operator ()( - vector first, - vector second + TableRow first, + TableRow second ); private: @@ -104,4 +119,4 @@ private: }; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TableComponent) -}; \ No newline at end of file +}; diff --git a/Source/TablesComponent.cpp b/Source/TablesComponent.cpp index 6e45cbb..1f89040 100644 --- a/Source/TablesComponent.cpp +++ b/Source/TablesComponent.cpp @@ -35,11 +35,12 @@ TablesComponent::TablesComponent( }; presetTable = new TableComponent( + valueTreeState, // {"#", "Name"}, - mapPresets( - banksToPresets, - selectedBank - ), + // mapPresets( + // banksToPresets, + // selectedBank + // ), [this](int preset){ this->onPresetSelected(preset); }, @@ -169,24 +170,24 @@ bool TablesComponent::keyPressed(const KeyPress &key) { return presetTable->keyPressed(key); } -void TablesComponent::fontChanged(FluidSynthModel *, const String &) { - banksToPresets = fluidSynthModel.getBanks(); +// void TablesComponent::fontChanged(FluidSynthModel *, const String &) { +// banksToPresets = fluidSynthModel.getBanks(); - fluid_preset_t* currentPreset = getCurrentPreset(); +// fluid_preset_t* currentPreset = getCurrentPreset(); - selectedBank = fluid_preset_get_banknum(currentPreset); - int selectedPreset = fluid_preset_get_num(currentPreset); +// selectedBank = fluid_preset_get_banknum(currentPreset); +// int selectedPreset = fluid_preset_get_num(currentPreset); - presetTable->setRows( - mapPresets( - banksToPresets, - selectedBank - ), - presetToIndexMapper(selectedPreset) - ); +// presetTable->setRows( +// mapPresets( +// banksToPresets, +// selectedBank +// ), +// presetToIndexMapper(selectedPreset) +// ); - banks->setItems( - mapBanks(banksToPresets), - selectedBank - ); -} +// banks->setItems( +// mapBanks(banksToPresets), +// selectedBank +// ); +// } diff --git a/Source/TablesComponent.h b/Source/TablesComponent.h index 9f57815..9156db6 100644 --- a/Source/TablesComponent.h +++ b/Source/TablesComponent.h @@ -15,8 +15,8 @@ using namespace std; -class TablesComponent : public Component, - public FluidSynthModel::Listener +class TablesComponent : public Component/*, + public FluidSynthModel::Listener */ { public: TablesComponent( @@ -28,7 +28,7 @@ public: void resized() override; bool keyPressed(const KeyPress &key) override; - void fontChanged(FluidSynthModel *, const String &) override; + // void fontChanged(FluidSynthModel *, const String &) override; private: AudioProcessorValueTreeState& valueTreeState; diff --git a/Source/Util.h b/Source/Util.h index c186f83..85de943 100644 --- a/Source/Util.h +++ b/Source/Util.h @@ -9,3 +9,11 @@ #define DEBUG_PRINT(str) #endif #endif + +namespace Util { + inline int compare(int a, int b) { + if (a > b) return 1; + if (a == b) return 0; + return -1; + } +} \ No newline at end of file