Nessuna descrizione
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.

Images.cs 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. using System.Collections.Generic;
  2. using System.IO;
  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. IconAddedLocal,
  19. IconAddedOverlay,
  20. IconPrivateOverlay,
  21. IconCheckedOutLocalOverlay,
  22. IconDeleted,
  23. IconDeletedLocalOverlay,
  24. IconDeletedRemote,
  25. IconDeletedRemoteOverlay,
  26. IconOutOfSync,
  27. IconOutOfSyncOverlay,
  28. IconMoved,
  29. IconMergeLink,
  30. Ignored,
  31. IgnoredOverlay,
  32. IconConflicted,
  33. IconConflictedOverlay,
  34. IconConflictResolvedOverlay,
  35. IconLockedLocalOverlay,
  36. IconLockedRemoteOverlay,
  37. XLink,
  38. Ok,
  39. SecondaryTabClose,
  40. SecondaryTabCloseHover,
  41. NotOnDisk,
  42. IconRepository,
  43. IconPlasticView,
  44. IconPlasticViewNotify,
  45. IconPlasticNotifyIncoming,
  46. IconPlasticNotifyConflict,
  47. Loading,
  48. IconEmptyGravatar,
  49. Step1,
  50. Step2,
  51. Step3,
  52. StepOk,
  53. ButtonSsoSignInUnity,
  54. ButtonSsoSignInEmail,
  55. ButtonSsoSignInGoogle,
  56. IconBranch,
  57. IconUndo,
  58. Refresh
  59. }
  60. internal static Texture2D GetHelpImage(HelpImage image)
  61. {
  62. // We use the dark version for both the light/dark skins since it matches the grey background better
  63. string helpImageFileName = string.Format(
  64. "d_{0}.png",
  65. HelpImageName.FromHelpImage(image));
  66. string imageRelativePath = GetImageFileRelativePath(helpImageFileName);
  67. Texture2D result = TryLoadImage(imageRelativePath, imageRelativePath);
  68. if (result != null)
  69. return result;
  70. mLog.WarnFormat("Image not found: {0}", helpImageFileName);
  71. return GetEmptyImage();
  72. }
  73. internal static Texture2D GetImage(Name image)
  74. {
  75. string imageFileName = image.ToString().ToLower() + ".png";
  76. string imageFileName2x = image.ToString().ToLower() + "@2x.png";
  77. string darkImageFileName = string.Format("d_{0}", imageFileName);
  78. string darkImageFileName2x = string.Format("d_{0}", imageFileName2x);
  79. string imageFileRelativePath = GetImageFileRelativePath(imageFileName);
  80. string imageFileRelativePath2x = GetImageFileRelativePath(imageFileName2x);
  81. string darkImageFileRelativePath = GetImageFileRelativePath(darkImageFileName);
  82. string darkImageFileRelativePath2x = GetImageFileRelativePath(darkImageFileName2x);
  83. Texture2D result = null;
  84. if (EditorGUIUtility.isProSkin)
  85. result = TryLoadImage(darkImageFileRelativePath, darkImageFileRelativePath2x);
  86. if (result != null)
  87. return result;
  88. result = TryLoadImage(imageFileRelativePath, imageFileRelativePath2x);
  89. if (result != null)
  90. return result;
  91. mLog.WarnFormat("Image not found: {0}", imageFileName);
  92. return GetEmptyImage();
  93. }
  94. internal static Texture GetFileIcon(string path)
  95. {
  96. string relativePath = GetRelativePath.ToApplication(path);
  97. return GetFileIconFromRelativePath(relativePath);
  98. }
  99. internal static Texture GetFileIconFromCmPath(string path)
  100. {
  101. return GetFileIconFromRelativePath(
  102. path.Substring(1).Replace("/",
  103. Path.DirectorySeparatorChar.ToString()));
  104. }
  105. internal static Texture GetDropDownIcon()
  106. {
  107. return GetIconFromEditorGUI("icon dropdown");
  108. }
  109. internal static Texture GetDirectoryIcon()
  110. {
  111. return GetIconFromEditorGUI("Folder Icon");
  112. }
  113. internal static Texture GetPrivatedOverlayIcon()
  114. {
  115. if (mPrivatedOverlayIcon == null)
  116. mPrivatedOverlayIcon = GetImage(Name.IconPrivateOverlay);
  117. return mPrivatedOverlayIcon;
  118. }
  119. internal static Texture GetAddedOverlayIcon()
  120. {
  121. if (mAddedOverlayIcon == null)
  122. mAddedOverlayIcon = GetImage(Name.IconAddedOverlay);
  123. return mAddedOverlayIcon;
  124. }
  125. internal static Texture GetDeletedLocalOverlayIcon()
  126. {
  127. if (mDeletedLocalOverlayIcon == null)
  128. mDeletedLocalOverlayIcon = GetImage(Name.IconDeletedLocalOverlay);
  129. return mDeletedLocalOverlayIcon;
  130. }
  131. internal static Texture GetDeletedRemoteOverlayIcon()
  132. {
  133. if (mDeletedRemoteOverlayIcon == null)
  134. mDeletedRemoteOverlayIcon = GetImage(Name.IconDeletedRemoteOverlay);
  135. return mDeletedRemoteOverlayIcon;
  136. }
  137. internal static Texture GetCheckedOutOverlayIcon()
  138. {
  139. if (mCheckedOutOverlayIcon == null)
  140. mCheckedOutOverlayIcon = GetImage(Name.IconCheckedOutLocalOverlay);
  141. return mCheckedOutOverlayIcon;
  142. }
  143. internal static Texture GetOutOfSyncOverlayIcon()
  144. {
  145. if (mOutOfSyncOverlayIcon == null)
  146. mOutOfSyncOverlayIcon = GetImage(Name.IconOutOfSyncOverlay);
  147. return mOutOfSyncOverlayIcon;
  148. }
  149. internal static Texture GetConflictedOverlayIcon()
  150. {
  151. if (mConflictedOverlayIcon == null)
  152. mConflictedOverlayIcon = GetImage(Name.IconConflictedOverlay);
  153. return mConflictedOverlayIcon;
  154. }
  155. internal static Texture GetConflictResolvedOverlayIcon()
  156. {
  157. if (mConflictResolvedOverlayIcon == null)
  158. mConflictResolvedOverlayIcon = GetImage(Name.IconConflictResolvedOverlay);
  159. return mConflictResolvedOverlayIcon;
  160. }
  161. internal static Texture GetLockedLocalOverlayIcon()
  162. {
  163. if (mLockedLocalOverlayIcon == null)
  164. mLockedLocalOverlayIcon = GetImage(Name.IconLockedLocalOverlay);
  165. return mLockedLocalOverlayIcon;
  166. }
  167. internal static Texture GetLockedRemoteOverlayIcon()
  168. {
  169. if (mLockedRemoteOverlayIcon == null)
  170. mLockedRemoteOverlayIcon = GetImage(Name.IconLockedRemoteOverlay);
  171. return mLockedRemoteOverlayIcon;
  172. }
  173. internal static Texture GetIgnoredOverlayIcon()
  174. {
  175. if (mIgnoredverlayIcon == null)
  176. mIgnoredverlayIcon = GetImage(Name.IgnoredOverlay);
  177. return mIgnoredverlayIcon;
  178. }
  179. internal static Texture GetWarnIcon()
  180. {
  181. return GetIconFromEditorGUI("console.warnicon.sml");
  182. }
  183. internal static Texture GetInfoIcon()
  184. {
  185. return GetIconFromEditorGUI("console.infoicon.sml");
  186. }
  187. internal static Texture GetErrorDialogIcon()
  188. {
  189. return GetIconFromEditorGUI("console.erroricon");
  190. }
  191. internal static Texture GetWarnDialogIcon()
  192. {
  193. return GetIconFromEditorGUI("console.warnicon");
  194. }
  195. internal static Texture GetInfoDialogIcon()
  196. {
  197. return GetIconFromEditorGUI("console.infoicon");
  198. }
  199. internal static Texture GetRefreshIcon()
  200. {
  201. if (mRefreshIcon == null)
  202. mRefreshIcon = GetImage(Name.Refresh);
  203. return mRefreshIcon;
  204. }
  205. internal static Texture GetSettingsIcon()
  206. {
  207. return GetIconFromEditorGUI("settings");
  208. }
  209. internal static Texture GetCloseIcon()
  210. {
  211. if (mCloseIcon == null)
  212. mCloseIcon = GetImage(Name.SecondaryTabClose);
  213. return mCloseIcon;
  214. }
  215. internal static Texture GetClickedCloseIcon()
  216. {
  217. if (mClickedCloseIcon == null)
  218. mClickedCloseIcon = GetImage(Name.SecondaryTabCloseHover);
  219. return mClickedCloseIcon;
  220. }
  221. internal static Texture GetHoveredCloseIcon()
  222. {
  223. if (mHoveredCloseIcon == null)
  224. mHoveredCloseIcon = GetImage(Name.SecondaryTabCloseHover);
  225. return mHoveredCloseIcon;
  226. }
  227. internal static Texture2D GetUndoIcon()
  228. {
  229. if (mUndoIcon == null)
  230. mUndoIcon = GetImage(Name.IconUndo);
  231. return mUndoIcon;
  232. }
  233. internal static Texture2D GetPlasticIcon()
  234. {
  235. if (mPlasticIcon == null)
  236. mPlasticIcon = GetImage(Name.IconPlastic);
  237. return mPlasticIcon;
  238. }
  239. internal static Texture2D GetBranchIcon()
  240. {
  241. if (mBranchIcon == null)
  242. mBranchIcon = GetImage(Name.IconBranch);
  243. return mBranchIcon;
  244. }
  245. internal static Texture2D GetConflictedIcon()
  246. {
  247. if (mConflictedIcon == null)
  248. mConflictedIcon = GetImage(Name.IconConflicted);
  249. return mConflictedIcon;
  250. }
  251. internal static Texture2D GetOutOfSyncIcon()
  252. {
  253. if (mOutOfSyncIcon == null)
  254. mOutOfSyncIcon = GetImage(Name.IconOutOfSync);
  255. return mOutOfSyncIcon;
  256. }
  257. internal static Texture2D GetPlasticViewIcon()
  258. {
  259. if (mPlasticViewIcon == null)
  260. mPlasticViewIcon = GetImage(Name.IconPlasticView);
  261. return mPlasticViewIcon;
  262. }
  263. internal static Texture2D GePlasticNotifyIncomingIcon()
  264. {
  265. if (mPlasticNotifyIncomingIcon == null)
  266. mPlasticNotifyIncomingIcon = GetImage(Name.IconPlasticNotifyIncoming);
  267. return mPlasticNotifyIncomingIcon;
  268. }
  269. internal static Texture2D GetPlasticNotifyConflictIcon()
  270. {
  271. if (mPlasticNotifyConflictIcon == null)
  272. mPlasticNotifyConflictIcon = GetImage(Name.IconPlasticNotifyConflict);
  273. return mPlasticNotifyConflictIcon;
  274. }
  275. internal static Texture2D GetEmptyGravatar()
  276. {
  277. if (mEmptyGravatarIcon == null)
  278. mEmptyGravatarIcon = Images.GetImage(Images.Name.IconEmptyGravatar);
  279. return mEmptyGravatarIcon;
  280. }
  281. internal static Texture2D GetStepOkIcon()
  282. {
  283. if (mStepOkIcon == null)
  284. mStepOkIcon = Images.GetImage(Images.Name.StepOk);
  285. return mStepOkIcon;
  286. }
  287. internal static Texture2D GetStep1Icon()
  288. {
  289. if (mStep1Icon == null)
  290. mStep1Icon = Images.GetImage(Images.Name.Step1);
  291. return mStep1Icon;
  292. }
  293. internal static Texture2D GetStep2Icon()
  294. {
  295. if (mStep2Icon == null)
  296. mStep2Icon = Images.GetImage(Images.Name.Step2);
  297. return mStep2Icon;
  298. }
  299. internal static Texture2D GetMergeLinkIcon()
  300. {
  301. if (mMergeLinkIcon == null)
  302. mMergeLinkIcon = Images.GetImage(Images.Name.IconMergeLink);
  303. return mMergeLinkIcon;
  304. }
  305. internal static Texture2D GetAddedLocalIcon()
  306. {
  307. if (mAddedLocalIcon == null)
  308. mAddedLocalIcon = Images.GetImage(Images.Name.IconAddedLocal);
  309. return mAddedLocalIcon;
  310. }
  311. internal static Texture2D GetDeletedRemoteIcon()
  312. {
  313. if (mDeletedRemoteIcon == null)
  314. mDeletedRemoteIcon = Images.GetImage(Images.Name.IconDeletedRemote);
  315. return mDeletedRemoteIcon;
  316. }
  317. internal static Texture2D GetDeletedIcon()
  318. {
  319. if (mDeletedIcon == null)
  320. mDeletedIcon = Images.GetImage(Images.Name.IconDeleted);
  321. return mDeletedIcon;
  322. }
  323. internal static Texture2D GetMovedIcon()
  324. {
  325. if (mMovedIcon == null)
  326. mMovedIcon = Images.GetImage(Images.Name.IconMoved);
  327. return mMovedIcon;
  328. }
  329. internal static Texture2D GetRepositoryIcon()
  330. {
  331. if (mRepositoryIcon == null)
  332. mRepositoryIcon = Images.GetImage(Images.Name.IconRepository);
  333. return mRepositoryIcon;
  334. }
  335. internal static Texture GetFileIcon()
  336. {
  337. if (mFileIcon == null)
  338. mFileIcon = EditorGUIUtility.FindTexture("DefaultAsset Icon");
  339. if (mFileIcon == null)
  340. mFileIcon = GetIconFromAssetPreview(typeof(DefaultAsset));
  341. if (mFileIcon == null)
  342. mFileIcon = GetEmptyImage();
  343. return mFileIcon;
  344. }
  345. internal static Texture2D GetLinkUnderlineImage()
  346. {
  347. if (mLinkUnderlineImage == null)
  348. {
  349. mLinkUnderlineImage = new Texture2D(1, 1);
  350. mLinkUnderlineImage.SetPixel(0, 0, UnityStyles.Colors.Link);
  351. mLinkUnderlineImage.Apply();
  352. }
  353. return mLinkUnderlineImage;
  354. }
  355. internal static Texture2D GetTreeviewBackgroundTexture()
  356. {
  357. if (mTreeviewBackgroundTexture == null)
  358. mTreeviewBackgroundTexture = GetTextureFromColor(UnityStyles.Colors.TreeViewBackground);
  359. return mTreeviewBackgroundTexture;
  360. }
  361. internal static Texture2D GetCommentBackgroundTexture()
  362. {
  363. if (mCommentBackground == null)
  364. mCommentBackground = GetTextureFromColor(UnityStyles.Colors.CommentsBackground);
  365. return mCommentBackground;
  366. }
  367. internal static Texture2D GetColumnsBackgroundTexture()
  368. {
  369. if (mColumnsBackgroundTexture == null)
  370. mColumnsBackgroundTexture = GetTextureFromColor(UnityStyles.Colors.ColumnsBackground);
  371. return mColumnsBackgroundTexture;
  372. }
  373. static Texture2D GetEmptyImage()
  374. {
  375. if (mEmptyImage == null)
  376. mEmptyImage = GetTextureFromColor(Color.clear);
  377. return mEmptyImage;
  378. }
  379. static Texture2D GetTextureFromColor(Color color)
  380. {
  381. Texture2D texture = new Texture2D(1, 1);
  382. texture.SetPixel(0, 0, color);
  383. texture.Apply();
  384. return texture;
  385. }
  386. static Texture GetFileIconFromRelativePath(string relativePath)
  387. {
  388. Texture result = AssetDatabase.GetCachedIcon(relativePath);
  389. if (result != null)
  390. return result;
  391. result = GetFileIconFromKnownExtension(relativePath);
  392. if (result != null)
  393. return result;
  394. return GetFileIcon();
  395. }
  396. static Texture GetFileIconFromKnownExtension(string relativePath)
  397. {
  398. if (relativePath.EndsWith(UnityConstants.TREEVIEW_META_LABEL))
  399. {
  400. relativePath = relativePath.Substring(0,
  401. relativePath.Length- UnityConstants.TREEVIEW_META_LABEL.Length);
  402. }
  403. string extension = Path.GetExtension(relativePath).ToLower();
  404. if (extension.Equals(".cs"))
  405. return GetIconFromEditorGUI("cs Script Icon");
  406. if (extension.Equals(".png") || extension.Equals(".jpg")
  407. || extension.Equals(".jpeg") || extension.Equals(".gif")
  408. || extension.Equals(".tga") || extension.Equals(".bmp")
  409. || extension.Equals(".tif") || extension.Equals(".tiff"))
  410. return GetIconFromEditorGUI("d_Texture Icon");
  411. if (extension.Equals(".mat"))
  412. return GetIconFromAssetPreview(typeof(UnityEngine.Material));
  413. if (extension.Equals(".fbx") || extension.Equals(".ma")
  414. || extension.Equals(".mb") || extension.Equals(".blend")
  415. || extension.Equals(".max") )
  416. return GetIconFromAssetPreview(typeof(UnityEngine.GameObject));
  417. if (extension.Equals(".wav") || extension.Equals(".mp3"))
  418. return GetIconFromAssetPreview(typeof(UnityEngine.AudioClip));
  419. if (extension.Equals(".anim"))
  420. return GetIconFromAssetPreview(typeof(UnityEngine.Animation));
  421. if (extension.Equals(".animator"))
  422. return GetIconFromAssetPreview(typeof(UnityEngine.Animator));
  423. if (extension.Equals(".shader"))
  424. return GetIconFromEditorGUI("d_Shader Icon");
  425. if (extension.Equals(".asset") && relativePath.StartsWith("ProjectSettings\\"))
  426. return GetIconFromEditorGUI("EditorSettings Icon");
  427. return null;
  428. }
  429. static Texture2D GetIconFromEditorGUI(string name)
  430. {
  431. Texture2D result;
  432. if (mImagesFromEditorGUICache.TryGetValue(name, out result))
  433. {
  434. if (result != null)
  435. return result;
  436. mImagesFromEditorGUICache.Remove(name);
  437. }
  438. result = EditorGUIUtility.IconContent(name).image as Texture2D;
  439. mImagesFromEditorGUICache.Add(name, result);
  440. return result;
  441. }
  442. static Texture2D GetIconFromAssetPreview(System.Type type)
  443. {
  444. Texture2D result;
  445. if (mImagesFromAssetPreviewCache.TryGetValue(type.ToString(), out result))
  446. {
  447. if (result != null)
  448. return result;
  449. mImagesFromAssetPreviewCache.Remove(type.ToString());
  450. }
  451. result = AssetPreview.GetMiniTypeThumbnail(type);
  452. mImagesFromAssetPreviewCache.Add(type.ToString(), result);
  453. return result;
  454. }
  455. static string GetImageFileRelativePath(string imageFileName)
  456. {
  457. return Path.Combine(
  458. AssetsPath.GetImagesFolderRelativePath(),
  459. imageFileName);
  460. }
  461. static Texture2D TryLoadImage(string imageFileRelativePath, string image2xFilePath)
  462. {
  463. bool isImageAvailable = File.Exists(Path.GetFullPath(imageFileRelativePath));
  464. if ((EditorGUIUtility.pixelsPerPoint > 1f || !isImageAvailable) && File.Exists(image2xFilePath))
  465. return LoadTextureFromFile(image2xFilePath);
  466. if (isImageAvailable)
  467. return LoadTextureFromFile(imageFileRelativePath);
  468. return null;
  469. }
  470. static Texture2D LoadTextureFromFile(string path)
  471. {
  472. Texture2D result;
  473. if (mImagesByPathCache.TryGetValue(path, out result))
  474. {
  475. if (result != null)
  476. return result;
  477. mImagesByPathCache.Remove(path);
  478. }
  479. byte[] fileData = File.ReadAllBytes(path);
  480. result = new Texture2D(1, 1);
  481. result.LoadImage(fileData); //auto-resizes the texture dimensions
  482. mImagesByPathCache.Add(path, result);
  483. return result;
  484. }
  485. static Dictionary<string, Texture2D> mImagesByPathCache =
  486. new Dictionary<string, Texture2D>();
  487. static Dictionary<string, Texture2D> mImagesFromEditorGUICache =
  488. new Dictionary<string, Texture2D>();
  489. static Dictionary<string, Texture2D> mImagesFromAssetPreviewCache =
  490. new Dictionary<string, Texture2D>();
  491. static Texture mFileIcon;
  492. static Texture mPrivatedOverlayIcon;
  493. static Texture mAddedOverlayIcon;
  494. static Texture mDeletedLocalOverlayIcon;
  495. static Texture mDeletedRemoteOverlayIcon;
  496. static Texture mCheckedOutOverlayIcon;
  497. static Texture mOutOfSyncOverlayIcon;
  498. static Texture mConflictedOverlayIcon;
  499. static Texture mConflictResolvedOverlayIcon;
  500. static Texture mLockedLocalOverlayIcon;
  501. static Texture mLockedRemoteOverlayIcon;
  502. static Texture mIgnoredverlayIcon;
  503. static Texture mRefreshIcon;
  504. static Texture mCloseIcon;
  505. static Texture mClickedCloseIcon;
  506. static Texture mHoveredCloseIcon;
  507. static Texture2D mLinkUnderlineImage;
  508. static Texture2D mEmptyImage;
  509. static Texture2D mTreeviewBackgroundTexture;
  510. static Texture2D mColumnsBackgroundTexture;
  511. static Texture2D mCommentBackground;
  512. static Texture2D mUndoIcon;
  513. static Texture2D mPlasticIcon;
  514. static Texture2D mBranchIcon;
  515. static Texture2D mConflictedIcon;
  516. static Texture2D mOutOfSyncIcon;
  517. static Texture2D mPlasticViewIcon;
  518. static Texture2D mPlasticNotifyIncomingIcon;
  519. static Texture2D mPlasticNotifyConflictIcon;
  520. static Texture2D mEmptyGravatarIcon;
  521. static Texture2D mStepOkIcon;
  522. static Texture2D mStep1Icon;
  523. static Texture2D mStep2Icon;
  524. static Texture2D mMergeLinkIcon;
  525. static Texture2D mAddedLocalIcon;
  526. static Texture2D mDeletedRemoteIcon;
  527. static Texture2D mDeletedIcon;
  528. static Texture2D mMovedIcon;
  529. static Texture2D mRepositoryIcon;
  530. static readonly ILog mLog = LogManager.GetLogger("Images");
  531. }
  532. }