Images.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using System.IO;
  2. using System.Reflection;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using Codice.LogWrapper;
  6. using PlasticGui.Help;
  7. using Unity.PlasticSCM.Editor.AssetUtils;
  8. namespace Unity.PlasticSCM.Editor.UI
  9. {
  10. internal class Images
  11. {
  12. internal enum Name
  13. {
  14. None,
  15. IconPlastic,
  16. IconCloseButton,
  17. IconPressedCloseButton,
  18. IconAdded,
  19. IconDeleted,
  20. IconChanged,
  21. IconMoved,
  22. IconMergeLink,
  23. Ignored,
  24. IconMergeConflict,
  25. IconMerged,
  26. IconFsChanged,
  27. IconMergeCategory,
  28. XLink,
  29. Ok,
  30. NotOnDisk,
  31. IconRepository,
  32. IconPlasticView,
  33. IconPlasticViewNotify,
  34. Loading,
  35. IconEmptyGravatar,
  36. Step1,
  37. Step2,
  38. Step3,
  39. StepOk,
  40. ButtonSsoSignInUnity,
  41. ButtonSsoSignInEmail,
  42. ButtonSsoSignInGoogle,
  43. }
  44. internal static Texture2D GetHelpImage(HelpImage image)
  45. {
  46. // We use the dark version for both the light/dark skins since it matches the grey background better
  47. string helpImageFileName = string.Format(
  48. "d_{0}.png",
  49. HelpImageName.FromHelpImage(image));
  50. string imageRelativePath = GetImageFileRelativePath(helpImageFileName);
  51. Texture2D result = TryLoadImage(imageRelativePath, imageRelativePath);
  52. if (result != null)
  53. return result;
  54. mLog.WarnFormat("Image not found: {0}", helpImageFileName);
  55. return GetEmptyImage();
  56. }
  57. internal static Texture2D GetImage(Name image)
  58. {
  59. string imageFileName = image.ToString().ToLower() + ".png";
  60. string imageFileName2x = image.ToString().ToLower() + "@2x.png";
  61. string darkImageFileName = string.Format("d_{0}", imageFileName);
  62. string darkImageFileName2x = string.Format("d_{0}", imageFileName2x);
  63. string imageFileRelativePath = GetImageFileRelativePath(imageFileName);
  64. string imageFileRelativePath2x = GetImageFileRelativePath(imageFileName);
  65. string darkImageFileRelativePath = GetImageFileRelativePath(darkImageFileName);
  66. string darkImageFileRelativePath2x = GetImageFileRelativePath(darkImageFileName2x);
  67. Texture2D result = null;
  68. if (EditorGUIUtility.isProSkin)
  69. result = TryLoadImage(darkImageFileRelativePath, darkImageFileRelativePath2x);
  70. if (result != null)
  71. return result;
  72. result = TryLoadImage(imageFileRelativePath, imageFileRelativePath2x);
  73. if (result != null)
  74. return result;
  75. mLog.WarnFormat("Image not found: {0}", imageFileName);
  76. return GetEmptyImage();
  77. }
  78. internal static Texture GetFileIcon(string path)
  79. {
  80. string relativePath = GetRelativePath.ToApplication(path);
  81. return GetFileIconFromRelativePath(relativePath);
  82. }
  83. internal static Texture GetFileIconFromCmPath(string path)
  84. {
  85. return GetFileIconFromRelativePath(
  86. path.Substring(1).Replace("/",
  87. Path.DirectorySeparatorChar.ToString()));
  88. }
  89. internal static Texture GetDropDownIcon()
  90. {
  91. if (mPopupIcon == null)
  92. mPopupIcon = EditorGUIUtility.IconContent("icon dropdown").image;
  93. return mPopupIcon;
  94. }
  95. internal static Texture GetDirectoryIcon()
  96. {
  97. if (mDirectoryIcon == null)
  98. mDirectoryIcon = EditorGUIUtility.IconContent("Folder Icon").image;
  99. return mDirectoryIcon;
  100. }
  101. internal static Texture GetPrivatedOverlayIcon()
  102. {
  103. if (mPrivatedOverlayIcon == null)
  104. mPrivatedOverlayIcon = EditorGUIUtility.IconContent("P4_Local").image;
  105. return mPrivatedOverlayIcon;
  106. }
  107. internal static Texture GetAddedOverlayIcon()
  108. {
  109. if (mAddedOverlayIcon == null)
  110. mAddedOverlayIcon = EditorGUIUtility.IconContent("P4_AddedLocal").image;
  111. return mAddedOverlayIcon;
  112. }
  113. internal static Texture GetDeletedOverlayIcon()
  114. {
  115. if (mDeletedOverlayIcon == null)
  116. mDeletedOverlayIcon = EditorGUIUtility.IconContent("P4_DeletedLocal").image;
  117. return mDeletedOverlayIcon;
  118. }
  119. internal static Texture GetCheckedOutOverlayIcon()
  120. {
  121. if (mCheckedOutOverlayIcon == null)
  122. mCheckedOutOverlayIcon = EditorGUIUtility.IconContent("P4_CheckOutLocal").image;
  123. return mCheckedOutOverlayIcon;
  124. }
  125. internal static Texture GetDeletedRemoteOverlayIcon()
  126. {
  127. if (mDeletedRemoteOverlayIcon == null)
  128. mDeletedRemoteOverlayIcon = EditorGUIUtility.IconContent("P4_DeletedRemote").image;
  129. return mDeletedRemoteOverlayIcon;
  130. }
  131. internal static Texture GetOutOfSyncOverlayIcon()
  132. {
  133. if (mOutOfSyncOverlayIcon == null)
  134. mOutOfSyncOverlayIcon = EditorGUIUtility.IconContent("P4_OutOfSync").image;
  135. return mOutOfSyncOverlayIcon;
  136. }
  137. internal static Texture GetConflictedOverlayIcon()
  138. {
  139. if (mConflictedOverlayIcon == null)
  140. mConflictedOverlayIcon = EditorGUIUtility.IconContent("P4_Conflicted").image;
  141. return mConflictedOverlayIcon;
  142. }
  143. internal static Texture GetLockedLocalOverlayIcon()
  144. {
  145. if (mLockedLocalOverlayIcon == null)
  146. mLockedLocalOverlayIcon = EditorGUIUtility.IconContent("P4_LockedLocal").image;
  147. return mLockedLocalOverlayIcon;
  148. }
  149. internal static Texture GetLockedRemoteOverlayIcon()
  150. {
  151. if (mLockedRemoteOverlayIcon == null)
  152. mLockedRemoteOverlayIcon = EditorGUIUtility.IconContent("P4_LockedRemote").image;
  153. return mLockedRemoteOverlayIcon;
  154. }
  155. internal static Texture GetWarnIcon()
  156. {
  157. if (mWarnIcon == null)
  158. mWarnIcon = EditorGUIUtility.IconContent("console.warnicon.sml").image;
  159. return mWarnIcon;
  160. }
  161. internal static Texture GetInfoIcon()
  162. {
  163. if (mInfoIcon == null)
  164. mInfoIcon = EditorGUIUtility.IconContent("console.infoicon.sml").image;
  165. return mInfoIcon;
  166. }
  167. internal static Texture GetErrorDialogIcon()
  168. {
  169. if (mErrorDialogIcon == null)
  170. mErrorDialogIcon = EditorGUIUtility.IconContent("console.erroricon").image;
  171. return mErrorDialogIcon;
  172. }
  173. internal static Texture GetWarnDialogIcon()
  174. {
  175. if (mWarnDialogIcon == null)
  176. mWarnDialogIcon = EditorGUIUtility.IconContent("console.warnicon").image;
  177. return mWarnDialogIcon;
  178. }
  179. internal static Texture GetInfoDialogIcon()
  180. {
  181. if (mInfoDialogIcon == null)
  182. mInfoDialogIcon = EditorGUIUtility.IconContent("console.infoicon").image;
  183. return mInfoDialogIcon;
  184. }
  185. internal static Texture GetRefreshIcon()
  186. {
  187. if (mRefreshIcon == null)
  188. mRefreshIcon = EditorGUIUtility.FindTexture("Refresh");
  189. return mRefreshIcon;
  190. }
  191. internal static Texture GetCloseIcon()
  192. {
  193. if (mCloseIcon == null)
  194. mCloseIcon = EditorGUIUtility.FindTexture("winbtn_win_close");
  195. return mCloseIcon;
  196. }
  197. internal static Texture GetClickedCloseIcon()
  198. {
  199. if (mClickedCloseIcon == null)
  200. mClickedCloseIcon = EditorGUIUtility.FindTexture("winbtn_win_close_a");
  201. return mClickedCloseIcon;
  202. }
  203. internal static Texture GetHoveredCloseIcon()
  204. {
  205. if (mHoveredCloseIcon == null)
  206. mHoveredCloseIcon = EditorGUIUtility.FindTexture("winbtn_win_close_h");
  207. return mHoveredCloseIcon;
  208. }
  209. internal static Texture GetFileIcon()
  210. {
  211. if (mFileIcon == null)
  212. mFileIcon = EditorGUIUtility.FindTexture("DefaultAsset Icon");
  213. if (mFileIcon == null)
  214. mFileIcon = AssetPreview.GetMiniTypeThumbnail(typeof(DefaultAsset));
  215. if (mFileIcon == null)
  216. mFileIcon = GetEmptyImage();
  217. return mFileIcon;
  218. }
  219. internal static Texture2D GetLinkUnderlineImage()
  220. {
  221. if (mLinkUnderlineImage == null)
  222. {
  223. mLinkUnderlineImage = new Texture2D(1, 1);
  224. mLinkUnderlineImage.SetPixel(0, 0, UnityStyles.Colors.Link);
  225. mLinkUnderlineImage.Apply();
  226. }
  227. return mLinkUnderlineImage;
  228. }
  229. static Texture2D GetEmptyImage()
  230. {
  231. if (mEmptyImage == null)
  232. {
  233. mEmptyImage = new Texture2D(1, 1);
  234. mEmptyImage.SetPixel(0, 0, Color.clear);
  235. mEmptyImage.Apply();
  236. }
  237. return mEmptyImage;
  238. }
  239. static Texture GetFileIconFromRelativePath(string relativePath)
  240. {
  241. Texture result = AssetDatabase.GetCachedIcon(relativePath);
  242. if (result == null)
  243. return GetFileIcon();
  244. return result;
  245. }
  246. static string GetImageFileRelativePath(string imageFileName)
  247. {
  248. return Path.Combine(
  249. AssetsPath.GetImagesFolderRelativePath(),
  250. imageFileName);
  251. }
  252. static Texture2D TryLoadImage(string imageFileRelativePath, string image2xFilePath)
  253. {
  254. if (EditorGUIUtility.pixelsPerPoint > 1f && File.Exists(image2xFilePath))
  255. return LoadTextureFromFile(image2xFilePath);
  256. if (File.Exists(Path.GetFullPath(imageFileRelativePath)))
  257. return LoadTextureFromFile(imageFileRelativePath);
  258. return null;
  259. }
  260. static Texture2D LoadTextureFromFile(string path)
  261. {
  262. byte[] fileData = File.ReadAllBytes(path);
  263. Texture2D result = new Texture2D(1, 1);
  264. result.LoadImage(fileData); //auto-resizes the texture dimensions
  265. return result;
  266. }
  267. static Texture mFileIcon;
  268. static Texture mDirectoryIcon;
  269. static Texture mPrivatedOverlayIcon;
  270. static Texture mAddedOverlayIcon;
  271. static Texture mDeletedOverlayIcon;
  272. static Texture mCheckedOutOverlayIcon;
  273. static Texture mDeletedRemoteOverlayIcon;
  274. static Texture mOutOfSyncOverlayIcon;
  275. static Texture mConflictedOverlayIcon;
  276. static Texture mLockedLocalOverlayIcon;
  277. static Texture mLockedRemoteOverlayIcon;
  278. static Texture mWarnIcon;
  279. static Texture mInfoIcon;
  280. static Texture mErrorDialogIcon;
  281. static Texture mWarnDialogIcon;
  282. static Texture mInfoDialogIcon;
  283. static Texture mRefreshIcon;
  284. static Texture mCloseIcon;
  285. static Texture mClickedCloseIcon;
  286. static Texture mHoveredCloseIcon;
  287. static Texture2D mLinkUnderlineImage;
  288. static Texture2D mEmptyImage;
  289. static Texture mPopupIcon;
  290. static readonly ILog mLog = LogManager.GetLogger("Images");
  291. }
  292. }