ReaWwise

REAPER extension
Log | Files | Refs | Submodules

HierarchyMappingControls.cpp (5039B)


      1 /*----------------------------------------------------------------------------------------
      2 
      3 Copyright (c) 2023 AUDIOKINETIC Inc.
      4 
      5 This file is licensed to use under the license available at:
      6 https://github.com/audiokinetic/ReaWwise/blob/main/License.txt (the "License").
      7 You may not use this file except in compliance with the License.
      8 
      9 Unless required by applicable law or agreed to in writing, software distributed
     10 under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
     11 CONDITIONS OF ANY KIND, either express or implied.  See the License for the
     12 specific language governing permissions and limitations under the License.
     13 
     14 ----------------------------------------------------------------------------------------*/
     15 
     16 #include "HierarchyMappingControls.h"
     17 
     18 #include "BinaryData.h"
     19 #include "Helpers/ImportHelper.h"
     20 #include "Model/IDs.h"
     21 #include "Model/Import.h"
     22 
     23 namespace AK::WwiseTransfer
     24 {
     25 	namespace HierarchyMappingControlsConstants
     26 	{
     27 		constexpr int buttonSize = 24;
     28 	}
     29 
     30 	HierarchyMappingControls::HierarchyMappingControls(juce::ValueTree appState, ApplicationProperties& applicationProperties, const juce::String& applicationName)
     31 		: applicationState(appState)
     32 		, hierarchyMapping(applicationState.getChildWithName(IDs::hierarchyMapping))
     33 		, selectedRow(hierarchyMapping, IDs::selectedRow, nullptr)
     34 		, insertButton("InsertButton", juce::Drawable::createFromImageData(BinaryData::General_SmallAdd_Normal_svg, BinaryData::General_SmallAdd_Normal_svgSize))
     35 		, removeButton("RemoveButton", juce::Drawable::createFromImageData(BinaryData::General_SmallDelete_Normal_svg, BinaryData::General_SmallDelete_Normal_svgSize))
     36 		, moveUpButton("MoveUpButton", juce::Drawable::createFromImageData(BinaryData::General_ListMoveUp_Normal_svg, BinaryData::General_ListMoveUp_Normal_svgSize))
     37 		, moveDownButton("MoveDownButton", juce::Drawable::createFromImageData(BinaryData::General_ListMoveDown_Normal_svg, BinaryData::General_ListMoveDown_Normal_svgSize))
     38 		, presetMenuComponent(applicationState, applicationProperties, applicationName)
     39 	{
     40 		insertButton.setTooltip("Insert");
     41 		removeButton.setTooltip("Remove");
     42 		moveUpButton.setTooltip("Move Up");
     43 		moveDownButton.setTooltip("Move Down");
     44 
     45 		insertButton.onClick = [this]
     46 		{
     47 			hierarchyMapping.addChild(ImportHelper::hierarchyMappingNodeToValueTree(Import::HierarchyMappingNode("", Wwise::ObjectType::Unknown)), selectedRow, nullptr);
     48 		};
     49 
     50 		removeButton.onClick = [this]
     51 		{
     52 			hierarchyMapping.removeChild(selectedRow, nullptr);
     53 		};
     54 
     55 		moveUpButton.onClick = [this]
     56 		{
     57 			hierarchyMapping.moveChild(selectedRow, selectedRow - 1, nullptr);
     58 		};
     59 
     60 		moveDownButton.onClick = [this]
     61 		{
     62 			hierarchyMapping.moveChild(selectedRow, selectedRow + 1, nullptr);
     63 		};
     64 
     65 		addAndMakeVisible(insertButton);
     66 		addAndMakeVisible(removeButton);
     67 		addAndMakeVisible(moveUpButton);
     68 		addAndMakeVisible(moveDownButton);
     69 		addAndMakeVisible(presetMenuComponent);
     70 
     71 		applicationState.addListener(this);
     72 
     73 		refreshComponent();
     74 	}
     75 
     76 	HierarchyMappingControls::~HierarchyMappingControls()
     77 	{
     78 		applicationState.removeListener(this);
     79 	}
     80 
     81 	void HierarchyMappingControls::refreshComponent()
     82 	{
     83 		removeButton.setEnabled(hierarchyMapping.getNumChildren() > 1);
     84 		moveUpButton.setEnabled(selectedRow > 0);
     85 		moveDownButton.setEnabled(selectedRow >= 0 && selectedRow < hierarchyMapping.getNumChildren() - 1);
     86 	}
     87 
     88 	void HierarchyMappingControls::resized()
     89 	{
     90 		using namespace HierarchyMappingControlsConstants;
     91 
     92 		auto area = getLocalBounds();
     93 
     94 		juce::FlexBox buttonSection;
     95 		buttonSection.flexDirection = juce::FlexBox::Direction::column;
     96 		buttonSection.justifyContent = juce::FlexBox::JustifyContent::spaceBetween;
     97 
     98 		auto buttonSectionWidth = area.getWidth();
     99 		buttonSection.items.add(juce::FlexItem(buttonSectionWidth, buttonSize, presetMenuComponent));
    100 		buttonSection.items.add(juce::FlexItem(buttonSectionWidth, buttonSize, insertButton));
    101 		buttonSection.items.add(juce::FlexItem(buttonSectionWidth, buttonSize, removeButton));
    102 		buttonSection.items.add(juce::FlexItem(buttonSectionWidth, buttonSize, moveUpButton));
    103 		buttonSection.items.add(juce::FlexItem(buttonSectionWidth, buttonSize, moveDownButton));
    104 
    105 		buttonSection.performLayout(area);
    106 	}
    107 
    108 	void HierarchyMappingControls::valueTreePropertyChanged(juce::ValueTree& treeWhosePropertyHasChanged, const juce::Identifier& property)
    109 	{
    110 		if(treeWhosePropertyHasChanged == hierarchyMapping && property == selectedRow.getPropertyID())
    111 		{
    112 			triggerAsyncUpdate();
    113 		}
    114 	}
    115 
    116 	void HierarchyMappingControls::valueTreeChildAdded(juce::ValueTree& parentTree, juce::ValueTree& childWhichHasBeenAdded)
    117 	{
    118 		if(parentTree == hierarchyMapping)
    119 		{
    120 			triggerAsyncUpdate();
    121 		}
    122 	}
    123 
    124 	void HierarchyMappingControls::valueTreeChildRemoved(juce::ValueTree& parentTree, juce::ValueTree& childWhichHasBeenRemoved, int indexFromWhichChildWasRemoved)
    125 	{
    126 		if(parentTree == hierarchyMapping)
    127 		{
    128 			triggerAsyncUpdate();
    129 		}
    130 	}
    131 
    132 	void HierarchyMappingControls::handleAsyncUpdate()
    133 	{
    134 		refreshComponent();
    135 	}
    136 } // namespace AK::WwiseTransfer