public static void TriggerEvent(string eventName, System.Object arg = null) { REvent thisEvent = null; if (instance.eventDictionary.TryGetValue(eventName, out thisEvent)) { thisEvent.Invoke(arg); } }
public static void StopListening(string eventName, UnityAction <System.Object> listener) { if (eventManager == null) { return; } REvent thisEvent = null; if (instance.eventDictionary.TryGetValue(eventName, out thisEvent)) { thisEvent.RemoveListener(listener); } }
public static void StartListening(string eventName, UnityAction <System.Object> listener) { REvent thisEvent = null; if (instance.eventDictionary.TryGetValue(eventName, out thisEvent)) { thisEvent.AddListener(listener); } else { thisEvent = new REvent(); thisEvent.AddListener(listener); instance.eventDictionary.Add(eventName, thisEvent); } }
static void Main(string[] args) { #region Start building tree out of TestTreeNodes objects TestTreeNode topNode = new TestTreeNode { NodeInfo = "TopNode" }; TestTreeNode level2Node1 = topNode.AddChild("Level2Node_1"); TestTreeNode level2Node2 = topNode.AddChild("Level2Node_2"); TestTreeNode level3Node1 = level2Node1.AddChild("Level3Node_1"); TestTreeNode level3Node2 = level2Node1.AddChild("Level3Node_2"); TestTreeNode level3Node3 = level2Node2.AddChild("Level3Node_3"); TestTreeNode level3Node4 = level2Node2.AddChild("Level3Node_4"); #endregion End tree building Func<TestTreeNode, TestTreeNode> toParentTreeFunction = (node) => node.Parent; Func<TestTreeNode, IEnumerable<TestTreeNode>> toChildrenTreeFunction = (node) => node.Children; IEnumerable<TreeNodeInfo<TestTreeNode>> allTreeNodes = topNode.SelfAndDescendantsWithLevelInfo(toChildrenTreeFunction); // print all the tree nodes Console.WriteLine("\nPrint all nodes"); foreach (TreeNodeInfo<TestTreeNode> treeNodeWithLevelInfo in allTreeNodes) { string shiftToRight = new string('\t', treeNodeWithLevelInfo.Level + 1); Console.WriteLine(shiftToRight + treeNodeWithLevelInfo.TheNode.NodeInfo); } // create REvent REvent<TestTreeNode, string> aTestEvent = new REvent<TestTreeNode, string>(); // assign handlers for each of the foreach (TreeNodeInfo<TestTreeNode> treeNodeWithLevelInfo in allTreeNodes) { TestTreeNode currentNode = treeNodeWithLevelInfo.TheNode; aTestEvent.AddHander ( currentNode, (str) => { Console.WriteLine("Target Node: " + currentNode.NodeInfo + "\t\t\tSource Node: " + str); } ); } Console.WriteLine("\nTesting event bubbling:"); aTestEvent.RaiseBubbleEvent(level3Node3, toParentTreeFunction, level3Node3.NodeInfo); Console.WriteLine("\nTesting event tunneling:"); aTestEvent.RaiseTunnelEvent(level3Node3, toParentTreeFunction, level3Node3.NodeInfo); Console.WriteLine("\nTesting event Direct Event (without bubbling and tunneling):"); aTestEvent.RaiseEvent(level3Node3, level3Node3.NodeInfo); Console.WriteLine("\nTesting event propagation to descendents:"); aTestEvent.RaiseEventPropagateToDescendents(level2Node1, toChildrenTreeFunction, level2Node1.NodeInfo); // stopping propagation by returning false from a handler aTestEvent.RemoveAllHandlers(level2Node2); aTestEvent.AddHander ( level2Node2, (eventInfo) => { Console.WriteLine("Terminating event propagation at node " + level2Node2.NodeInfo); eventInfo.IsCanceled = true; }); // terminate event propagation at this node Console.WriteLine("\nTesting event bubbling with event propagation termination:"); aTestEvent.RaiseBubbleEvent(level3Node3, toParentTreeFunction, level3Node3.NodeInfo); Console.WriteLine("\nTesting event tunneling with event propagation termination:"); aTestEvent.RaiseTunnelEvent(level3Node3, toParentTreeFunction, level3Node3.NodeInfo); Console.WriteLine("\nTesting event propagation from the TopNode to its descendents with event propagation termination:"); aTestEvent.RaiseEventPropagateToDescendents(topNode, toChildrenTreeFunction, topNode.NodeInfo); }