示例#1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Foo foo = new Foo {
                name = "fooooo"
            };

            foo.addChild(new Foo {
                name = "foo-1"
            });
            foo.addChild(new Foo {
                name = "foo-2"
            });
            foo.children[1].addChild(new Foo {
                name = "foo-2-a"
            });
            foo.children[1].addChild(new Foo {
                name = "foo-2-b"
            });

            Foo fooChild = foo.children[1].children[0];



            ConsoleTitle("basic test");

            Kit.Event.On <MySpecialEvent>(foo, "*",
                                          e => Console.WriteLine($"top({foo.name}): " + e.ToLongString()), key: "basic test");

            Kit.Event.On <MySpecialEvent>(fooChild, "*",
                                          e => Console.WriteLine($"bottom({fooChild.name}): " + e.ToLongString()), key: "basic test");

            Kit.Event.Dispatch(new MySpecialEvent {
                Target = foo, type = "hey"
            });

            Kit.Event.Dispatch(new MySpecialEvent
            {
                Target      = fooChild,
                type        = "ascending I",
                propagation = obj => (obj as Foo).parent,
                number      = MathF.E,
            });

            Kit.Event.Dispatch(new MySpecialEvent
            {
                Target      = foo,
                type        = "descending I",
                propagation = obj => (obj as Foo).children,
                number      = MathF.PI,
            });



            ConsoleTitle("Cancel() test");

            Kit.Event.On <MySpecialEvent>(foo.children[1], "*", e => {
                if (e.Cancel())
                {
                    Console.WriteLine($"event \"{e.type}\" canceled!!!!");
                }
                else
                {
                    Console.WriteLine($"event \"{e.type}\" NOOOOT canceled!");
                }
            });

            Kit.Event.Dispatch(new MySpecialEvent
            {
                Target      = fooChild,
                type        = "ascending II",
                propagation = obj => (obj as Foo).parent,
            });

            Kit.Event.Dispatch(new MySpecialEvent
            {
                Target      = foo,
                type        = "descending II",
                propagation = obj => (obj as Foo).children,
            });



            ConsoleTitle("Cancelable (= false) test");

            Kit.Event.Dispatch(new MySpecialEvent
            {
                Target      = fooChild,
                type        = "ascending III (uncancelable)",
                Cancelable  = false,
                propagation = obj => (obj as Foo).parent,
            });


            // off
            Kit.Event.Off(foo);
            Kit.Event.Off(fooChild);



            ConsoleTitle("global test");

            Kit.Event.On <MySpecialEvent>(e => {
                Console.WriteLine($"global listener @\"{e.type}\" ({e.number})");
            }, key: "yolo");
            Kit.Event.Dispatch(new MySpecialEvent {
                number = MathF.PI * 2
            });
            Kit.Event.Dispatch(new MySpecialEvent {
                number = MathF.PI * 3
            });
            Kit.Event.Off(key: "yolo");
            Kit.Event.Dispatch(new MySpecialEvent {
                number = MathF.PI * 4
            });



            ConsoleTitle("AlsoGlobal test");

            Kit.Event.On <MySpecialEvent>(e => {
                Console.WriteLine($"also global listener {e.ToLongString("type|Target|OriginTarget|number")}");
                e.number++;
            }, key: "yolo");

            Kit.Event.Dispatch(new MySpecialEvent
            {
                type        = "Ascending",
                Target      = fooChild,
                AlsoGlobal  = true,
                number      = 0,
                propagation = obj => (obj as Foo).parent,
                Cancelable  = false,
            });

            Console.WriteLine();

            Kit.Event.Dispatch(new MySpecialEvent
            {
                type        = "Descending",
                Target      = foo,
                AlsoGlobal  = true,
                number      = 0,
                propagation = obj => (obj as Foo).children,
                Cancelable  = false,
            });

            Kit.Event.Off(key: "yolo");



            ConsoleTitle("cast test");

            Kit.Event.On("*", e => Console.WriteLine($"e: {e}"));
            Kit.Event.On <MySpecialEvent>("*", e => Console.WriteLine($"(MySpecialEvent)e: {e}"));
            Kit.Event.Dispatch(new Kit.Event {
                type = "basic event"
            });
            Kit.Event.Dispatch(new MySpecialEvent {
                type = "basic event"
            });
        }
示例#2
0
 public void addChild(Foo child)
 {
     child.parent = this;
     children.Add(child);
 }