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.

PlasticQuestionAlert.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using Codice.Client.Common;
  5. using PlasticGui;
  6. namespace Unity.PlasticSCM.Editor.UI.Message
  7. {
  8. internal class PlasticQuestionAlert : PlasticDialog
  9. {
  10. protected override Rect DefaultRect
  11. {
  12. get
  13. {
  14. var baseRect = base.DefaultRect;
  15. string buttonsText = mFirst + mSecond + (mThird ?? string.Empty);
  16. int textWidth = (int)((GUIStyle)UnityStyles.Dialog.AcceptButtonText)
  17. .CalcSize(new GUIContent(buttonsText)).x;
  18. return new Rect(
  19. baseRect.x, baseRect.y,
  20. Math.Max(500, textWidth + 150), 180);
  21. }
  22. }
  23. internal static ResponseType Show(
  24. string title,
  25. string message, string first,
  26. string second, string third,
  27. bool isFirstButtonEnabled,
  28. GuiMessage.GuiMessageType alertType,
  29. EditorWindow parentWindow)
  30. {
  31. PlasticQuestionAlert alert = Create(
  32. title, message, first, second, third,
  33. isFirstButtonEnabled, alertType);
  34. return alert.RunModal(parentWindow);
  35. }
  36. protected override void OnModalGUI()
  37. {
  38. DoMessageArea();
  39. GUILayout.FlexibleSpace();
  40. GUILayout.Space(20);
  41. DoButtonsArea();
  42. }
  43. protected override string GetTitle()
  44. {
  45. return PlasticLocalization.GetString(
  46. PlasticLocalization.Name.UnityVersionControl);
  47. }
  48. void DoMessageArea()
  49. {
  50. using (new EditorGUILayout.HorizontalScope())
  51. {
  52. DrawDialogIcon.ForMessage(mAlertType);
  53. using (new EditorGUILayout.VerticalScope())
  54. {
  55. GUILayout.Label(mTitle, UnityStyles.Dialog.MessageTitle);
  56. GUIContent message = new GUIContent(mMessage);
  57. Rect lastRect = GUILayoutUtility.GetLastRect();
  58. GUIStyle scrollPlaceholder = new GUIStyle(UnityStyles.Dialog.MessageText);
  59. scrollPlaceholder.normal.textColor = Color.clear;
  60. scrollPlaceholder.clipping = TextClipping.Clip;
  61. if (Event.current.type == EventType.Repaint)
  62. {
  63. mMessageDesiredHeight = ((GUIStyle)UnityStyles.Dialog.MessageText)
  64. .CalcHeight(message, lastRect.width - 20) + 20;
  65. mMessageViewHeight = Mathf.Min(mMessageDesiredHeight, 60);
  66. }
  67. GUILayout.Space(mMessageViewHeight);
  68. Rect scrollPanelRect = new Rect(
  69. lastRect.xMin, lastRect.yMax,
  70. lastRect.width + 20, mMessageViewHeight);
  71. Rect contentRect = new Rect(
  72. scrollPanelRect.xMin,
  73. scrollPanelRect.yMin,
  74. scrollPanelRect.width - 20,
  75. mMessageDesiredHeight);
  76. mScroll = GUI.BeginScrollView(scrollPanelRect, mScroll, contentRect);
  77. GUI.Label(contentRect, mMessage, UnityStyles.Dialog.MessageText);
  78. GUI.EndScrollView();
  79. }
  80. }
  81. }
  82. void DoButtonsArea()
  83. {
  84. using (new EditorGUILayout.HorizontalScope())
  85. {
  86. GUILayout.FlexibleSpace();
  87. if (Application.platform == RuntimePlatform.WindowsEditor)
  88. {
  89. DoFirstButton();
  90. DoSecondButton();
  91. DoThirdButton();
  92. return;
  93. }
  94. DoThirdButton();
  95. DoSecondButton();
  96. DoFirstButton();
  97. }
  98. }
  99. void DoFirstButton()
  100. {
  101. GUI.enabled = mIsFirstButtonEnabled;
  102. bool pressed = mIsFirstButtonEnabled ?
  103. AcceptButton(mFirst) :
  104. NormalButton(mFirst);
  105. GUI.enabled = true;
  106. if (!pressed)
  107. return;
  108. OkButtonAction();
  109. }
  110. void DoSecondButton()
  111. {
  112. if (!NormalButton(mSecond))
  113. return;
  114. CancelButtonAction();
  115. }
  116. void DoThirdButton()
  117. {
  118. if (mThird == null)
  119. return;
  120. bool pressed = mIsFirstButtonEnabled ?
  121. NormalButton(mThird) :
  122. AcceptButton(mThird);
  123. if (!pressed)
  124. return;
  125. ApplyButtonAction();
  126. }
  127. static PlasticQuestionAlert Create(
  128. string title, string message, string first,
  129. string second, string third, bool isFirstButtonEnabled,
  130. GuiMessage.GuiMessageType alertType)
  131. {
  132. var instance = CreateInstance<PlasticQuestionAlert>();
  133. instance.titleContent = new GUIContent(title);
  134. instance.mTitle = title;
  135. instance.mMessage = message;
  136. instance.mFirst = first;
  137. instance.mSecond = second;
  138. instance.mThird = third;
  139. instance.mIsFirstButtonEnabled = isFirstButtonEnabled;
  140. instance.mAlertType = alertType;
  141. instance.mEnterKeyAction = GetEnterKeyAction(isFirstButtonEnabled, instance);
  142. instance.mEscapeKeyAction = instance.CancelButtonAction;
  143. return instance;
  144. }
  145. static Action GetEnterKeyAction(
  146. bool isFirstButtonEnabled,
  147. PlasticQuestionAlert instance)
  148. {
  149. if (isFirstButtonEnabled)
  150. return instance.OkButtonAction;
  151. return instance.ApplyButtonAction;
  152. }
  153. string mTitle;
  154. string mMessage, mFirst, mSecond, mThird;
  155. bool mIsFirstButtonEnabled;
  156. GuiMessage.GuiMessageType mAlertType;
  157. Vector2 mScroll;
  158. float mMessageDesiredHeight;
  159. float mMessageViewHeight;
  160. }
  161. }