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.

DrawHelpPanel.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. Texture image = Images.GetHelpImage(helpPanel.Image);
  62. var imgRect = GUILayoutUtility.GetRect(
  63. image.width, image.width, image.height, image.height);
  64. GUI.DrawTexture(imgRect, image, ScaleMode.ScaleToFit);
  65. using (new EditorGUILayout.VerticalScope())
  66. {
  67. GUIStyle helpParagraph = UnityStyles.Paragraph;
  68. helpPanel.TextScroll = GUILayout.BeginScrollView(helpPanel.TextScroll);
  69. GUILayout.Label(helpPanel.GUIContent, helpParagraph);
  70. if (Event.current.type != EventType.Layout)
  71. DoHelpPanelLinks(helpPanel, helpParagraph);
  72. GUILayout.EndScrollView();
  73. Rect scrollRect = GUILayoutUtility.GetLastRect();
  74. if (Mouse.IsRightMouseButtonPressed(Event.current) &&
  75. scrollRect.Contains(Event.current.mousePosition))
  76. {
  77. GenericMenu contextMenu = BuildHelpPanelMenu(helpPanel.Data.CleanText);
  78. contextMenu.ShowAsContext();
  79. }
  80. }
  81. }
  82. }
  83. static void DoHelpPanelLinks(
  84. HelpPanel helpPanel,
  85. GUIStyle helpParagraph)
  86. {
  87. var lastRect = GUILayoutUtility.GetLastRect();
  88. bool linkWasClicked = false;
  89. GUIContent charContent = new GUIContent();
  90. for (int charIdx = 0; charIdx < helpPanel.GUIContent.text.Length; charIdx++)
  91. {
  92. HelpLink link;
  93. if (!helpPanel.TryGetLinkAtChar(charIdx, out link))
  94. continue;
  95. charContent.text = helpPanel.GUIContent.text[charIdx].ToString();
  96. var pos = helpParagraph.GetCursorPixelPosition(
  97. lastRect, helpPanel.GUIContent, charIdx);
  98. float charWidth = helpParagraph.CalcSize(charContent).x;
  99. Rect charRect = new Rect(pos, new Vector2(
  100. charWidth - 4, helpParagraph.lineHeight));
  101. if (!linkWasClicked &&
  102. Mouse.IsLeftMouseButtonPressed(Event.current) &&
  103. charRect.Contains(Event.current.mousePosition))
  104. {
  105. linkWasClicked = true;
  106. OnHelpLinkClicked(helpPanel, link);
  107. }
  108. // Underline for links
  109. charRect.y = charRect.yMax - 1;
  110. charRect.height = 1;
  111. GUI.DrawTexture(charRect, Images.GetLinkUnderlineImage());
  112. }
  113. }
  114. static void OnHelpLinkClicked(
  115. HelpPanel helpPanel,
  116. HelpLink helpLink)
  117. {
  118. HelpLink.LinkType linkType;
  119. string content;
  120. if (!HelpLinkData.TryGet(helpLink.Link, out linkType, out content))
  121. return;
  122. switch (linkType)
  123. {
  124. case HelpLink.LinkType.Action:
  125. GuiMessage.ShowInformation(
  126. "An ACTION link has been clicked:\n" + content);
  127. break;
  128. case HelpLink.LinkType.Help:
  129. helpPanel.Show(
  130. PlasticGui.Help.HelpImage.GenericBuho,
  131. content == "sample1" ?
  132. TestingHelpData.GetSample1() :
  133. TestingHelpData.GetSample2());
  134. break;
  135. case HelpLink.LinkType.Link:
  136. Process.Start(content);
  137. break;
  138. }
  139. }
  140. static void CopyToClipboard(string data)
  141. {
  142. EditorGUIUtility.systemCopyBuffer = data;
  143. }
  144. static GenericMenu BuildHelpPanelMenu(string cleanText)
  145. {
  146. GenericMenu result = new GenericMenu();
  147. result.AddItem(
  148. new GUIContent(PlasticLocalization.GetString(PlasticLocalization.Name.Copy)),
  149. false,
  150. () => CopyToClipboard(cleanText)
  151. );
  152. return result;
  153. }
  154. }
  155. }