// UltEvents // Copyright 2020 Kybernetik //
#if UNITY_EDITOR
using System;
using UnityEditor;
using Object = UnityEngine.Object;
namespace UltEvents.Editor
{
/// [Editor-Only]
/// Manages the copying and pasting of events and persistent calls.
///
public static class Clipboard
{
/************************************************************************************************************************/
private static UltEventBase _Event;
/// Indicates whether an event has been copied.
public static bool HasEvent { get { return _Event != null; } }
/************************************************************************************************************************/
/// Stores the details of the specified event.
public static void CopyEvent(UltEventBase e)
{
var eventType = e.GetType();
if (_Event == null || _Event.GetType() != eventType)
_Event = (UltEventBase)Activator.CreateInstance(eventType);
_Event.CopyFrom(e);
}
/// Stores the details of the event contained in the specified property.
public static void CopyEvent(Serialization.PropertyAccessor accessor, Object target)
{
var e = (UltEventBase)accessor.GetValue(target);
CopyEvent(e);
}
/// Stores the details of the event contained in the specified property.
public static void CopyEvent(SerializedProperty property)
{
var accessor = property.GetAccessor();
if (accessor == null)
return;
CopyEvent(accessor, property.serializedObject.targetObject);
}
/************************************************************************************************************************/
/// Overwrites the specified event with the previously copied details.
public static void Paste(UltEventBase e)
{
e.CopyFrom(_Event);
}
/************************************************************************************************************************/
private static PersistentCall _Call;
/// Indicates whether a persistent call has been copied.
public static bool HasCall { get { return _Call != null; } }
/************************************************************************************************************************/
/// Stores the details of the specified call.
public static void CopyCall(PersistentCall call)
{
if (_Call == null)
_Call = new PersistentCall();
_Call.CopyFrom(call);
}
/// Stores the details of the call contained in the specified property.
public static void CopyCall(Serialization.PropertyAccessor accessor, Object target)
{
var call = (PersistentCall)accessor.GetValue(target);
CopyCall(call);
}
/// Stores the details of the call contained in the specified property.
public static void CopyCall(SerializedProperty property)
{
var accessor = property.GetAccessor();
if (accessor == null)
return;
CopyCall(accessor, property.serializedObject.targetObject);
}
/************************************************************************************************************************/
/// Overwrites the specified call with the previously copied details.
public static void PasteCall(PersistentCall call)
{
call.CopyFrom(_Call);
}
/// Overwrites the call contained in the specified property with the copied details.
public static void PasteCall(Serialization.PropertyAccessor accessor, Object target)
{
var call = (PersistentCall)accessor.GetValue(target);
PasteCall(call);
}
/// Overwrites the call contained in the specified property with the copied details.
public static void PasteCall(SerializedProperty property)
{
property.ModifyValues((call) =>
{
PasteCall(call);
}, "Paste PersistentCall");
}
/************************************************************************************************************************/
}
}
#endif