No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DrawTreeViewItem.cs 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. int depth,
  18. string label,
  19. string infoLabel,
  20. bool isSelected,
  21. bool isFocused)
  22. {
  23. float indent = GetIndent(depth);
  24. rowRect.x += indent;
  25. rowRect.width -= indent;
  26. //add a little indentation
  27. rowRect.x += 5;
  28. rowRect.width -= 5;
  29. TreeView.DefaultGUI.Label(rowRect, label, isSelected, isFocused);
  30. if (!string.IsNullOrEmpty(infoLabel))
  31. DrawInfolabel(rowRect, label, infoLabel);
  32. }
  33. internal static bool ForCheckableCategoryItem(
  34. Rect rowRect,
  35. float rowHeight,
  36. int depth,
  37. string label,
  38. string infoLabel,
  39. bool isSelected,
  40. bool isFocused,
  41. bool wasChecked,
  42. bool hadCheckedChildren,
  43. bool hadPartiallyCheckedChildren)
  44. {
  45. float indent = GetIndent(depth);
  46. rowRect.x += indent;
  47. rowRect.width -= indent;
  48. Rect checkRect = GetCheckboxRect(rowRect, rowHeight);
  49. if (!wasChecked && (hadCheckedChildren || hadPartiallyCheckedChildren))
  50. EditorGUI.showMixedValue = true;
  51. bool isChecked = EditorGUI.Toggle(checkRect, wasChecked);
  52. EditorGUI.showMixedValue = false;
  53. rowRect.x = checkRect.xMax - 4;
  54. rowRect.width -= checkRect.width;
  55. //add a little indentation
  56. rowRect.x += 5;
  57. rowRect.width -= 5;
  58. TreeView.DefaultGUI.Label(rowRect, label, isSelected, isFocused);
  59. if (!string.IsNullOrEmpty(infoLabel))
  60. DrawInfolabel(rowRect, label, infoLabel);
  61. return isChecked;
  62. }
  63. internal static void ForItemCell(
  64. Rect rect,
  65. float rowHeight,
  66. int depth,
  67. Texture icon,
  68. Texture overlayIcon,
  69. string label,
  70. bool isSelected,
  71. bool isFocused,
  72. bool isBoldText,
  73. bool isSecondaryLabel)
  74. {
  75. float indent = GetIndent(depth);
  76. rect.x += indent;
  77. rect.width -= indent;
  78. rect = DrawIconLeft(
  79. rect, rowHeight, icon, overlayIcon);
  80. if (isSecondaryLabel)
  81. {
  82. ForSecondaryLabel(rect, label, isSelected, isFocused, isBoldText);
  83. return;
  84. }
  85. ForLabel(rect, label, isSelected, isFocused, isBoldText);
  86. }
  87. internal static bool ForCheckableItemCell(
  88. Rect rect,
  89. float rowHeight,
  90. int depth,
  91. Texture icon,
  92. Texture overlayIcon,
  93. string label,
  94. bool isSelected,
  95. bool isFocused,
  96. bool isHighlighted,
  97. bool wasChecked)
  98. {
  99. float indent = GetIndent(depth);
  100. rect.x += indent;
  101. rect.width -= indent;
  102. Rect checkRect = GetCheckboxRect(rect, rowHeight);
  103. bool isChecked = EditorGUI.Toggle(checkRect, wasChecked);
  104. rect.x = checkRect.xMax;
  105. rect.width -= checkRect.width;
  106. rect = DrawIconLeft(
  107. rect, rowHeight, icon, overlayIcon);
  108. if (isHighlighted)
  109. TreeView.DefaultGUI.BoldLabel(rect, label, isSelected, isFocused);
  110. else
  111. TreeView.DefaultGUI.Label(rect, label, isSelected, isFocused);
  112. return isChecked;
  113. }
  114. internal static Rect DrawIconLeft(
  115. Rect rect,
  116. float rowHeight,
  117. Texture icon,
  118. Texture overlayIcon)
  119. {
  120. if (icon == null)
  121. return rect;
  122. float iconWidth = rowHeight * ((float)icon.width / icon.height);
  123. Rect iconRect = new Rect(rect.x, rect.y, iconWidth, rowHeight);
  124. EditorGUI.LabelField(iconRect, new GUIContent(icon), UnityStyles.Tree.IconStyle);
  125. if (overlayIcon != null)
  126. {
  127. Rect overlayIconRect = OverlayRect.GetOverlayRect(
  128. iconRect,
  129. OVERLAY_ICON_OFFSET);
  130. GUI.DrawTexture(
  131. overlayIconRect, overlayIcon,
  132. ScaleMode.ScaleToFit);
  133. }
  134. rect.x += iconRect.width;
  135. rect.width -= iconRect.width;
  136. return rect;
  137. }
  138. static void DrawInfolabel(
  139. Rect rect,
  140. string label,
  141. string infoLabel)
  142. {
  143. Vector2 labelSize = ((GUIStyle)UnityStyles.Tree.Label)
  144. .CalcSize(new GUIContent(label));
  145. rect.x += labelSize.x;
  146. GUI.Label(rect, infoLabel, UnityStyles.Tree.InfoLabel);
  147. }
  148. static Rect GetCheckboxRect(Rect rect, float rowHeight)
  149. {
  150. return new Rect(
  151. rect.x,
  152. rect.y + UnityConstants.TREEVIEW_CHECKBOX_Y_OFFSET,
  153. UnityConstants.TREEVIEW_CHECKBOX_SIZE,
  154. rect.height);
  155. }
  156. static float GetIndent(int depth)
  157. {
  158. if (depth == -1)
  159. return 0;
  160. return 16 + (depth * 16);
  161. }
  162. internal static void ForSecondaryLabelRightAligned(
  163. Rect rect,
  164. string label,
  165. bool isSelected,
  166. bool isFocused,
  167. bool isBoldText)
  168. {
  169. if (Event.current.type != EventType.Repaint)
  170. return;
  171. if (isBoldText)
  172. {
  173. GUIStyle secondaryBoldRightAligned =
  174. UnityStyles.Tree.SecondaryLabelBoldRightAligned;
  175. secondaryBoldRightAligned.Draw(
  176. rect, label, false, true, isSelected, isFocused);
  177. return;
  178. }
  179. GUIStyle secondaryLabelRightAligned =
  180. UnityStyles.Tree.SecondaryLabelRightAligned;
  181. secondaryLabelRightAligned.Draw(
  182. rect, label, false, true, isSelected, isFocused);
  183. }
  184. internal static void ForSecondaryLabel(
  185. Rect rect,
  186. string label,
  187. bool isSelected,
  188. bool isFocused,
  189. bool isBoldText)
  190. {
  191. if (Event.current.type != EventType.Repaint)
  192. return;
  193. if (isBoldText)
  194. {
  195. GUIStyle secondaryBoldLabel =
  196. UnityStyles.Tree.SecondaryBoldLabel;
  197. secondaryBoldLabel.fontSize = UnityConstants.PENDING_CHANGES_FONT_SIZE;
  198. secondaryBoldLabel.normal.textColor = Color.red;
  199. secondaryBoldLabel.Draw(
  200. rect, label, false, true, isSelected, isFocused);
  201. return;
  202. }
  203. GUIStyle secondaryLabel =
  204. UnityStyles.Tree.SecondaryLabel;
  205. secondaryLabel.fontSize = UnityConstants.PENDING_CHANGES_FONT_SIZE;
  206. secondaryLabel.Draw(
  207. rect, label, false, true, isSelected, isFocused);
  208. }
  209. internal static void ForLabel(
  210. Rect rect,
  211. string label,
  212. bool isSelected,
  213. bool isFocused,
  214. bool isBoldText)
  215. {
  216. if (Event.current.type != EventType.Repaint)
  217. return;
  218. if (isBoldText)
  219. {
  220. TreeView.DefaultStyles.boldLabel.Draw(
  221. rect, label, false, true, isSelected, isFocused);
  222. return;
  223. }
  224. TreeView.DefaultStyles.label.Draw(
  225. rect, label, false, true, isSelected, isFocused);
  226. }
  227. const float OVERLAY_ICON_OFFSET = 16f;
  228. }
  229. }