Skip to content

Commit

Permalink
fix: fix each node local ref binding error
Browse files Browse the repository at this point in the history
  • Loading branch information
molingyu committed Jun 30, 2024
1 parent 181699d commit bc4237a
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions GUML/GumlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ public struct BindingExprEnv
public Control BindObj;
public bool IsDefine;
public Dictionary<string, object> GlobalRefs;
public Dictionary<string, (INotifyListChanged, int)> LocalRefs;
public Dictionary<string, object?> LocalRefs;
}

public static class GumlRenderer
{
private static GumlDoc _sGumlDoc;
private static GuiController? _sController;
private static Stack<Dictionary<string, object?>> _sLocalStack = new ();

private static Stack<INotifyListChanged> _eachSource = new ();
private static Stack<int> _eachIndex = new ();
private static Stack<string> _eachIndexName = new ();

public static void Render(GumlDoc gumlDoc, GuiController controller, Node rootNode, string dir)
{
ReinitializeRender();
Expand Down Expand Up @@ -83,6 +86,9 @@ private static void ReinitializeRender()
{
_sController = null;
_sLocalStack = new Stack<Dictionary<string, object?>>();
_eachSource = new Stack<INotifyListChanged>();
_eachIndex = new Stack<int>();
_eachIndexName = new Stack<string>();
}

private static Control CreateComponent(GumlSyntaxNode node)
Expand Down Expand Up @@ -121,7 +127,7 @@ private static Control CreateComponent(GumlSyntaxNode node)
BindObj = guiNode,
IsDefine = true,
GlobalRefs = new Dictionary<string, object>(),
LocalRefs = new Dictionary<string, (INotifyListChanged, int)>(),
LocalRefs = new Dictionary<string, object?>(),
Source = propertyNode
};
value = ExprEval(propertyNode, env);
Expand Down Expand Up @@ -164,13 +170,18 @@ private static Control CreateComponent(GumlSyntaxNode node)
{
_sLocalStack.Peek()[eachNode.IndexName] = index;
_sLocalStack.Peek()[eachNode.ValueName] = obj;
_eachSource.Push(dataSource);
_eachIndex.Push(index);
_eachIndexName.Push(eachNode.IndexName);
eachNode.Children.ForEach(child =>
{
guiNode.AddChild(CreateComponent(child));
});
index += 1;
}
_eachSource.Pop();
_eachIndex.Pop();
_eachIndexName.Pop();
var localStack = CopyEnv(_sLocalStack);
var controller = _sController;
dataSource.ListChanged += (_, changeType, changeIndex, obj) =>
Expand Down Expand Up @@ -653,16 +664,24 @@ private static Control GetLocalAliasRefValue(GumlValueNode valueNode)
{
if (env != null)
{
if (!env.Value.IsDefine)
if (env.Value.IsDefine)
{
if (valueNode.RefName == _eachIndexName.Peek())
{
env.Value.LocalRefs.Add(valueNode.RefName, _eachIndex.Peek());
}
else
{
env.Value.LocalRefs.Add(valueNode.RefName, _eachSource.Peek()[_eachIndex.Peek()]);
}
}
else
{
if (env.Value.LocalRefs.TryGetValue(valueNode.RefName, out var value))
{
if (value.Item1.Count - 1 > value.Item2)
{
return value.Item1[value.Item2]!;
}
throw new Exception();
return value;
}
throw new Exception($"Local ref '{valueNode.RefName}' not find.");
}
}
foreach (var dict in _sLocalStack)
Expand Down

0 comments on commit bc4237a

Please sign in to comment.