From 917e9e0b320775634dc2e710f7deac74fd0822f0 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 22 Oct 2020 23:30:02 +0800 Subject: * amplify shader editor --- .../Plugins/Editor/Utils/ShortcutsManager.cs | 215 +++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 Assets/AmplifyShaderEditor/Plugins/Editor/Utils/ShortcutsManager.cs (limited to 'Assets/AmplifyShaderEditor/Plugins/Editor/Utils/ShortcutsManager.cs') diff --git a/Assets/AmplifyShaderEditor/Plugins/Editor/Utils/ShortcutsManager.cs b/Assets/AmplifyShaderEditor/Plugins/Editor/Utils/ShortcutsManager.cs new file mode 100644 index 00000000..3e48aa82 --- /dev/null +++ b/Assets/AmplifyShaderEditor/Plugins/Editor/Utils/ShortcutsManager.cs @@ -0,0 +1,215 @@ +// Amplify Shader Editor - Visual Shader Editing Tool +// Copyright (c) Amplify Creations, Lda + +using UnityEngine; +using System.Collections.Generic; + +namespace AmplifyShaderEditor +{ + public class ShortcutItem + { + public delegate void ShortcutFunction(); + public ShortcutFunction MyKeyDownFunctionPtr; + public ShortcutFunction MyKeyUpFunctionPtr; + public string Name; + public string Description; + + public ShortcutItem( string name, string description ) + { + Name = name; + Description = description; + } + + public ShortcutItem( string name, string description, ShortcutFunction myKeyDownFunctionPtr, ShortcutFunction myKeyUpFunctionPtr = null ) + { + Name = name; + Description = description; + MyKeyDownFunctionPtr = myKeyDownFunctionPtr; + MyKeyUpFunctionPtr = myKeyUpFunctionPtr; + } + + public void Destroy() + { + MyKeyDownFunctionPtr = null; + MyKeyUpFunctionPtr = null; + } + } + + public class ShortcutsManager + { + public static readonly KeyCode ScrollUpKey = KeyCode.PageUp; + public static readonly KeyCode ScrollDownKey = KeyCode.PageDown; + + + private const string ItemWikiFormat = "*[{0}]: {1}\n"; + private Dictionary> m_editorShortcutsDict = new Dictionary>(); + private Dictionary m_editorNoModifiersShortcutsDict = new Dictionary(); + private List m_editorShortcutsList = new List(); + + private Dictionary m_nodesShortcutsDict = new Dictionary(); + private List m_nodesShortcutsList = new List(); + + public void DumpShortcutsToDisk( string pathname ) + { + if ( !System.IO.Directory.Exists( pathname ) ) + { + System.IO.Directory.CreateDirectory( pathname ); + } + + string list = "=== Full Shortcut List ===\n"; + list += "==== Editor ====\n"; + for ( int i = 0; i < m_editorShortcutsList.Count; i++ ) + { + list += string.Format( ItemWikiFormat, m_editorShortcutsList[ i ].Name, m_editorShortcutsList[ i ].Description ); + } + list += "\n"; + list += "==== Nodes ====\n"; + for ( int i = 0; i < m_nodesShortcutsList.Count; i++ ) + { + list += string.Format( ItemWikiFormat, m_nodesShortcutsList[ i ].Name, m_nodesShortcutsList[ i ].Description ); + } + + string shortcutsPathnames = pathname + "KeyboardShortcuts.txt"; + Debug.Log( " Creating shortcuts file at " + shortcutsPathnames ); + IOUtils.SaveTextfileToDisk( list, shortcutsPathnames, false ); + } + + public void RegisterNodesShortcuts( KeyCode key, string nodeName ) + { + if ( m_nodesShortcutsDict.ContainsKey( key ) ) + { + if ( DebugConsoleWindow.DeveloperMode ) + { + Debug.Log( "Attempting to register an already used node shortcut key " + key ); + } + return; + } + m_nodesShortcutsDict.Add( key, new ShortcutItem( key.ToString(), nodeName ) ); + m_nodesShortcutsList.Add( m_nodesShortcutsDict[ key ] ); + } + + public void RegisterEditorShortcut( bool showOnList, EventModifiers modifiers, KeyCode key, string description, ShortcutItem.ShortcutFunction myKeyDownFunctionPtr, ShortcutItem.ShortcutFunction myKeyUpFunctionPtr = null ) + { + if ( m_editorShortcutsDict.ContainsKey( key ) ) + { + if ( m_editorShortcutsDict[ key ].ContainsKey( modifiers ) ) + { + if ( DebugConsoleWindow.DeveloperMode ) + { + Debug.Log( "Attempting to register an already used editor shortcut key " + key ); + } + return; + } + } + else + { + m_editorShortcutsDict.Add( key, new Dictionary() ); + } + ShortcutItem item = new ShortcutItem( ( ( modifiers == EventModifiers.None || modifiers == EventModifiers.FunctionKey ) ? key.ToString() : modifiers + " + " + key ), description, myKeyDownFunctionPtr, myKeyUpFunctionPtr ); + m_editorShortcutsDict[ key ].Add( modifiers, item ); + if ( showOnList ) + m_editorShortcutsList.Add( item ); + } + + public void RegisterEditorShortcut( bool showOnList, KeyCode key, string description, ShortcutItem.ShortcutFunction myKeyDownFunctionPtr, ShortcutItem.ShortcutFunction myKeyUpFunctionPtr = null ) + { + if ( m_editorNoModifiersShortcutsDict.ContainsKey( key ) ) + { + if ( DebugConsoleWindow.DeveloperMode ) + { + Debug.Log( "Attempting to register an already used editor shortcut key " + key ); + } + return; + } + + ShortcutItem item = new ShortcutItem( key.ToString(), description, myKeyDownFunctionPtr, myKeyUpFunctionPtr ); + m_editorNoModifiersShortcutsDict.Add( key, item ); + if ( showOnList ) + m_editorShortcutsList.Add( item ); + } + + public bool ActivateShortcut( EventModifiers modifiers, KeyCode key, bool isKeyDown ) + { + if ( m_editorShortcutsDict.ContainsKey( key ) ) + { + if ( isKeyDown ) + { + if ( m_editorShortcutsDict[ key ].ContainsKey( modifiers ) ) + { + if ( m_editorShortcutsDict[ key ][ modifiers ].MyKeyDownFunctionPtr != null ) + { + m_editorShortcutsDict[ key ][ modifiers ].MyKeyDownFunctionPtr(); + return true; + } + } + } + else + { + if ( m_editorShortcutsDict[ key ].ContainsKey( modifiers ) ) + { + if ( m_editorShortcutsDict[ key ][ modifiers ].MyKeyUpFunctionPtr != null ) + { + m_editorShortcutsDict[ key ][ modifiers ].MyKeyUpFunctionPtr(); + return true; + } + } + } + } + + if ( modifiers == EventModifiers.None && m_editorNoModifiersShortcutsDict.ContainsKey( key ) ) + { + if ( isKeyDown ) + { + if ( m_editorNoModifiersShortcutsDict[ key ].MyKeyDownFunctionPtr != null ) + { + m_editorNoModifiersShortcutsDict[ key ].MyKeyDownFunctionPtr(); + return true; + } + } + else + { + if ( m_editorNoModifiersShortcutsDict[ key ].MyKeyUpFunctionPtr != null ) + { + m_editorNoModifiersShortcutsDict[ key ].MyKeyUpFunctionPtr(); + return true; + } + } + } + + return false; + } + + public void Destroy() + { + foreach ( KeyValuePair kvp in m_editorNoModifiersShortcutsDict ) + { + kvp.Value.Destroy(); + } + m_editorNoModifiersShortcutsDict.Clear(); + m_editorNoModifiersShortcutsDict = null; + + foreach ( KeyValuePair> kvpKey in m_editorShortcutsDict ) + { + foreach ( KeyValuePair kvpMod in kvpKey.Value ) + { + kvpMod.Value.Destroy(); + } + kvpKey.Value.Clear(); + } + m_editorShortcutsDict.Clear(); + m_editorShortcutsDict = null; + + m_editorShortcutsList.Clear(); + m_editorShortcutsList = null; + + m_nodesShortcutsDict.Clear(); + m_nodesShortcutsDict = null; + + m_nodesShortcutsList.Clear(); + m_nodesShortcutsList = null; + } + + public List AvailableEditorShortcutsList { get { return m_editorShortcutsList; } } + public List AvailableNodesShortcutsList { get { return m_nodesShortcutsList; } } + } +} -- cgit v1.1-26-g67d0