Skip to content

Commit

Permalink
新增 情景变量
Browse files Browse the repository at this point in the history
修复 情景取消错误
  • Loading branch information
MakesYT committed Nov 2, 2023
1 parent 3be5a05 commit f3ede64
Show file tree
Hide file tree
Showing 6 changed files with 596 additions and 200 deletions.
32 changes: 30 additions & 2 deletions Core/SDKs/CustomScenario/CustomScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Runtime.Serialization;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Core.SDKs.CustomType;
using Core.SDKs.Services.Plugin;
using Core.SDKs.Tools;
using Core.ViewModel.TaskEditor;
Expand Down Expand Up @@ -42,7 +43,7 @@ public partial class CustomScenario : ObservableRecipient, IDisposable

private TickUtil? tick;
[JsonIgnore] [ObservableProperty] private int tickPerSecond = 20;
[JsonIgnore] [ObservableProperty] private Dictionary<string, object> values = new() { { "变量1", "2" } };
[JsonIgnore] [ObservableProperty] private ObservableDictionary<string, object> values = new() { { "1", "2" } };

public string? UUID
{
Expand Down Expand Up @@ -184,7 +185,11 @@ private void StartRun(bool notRealTime)
continue;
}
_cancellationTokenSource.Cancel();
if (notRealTime)
{
_cancellationTokenSource.Cancel();
}
IsRunning = false;
Log.Debug($"场景运行完成:{Name}");
break;
Expand Down Expand Up @@ -364,6 +369,29 @@ private void ParsePointItem(PointItem nowPointItem, bool onlyForward, bool notRe

break;
}
case "valueSet":
{
if (Values.ContainsKey(nowPointItem.ValueRef!))
{
Values.SetValueWithoutNotify(nowPointItem.ValueRef!,
nowPointItem.Input[1].InputObject!);
}

break;
}
case "valueGet":
{
if (Values.ContainsKey(nowPointItem.ValueRef!))
{
foreach (var item in nowPointItem.Output[0].GetSourceOrNextConnectorItems(connections))
{
item.InputObject = Values[nowPointItem.ValueRef!];
MakeSourcePointState(item, nowPointItem);
}
}

break;
}
default:
{
var userInputConnector = nowPointItem.Input.FirstOrDefault();
Expand Down
8 changes: 7 additions & 1 deletion Core/SDKs/CustomScenario/PointItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public string? Plugin
}


public string MerthodName
public string? MerthodName
{
get;
set;
}

public string? ValueRef
{
get;
set;
Expand Down
187 changes: 187 additions & 0 deletions Core/SDKs/CustomType/ObservableDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using System.Collections.Specialized;
using System.ComponentModel;

namespace Core.SDKs.CustomType;

public class ObservableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, INotifyCollectionChanged,
INotifyPropertyChanged
{
private int _index;

public new KeyCollection Keys => base.Keys;

public new ValueCollection Values => base.Values;

public new int Count => base.Count;

public new TValue this[TKey key]
{
get => GetValue(key);
set => SetValue(key, value);
}

public TValue this[int index]
{
get => GetIndexValue(index);
set => SetIndexValue(index, value);
}

public event NotifyCollectionChangedEventHandler? CollectionChanged;
public event PropertyChangedEventHandler? PropertyChanged;

public new void Add(TKey key, TValue value)
{
base.Add(key, value);
OnCollectionChanged(
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, FindPair(key), _index));
OnPropertyChanged("Keys");
OnPropertyChanged("Values");
OnPropertyChanged("Count");
}

public new void Clear()
{
base.Clear();
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
OnPropertyChanged("Keys");
OnPropertyChanged("Values");
OnPropertyChanged("Count");
}

public new bool Remove(TKey key)
{
var pair = FindPair(key);
if (base.Remove(key))
{
OnCollectionChanged(
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, pair, _index));
OnPropertyChanged("Keys");
OnPropertyChanged("Values");
OnPropertyChanged("Count");
return true;
}

return false;
}

protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
{
CollectionChanged(this, e);
}
}

protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

#region private方法

private TValue GetIndexValue(int index)
{
for (int i = 0; i < Count; i++)
{
if (i == index)
{
var pair = this.ElementAt(i);
return pair.Value;
}
}

return default(TValue);
}

private void SetIndexValue(int index, TValue value)
{
try
{
var pair = this.ElementAtOrDefault(index);
SetValue(pair.Key, value);
}
catch (Exception)
{
}
}

private TValue GetValue(TKey key)
{
if (ContainsKey(key))
{
return base[key];
}
else
{
return default(TValue);
}
}

public void SetValueWithoutNotify(TKey key, TValue value)
{
if (ContainsKey(key))
{
base[key] = value;
}
else
{
Add(key, value);
}
}

private void SetValue(TKey key, TValue value)
{
if (ContainsKey(key))
{
var pair = FindPair(key);
int index = _index;
base[key] = value;
var newpair = FindPair(key);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newpair,
pair, index));
OnPropertyChanged("Values");
OnPropertyChanged("Item[]");
}
else
{
Add(key, value);
}
}

private KeyValuePair<TKey, TValue> FindPair(TKey key)
{
_index = 0;
foreach (var item in this)
{
if (item.Key.Equals(key))
{
return item;
}

_index++;
}

return default(KeyValuePair<TKey, TValue>);
}

private int IndexOf(TKey key)
{
int index = 0;
foreach (var item in this)
{
if (item.Key.Equals(key))
{
return index;
}

index++;
}

return -1;
}

#endregion
}
38 changes: 38 additions & 0 deletions Core/ViewModel/TaskEditor/TaskEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ private void AddNodes(PointItem pointItem)
{
Title = pointItem.Title,
Plugin = pointItem.Plugin,
ValueRef = pointItem.ValueRef,
MerthodName = pointItem.MerthodName,
Location = new Point(pointItem.Location.X, pointItem.Location.Y)
};
Expand Down Expand Up @@ -285,6 +286,7 @@ private void CopyNode(PointItem pointItem)
{
Title = pointItem.Title,
Plugin = pointItem.Plugin,
ValueRef = pointItem.ValueRef,
MerthodName = pointItem.MerthodName,
Location = new Point(pointItem.Location.X + 40, pointItem.Location.Y + 40)
};
Expand Down Expand Up @@ -609,6 +611,42 @@ private void AddKey(string key)
}

#endregion

#region 变量

[NotifyPropertyChangedFor(nameof(valueCanAdd))]
[NotifyCanExecuteChangedFor(nameof(AddValueCommand))]
[ObservableProperty]
private string? _valueValue = String.Empty;

private bool valueCanAdd => !string.IsNullOrEmpty(ValueValue);

[RelayCommand(CanExecute = nameof(valueCanAdd))]
private void AddValue(string key)
{
if (Scenario.Values.ContainsKey(key))
{
return;
}

ValueValue = null;
Scenario.Values.Add(key, new object());
OnPropertyChanged(CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Values);
IsModified = true;
}

[RelayCommand]
private void DelValue(string key)
{
if (Scenario.Values.ContainsKey(key))
{
Scenario.Values.Remove(key);
}

IsModified = true;
}

#endregion
}

public class ConnectionItem
Expand Down
Loading

0 comments on commit f3ede64

Please sign in to comment.