暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DrawHelpPanel.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System.Diagnostics;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using Codice.Client.Common;
  5. using PlasticGui;
  6. using Unity.PlasticSCM.Editor.UI;
  7. namespace Unity.PlasticSCM.Editor.Help
  8. {
  9. internal static class DrawHelpPanel
  10. {
  11. internal static void For(
  12. HelpPanel helpPanel)
  13. {
  14. if (!helpPanel.Visible)
  15. return;
  16. DoHelpPanelToolbar(helpPanel);
  17. GUILayout.Space(10);
  18. DoHelpPanelContent(helpPanel);
  19. }
  20. static void DoHelpPanelToolbar(
  21. HelpPanel helpPanel)
  22. {
  23. Rect rect = GUILayoutUtility.GetLastRect();
  24. rect.y = rect.yMax;
  25. rect.height = 22;
  26. GUILayout.Space(1);
  27. GUIStyle expandableToolbar = new GUIStyle(EditorStyles.toolbar);
  28. expandableToolbar.fixedHeight = 0;
  29. GUI.Label(rect, GUIContent.none, expandableToolbar);
  30. using (new EditorGUILayout.HorizontalScope())
  31. {
  32. if (GUILayout.Button("<", EditorStyles.miniButtonLeft))
  33. {
  34. // TODO(codice): On Left Clicked
  35. }
  36. if (GUILayout.Button(">", EditorStyles.miniButtonRight))
  37. {
  38. // TODO(codice): On Right Clicked
  39. }
  40. GUILayout.FlexibleSpace();
  41. // TODO(codice): The bool used here must be loaded and persisted by some means
  42. helpPanel.Data.ShouldShowAgain = EditorGUILayout.ToggleLeft(
  43. PlasticLocalization.GetString(PlasticLocalization.Name.DontShowItAgain),
  44. helpPanel.Data.ShouldShowAgain, UnityStyles.MiniToggle);
  45. bool okWasPressed = GUILayout.Button(
  46. PlasticLocalization.GetString(PlasticLocalization.Name.OkButton),
  47. EditorStyles.miniButton);
  48. if (okWasPressed)
  49. {
  50. helpPanel.Hide();
  51. // TODO(codice): Do on helppanel dismiss actions
  52. return;
  53. }
  54. }
  55. }
  56. static void DoHelpPanelContent(
  57. HelpPanel helpPanel)
  58. {
  59. using (new EditorGUILayout.HorizontalScope())
  60. {
  61. using (new EditorGUILayout.VerticalScope())
  62. {
  63. GUIStyle helpParagraph = UnityStyles.Paragraph;
  64. helpPanel.TextScroll = GUILayout.BeginScrollView(helpPanel.TextScroll);
  65. GUILayout.Label(helpPanel.GUIContent, helpParagraph);
  66. if (Event.current.type != EventType.Layout)
  67. DoHelpPanelLinks(helpPanel, helpParagraph);
  68. GUILayout.EndScrollView();
  69. Rect scrollRect = GUILayoutUtility.GetLastRect();
  70. if (Mouse.IsRightMouseButtonPressed(Event.current) &&
  71. scrollRect.Contains(Event.current.mousePosition))
  72. {
  73. GenericMenu contextMenu = BuildHelpPanelMenu(helpPanel.Data.CleanText);
  74. contextMenu.ShowAsContext();
  75. }
  76. }
  77. }
  78. }
  79. static void DoHelpPanelLinks(
  80. HelpPanel helpPanel,
  81. GUIStyle helpParagraph)
  82. {
  83. var lastRect = GUILayoutUtility.GetLastRect();
  84. bool linkWasClicked = false;
  85. GUIContent charContent = new GUIContent();
  86. for (int charIdx = 0; charIdx < helpPanel.GUIContent.text.Length; charIdx++)
  87. {
  88. HelpLink link;
  89. if (!helpPanel.TryGetLinkAtChar(charIdx, out link))
  90. continue;
  91. charContent.text = helpPanel.GUIContent.text[charIdx].ToString();
  92. var pos = helpParagraph.GetCursorPixelPosition(
  93. lastRect, helpPanel.GUIContent, charIdx);
  94. float charWidth = helpParagraph.CalcSize(charContent).x;
  95. Rect charRect = new Rect(pos, new Vector2(
  96. charWidth - 4, helpParagraph.lineHeight));
  97. if (!linkWasClicked &&
  98. Mouse.IsLeftMouseButtonPressed(Event.current) &&
  99. charRect.Contains(Event.current.mousePosition))
  100. {
  101. linkWasClicked = true;
  102. OnHelpLinkClicked(helpPanel, link);
  103. }
  104. // Underline for links
  105. charRect.y = charRect.yMax - 1;
  106. charRect.height = 1;
  107. GUI.DrawTexture(charRect, Images.GetLinkUnderlineImage());
  108. }
  109. }
  110. static void OnHelpLinkClicked(
  111. HelpPanel helpPanel,
  112. HelpLink helpLink)
  113. {
  114. HelpLink.LinkType linkType;
  115. string content;
  116. if (!HelpLinkData.TryGet(helpLink.Link, out linkType, out content))
  117. return;
  118. switch (linkType)
  119. {
  120. case HelpLink.LinkType.Action:
  121. GuiMessage.ShowInformation(
  122. "An ACTION link has been clicked:\n" + content);
  123. break;
  124. case HelpLink.LinkType.Help:
  125. helpPanel.Show(
  126. content == "sample1" ?
  127. TestingHelpData.GetSample1() :
  128. TestingHelpData.GetSample2());
  129. break;
  130. case HelpLink.LinkType.Link:
  131. Process.Start(content);
  132. break;
  133. }
  134. }
  135. static void CopyToClipboard(string data)
  136. {
  137. EditorGUIUtility.systemCopyBuffer = data;
  138. }
  139. static GenericMenu BuildHelpPanelMenu(string cleanText)
  140. {
  141. GenericMenu result = new GenericMenu();
  142. result.AddItem(
  143. new GUIContent(PlasticLocalization.GetString(PlasticLocalization.Name.Copy)),
  144. false,
  145. () => CopyToClipboard(cleanText)
  146. );
  147. return result;
  148. }
  149. }
  150. }