-
-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathBlackboardKeyViewModel.cs
107 lines (94 loc) · 3.06 KB
/
BlackboardKeyViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System.Collections.Generic;
namespace Nodify.StateMachine
{
public class BlackboardKeyViewModel : ObservableObject
{
// Cache the key and the input value so we can restore them when swapping input types
private readonly Dictionary<bool, object?> _values = new Dictionary<bool, object?>();
public string? PropertyName { get; set; }
private string _name = "New key";
public string Name
{
get => _name;
set
{
if (!string.IsNullOrWhiteSpace(value))
{
SetProperty(ref _name, value);
}
}
}
private BlackboardKeyType _type;
public BlackboardKeyType Type
{
get => _type;
set
{
if (SetProperty(ref _type, value))
{
Value = GetDefaultValue(_type);
}
}
}
private object? _value = BoxValue.False;
public object? Value
{
get => _value;
set => SetProperty(ref _value, GetRealValue(value)).Then(() => _values[ValueIsKey] = Value);
}
private bool _valueIsKey;
public bool ValueIsKey
{
get => _valueIsKey;
set
{
if (SetProperty(ref _valueIsKey, value) && _values.TryGetValue(_valueIsKey, out var existingValue))
{
Value = existingValue;
}
}
}
private bool _canChangeType = true;
public bool CanChangeType
{
get => _canChangeType;
set => SetProperty(ref _canChangeType, value);
}
private object? GetRealValue(object? value)
{
if (value is string str)
{
switch (Type)
{
case BlackboardKeyType.Boolean:
bool.TryParse(str, out var b);
value = b;
break;
case BlackboardKeyType.Integer:
int.TryParse(str, out var i);
value = i;
break;
case BlackboardKeyType.Double:
double.TryParse(str, out var d);
value = d;
break;
case BlackboardKeyType.String:
case BlackboardKeyType.Object:
value = str;
break;
}
}
return value;
}
public static object? GetDefaultValue(BlackboardKeyType type)
=> type switch
{
BlackboardKeyType.Boolean => BoxValue.False,
BlackboardKeyType.Integer => BoxValue.Int0,
BlackboardKeyType.Double => BoxValue.Double0,
BlackboardKeyType.String => null,
BlackboardKeyType.Object => null,
_ => null
};
}
}