Browse Source

add timer execute and fixed Name bugs in Example

master
jkpete 2 months ago
parent
commit
98d5d0d6e3
  1. 2
      Example/SystemExamples/PlayerStorage/Script/DataStorageItem.cs
  2. 2
      Example/SystemExamples/PlayerStorage/Script/InterfaceStorage.cs
  3. 12
      Example/UsingTest/Script/EGSaveTest.cs
  4. 62
      addons/EGFramework/Module/EGEvent.cs
  5. 34
      addons/EGFramework/Module/NodeExtension/EGThread.cs

2
Example/SystemExamples/PlayerStorage/Script/DataStorageItem.cs

@ -28,7 +28,7 @@ namespace EGFramework.Example.SystemExamples.PlayerStorage
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string GetName() public string GetItemName()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

2
Example/SystemExamples/PlayerStorage/Script/InterfaceStorage.cs

@ -9,7 +9,7 @@ namespace EGFramework.Example.SystemExamples.PlayerStorage
public interface IItem public interface IItem
{ {
public int GetItemId(); public int GetItemId();
public string GetName(); public string GetItemName();
public Texture GetIcon(); public Texture GetIcon();
public string GetInfo(); public string GetInfo();
} }

12
Example/UsingTest/Script/EGSaveTest.cs

@ -12,19 +12,23 @@ namespace EGFramework.Examples.Test{
{ {
this.Label = this.GetNode<Label>("Label"); this.Label = this.GetNode<Label>("Label");
this.EGEnabledThread(); this.EGEnabledThread();
TestThread(); this.ExecuteAfterSecond(TestMainThreadFunc,2.0f);
//base._Ready(); //base._Ready();
//TestCode(); //TestCode();
} }
public async void TestThread(){ public async void TestThread(){
await Task.Run(()=>{ await Task.Run(()=>{
this.ExecuteInMainThread(()=>{ //this.ExecuteInMainThread(TestMainThreadFunc);
this.Label.Text = "Thread Test"; this.ExecuteAfterSecond(TestMainThreadFunc,2.0f);
});
}); });
} }
public void TestMainThreadFunc(){
this.Label.Text = "Thread Test";
GD.Print("Invoked!");
}
public void TestSqlite(){ public void TestSqlite(){
// string result = this.EGSqlite().CreateTable<SqliteBackpackItem>(); // string result = this.EGSqlite().CreateTable<SqliteBackpackItem>();
this.EGSqlite().SaveData(new SqliteBackpackItem{ this.EGSqlite().SaveData(new SqliteBackpackItem{

62
addons/EGFramework/Module/EGEvent.cs

@ -42,7 +42,7 @@ namespace EGFramework
} }
} }
public class EasyEvents public class EasyEvents
{ {
private static EasyEvents GlobalEvents = new EasyEvents(); private static EasyEvents GlobalEvents = new EasyEvents();
public static T Get<T>() where T : IEasyEvent public static T Get<T>() where T : IEasyEvent
@ -80,6 +80,66 @@ namespace EGFramework
} }
} }
/// <summary>
/// This EasyEvent will release all registered function while invoked.
/// </summary>
/// <typeparam name="T"></typeparam>
public class EasyEventOnce<T> : IEasyEvent
{
private Action<T> OnEvent = e => { };
private List<CustomUnRegister> AutoUnRegister = new List<CustomUnRegister>();
public IUnRegister Register(Action<T> onEvent)
{
OnEvent += onEvent;
CustomUnRegister unRegister = new CustomUnRegister(() => { UnRegister(onEvent); });
AutoUnRegister.Add(unRegister);
return unRegister;
}
public void UnRegister(Action<T> onEvent)
{
OnEvent -= onEvent;
}
public void Invoke(T t)
{
if(AutoUnRegister.Count>0){
OnEvent?.Invoke(t);
foreach(CustomUnRegister unRegister in AutoUnRegister){
unRegister.UnRegister();
}
AutoUnRegister.Clear();
}
}
}
/// <summary>
/// This EasyEvent will release all registered function while invoked.
/// </summary>
public class EasyEventOnce : IEasyEvent{
private Action OnEvent = () => { };
private List<CustomUnRegister> AutoUnRegister = new List<CustomUnRegister>();
public IUnRegister Register(Action onEvent)
{
OnEvent += onEvent;
CustomUnRegister unRegister = new CustomUnRegister(() => { UnRegister(onEvent); });
AutoUnRegister.Add(unRegister);
return unRegister;
}
public void UnRegister(Action onEvent)
{
OnEvent -= onEvent;
}
public void Invoke()
{
if(AutoUnRegister.Count>0){
OnEvent?.Invoke();
foreach(CustomUnRegister unRegister in AutoUnRegister){
unRegister.UnRegister();
}
AutoUnRegister.Clear();
}
}
}
public static class CanRegisterEventExtension public static class CanRegisterEventExtension
{ {

34
addons/EGFramework/Module/NodeExtension/EGThread.cs

@ -6,13 +6,14 @@ using System.Collections.Generic;
namespace EGFramework{ namespace EGFramework{
public partial class EGThread : Node,IModule,IEGFramework{ public partial class EGThread : Node,IModule,IEGFramework{
public Queue<Action> ActionQueue = new Queue<Action>(); public EasyEventOnce EventPool = new EasyEventOnce();
public IOCContainer ActionPool = new IOCContainer(); public Dictionary<Action,SceneTreeTimer> EventDelayPool = new Dictionary<Action, SceneTreeTimer>();
public void Init() public void Init()
{ {
} }
public override void _Ready() public override void _Ready()
{ {
@ -21,22 +22,21 @@ namespace EGFramework{
public override void _Process(double delta) public override void _Process(double delta)
{ {
//base._Process(delta); //base._Process(delta);
if(ActionQueue.Count>0){ EventPool.Invoke();
Action execute = ActionQueue.Dequeue();
execute.Invoke();
execute = null;
}
} }
public void ExecuteInMainThread(Action action){ public void ExecuteInMainThread(Action action){
ActionQueue.Enqueue(action); //ActionQueue.Enqueue(action);
EventPool.Register(action);
} }
public void ExecuteInMainThread<T>(Action<T> action){ public void ExecuteAfterSecond(Action action,double delay){
SceneTreeTimer timer = this.GetTree().CreateTimer(delay);
timer.Timeout += action;
timer.Timeout += () => EventDelayPool.Remove(action);
EventDelayPool.Add(action,timer);
} }
public IArchitecture GetArchitecture() public IArchitecture GetArchitecture()
{ {
return EGArchitectureImplement.Interface; return EGArchitectureImplement.Interface;
@ -49,16 +49,8 @@ namespace EGFramework{
self.NodeModule<EGThread>().ExecuteInMainThread(action); self.NodeModule<EGThread>().ExecuteInMainThread(action);
} }
public static void ExecuteInMainThread<T>(this Node self, Action<T> action){ public static void ExecuteAfterSecond(this Node self, Action action,double delay){
//action.Invoke(); self.NodeModule<EGThread>().ExecuteAfterSecond(action,delay);
}
public static void ExecuteAfterSecond(this Node self, Action action,float delay){
}
public static void ExecuteAfterSecond<T>(this Node self, Action<T> action,float delay){
} }
public static void EGEnabledThread(this Node self){ public static void EGEnabledThread(this Node self){

Loading…
Cancel
Save