77using UnityEditor ;
88using System . Collections ;
99using System . Collections . Generic ;
10+ using UnityEditor . ShortcutManagement ;
1011using UnityEditorInternal ;
1112using Object = UnityEngine . Object ;
1213
@@ -21,6 +22,25 @@ internal enum WrapModeFixed
2122 PingPong = ( int ) WrapMode . PingPong
2223 }
2324
25+ class AnimationShortcutContex : IShortcutToolContext
26+ {
27+ AnimEditor m_AnimEditor ;
28+ public AnimationShortcutContex ( AnimEditor animEditor )
29+ {
30+ m_AnimEditor = animEditor ;
31+ }
32+
33+ public bool active
34+ {
35+ get { return ! animEditor . stateDisabled && ! animEditor . state . animatorIsOptimized ; }
36+ }
37+
38+ public AnimEditor animEditor
39+ {
40+ get { return m_AnimEditor ; }
41+ }
42+ }
43+
2444 internal class AnimEditor : ScriptableObject
2545 {
2646 // Active Animation windows
@@ -82,16 +102,7 @@ static private Color inRangeColor
82102 }
83103 }
84104
85- internal static PrefKey kAnimationPlayToggle = new PrefKey ( "Animation/Play Animation" , " " ) ;
86- internal static PrefKey kAnimationPrevFrame = new PrefKey ( "Animation/Previous Frame" , "," ) ;
87- internal static PrefKey kAnimationNextFrame = new PrefKey ( "Animation/Next Frame" , "." ) ;
88- internal static PrefKey kAnimationPrevKeyframe = new PrefKey ( "Animation/Previous Keyframe" , "&," ) ;
89- internal static PrefKey kAnimationNextKeyframe = new PrefKey ( "Animation/Next Keyframe" , "&." ) ;
90- internal static PrefKey kAnimationFirstKey = new PrefKey ( "Animation/First Keyframe" , "#," ) ;
91- internal static PrefKey kAnimationLastKey = new PrefKey ( "Animation/Last Keyframe" , "#." ) ;
92- internal static PrefKey kAnimationRecordKeyframeSelected = new PrefKey ( "Animation/Key Selected" , "k" ) ;
93- internal static PrefKey kAnimationRecordKeyframeModified = new PrefKey ( "Animation/Key Modified" , "#k" ) ;
94- internal static PrefKey kAnimationShowCurvesToggle = new PrefKey ( "Animation/Show Curves" , "c" ) ;
105+ AnimationShortcutContex m_AnimationShortcutContex ;
95106
96107 internal const int kSliderThickness = 15 ;
97108 internal const int kLayoutRowHeight = EditorGUI . kWindowToolbarHeight + 1 ;
@@ -223,8 +234,6 @@ public void OnAnimEditorGUI(EditorWindow parent, Rect position)
223234 OverlayOnGUI ( contentLayoutRect ) ;
224235
225236 RenderEventTooltip ( ) ;
226-
227- HandleHotKeys ( ) ;
228237 }
229238 }
230239
@@ -348,7 +357,8 @@ public void OnEnable()
348357 m_CurveEditor . curvesUpdated += SaveChangedCurvesFromCurveEditor ;
349358 m_CurveEditor . OnEnable ( ) ;
350359
351- EditorApplication . globalEventHandler += HandleGlobalHotkeys ;
360+ m_AnimationShortcutContex = new AnimationShortcutContex ( this ) ;
361+ ShortcutIntegration . instance . contextManager . RegisterToolContext ( m_AnimationShortcutContex ) ;
352362 }
353363
354364 public void OnDisable ( )
@@ -365,8 +375,7 @@ public void OnDisable()
365375 m_DopeSheet . OnDisable ( ) ;
366376
367377 m_State . OnDisable ( ) ;
368-
369- EditorApplication . globalEventHandler -= HandleGlobalHotkeys ;
378+ ShortcutIntegration . instance . contextManager . DeregisterToolContext ( m_AnimationShortcutContex ) ;
370379 }
371380
372381 public void OnDestroy ( )
@@ -495,11 +504,6 @@ private void TabSelectionOnGUI()
495504 {
496505 SwitchBetweenCurvesAndDopesheet ( ) ;
497506 }
498- else if ( kAnimationShowCurvesToggle . activated )
499- {
500- SwitchBetweenCurvesAndDopesheet ( ) ;
501- Event . current . Use ( ) ;
502- }
503507 }
504508
505509 private void HierarchyOnGUI ( )
@@ -723,110 +727,120 @@ private void LinkOptionsOnGUI()
723727 }
724728 }
725729
726- private void HandleHotKeys ( )
730+ static void ExecuteShortcut ( ShortcutArguments args , Action < AnimEditor > exp )
727731 {
728- if ( ! GUI . enabled || m_State . disabled )
729- return ;
730-
731- bool keyChanged = false ;
732+ var animEditorContext = ( AnimationShortcutContex ) args . context ;
733+ var animEditor = animEditorContext . animEditor ;
732734
733- if ( kAnimationPrevKeyframe . activated )
734- {
735- controlInterface . GoToPreviousKeyframe ( ) ;
736- keyChanged = true ;
737- }
738-
739- if ( kAnimationNextKeyframe . activated )
740- {
741- controlInterface . GoToNextKeyframe ( ) ;
742- keyChanged = true ;
743- }
735+ if ( EditorWindow . focusedWindow != animEditor . m_OwnerWindow )
736+ return ;
744737
745- if ( kAnimationNextFrame . activated )
746- {
747- controlInterface . GoToNextFrame ( ) ;
748- keyChanged = true ;
749- }
738+ exp ( animEditor ) ;
750739
751- if ( kAnimationPrevFrame . activated )
752- {
753- controlInterface . GoToPreviousFrame ( ) ;
754- keyChanged = true ;
755- }
756-
757- if ( kAnimationFirstKey . activated )
758- {
759- controlInterface . GoToFirstKeyframe ( ) ;
760- keyChanged = true ;
761- }
740+ animEditor . Repaint ( ) ;
741+ }
762742
763- if ( kAnimationLastKey . activated )
764- {
765- controlInterface . GoToLastKeyframe ( ) ;
766- keyChanged = true ;
767- }
743+ static void ExecuteShortcut ( ShortcutArguments args , Action < IAnimationWindowControl > exp )
744+ {
745+ ExecuteShortcut ( args , animEditor => exp ( animEditor . controlInterface ) ) ;
746+ }
768747
769- if ( keyChanged )
770- {
771- Event . current . Use ( ) ;
772- Repaint ( ) ;
773- }
748+ [ FormerlyPrefKeyAs ( "Animation/Show Curves" , "c" ) ]
749+ [ Shortcut ( "Animation/Show Curves" , typeof ( AnimationShortcutContex ) , "c" ) ]
750+ static void ShowCurves ( ShortcutArguments args )
751+ {
752+ ExecuteShortcut ( args , animEditor => { animEditor . SwitchBetweenCurvesAndDopesheet ( ) ; } ) ;
753+ }
774754
775- if ( kAnimationPlayToggle . activated )
755+ [ FormerlyPrefKeyAs ( "Animation/Play Animation" , " " ) ]
756+ [ Shortcut ( "Animation/Play Animation" , typeof ( AnimationShortcutContex ) , " " ) ]
757+ static void TogglePlayAnimation ( ShortcutArguments args )
758+ {
759+ ExecuteShortcut ( args , controlInterface =>
776760 {
777761 if ( controlInterface . playing )
778762 controlInterface . StopPlayback ( ) ;
779763 else
780764 controlInterface . StartPlayback ( ) ;
765+ } ) ;
766+ }
781767
782- Event . current . Use ( ) ;
783- }
768+ [ FormerlyPrefKeyAs ( "Animation/Next Frame" , "." ) ]
769+ [ Shortcut ( "Animation/Next Frame" , typeof ( AnimationShortcutContex ) , "." ) ]
770+ static void NextFrame ( ShortcutArguments args )
771+ {
772+ ExecuteShortcut ( args , controlInterface => controlInterface . GoToNextFrame ( ) ) ;
773+ }
784774
785- if ( kAnimationRecordKeyframeSelected . activated )
786- {
787- SaveCurveEditorKeySelection ( ) ;
788- AnimationWindowUtility . AddSelectedKeyframes ( m_State , controlInterface . time ) ;
789- UpdateSelectedKeysToCurveEditor ( ) ;
775+ [ FormerlyPrefKeyAs ( "Animation/Previous Frame" , "," ) ]
776+ [ Shortcut ( "Animation/Previous Frame" , typeof ( AnimationShortcutContex ) , "," ) ]
777+ static void PreviousFrame ( ShortcutArguments args )
778+ {
779+ ExecuteShortcut ( args , controlInterface => controlInterface . GoToPreviousFrame ( ) ) ;
780+ }
790781
791- Event . current . Use ( ) ;
792- }
782+ [ FormerlyPrefKeyAs ( "Animation/Previous Keyframe" , "&," ) ]
783+ [ Shortcut ( "Animation/Previous Keyframe" , typeof ( AnimationShortcutContex ) , "&," ) ]
784+ static void PreviousKeyFrame ( ShortcutArguments args )
785+ {
786+ ExecuteShortcut ( args , controlInterface => controlInterface . GoToPreviousKeyframe ( ) ) ;
787+ }
793788
794- if ( kAnimationRecordKeyframeModified . activated )
795- {
796- SaveCurveEditorKeySelection ( ) ;
797- controlInterface . ProcessCandidates ( ) ;
798- UpdateSelectedKeysToCurveEditor ( ) ;
789+ [ FormerlyPrefKeyAs ( "Animation/Next Keyframe" , "&." ) ]
790+ [ Shortcut ( "Animation/Next Keyframe" , typeof ( AnimationShortcutContex ) , "&." ) ]
791+ static void NextKeyFrame ( ShortcutArguments args )
792+ {
793+ ExecuteShortcut ( args , controlInterface => controlInterface . GoToNextKeyframe ( ) ) ;
794+ }
799795
800- Event . current . Use ( ) ;
801- }
796+ [ FormerlyPrefKeyAs ( "Animation/First Keyframe" , "#," ) ]
797+ [ Shortcut ( "Animation/First Keyframe" , typeof ( AnimationShortcutContex ) , "#," ) ]
798+ static void FirstKeyFrame ( ShortcutArguments args )
799+ {
800+ ExecuteShortcut ( args , controlInterface => controlInterface . GoToFirstKeyframe ( ) ) ;
802801 }
803802
804- public void HandleGlobalHotkeys ( )
803+ [ FormerlyPrefKeyAs ( "Animation/Last Keyframe" , "#." ) ]
804+ [ Shortcut ( "Animation/Last Keyframe" , typeof ( AnimationShortcutContex ) , "#." ) ]
805+ static void LastKeyFrame ( ShortcutArguments args )
805806 {
806- if ( ! m_State . previewing )
807- return ;
807+ ExecuteShortcut ( args , controlInterface => controlInterface . GoToLastKeyframe ( ) ) ;
808+ }
809+
810+ [ FormerlyPrefKeyAs ( "Animation/Key Selected" , "k" ) ]
811+ [ Shortcut ( "Animation/Key Selected" , typeof ( AnimationShortcutContex ) , "k" ) ]
812+ static void KeySelected ( ShortcutArguments args )
813+ {
814+ var animEditorContext = ( AnimationShortcutContex ) args . context ;
815+ var animEditor = animEditorContext . animEditor ;
808816
809- if ( ! GUI . enabled || m_State . disabled )
817+ if ( ! animEditor . m_State . previewing )
810818 return ;
811819
812- if ( kAnimationRecordKeyframeSelected . activated )
813- {
814- SaveCurveEditorKeySelection ( ) ;
815- AnimationWindowUtility . AddSelectedKeyframes ( m_State , controlInterface . time ) ;
816- controlInterface . ClearCandidates ( ) ;
817- UpdateSelectedKeysToCurveEditor ( ) ;
820+ animEditor . SaveCurveEditorKeySelection ( ) ;
821+ AnimationWindowUtility . AddSelectedKeyframes ( animEditor . m_State , animEditor . controlInterface . time ) ;
822+ if ( ! animEditor . m_OwnerWindow . hasFocus )
823+ animEditor . controlInterface . ClearCandidates ( ) ;
824+ animEditor . UpdateSelectedKeysToCurveEditor ( ) ;
818825
819- Event . current . Use ( ) ;
820- }
826+ animEditor . Repaint ( ) ;
827+ }
821828
822- if ( kAnimationRecordKeyframeModified . activated )
823- {
824- SaveCurveEditorKeySelection ( ) ;
825- controlInterface . ProcessCandidates ( ) ;
826- UpdateSelectedKeysToCurveEditor ( ) ;
829+ [ FormerlyPrefKeyAs ( "Animation/Key Modified" , "#k" ) ]
830+ [ Shortcut ( "Animation/Key Modified" , typeof ( AnimationShortcutContex ) , "#k" ) ]
831+ static void KeyModified ( ShortcutArguments args )
832+ {
833+ var animEditorContext = ( AnimationShortcutContex ) args . context ;
834+ var animEditor = animEditorContext . animEditor ;
827835
828- Event . current . Use ( ) ;
829- }
836+ if ( ! animEditor . m_State . previewing )
837+ return ;
838+
839+ animEditor . SaveCurveEditorKeySelection ( ) ;
840+ animEditor . controlInterface . ProcessCandidates ( ) ;
841+ animEditor . UpdateSelectedKeysToCurveEditor ( ) ;
842+
843+ animEditor . Repaint ( ) ;
830844 }
831845
832846 private void PlayButtonOnGUI ( )
0 commit comments