Browse Source

add table creator

master
jkpete 3 months ago
parent
commit
f5699fb880
  1. 24
      Example/SaveSystem/Script/ViewSaveSystem.cs
  2. 42
      addons/EGFramework/Module/NodeExtension/EGCreate.cs

24
Example/SaveSystem/Script/ViewSaveSystem.cs

@ -20,14 +20,12 @@ namespace EGFramework.Examples.Test{
string[] c = {"Jerry","20"}; string[] c = {"Jerry","20"};
DataList[2] = c; DataList[2] = c;
this.GetNode<TabContainer>("TabContainer").CreateTable(DataList,"Student"); this.GetNode<TabContainer>("TabContainer").CreateTable(DataList,"Student");
DataList2 = new string[3][]; DataStudent dataStudent = new DataStudent("S",18);
string[] d = {"Name","Age"}; DataStudent dataStudent2 = new DataStudent(null,20);
DataList2[0] = d; List<DataStudent> dataStudents = new List<DataStudent>();
string[] e = {"Jim","60"}; dataStudents.Add(dataStudent);
DataList2[1] = e; dataStudents.Add(dataStudent2);
string[] f = {"Bob","50"}; this.GetNode<TabContainer>("TabContainer").CreateTable<DataStudent>(dataStudents,"Teacher");
DataList2[2] = f;
this.GetNode<TabContainer>("TabContainer").CreateTable(DataList2,"Teacher");
} }
public override void _ExitTree() public override void _ExitTree()
@ -36,4 +34,14 @@ namespace EGFramework.Examples.Test{
} }
} }
public struct DataStudent{
public string Name { get; set; }
public int Age;
public int ID;
public DataStudent(string name,int age){
Name = name;
Age = age;
ID = 0;
}
}
} }

42
addons/EGFramework/Module/NodeExtension/EGCreate.cs

@ -1,5 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Godot; using Godot;
using Mysqlx.Crud;
using static Godot.GD; using static Godot.GD;
namespace EGFramework{ namespace EGFramework{
@ -53,8 +59,13 @@ namespace EGFramework{
RowData.Name = rowName; RowData.Name = rowName;
foreach(string s in titleList){ foreach(string s in titleList){
Label label = new Label(); Label label = new Label();
if(s != null){
label.Name = s; label.Name = s;
label.Text = s; label.Text = s;
}else{
label.Name = "Null";
label.Text = "Null";
}
label.HorizontalAlignment = HorizontalAlignment.Center; label.HorizontalAlignment = HorizontalAlignment.Center;
label.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill; label.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
RowData.AddChild(label); RowData.AddChild(label);
@ -71,8 +82,37 @@ namespace EGFramework{
dataPointer++; dataPointer++;
} }
self.AddChild(Table); self.AddChild(Table);
EG.Print("CreateTable",tableStr.Length);
return Table; return Table;
} }
public static VBoxContainer CreateTable<T>(this Node self,IEnumerable<T> tableData,string tableName = "ObjectTable",int limit = 0){
VBoxContainer Table = new VBoxContainer();
Table.Name = tableName;
MemberInfo[] propertyNames = typeof(T).GetProperties();
MemberInfo[] fieldNames = typeof(T).GetFields();
MemberInfo[] memberInfos = propertyNames.Concat(fieldNames).ToArray();
string[] propertyName = new string[memberInfos.Length];
int dataPointer = 0;
for (int i = 0; i < memberInfos.Length; i++)
{
propertyName[i] = memberInfos[i].Name;
}
Table.CreateRowData(propertyName,"Title");
foreach (T t in tableData)
{
string[] s = t.GetType().GetProperties().Select(p => p.GetValue(t)?.ToString()).ToArray();
string[] a = t.GetType().GetFields().Select(p => p.GetValue(t)?.ToString()).ToArray();
string[] result = s.Concat(a).ToArray();
Table.CreateRowData(result, "tableRowData"+dataPointer);
dataPointer++;
}
self.AddChild(Table);
return Table;
}
public static Tree CreateTree(this Node self,string treeName = "Tree"){
Tree tree = new Tree();
tree.Name = treeName;
self.AddChild(tree);
return tree;
}
} }
} }

Loading…
Cancel
Save