public void Init(BlendClipEditor editor, BlenderEditorWindow window) { clipEditors.Clear(); clipEditors.Add(editor); parentWindow = window; minSize = maxSize = new Vector2(300, 350); }
void UpdateClipEditors() { int count = 0; for (int i = 0; i < sequence.trackCount; i++) { count += sequence.GetTrack(i).clipCount; } if (clipEditors.Length != count) { clipEditors = new BlendClipEditor[count]; } int index = 0; for (int i = 0; i < sequence.trackCount; i++) { for (int j = 0; j < sequence.GetTrack(i).clipCount; j++) { clipEditors[index++] = new BlendClipEditor(sequence.GetTrack(i).GetClip(j), i); clipEditors[index - 1].HandleDuration(); } } }
private void OnGUI() { if (sequence == null) { CloseWindow(); return; } if (sequence.blender == null) { CloseWindow(); return; } if (!isInit) { Init(); } bool repaint = false; bool rightDown = Event.current.type == EventType.MouseDown && Event.current.button == 1; bool rightUp = Event.current.type == EventType.MouseUp && Event.current.button == 1; bool leftDown = Event.current.type == EventType.MouseDown && Event.current.button == 0; bool leftUp = Event.current.type == EventType.MouseUp && Event.current.button == 0; if (!left) { left = leftDown; } else if (leftUp) { left = false; } if (leftDown || leftUp) { repaint = true; } if (left) { Undo.RecordObject(sequence.blender, "Edit Tracks"); } scrollRect = new Rect(panelWidth, 25, Screen.width - panelWidth, Screen.height - 50); if (Event.current.type == EventType.ScrollWheel) { if (scrollRect.Contains(Event.current.mousePosition)) { if (Event.current.control) { zoom.y -= Event.current.delta.y * 0.15f; zoom.y = Mathf.Clamp(zoom.y, minZoomY, maxZoomY); } } } float trackWidth = (Screen.width - panelWidth - 2) * zoom.x; viewRect = new Rect(0, 0, trackWidth, trackEditors.Length * trackHeight * zoom.y); if (viewRect.height > Screen.height - 12) { trackWidth -= 30; } float trackHover = 0f; GUI.BeginGroup(new Rect(panelWidth + 1, 0, (Screen.width - panelWidth - 2), 25)); if (TimeTrack.Draw(new Rect(-trackScroll.x, 0, trackWidth, 25), sequence.duration, 5, Mathf.FloorToInt((sequence.duration / zoom.x) / 15), BlendaEditorGUI.borderColor, out trackHover, 30, trackLabelStyle)) { if (leftDown) { dragTracker = true; } if (rightDown) { GenericMenu menu = new GenericMenu(); menu.AddItem(new GUIContent("Set Time To"), false, OnSetStartTime); menu.AddItem(new GUIContent("Set Duration To"), false, OnSetDuration); menu.ShowAsContext(); } } GUI.EndGroup(); if (dragTracker) { timeTrack = sequence.duration * trackHover; if (snapOn) { SnapTracker(); } repaint = true; if (leftUp) { dragTracker = false; } } trackScroll = GUI.BeginScrollView(scrollRect, trackScroll, viewRect); if (trackEditors.Length != sequence.trackCount) { UpdateTrackEditors(); UpdateClipEditors(); ClearSelection(); FindOverlappingClips(); } HandleKeyboard(); for (int i = 0; i < trackEditors.Length; i++) { trackEditors[i].trackRect = new Rect(1, i * trackHeight * zoom.y, trackWidth, trackHeight * zoom.y); if (trackEditors[i].trackRect.Contains(Event.current.mousePosition) && (leftDown || rightDown)) { selectedTrack = i; if (rightDown) { if (rightDown) { GenericMenu menu = new GenericMenu(); if (copiedClips.Count > 0) { menu.AddItem(new GUIContent("Paste"), false, PasteCopiedClips); } else { menu.AddDisabledItem(new GUIContent("Paste")); } menu.ShowAsContext(); } } } trackEditors[i].DrawTrack(); if (i == 0) { trackEditors[i].DrawSeparatorTop(); } trackEditors[i].DrawSeparatorBottom(); bool hasDrag = false; if (trackEditors[i].GetDraggedClip(out hasDrag)) { UpdateClipEditors(); for (int j = 0; j < clipEditors.Length; j++) { if (clipEditors[j].clip == trackEditors[i].track.GetClip(trackEditors[i].track.clipCount - 1)) { selectedClips.Clear(); selectedClips.Add(j); break; } } } if (hasDrag) { repaint = true; } } BlendClipEditor.HoverType hoverType = BlendClipEditor.HoverType.None; for (int i = 0; i < clipEditors.Length; i++) { clipEditors[i].containerRect = trackEditors[clipEditors[i].trackId].trackRect; BlendClipEditor.HoverType h = clipEditors[i].HandleLayout(); if (h != BlendClipEditor.HoverType.None) { if (leftDown) { lastClickedEditor = i; SelectClip(i); repaint = true; } hoverType = h; if (rightDown) { lastClickedEditor = i; SelectClip(i); repaint = true; GenericMenu menu = new GenericMenu(); menu.AddItem(new GUIContent("Select GameObject"), false, OnSelectClipObject, i); menu.AddItem(new GUIContent("Select AudioClip"), false, OnSelectAudioClip, i); menu.AddSeparator(""); menu.AddItem(new GUIContent("Fit/Fit Sequence To"), false, OnFitSequence); menu.AddItem(new GUIContent("Fit/Fit To Sequence"), false, OnFitClips); menu.AddItem(new GUIContent("Fit/Set Duration At End"), false, OnDurationSetAtEnd, i); menu.AddItem(new GUIContent("Fit/Set Time At Beginning"), false, OnTimeSetAtBeginning, i); menu.AddItem(new GUIContent("Fit/Set Time At End"), false, OnTimeSetAtEnd, i); menu.AddSeparator(""); menu.AddItem(new GUIContent("Settings"), false, OnClipSettings, i); menu.AddSeparator(""); menu.AddItem(new GUIContent("Copy"), false, CopySelectedClips); menu.AddSeparator(""); menu.AddItem(new GUIContent("Delete"), false, DeleteSelectedClips); menu.ShowAsContext(); } } if (leftDown) { clipEditors[i].UpdateValues(); repaint = true; } } if (leftDown) { dragAnchor = Event.current.mousePosition; editTool = hoverType; if (hoverType == BlendClipEditor.HoverType.None && !Event.current.control) { ClearSelection(); } } if (leftUp) { editTool = BlendClipEditor.HoverType.None; } Vector2 dragDelta = Event.current.mousePosition - dragAnchor; if (Event.current.shift) { dragDelta.x = 0f; } int trackShift = Mathf.FloorToInt(dragDelta.y / (trackHeight * zoom.y * 0.9f)); if (dragDelta.y < 0f) { trackShift = Mathf.CeilToInt(dragDelta.y / (trackHeight * zoom.y)); } if (editTool != BlendClipEditor.HoverType.None && lastDrag != dragDelta) { lastDrag = dragDelta; repaint = true; } if (editTool != BlendClipEditor.HoverType.None) { for (int i = 0; i < selectedClips.Count; i++) { BlendClipEditor editor = clipEditors[selectedClips[i]]; if (trackShift != 0 && editTool == BlendClipEditor.HoverType.Move) { int targetTrack = editor.trackId + trackShift; if (targetTrack < 0) { targetTrack = 0; } if (targetTrack > trackEditors.Length - 1) { targetTrack = trackEditors.Length - 1; } if (targetTrack != editor.trackId) { sequence.GetTrack(targetTrack).AddClip(editor.clip); sequence.GetTrack(editor.trackId).UnlinkClip(editor.clip); editor.trackId = targetTrack; editor.containerRect = trackEditors[editor.trackId].trackRect; } } switch (editTool) { case BlendClipEditor.HoverType.Move: clipEditors[selectedClips[i]].Move(dragDelta.x); break; case BlendClipEditor.HoverType.TrimLeft: clipEditors[selectedClips[i]].TrimLeft(dragDelta.x); break; case BlendClipEditor.HoverType.TrimRight: clipEditors[selectedClips[i]].TrimRight(dragDelta.x); break; case BlendClipEditor.HoverType.FadeLeft: clipEditors[selectedClips[i]].FadeLeft(dragDelta.x); break; case BlendClipEditor.HoverType.FadeRight: clipEditors[selectedClips[i]].FadeRight(dragDelta.x); break; } } } if (leftUp || rightUp) { if (lastClickedEditor >= 0 && dragDelta.magnitude <= 0.5f && !Event.current.shift) { selectedClips.Clear(); SelectClip(lastClickedEditor); lastClickedEditor = -1; } } if (trackShift != 0) { dragAnchor.y = Event.current.mousePosition.y; } if (editTool != BlendClipEditor.HoverType.None) { FindOverlappingClips(); if (snapOn) { SnapClips(); if (snap != null) { float selectedAnchor = snap.selectedSide == 0 ? snap.selectedEditor.clip.start : snap.selectedEditor.clip.end; float snapDelta = selectedAnchor - snap.position; for (int i = 0; i < selectedClips.Count; i++) { clipEditors[selectedClips[i]].clip.start -= snapDelta; clipEditors[selectedClips[i]].HandleLayout(); } } } } for (int i = 0; i < clipEditors.Length; i++) { clipEditors[i].Draw(selectedClips.Contains(i)); } for (int i = 0; i < trackEditors.Length; i++) { if (trackEditors[i].track.mute) { EditorGUI.DrawRect(trackEditors[i].trackRect, new Color(0f, 0f, 0f, 0.3f)); } } GUI.EndScrollView(); GUI.BeginGroup(new Rect(panelWidth, 0, Screen.width - panelWidth - 2, Screen.height - 25)); EditorGUI.DrawRect(new Rect((Screen.width - panelWidth) * sequence.normalizedTime * zoom.x - 1 - trackScroll.x, 25, 2f, scrollRect.height), BlendaEditorGUI.borderColor); GUI.color = BlendaEditorGUI.borderColor; GUI.DrawTexture(new Rect((Screen.width - panelWidth) * sequence.normalizedTime * zoom.x - 1 - trackScroll.x, 13, 8, 8), startHeadImg, ScaleMode.StretchToFill); GUI.color = Color.white; EditorGUI.DrawRect(new Rect((Screen.width - panelWidth) * (timeTrack / sequence.duration) * zoom.x - 1 - trackScroll.x, 25, 2f, scrollRect.height), BlendaEditorGUI.highlightColor); GUI.color = BlendaEditorGUI.highlightColor; GUI.DrawTexture(new Rect((Screen.width - panelWidth) * (timeTrack / sequence.duration) * zoom.x - 4 - trackScroll.x, 13, 8, 8), trackerHeadImg, ScaleMode.StretchToFill); GUI.color = Color.white; GUI.EndGroup(); EditorGUI.DrawRect(new Rect(0, 25, panelWidth, Screen.height - 25), BlendaEditorGUI.lightDarkColor); GUI.BeginGroup(new Rect(0, 25, panelWidth, Screen.height - 51)); float trackBlendCenter = sequence.trackPanPercent * (trackEditors.Length - 1); for (int i = 0; i < trackEditors.Length; i++) { trackEditors[i].panelRect = new Rect(0f, i * trackHeight * zoom.y - trackScroll.y, panelWidth, trackHeight * zoom.y); if (trackEditors[i].panelRect.Contains(Event.current.mousePosition) && (leftDown || rightDown)) { selectedTrack = i; } if (sequence.panTracks) { trackEditors[i].blendAmount = sequence.trackPanFalloff.Evaluate(1f - Mathf.Abs(i - trackBlendCenter) / sequence.panRange); } else { trackEditors[i].blendAmount = 0f; } trackEditors[i].DrawPanel(selectedTrack == i); } GUI.EndGroup(); Footer(); if (Event.current.type == EventType.KeyDown || Event.current.type == EventType.KeyUp) { repaint = true; } if (repaint || Application.isPlaying) { Repaint(); } }