From 3eaf912170ed3ec94782b657ab1fd26ca45e7b97 Mon Sep 17 00:00:00 2001 From: jkpete <1031139173@qq.com> Date: Wed, 16 Jul 2025 16:46:56 +0800 Subject: [PATCH] add EGodotTree and can init by json --- Example/SaveSystem/Script/ViewSaveSystem.cs | 68 ++++++--- .../Templete/Godot/UI/EGodotTree.cs | 42 +++--- .../Templete/Variant/EGDataStruct.cs | 132 +++++++++++++++++- 3 files changed, 206 insertions(+), 36 deletions(-) diff --git a/Example/SaveSystem/Script/ViewSaveSystem.cs b/Example/SaveSystem/Script/ViewSaveSystem.cs index adaa482..125b505 100644 --- a/Example/SaveSystem/Script/ViewSaveSystem.cs +++ b/Example/SaveSystem/Script/ViewSaveSystem.cs @@ -1,9 +1,11 @@ using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; using EGFramework.UI; using Godot; using LiteDB; +using Newtonsoft.Json; using Renci.SshNet; namespace EGFramework.Examples.Test { @@ -14,24 +16,7 @@ namespace EGFramework.Examples.Test { Container container{ set; get; } public override void _Ready() { - container = this.GetNode("TabContainer"); - Tree newTree = container.CreateNode("treeTest"); - TreeItem root = newTree.CreateItem(); - TreeItem child1 = newTree.CreateItem(root); - TreeItem child2 = newTree.CreateItem(root); - TreeItem subchild1 = newTree.CreateItem(child1); - subchild1.SetText(0, "Subchild1"); - child1.SetText(0, "child1"); - child2.SetText(0, "child2"); - Image image = Image.LoadFromFile("icon.svg"); - ImageTexture texture = ImageTexture.CreateFromImage(image); - - child2.AddButton(0, texture); - child2.SetButtonColor(0, 0, Colors.AliceBlue); - child2.SetIcon(0, texture); - child2.SetCellMode(0, TreeItem.TreeCellMode.String); - subchild1.SetIndeterminate(0, false); - child1.SetEditable(0,true); + TestTree(); } public override void _ExitTree() @@ -39,8 +24,55 @@ namespace EGFramework.Examples.Test { } + public void TestJson() + { + string json = @"{ + 'CPU': 'Intel', + 'PSU': '500W', + 'Drives': [ + 'DVD read/writer' + /*(broken)*/, + '500 gigabyte hard drive', + '200 gigabyte hard drive' + ], + 'My' : { + 'AA':'BB', + 'Date': new Date(123456789) + } + }"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + while (reader.Read()) + { + if (reader.Value != null) + { + GD.Print("Token: {"+reader.TokenType+"}, Value: {"+ reader.Value+"}"); + } + else + { + GD.Print("Token: {"+ reader.TokenType+"}"); + } + } + } + + public void TestTree() + { + string json = @"{ + 'CPU': 'Intel', + 'PSU': '500W', + 'My' : { + 'AA':'BB', + 'Date': 111 + } + }"; + container = this.GetNode("TabContainer"); + EGodotTree eGodotTree = container.CreateNode("TestTree"); + eGodotTree.InitByJson(json); + } + public void TestTable() { + container = this.GetNode("TabContainer"); List dataStudents = new List(); for (int stu = 0; stu < 10; stu++) { diff --git a/addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotTree.cs b/addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotTree.cs index 24ca247..6e593d9 100644 --- a/addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotTree.cs +++ b/addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotTree.cs @@ -7,29 +7,39 @@ namespace EGFramework.UI { void RenderTree(); } - public partial class EGodotTree : Tree, IEGFramework,IEGodotTree + public partial class EGodotTree : Tree, IEGFramework, IEGodotTree { - public void RenderTree() - { + public EGTree Tree { set; get; } + public void Init(EGTree tree) + { + this.Tree = tree; + RenderTree(); } - } - public struct EGodotTreeItem - { - public string Name { set; get; } - /// - /// SVG Code. - /// - /// - public string IconCode { set; get; } + public void InitByJson(string json) + { + EGTree eGTree = EGTreeFactory.CreateTreeByJson(json); + this.Tree = eGTree; + RenderTree(); + } - public Stack Parents { set; get; } - public List Childs { set; get; } + public void RenderTree() + { + this.HideRoot = true; + CreateTreeItem(Tree, this.GetRoot()); + } + - public void AddChild() + public void CreateTreeItem(EGTree tree,TreeItem parent) { - + TreeItem current = this.CreateItem(parent); + current.SetText(0,tree.Name); + current.SetTooltipText(0, tree.StrValue); + foreach (EGTree child in tree.GetChilds()) + { + CreateTreeItem(child, current); + } } } diff --git a/addons/EGFramework/Module/GenerateTools/Templete/Variant/EGDataStruct.cs b/addons/EGFramework/Module/GenerateTools/Templete/Variant/EGDataStruct.cs index eade494..4e025f8 100644 --- a/addons/EGFramework/Module/GenerateTools/Templete/Variant/EGDataStruct.cs +++ b/addons/EGFramework/Module/GenerateTools/Templete/Variant/EGDataStruct.cs @@ -1,4 +1,10 @@ +using System; using System.Collections.Generic; +using System.IO; +using System.Linq; +using Godot; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace EGFramework { @@ -37,12 +43,13 @@ namespace EGFramework return Path; } } - + public interface IEGReadOnlyString { public string GetString(); } - public struct EGReadOnlyString : IEGReadOnlyString{ + public struct EGReadOnlyString : IEGReadOnlyString + { public string Value { get; private set; } public EGReadOnlyString(string value) { @@ -54,4 +61,125 @@ namespace EGFramework return Value; } } + + public interface IEGTree + { + public string Name { set; get; } + public IEGTree GetParent(); + public void SetParent(IEGTree tree); + public IEnumerable GetChilds(); + public void AppendTree(IEGTree tree); + + } + + public class EGTree : IEGTree + { + public string Name { set; get; } + + public string StrValue { set; get; } + public long IntegerValue { set; get; } + public byte[] ByteValue { set; get; } + public float FloatValue { set; get; } + public bool BoolValue { set; get; } + + public IEGTree Parent { set; get; } + public List Childs { set; get; } = new List(); + + public EGTree() + { + } + + public EGTree(string name) + { + this.Name = name; + } + + public void AppendTree(IEGTree tree) + { + tree.SetParent(this); + Childs.Add(tree); + } + + public virtual IEnumerable GetChilds() + { + return Childs; + } + + public virtual IEGTree GetParent() + { + return Parent; + } + + public void SetParent(IEGTree tree) + { + this.Parent = tree; + } + } + + public static class EGTreeFactory + { + public static EGTree CreateTreeByJson(string json) + { + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + Stack TreeStack = new Stack(); + EGTree resultTree = new EGTree(); + EGTree lastTree = null; + while (reader.Read()) + { + if (reader.TokenType == JsonToken.StartObject) + { + if (lastTree != null) + { + TreeStack.Push(lastTree); + } + } + if (reader.TokenType == JsonToken.EndObject) + { + if(TreeStack.Count>0) + TreeStack.Pop(); + } + + if (reader.TokenType == JsonToken.PropertyName) + { + EGTree newTree = new EGTree(); + if (reader.Value != null) + { + newTree.Name = reader.Value.ToString(); + } + if (TreeStack.Count > 0) + { + TreeStack.Peek().AppendTree(newTree); + } + else + { + resultTree.AppendTree(newTree); + } + lastTree = newTree; + } + + if (lastTree != null && reader.TokenType == JsonToken.Integer) + { + lastTree.IntegerValue = (long)reader.Value; + } + if (lastTree != null && reader.TokenType == JsonToken.Boolean) + { + lastTree.BoolValue = (bool)reader.Value; + } + if (lastTree != null && reader.TokenType == JsonToken.String) + { + lastTree.StrValue = (string)reader.Value; + } + if (lastTree != null && reader.TokenType == JsonToken.Float) + { + lastTree.FloatValue = (float)reader.Value; + } + if (lastTree!=null && reader.TokenType == JsonToken.Bytes) + { + lastTree.ByteValue = (byte[])reader.Value; + } + } + return resultTree; + } + } + } \ No newline at end of file