A game about forced loneliness, made by TACStudios
at master 230 lines 6.8 kB view raw
1using UnityEngine; 2 3namespace UnityEditor.U2D.Animation 4{ 5 internal struct SliderData 6 { 7 public Vector3 position; 8 public Vector3 forward; 9 public Vector3 up; 10 public Vector3 right; 11 12 public static readonly SliderData zero = new SliderData() { position = Vector3.zero, forward = Vector3.forward, up = Vector3.up, right = Vector3.right }; 13 } 14 15 internal interface IGUIWrapper 16 { 17 Vector2 mousePosition { get; } 18 int mouseButton { get; } 19 int clickCount { get; } 20 bool isShiftDown { get; } 21 bool isAltDown { get; } 22 bool isActionKeyDown { get; } 23 EventType eventType { get; } 24 string commandName { get; } 25 bool IsMouseDown(int button); 26 bool IsMouseUp(int button); 27 bool IsKeyDown(KeyCode keyCode); 28 int GetControlID(int hint, FocusType focusType); 29 void LayoutControl(int controlID, float distance); 30 bool IsControlNearest(int controlID); 31 bool IsControlHot(int controlID); 32 bool IsMultiStepControlHot(int controlID); 33 void SetControlHot(int controlID); 34 void SetMultiStepControlHot(int controlID); 35 bool DoSlider(int id, SliderData sliderData, out Vector3 newPosition); 36 void UseCurrentEvent(); 37 float DistanceToSegment(Vector3 p1, Vector3 p2); 38 float DistanceToSegmentClamp(Vector3 p1, Vector3 p2); 39 float DistanceToCircle(Vector3 center, float radius); 40 Vector3 GUIToWorld(Vector2 guiPosition); 41 Vector3 GUIToWorld(Vector2 guiPosition, Vector3 planeNormal, Vector3 planePosition); 42 void Repaint(); 43 bool IsRepainting(); 44 bool IsEventOutsideWindow(); 45 void SetGuiChanged(bool changed); 46 float GetHandleSize(Vector3 position); 47 bool IsViewToolActive(); 48 bool HasCurrentCamera(); 49 } 50 51 internal class GUIWrapper : IGUIWrapper 52 { 53 private Handles.CapFunction nullCap = (int c, Vector3 p , Quaternion r, float s, EventType ev) => {}; 54 private int m_MultiStepHotControl = 0; 55 56 public Vector2 mousePosition 57 { 58 get { return Event.current.mousePosition; } 59 } 60 61 public int mouseButton 62 { 63 get { return Event.current.button; } 64 } 65 66 public int clickCount 67 { 68 get { return Event.current.clickCount; } 69 } 70 71 public bool isShiftDown 72 { 73 get { return Event.current.shift; } 74 } 75 76 public bool isAltDown 77 { 78 get { return Event.current.alt; } 79 } 80 81 public bool isActionKeyDown 82 { 83 get { return EditorGUI.actionKey; } 84 } 85 86 public EventType eventType 87 { 88 get { return Event.current.type; } 89 } 90 91 public string commandName 92 { 93 get { return Event.current.commandName; } 94 } 95 96 public bool IsMouseDown(int button) 97 { 98 return Event.current.type == EventType.MouseDown && Event.current.button == button; 99 } 100 101 public bool IsMouseUp(int button) 102 { 103 return Event.current.type == EventType.MouseUp && Event.current.button == button; 104 } 105 106 public bool IsKeyDown(KeyCode keyCode) 107 { 108 return Event.current.type == EventType.KeyDown && Event.current.keyCode == keyCode; 109 } 110 111 public int GetControlID(int hint, FocusType focusType) 112 { 113 return GUIUtility.GetControlID(hint, focusType); 114 } 115 116 public void LayoutControl(int controlID, float distance) 117 { 118 if (Event.current.type == EventType.Layout) 119 HandleUtility.AddControl(controlID, distance); 120 } 121 122 public bool IsControlNearest(int controlID) 123 { 124 return HandleUtility.nearestControl == controlID; 125 } 126 127 public bool IsControlHot(int controlID) 128 { 129 return GUIUtility.hotControl == controlID; 130 } 131 132 public bool IsMultiStepControlHot(int controlID) 133 { 134 return m_MultiStepHotControl == controlID; 135 } 136 137 public void SetControlHot(int controlID) 138 { 139 GUIUtility.hotControl = controlID; 140 } 141 142 public void SetMultiStepControlHot(int controlID) 143 { 144 m_MultiStepHotControl = controlID; 145 } 146 147 public bool DoSlider(int id, SliderData sliderData, out Vector3 newPosition) 148 { 149 EditorGUI.BeginChangeCheck(); 150 151 if (HasCurrentCamera()) 152 newPosition = Handles.Slider2D(id, sliderData.position, sliderData.forward, sliderData.right, sliderData.up, 1f, nullCap, Vector2.zero); 153 else 154 newPosition = Slider2D.Do(id, sliderData.position, null); 155 156 return EditorGUI.EndChangeCheck(); 157 } 158 159 public void UseCurrentEvent() 160 { 161 Event.current.Use(); 162 } 163 164 public float DistanceToSegment(Vector3 p1, Vector3 p2) 165 { 166 p1 = HandleUtility.WorldToGUIPoint(p1); 167 p2 = HandleUtility.WorldToGUIPoint(p2); 168 169 return HandleUtility.DistancePointToLineSegment(mousePosition, p1, p2); 170 } 171 172 public float DistanceToSegmentClamp(Vector3 p1, Vector3 p2) 173 { 174 p1 = HandleUtility.WorldToGUIPoint(p1); 175 p2 = HandleUtility.WorldToGUIPoint(p2); 176 177 return MathUtility.DistanceToSegmentClamp(mousePosition, p1, p2); 178 } 179 180 public float DistanceToCircle(Vector3 center, float radius) 181 { 182 return HandleUtility.DistanceToCircle(center, radius); 183 } 184 185 public Vector3 GUIToWorld(Vector2 guiPosition) 186 { 187 return ModuleUtility.GUIToWorld(guiPosition); 188 } 189 190 public Vector3 GUIToWorld(Vector2 guiPosition, Vector3 planeNormal, Vector3 planePosition) 191 { 192 return ModuleUtility.GUIToWorld(guiPosition, planeNormal, planePosition); 193 } 194 195 public void Repaint() 196 { 197 HandleUtility.Repaint(); 198 } 199 200 public bool IsRepainting() 201 { 202 return eventType == EventType.Repaint; 203 } 204 205 public void SetGuiChanged(bool changed) 206 { 207 GUI.changed = true; 208 } 209 210 public bool IsEventOutsideWindow() 211 { 212 return Event.current.type == EventType.Ignore; 213 } 214 215 public float GetHandleSize(Vector3 position) 216 { 217 return HandleUtility.GetHandleSize(position); 218 } 219 220 public bool IsViewToolActive() 221 { 222 return UnityEditor.Tools.current == Tool.View || isAltDown || mouseButton == 1 || mouseButton == 2; 223 } 224 225 public bool HasCurrentCamera() 226 { 227 return Camera.current != null; 228 } 229 } 230}