ShortcutAttribute.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Linq;
  3. using UnityEditor.ShortcutManagement;
  4. using UnityEngine;
  5. namespace UnityEditor.Timeline
  6. {
  7. [AttributeUsage(AttributeTargets.Class, Inherited = false)]
  8. class ShortcutAttribute : Attribute
  9. {
  10. readonly string m_Identifier;
  11. readonly string m_EventCommandName;
  12. readonly string m_MenuShortcut;
  13. public ShortcutAttribute(string identifier)
  14. {
  15. m_Identifier = identifier;
  16. m_EventCommandName = identifier;
  17. }
  18. public ShortcutAttribute(string identifier, string commandName)
  19. {
  20. m_Identifier = identifier;
  21. m_EventCommandName = commandName;
  22. }
  23. public ShortcutAttribute(KeyCode key, ShortcutModifiers modifiers = ShortcutModifiers.None)
  24. {
  25. m_MenuShortcut = new KeyCombination(key, modifiers).ToMenuShortcutString();
  26. }
  27. public string GetMenuShortcut()
  28. {
  29. if (m_MenuShortcut != null)
  30. return m_MenuShortcut;
  31. //find the mapped shortcut in the shortcut manager
  32. var shortcut = ShortcutIntegration.instance.directory.FindShortcutEntry(m_Identifier);
  33. if (shortcut != null && shortcut.combinations.Any())
  34. {
  35. return KeyCombination.SequenceToMenuString(shortcut.combinations);
  36. }
  37. return string.Empty;
  38. }
  39. public bool MatchesEvent(Event evt)
  40. {
  41. if (evt.type != EventType.ExecuteCommand)
  42. return false;
  43. return evt.commandName == m_EventCommandName;
  44. }
  45. }
  46. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
  47. class ShortcutPlatformOverrideAttribute : ShortcutAttribute
  48. {
  49. RuntimePlatform platform { get; }
  50. public ShortcutPlatformOverrideAttribute(RuntimePlatform platform, KeyCode key, ShortcutModifiers modifiers = ShortcutModifiers.None)
  51. : base(key, modifiers)
  52. {
  53. this.platform = platform;
  54. }
  55. public bool MatchesCurrentPlatform()
  56. {
  57. return Application.platform == platform;
  58. }
  59. }
  60. }