ConsoleLogWindow.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace AmplifyShaderEditor
  7. {
  8. [System.Serializable]
  9. public class Toast
  10. {
  11. public MessageSeverity ItemType;
  12. public string ItemMessage;
  13. public double ItemTime;
  14. public int ItemOwnerId;
  15. public Toast( MessageSeverity itemType, string itemMessage, double itemTime,int itemOwnerId )
  16. {
  17. ItemType = itemType;
  18. ItemMessage = itemMessage;
  19. ItemTime = itemTime;
  20. ItemOwnerId = itemOwnerId;
  21. }
  22. }
  23. public class ConsoleLogWindow
  24. {
  25. public const int MAXWIDTH = 500;
  26. public const float FADETIME = 7;
  27. private readonly GUIContent m_boxToggleContent = new GUIContent( "\u2261", "Toggle Message Box" );
  28. private readonly GUIContent m_clearContent = new GUIContent( "\u00D7", "Clear Messages" );
  29. protected AmplifyShaderEditorWindow m_parentWindow = null;
  30. // needs to be serialized
  31. private Vector2 m_currentScrollPos;
  32. int lastCall = -1;
  33. public ConsoleLogWindow( AmplifyShaderEditorWindow parentWindow )
  34. {
  35. m_parentWindow = parentWindow;
  36. }
  37. public void AddMessage( MessageSeverity itemType, string itemMessage , int itemOwnerId )
  38. {
  39. var toast = new Toast( itemType, itemMessage, Time.realtimeSinceStartup, itemOwnerId );
  40. m_parentWindow.Messages.Insert( 0, toast );
  41. m_currentScrollPos.y = Mathf.Infinity;
  42. if( !m_parentWindow.MaximizeMessages )
  43. lastCall = Mathf.Max( (int)itemType, lastCall );
  44. GUIContent gc = new GUIContent( m_parentWindow.Messages.Count + ": " + itemMessage );
  45. float maxWidth = m_parentWindow.MaxMsgWidth;
  46. maxWidth = Mathf.Max( UIUtils.ConsoleLogLabel.CalcSize( gc ).x + 16, maxWidth );
  47. maxWidth = Mathf.Min( maxWidth, MAXWIDTH );
  48. m_parentWindow.MaxMsgWidth = maxWidth;
  49. }
  50. public void Draw( Rect parentPosition, Vector2 mousePosition, int mouseButtonId, bool hasKeyboadFocus, float rightSide )
  51. {
  52. EventType currentEventType = Event.current.type;
  53. var messages = m_parentWindow.Messages;
  54. var maximize = m_parentWindow.MaximizeMessages;
  55. Rect button = parentPosition;
  56. button.width = 22;
  57. button.height = 22;
  58. button.x = parentPosition.x + parentPosition.width - button.width - rightSide - 8;
  59. button.y = parentPosition.y + parentPosition.height - button.height - ( m_parentWindow.CurrentSelection == ASESelectionMode.Material ? 52 : 8 );
  60. Rect toolbarArea = button;
  61. toolbarArea.y -= 5;
  62. if( maximize )
  63. {
  64. toolbarArea.xMin -= m_parentWindow.MaxMsgWidth;
  65. toolbarArea.yMin -= 66;
  66. }
  67. toolbarArea.x -= 6;
  68. bool needsRepaint = false;
  69. if( maximize )
  70. {
  71. GUIStyle labelStyle = UIUtils.ConsoleLogLabel;
  72. toolbarArea.y -= 16 + 8;
  73. GUILayout.BeginArea( toolbarArea, UIUtils.ConsoleLogMessage );
  74. EditorGUILayout.BeginVertical();
  75. m_currentScrollPos = EditorGUILayout.BeginScrollView( m_currentScrollPos );
  76. {
  77. int count = messages.Count;
  78. for( int i = count - 1; i >= 0; i-- )
  79. {
  80. switch( messages[ i ].ItemType )
  81. {
  82. case MessageSeverity.Error:
  83. labelStyle.normal.textColor = Color.red;
  84. break;
  85. case MessageSeverity.Warning:
  86. labelStyle.normal.textColor = Color.yellow;
  87. break;
  88. default:
  89. case MessageSeverity.Normal:
  90. labelStyle.normal.textColor = Color.white;
  91. break;
  92. }
  93. if( messages[ i ].ItemOwnerId < 0 )
  94. {
  95. if( Event.current.control && Event.current.shift )
  96. {
  97. if( GUILayout.Button( ( count - i ) + ": " + messages[ i ].ItemMessage, labelStyle ) )
  98. {
  99. if( Event.current.button == 1 )
  100. {
  101. EditorGUIUtility.systemCopyBuffer = messages[ i ].ItemMessage;
  102. }
  103. }
  104. }
  105. else
  106. {
  107. GUILayout.Label( ( count - i ) + ": " + messages[ i ].ItemMessage, labelStyle );
  108. }
  109. }
  110. else
  111. {
  112. if( GUILayout.Button( ( count - i ) + ": " + messages[ i ].ItemMessage, labelStyle ) )
  113. {
  114. UIUtils.CurrentWindow.FocusOnNode( messages[ i ].ItemOwnerId, 1, true );
  115. if( Event.current.button == 1 )
  116. {
  117. EditorGUIUtility.systemCopyBuffer = messages[ i ].ItemMessage;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. EditorGUILayout.EndScrollView();
  124. EditorGUILayout.EndVertical();
  125. GUILayout.EndArea();
  126. }
  127. else
  128. {
  129. // draw toaster
  130. int count = messages.Count;
  131. Rect rect = toolbarArea;
  132. rect.xMin -= 200;
  133. float startFade = FADETIME - 1;
  134. for( int i = 0; i < count; i++ )
  135. {
  136. GUIStyle msgstyle = UIUtils.ConsoleLogMessage;
  137. float delta = (float)(Time.realtimeSinceStartup - messages[ i ].ItemTime);
  138. if( delta > FADETIME )
  139. continue;
  140. if( delta < 0.1f )
  141. {
  142. msgstyle.normal.textColor = Color.cyan;
  143. }
  144. else if( delta < startFade )
  145. {
  146. switch( messages[ i ].ItemType )
  147. {
  148. case MessageSeverity.Error:
  149. msgstyle.normal.textColor = Color.red;
  150. break;
  151. case MessageSeverity.Warning:
  152. msgstyle.normal.textColor = Color.yellow;
  153. break;
  154. default:
  155. case MessageSeverity.Normal:
  156. msgstyle.normal.textColor = Color.white;
  157. break;
  158. }
  159. }
  160. else
  161. {
  162. switch( messages[ i ].ItemType )
  163. {
  164. case MessageSeverity.Error:
  165. msgstyle.normal.textColor = new Color( 1, 0, 0, FADETIME - delta );
  166. break;
  167. case MessageSeverity.Warning:
  168. msgstyle.normal.textColor = new Color( 1, 1, 0, FADETIME - delta );
  169. break;
  170. default:
  171. case MessageSeverity.Normal:
  172. msgstyle.normal.textColor = new Color( 1, 1, 1, FADETIME - delta );
  173. break;
  174. }
  175. }
  176. needsRepaint = true;
  177. GUIContent gc = new GUIContent( messages[ i ].ItemMessage );
  178. var sizes = msgstyle.CalcSize( gc );
  179. rect.xMin -= sizes.x - rect.width;
  180. rect.height = sizes.y;
  181. rect.y -= rect.height + 2;
  182. if( messages[ i ].ItemOwnerId < 0 )
  183. {
  184. GUI.Label( rect, gc, msgstyle );
  185. }
  186. else
  187. {
  188. if( GUI.Button( rect, gc, msgstyle ))
  189. {
  190. UIUtils.CurrentWindow.FocusOnNode( messages[ i ].ItemOwnerId, 1, true );
  191. if( Event.current.button == 1 )
  192. {
  193. EditorGUIUtility.systemCopyBuffer = messages[ i ].ItemMessage;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. //GUI.color = cached;
  200. if( needsRepaint )
  201. m_parentWindow.MarkToRepaint();
  202. GUIStyle style = UIUtils.ConsoleLogCircle;
  203. button.size = Vector2.one * 16;
  204. switch( lastCall )
  205. {
  206. case 0:
  207. style.normal.textColor = Color.cyan;
  208. break;
  209. case 1:
  210. style.normal.textColor = Color.yellow;
  211. break;
  212. case 2:
  213. style.normal.textColor = Color.red;
  214. break;
  215. default:
  216. style.normal.textColor = new Color( 1, 1, 1, 0.5f );
  217. break;
  218. }
  219. if( GUI.Button( button, m_boxToggleContent, style ) )
  220. {
  221. maximize = !maximize;
  222. m_parentWindow.MaximizeMessages = maximize;
  223. m_currentScrollPos.y = Mathf.Infinity;
  224. lastCall = -1;
  225. }
  226. style.normal.textColor = new Color( 1, 1, 1, 0.5f );
  227. //GUI.color = cached;
  228. button.x -= button.width + 2;
  229. if( maximize && GUI.Button( button, m_clearContent, style ) )
  230. {
  231. if( messages.Count == 0 )
  232. {
  233. maximize = false;
  234. m_parentWindow.MaximizeMessages = maximize;
  235. }
  236. ClearMessages();
  237. }
  238. button.width += button.width + 2;
  239. bool mouseOnTop = button.Contains( mousePosition );
  240. if( currentEventType == EventType.MouseMove && mouseOnTop )
  241. m_parentWindow.MarkToRepaint();
  242. if( DebugConsoleWindow.DeveloperMode )
  243. {
  244. if( Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha1 )
  245. {
  246. UIUtils.ShowMessage( "This is an info message\nwith two lines" );
  247. }
  248. if( Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha2 )
  249. {
  250. UIUtils.ShowMessage( "This is a warning message", MessageSeverity.Warning );
  251. }
  252. if( Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha3 )
  253. {
  254. UIUtils.ShowMessage( "THIS IS AN ERROR MESSAGE!!", MessageSeverity.Error );
  255. }
  256. }
  257. }
  258. public void ClearMessages()
  259. {
  260. m_parentWindow.Messages.Clear();
  261. m_parentWindow.MaxMsgWidth = MAXWIDTH;
  262. }
  263. public void Toggle()
  264. {
  265. }
  266. public void Destroy()
  267. {
  268. m_parentWindow = null;
  269. }
  270. }
  271. }