DrawTreeViewItem.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using UnityEditor;
  2. using UnityEditor.IMGUI.Controls;
  3. using UnityEngine;
  4. namespace Unity.PlasticSCM.Editor.UI.Tree
  5. {
  6. internal static class DrawTreeViewItem
  7. {
  8. internal static void InitializeStyles()
  9. {
  10. if (EditorStyles.label == null)
  11. return;
  12. TreeView.DefaultStyles.label = UnityStyles.Tree.Label;
  13. TreeView.DefaultStyles.boldLabel = UnityStyles.Tree.BoldLabel;
  14. }
  15. internal static void ForCategoryItem(
  16. Rect rowRect,
  17. float rowHeight,
  18. int depth,
  19. Texture icon,
  20. string label,
  21. bool isSelected,
  22. bool isFocused)
  23. {
  24. float indent = GetIndent(depth);
  25. rowRect.x += indent;
  26. rowRect.width -= indent;
  27. rowRect = DrawIconLeft(rowRect, rowHeight, icon, null);
  28. TreeView.DefaultGUI.Label(rowRect, label, isSelected, isFocused);
  29. }
  30. internal static bool ForCheckableCategoryItem(
  31. Rect rowRect,
  32. float rowHeight,
  33. int depth,
  34. Texture icon,
  35. string label,
  36. bool isSelected,
  37. bool isFocused,
  38. bool wasChecked,
  39. bool hadCheckedChildren)
  40. {
  41. float indent = GetIndent(depth);
  42. rowRect.x += indent;
  43. rowRect.width -= indent;
  44. Rect checkRect = GetCheckboxRect(rowRect, rowHeight);
  45. if (!wasChecked && hadCheckedChildren)
  46. EditorGUI.showMixedValue = true;
  47. bool isChecked = EditorGUI.Toggle(checkRect, wasChecked);
  48. EditorGUI.showMixedValue = false;
  49. rowRect.x = checkRect.xMax - 4;
  50. rowRect.width -= checkRect.width;
  51. rowRect = DrawIconLeft(rowRect, rowHeight, icon, null);
  52. TreeView.DefaultGUI.Label(rowRect, label, isSelected, isFocused);
  53. return isChecked;
  54. }
  55. internal static void ForItemCell(
  56. Rect rect,
  57. float rowHeight,
  58. int depth,
  59. Texture icon,
  60. GetChangesOverlayIcon.Data overlayIconData,
  61. string label,
  62. bool isSelected,
  63. bool isFocused,
  64. bool isBoldText,
  65. bool isSecondaryLabel)
  66. {
  67. float indent = GetIndent(depth);
  68. rect.x += indent;
  69. rect.width -= indent;
  70. rect = DrawIconLeft(
  71. rect, rowHeight, icon, overlayIconData);
  72. if (isSecondaryLabel)
  73. {
  74. ForSecondaryLabel(rect, label, isSelected, isFocused, isBoldText);
  75. return;
  76. }
  77. ForLabel(rect, label, isSelected, isFocused, isBoldText);
  78. }
  79. internal static bool ForCheckableItemCell(
  80. Rect rect,
  81. float rowHeight,
  82. int depth,
  83. Texture icon,
  84. GetChangesOverlayIcon.Data overlayIconData,
  85. string label,
  86. bool isSelected,
  87. bool isFocused,
  88. bool isHighlighted,
  89. bool wasChecked)
  90. {
  91. float indent = GetIndent(depth);
  92. rect.x += indent;
  93. rect.width -= indent;
  94. Rect checkRect = GetCheckboxRect(rect, rowHeight);
  95. bool isChecked = EditorGUI.Toggle(checkRect, wasChecked);
  96. rect.x = checkRect.xMax;
  97. rect.width -= checkRect.width;
  98. rect = DrawIconLeft(
  99. rect, rowHeight, icon, overlayIconData);
  100. if (isHighlighted)
  101. TreeView.DefaultGUI.BoldLabel(rect, label, isSelected, isFocused);
  102. else
  103. TreeView.DefaultGUI.Label(rect, label, isSelected, isFocused);
  104. return isChecked;
  105. }
  106. static Rect DrawIconLeft(
  107. Rect rect,
  108. float rowHeight,
  109. Texture icon,
  110. GetChangesOverlayIcon.Data overlayIconData)
  111. {
  112. if (icon == null)
  113. return rect;
  114. float iconWidth = rowHeight * ((float)icon.width / icon.height);
  115. Rect iconRect = new Rect(rect.x, rect.y, iconWidth, rowHeight);
  116. EditorGUI.LabelField(iconRect, new GUIContent(icon), UnityStyles.Tree.IconStyle);
  117. if (overlayIconData != null && overlayIconData.Texture != null)
  118. {
  119. Rect overlayIconRect = new Rect(
  120. iconRect.x + overlayIconData.XOffset,
  121. iconRect.y + overlayIconData.YOffset,
  122. overlayIconData.Size, overlayIconData.Size);
  123. GUI.DrawTexture(
  124. overlayIconRect, overlayIconData.Texture,
  125. ScaleMode.ScaleToFit);
  126. }
  127. rect.x += iconRect.width;
  128. rect.width -= iconRect.width;
  129. return rect;
  130. }
  131. static Rect GetCheckboxRect(Rect rect, float rowHeight)
  132. {
  133. return new Rect(
  134. rect.x,
  135. rect.y + UnityConstants.TREEVIEW_CHECKBOX_Y_OFFSET,
  136. UnityConstants.TREEVIEW_CHECKBOX_SIZE,
  137. rect.height);
  138. }
  139. static float GetIndent(int depth)
  140. {
  141. if (depth == -1)
  142. return 0;
  143. return 16 + (depth * 16);
  144. }
  145. internal static void ForSecondaryLabelRightAligned(
  146. Rect rect,
  147. string label,
  148. bool isSelected,
  149. bool isFocused,
  150. bool isBoldText)
  151. {
  152. if (Event.current.type != EventType.Repaint)
  153. return;
  154. if (isBoldText)
  155. {
  156. GUIStyle secondaryBoldRightAligned =
  157. UnityStyles.Tree.SecondaryLabelBoldRightAligned;
  158. secondaryBoldRightAligned.Draw(
  159. rect, label, false, true, isSelected, isFocused);
  160. return;
  161. }
  162. GUIStyle secondaryLabelRightAligned =
  163. UnityStyles.Tree.SecondaryLabelRightAligned;
  164. secondaryLabelRightAligned.Draw(
  165. rect, label, false, true, isSelected, isFocused);
  166. }
  167. internal static void ForSecondaryLabel(
  168. Rect rect,
  169. string label,
  170. bool isSelected,
  171. bool isFocused,
  172. bool isBoldText)
  173. {
  174. if (Event.current.type != EventType.Repaint)
  175. return;
  176. if (isBoldText)
  177. {
  178. GUIStyle secondaryBoldLabel =
  179. UnityStyles.Tree.SecondaryBoldLabel;
  180. secondaryBoldLabel.Draw(
  181. rect, label, false, true, isSelected, isFocused);
  182. return;
  183. }
  184. GUIStyle secondaryLabel =
  185. UnityStyles.Tree.SecondaryLabel;
  186. secondaryLabel.Draw(
  187. rect, label, false, true, isSelected, isFocused);
  188. }
  189. internal static void ForLabel(
  190. Rect rect,
  191. string label,
  192. bool isSelected,
  193. bool isFocused,
  194. bool isBoldText)
  195. {
  196. if (Event.current.type != EventType.Repaint)
  197. return;
  198. if (isBoldText)
  199. {
  200. TreeView.DefaultStyles.boldLabel.Draw(
  201. rect, label, false, true, isSelected, isFocused);
  202. return;
  203. }
  204. TreeView.DefaultStyles.label.Draw(
  205. rect, label, false, true, isSelected, isFocused);
  206. }
  207. }
  208. }