Skip to content

Commit

Permalink
Rewrote DragnDrop
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsakharov committed Apr 4, 2024
1 parent 3c60e23 commit a825d3d
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 121 deletions.
11 changes: 5 additions & 6 deletions Prowl.Editor/Drawers/PropertyDrawerAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ protected override bool Draw(string label, ref IAssetRef value, float width)
if (value.IsAvailable && ImGui.IsItemHovered() && ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left))
GlobalSelectHandler.Select(value);

// DragDrop code
string payloadName = value.InstanceType.Name;
if (value.InstanceType.IsAssignableTo(typeof(ScriptableObject)))
payloadName = "ScriptableObject"; // Scriptable objects are a special case
if (DragnDrop.ReceiveAsset(out Guid assetGuid, payloadName)) {
value.AssetID = assetGuid;
// Drag and drop support
if (DragnDrop.Drop(out var instance, value.InstanceType))
{
// SetInstance() will also set the AssetID if the instance is an asset
value.SetInstance(instance);
changed = true;
}

Expand Down
29 changes: 16 additions & 13 deletions Prowl.Editor/Editor/AssetBrowserWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,23 @@ protected override void Draw()
ImGui.EndChild();
AssetsWindow.HandleFileContextMenu(null, CurDirectory, true);

if (DragnDrop.ReceiveReference<GameObject>(out var go))
if (DragnDrop.Drop<GameObject>(out var go))
{
var prefab = new Prefab {
GameObject = Serializer.Serialize(go),
Name = go.Name
};
FileInfo file = new FileInfo(CurDirectory + $"/{prefab.Name}.prefab");
while (File.Exists(file.FullName))
file = new FileInfo(file.FullName.Replace(".prefab", "") + " new.prefab");

StringTagConverter.WriteToFile(Serializer.Serialize(prefab), file);

AssetDatabase.Update();
AssetDatabase.Ping(file);
if(go.AssetID == Guid.Empty)
{
var prefab = new Prefab {
GameObject = Serializer.Serialize(go),
Name = go.Name
};
FileInfo file = new FileInfo(CurDirectory + $"/{prefab.Name}.prefab");
while (File.Exists(file.FullName))
file = new FileInfo(file.FullName.Replace(".prefab", "") + " new.prefab");

StringTagConverter.WriteToFile(Serializer.Serialize(prefab), file);

AssetDatabase.Update();
AssetDatabase.Ping(file);
}
}

if (!AssetsWindow.SelectHandler.SelectedThisFrame && ImGui.IsItemClicked(0))
Expand Down
12 changes: 6 additions & 6 deletions Prowl.Editor/Editor/AssetsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ public static void HandleFileClick(FileInfo entry)

if (isAsset && ImporterAttribute.SupportsExtension(entry.Extension))
{
Type type = ImporterAttribute.GetGeneralType(entry.Extension);
if (type != null)
{
if (DragnDrop.OfferAsset(guid, type.Name))
return;
}
var serialized = AssetDatabase.LoadAsset(guid);
DragnDrop.Drag(serialized.Main, entry);
}
else
{
DragnDrop.Drag(entry);
}

if (ImGui.IsMouseReleased(0))
Expand Down
7 changes: 4 additions & 3 deletions Prowl.Editor/Editor/CustomEditors/MaterialEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,11 @@ public override void OnInspectorGUI()
if (ImGui.IsItemClicked(ImGuiMouseButton.Left) && ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left))
new AssetSelectorWindow(typeof(Texture2D), (guid, fileid) => { assignedGUID = guid; guidAssignedToID = imguiID; assignedFileID = fileid; });

// DragDrop code
if (DragnDrop.ReceiveAsset<Texture2D>(out var droppedTex))
// Drag and drop support
if (DragnDrop.Drop(out var instance, typeof(Texture2D)))
{
tex.AssetID = droppedTex.AssetID;
// SetInstance() will also set the AssetID if the instance is an asset
tex.SetInstance(instance);
changed = true;
}

Expand Down
29 changes: 13 additions & 16 deletions Prowl.Editor/Editor/HierarchyWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Prowl.Runtime.SceneManagement;
using Silk.NET.Input;
using static Prowl.Editor.EditorConfiguration;
using Prowl.Editor.Assets;

namespace Prowl.Editor.EditorWindows;

