2021-01-11 22:57:56 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
# include "CoreMinimal.h"
# include "MetasoundBuilderInterface.h"
# include "MetasoundDataReferenceCollection.h"
# include "MetasoundExecutableOperator.h"
# include "MetasoundFacade.h"
# include "MetasoundNode.h"
# include "MetasoundNodeInterface.h"
# include "MetasoundNodeRegistrationMacro.h"
# include "MetasoundOperatorInterface.h"
# include "MetasoundPrimitives.h"
2021-01-28 19:02:51 -04:00
# include "MetasoundStandardNodesNames.h"
2021-03-05 23:03:16 -04:00
# include "MetasoundStandardNodesCategories.h"
2021-03-24 03:22:53 -04:00
# include "MetasoundParamHelper.h"
2021-01-11 22:57:56 -04:00
2021-04-01 20:10:37 -04:00
# define LOCTEXT_NAMESPACE "MetasoundStandardNodes_DelayNode"
2021-01-11 22:57:56 -04:00
namespace Metasound
{
2021-03-24 03:22:53 -04:00
namespace TriggerDelayVertexNames
{
2021-04-01 20:10:37 -04:00
METASOUND_PARAM ( InputInTriggerDelay , " In " , " Input trigger which results in a delayed trigger. " ) ;
METASOUND_PARAM ( InputResetDelay , " Reset " , " Resets the trigger delay, clearing the execution task if pending. " ) ;
2021-03-24 03:22:53 -04:00
METASOUND_PARAM ( InputDelayTime , " Delay Time " , " Time to delay and execute deferred trigger in seconds. " ) ;
METASOUND_PARAM ( OutputOnTrigger , " Out " , " The delayed output trigger. " ) ;
}
2021-01-11 22:57:56 -04:00
class FTriggerDelayOperator : public TExecutableOperator < FTriggerDelayOperator >
{
public :
2021-01-28 19:02:51 -04:00
static const FNodeClassMetadata & GetNodeInfo ( ) ;
2021-02-03 15:40:56 -04:00
static const FVertexInterface & GetVertexInterface ( ) ;
2021-01-11 22:57:56 -04:00
static TUniquePtr < IOperator > CreateOperator ( const FCreateOperatorParams & InParams , FBuildErrorArray & OutErrors ) ;
2021-02-16 01:26:50 -04:00
FTriggerDelayOperator ( const FOperatorSettings & InSettings , const FTriggerReadRef & InTriggerReset , const FTriggerReadRef & InTriggerIn , const FTimeReadRef & InDelay ) ;
2021-01-11 22:57:56 -04:00
2023-05-22 13:28:27 -04:00
virtual void BindInputs ( FInputVertexInterfaceData & InOutVertexData ) override ;
virtual void BindOutputs ( FOutputVertexInterfaceData & InOutVertexData ) override ;
2021-01-11 22:57:56 -04:00
virtual FDataReferenceCollection GetInputs ( ) const override ;
virtual FDataReferenceCollection GetOutputs ( ) const override ;
void Execute ( ) ;
2023-03-02 14:40:35 -05:00
void Reset ( const IOperator : : FResetParams & InParams ) ;
2021-01-11 22:57:56 -04:00
private :
2022-09-30 16:18:48 -04:00
float SampleRate ;
2021-01-11 22:57:56 -04:00
FTriggerReadRef TriggerIn ;
FTriggerReadRef TriggerReset ;
FTriggerWriteRef TriggerOut ;
2021-03-24 03:22:53 -04:00
FTimeReadRef DelayTime ;
2021-01-11 22:57:56 -04:00
} ;
2021-03-24 03:22:53 -04:00
FTriggerDelayOperator : : FTriggerDelayOperator ( const FOperatorSettings & InSettings , const FTriggerReadRef & InTriggerReset , const FTriggerReadRef & InTriggerIn , const FTimeReadRef & InTimeDelay )
2022-09-30 16:18:48 -04:00
: SampleRate ( InSettings . GetSampleRate ( ) )
2021-03-24 03:22:53 -04:00
, TriggerIn ( InTriggerIn )
, TriggerReset ( InTriggerReset )
, TriggerOut ( FTriggerWriteRef : : CreateNew ( InSettings ) )
, DelayTime ( InTimeDelay )
2021-01-11 22:57:56 -04:00
{
}
2023-05-22 13:28:27 -04:00
void FTriggerDelayOperator : : BindInputs ( FInputVertexInterfaceData & InOutVertexData )
2021-01-11 22:57:56 -04:00
{
2021-03-24 03:22:53 -04:00
using namespace TriggerDelayVertexNames ;
2023-05-22 13:28:27 -04:00
InOutVertexData . BindReadVertex ( METASOUND_GET_PARAM_NAME ( InputInTriggerDelay ) , TriggerIn ) ;
InOutVertexData . BindReadVertex ( METASOUND_GET_PARAM_NAME ( InputResetDelay ) , TriggerReset ) ;
InOutVertexData . BindReadVertex ( METASOUND_GET_PARAM_NAME ( InputDelayTime ) , DelayTime ) ;
}
2021-03-24 03:22:53 -04:00
2023-05-22 13:28:27 -04:00
void FTriggerDelayOperator : : BindOutputs ( FOutputVertexInterfaceData & InOutVertexData )
{
using namespace TriggerDelayVertexNames ;
InOutVertexData . BindReadVertex ( METASOUND_GET_PARAM_NAME ( OutputOnTrigger ) , TriggerOut ) ;
}
FDataReferenceCollection FTriggerDelayOperator : : GetInputs ( ) const
{
// This should never be called. Bind(...) is called instead. This method
// exists as a stop-gap until the API can be deprecated and removed.
checkNoEntry ( ) ;
return { } ;
2021-01-11 22:57:56 -04:00
}
FDataReferenceCollection FTriggerDelayOperator : : GetOutputs ( ) const
{
2023-05-22 13:28:27 -04:00
// This should never be called. Bind(...) is called instead. This method
// exists as a stop-gap until the API can be deprecated and removed.
checkNoEntry ( ) ;
return { } ;
2021-01-11 22:57:56 -04:00
}
void FTriggerDelayOperator : : Execute ( )
{
// Advance internal counter to get rid of old triggers.
TriggerOut - > AdvanceBlock ( ) ;
2022-09-30 16:18:48 -04:00
TriggerIn - > ExecuteBlock (
[ & ] ( int32 StartFrame , int32 EndFrame )
{
} ,
[ this ] ( int32 StartFrame , int32 EndFrame )
{
const int32 FrameToTrigger = FMath : : Max ( 0 , FMath : : RoundToInt ( DelayTime - > GetSeconds ( ) * SampleRate ) ) + StartFrame ;
TriggerOut - > TriggerFrame ( FrameToTrigger ) ;
}
) ;
2021-02-16 01:26:50 -04:00
2021-02-03 14:36:36 -04:00
TriggerReset - > ExecuteBlock (
[ & ] ( int32 StartFrame , int32 EndFrame )
{
} ,
[ this ] ( int32 StartFrame , int32 EndFrame )
{
2022-09-30 16:18:48 -04:00
TriggerOut - > RemoveAfter ( StartFrame ) ;
2021-02-03 14:36:36 -04:00
}
) ;
2021-01-11 22:57:56 -04:00
}
2023-03-02 14:40:35 -05:00
void FTriggerDelayOperator : : Reset ( const IOperator : : FResetParams & InParams )
{
TriggerOut - > Reset ( ) ;
}
2021-01-11 22:57:56 -04:00
TUniquePtr < IOperator > FTriggerDelayOperator : : CreateOperator ( const FCreateOperatorParams & InParams , FBuildErrorArray & OutErrors )
{
2021-03-24 03:22:53 -04:00
using namespace TriggerDelayVertexNames ;
2021-02-03 15:40:56 -04:00
const FInputVertexInterface & InputInterface = GetVertexInterface ( ) . GetInputInterface ( ) ;
2022-09-30 16:18:48 -04:00
2021-03-24 21:23:57 -04:00
FTimeReadRef Delay = InParams . InputDataReferences . GetDataReadReferenceOrConstructWithVertexDefault < FTime > ( InputInterface , METASOUND_GET_PARAM_NAME ( InputDelayTime ) , InParams . OperatorSettings ) ;
2021-02-03 15:40:56 -04:00
2021-04-01 20:10:37 -04:00
FTriggerReadRef TriggerIn = InParams . InputDataReferences . GetDataReadReferenceOrConstruct < FTrigger > ( METASOUND_GET_PARAM_NAME ( InputInTriggerDelay ) , InParams . OperatorSettings ) ;
FTriggerReadRef TriggerReset = InParams . InputDataReferences . GetDataReadReferenceOrConstruct < FTrigger > ( METASOUND_GET_PARAM_NAME ( InputResetDelay ) , InParams . OperatorSettings ) ;
2021-01-11 22:57:56 -04:00
return MakeUnique < FTriggerDelayOperator > ( InParams . OperatorSettings , TriggerReset , TriggerIn , Delay ) ;
}
2021-02-03 15:40:56 -04:00
const FVertexInterface & FTriggerDelayOperator : : GetVertexInterface ( )
2021-01-11 22:57:56 -04:00
{
2021-03-24 03:22:53 -04:00
using namespace TriggerDelayVertexNames ;
2021-01-11 22:57:56 -04:00
static const FVertexInterface Interface (
FInputVertexInterface (
2022-03-31 16:49:59 -04:00
TInputDataVertex < FTrigger > ( METASOUND_GET_PARAM_NAME_AND_METADATA ( InputInTriggerDelay ) ) ,
TInputDataVertex < FTrigger > ( METASOUND_GET_PARAM_NAME_AND_METADATA ( InputResetDelay ) ) ,
TInputDataVertex < FTime > ( METASOUND_GET_PARAM_NAME_AND_METADATA ( InputDelayTime ) , 1.0f )
2021-01-11 22:57:56 -04:00
) ,
FOutputVertexInterface (
2022-03-31 16:49:59 -04:00
TOutputDataVertex < FTrigger > ( METASOUND_GET_PARAM_NAME_AND_METADATA ( OutputOnTrigger ) )
2021-03-24 03:22:53 -04:00
)
2021-01-11 22:57:56 -04:00
) ;
return Interface ;
}
2021-01-28 19:02:51 -04:00
const FNodeClassMetadata & FTriggerDelayOperator : : GetNodeInfo ( )
2021-01-11 22:57:56 -04:00
{
2021-01-28 19:02:51 -04:00
auto InitNodeInfo = [ ] ( ) - > FNodeClassMetadata
2021-01-11 22:57:56 -04:00
{
2021-01-28 19:02:51 -04:00
FNodeClassMetadata Info ;
2021-08-09 15:08:37 -04:00
Info . ClassName = { StandardNodes : : Namespace , TEXT ( " Trigger Delay " ) , TEXT ( " " ) } ;
2021-01-11 22:57:56 -04:00
Info . MajorVersion = 1 ;
Info . MinorVersion = 0 ;
2022-02-10 18:36:47 -05:00
Info . DisplayName = METASOUND_LOCTEXT ( " Metasound_TriggerDelayNodeDisplayName " , " Trigger Delay " ) ;
Info . Description = METASOUND_LOCTEXT ( " Metasound_TriggerDelayNodeDescription " , " Executes output trigger after the given delay time from the most recent execution of the input trigger . " ) ;
2021-01-11 22:57:56 -04:00
Info . Author = PluginAuthor ;
Info . PromptIfMissing = PluginNodeMissingPrompt ;
2021-02-03 15:40:56 -04:00
Info . DefaultInterface = GetVertexInterface ( ) ;
2021-08-09 15:08:37 -04:00
Info . CategoryHierarchy . Emplace ( NodeCategories : : Trigger ) ;
2021-01-11 22:57:56 -04:00
return Info ;
} ;
2021-01-28 19:02:51 -04:00
static const FNodeClassMetadata Info = InitNodeInfo ( ) ;
2021-01-11 22:57:56 -04:00
return Info ;
}
2021-04-01 20:10:37 -04:00
class METASOUNDSTANDARDNODES_API FTriggerDelayNode : public FNodeFacade
2021-01-11 22:57:56 -04:00
{
2021-04-01 20:10:37 -04:00
public :
FTriggerDelayNode ( const FNodeInitData & InInitData )
: FNodeFacade ( InInitData . InstanceName , InInitData . InstanceID , TFacadeOperatorClass < FTriggerDelayOperator > ( ) )
{
}
} ;
METASOUND_REGISTER_NODE ( FTriggerDelayNode )
2021-01-11 22:57:56 -04:00
}
# undef LOCTEXT_NAMESPACE // MetasoundTriggerDelayNode