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

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

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

12
Example/UsingTest/Script/EGSaveTest.cs

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

62
addons/EGFramework/Module/EGEvent.cs

@ -42,7 +42,7 @@ namespace EGFramework @@ -42,7 +42,7 @@ namespace EGFramework
}
}
public class EasyEvents
public class EasyEvents
{
private static EasyEvents GlobalEvents = new EasyEvents();
public static T Get<T>() where T : IEasyEvent
@ -80,6 +80,66 @@ namespace EGFramework @@ -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
{

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

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

Loading…
Cancel
Save