2019-12-26 15:33:43 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2015-02-09 11:42:01 -05:00
# include "DragDropHandler.h"
Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340)
#lockdown Nick.Penwarden
#rb none
==========================
MAJOR FEATURES + CHANGES
==========================
Change 3209340 on 2016/11/23 by Ben.Marsh
Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h.
Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms.
* Every header now includes everything it needs to compile.
* There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first.
* There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h.
* Every .cpp file includes its matching .h file first.
* This helps validate that each header is including everything it needs to compile.
* No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more.
* You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there.
* There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible.
* No engine code explicitly includes a precompiled header any more.
* We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies.
* PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files.
Tool used to generate this transform is at Engine\Source\Programs\IncludeTool.
[CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00
# include "Layout/WidgetPath.h"
# include "Framework/Application/MenuStack.h"
# include "Framework/Application/SlateApplication.h"
# include "Textures/SlateIcon.h"
# include "Framework/Commands/UIAction.h"
# include "Framework/MultiBox/MultiBoxBuilder.h"
2020-06-23 18:40:00 -04:00
# include "ToolMenus.h"
2022-05-09 13:12:28 -04:00
# include "Styling/AppStyle.h"
2020-01-21 14:04:13 -05:00
# include "AssetToolsModule.h"
2021-10-12 21:21:22 -04:00
# include "Misc/NamePermissionList.h"
2015-02-09 11:42:01 -05:00
# include "DragAndDrop/AssetDragDropOp.h"
# include "ContentBrowserUtils.h"
2020-06-23 18:40:00 -04:00
# include "ContentBrowserItem.h"
# include "ContentBrowserDataSource.h"
# include "ContentBrowserDataDragDropOp.h"
# include "ContentBrowserDataMenuContexts.h"
2015-02-09 11:42:01 -05:00
# define LOCTEXT_NAMESPACE "ContentBrowser"
2020-06-23 18:40:00 -04:00
namespace DragDropHandler
2015-02-09 11:42:01 -05:00
{
2015-06-19 07:33:02 -04:00
2020-06-23 18:40:00 -04:00
TSharedPtr < FDragDropOperation > CreateDragOperation ( TArrayView < const FContentBrowserItem > InItems )
{
if ( InItems . Num ( ) = = 0 )
{
return nullptr ;
}
// Batch these by their data sources
TMap < UContentBrowserDataSource * , TArray < FContentBrowserItemData > > SourcesAndItems ;
for ( const FContentBrowserItem & Item : InItems )
{
FContentBrowserItem : : FItemDataArrayView ItemDataArray = Item . GetInternalItems ( ) ;
for ( const FContentBrowserItemData & ItemData : ItemDataArray )
{
if ( UContentBrowserDataSource * ItemDataSource = ItemData . GetOwnerDataSource ( ) )
{
TArray < FContentBrowserItemData > & ItemsForSource = SourcesAndItems . FindOrAdd ( ItemDataSource ) ;
ItemsForSource . Add ( ItemData ) ;
}
}
}
// Custom handling via a data source?
for ( const auto & SourceAndItemsPair : SourcesAndItems )
{
if ( TSharedPtr < FDragDropOperation > CustomDragOp = SourceAndItemsPair . Key - > CreateCustomDragOperation ( SourceAndItemsPair . Value ) )
{
return CustomDragOp ;
}
}
// Generic handling
return FContentBrowserDataDragDropOp : : New ( InItems ) ;
}
template < typename FuncType >
bool HandleDragEventOverride ( const FContentBrowserItem & InItem , const FDragDropEvent & InDragDropEvent , FuncType Func )
{
FContentBrowserItem : : FItemDataArrayView ItemDataArray = InItem . GetInternalItems ( ) ;
for ( const FContentBrowserItemData & ItemData : ItemDataArray )
{
if ( UContentBrowserDataSource * ItemDataSource = ItemData . GetOwnerDataSource ( ) )
{
if ( Invoke ( Func , ItemDataSource , ItemData , InDragDropEvent ) )
{
return true ;
}
}
}
return false ;
}
bool ValidateGenericDragEvent ( const FContentBrowserItem & InItem , const FDragDropEvent & InDragDropEvent )
{
if ( ! InItem . IsFolder ( ) )
2015-02-09 11:42:01 -05:00
{
return false ;
}
2020-06-23 18:40:00 -04:00
if ( TSharedPtr < FContentBrowserDataDragDropOp > ContentDragDropOp = InDragDropEvent . GetOperationAs < FContentBrowserDataDragDropOp > ( ) )
2015-02-09 11:42:01 -05:00
{
2020-06-23 18:40:00 -04:00
if ( EnumHasAnyFlags ( InItem . GetItemCategory ( ) , EContentBrowserItemFlags : : Category_Collection ) )
2015-02-09 11:42:01 -05:00
{
2022-05-09 13:12:28 -04:00
ContentDragDropOp - > SetToolTip ( LOCTEXT ( " OnDragFoldersOverFolder_CannotDropOnCollectionFolder " , " Cannot drop onto a collection folder. Drop onto the collection in the collection view instead. " ) , FAppStyle : : GetBrush ( TEXT ( " Graph.ConnectorFeedback.Error " ) ) ) ;
2015-02-09 11:42:01 -05:00
}
2020-06-23 18:40:00 -04:00
else if ( ContentDragDropOp - > GetDraggedItems ( ) . Num ( ) = = 1 & & ContentDragDropOp - > GetDraggedItems ( ) [ 0 ] . GetVirtualPath ( ) = = InItem . GetVirtualPath ( ) )
2015-02-09 11:42:01 -05:00
{
2022-05-09 13:12:28 -04:00
ContentDragDropOp - > SetToolTip ( LOCTEXT ( " OnDragFoldersOverFolder_CannotSelfDrop " , " Cannot move or copy a folder onto itself " ) , FAppStyle : : GetBrush ( TEXT ( " Graph.ConnectorFeedback.Error " ) ) ) ;
2015-02-09 11:42:01 -05:00
}
else
{
2020-06-23 18:40:00 -04:00
int32 NumDraggedItems = ContentDragDropOp - > GetDraggedItems ( ) . Num ( ) ;
int32 NumCanMoveOrCopy = 0 ;
for ( const FContentBrowserItem & DraggedItem : ContentDragDropOp - > GetDraggedItems ( ) )
{
const bool bCanMoveOrCopy = DraggedItem . CanMove ( InItem . GetVirtualPath ( ) ) | | DraggedItem . CanCopy ( InItem . GetVirtualPath ( ) ) ;
if ( bCanMoveOrCopy )
{
+ + NumCanMoveOrCopy ;
}
}
if ( NumCanMoveOrCopy = = 0 )
{
2022-05-09 13:12:28 -04:00
ContentDragDropOp - > SetToolTip ( LOCTEXT ( " OnDragFoldersOverFolder_CannotDrop " , " Cannot move or copy to this folder " ) , FAppStyle : : GetBrush ( TEXT ( " Graph.ConnectorFeedback.Error " ) ) ) ;
2020-06-23 18:40:00 -04:00
}
else
{
const FText FirstItemText = ContentDragDropOp - > GetDraggedItems ( ) [ 0 ] . GetDisplayName ( ) ;
const FText MoveOrCopyText = ( NumCanMoveOrCopy > 1 )
? FText : : Format ( LOCTEXT ( " OnDragAssetsOverFolder_MultipleItems " , " Move or copy '{0}' and {1} {1}|plural(one=other,other=others) " ) , FirstItemText , NumDraggedItems - 1 )
: FText : : Format ( LOCTEXT ( " OnDragAssetsOverFolder_SingularItems " , " Move or copy '{0}' " ) , FirstItemText ) ;
if ( NumCanMoveOrCopy < NumDraggedItems )
{
2022-05-09 13:12:28 -04:00
ContentDragDropOp - > SetToolTip ( FText : : Format ( LOCTEXT ( " OnDragAssetsOverFolder_SomeInvalidItems " , " {0} \n \n {1} {1}|plural(one=item,other=items) will be ignored as they cannot be moved or copied " ) , MoveOrCopyText , NumDraggedItems - NumCanMoveOrCopy ) , FAppStyle : : GetBrush ( TEXT ( " Graph.ConnectorFeedback.OKWarn " ) ) ) ;
2020-06-23 18:40:00 -04:00
}
else
{
2022-05-09 13:12:28 -04:00
ContentDragDropOp - > SetToolTip ( MoveOrCopyText , FAppStyle : : GetBrush ( TEXT ( " Graph.ConnectorFeedback.OK " ) ) ) ;
2020-06-23 18:40:00 -04:00
}
}
}
return true ;
}
return false ;
}
bool HandleDragEnterItem ( const FContentBrowserItem & InItem , const FDragDropEvent & InDragDropEvent )
{
// Custom handling via a data source?
if ( HandleDragEventOverride ( InItem , InDragDropEvent , & UContentBrowserDataSource : : HandleDragEnterItem ) )
{
return true ;
}
// Generic handling
return ValidateGenericDragEvent ( InItem , InDragDropEvent ) ;
}
bool HandleDragOverItem ( const FContentBrowserItem & InItem , const FDragDropEvent & InDragDropEvent )
{
// Custom handling via a data source?
if ( HandleDragEventOverride ( InItem , InDragDropEvent , & UContentBrowserDataSource : : HandleDragOverItem ) )
{
return true ;
}
// Generic handling
return ValidateGenericDragEvent ( InItem , InDragDropEvent ) ;
}
bool HandleDragLeaveItem ( const FContentBrowserItem & InItem , const FDragDropEvent & InDragDropEvent )
{
// Custom handling via a data source?
if ( HandleDragEventOverride ( InItem , InDragDropEvent , & UContentBrowserDataSource : : HandleDragLeaveItem ) )
{
return true ;
}
if ( ! InItem . IsFolder ( ) )
{
return false ;
}
// Generic handling
if ( TSharedPtr < FContentBrowserDataDragDropOp > ContentDragDropOp = InDragDropEvent . GetOperationAs < FContentBrowserDataDragDropOp > ( ) )
{
ContentDragDropOp - > ResetToDefaultToolTip ( ) ;
return true ;
}
return false ;
}
template < typename CanMoveOrCopyFuncType , typename BulkMoveOrCopyFuncType >
void HandleDragDropMoveOrCopy ( const FContentBrowserItem & InDropTargetItem , const TArray < FContentBrowserItem > & InDraggedItems , const TSharedPtr < SWidget > & InParentWidget , const FText InMoveOrCopyMsg , CanMoveOrCopyFuncType InCanMoveOrCopyFunc , BulkMoveOrCopyFuncType InBulkMoveOrCopyFunc )
{
const FName InDropTargetPath = InDropTargetItem . GetVirtualPath ( ) ;
// Batch these by their data sources
TMap < UContentBrowserDataSource * , TArray < FContentBrowserItemData > > SourcesAndItems ;
for ( const FContentBrowserItem & DraggedItem : InDraggedItems )
{
FContentBrowserItem : : FItemDataArrayView ItemDataArray = DraggedItem . GetInternalItems ( ) ;
for ( const FContentBrowserItemData & ItemData : ItemDataArray )
{
if ( UContentBrowserDataSource * ItemDataSource = ItemData . GetOwnerDataSource ( ) )
{
FText MoveOrCopyErrorMsg ;
if ( Invoke ( InCanMoveOrCopyFunc , * ItemDataSource , ItemData , InDropTargetPath , & MoveOrCopyErrorMsg ) )
{
TArray < FContentBrowserItemData > & ItemsForSource = SourcesAndItems . FindOrAdd ( ItemDataSource ) ;
ItemsForSource . Add ( ItemData ) ;
}
else
{
AssetViewUtils : : ShowErrorNotifcation ( MoveOrCopyErrorMsg ) ;
}
}
2015-02-09 11:42:01 -05:00
}
}
2020-06-23 18:40:00 -04:00
// Execute the operation now
int32 NumMovedOrCopiedItems = 0 ;
for ( const auto & SourceAndItemsPair : SourcesAndItems )
2015-02-09 11:42:01 -05:00
{
2020-06-23 18:40:00 -04:00
if ( Invoke ( InBulkMoveOrCopyFunc , * SourceAndItemsPair . Key , SourceAndItemsPair . Value , InDropTargetPath ) )
{
// This assumes that everything passed is moved or copied, which may not be true, but we've validated as best we can when building this array
NumMovedOrCopiedItems + = SourceAndItemsPair . Value . Num ( ) ;
}
2015-02-09 11:42:01 -05:00
}
2020-06-23 18:40:00 -04:00
// Show a message if the move or copy was successful
if ( NumMovedOrCopiedItems > 0 & & InParentWidget )
2015-02-09 11:42:01 -05:00
{
2020-06-23 18:40:00 -04:00
const FText Message = FText : : Format ( InMoveOrCopyMsg , NumMovedOrCopiedItems , FText : : FromName ( InDropTargetPath ) ) ;
const FVector2D & CursorPos = FSlateApplication : : Get ( ) . GetCursorPos ( ) ;
FSlateRect MessageAnchor ( CursorPos . X , CursorPos . Y , CursorPos . X , CursorPos . Y ) ;
ContentBrowserUtils : : DisplayMessage ( Message , MessageAnchor , InParentWidget . ToSharedRef ( ) ) ;
2015-02-09 11:42:01 -05:00
}
}
2020-06-23 18:40:00 -04:00
void HandleDragDropMove ( const FContentBrowserItem & InDropTargetItem , const TArray < FContentBrowserItem > & InDraggedItems , const TSharedPtr < SWidget > & InParentWidget )
2015-02-09 11:42:01 -05:00
{
2020-06-23 18:40:00 -04:00
return HandleDragDropMoveOrCopy ( InDropTargetItem , InDraggedItems , InParentWidget , LOCTEXT ( " ItemsDroppedMove " , " {0} {0}|plural(one=item,other=items) moved to ' { 1 } ' " ), &UContentBrowserDataSource::CanMoveItem, &UContentBrowserDataSource::BulkMoveItems);
2015-02-09 11:42:01 -05:00
}
2020-06-23 18:40:00 -04:00
void HandleDragDropCopy ( const FContentBrowserItem & InDropTargetItem , const TArray < FContentBrowserItem > & InDraggedItems , const TSharedPtr < SWidget > & InParentWidget )
{
return HandleDragDropMoveOrCopy ( InDropTargetItem , InDraggedItems , InParentWidget , LOCTEXT ( " ItemsDroppedCopy " , " {0} {0}|plural(one=item,other=items) copied to ' { 1 } ' " ), &UContentBrowserDataSource::CanCopyItem, &UContentBrowserDataSource::BulkCopyItems);
}
bool HandleDragDropOnItem ( const FContentBrowserItem & InItem , const FDragDropEvent & InDragDropEvent , const TSharedRef < SWidget > & InParentWidget )
{
// Custom handling via a data source?
if ( HandleDragEventOverride ( InItem , InDragDropEvent , & UContentBrowserDataSource : : HandleDragDropOnItem ) )
{
return true ;
}
if ( ! InItem . IsFolder ( ) )
{
return false ;
}
// Generic handling
if ( TSharedPtr < FContentBrowserDataDragDropOp > ContentDragDropOp = InDragDropEvent . GetOperationAs < FContentBrowserDataDragDropOp > ( ) )
{
static const FName MenuName = " ContentBrowser.DragDropContextMenu " ;
UToolMenus * ToolMenus = UToolMenus : : Get ( ) ;
if ( ! ToolMenus - > IsMenuRegistered ( MenuName ) )
{
UToolMenu * Menu = ToolMenus - > RegisterMenu ( MenuName ) ;
FToolMenuSection & Section = Menu - > AddSection ( " MoveCopy " , LOCTEXT ( " MoveCopyMenuHeading_Generic " , " Move/Copy... " ) ) ;
Section . AddDynamicEntry ( " DragDropMoveCopy_Dynamic " , FNewToolMenuSectionDelegate : : CreateLambda ( [ ] ( FToolMenuSection & InSection )
{
const UContentBrowserDataMenuContext_DragDropMenu * ContextObject = InSection . FindContext < UContentBrowserDataMenuContext_DragDropMenu > ( ) ;
checkf ( ContextObject , TEXT ( " Required context UContentBrowserDataMenuContext_DragDropMenu was missing! " ) ) ;
if ( ContextObject - > bCanMove )
{
InSection . AddMenuEntry (
" DragDropMove " ,
LOCTEXT ( " DragDropMove " , " Move Here " ) ,
LOCTEXT ( " DragDropMoveTooltip " , " Move the dragged items to this folder, preserving the structure of any copied folders. " ) ,
FSlateIcon ( ) ,
FUIAction ( FExecuteAction : : CreateLambda ( [ DropTargetItem = ContextObject - > DropTargetItem , DraggedItems = ContextObject - > DraggedItems , ParentWidget = ContextObject - > ParentWidget ] ( ) { HandleDragDropMove ( DropTargetItem , DraggedItems , ParentWidget . Pin ( ) ) ; } ) )
) ;
}
if ( ContextObject - > bCanCopy )
{
InSection . AddMenuEntry (
" DragDropCopy " ,
LOCTEXT ( " DragDropCopy " , " Copy Here " ) ,
LOCTEXT ( " DragDropCopyTooltip " , " Copy the dragged items to this folder, preserving the structure of any copied folders. " ) ,
FSlateIcon ( ) ,
FUIAction ( FExecuteAction : : CreateLambda ( [ DropTargetItem = ContextObject - > DropTargetItem , DraggedItems = ContextObject - > DraggedItems , ParentWidget = ContextObject - > ParentWidget ] ( ) { HandleDragDropCopy ( DropTargetItem , DraggedItems , ParentWidget . Pin ( ) ) ; } ) )
) ;
}
} ) ) ;
}
if ( UToolMenu * Menu = ToolMenus - > ExtendMenu ( MenuName ) )
{
// Update the section display name for the current drop target
Menu - > AddSection ( " MoveCopy " , FText : : Format ( LOCTEXT ( " MoveCopyMenuHeading_Fmt " , " Move/Copy to {0} " ) , InItem . GetDisplayName ( ) ) ) ;
}
UContentBrowserDataMenuContext_DragDropMenu * ContextObject = NewObject < UContentBrowserDataMenuContext_DragDropMenu > ( ) ;
ContextObject - > DropTargetItem = InItem ;
ContextObject - > DraggedItems = ContentDragDropOp - > GetDraggedItems ( ) ;
ContextObject - > bCanMove = false ;
ContextObject - > bCanCopy = false ;
for ( const FContentBrowserItem & DraggedItem : ContextObject - > DraggedItems )
{
ContextObject - > bCanMove | = DraggedItem . CanMove ( ContextObject - > DropTargetItem . GetVirtualPath ( ) ) ;
ContextObject - > bCanCopy | = DraggedItem . CanCopy ( ContextObject - > DropTargetItem . GetVirtualPath ( ) ) ;
if ( ContextObject - > bCanMove & & ContextObject - > bCanCopy )
{
break ;
}
}
ContextObject - > ParentWidget = InParentWidget ;
FToolMenuContext MenuContext ( ContextObject ) ;
TSharedRef < SWidget > MenuWidget = ToolMenus - > GenerateWidget ( MenuName , MenuContext ) ;
FSlateApplication : : Get ( ) . PushMenu (
InParentWidget ,
FWidgetPath ( ) ,
MenuWidget ,
FSlateApplication : : Get ( ) . GetCursorPos ( ) ,
FPopupTransitionEffect ( FPopupTransitionEffect : : ContextMenu )
) ;
return true ;
}
return false ;
}
} // namespace DragDropHandler
2015-02-09 11:42:01 -05:00
# undef LOCTEXT_NAMESPACE