A game about forced loneliness, made by TACStudios
1using System;
2
3using UnityEngine;
4
5namespace Unity.PlasticSCM.Editor.UI.Tree
6{
7 internal class EmptyStateData
8 {
9 internal Rect Rect { get { return mLastValidRect; } }
10 internal GUIContent Content { get { return mContent; } }
11
12 internal bool IsEmpty()
13 {
14 return string.IsNullOrEmpty(mContent.text);
15 }
16
17 internal void Update(string contentText, Rect rect, EventType eventType, Action repaint)
18 {
19 UpdateText(contentText);
20
21 UpdateValidRect(rect, eventType, repaint);
22 }
23
24 internal void UpdateText(string contentText)
25 {
26 mContent.text = contentText;
27 }
28
29 internal void UpdateValidRect(Rect rect, EventType eventType, Action repaint)
30 {
31 mLastValidRect = EnsureValidRect(rect, mLastValidRect, eventType, repaint);
32 }
33
34 internal static Rect EnsureValidRect(
35 Rect rect, Rect lastValidRect, EventType eventType, Action repaint)
36 {
37 if (eventType == EventType.Layout)
38 return lastValidRect;
39
40 if (lastValidRect == rect)
41 return lastValidRect;
42
43 // Unity's layout system initially provides a placeholder rectangle during Layout.
44 // A valid rectangle is only provided on following events like Repaint or Mouse events.
45 //
46 // - If we use the placeholder rectangle, the layout system won't position UI elements correctly.
47 // - If we skip layout processing when the rectangle is invalid, we break GUILayout’s Begin/End pairing.
48 //
49 // To prevent both issues, we save the last valid rectangle and use it for drawing.
50
51 repaint();
52
53 return rect;
54 }
55
56 Rect mLastValidRect;
57
58 readonly GUIContent mContent = new GUIContent(string.Empty);
59 }
60
61 internal static class DrawTreeViewEmptyState
62 {
63 internal static void For(EmptyStateData data)
64 {
65 DrawCenteredOnRect(data.Rect, ()=>
66 {
67 GUILayout.Label(data.Content, UnityStyles.Tree.StatusLabel);
68 });
69 }
70
71 internal static void For(Texture2D icon, EmptyStateData data)
72 {
73 DrawCenteredOnRect(data.Rect, () =>
74 {
75 DrawIconAndLabel(icon, data.Content);
76 });
77 }
78
79 internal static void DrawCenteredOnRect(Rect rect, Action onGUI)
80 {
81 GUILayout.BeginArea(rect);
82
83 GUILayout.FlexibleSpace();
84
85 GUILayout.BeginHorizontal();
86 GUILayout.FlexibleSpace();
87
88 onGUI.Invoke();
89
90 GUILayout.FlexibleSpace();
91 GUILayout.EndHorizontal();
92
93 GUILayout.FlexibleSpace();
94
95 GUILayout.EndArea();
96 }
97
98 static void DrawIconAndLabel(Texture2D icon, GUIContent label)
99 {
100 GUILayout.Label(
101 icon,
102 UnityStyles.Tree.StatusLabel,
103 GUILayout.Width(UnityConstants.TREEVIEW_STATUS_ICON_SIZE),
104 GUILayout.Height(UnityConstants.TREEVIEW_STATUS_ICON_SIZE));
105 GUILayout.Space(UnityConstants.TREEVIEW_STATUS_CONTENT_PADDING);
106 GUILayout.Label(label, UnityStyles.Tree.StatusLabel);
107 }
108 }
109}