A game about forced loneliness, made by TACStudios
at master 8.2 kB view raw
1using UnityEditor; 2using UnityEditor.IMGUI.Controls; 3using UnityEngine; 4 5namespace Unity.PlasticSCM.Editor.UI.Tree 6{ 7 internal static class DrawTreeViewItem 8 { 9 internal static void InitializeStyles() 10 { 11 if (EditorStyles.label == null) 12 return; 13 14 TreeView.DefaultStyles.label = UnityStyles.Tree.Label; 15 TreeView.DefaultStyles.boldLabel = UnityStyles.Tree.BoldLabel; 16 } 17 18 internal static void ForCategoryItem( 19 Rect rowRect, 20 int depth, 21 string label, 22 string infoLabel, 23 bool isSelected, 24 bool isFocused) 25 { 26 float indent = GetIndent(depth); 27 28 rowRect.x += indent; 29 rowRect.width -= indent; 30 31 //add a little indentation 32 rowRect.x += 5; 33 rowRect.width -= 5; 34 35 TreeView.DefaultGUI.Label(rowRect, label, isSelected, isFocused); 36 37 if (!string.IsNullOrEmpty(infoLabel)) 38 DrawInfolabel(rowRect, label, infoLabel); 39 } 40 41 internal static bool ForCheckableCategoryItem( 42 Rect rowRect, 43 float rowHeight, 44 int depth, 45 string label, 46 string infoLabel, 47 bool isSelected, 48 bool isFocused, 49 bool wasChecked, 50 bool hadCheckedChildren, 51 bool hadPartiallyCheckedChildren) 52 { 53 float indent = GetIndent(depth); 54 55 rowRect.x += indent; 56 rowRect.width -= indent; 57 58 Rect checkRect = GetCheckboxRect(rowRect, rowHeight); 59 60 if (!wasChecked && (hadCheckedChildren || hadPartiallyCheckedChildren)) 61 EditorGUI.showMixedValue = true; 62 63 bool isChecked = EditorGUI.Toggle(checkRect, wasChecked); 64 EditorGUI.showMixedValue = false; 65 66 rowRect.x = checkRect.xMax - 4; 67 rowRect.width -= checkRect.width; 68 69 //add a little indentation 70 rowRect.x += 5; 71 rowRect.width -= 5; 72 73 TreeView.DefaultGUI.Label(rowRect, label, isSelected, isFocused); 74 75 if (!string.IsNullOrEmpty(infoLabel)) 76 DrawInfolabel(rowRect, label, infoLabel); 77 78 return isChecked; 79 } 80 81 internal static void ForItemCell( 82 Rect rect, 83 float rowHeight, 84 int depth, 85 Texture icon, 86 Texture overlayIcon, 87 string label, 88 bool isSelected, 89 bool isFocused, 90 bool isBoldText, 91 bool isSecondaryLabel) 92 { 93 float indent = GetIndent(depth); 94 95 rect.x += indent; 96 rect.width -= indent; 97 98 rect = DrawIconLeft( 99 rect, rowHeight, icon, overlayIcon); 100 101 if (isSecondaryLabel) 102 { 103 ForSecondaryLabel(rect, label, isSelected, isFocused, isBoldText); 104 return; 105 } 106 107 ForLabel(rect, label, isSelected, isFocused, isBoldText); 108 } 109 110 internal static bool ForCheckableItemCell( 111 Rect rect, 112 float rowHeight, 113 int depth, 114 Texture icon, 115 Texture overlayIcon, 116 string label, 117 bool isSelected, 118 bool isFocused, 119 bool isHighlighted, 120 bool wasChecked) 121 { 122 float indent = GetIndent(depth); 123 124 rect.x += indent; 125 rect.width -= indent; 126 127 Rect checkRect = GetCheckboxRect(rect, rowHeight); 128 129 bool isChecked = EditorGUI.Toggle(checkRect, wasChecked); 130 131 rect.x = checkRect.xMax; 132 rect.width -= checkRect.width; 133 134 rect = DrawIconLeft( 135 rect, rowHeight, icon, overlayIcon); 136 137 if (isHighlighted) 138 TreeView.DefaultGUI.BoldLabel(rect, label, isSelected, isFocused); 139 else 140 TreeView.DefaultGUI.Label(rect, label, isSelected, isFocused); 141 142 return isChecked; 143 } 144 145 internal static Rect DrawIconLeft( 146 Rect rect, 147 float rowHeight, 148 Texture icon, 149 Texture overlayIcon) 150 { 151 if (icon == null) 152 return rect; 153 154 float iconWidth = rowHeight * ((float)icon.width / icon.height); 155 156 Rect iconRect = new Rect(rect.x, rect.y, iconWidth, rowHeight); 157 158 EditorGUI.LabelField(iconRect, new GUIContent(icon), UnityStyles.Tree.IconStyle); 159 160 if (overlayIcon != null) 161 { 162 Rect overlayIconRect = OverlayRect.GetOverlayRect( 163 iconRect, 164 OVERLAY_ICON_OFFSET); 165 166 GUI.DrawTexture( 167 overlayIconRect, overlayIcon, 168 ScaleMode.ScaleToFit); 169 } 170 171 rect.x += iconRect.width; 172 rect.width -= iconRect.width; 173 174 return rect; 175 } 176 177 static void DrawInfolabel( 178 Rect rect, 179 string label, 180 string infoLabel) 181 { 182 Vector2 labelSize = ((GUIStyle)UnityStyles.Tree.Label) 183 .CalcSize(new GUIContent(label)); 184 185 rect.x += labelSize.x; 186 187 GUI.Label(rect, infoLabel, UnityStyles.Tree.InfoLabel); 188 } 189 190 static Rect GetCheckboxRect(Rect rect, float rowHeight) 191 { 192 return new Rect( 193 rect.x, 194 rect.y + UnityConstants.TREEVIEW_CHECKBOX_Y_OFFSET, 195 UnityConstants.TREEVIEW_CHECKBOX_SIZE, 196 rect.height); 197 } 198 199 static float GetIndent(int depth) 200 { 201 if (depth == -1) 202 return 0; 203 204 return 16 + (depth * 16); 205 } 206 207 internal static void ForSecondaryLabelRightAligned( 208 Rect rect, 209 string label, 210 bool isSelected, 211 bool isFocused, 212 bool isBoldText) 213 { 214 if (Event.current.type != EventType.Repaint) 215 return; 216 217 if (isBoldText) 218 { 219 GUIStyle secondaryBoldRightAligned = 220 UnityStyles.Tree.SecondaryLabelBoldRightAligned; 221 secondaryBoldRightAligned.Draw( 222 rect, label, false, true, isSelected, isFocused); 223 return; 224 } 225 226 GUIStyle secondaryLabelRightAligned = 227 UnityStyles.Tree.SecondaryLabelRightAligned; 228 secondaryLabelRightAligned.Draw( 229 rect, label, false, true, isSelected, isFocused); 230 } 231 232 internal static void ForSecondaryLabel( 233 Rect rect, 234 string label, 235 bool isSelected, 236 bool isFocused, 237 bool isBoldText) 238 { 239 if (Event.current.type != EventType.Repaint) 240 return; 241 242 if (isBoldText) 243 { 244 GUIStyle secondaryBoldLabel = 245 UnityStyles.Tree.SecondaryBoldLabel; 246 247 secondaryBoldLabel.normal.textColor = Color.red; 248 249 secondaryBoldLabel.Draw( 250 rect, label, false, true, isSelected, isFocused); 251 return; 252 } 253 254 GUIStyle secondaryLabel = 255 UnityStyles.Tree.SecondaryLabel; 256 257 secondaryLabel.Draw( 258 rect, label, false, true, isSelected, isFocused); 259 } 260 261 internal static void ForLabel( 262 Rect rect, 263 string label, 264 bool isSelected, 265 bool isFocused, 266 bool isBoldText) 267 { 268 if (Event.current.type != EventType.Repaint) 269 return; 270 271 if (isBoldText) 272 { 273 GUIStyle boldLabel = UnityStyles.Tree.BoldLabel; 274 boldLabel.Draw( 275 rect, label, false, true, isSelected, isFocused); 276 return; 277 } 278 279 GUIStyle normalLabel = UnityStyles.Tree.Label; 280 normalLabel.Draw( 281 rect, label, false, true, isSelected, isFocused); 282 } 283 284 const float OVERLAY_ICON_OFFSET = 16f; 285 } 286}