TrackActions.cs 17.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Timeline;

namespace UnityEditor.Timeline
{
    [ActiveInMode(TimelineModes.Default)]
    abstract class TrackAction : MenuItemActionBase
    {
        public abstract bool Execute(WindowState state, TrackAsset[] tracks);

        protected virtual MenuActionDisplayState GetDisplayState(WindowState state, TrackAsset[] tracks)
        {
            return tracks.Length > 0 ? MenuActionDisplayState.Visible : MenuActionDisplayState.Disabled;
        }

        protected virtual bool IsChecked(WindowState state, TrackAsset[] tracks)
        {
            return false;
        }

        protected virtual string GetDisplayName(TrackAsset[] tracks)
        {
            return menuName;
        }

        public static void Invoke<T>(WindowState state, TrackAsset[] tracks) where T : TrackAction
        {
            actions.First(x => x.GetType() == typeof(T)).Execute(state, tracks);
        }

        static List<TrackAction> s_ActionClasses;

        static List<TrackAction> actions
        {
            get
            {
                if (s_ActionClasses == null)
                    s_ActionClasses =
                        GetActionsOfType(typeof(TrackAction))
                            .Select(x => (TrackAction)x.GetConstructors()[0].Invoke(null))
                            .OrderBy(x => x.priority).ThenBy(x => x.category)
                            .ToList();

                return s_ActionClasses;
            }
        }

        public static void GetMenuEntries(WindowState state, Vector2? mousePos, TrackAsset[] tracks, List<MenuActionItem> items)
        {
            var mode = TimelineWindow.instance.currentMode.mode;
            foreach (var action in actions)
            {
                if (!action.showInMenu)
                    continue;

                var actionItem = action;
                items.Add(
                    new MenuActionItem()
                    {
                        category =  action.category,
                        entryName = action.GetDisplayName(tracks),
                        shortCut = action.shortCut,
                        isChecked = action.IsChecked(state, tracks),
                        isActiveInMode = IsActionActiveInMode(action, mode),
                        priority = action.priority,
                        state = action.GetDisplayState(state, tracks),
                        callback = () =>
                        {
                            actionItem.mousePosition = mousePos;
                            actionItem.Execute(state, tracks);
                            actionItem.mousePosition = null;
                        }
                    }
                );
            }
        }

        public static bool HandleShortcut(WindowState state, Event evt, TrackAsset[] tracks)
        {
            foreach (var action in actions)
            {
                var attr = action.GetType().GetCustomAttributes(typeof(ShortcutAttribute), true);

                foreach (ShortcutAttribute shortcut in attr)
                {
                    if (shortcut.MatchesEvent(evt))
                    {
                        if (s_ShowActionTriggeredByShortcut)
                            Debug.Log(action.GetType().Name);

                        if (!IsActionActiveInMode(action, TimelineWindow.instance.currentMode.mode))
                            return false;

                        return action.Execute(state, tracks);
                    }
                }
            }

            return false;
        }

        // For testing
        internal MenuActionDisplayState InternalGetDisplayState(WindowState state, TrackAsset[] tracks)
        {
            return GetDisplayState(state, tracks);
        }
    }

    [MenuEntry("Edit in Animation Window", MenuOrder.TrackAction.EditInAnimationWindow)]
    class EditTrackInAnimationWindow : TrackAction
    {
        public static bool Do(WindowState state, TrackAsset track)
        {
            AnimationClip clipToEdit = null;

            AnimationTrack animationTrack = track as AnimationTrack;
            if (animationTrack != null)
            {
                if (!animationTrack.CanConvertToClipMode())
                    return false;

                clipToEdit = animationTrack.infiniteClip;
            }
            else if (track.hasCurves)
            {
                clipToEdit = track.curves;
            }

            if (clipToEdit == null)
                return false;

            var gameObject = state.GetSceneReference(track);
            var timeController = TimelineAnimationUtilities.CreateTimeController(state, CreateTimeControlClipData(track));
            TimelineAnimationUtilities.EditAnimationClipWithTimeController(clipToEdit, timeController, gameObject);

            return true;
        }

