Browse Source

add primary key get method,and add default data

master
jkpete 2 months ago
parent
commit
6b67781961
  1. 4
      Example/SaveSystem/Script/ViewSaveSystem.cs
  2. 2
      addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotSaveTable.cs
  3. 33
      addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotTable.cs
  4. 66
      addons/EGFramework/Module/GenerateTools/Templete/Variant/EGVariantGenerator.cs

4
Example/SaveSystem/Script/ViewSaveSystem.cs

@ -23,11 +23,11 @@ namespace EGFramework.Examples.Test {
{ {
dataStudents.Add(new DataStudent("A" + stu, 20 + stu)); dataStudents.Add(new DataStudent("A" + stu, 20 + stu));
} }
EGodotTable table = container.CreateNode<EGodotTable>("Teacher"); EGodotTable table = container.CreateNode<EGodotTable>("Default");
table.InitData<DataStudent>(dataStudents); table.InitData<DataStudent>(dataStudents);
IEGSaveData SqliteTest = this.EGSave().LoadDataFile<EGSqliteSave>("SaveData/test.db"); IEGSaveData SqliteTest = this.EGSave().LoadDataFile<EGSqliteSave>("SaveData/test.db");
EGodotSaveTable PersonTable = container.CreateNode<EGodotSaveTable>("Person"); EGodotSaveTable PersonTable = container.CreateNode<EGodotSaveTable>("SQLite");
PersonTable.InitSaveData<EGSqliteSave>(SqliteTest); PersonTable.InitSaveData<EGSqliteSave>(SqliteTest);
PersonTable.InitData<DataPerson>("person"); PersonTable.InitData<DataPerson>("person");

2
addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotSaveTable.cs

@ -36,9 +36,9 @@ namespace EGFramework.UI
InitTitle(typeof(T).EGenerateDictiontaryByType()); InitTitle(typeof(T).EGenerateDictiontaryByType());
InitRowData(null); InitRowData(null);
InitPageMenu(); InitPageMenu();
} }
public void QueryPageData<T>() where T : new() public void QueryPageData<T>() where T : new()
{ {
if (PageAdapter.CurrentPage <= 1) if (PageAdapter.CurrentPage <= 1)

33
addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotTable.cs

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@ -28,6 +29,10 @@ namespace EGFramework.UI
protected List<Dictionary<string, object>> TableData { set; get; } protected List<Dictionary<string, object>> TableData { set; get; }
protected Dictionary<string,object> EmptyData { set; get; }
protected EasyEvent<Dictionary<string, object>> AddData { set; get; } = new EasyEvent<Dictionary<string, object>>();
/// <summary> /// <summary>
/// The max data count for one page. /// The max data count for one page.
/// </summary> /// </summary>
@ -47,12 +52,28 @@ namespace EGFramework.UI
PageAdapter.Reload(count, PageLimit); PageAdapter.Reload(count, PageLimit);
} }
this.Vertical = true; this.Vertical = true;
EmptyData = typeof(T).EGenerateEmptyDictiontaryByType();
InitFunctionMenu(); InitFunctionMenu();
InitTitle(typeof(T).EGenerateDictiontaryByType()); InitTitle(typeof(T).EGenerateDictiontaryByType());
InitRowData(tableData.EGenerateDictionaryByGroup()); InitRowData(tableData.EGenerateDictionaryByGroup());
InitPageMenu(); InitPageMenu();
} }
public virtual void OnAddData(Dictionary<string, object> data)
{
GD.Print("Add : " + data["Name"]);
string primaryKey = data.EGetDefaultPrimaryKey();
if (primaryKey != "")
{
data[primaryKey] = TableData.Count.ToString();
}
TableData.Add(new Dictionary<string, object>(data));
PageAdapter.DataLength++;
PageAdapter.Reload(PageAdapter.DataLength, PageLimit);
InitPageData();
OnPageChanged.Invoke();
}
public virtual void InitFunctionMenu() public virtual void InitFunctionMenu()
{ {
@ -60,6 +81,16 @@ namespace EGFramework.UI
{ {
FunctionContainer = this.CreateNode<BoxContainer>("FunctionContainer"); FunctionContainer = this.CreateNode<BoxContainer>("FunctionContainer");
FunctionContainer.Vertical = false; FunctionContainer.Vertical = false;
Button add = FunctionContainer.CreateNode<Button>("add");
add.Text = "Add";
add.Connect("pressed", Callable.From(()=>this.EGEditDialog(EmptyData, OnAddData, "Add")));
add.FocusMode = FocusModeEnum.None;
Button refresh = FunctionContainer.CreateNode<Button>("refresh");
refresh.Text = "Refresh";
refresh.Connect("pressed", Callable.From(InitPageData));
refresh.FocusMode = FocusModeEnum.None;
} }
} }
public void InitTitle(Dictionary<string, object> titleData) public void InitTitle(Dictionary<string, object> titleData)
@ -67,7 +98,7 @@ namespace EGFramework.UI
if (Title == null) if (Title == null)
{ {
Title = this.CreateNode<EGodotRowData>("TitleContainer"); Title = this.CreateNode<EGodotRowData>("TitleContainer");
titleData.Add("Operate", ""); titleData.Add("Operate", "Operate");
Title.Init(titleData); Title.Init(titleData);
} }
else else

