Archived
forked from RDTKEditor/RDTKEditor
149 lines
3.1 KiB
C#
149 lines
3.1 KiB
C#
using RhythmBase.Components;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace RDTKEditor.Models
|
|
{
|
|
internal enum RDPropertyType
|
|
{
|
|
Bool,
|
|
Integer,
|
|
Float,
|
|
Expression,
|
|
IntegerPair,
|
|
FloatPair,
|
|
ExpressionPair,
|
|
String,
|
|
Color,
|
|
Enum,
|
|
}
|
|
internal interface IRDTKPropertyVerifier
|
|
{
|
|
RDPropertyType Type { get; }
|
|
bool TryParse(string value, out object result);
|
|
string ToString(object value);
|
|
}
|
|
internal class RDTKPropertyVerifierEnum(Type enumType) : IRDTKPropertyVerifier
|
|
{
|
|
public RDPropertyType Type => RDPropertyType.Enum;
|
|
public Type EnumType { get; set; } = enumType;
|
|
public bool TryParse(string value, out object result)
|
|
{
|
|
if (Enum.TryParse(EnumType, value, out object v))
|
|
{
|
|
result = v;
|
|
return true;
|
|
}
|
|
result = null;
|
|
return false;
|
|
}
|
|
public string ToString(object value) => value?.ToString() ?? "null";
|
|
}
|
|
internal class RDTKPropertyVerifierBool : IRDTKPropertyVerifier
|
|
{
|
|
public RDPropertyType Type => RDPropertyType.Bool;
|
|
public bool TryParse(string value, out object result)
|
|
{
|
|
var t = bool.TryParse(value, out bool v);
|
|
result = v;
|
|
return t;
|
|
}
|
|
public string ToString(object value) => (bool)value ? "true" : "false";
|
|
}
|
|
internal class RDTKPropertyVerifierDefault : IRDTKPropertyVerifier
|
|
{
|
|
public RDPropertyType Type { get; set; }
|
|
public bool TryParse(string value, out object result)
|
|
{
|
|
result = value;
|
|
return true;
|
|
}
|
|
public string ToString(object value) => value?.ToString() ?? "null";
|
|
}
|
|
internal struct RDTKProperty(object value)
|
|
{
|
|
public event EventHandler<object?>? PropertyChanged = null;
|
|
private object _value = value;
|
|
public string Name { get; set; } = "";
|
|
public string Description { get; set; } = "";
|
|
public RDPropertyType Type { get; set; }
|
|
public IRDTKPropertyVerifier Verifier { get; set; } = new RDTKPropertyVerifierDefault();
|
|
public string Value
|
|
{
|
|
readonly get => Verifier.ToString(_value);
|
|
set
|
|
{
|
|
if (Verifier.TryParse(value, out object result))
|
|
{
|
|
_value = result;
|
|
}
|
|
}
|
|
}
|
|
public void OnChanged(object value)
|
|
{
|
|
if (_value == value) return;
|
|
if (Verifier.TryParse(value?.ToString()??"", out object result))
|
|
{
|
|
_value = result;
|
|
PropertyChanged?.Invoke(this, value);
|
|
}
|
|
}
|
|
}
|
|
internal class RDTKView
|
|
{
|
|
|
|
private RDLevel? Level { get; set; }
|
|
public RDTKView()
|
|
{
|
|
}
|
|
public RDTKViewMessage UpdateLevelByPath(string path)
|
|
{
|
|
try
|
|
{
|
|
Level = RDLevel.Read(path);
|
|
return new()
|
|
{
|
|
Message = "关卡已加载。",
|
|
Level = RDTKViewMessageLevel.Info,
|
|
Details = $"关卡路径:{path}",
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new()
|
|
{
|
|
Message = ex.Message,
|
|
Level = RDTKViewMessageLevel.Error,
|
|
Details = ex.StackTrace ?? "",
|
|
};
|
|
}
|
|
}
|
|
public RDTKViewMessage SaveLevelByPath(string path)
|
|
{
|
|
try
|
|
{
|
|
Level?.Write(path);
|
|
|
|
return new()
|
|
{
|
|
Message = "关卡已加载。",
|
|
Level = RDTKViewMessageLevel.Info,
|
|
Details = $"关卡路径:{path}",
|
|
};
|
|
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new()
|
|
{
|
|
Message = ex.Message,
|
|
Level = RDTKViewMessageLevel.Error,
|
|
Details = ex.StackTrace ?? "",
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|