A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using NUnit.Framework;
6using UnityEngine.EventSystems;
7
8namespace UnityEngine.UI.Tests
9{
10 public static class UIBehaviourExtensions
11 {
12 private static object InvokeMethodAndRethrow(Type type, Object obj, string methodName, params object[] args)
13 {
14 BindingFlags flags = BindingFlags.Default;
15 flags |= BindingFlags.Public;
16 flags |= BindingFlags.NonPublic;
17 if (obj != null)
18 flags |= BindingFlags.Instance;
19 else
20 flags |= BindingFlags.Static;
21 MethodInfo method;
22
23 try
24 {
25 // Attempt to get the method plainly at first
26 method = type.GetMethod(methodName, flags);
27 }
28 catch (AmbiguousMatchException)
29 {
30 // If it's ambiguous, then attempt to get it using its params (though nulls would mess things up).
31 method = type.GetMethod(methodName, flags, null, args.Select(a => a != null ? a.GetType() : null).Where(a => a != null).ToArray(), new ParameterModifier[0]);
32 }
33 Assert.NotNull(method, string.Format("Not method {0} found on object {1}", methodName, obj));
34 return method.Invoke(obj, args);
35 }
36
37 public static object InvokeMethodAndRethrow<T>(Object obj, string methodName, params object[] args)
38 {
39 return InvokeMethodAndRethrow(typeof(T), obj, methodName, args);
40 }
41
42 public static object InvokeMethodAndRethrow(Object obj, string methodName, params object[] args)
43 {
44 return InvokeMethodAndRethrow(obj.GetType(), obj, methodName, args);
45 }
46
47 public static void InvokeOnEnable(this UIBehaviour behaviour)
48 {
49 InvokeMethodAndRethrow(behaviour, "OnEnable");
50 }
51
52 public static void InvokeOnDisable(this UIBehaviour behaviour)
53 {
54 InvokeMethodAndRethrow(behaviour, "OnDisable");
55 }
56
57 public static void InvokeAwake(this UIBehaviour behaviour)
58 {
59 InvokeMethodAndRethrow(behaviour, "Awake");
60 }
61
62 public static void InvokeRebuild(this UIBehaviour behaviour, CanvasUpdate type)
63 {
64 InvokeMethodAndRethrow(behaviour, "Rebuild", type);
65 }
66
67 public static void InvokeLateUpdate(this UIBehaviour behaviour)
68 {
69 InvokeMethodAndRethrow(behaviour, "LateUpdate");
70 }
71
72 public static void InvokeUpdate(this UIBehaviour behaviour)
73 {
74 InvokeMethodAndRethrow(behaviour, "Update");
75 }
76
77 public static void InvokeOnRectTransformDimensionsChange(this UIBehaviour behaviour)
78 {
79 InvokeMethodAndRethrow(behaviour, "OnRectTransformDimensionsChange");
80 }
81
82 public static void InvokeOnCanvasGroupChanged(this UIBehaviour behaviour)
83 {
84 InvokeMethodAndRethrow(behaviour, "OnCanvasGroupChanged");
85 }
86
87 public static void InvokeOnDidApplyAnimationProperties(this UIBehaviour behaviour)
88 {
89 InvokeMethodAndRethrow(behaviour, "OnDidApplyAnimationProperties");
90 }
91 }
92
93 public static class SelectableExtensions
94 {
95 public static void InvokeOnPointerDown(this Selectable selectable, PointerEventData data)
96 {
97 UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnPointerDown", data);
98 }
99
100 public static void InvokeOnPointerUp(this Selectable selectable, PointerEventData data)
101 {
102 UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnPointerUp", data);
103 }
104
105 public static void InvokeOnPointerEnter(this Selectable selectable, PointerEventData data)
106 {
107 UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnPointerEnter", data);
108 }
109
110 public static void InvokeOnPointerExit(this Selectable selectable, PointerEventData data)
111 {
112 UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnPointerExit", data);
113 }
114
115 public static void InvokeTriggerAnimation(this Selectable selectable, string triggerName)
116 {
117 UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "TriggerAnimation", triggerName);
118 }
119
120 public static void InvokeOnSelect(this Selectable selectable, string triggerName)
121 {
122 UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnSelect", triggerName);
123 }
124 }
125
126 public static class GraphicExtension
127 {
128 public static void InvokeOnPopulateMesh(this Graphic graphic, VertexHelper vh)
129 {
130 UIBehaviourExtensions.InvokeMethodAndRethrow(graphic, "OnPopulateMesh", vh);
131 }
132 }
133
134 public static class GraphicRaycasterExtension
135 {
136 public static void InvokeRaycast(Canvas canvas, Camera eventCamera, Vector2 pointerPosition, List<Graphic> results)
137 {
138 UIBehaviourExtensions.InvokeMethodAndRethrow<GraphicRaycaster>(null, "Raycast", canvas, eventCamera, pointerPosition, results);
139 }
140 }
141
142 public static class ToggleGroupExtension
143 {
144 public static void InvokeValidateToggleIsInGroup(this ToggleGroup tgroup, Toggle toggle)
145 {
146 UIBehaviourExtensions.InvokeMethodAndRethrow<ToggleGroup>(tgroup, "ValidateToggleIsInGroup", toggle);
147 }
148 }
149}