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.

215 lines
6.8 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. namespace AmplifyShaderEditor
  6. {
  7. public class ShortcutItem
  8. {
  9. public delegate void ShortcutFunction();
  10. public ShortcutFunction MyKeyDownFunctionPtr;
  11. public ShortcutFunction MyKeyUpFunctionPtr;
  12. public string Name;
  13. public string Description;
  14. public ShortcutItem( string name, string description )
  15. {
  16. Name = name;
  17. Description = description;
  18. }
  19. public ShortcutItem( string name, string description, ShortcutFunction myKeyDownFunctionPtr, ShortcutFunction myKeyUpFunctionPtr = null )
  20. {
  21. Name = name;
  22. Description = description;
  23. MyKeyDownFunctionPtr = myKeyDownFunctionPtr;
  24. MyKeyUpFunctionPtr = myKeyUpFunctionPtr;
  25. }
  26. public void Destroy()
  27. {
  28. MyKeyDownFunctionPtr = null;
  29. MyKeyUpFunctionPtr = null;
  30. }
  31. }
  32. public class ShortcutsManager
  33. {
  34. public static readonly KeyCode ScrollUpKey = KeyCode.PageUp;
  35. public static readonly KeyCode ScrollDownKey = KeyCode.PageDown;
  36. private const string ItemWikiFormat = "*<b>[{0}]:</b> {1}\n";
  37. private Dictionary<KeyCode, Dictionary<EventModifiers, ShortcutItem>> m_editorShortcutsDict = new Dictionary<KeyCode, Dictionary<EventModifiers, ShortcutItem>>();
  38. private Dictionary<KeyCode, ShortcutItem> m_editorNoModifiersShortcutsDict = new Dictionary<KeyCode, ShortcutItem>();
  39. private List<ShortcutItem> m_editorShortcutsList = new List<ShortcutItem>();
  40. private Dictionary<KeyCode, ShortcutItem> m_nodesShortcutsDict = new Dictionary<KeyCode, ShortcutItem>();
  41. private List<ShortcutItem> m_nodesShortcutsList = new List<ShortcutItem>();
  42. public void DumpShortcutsToDisk( string pathname )
  43. {
  44. if ( !System.IO.Directory.Exists( pathname ) )
  45. {
  46. System.IO.Directory.CreateDirectory( pathname );
  47. }
  48. string list = "=== Full Shortcut List ===\n";
  49. list += "==== Editor ====\n";
  50. for ( int i = 0; i < m_editorShortcutsList.Count; i++ )
  51. {
  52. list += string.Format( ItemWikiFormat, m_editorShortcutsList[ i ].Name, m_editorShortcutsList[ i ].Description );
  53. }
  54. list += "\n";
  55. list += "==== Nodes ====\n";
  56. for ( int i = 0; i < m_nodesShortcutsList.Count; i++ )
  57. {
  58. list += string.Format( ItemWikiFormat, m_nodesShortcutsList[ i ].Name, m_nodesShortcutsList[ i ].Description );
  59. }
  60. string shortcutsPathnames = pathname + "KeyboardShortcuts.txt";
  61. Debug.Log( " Creating shortcuts file at " + shortcutsPathnames );
  62. IOUtils.SaveTextfileToDisk( list, shortcutsPathnames, false );
  63. }
  64. public void RegisterNodesShortcuts( KeyCode key, string nodeName )
  65. {
  66. if ( m_nodesShortcutsDict.ContainsKey( key ) )
  67. {
  68. if ( DebugConsoleWindow.DeveloperMode )
  69. {
  70. Debug.Log( "Attempting to register an already used node shortcut key " + key );
  71. }
  72. return;
  73. }
  74. m_nodesShortcutsDict.Add( key, new ShortcutItem( key.ToString(), nodeName ) );
  75. m_nodesShortcutsList.Add( m_nodesShortcutsDict[ key ] );
  76. }
  77. public void RegisterEditorShortcut( bool showOnList, EventModifiers modifiers, KeyCode key, string description, ShortcutItem.ShortcutFunction myKeyDownFunctionPtr, ShortcutItem.ShortcutFunction myKeyUpFunctionPtr = null )
  78. {
  79. if ( m_editorShortcutsDict.ContainsKey( key ) )
  80. {
  81. if ( m_editorShortcutsDict[ key ].ContainsKey( modifiers ) )
  82. {
  83. if ( DebugConsoleWindow.DeveloperMode )
  84. {
  85. Debug.Log( "Attempting to register an already used editor shortcut key " + key );
  86. }
  87. return;
  88. }
  89. }
  90. else
  91. {
  92. m_editorShortcutsDict.Add( key, new Dictionary<EventModifiers, ShortcutItem>() );
  93. }
  94. ShortcutItem item = new ShortcutItem( ( ( modifiers == EventModifiers.None || modifiers == EventModifiers.FunctionKey ) ? key.ToString() : modifiers + " + " + key ), description, myKeyDownFunctionPtr, myKeyUpFunctionPtr );
  95. m_editorShortcutsDict[ key ].Add( modifiers, item );
  96. if ( showOnList )
  97. m_editorShortcutsList.Add( item );
  98. }
  99. public void RegisterEditorShortcut( bool showOnList, KeyCode key, string description, ShortcutItem.ShortcutFunction myKeyDownFunctionPtr, ShortcutItem.ShortcutFunction myKeyUpFunctionPtr = null )
  100. {
  101. if ( m_editorNoModifiersShortcutsDict.ContainsKey( key ) )
  102. {
  103. if ( DebugConsoleWindow.DeveloperMode )
  104. {
  105. Debug.Log( "Attempting to register an already used editor shortcut key " + key );
  106. }
  107. return;
  108. }
  109. ShortcutItem item = new ShortcutItem( key.ToString(), description, myKeyDownFunctionPtr, myKeyUpFunctionPtr );
  110. m_editorNoModifiersShortcutsDict.Add( key, item );
  111. if ( showOnList )
  112. m_editorShortcutsList.Add( item );
  113. }
  114. public bool ActivateShortcut( EventModifiers modifiers, KeyCode key, bool isKeyDown )
  115. {
  116. if ( m_editorShortcutsDict.ContainsKey( key ) )
  117. {
  118. if ( isKeyDown )
  119. {
  120. if ( m_editorShortcutsDict[ key ].ContainsKey( modifiers ) )
  121. {
  122. if ( m_editorShortcutsDict[ key ][ modifiers ].MyKeyDownFunctionPtr != null )
  123. {
  124. m_editorShortcutsDict[ key ][ modifiers ].MyKeyDownFunctionPtr();
  125. return true;
  126. }
  127. }
  128. }
  129. else
  130. {
  131. if ( m_editorShortcutsDict[ key ].ContainsKey( modifiers ) )
  132. {
  133. if ( m_editorShortcutsDict[ key ][ modifiers ].MyKeyUpFunctionPtr != null )
  134. {
  135. m_editorShortcutsDict[ key ][ modifiers ].MyKeyUpFunctionPtr();
  136. return true;
  137. }
  138. }
  139. }
  140. }
  141. if ( modifiers == EventModifiers.None && m_editorNoModifiersShortcutsDict.ContainsKey( key ) )
  142. {
  143. if ( isKeyDown )
  144. {
  145. if ( m_editorNoModifiersShortcutsDict[ key ].MyKeyDownFunctionPtr != null )
  146. {
  147. m_editorNoModifiersShortcutsDict[ key ].MyKeyDownFunctionPtr();
  148. return true;
  149. }
  150. }
  151. else
  152. {
  153. if ( m_editorNoModifiersShortcutsDict[ key ].MyKeyUpFunctionPtr != null )
  154. {
  155. m_editorNoModifiersShortcutsDict[ key ].MyKeyUpFunctionPtr();
  156. return true;
  157. }
  158. }
  159. }
  160. return false;
  161. }
  162. public void Destroy()
  163. {
  164. foreach ( KeyValuePair<KeyCode, ShortcutItem> kvp in m_editorNoModifiersShortcutsDict )
  165. {
  166. kvp.Value.Destroy();
  167. }
  168. m_editorNoModifiersShortcutsDict.Clear();
  169. m_editorNoModifiersShortcutsDict = null;
  170. foreach ( KeyValuePair<KeyCode, Dictionary<EventModifiers, ShortcutItem>> kvpKey in m_editorShortcutsDict )
  171. {
  172. foreach ( KeyValuePair<EventModifiers, ShortcutItem> kvpMod in kvpKey.Value )
  173. {
  174. kvpMod.Value.Destroy();
  175. }
  176. kvpKey.Value.Clear();
  177. }
  178. m_editorShortcutsDict.Clear();
  179. m_editorShortcutsDict = null;
  180. m_editorShortcutsList.Clear();
  181. m_editorShortcutsList = null;
  182. m_nodesShortcutsDict.Clear();
  183. m_nodesShortcutsDict = null;
  184. m_nodesShortcutsList.Clear();
  185. m_nodesShortcutsList = null;
  186. }
  187. public List<ShortcutItem> AvailableEditorShortcutsList { get { return m_editorShortcutsList; } }
  188. public List<ShortcutItem> AvailableNodesShortcutsList { get { return m_nodesShortcutsList; } }
  189. }
  190. }