Przeglądaj źródła

Updated GenericEvents and Timer utility

James Peret 2 lat temu
rodzic
commit
eb6abd1160
2 zmienionych plików z 55 dodań i 0 usunięć
  1. 39 0
      Runtime/EventManager/GenericEvents.cs
  2. 16 0
      Runtime/Timer.cs

+ 39 - 0
Runtime/EventManager/GenericEvents.cs

@@ -398,5 +398,44 @@ namespace KairoEngine.Core
             }
         }
         #endregion
+
+        #region String_Bool_Event
+        
+        public static Dictionary<string, System.Action<string, bool>> listStringBool = new Dictionary<string, System.Action<string, bool>>();
+        
+        public static void StartListening(string title, System.Action<string, bool> listener)
+        {
+            System.Action<string, bool> action = null;
+            if (listStringBool.TryGetValue(title, out action))
+            {
+                action += listener;
+                listStringBool[title] = action;
+            }
+            else
+            {
+                action += listener;
+                listStringBool.Add(title, action);
+            }
+        }
+
+        public static void StopListening(string title, System.Action<string, bool> listener)
+        {
+            System.Action<string, bool> action = null;
+            if (listStringBool.TryGetValue(title, out action))
+            {
+                action -= listener;
+                listStringBool[title] = action;
+            }
+        }
+
+        public static void Trigger(string title, string text, bool value)
+        {
+            System.Action<string, bool> action = null;
+            if (listStringBool.TryGetValue(title, out action))
+            {
+                if(action != null) action(text, value);
+            }
+        }
+        #endregion
     }
 }

+ 16 - 0
Runtime/Timer.cs

@@ -88,6 +88,22 @@ namespace KairoEngine.Core
             }).AddTo(cancel);
             return cancel;
         }
+
+        /// <summary>
+        /// Executes a function after a certain real time has passed.
+        /// </summary>
+        /// <param name="time">In miliseconds</param>
+        /// <param name="action">A function to be executed</param>
+        /// <returns>A CompositeDisposable which can be used to cancel the timer</returns>
+        public static void ExecuteRealTimeNotDisposable(float time, Action action)
+        {
+            var timer = Observable.Timer(TimeSpan.FromMilliseconds(time), Scheduler.MainThreadIgnoreTimeScale);
+            CompositeDisposable cancel = new CompositeDisposable();
+            timer.Subscribe(xs => {
+                action();
+                cancel.Dispose();
+            }).AddTo(cancel);
+        }
     }
 }