Files
sbox-public/game/addons/tools/Code/Scene/SceneView/DropObjects/PrefabDropObject.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

101 lines
2.3 KiB
C#

using System.Threading;
namespace Editor;
[DropObject( "prefab", "prefab", "prefab_c" )]
partial class PrefabDropObject : BaseDropObject
{
private IDisposable undoScope;
protected override async Task Initialize( string dragData, CancellationToken token )
{
Asset asset = await InstallAsset( dragData, token );
if ( asset is null )
return;
if ( token.IsCancellationRequested )
return;
var pf = asset.LoadResource<PrefabFile>();
if ( pf is null ) return;
if ( SceneEditorSession.Active.Scene is PrefabScene prefabScene )
{
var currentPf = prefabScene.ToPrefabFile();
if ( currentPf.ResourceId == pf.ResourceId )
{
Log.Warning( "Cannot place the same prefab in itself." );
return;
}
}
// We should REALLY be scoped over whatever scene we're dragging over
using ( SceneEditorSession.Scope() )
{
undoScope = SceneEditorSession.Active.UndoScope( "Drop Prefab" ).WithGameObjectCreations().Push();
var go = SceneUtility.GetPrefabScene( pf );
GameObject = go.Clone();
GameObject.Flags = GameObjectFlags.NotSaved | GameObjectFlags.Hidden;
GameObject.Tags.Add( "isdragdrop" );
Bounds = GameObject.GetLocalBounds();
if ( Bounds.Size.Length < 4 )
Bounds = BBox.FromPositionAndSize( 0, 4 );
PivotPosition = Bounds.ClosestPoint( Vector3.Down * 10000 );
Rotation = GameObject.WorldRotation;
Scale = GameObject.WorldScale;
}
}
public override void OnUpdate()
{
if ( GameObject.IsValid() )
{
GameObject.WorldTransform = traceTransform;
}
using var scope = Gizmo.Scope( "DropObject", traceTransform );
Gizmo.Draw.Color = Color.White.WithAlpha( 0.3f );
Gizmo.Draw.LineBBox( Bounds );
Gizmo.Draw.Color = Color.White;
if ( !string.IsNullOrWhiteSpace( PackageStatus ) )
{
Gizmo.Draw.Text( PackageStatus, new Transform( Bounds.Center ), "Inter", 12 );
}
}
public override async Task OnDrop()
{
await WaitForLoad();
if ( !GameObject.IsValid() )
return;
GameObject.WorldTransform = traceTransform;
GameObject.Flags = GameObjectFlags.None;
GameObject.Tags.Remove( "isdragdrop" );
EditorScene.Selection.Clear();
EditorScene.Selection.Add( GameObject );
undoScope.Dispose();
undoScope = null;
GameObject = null;
}
public override void OnDestroy()
{
GameObject?.Destroy();
GameObject = null;
}
}