2021-11-26 15:48:13 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
# include "SmartObjectDefinition.h"
2022-03-02 13:25:37 -05:00
# include "SmartObjectSettings.h"
2021-11-26 15:48:13 -05:00
# include "SmartObjectTypes.h"
2022-01-07 14:28:06 -05:00
namespace UE : : SmartObject
{
const FVector DefaultSlotSize ( 40 , 40 , 90 ) ;
}
2022-03-02 13:25:37 -05:00
USmartObjectDefinition : : USmartObjectDefinition ( const FObjectInitializer & ObjectInitializer ) : UDataAsset ( ObjectInitializer )
{
TagFilteringPolicy = GetDefault < USmartObjectSettings > ( ) - > DefaultTagFilteringPolicy ;
}
2021-11-26 15:48:13 -05:00
bool USmartObjectDefinition : : Validate ( ) const
{
bValid = false ;
if ( Slots . Num ( ) = = 0 )
{
UE_LOG ( LogSmartObject , Error , TEXT ( " Need to provide at least one slot definition " ) ) ;
return false ;
}
// Detect null entries in default definitions
int32 NullEntryIndex ;
if ( DefaultBehaviorDefinitions . Find ( nullptr , NullEntryIndex ) )
{
UE_LOG ( LogSmartObject , Error , TEXT ( " Null entry found at index %d in default behavior definition list " ) , NullEntryIndex ) ;
return false ;
}
// Detect null entries in slot definitions
for ( int i = 0 ; i < Slots . Num ( ) ; + + i )
{
2022-01-19 14:16:16 -05:00
const FSmartObjectSlotDefinition & Slot = Slots [ i ] ;
2021-11-26 15:48:13 -05:00
if ( Slot . BehaviorDefinitions . Find ( nullptr , NullEntryIndex ) )
{
UE_LOG ( LogSmartObject , Error , TEXT ( " Null definition entry found at index %d in behavior list of slot %d " ) , i , NullEntryIndex ) ;
return false ;
}
}
// Detect missing definitions in slots if no default one are provided
if ( DefaultBehaviorDefinitions . Num ( ) = = 0 )
{
for ( int i = 0 ; i < Slots . Num ( ) ; + + i )
{
2022-01-19 14:16:16 -05:00
const FSmartObjectSlotDefinition & Slot = Slots [ i ] ;
2021-11-26 15:48:13 -05:00
if ( Slot . BehaviorDefinitions . Num ( ) = = 0 )
{
UE_LOG ( LogSmartObject , Error , TEXT ( " Slot at index %d needs to provide a behavior definition since there is no default one in the SmartObject definition " ) , i ) ;
return false ;
}
}
}
bValid = true ;
return true ;
}
const USmartObjectBehaviorDefinition * USmartObjectDefinition : : GetBehaviorDefinition ( const FSmartObjectSlotIndex & SlotIndex ,
2022-03-02 13:25:37 -05:00
const TSubclassOf < USmartObjectBehaviorDefinition > & DefinitionClass ) const
2021-11-26 15:48:13 -05:00
{
const USmartObjectBehaviorDefinition * Definition = nullptr ;
if ( Slots . IsValidIndex ( SlotIndex ) )
{
Definition = GetBehaviorDefinitionByType ( Slots [ SlotIndex ] . BehaviorDefinitions , DefinitionClass ) ;
}
if ( Definition = = nullptr )
{
Definition = GetBehaviorDefinitionByType ( DefaultBehaviorDefinitions , DefinitionClass ) ;
}
return Definition ;
}
2022-01-07 14:28:06 -05:00
FBox USmartObjectDefinition : : GetBounds ( ) const
{
FBox BoundingBox ( ForceInitToZero ) ;
2022-01-19 14:16:16 -05:00
for ( const FSmartObjectSlotDefinition & Slot : GetSlots ( ) )
2022-01-07 14:28:06 -05:00
{
BoundingBox + = Slot . Offset + UE : : SmartObject : : DefaultSlotSize ;
BoundingBox + = Slot . Offset - UE : : SmartObject : : DefaultSlotSize ;
}
return BoundingBox ;
}
2021-11-26 15:48:13 -05:00
TOptional < FTransform > USmartObjectDefinition : : GetSlotTransform ( const FTransform & OwnerTransform , const FSmartObjectSlotIndex SlotIndex ) const
{
TOptional < FTransform > Transform ;
2022-01-19 14:16:16 -05:00
if ( ensureMsgf ( Slots . IsValidIndex ( SlotIndex ) , TEXT ( " Requesting slot transform for an out of range index: %s " ) , * LexToString ( SlotIndex ) ) )
2021-11-26 15:48:13 -05:00
{
2022-01-19 14:16:16 -05:00
const FSmartObjectSlotDefinition & Slot = Slots [ SlotIndex ] ;
2022-01-07 14:28:06 -05:00
Transform = FTransform ( Slot . Rotation , Slot . Offset ) * OwnerTransform ;
2021-11-26 15:48:13 -05:00
}
return Transform ;
}
const USmartObjectBehaviorDefinition * USmartObjectDefinition : : GetBehaviorDefinitionByType ( const TArray < USmartObjectBehaviorDefinition * > & BehaviorDefinitions ,
const TSubclassOf < USmartObjectBehaviorDefinition > & DefinitionClass )
{
USmartObjectBehaviorDefinition * const * BehaviorDefinition = BehaviorDefinitions . FindByPredicate ( [ & DefinitionClass ] ( USmartObjectBehaviorDefinition * SlotBehaviorDefinition )
{
return SlotBehaviorDefinition ! = nullptr & & SlotBehaviorDefinition - > GetClass ( ) - > IsChildOf ( * DefinitionClass ) ;
} ) ;
return BehaviorDefinition ! = nullptr ? * BehaviorDefinition : nullptr ;
}