Assignment for RMIT Mixed Reality in 2020
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.

296 lines
7.8 KiB

  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 = 400;
  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( GUIStyle.none.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. GUILayout.Label( ( count - i ) + ": " + messages[ i ].ItemMessage, labelStyle );
  96. }
  97. else
  98. {
  99. if( GUILayout.Button( ( count - i ) + ": " + messages[ i ].ItemMessage, labelStyle ) )
  100. {
  101. UIUtils.CurrentWindow.FocusOnNode( messages[ i ].ItemOwnerId, 1, true );
  102. if( Event.current.button == 1 )
  103. {
  104. EditorGUIUtility.systemCopyBuffer = messages[ i ].ItemMessage;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. EditorGUILayout.EndScrollView();
  111. EditorGUILayout.EndVertical();
  112. GUILayout.EndArea();
  113. }
  114. else
  115. {
  116. // draw toaster
  117. int count = messages.Count;
  118. Rect rect = toolbarArea;
  119. rect.xMin -= 200;
  120. float startFade = FADETIME - 1;
  121. for( int i = 0; i < count; i++ )
  122. {
  123. GUIStyle msgstyle = UIUtils.ConsoleLogMessage;
  124. float delta = (float)(Time.realtimeSinceStartup - messages[ i ].ItemTime);
  125. if( delta > FADETIME )
  126. continue;
  127. if( delta < 0.1f )
  128. {
  129. msgstyle.normal.textColor = Color.cyan;
  130. }
  131. else if( delta < startFade )
  132. {
  133. switch( messages[ i ].ItemType )
  134. {
  135. case MessageSeverity.Error:
  136. msgstyle.normal.textColor = Color.red;
  137. break;
  138. case MessageSeverity.Warning:
  139. msgstyle.normal.textColor = Color.yellow;
  140. break;
  141. default:
  142. case MessageSeverity.Normal:
  143. msgstyle.normal.textColor = Color.white;
  144. break;
  145. }
  146. }
  147. else
  148. {
  149. switch( messages[ i ].ItemType )
  150. {
  151. case MessageSeverity.Error:
  152. msgstyle.normal.textColor = new Color( 1, 0, 0, FADETIME - delta );
  153. break;
  154. case MessageSeverity.Warning:
  155. msgstyle.normal.textColor = new Color( 1, 1, 0, FADETIME - delta );
  156. break;
  157. default:
  158. case MessageSeverity.Normal:
  159. msgstyle.normal.textColor = new Color( 1, 1, 1, FADETIME - delta );
  160. break;
  161. }
  162. }
  163. needsRepaint = true;
  164. GUIContent gc = new GUIContent( messages[ i ].ItemMessage );
  165. var sizes = msgstyle.CalcSize( gc );
  166. rect.xMin -= sizes.x - rect.width;
  167. rect.height = sizes.y;
  168. rect.y -= rect.height + 2;
  169. if( messages[ i ].ItemOwnerId < 0 )
  170. {
  171. GUI.Label( rect, gc, msgstyle );
  172. }
  173. else
  174. {
  175. if( GUI.Button( rect, gc, msgstyle ))
  176. {
  177. UIUtils.CurrentWindow.FocusOnNode( messages[ i ].ItemOwnerId, 1, true );
  178. if( Event.current.button == 1 )
  179. {
  180. EditorGUIUtility.systemCopyBuffer = messages[ i ].ItemMessage;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. //GUI.color = cached;
  187. if( needsRepaint )
  188. m_parentWindow.MarkToRepaint();
  189. GUIStyle style = UIUtils.ConsoleLogCircle;
  190. button.size = Vector2.one * 16;
  191. switch( lastCall )
  192. {
  193. case 0:
  194. style.normal.textColor = Color.cyan;
  195. break;
  196. case 1:
  197. style.normal.textColor = Color.yellow;
  198. break;
  199. case 2:
  200. style.normal.textColor = Color.red;
  201. break;
  202. default:
  203. style.normal.textColor = new Color( 1, 1, 1, 0.5f );
  204. break;
  205. }
  206. if( GUI.Button( button, m_boxToggleContent, style ) )
  207. {
  208. maximize = !maximize;
  209. m_parentWindow.MaximizeMessages = maximize;
  210. m_currentScrollPos.y = Mathf.Infinity;
  211. lastCall = -1;
  212. }
  213. style.normal.textColor = new Color( 1, 1, 1, 0.5f );
  214. //GUI.color = cached;
  215. button.x -= button.width + 2;
  216. if( maximize && GUI.Button( button, m_clearContent, style ) )
  217. {
  218. if( messages.Count == 0 )
  219. {
  220. maximize = false;
  221. m_parentWindow.MaximizeMessages = maximize;
  222. }
  223. ClearMessages();
  224. }
  225. button.width += button.width + 2;
  226. bool mouseOnTop = button.Contains( mousePosition );
  227. if( currentEventType == EventType.MouseMove && mouseOnTop )
  228. m_parentWindow.MarkToRepaint();
  229. if( DebugConsoleWindow.DeveloperMode )
  230. {
  231. if( Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha1 )
  232. {
  233. UIUtils.ShowMessage( "This is an info message\nwith two lines" );
  234. }
  235. if( Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha2 )
  236. {
  237. UIUtils.ShowMessage( "This is a warning message", MessageSeverity.Warning );
  238. }
  239. if( Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha3 )
  240. {
  241. UIUtils.ShowMessage( "THIS IS AN ERROR MESSAGE!!", MessageSeverity.Error );
  242. }
  243. }
  244. }
  245. public void ClearMessages()
  246. {
  247. m_parentWindow.Messages.Clear();
  248. m_parentWindow.MaxMsgWidth = 100;
  249. }
  250. public void Toggle()
  251. {
  252. }
  253. public void Destroy()
  254. {
  255. m_parentWindow = null;
  256. }
  257. }
  258. }