Parcourir la source

Updated GenericEvents and Timer utility

James Peret il y a 2 ans
Parent
commit
eb6abd1160
2 fichiers modifiés avec 55 ajouts et 0 suppressions
  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
         #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);
             }).AddTo(cancel);
             return 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);
+        }
     }
     }
 }
 }