TrackActions.cs 15.3 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
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using UnityEditor.Timeline.Actions;
using UnityEngine;
using UnityEngine.Timeline;

namespace UnityEditor.Timeline
{
    [MenuEntry("Edit in Animation Window", MenuPriority.TrackActionSection.editInAnimationWindow)]
    class EditTrackInAnimationWindow : TrackAction
    {
        public static bool Do(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;

            GameObject gameObject = null;
            if (TimelineEditor.inspectedDirector != null)
                gameObject = TimelineUtility.GetSceneGameObject(TimelineEditor.inspectedDirector, track);

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

            return true;
        }

        public override ActionValidity Validate(IEnumerable<TrackAsset> tracks)
        {
            if (!tracks.Any())
                return ActionValidity.Invalid;

            var firstTrack = tracks.First();
            if (firstTrack is AnimationTrack)
            {
                var animTrack = firstTrack as AnimationTrack;
                if (animTrack.CanConvertToClipMode())
                    return ActionValidity.Valid;
            }
            else if (firstTrack.hasCurves)
            {
                return ActionValidity.Valid;
            }

            return ActionValidity.NotApplicable;
        }

        public override bool Execute(IEnumerable<TrackAsset> tracks)
        {
            return Do(tracks.First());
        }

        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", MenuPriority.TrackActionSection.lockSelected)]
    class LockSelectedTrack : TrackAction, IMenuName
    {
        public static readonly string LockSelectedTrackOnlyText = L10n.Tr("Lock selected track only");
        public static readonly string UnlockSelectedTrackOnlyText = L10n.Tr("Unlock selected track only");

        public string menuName { get; private set; }

        public override ActionValidity Validate(IEnumerable<TrackAsset> tracks)
        {
            UpdateMenuName(tracks);
            if (tracks.Any(track => TimelineUtility.IsLockedFromGroup(track) || track is GroupTrack || !track.subTracksObjects.Any()))
                return ActionValidity.NotApplicable;
            return ActionValidity.Valid;
        }