        protected override MenuActionDisplayState GetDisplayState(WindowState state, TrackAsset[] tracks)
        {
            if (tracks.Length == 0)
                return MenuActionDisplayState.Hidden;

            if (tracks[0] is AnimationTrack)
            {
                var animTrack = tracks[0] as AnimationTrack;
                if (animTrack.CanConvertToClipMode())
                    return MenuActionDisplayState.Visible;
            }
            else if (tracks[0].hasCurves)
            {
                return MenuActionDisplayState.Visible;
            }

            return MenuActionDisplayState.Hidden;
        }

        public override bool Execute(WindowState state, TrackAsset[] tracks)
        {
            return Do(state, tracks[0]);
        }

        static TimelineWindowTimeControl.ClipData CreateTimeControlClipData(TrackAsset track)
        {
            var data = new TimelineWindowTimeControl.ClipData();
            data.track = track;
            data.start = track.start;
            data.duration = track.duration;
            return data;
        }
    }

    [MenuEntry("Lock selected track only", MenuOrder.TrackAction.LockSelected)]
    class LockSelectedTrack : TrackAction
    {
        public static readonly string LockSelectedTrackOnlyText = L10n.Tr("Lock selected track only");
        public static readonly string UnlockSelectedTrackOnlyText = L10n.Tr("Unlock selected track only");

        protected override MenuActionDisplayState GetDisplayState(WindowState state, TrackAsset[] tracks)
        {
            if (tracks.Any(track => TimelineUtility.IsLockedFromGroup(track) || track is GroupTrack ||
                !track.subTracksObjects.Any()))
                return MenuActionDisplayState.Hidden;
            return MenuActionDisplayState.Visible;
        }

        public override bool Execute(WindowState state, TrackAsset[] tracks)
        {
            if (!tracks.Any()) return false;

            var hasUnlockedTracks = tracks.Any(x => !x.locked);
            Lock(state, tracks.Where(p => !(p is GroupTrack)).ToArray(), hasUnlockedTracks);
            return true;
        }

        protected override string GetDisplayName(TrackAsset[] tracks)
        {
            return tracks.All(t => t.locked) ? UnlockSelectedTrackOnlyText : LockSelectedTrackOnlyText;
        }

        public static void Lock(WindowState state, TrackAsset[] tracks, bool shouldlock)
        {
            if (tracks.Length == 0)
                return;

            foreach (var track in tracks.Where(t => !TimelineUtility.IsLockedFromGroup(t)))
            {
                TimelineUndo.PushUndo(track, "Lock Tracks");
                track.locked = shouldlock;
            }
            TimelineEditor.Refresh(RefreshReason.WindowNeedsRedraw);
        }
    }

    [MenuEntry("Lock", MenuOrder.TrackAction.LockTrack)]
    [Shortcut(Shortcuts.Timeline.toggleLock)]
    class LockTrack : TrackAction
    {
        public static readonly string UnlockText = L10n.Tr("Unlock");

        protected override MenuActionDisplayState GetDisplayState(WindowState state, TrackAsset[] tracks)
        {
            bool hasUnlockableTracks = tracks.Any(x => TimelineUtility.IsLockedFromGroup(x));
            if (hasUnlockableTracks)
                return MenuActionDisplayState.Disabled;
            return MenuActionDisplayState.Visible;
        }

        protected override string GetDisplayName(TrackAsset[] tracks)
        {
            return tracks.Any(x => !x.locked) ? base.GetDisplayName(tracks) : UnlockText;
        }

        public override bool Execute(WindowState state, TrackAsset[] tracks)
        {
            if (!tracks.Any()) return false;

            var hasUnlockedTracks = tracks.Any(x => !x.locked);
            SetLockState(tracks, hasUnlockedTracks, state);
            return true;
        }