66
addons/EGFramework/Module/GenerateTools/Templete/Variant/EGVariantGenerator.cs

@ -4,16 +4,35 @@ using System.Linq;
using System.Reflection; using System.Reflection;
namespace EGFramework{ namespace EGFramework{
public static class EGenerateVariant{ public static class EGenerateVariant
public static Dictionary<string,object> EGenerateDictiontaryByType(this Type self){ {
public static Dictionary<string, object> EGenerateDictiontaryByType(this Type self)
{
PropertyInfo[] propertyNames = self.GetProperties();
FieldInfo[] fieldNames = self.GetFields();
Dictionary<string, object> result = new Dictionary<string, object>();
foreach (PropertyInfo pName in propertyNames)
{
result.Add(pName.Name, pName.Name);
}
foreach (FieldInfo fName in fieldNames)
{
result.Add(fName.Name, fName.Name);
}
return result;
}
public static Dictionary<string, object> EGenerateEmptyDictiontaryByType(this Type self)
{
PropertyInfo[] propertyNames = self.GetProperties(); PropertyInfo[] propertyNames = self.GetProperties();
FieldInfo[] fieldNames = self.GetFields(); FieldInfo[] fieldNames = self.GetFields();
Dictionary<string,object> result = new Dictionary<string, object>(); Dictionary<string, object> result = new Dictionary<string, object>();
foreach(PropertyInfo pName in propertyNames){ foreach (PropertyInfo pName in propertyNames)
result.Add(pName.Name,pName.Name); {
result.Add(pName.Name, "");
} }
foreach(FieldInfo fName in fieldNames){ foreach (FieldInfo fName in fieldNames)
result.Add(fName.Name,fName.Name); {
result.Add(fName.Name, "");
} }
return result; return result;
} }
@ -38,23 +57,40 @@ namespace EGFramework{
return result; return result;
} }
public static List<Dictionary<string,object>> EGenerateDictionaryByGroup<T>(this IEnumerable<T> self){ public static List<Dictionary<string, object>> EGenerateDictionaryByGroup<T>(this IEnumerable<T> self)
List<Dictionary<string,object>> result = new List<Dictionary<string, object>>(); {
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
PropertyInfo[] propertyNames = typeof(T).GetProperties(); PropertyInfo[] propertyNames = typeof(T).GetProperties();
FieldInfo[] fieldNames = typeof(T).GetFields(); FieldInfo[] fieldNames = typeof(T).GetFields();
foreach(T member in self){ foreach (T member in self)
Dictionary<string,object> mResult = new Dictionary<string, object>(); {
foreach(PropertyInfo pName in propertyNames){ Dictionary<string, object> mResult = new Dictionary<string, object>();
foreach (PropertyInfo pName in propertyNames)
{
object p = pName.GetValue(member); object p = pName.GetValue(member);
mResult.Add(pName.Name,p); mResult.Add(pName.Name, p);
} }
foreach(FieldInfo fName in fieldNames){ foreach (FieldInfo fName in fieldNames)
{
object p = fName.GetValue(member); object p = fName.GetValue(member);
mResult.Add(fName.Name,p); mResult.Add(fName.Name, p);
} }
result.Add(mResult); result.Add(mResult);
} }
return result; return result;
} }
//Default primary key is id,Id,ID.
public static string EGetDefaultPrimaryKey(this Dictionary<string, object> self)
{
foreach (KeyValuePair<string, object> param in self)
{
if (param.Key == "ID" || param.Key == "Id" || param.Key == "id")
{
return param.Key;
}
}
return "";
}
} }
} }
Loading…
Cancel
Save