        public override bool Execute(IEnumerable<TrackAsset> tracks)
        {
            if (!tracks.Any()) return false;

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

        void UpdateMenuName(IEnumerable<TrackAsset> tracks)
        {
            menuName = tracks.All(t => t.locked) ? UnlockSelectedTrackOnlyText : LockSelectedTrackOnlyText;
        }

        public static void Lock(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", MenuPriority.TrackActionSection.lockTrack)]
    [Shortcut(Shortcuts.Timeline.toggleLock)]
    class LockTrack : TrackAction, IMenuName
    {
        static readonly string k_LockText = L10n.Tr("Lock");
        static readonly string k_UnlockText = L10n.Tr("Unlock");

        public string menuName { get; private set; }


        void UpdateMenuName(IEnumerable<TrackAsset> tracks)
        {
            menuName = tracks.Any(x => !x.locked) ? k_LockText : k_UnlockText;
        }

        public override bool Execute(IEnumerable<TrackAsset> tracks)
        {
            if (!tracks.Any()) return false;

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

        public override ActionValidity Validate(IEnumerable<TrackAsset> tracks)
        {
            UpdateMenuName(tracks);
            if (tracks.Any(TimelineUtility.IsLockedFromGroup))
                return ActionValidity.Invalid;
            return ActionValidity.Valid;
        }

        public static void SetLockState(IEnumerable<TrackAsset> tracks, bool shouldLock)
        {
            if (!tracks.Any())
                return;

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

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

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

            // 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);
                track.UnarmForRecord();
                foreach (var child in flattenedChildTracks)
                {
                    SelectionManager.Remove(child);
                    child.UnarmForRecord();
                    foreach (var clip in child.GetClips())
                        SelectionManager.Remove(clip);
                }
            }

            // no need to rebuild, just repaint (including inspectors)
            InspectorWindow.RepaintAllInspectors();
            TimelineEditor.Refresh(RefreshReason.WindowNeedsRedraw);
        }
    }

    [UsedImplicitly]
    [MenuEntry("Show Markers", MenuPriority.TrackActionSection.showHideMarkers)]
    [ActiveInMode(TimelineModes.Default | TimelineModes.ReadOnly)]
    class ShowHideMarkers : TrackAction, IMenuChecked
    {
        public bool isChecked { get; private set; }

        void UpdateCheckedStatus(IEnumerable<TrackAsset> tracks)
        {
            isChecked = tracks.All(x => x.GetShowMarkers());
        }

        public override ActionValidity Validate(IEnumerable<TrackAsset> tracks)
        {
            UpdateCheckedStatus(tracks);
            if (tracks.Any(x => x is GroupTrack) || tracks.Any(t => t.GetMarkerCount() == 0))
                return ActionValidity.NotApplicable;

            if (tracks.Any(t => t.lockedInHierarchy))
            {
                return ActionValidity.Invalid;
            }

            return ActionValidity.Valid;
        }

        public override bool Execute(IEnumerable<TrackAsset> tracks)
        {
            if (!tracks.Any()) return false;

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

        static void ShowHide(IEnumerable<TrackAsset> tracks, bool shouldLock)
        {
            if (!tracks.Any())
                return;

            foreach (var track in tracks)
                track.SetShowTrackMarkers(shouldLock);

            TimelineEditor.Refresh(RefreshReason.WindowNeedsRedraw);
        }
    }

    [MenuEntry("Mute selected track only", MenuPriority.TrackActionSection.muteSelected), UsedImplicitly]
    class MuteSelectedTrack : TrackAction, IMenuName
    {
        public static readonly string MuteSelectedText = L10n.Tr("Mute selected track only");
        public static readonly string UnmuteSelectedText = L10n.Tr("Unmute selected track only");

        public string menuName { get; private set; }

        public override ActionValidity Validate(IEnumerable<TrackAsset> tracks)
        {
            UpdateMenuName(tracks);
            if (tracks.Any(track => TimelineUtility.IsParentMuted(track) || track is GroupTrack || !track.subTracksObjects.Any()))
                return ActionValidity.NotApplicable;
            return ActionValidity.Valid;
        }

        public override bool Execute(IEnumerable<TrackAsset> tracks)
        {
            if (!tracks.Any())
                return false;

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

        void UpdateMenuName(IEnumerable<TrackAsset> tracks)
        {
            menuName = tracks.All(t => t.muted) ? UnmuteSelectedText : MuteSelectedText;
        }

        public static void Mute(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;
            }

            TimelineEditor.Refresh(RefreshReason.ContentsModified);
        }
    }

    [MenuEntry("Mute", MenuPriority.TrackActionSection.mute)]
    [Shortcut(Shortcuts.Timeline.toggleMute)]
    class MuteTrack : TrackAction, IMenuName
    {
        static readonly string k_MuteText = L10n.Tr("Mute");
        static readonly string k_UnMuteText = L10n.Tr("Unmute");

        public string menuName { get; private set; }

        void UpdateMenuName(IEnumerable<TrackAsset> tracks)
        {
            menuName = tracks.Any(x => !x.muted) ? k_MuteText : k_UnMuteText;
        }

        public override bool Execute(IEnumerable<TrackAsset> tracks)
        {
            if (!tracks.Any() || tracks.Any(TimelineUtility.IsParentMuted))
                return false;

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

        public override ActionValidity Validate(IEnumerable<TrackAsset> tracks)
        {
            UpdateMenuName(tracks);
            if (tracks.Any(TimelineUtility.IsLockedFromGroup))
                return ActionValidity.Invalid;
            return ActionValidity.Valid;
        }

        public static void Mute(IEnumerable<TrackAsset> tracks, bool shouldMute)
        {
            if (!tracks.Any())
                return;

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

            TimelineEditor.Refresh(RefreshReason.ContentsModified);
        }
    }

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

        public override ActionValidity Validate(IEnumerable<TrackAsset> tracks) => ActionValidity.Valid;

        public override bool Execute(IEnumerable<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
            TimelineEditor.state.previewMode = false;

            TimelineAnimationUtilities.UnlinkAnimationWindowFromTracks(tracks);

            foreach (var track in tracks)
                Do(TimelineEditor.inspectedAsset, track);

            TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved);

            return true;
        }
    }

    class CopyTracksToClipboard : TrackAction
    {
        public static bool Do(TrackAsset[] tracks)
        {
            var action = new CopyTracksToClipboard();
            return action.Execute(tracks);
        }

        public override ActionValidity Validate(IEnumerable<TrackAsset> tracks) => ActionValidity.Valid;

        public override bool Execute(IEnumerable<TrackAsset> tracks)
        {
            TimelineEditor.clipboard.CopyTracks(tracks);

            return true;
        }
    }

    class DuplicateTracks : TrackAction
    {
        public override ActionValidity Validate(IEnumerable<TrackAsset> tracks) => ActionValidity.Valid;

        public override bool Execute(IEnumerable<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);
                }

                if (TimelineEditor.inspectedDirector != null)
                {
                    var binding = TimelineEditor.inspectedDirector.GetGenericBinding(track);
                    if (binding != null)
                    {
                        TimelineUndo.PushUndo(TimelineEditor.inspectedDirector, "Duplicate");
                        TimelineEditor.inspectedDirector.SetGenericBinding(newTrack, binding);
                    }
                }
            }

            TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved);

            return true;
        }
    }

    [MenuEntry("Remove Invalid Markers", MenuPriority.TrackActionSection.removeInvalidMarkers), UsedImplicitly]
    class RemoveInvalidMarkersAction : TrackAction
    {
        public override ActionValidity Validate(IEnumerable<TrackAsset> tracks)
        {
            if (tracks.Any(target => target != null && target.GetMarkerCount() != target.GetMarkersRaw().Count()))
                return ActionValidity.Valid;

            return ActionValidity.NotApplicable;
        }

        public override bool Execute(IEnumerable<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;
        }
    }
}