        public static void SetLockState(TrackAsset[] tracks, bool shouldLock, WindowState state = null)
        {
            if (tracks.Length == 0)
                return;

            foreach (var track in tracks)
            {
                if (TimelineUtility.IsLockedFromGroup(track))
                    continue;

                if (track as GroupTrack == null)
                    SetLockState(track.GetChildTracks().ToArray(), shouldLock, state);

                TimelineUndo.PushUndo(track, "Lock Tracks");
                track.locked = shouldLock;
            }

            if (state != null)
            {
                // find the tracks we've locked. unselect anything locked and remove recording.
                foreach (var track in tracks)
                {
                    if (TimelineUtility.IsLockedFromGroup(track) || !track.locked)
                        continue;

                    var flattenedChildTracks = track.GetFlattenedChildTracks();
                    foreach (var i in track.clips)
                        SelectionManager.Remove(i);
                    state.UnarmForRecord(track);
                    foreach (var child in flattenedChildTracks)
                    {
                        SelectionManager.Remove(child);
                        state.UnarmForRecord(child);
                        foreach (var clip in child.GetClips())
                            SelectionManager.Remove(clip);
                    }
                }

                // no need to rebuild, just repaint (including inspectors)
                InspectorWindow.RepaintAllInspectors();
                state.editorWindow.Repaint();
            }
        }
    }

    [UsedImplicitly]
    [MenuEntry("Show Markers", MenuOrder.TrackAction.ShowHideMarkers)]
    [ActiveInMode(TimelineModes.Default | TimelineModes.ReadOnly)]
    class ShowHideMarkers : TrackAction
    {
        protected override bool IsChecked(WindowState state, TrackAsset[] tracks)
        {
            return tracks.All(x => x.GetShowMarkers());
        }

        protected override MenuActionDisplayState GetDisplayState(WindowState state, TrackAsset[] tracks)
        {
            if (tracks.Any(x => x is GroupTrack) || tracks.Any(t => t.GetMarkerCount() == 0))
                return MenuActionDisplayState.Hidden;

            if (tracks.Any(t => t.lockedInHierarchy))
                return MenuActionDisplayState.Disabled;

            return MenuActionDisplayState.Visible;
        }

        public override bool Execute(WindowState state, TrackAsset[] tracks)
        {
            if (!tracks.Any()) return false;

            var hasUnlockedTracks = tracks.Any(x => !x.GetShowMarkers());
            ShowHide(state, tracks, hasUnlockedTracks);
            return true;
        }

        static void ShowHide(WindowState state, TrackAsset[] tracks, bool shouldLock)
        {
            if (tracks.Length == 0)
                return;

            var window = state.GetWindow();
            foreach (var track in tracks)
            {
                window.SetShowTrackMarkers(track, shouldLock);
            }

            TimelineEditor.Refresh(RefreshReason.WindowNeedsRedraw);
        }
    }

    [MenuEntry("Mute selected track only", MenuOrder.TrackAction.MuteSelected), UsedImplicitly]
    class MuteSelectedTrack : TrackAction
    {
        public static readonly string UnmuteSelectedText = L10n.Tr("Unmute selected track only");
        protected override MenuActionDisplayState GetDisplayState(WindowState state, TrackAsset[] tracks)
        {
            if (tracks.Any(track => TimelineUtility.IsParentMuted(track) || track is GroupTrack ||
                !track.subTracksObjects.Any()))
                return MenuActionDisplayState.Hidden;
            return MenuActionDisplayState.Visible;
        }

        public override bool Execute(WindowState state, TrackAsset[] tracks)
        {
            if (!tracks.Any())
                return false;

            var hasUnmutedTracks = tracks.Any(x => !x.muted);
            Mute(state, tracks.Where(p => !(p is GroupTrack)).ToArray(), hasUnmutedTracks);
            return true;
        }

        protected override string GetDisplayName(TrackAsset[] tracks)
        {
            return tracks.All(t => t.muted) ?  UnmuteSelectedText : base.GetDisplayName(tracks);
        }

        public static void Mute(WindowState state, TrackAsset[] tracks, bool shouldMute)
        {
            if (tracks.Length == 0)
                return;

            foreach (var track in tracks.Where(t => !TimelineUtility.IsParentMuted(t)))
            {
                TimelineUndo.PushUndo(track, "Mute Tracks");
                track.muted = shouldMute;
            }

            state.Refresh();
        }
    }

    [MenuEntry("Mute", MenuOrder.TrackAction.MuteTrack)]
    [Shortcut(Shortcuts.Timeline.toggleMute)]
    class MuteTrack : TrackAction
    {
        public static readonly string UnMuteText = L10n.Tr("Unmute");

