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.

DrawAssetOverlay.cs 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using Codice.LogWrapper;
  5. using PlasticGui;
  6. using Unity.PlasticSCM.Editor.AssetsOverlays.Cache;
  7. using Unity.PlasticSCM.Editor.AssetUtils;
  8. using Unity.PlasticSCM.Editor.UI;
  9. namespace Unity.PlasticSCM.Editor.AssetsOverlays
  10. {
  11. internal static class DrawAssetOverlay
  12. {
  13. internal static void Enable(
  14. string wkPath,
  15. IAssetStatusCache assetStatusCache)
  16. {
  17. if (mIsEnabled)
  18. return;
  19. mLog.Debug("Enable");
  20. mWkPath = wkPath;
  21. mAssetStatusCache = assetStatusCache;
  22. mIsEnabled = true;
  23. mRepaintProjectWindow = ProjectWindow.Repaint;
  24. EditorApplication.projectWindowItemOnGUI += OnProjectWindowItemGUI;
  25. mRepaintProjectWindow();
  26. }
  27. internal static void Disable()
  28. {
  29. mLog.Debug("Disable");
  30. mIsEnabled = false;
  31. EditorApplication.projectWindowItemOnGUI -= OnProjectWindowItemGUI;
  32. mRepaintProjectWindow();
  33. mWkPath = null;
  34. mAssetStatusCache = null;
  35. }
  36. internal static string GetStatusString(AssetStatus assetStatus)
  37. {
  38. if (ClassifyAssetStatus.IsPrivate(assetStatus))
  39. return PlasticLocalization.GetString(
  40. PlasticLocalization.Name.Private);
  41. if (ClassifyAssetStatus.IsIgnored(assetStatus))
  42. return PlasticLocalization.GetString(
  43. PlasticLocalization.Name.StatusIgnored);
  44. if (ClassifyAssetStatus.IsAdded(assetStatus))
  45. return PlasticLocalization.GetString(
  46. PlasticLocalization.Name.StatusAdded);
  47. if (ClassifyAssetStatus.IsConflicted(assetStatus))
  48. return PlasticLocalization.GetString(
  49. PlasticLocalization.Name.StatusConflicted);
  50. if (ClassifyAssetStatus.IsDeletedOnServer(assetStatus))
  51. return PlasticLocalization.GetString(
  52. PlasticLocalization.Name.StatusDeletedOnServer);
  53. if (ClassifyAssetStatus.IsLockedRemote(assetStatus))
  54. return PlasticLocalization.GetString(
  55. PlasticLocalization.Name.StatusLockedRemote);
  56. if (ClassifyAssetStatus.IsOutOfDate(assetStatus))
  57. return PlasticLocalization.GetString(
  58. PlasticLocalization.Name.StatusOutOfDate);
  59. if (ClassifyAssetStatus.IsLocked(assetStatus))
  60. return PlasticLocalization.GetString(
  61. PlasticLocalization.Name.StatusLockedByMe);
  62. if (ClassifyAssetStatus.IsRetained(assetStatus))
  63. return PlasticLocalization.GetString(
  64. PlasticLocalization.Name.StatusRetained);
  65. if (ClassifyAssetStatus.IsCheckedOut(assetStatus))
  66. return PlasticLocalization.GetString(
  67. PlasticLocalization.Name.StatusCheckout);
  68. return string.Empty;
  69. }
  70. internal static string GetTooltipText(
  71. AssetStatus statusValue,
  72. LockStatusData lockStatusData)
  73. {
  74. string statusText = GetStatusString(statusValue);
  75. if (lockStatusData == null)
  76. return statusText;
  77. // example:
  78. // Changed by:
  79. // * dani_pen@hotmail.com
  80. // * workspace wkLocal"
  81. char bulletCharacter = '\u25cf';
  82. string line1 = ClassifyAssetStatus.IsLocked(statusValue) ?
  83. statusText + ":" :
  84. PlasticLocalization.GetString(
  85. PlasticLocalization.Name.AssetOverlayTooltipStatus,
  86. statusText);
  87. string line2 = string.Format("{0} {1}",
  88. bulletCharacter,
  89. lockStatusData.LockedBy);
  90. string line3 = string.Format("{0} {1}",
  91. bulletCharacter,
  92. PlasticLocalization.GetString(
  93. PlasticLocalization.Name.AssetOverlayTooltipOn,
  94. lockStatusData.HolderBranchName));
  95. return string.Format(
  96. "{0}" + Environment.NewLine +
  97. "{1}" + Environment.NewLine +
  98. "{2}",
  99. line1,
  100. line2,
  101. line3);
  102. }
  103. static void OnProjectWindowItemGUI(string guid, Rect selectionRect)
  104. {
  105. if (string.IsNullOrEmpty(guid))
  106. return;
  107. if (Event.current.type != EventType.Repaint)
  108. return;
  109. string fullPath = AssetsPath.GetFullPathUnderWorkspace.
  110. ForGuid(mWkPath, guid);
  111. if (fullPath == null)
  112. return;
  113. AssetStatus assetStatus = mAssetStatusCache.GetStatus(fullPath);
  114. string tooltipText = GetTooltipText(
  115. assetStatus,
  116. mAssetStatusCache.GetLockStatusData(fullPath));
  117. DrawOverlayIcon.ForStatus(
  118. selectionRect,
  119. assetStatus,
  120. tooltipText);
  121. }
  122. internal static class DrawOverlayIcon
  123. {
  124. internal static void ForStatus(
  125. Rect selectionRect,
  126. AssetStatus status,
  127. string tooltipText)
  128. {
  129. Texture overlayIcon = GetOverlayIcon(status);
  130. if (overlayIcon == null)
  131. return;
  132. Rect overlayRect = OverlayRect.GetOverlayRect(
  133. selectionRect,
  134. OVERLAY_ICON_OFFSET);
  135. GUI.DrawTexture(
  136. overlayRect, overlayIcon, ScaleMode.ScaleToFit);
  137. Rect tooltipRect = GetTooltipRect(selectionRect, overlayRect);
  138. GUI.Label(tooltipRect, new GUIContent(string.Empty, tooltipText));
  139. }
  140. internal static Texture GetOverlayIcon(AssetStatus assetStatus)
  141. {
  142. if (ClassifyAssetStatus.IsPrivate(assetStatus))
  143. return Images.GetPrivatedOverlayIcon();
  144. if (ClassifyAssetStatus.IsIgnored(assetStatus))
  145. return Images.GetIgnoredOverlayIcon();
  146. if (ClassifyAssetStatus.IsAdded(assetStatus))
  147. return Images.GetAddedOverlayIcon();
  148. if (ClassifyAssetStatus.IsConflicted(assetStatus))
  149. return Images.GetConflictedOverlayIcon();
  150. if (ClassifyAssetStatus.IsDeletedOnServer(assetStatus))
  151. return Images.GetDeletedRemoteOverlayIcon();
  152. if (ClassifyAssetStatus.IsLockedRemote(assetStatus))
  153. return Images.GetLockedRemoteOverlayIcon();
  154. if (ClassifyAssetStatus.IsOutOfDate(assetStatus))
  155. return Images.GetOutOfSyncOverlayIcon();
  156. if (ClassifyAssetStatus.IsLocked(assetStatus))
  157. return Images.GetLockedLocalOverlayIcon();
  158. if (ClassifyAssetStatus.IsRetained(assetStatus))
  159. return Images.GetRetainedOverlayIcon();
  160. if (ClassifyAssetStatus.IsCheckedOut(assetStatus))
  161. return Images.GetCheckedOutOverlayIcon();
  162. return null;
  163. }
  164. static Rect Inflate(Rect rect, float width, float height)
  165. {
  166. return new Rect(
  167. rect.x - width,
  168. rect.y - height,
  169. rect.width + 2f * width,
  170. rect.height + 2f * height);
  171. }
  172. static Rect GetTooltipRect(
  173. Rect selectionRect,
  174. Rect overlayRect)
  175. {
  176. if (selectionRect.width > selectionRect.height)
  177. {
  178. return overlayRect;
  179. }
  180. return Inflate(overlayRect, 3f, 3f);
  181. }
  182. }
  183. static Action mRepaintProjectWindow;
  184. static bool mIsEnabled;
  185. static IAssetStatusCache mAssetStatusCache;
  186. static string mWkPath;
  187. const float OVERLAY_ICON_OFFSET = 20f;
  188. static readonly ILog mLog = PlasticApp.GetLogger("DrawAssetOverlay");
  189. }
  190. }