Browse Source

add table creator

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

24
Example/SaveSystem/Script/ViewSaveSystem.cs

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

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

@ -1,5 +1,11 @@ @@ -1,5 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Godot;
using Mysqlx.Crud;
using static Godot.GD;
namespace EGFramework{
@ -53,8 +59,13 @@ namespace EGFramework{ @@ -53,8 +59,13 @@ namespace EGFramework{
RowData.Name = rowName;
foreach(string s in titleList){
Label label = new Label();
label.Name = s;
label.Text = s;
if(s != null){
label.Name = s;
label.Text = s;
}else{
label.Name = "Null";
label.Text = "Null";
}
label.HorizontalAlignment = HorizontalAlignment.Center;
label.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
RowData.AddChild(label);
@ -71,8 +82,37 @@ namespace EGFramework{ @@ -71,8 +82,37 @@ namespace EGFramework{
dataPointer++;
}
self.AddChild(Table);
EG.Print("CreateTable",tableStr.Length);
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