using Prism.Mvvm;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace PLCSharp.VVMs.Vision
{
///
/// 局部变量表中的一条记录,保存在 中。
///
public class LocalVariableItem : BindableBase
{
private string _Name = "";
///
/// 变量名称
///
public string Name
{
get { return _Name; }
set { SetProperty(ref _Name, value); }
}
private string _VarType = "";
///
/// 变量类型:Point / Line / Circle / Rect / Double
///
public string VarType
{
get { return _VarType; }
set { SetProperty(ref _VarType, value); }
}
private string _StrValue = "";
[JsonIgnore]
public string StrValue
{
get { return _StrValue; }
set { SetProperty(ref _StrValue, value); }
}
private object _RawValue = "";
///
/// 原始值
///
public object RawValue
{
get { return _RawValue; }
set
{
try
{
switch (_VarType)
{
case "Pos":
{
var r = new Pos();
if (value is Pos pos)
{
r = pos;
}
else
{
if (value == null) break;
r = JsonSerializer.Deserialize(value.ToString(), PLCSharp.Core.Tools.JsonOptions.Default);
}
SetProperty(ref _RawValue, r);
}
break;
case "Line":
{
var r = new Line();
if (value is Line line)
{
r = line;
}
else
{
if (value == null) break;
r = JsonSerializer.Deserialize(value.ToString(), PLCSharp.Core.Tools.JsonOptions.Default);
}
SetProperty(ref _RawValue, r);
}
break;
case "Circle":
{
var r = new Circle();
if (value is Circle line)
{
r = line;
}
else
{
if (value == null) break;
r = JsonSerializer.Deserialize(value.ToString(), PLCSharp.Core.Tools.JsonOptions.Default);
}
SetProperty(ref _RawValue, r);
}
break;
case "Rect":
{
var r = new Rect();
if (value is Rect rect)
{
r = rect;
}
else
{
if (value == null) break;
r = JsonSerializer.Deserialize(value.ToString(), PLCSharp.Core.Tools.JsonOptions.Default);
}
SetProperty(ref _RawValue, r);
}
break;
case "String":
case "Double":
SetProperty(ref _RawValue, value);
break;
case "Barcode":
{
var r = new Barcode();
if (value is Barcode barcode)
{
r = barcode;
}
else
{
if (value == null) break;
r = JsonSerializer.Deserialize(value.ToString(), PLCSharp.Core.Tools.JsonOptions.Default);
}
SetProperty(ref _RawValue, r);
}
break;
}
StrValue = FormatValue(_RawValue, _VarType);
}
catch (Exception)
{
}
}
}
///
/// 默认构造函数(用于 JSON 反序列化)
///
public LocalVariableItem()
{ }
///
/// 用指定名称、类型和初始值构造变量项,自动生成显示文本
///
/// 变量名称
/// 变量类型(Point/Line/Circle/Rect/Double)
/// 初始值
public LocalVariableItem(string name, string varType, object? rawValue)
{
_Name = name;
_VarType = varType;
RawValue = rawValue;
}
private static string FormatValue(object? val, string type)
{
if (val == null) return "null";
try
{
switch (type)
{
case "Pos":
if (val is Pos pt)
return $"位置(X:{pt.X:F4}, Y:{pt.Y:F4}), 角度:({pt.Angle:F4})";
else
return val?.ToString() ?? "";
case "Line":
if (val is Line line)
return $"(X:{line.From.X:F4}, Y:{line.From.Y:F4}) -> (X:{line.To.X:F4}, Y:{line.To.Y:F4})";
else
return val?.ToString() ?? "";
case "Circle":
if (val is Circle c)
return $"中心(X:{c.Center.X:F4} Y:{c.Center.Y:F4}) 半径=({c.Radius:F4})";
else
return val?.ToString() ?? "";
case "Rect":
if (val is Rect r)
return $"中心:(X:{r.Center.X:F4}, Y:{r.Center.Y:F4}) 宽高:({r.Width:F4} × {r.Height:F4}) 角度:({r.Center.Angle:F4})";
else
return val?.ToString() ?? "";
case "String":
return val?.ToString() ?? "";
default:
return val?.ToString() ?? "";
}
}
catch (Exception)
{
return val?.ToString() ?? "";
}
}
}
}