A game about forced loneliness, made by TACStudios
1using System;
2using UnityEngine;
3
4namespace UnityEditor.U2D.Animation
5{
6 internal class RectSelectionTool<T>
7 {
8 private int m_HashCode = "RectSelectionTool".GetHashCode();
9 private int m_ControlID = -1;
10 private bool m_Moved = false;
11 private RectSlider m_RectSlider = new RectSlider();
12
13 public int controlID => m_ControlID;
14
15 public IRectSelector<T> rectSelector { get; set; }
16 public ICacheUndo cacheUndo { get; set; }
17 public Action onSelectionStart = () => { };
18 public Action onSelectionUpdate = () => { };
19 public Action onSelectionEnd = () => { };
20
21 public void OnGUI()
22 {
23 Debug.Assert(rectSelector != null);
24 Debug.Assert(cacheUndo != null);
25
26 m_ControlID = GUIUtility.GetControlID(m_HashCode, FocusType.Passive);
27
28 Event ev = Event.current;
29 EventType eventType = ev.GetTypeForControl(m_ControlID);
30
31 if (GUIUtility.hotControl == 0 && HandleUtility.nearestControl == m_ControlID &&
32 rectSelector.selection.Count > 0 && eventType == EventType.MouseDown && ev.button == 0 && !ev.alt)
33 {
34 m_Moved = false;
35 onSelectionStart();
36 }
37
38 if (m_Moved && GUIUtility.hotControl == m_ControlID && eventType == EventType.MouseUp && ev.button == 0)
39 {
40 cacheUndo.BeginUndoOperation(TextContent.selection);
41 rectSelector.selection.EndSelection(true);
42 onSelectionEnd();
43 }
44
45 EditorGUI.BeginChangeCheck();
46
47 rectSelector.rect = m_RectSlider.Do(m_ControlID);
48
49 if (EditorGUI.EndChangeCheck())
50 {
51 if (!m_Moved)
52 {
53 cacheUndo.BeginUndoOperation(TextContent.selection);
54
55 if (!ev.shift)
56 rectSelector.selection.Clear();
57
58 m_Moved = true;
59 }
60
61 rectSelector.selection.BeginSelection();
62 rectSelector.Select();
63 onSelectionUpdate();
64 }
65
66 if (eventType == EventType.Repaint && GUIUtility.hotControl == m_ControlID)
67 {
68 DrawingUtility.DrawRect(rectSelector.rect, Vector3.zero, Quaternion.identity, new Color(0f, 1f, 1f, 1f), 0.05f, 0.8f);
69 }
70 }
71 }
72}