using System; using LE_LevelEditor.Core; using LE_LevelEditor.Logic; using LE_LevelEditor.UI; using S_SnapTools; using UnityEngine; namespace LE_LevelEditor.Commands { public class LE_CmdSnapObjectToObject : LE_CmdBase { private LE_GUI3dObject m_gui3d; private LE_CmdObjectLink m_sourceObj; private LE_CmdObjectLink m_snappedObj = new LE_CmdObjectLink(); private int m_sourceSnapPointIndex; private S_SnapToObjectPrefab m_snapPrefab; public LE_CmdSnapObjectToObject(LE_GUI3dObject p_gui3d, int p_sourceObjUID, int p_sourceSnapPointIndex, S_SnapToObjectPrefab p_snapPrefab) { m_gui3d = p_gui3d; m_sourceObj = new LE_CmdObjectLink(p_sourceObjUID); m_sourceSnapPointIndex = p_sourceSnapPointIndex; m_snapPrefab = p_snapPrefab; } public override long GetStoredBytes() { return 30L; } public override bool Execute() { if (!base.Execute()) { return false; } if (m_snapPrefab == null) { Debug.LogError("LE_CmdSnapObjectToObject: Execute: could not execute, m_snapPrefab is null!"); return false; } if (m_sourceObj.Obj == null) { Debug.LogError("LE_CmdSnapObjectToObject: Execute: could not execute, m_sourceObj is null!"); return false; } if (m_sourceSnapPointIndex >= m_sourceObj.Obj.ObjectSnapPoints.Length) { Debug.LogError("LE_CmdSnapObjectToObject: Execute: could not execute, m_sourceSnapPointIndex('" + m_sourceSnapPointIndex + "') is not a valid[0," + (m_sourceObj.Obj.ObjectSnapPoints.Length - 1) + "] snap point index!"); return false; } S_SnapToObject snapSystemInstance = m_sourceObj.Obj.ObjectSnapPoints[m_sourceSnapPointIndex].SnapSystemInstance; if (snapSystemInstance == null) { Debug.LogError("LE_CmdSnapObjectToObject: Execute: could not execute, m_sourceSnapPointIndex('" + m_sourceSnapPointIndex + "') leads to a null SnapSystemInstance!"); return false; } snapSystemInstance.OnAfterObjectSnapped = (EventHandler)Delegate.Combine(snapSystemInstance.OnAfterObjectSnapped, new EventHandler(OnAfterObjectSnapped)); snapSystemInstance.PlacePrefab(m_snapPrefab); snapSystemInstance.OnAfterObjectSnapped = (EventHandler)Delegate.Remove(snapSystemInstance.OnAfterObjectSnapped, new EventHandler(OnAfterObjectSnapped)); return true; } public override bool Rollback() { if (!base.Rollback()) { return false; } if (m_snappedObj.Obj == null) { Debug.LogError("LE_CmdSnapObjectToObject: Rollback: could not rollback, m_snappedObj is null!"); return false; } if (m_gui3d == null) { Debug.LogError("LE_CmdSnapObjectToObject: Rollback: could not rollback, m_gui3d is null!"); return false; } LE_LogicObjects.DeleteObject(m_gui3d, m_snappedObj.Obj); return true; } private void OnAfterObjectSnapped(object p_sender, S_SnapToObjectEventArgs p_args) { if (m_gui3d == null) { Debug.LogError("LE_CmdSnapObjectToObject: OnAfterObjectSnapped: m_gui3d is null!"); return; } LE_Object component = p_args.NewInstance.GetComponent(); if (component != null) { if (m_snappedObj.UID > 0) { component.UID = m_snappedObj.UID; } m_snappedObj.Obj = component; LE_LogicObjects.OnNewObjectSnapped(m_gui3d, component, p_args); } else { Debug.LogError("LE_CmdSnapObjectToObject: OnAfterObjectSnapped: LE_Object is not attached to the root of the new object! This object will not be saved if it has no LE_Object attached!"); } } } }