aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Engine/Misc/EventList.cs
blob: e5202e7237ea17f0420d5d1b1b14e23636df635c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Linq;

namespace ShiftOS.Engine.Misc
{
	[Serializable]
	public class EventList<T> : List<T>
	{
		public event EventHandler<EventListArgs<T>> ItemAdded;
		public event EventHandler<EventListArgs<T>> ItemRemoved;

		public new void Add(T obj)
		{
			base.Add(obj);
			ItemAdded?.Invoke(this, new EventListArgs<T>(obj));
		}

		public new void AddRange(IEnumerable<T> objs)
		{
			foreach (var obj in objs)
			{
				base.Add(obj);
				ItemAdded?.Invoke(this, new EventListArgs<T>(obj));
			}
		}

		public new bool Remove(T obj)
		{
			var b = base.Remove(obj);

			ItemRemoved?.Invoke(this, new EventListArgs<T>(obj));
			return b;
		}

		public new void RemoveAt(int index)
		{
			base.RemoveAt(index);
			ItemRemoved?.Invoke(this, new EventListArgs<T>(default));
		}

		public new void RemoveAll(Predicate<T> match)
		{
			//will this work
			foreach (var item in this.Where(match as Func<T, bool> ?? throw new InvalidOperationException()))
			{
				Remove(item);
			}
		}

		public new void RemoveRange(int start, int end)
		{
			for (var i = start; i <= end; i++)
			{
				Remove(this[i]);
			}
		}

		public new void Clear()
		{
			RemoveAll(x => true);
		}
	}

	public class EventListArgs<T> : EventArgs
	{
		public EventListArgs(T item) => Item = item;

		public T Item { get; }
	}
}