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.

TabButton.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace Unity.PlasticSCM.Editor.UI
  5. {
  6. internal class TabButton
  7. {
  8. internal bool DrawTabButton(
  9. string buttonText,
  10. bool wasActive,
  11. float width)
  12. {
  13. bool isCloseButtonClicked;
  14. return DrawClosableTabButton(
  15. buttonText,
  16. wasActive,
  17. false,
  18. width,
  19. null,
  20. out isCloseButtonClicked);
  21. }
  22. internal bool DrawClosableTabButton(
  23. string buttonText,
  24. bool wasActive,
  25. bool isClosable,
  26. float width,
  27. Action repaintAction,
  28. out bool isCloseButtonClicked)
  29. {
  30. isCloseButtonClicked = false;
  31. GUIContent buttonContent = new GUIContent(buttonText);
  32. GUIStyle buttonStyle = UnityStyles.PlasticWindow.TabButton;
  33. Rect toggleRect = GUILayoutUtility.GetRect(
  34. buttonContent, buttonStyle,
  35. GUILayout.Width(width));
  36. if (isClosable && Event.current.type == EventType.MouseMove)
  37. {
  38. if (mCloseButtonRect.Contains(Event.current.mousePosition))
  39. {
  40. SetCloseButtonState(
  41. CloseButtonState.Hovered,
  42. repaintAction);
  43. }
  44. else
  45. {
  46. SetCloseButtonState(
  47. CloseButtonState.Normal,
  48. repaintAction);
  49. }
  50. }
  51. if (isClosable && Event.current.type == EventType.MouseDown)
  52. {
  53. if (mCloseButtonRect.Contains(Event.current.mousePosition))
  54. {
  55. SetCloseButtonState(
  56. CloseButtonState.Clicked,
  57. repaintAction);
  58. Event.current.Use();
  59. }
  60. }
  61. if (isClosable && Event.current.type == EventType.MouseUp)
  62. {
  63. if (mCloseButtonRect.Contains(Event.current.mousePosition))
  64. {
  65. Event.current.Use();
  66. isCloseButtonClicked = true;
  67. }
  68. if (IsTabClickWithMiddleButton(toggleRect, Event.current))
  69. {
  70. Event.current.Use();
  71. isCloseButtonClicked = true;
  72. }
  73. SetCloseButtonState(
  74. CloseButtonState.Normal,
  75. repaintAction);
  76. }
  77. bool isActive = GUI.Toggle(
  78. toggleRect, wasActive, buttonText, buttonStyle);
  79. if (isClosable && toggleRect.height > 1)
  80. {
  81. mCloseButtonRect = DrawCloseButton(
  82. toggleRect,
  83. mCloseButtonState);
  84. }
  85. if (wasActive)
  86. {
  87. DrawUnderline(toggleRect);
  88. }
  89. return isActive;
  90. }
  91. static Rect DrawCloseButton(
  92. Rect toggleRect,
  93. CloseButtonState state)
  94. {
  95. int closeButtonSize = 15;
  96. GUIContent closeImage = new GUIContent(GetCloseImage(state));
  97. Rect closeTabRect = new Rect(
  98. toggleRect.xMax - closeButtonSize - 1,
  99. toggleRect.y + (toggleRect.height / 2 - closeButtonSize / 2),
  100. closeButtonSize,
  101. closeButtonSize);
  102. GUI.Button(closeTabRect, closeImage, EditorStyles.label);
  103. return new Rect(
  104. closeTabRect.x - 1,
  105. closeTabRect.y - 1,
  106. closeTabRect.width + 2,
  107. closeTabRect.height + 2);
  108. }
  109. static void DrawUnderline(Rect toggleRect)
  110. {
  111. GUIStyle activeTabStyle =
  112. UnityStyles.PlasticWindow.ActiveTabUnderline;
  113. Rect underlineRect = new Rect(
  114. toggleRect.x,
  115. toggleRect.yMax - (activeTabStyle.fixedHeight / 2),
  116. toggleRect.width,
  117. activeTabStyle.fixedHeight);
  118. GUI.Label(underlineRect, string.Empty, activeTabStyle);
  119. }
  120. static bool IsTabClickWithMiddleButton(Rect toggleRect, Event currentEvent)
  121. {
  122. if (currentEvent.button != 2)
  123. return false;
  124. return toggleRect.height > 1 &&
  125. toggleRect.Contains(Event.current.mousePosition);
  126. }
  127. static Texture GetCloseImage(CloseButtonState state)
  128. {
  129. if (state == CloseButtonState.Hovered)
  130. return Images.GetHoveredCloseIcon();
  131. if (state == CloseButtonState.Clicked)
  132. return Images.GetClickedCloseIcon();
  133. return Images.GetCloseIcon();
  134. }
  135. void SetCloseButtonState(
  136. CloseButtonState newState,
  137. Action repaintAction)
  138. {
  139. if (mCloseButtonState == newState)
  140. return;
  141. mCloseButtonState = newState;
  142. if (repaintAction != null)
  143. repaintAction();
  144. }
  145. Rect mCloseButtonRect;
  146. CloseButtonState mCloseButtonState;
  147. enum CloseButtonState
  148. {
  149. Normal,
  150. Clicked,
  151. Hovered,
  152. }
  153. }
  154. }