A game about forced loneliness, made by TACStudios
at master 127 lines 4.0 kB view raw
1using System; 2using UnityEngine; 3 4namespace UnityEditor.U2D.Animation 5{ 6 internal class Brush 7 { 8 private static readonly float kWheelSizeSpeed = 1f; 9 private static readonly int kBrushHashCode = "Brush".GetHashCode(); 10 private IGUIWrapper m_GUIWrapper; 11 private float m_DeltaAcc = 0f; 12 private int m_ControlID = -1; 13 private SliderData m_SliderData = SliderData.zero; 14 15 public event Action<Brush> onMove = (b) => {}; 16 public event Action<Brush> onSize = (b) => {}; 17 public event Action<Brush> onRepaint = (b) => {}; 18 public event Action<Brush> onStrokeBegin = (b) => {}; 19 public event Action<Brush> onStrokeDelta = (b) => {}; 20 public event Action<Brush> onStrokeStep = (b) => {}; 21 public event Action<Brush> onStrokeEnd = (b) => {}; 22 23 public bool isHot 24 { 25 get { return m_GUIWrapper.IsControlHot(m_ControlID); } 26 } 27 public bool isActivable 28 { 29 get { return m_GUIWrapper.IsControlHot(0) && m_GUIWrapper.IsControlNearest(m_ControlID); } 30 } 31 32 public int controlID 33 { 34 get { return m_ControlID; } 35 } 36 37 public float hardness { get; set; } 38 public float step { get; set; } 39 public float size { get; set; } 40 public Vector3 position 41 { 42 get { return m_SliderData.position; } 43 } 44 45 public Brush(IGUIWrapper guiWrapper) 46 { 47 m_GUIWrapper = guiWrapper; 48 size = 25f; 49 step = 20f; 50 } 51 52 public void OnGUI() 53 { 54 m_ControlID = m_GUIWrapper.GetControlID(kBrushHashCode, FocusType.Passive); 55 56 var eventType = m_GUIWrapper.eventType; 57 58 if (!m_GUIWrapper.isAltDown) 59 m_GUIWrapper.LayoutControl(controlID, 0f); 60 61 if (isActivable) 62 { 63 m_SliderData.position = m_GUIWrapper.GUIToWorld(m_GUIWrapper.mousePosition); 64 65 if (m_GUIWrapper.IsMouseDown(0)) 66 { 67 m_DeltaAcc = 0f; 68 onStrokeBegin(this); 69 onStrokeStep(this); 70 m_GUIWrapper.SetGuiChanged(true); 71 } 72 73 if (eventType == EventType.MouseMove) 74 { 75 onMove(this); 76 m_GUIWrapper.UseCurrentEvent(); 77 } 78 79 if (m_GUIWrapper.isShiftDown && eventType == EventType.ScrollWheel) 80 { 81 var sizeDelta = HandleUtility.niceMouseDeltaZoom * kWheelSizeSpeed; 82 size = Mathf.Max(1f, size + sizeDelta); 83 onSize(this); 84 m_GUIWrapper.UseCurrentEvent(); 85 } 86 } 87 88 if (isHot && m_GUIWrapper.IsMouseUp(0)) 89 onStrokeEnd(this); 90 91 if (m_GUIWrapper.IsRepainting() && (isHot || isActivable)) 92 onRepaint(this); 93 94 Vector3 position; 95 if (m_GUIWrapper.DoSlider(m_ControlID, m_SliderData, out position)) 96 { 97 step = Mathf.Max(step, 1f); 98 99 var delta = position - m_SliderData.position; 100 var direction = delta.normalized; 101 var magnitude = delta.magnitude; 102 103 m_SliderData.position -= direction * m_DeltaAcc; 104 105 m_DeltaAcc += magnitude; 106 107 if (m_DeltaAcc >= step) 108 { 109 var stepVector = direction * step; 110 111 while (m_DeltaAcc >= step) 112 { 113 m_SliderData.position += stepVector; 114 115 onMove(this); 116 onStrokeStep(this); 117 118 m_DeltaAcc -= step; 119 } 120 } 121 122 m_SliderData.position = position; 123 onStrokeDelta(this); 124 } 125 } 126 } 127}