// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using FlaxEditor.SceneGraph;
using FlaxEngine;
using Newtonsoft.Json;
namespace FlaxEditor
{
internal class SceneTreeNodeConverter : JsonConverter
{
///
public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
Guid id = Guid.Empty;
if (value is SceneGraphNode obj)
id = obj.ID;
writer.WriteValue(id.ToString("N"));
}
///
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
{
var id = Guid.Parse((string)reader.Value);
return SceneGraphFactory.FindNode(id);
}
return null;
}
///
public override bool CanConvert(Type objectType)
{
return typeof(SceneGraphNode).IsAssignableFrom(objectType);
}
}
///
/// Base class for implementations. Stores undo data serialized and preserves references to the game objects.
///
/// The type of the data. Must have .
///
[Serializable]
public abstract class UndoActionBase : IUndoAction where TData : struct
{
///
/// The serialized data (Json text).
///
[Serialize]
protected string _data;
///
/// Gets or sets the serialized undo data.
///
[NoSerialize]
public virtual TData Data
{
get => JsonConvert.DeserializeObject(_data, FlaxEngine.Json.JsonSerializer.Settings);
protected set => _data = JsonConvert.SerializeObject(value, Formatting.None, FlaxEngine.Json.JsonSerializer.Settings);
}
///
public abstract string ActionString { get; }
///
public abstract void Do();
///
public abstract void Undo();
///
public virtual void Dispose()
{
_data = null;
}
}
}