ReaWwise

REAPER extension
Log | Files | Refs | Submodules

ImportDestinationComponent.cpp (5202B)


      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 "ImportDestinationComponent.h"
     17 
     18 #include "BinaryData.h"
     19 #include "Persistance/ApplicationState.h"
     20 #include "Theme/CustomLookAndFeel.h"
     21 
     22 namespace AK::WwiseTransfer
     23 {
     24 	namespace ImportDestinationComponentConstants
     25 	{
     26 		constexpr int margin = 10;
     27 		constexpr int spacing = 4;
     28 		constexpr int editorBoxHeight = 26;
     29 		constexpr int labelWidth = 122;
     30 		constexpr int syncButtonWidth = 36;
     31 		constexpr int iconSize = 16;
     32 	} // namespace ImportDestinationComponentConstants
     33 
     34 	ImportDestinationComponent::ImportDestinationComponent(juce::ValueTree appState, WaapiClient& waapiClient)
     35 		: applicationState(appState)
     36 		, waapiClient(waapiClient)
     37 		, importDestination(applicationState, IDs::importDestination, nullptr)
     38 		, importDestinationType(applicationState, IDs::importDestinationType, nullptr)
     39 		, projectPath(applicationState, IDs::projectPath, nullptr)
     40 		, updateImportDestinationButton("UpdateImportDestinationButton", juce::Drawable::createFromImageData(BinaryData::General_GetFromWwise_Normal_svg, BinaryData::General_GetFromWwise_Normal_svgSize))
     41 	{
     42 		importDestinationLabel.setText("Import Destination", juce::dontSendNotification);
     43 		importDestinationLabel.setBorderSize(juce::BorderSize(0));
     44 		importDestinationLabel.setMinimumHorizontalScale(1.0f);
     45 		importDestinationLabel.setJustificationType(juce::Justification::right);
     46 
     47 		importDestinationEditor.setFont(CustomLookAndFeelConstants::smallFontSize);
     48 		importDestinationEditor.getTextValue().referTo(importDestination.getPropertyAsValue());
     49 		importDestinationEditor.getValidationValue().referTo(applicationState.getPropertyAsValue(IDs::importDestinationValid, nullptr));
     50 		importDestinationEditor.getErrorMessageValue().referTo(applicationState.getPropertyAsValue(IDs::importDestinationErrorMessage, nullptr));
     51 
     52 		updateImportDestinationButton.setTooltip("Sync with selected object in Wwise");
     53 
     54 		updateImportDestinationButton.onClick = [this]
     55 		{
     56 			updateImportDestination();
     57 		};
     58 
     59 		applicationState.addListener(this);
     60 
     61 		addAndMakeVisible(importDestinationLabel);
     62 		addAndMakeVisible(importDestinationEditor);
     63 		addAndMakeVisible(updateImportDestinationButton);
     64 		addAndMakeVisible(objectTypeIconComposite);
     65 
     66 		refreshComponent();
     67 	}
     68 
     69 	ImportDestinationComponent::~ImportDestinationComponent()
     70 	{
     71 		applicationState.removeListener(this);
     72 	}
     73 
     74 	void ImportDestinationComponent::resized()
     75 	{
     76 		using namespace ImportDestinationComponentConstants;
     77 
     78 		auto area = getLocalBounds();
     79 
     80 		auto importDestinationSection = area.removeFromTop(editorBoxHeight);
     81 		{
     82 			importDestinationLabel.setBounds(importDestinationSection.removeFromLeft(labelWidth));
     83 			importDestinationSection.removeFromLeft(margin);
     84 
     85 			updateImportDestinationButton.setBounds(importDestinationSection.removeFromRight(syncButtonWidth));
     86 			importDestinationSection.removeFromRight(spacing);
     87 
     88 			objectTypeIconComposite.setBounds(importDestinationSection.removeFromRight(iconSize).withSizeKeepingCentre(iconSize, iconSize));
     89 
     90 			importDestinationSection.removeFromRight(spacing);
     91 
     92 			importDestinationEditor.setBounds(importDestinationSection);
     93 		}
     94 	}
     95 
     96 	void ImportDestinationComponent::refreshComponent()
     97 	{
     98 		auto projectPathEmpty = projectPath.get().isEmpty();
     99 
    100 		updateImportDestinationButton.setEnabled(!projectPathEmpty);
    101 
    102 		auto* customLookAndFeel = dynamic_cast<CustomLookAndFeel*>(&getLookAndFeel());
    103 
    104 		if(customLookAndFeel)
    105 		{
    106 			// Reset the icon and add it as a child of the composite. The composite will refresh the icon automatically
    107 			objectTypeIconComposite.removeAllChildren();
    108 			objectTypeIcon = customLookAndFeel->getIconForObjectType(importDestinationType);
    109 			objectTypeIconComposite.addAndMakeVisible(objectTypeIcon.get());
    110 		}
    111 	}
    112 
    113 	void ImportDestinationComponent::updateImportDestination()
    114 	{
    115 		auto onGetSelectedObject = [this](const Waapi::Response<Waapi::ObjectResponse>& response)
    116 		{
    117 			if(response.result.path.isEmpty())
    118 				return juce::AlertWindow::showMessageBoxAsync(juce::MessageBoxIconType::InfoIcon, "Import Destination", "No object is selected in Wwise. Please select one and try again.");
    119 
    120 			importDestination = response.result.path;
    121 		};
    122 
    123 		waapiClient.getSelectedObjectAsync(onGetSelectedObject);
    124 	}
    125 
    126 	void ImportDestinationComponent::valueTreePropertyChanged(juce::ValueTree& treeWhosePropertyHasChanged, const juce::Identifier& property)
    127 	{
    128 		triggerAsyncUpdate();
    129 	}
    130 
    131 	void ImportDestinationComponent::handleAsyncUpdate()
    132 	{
    133 		refreshComponent();
    134 	}
    135 } // namespace AK::WwiseTransfer