2019-12-26 15:33:43 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2019-01-08 11:38:48 -05:00
# include "CoreMinimal.h"
# include "InputCoreTypes.h"
# include "Materials/MaterialInterface.h"
# include "AI/NavigationSystemBase.h"
# include "Materials/MaterialInstanceDynamic.h"
2020-09-07 20:36:09 -04:00
# include "UnrealWidgetFwd.h"
2019-01-08 11:38:48 -05:00
# include "EditorModeManager.h"
# include "EditorViewportClient.h"
# include "LandscapeToolInterface.h"
# include "LandscapeProxy.h"
# include "LandscapeEdMode.h"
# include "Containers/ArrayView.h"
# include "LandscapeEditorObject.h"
# include "ScopedTransaction.h"
# include "LandscapeEdit.h"
# include "LandscapeDataAccess.h"
# include "LandscapeRender.h"
# include "LandscapeHeightfieldCollisionComponent.h"
# include "LandscapeEdModeTools.h"
2019-06-14 16:11:59 -04:00
# include "LandscapeBlueprintBrushBase.h"
2019-01-08 11:38:48 -05:00
# include "LandscapeInfo.h"
# include "Landscape.h"
2020-10-29 13:38:15 -04:00
# include "ActorFactories/ActorFactory.h"
2019-01-08 11:38:48 -05:00
//#include "LandscapeDataAccess.h"
# define LOCTEXT_NAMESPACE "Landscape"
template < class ToolTarget >
2019-06-05 11:09:58 -04:00
class FLandscapeToolBlueprintBrush : public FLandscapeTool
2019-01-08 11:38:48 -05:00
{
protected :
FEdModeLandscape * EdMode ;
public :
2019-06-05 11:09:58 -04:00
FLandscapeToolBlueprintBrush ( FEdModeLandscape * InEdMode )
2019-01-08 11:38:48 -05:00
: EdMode ( InEdMode )
{
}
virtual bool UsesTransformWidget ( ) const { return true ; }
virtual bool OverrideWidgetLocation ( ) const { return false ; }
virtual bool OverrideWidgetRotation ( ) const { return false ; }
virtual void AddReferencedObjects ( FReferenceCollector & Collector ) override
{
}
2019-06-05 11:09:58 -04:00
virtual const TCHAR * GetToolName ( ) override { return TEXT ( " BlueprintBrush " ) ; }
2019-01-08 11:38:48 -05:00
virtual FText GetDisplayName ( ) override { return FText ( ) ; } ;
2020-01-27 20:11:15 -05:00
virtual FText GetDisplayMessage ( ) override { return FText ( ) ; } ;
2019-01-08 11:38:48 -05:00
virtual void SetEditRenderType ( ) override { GLandscapeEditRenderMode = ELandscapeEditRenderMode : : None | ( GLandscapeEditRenderMode & ELandscapeEditRenderMode : : BitMaskForMask ) ; }
virtual bool SupportsMask ( ) override { return false ; }
virtual ELandscapeToolTargetTypeMask : : Type GetSupportedTargetTypes ( ) override
{
return ELandscapeToolTargetTypeMask : : FromType ( ToolTarget : : TargetType ) ;
}
virtual void EnterTool ( ) override
{
}
virtual void ExitTool ( ) override
{
2019-06-05 11:09:58 -04:00
GEditor - > SelectNone ( true , true ) ;
2019-01-08 11:38:48 -05:00
}
virtual void Tick ( FEditorViewportClient * ViewportClient , float DeltaTime )
{
}
virtual bool BeginTool ( FEditorViewportClient * ViewportClient , const FLandscapeToolTarget & Target , const FVector & InHitLocation ) override
{
2020-10-29 13:38:15 -04:00
UClass * BrushClassPtr = EdMode - > UISettings - > BlueprintBrush . Get ( ) ;
if ( BrushClassPtr = = nullptr )
2019-01-08 11:38:48 -05:00
{
return false ;
}
2020-10-29 13:38:15 -04:00
ALandscapeBlueprintBrushBase * DefaultObject = Cast < ALandscapeBlueprintBrushBase > ( BrushClassPtr - > GetDefaultObject ( false ) ) ;
2019-01-08 11:38:48 -05:00
if ( DefaultObject = = nullptr )
{
return false ;
}
// Only allow placing brushes that would affect our target type
if ( ( DefaultObject - > IsAffectingHeightmap ( ) & & Target . TargetType = = ELandscapeToolTargetType : : Heightmap ) | | ( DefaultObject - > IsAffectingWeightmap ( ) & & Target . TargetType = = ELandscapeToolTargetType : : Weightmap ) )
{
ULandscapeInfo * Info = EdMode - > CurrentToolTarget . LandscapeInfo . Get ( ) ;
check ( Info ) ;
FVector SpawnLocation = Info - > GetLandscapeProxy ( ) - > LandscapeActorToWorld ( ) . TransformPosition ( InHitLocation ) ;
2020-10-29 13:38:15 -04:00
FString BrushActorString = FString : : Format ( TEXT ( " {0}_{1} " ) , { Info - > LandscapeActor . Get ( ) - > GetActorLabel ( ) , BrushClassPtr - > GetName ( ) } ) ;
FName BrushActorName = MakeUniqueObjectName ( Info - > LandscapeActor . Get ( ) - > GetLevel ( ) , BrushClassPtr , FName ( BrushActorString ) ) ;
2019-01-08 11:38:48 -05:00
FActorSpawnParameters SpawnInfo ;
SpawnInfo . SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod : : AlwaysSpawn ;
SpawnInfo . bNoFail = true ;
2020-08-11 01:36:57 -04:00
SpawnInfo . OverrideLevel = Info - > LandscapeActor . Get ( ) - > GetLevel ( ) ; // always spawn in the same level as the one containing the ALandscape
2020-10-29 13:38:15 -04:00
SpawnInfo . Name = BrushActorName ;
2019-01-08 11:38:48 -05:00
2019-06-14 16:11:59 -04:00
FScopedTransaction Transaction ( LOCTEXT ( " LandscapeEdModeBlueprintToolSpawn " , " Create landscape brush " ) ) ;
2020-10-29 13:38:15 -04:00
// Use the class factory if there's one :
UActorFactory * BrushActorFactory = GEditor - > FindActorFactoryForActorClass ( BrushClassPtr ) ;
UWorld * ActorWorld = ViewportClient - > GetWorld ( ) ;
ALandscapeBlueprintBrushBase * Brush = ( BrushActorFactory ! = nullptr )
2021-05-06 08:19:17 -04:00
? CastChecked < ALandscapeBlueprintBrushBase > ( BrushActorFactory - > CreateActor ( ActorWorld , SpawnInfo . OverrideLevel , FTransform ( SpawnLocation ) , SpawnInfo ) )
2020-10-29 13:38:15 -04:00
: ActorWorld - > SpawnActor < ALandscapeBlueprintBrushBase > ( BrushClassPtr , SpawnLocation , FRotator ( 0.0f ) , SpawnInfo ) ;
2019-06-14 16:11:59 -04:00
EdMode - > UISettings - > BlueprintBrush = nullptr ;
2019-01-08 11:38:48 -05:00
2020-10-29 13:38:15 -04:00
Brush - > SetActorLabel ( BrushActorString ) ;
2019-01-08 11:38:48 -05:00
GEditor - > SelectNone ( true , true ) ;
GEditor - > SelectActor ( Brush , true , true ) ;
EdMode - > RefreshDetailPanel ( ) ;
}
return true ;
}
virtual void EndTool ( FEditorViewportClient * ViewportClient ) override
{
}
virtual bool MouseMove ( FEditorViewportClient * ViewportClient , FViewport * Viewport , int32 x , int32 y ) override
{
return false ;
}
virtual bool InputKey ( FEditorViewportClient * InViewportClient , FViewport * InViewport , FKey InKey , EInputEvent InEvent ) override
{
if ( InKey = = EKeys : : Enter & & InEvent = = IE_Pressed )
{
}
return false ;
}
virtual bool InputDelta ( FEditorViewportClient * InViewportClient , FViewport * InViewport , FVector & InDrag , FRotator & InRot , FVector & InScale ) override
{
return false ;
}
virtual void Render ( const FSceneView * View , FViewport * Viewport , FPrimitiveDrawInterface * PDI ) override
{
// The editor can try to render the tool before the UpdateLandscapeEditorData command runs and the landscape editor realizes that the landscape has been hidden/deleted
const ULandscapeInfo * const LandscapeInfo = EdMode - > CurrentToolTarget . LandscapeInfo . Get ( ) ;
const ALandscapeProxy * const LandscapeProxy = LandscapeInfo - > GetLandscapeProxy ( ) ;
if ( LandscapeProxy )
{
const FTransform LandscapeToWorld = LandscapeProxy - > LandscapeActorToWorld ( ) ;
int32 MinX , MinY , MaxX , MaxY ;
if ( LandscapeInfo - > GetLandscapeExtent ( MinX , MinY , MaxX , MaxY ) )
{
// TODO if required
}
}
}
protected :
/* float GetLocalZAtPoint(const ULandscapeInfo* LandscapeInfo, int32 x, int32 y) const
{
// try to find Z location
TSet < ULandscapeComponent * > Components ;
LandscapeInfo - > GetComponentsInRegion ( x , y , x , y , Components ) ;
for ( ULandscapeComponent * Component : Components )
{
FLandscapeComponentDataInterface DataInterface ( Component ) ;
return LandscapeDataAccess : : GetLocalHeight ( DataInterface . GetHeight ( x - Component - > SectionBaseX , y - Component - > SectionBaseY ) ) ;
}
return 0.0f ;
}
*/
public :
} ;
/*
void FEdModeLandscape : : ApplyMirrorTool ( )
{
if ( CurrentTool - > GetToolName ( ) = = FName ( " Mirror " ) )
{
FLandscapeToolMirror * MirrorTool = ( FLandscapeToolMirror * ) CurrentTool ;
MirrorTool - > ApplyMirror ( ) ;
GEditor - > RedrawLevelEditingViewports ( ) ;
}
}
void FEdModeLandscape : : CenterMirrorTool ( )
{
if ( CurrentTool - > GetToolName ( ) = = FName ( " Mirror " ) )
{
FLandscapeToolMirror * MirrorTool = ( FLandscapeToolMirror * ) CurrentTool ;
MirrorTool - > CenterMirrorPoint ( ) ;
GEditor - > RedrawLevelEditingViewports ( ) ;
}
}
*/
//
// Toolset initialization
//
2019-06-05 11:09:58 -04:00
void FEdModeLandscape : : InitializeTool_BlueprintBrush ( )
2019-01-08 11:38:48 -05:00
{
2019-06-05 11:09:58 -04:00
auto Sculpt_Tool_BlueprintBrush = MakeUnique < FLandscapeToolBlueprintBrush < FHeightmapToolTarget > > ( this ) ;
Sculpt_Tool_BlueprintBrush - > ValidBrushes . Add ( " BrushSet_Dummy " ) ;
LandscapeTools . Add ( MoveTemp ( Sculpt_Tool_BlueprintBrush ) ) ;
2019-01-08 11:38:48 -05:00
2019-06-05 11:09:58 -04:00
auto Paint_Tool_BlueprintBrush = MakeUnique < FLandscapeToolBlueprintBrush < FWeightmapToolTarget > > ( this ) ;
Paint_Tool_BlueprintBrush - > ValidBrushes . Add ( " BrushSet_Dummy " ) ;
LandscapeTools . Add ( MoveTemp ( Paint_Tool_BlueprintBrush ) ) ;
2019-01-08 11:38:48 -05:00
}
# undef LOCTEXT_NAMESPACE