Expand Down Expand Up @@ -221,29 +222,25 @@ private static ImGuiTreeNodeFlags CalculateFlags(GameObject entity, bool isSelec

private void HandleDragnDrop(GameObject? entity)
{
// GameObject from Hierarchy
if (DragnDrop.ReceiveReference<GameObject>(out var go)) {
if (DragnDrop.Drop<GameObject>(out var original))
{
GameObject go = original;
if (!SceneManager.Has(original)) // If its not already in the scene, Instantiate it
go = (GameObject)EngineObject.Instantiate(original, true);
go.SetParent(entity); // null is root
SelectHandler.SetSelection(new WeakReference(go));
}
// GameObject from Assets
if (DragnDrop.ReceiveAsset<GameObject>(out var original)) {
GameObject clone = (GameObject)EngineObject.Instantiate(original.Res!, true);
clone.AssetID = Guid.Empty; // Remove AssetID so it's not a Prefab - These are just GameObjects like Models
clone.SetParent(entity); // null is root
SelectHandler.SetSelection(new WeakReference(clone));
else if (DragnDrop.Drop<Prefab>(out var prefab))
{
SelectHandler.SetSelection(new WeakReference(prefab.Instantiate()));
}

// Prefab from Assets
if (DragnDrop.ReceiveAsset<Prefab>(out var prefab))
SelectHandler.SetSelection(new WeakReference(prefab.Res.Instantiate()));

// Scene from Assets
if (DragnDrop.ReceiveAsset<Scene>(out var scene))
else if (DragnDrop.Drop<Scene>(out var scene))
{
SceneManager.LoadScene(scene);
}

// Offer GameObject up from Hierarchy for Drag And Drop
if (entity != null) DragnDrop.OfferReference(entity);
if (entity != null) DragnDrop.Drag(entity);
}

private static void DrawTagIcon(GameObject entity)
Expand Down
5 changes: 3 additions & 2 deletions Prowl.Editor/Editor/RenderPipelineWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ protected override void Draw()
// Drag and drop support for the render pipeline asset
var cStart = ImGui.GetCursorPos();
ImGui.Dummy(ImGui.GetContentRegionAvail());
if (DragnDrop.ReceiveAsset<ScriptableObject>(out var asset) && asset.Res is RenderPipeline rp)
CurrentRenderPipeline = rp;

if (DragnDrop.Drop<RenderPipeline>(out var asset))
CurrentRenderPipeline = asset;
ImGui.SetCursorPos(cStart);

if (CurrentRenderPipeline.IsAvailable == false) return;
Expand Down
32 changes: 18 additions & 14 deletions Prowl.Editor/Editor/ViewportWindow.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Hexa.NET.ImGui;
using Hexa.NET.ImGuizmo;
using Jitter2.LinearMath;
using Prowl.Editor.Assets;
using Prowl.Editor.Editor.Preferences;
using Prowl.Editor.ImGUI.Widgets;
using Prowl.Icons;
Expand Down Expand Up @@ -297,28 +298,31 @@ private void DrawGizmos(IEnumerable<GameObject> selectedGOs, System.Numerics.Mat

private void HandleDragnDrop()
{
// GameObject from Assets
if (DragnDrop.ReceiveAsset<GameObject>(out var original)) {
GameObject clone = (GameObject)EngineObject.Instantiate(original.Res!, true);
clone.AssetID = Guid.Empty; // Remove AssetID so it's not a Prefab - These are just GameObjects like Models
var t = clone;
if (t != null) {
t.transform.position = Cam.GameObject.transform.position + Cam.GameObject.transform.forward * 10;
if (DragnDrop.Drop<GameObject>(out var original))
{
if (original.AssetID == Guid.Empty) return;

GameObject go = (GameObject)EngineObject.Instantiate(original, true);
if (go != null)
{
go.transform.position = Cam.GameObject.transform.position + Cam.GameObject.transform.forward * 10;
}
HierarchyWindow.SelectHandler.SetSelection(new WeakReference(clone));
HierarchyWindow.SelectHandler.SetSelection(new WeakReference(go));
}
// Prefab from Assets
if (DragnDrop.ReceiveAsset<Prefab>(out var prefab)) {
var go = prefab.Res.Instantiate();
else if (DragnDrop.Drop<Prefab>(out var prefab))
{
var go = prefab.Instantiate();
var t = go;
if (t != null) {
if (t != null)
{
t.transform.position = Cam.GameObject.transform.position + Cam.GameObject.transform.forward * 10;
}
HierarchyWindow.SelectHandler.SetSelection(new WeakReference(go));
}
// Scene from Assets
if (DragnDrop.ReceiveAsset<Scene>(out var scene))
else if (DragnDrop.Drop<Scene>(out var scene))
{
SceneManager.LoadScene(scene);
}
}

protected override void Update()
Expand Down
Loading

0 comments on commit a825d3d

Please sign in to comment.