A game about forced loneliness, made by TACStudios
1using System;
2using UnityEngine;
3using UnityEditor.Experimental.GraphView;
4using UnityEngine.UIElements;
5
6namespace UnityEditor.ShaderGraph.Drawing
7{
8 class PortInputView : GraphElement, IDisposable
9 {
10 readonly CustomStyleProperty<Color> k_EdgeColorProperty = new CustomStyleProperty<Color>("--edge-color");
11
12 Color m_EdgeColor = Color.red;
13
14 public Color edgeColor
15 {
16 get { return m_EdgeColor; }
17 }
18
19 public MaterialSlot slot
20 {
21 get { return m_Slot; }
22 }
23
24 MaterialSlot m_Slot;
25 ConcreteSlotValueType m_SlotType;
26 VisualElement m_Control;
27 VisualElement m_Container;
28 EdgeControl m_EdgeControl;
29
30 public PortInputView(MaterialSlot slot)
31 {
32 styleSheets.Add(Resources.Load<StyleSheet>("Styles/PortInputView"));
33 pickingMode = PickingMode.Ignore;
34 ClearClassList();
35 m_Slot = slot;
36 m_SlotType = slot.concreteValueType;
37 AddToClassList("type" + m_SlotType);
38
39 m_EdgeControl = new EdgeControl
40 {
41 @from = new Vector2(232f - 21f, 11.5f),
42 to = new Vector2(232f, 11.5f),
43 edgeWidth = 2,
44 pickingMode = PickingMode.Ignore
45 };
46 Add(m_EdgeControl);
47
48 m_Container = new VisualElement { name = "container" };
49 {
50 CreateControl();
51
52 var slotElement = new VisualElement { name = "slot" };
53 {
54 slotElement.Add(new VisualElement { name = "dot" });
55 }
56 m_Container.Add(slotElement);
57 }
58 Add(m_Container);
59
60 m_Container.Add(new VisualElement() { name = "disabledOverlay", pickingMode = PickingMode.Ignore });
61 RegisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
62 }
63
64 void OnCustomStyleResolved(CustomStyleResolvedEvent e)
65 {
66 Color colorValue;
67
68 if (e.customStyle.TryGetValue(k_EdgeColorProperty, out colorValue))
69 m_EdgeColor = colorValue;
70
71 m_EdgeControl.UpdateLayout();
72 m_EdgeControl.inputColor = edgeColor;
73 m_EdgeControl.outputColor = edgeColor;
74 }
75
76 public void UpdateSlot(MaterialSlot newSlot)
77 {
78 m_Slot = newSlot;
79 Recreate();
80 }
81
82 public void UpdateSlotType()
83 {
84 if (slot.concreteValueType != m_SlotType)
85 Recreate();
86 }
87
88 void Recreate()
89 {
90 RemoveFromClassList("type" + m_SlotType);
91 m_SlotType = slot.concreteValueType;
92 AddToClassList("type" + m_SlotType);
93 if (m_Control != null)
94 {
95 if (m_Control is IDisposable disposable)
96 disposable.Dispose();
97 m_Container.Remove(m_Control);
98 }
99 CreateControl();
100 }
101
102 void CreateControl()
103 {
104 // Specially designated properties (Use Custom Binding) are shown as a label on the slot when the slot is disconnected, with no ability to set an explicit default.
105 // If the port for this property is connected to, it will use the regular slot control.
106 m_Control = (!slot.isConnected && slot.IsConnectionTestable()) ? slot.InstantiateCustomControl() : slot.InstantiateControl();
107 if (m_Control != null)
108 {
109 m_Container.Insert(0, m_Control);
110 }
111 else
112 {
113 // Some slot types don't support an input control, so hide this
114 m_Container.visible = m_EdgeControl.visible = false;
115 }
116 }
117
118 public void Dispose()
119 {
120 if (m_Control is IDisposable disposable)
121 disposable.Dispose();
122
123 styleSheets.Clear();
124 m_Control = null;
125 m_EdgeControl = null;
126 UnregisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
127 }
128 }
129}