        protected override MenuActionDisplayState GetDisplayState(WindowState state, TrackAsset[] tracks)
        {
            if (tracks.Any(track => TimelineUtility.IsParentMuted(track)))
                return MenuActionDisplayState.Disabled;
            return MenuActionDisplayState.Visible;
        }

        protected override string GetDisplayName(TrackAsset[] tracks)
        {
            return tracks.Any(x => !x.muted) ? base.GetDisplayName(tracks) : UnMuteText;
        }

        public override bool Execute(WindowState state, TrackAsset[] tracks)
        {
            if (!tracks.Any() || tracks.Any(track => TimelineUtility.IsParentMuted(track)))
                return false;

            var hasUnmutedTracks = tracks.Any(x => !x.muted);
            Mute(state, tracks, hasUnmutedTracks);
            return true;
        }

        public static void Mute(WindowState state, TrackAsset[] tracks, bool shouldMute)
        {
            if (tracks.Length == 0)
                return;

            foreach (var track in tracks)
            {
                if (track as GroupTrack == null)
                    Mute(state, track.GetChildTracks().ToArray(), shouldMute);
                TimelineUndo.PushUndo(track, "Mute Tracks");
                track.muted = shouldMute;
            }

            state.Refresh();
        }
    }

    class DeleteTracks : TrackAction
    {
        public static void Do(TimelineAsset timeline, TrackAsset track)
        {
            SelectionManager.Remove(track);
            TrackModifier.DeleteTrack(timeline, track);
        }

        public override bool Execute(WindowState state, TrackAsset[] tracks)
        {
            // disable preview mode so deleted tracks revert to default state
            // Case 956129: Disable preview mode _before_ deleting the tracks, since clip data is still needed
            state.previewMode = false;

            TimelineAnimationUtilities.UnlinkAnimationWindowFromTracks(tracks);

            foreach (var track in tracks)
                Do(state.editSequence.asset, track);

            state.Refresh();

            return true;
        }
    }

    class CopyTracksToClipboard : TrackAction
    {
        public static bool Do(WindowState state, TrackAsset[] tracks)
        {
            var action = new CopyTracksToClipboard();

            return action.Execute(state, tracks);
        }

        public override bool Execute(WindowState state, TrackAsset[] tracks)
        {
            TimelineEditor.clipboard.CopyTracks(tracks);

            return true;
        }
    }

    class DuplicateTracks : TrackAction
    {
        public override bool Execute(WindowState state, TrackAsset[] tracks)
        {
            if (tracks.Any())
            {
                SelectionManager.RemoveTimelineSelection();
            }

            foreach (var track in TrackExtensions.FilterTracks(tracks))
            {
                var newTrack = track.Duplicate(TimelineEditor.inspectedDirector, TimelineEditor.inspectedDirector);
                SelectionManager.Add(newTrack);
                foreach (var childTrack in newTrack.GetFlattenedChildTracks())
                {
                    SelectionManager.Add(childTrack);
                }
            }

            state.Refresh();

            return true;
        }
    }

    [MenuEntry("Remove Invalid Markers", MenuOrder.TrackAction.RemoveInvalidMarkers), UsedImplicitly]
    class RemoveInvalidMarkersAction : TrackAction
    {
        protected override MenuActionDisplayState GetDisplayState(WindowState state, TrackAsset[] tracks)
        {
            if (tracks.Any(target => target != null && target.GetMarkerCount() != target.GetMarkersRaw().Count()))
                return MenuActionDisplayState.Visible;

            return MenuActionDisplayState.Hidden;
        }

        public override bool Execute(WindowState state, TrackAsset[] tracks)
        {
            bool anyRemoved = false;
            foreach (var target in tracks)
            {
                var invalids = target.GetMarkersRaw().Where(x => !(x is IMarker)).ToList();
                foreach (var m in invalids)
                {
                    anyRemoved = true;
                    target.DeleteMarkerRaw(m);
                }
            }

            if (anyRemoved)
                TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved);

            return anyRemoved;
        }
    }
}