public void DoTimeControl(Rect rect) { if (TimeControl.s_Styles == null) { TimeControl.s_Styles = new TimeControl.Styles(); } Event current = Event.current; int controlID = GUIUtility.GetControlID(TimeControl.kScrubberIDHash, FocusType.Keyboard); Rect rect2 = rect; rect2.height = 21f; Rect rect3 = rect2; rect3.xMin += 33f; switch (current.GetTypeForControl(controlID)) { case EventType.MouseDown: if (rect.Contains(current.mousePosition)) { GUIUtility.keyboardControl = controlID; } if (rect3.Contains(current.mousePosition)) { EditorGUIUtility.SetWantsMouseJumping(1); GUIUtility.hotControl = controlID; this.m_MouseDrag = current.mousePosition.x - rect3.xMin; this.nextCurrentTime = this.m_MouseDrag * (this.stopTime - this.startTime) / rect3.width + this.startTime; this.m_WrapForwardDrag = false; current.Use(); } break; case EventType.MouseUp: if (GUIUtility.hotControl == controlID) { EditorGUIUtility.SetWantsMouseJumping(0); GUIUtility.hotControl = 0; current.Use(); } break; case EventType.MouseDrag: if (GUIUtility.hotControl == controlID) { this.m_MouseDrag += current.delta.x * this.playbackSpeed; if (this.loop && ((this.m_MouseDrag < 0f && this.m_WrapForwardDrag) || this.m_MouseDrag > rect3.width)) { if (this.m_MouseDrag > rect3.width) { this.currentTime -= this.stopTime - this.startTime; } else if (this.m_MouseDrag < 0f) { this.currentTime += this.stopTime - this.startTime; } this.m_WrapForwardDrag = true; this.m_MouseDrag = Mathf.Repeat(this.m_MouseDrag, rect3.width); } this.nextCurrentTime = Mathf.Clamp(this.m_MouseDrag, 0f, rect3.width) * (this.stopTime - this.startTime) / rect3.width + this.startTime; current.Use(); } break; case EventType.KeyDown: if (GUIUtility.keyboardControl == controlID) { if (current.keyCode == KeyCode.LeftArrow) { if (this.currentTime - this.startTime > 0.01f) { this.deltaTime = -0.01f; } current.Use(); } if (current.keyCode == KeyCode.RightArrow) { if (this.stopTime - this.currentTime > 0.01f) { this.deltaTime = 0.01f; } current.Use(); } } break; } GUI.Box(rect2, GUIContent.none, TimeControl.s_Styles.timeScrubber); this.playing = GUI.Toggle(rect2, this.playing, (!this.playing) ? TimeControl.s_Styles.playIcon : TimeControl.s_Styles.pauseIcon, TimeControl.s_Styles.playButton); float x = Mathf.Lerp(rect3.x, rect3.xMax, this.normalizedTime); TimeArea.DrawPlayhead(x, rect3.yMin, rect3.yMax, 2f, (GUIUtility.keyboardControl != controlID) ? 0.5f : 1f); }
public void DoTimeControl(Rect rect) { if (s_Styles == null) { s_Styles = new Styles(); } var evt = Event.current; int id = EditorGUIUtility.GetControlID(kScrubberIDHash, FocusType.Keyboard); // Play/Pause Button + Scrubber Rect timelineRect = rect; timelineRect.height = kScrubberHeight; // Only Scrubber Rect scrubberRect = timelineRect; scrubberRect.xMin += kPlayButtonWidth; // Handle Input switch (evt.GetTypeForControl(id)) { case EventType.MouseDown: if (rect.Contains(evt.mousePosition)) { EditorGUIUtility.keyboardControl = id; } if (scrubberRect.Contains(evt.mousePosition)) { EditorGUIUtility.SetWantsMouseJumping(1); EditorGUIUtility.hotControl = id; m_MouseDrag = evt.mousePosition.x - scrubberRect.xMin; nextCurrentTime = (m_MouseDrag * (stopTime - startTime) / scrubberRect.width + startTime); m_WrapForwardDrag = false; evt.Use(); } break; case EventType.MouseDrag: if (EditorGUIUtility.hotControl == id) { m_MouseDrag += evt.delta.x * playbackSpeed; // We want to not wrap if we immediately drag to the beginning, but we do want to wrap if we drag past the end. if (loop && ((m_MouseDrag < 0.0f && m_WrapForwardDrag) || (m_MouseDrag > scrubberRect.width))) { // scrubing out of range was generating a big deltaTime in wrong time direction // this new code prevent this and it is compliant with new and more robust v5.0 root motion looping of animation clip if (m_MouseDrag > scrubberRect.width) { currentTime -= (stopTime - startTime); } else if (m_MouseDrag < 0) { currentTime += (stopTime - startTime); } m_WrapForwardDrag = true; m_MouseDrag = Mathf.Repeat(m_MouseDrag, scrubberRect.width); } nextCurrentTime = (Mathf.Clamp(m_MouseDrag, 0.0f, scrubberRect.width) * (stopTime - startTime) / scrubberRect.width + startTime); evt.Use(); } break; case EventType.MouseUp: if (EditorGUIUtility.hotControl == id) { EditorGUIUtility.SetWantsMouseJumping(0); EditorGUIUtility.hotControl = 0; evt.Use(); } break; case EventType.KeyDown: if (EditorGUIUtility.keyboardControl == id) { // TODO: loop? if (evt.keyCode == KeyCode.LeftArrow) { if (currentTime - startTime > kStepTime) { deltaTime = -kStepTime; } evt.Use(); } if (evt.keyCode == KeyCode.RightArrow) { if (stopTime - currentTime > kStepTime) { deltaTime = kStepTime; } evt.Use(); } } break; } // background GUI.Box(timelineRect, GUIContent.none, s_Styles.timeScrubber); // Play/Pause Button playing = GUI.Toggle(timelineRect, playing, playing ? s_Styles.pauseIcon : s_Styles.playIcon, s_Styles.playButton); // Current time indicator float normalizedPosition = Mathf.Lerp(scrubberRect.x, scrubberRect.xMax, normalizedTime); TimeArea.DrawPlayhead(normalizedPosition, scrubberRect.yMin, scrubberRect.yMax, 2f, (EditorGUIUtility.keyboardControl == id) ? 1f : 0.5f); }
private void DoSliceScrubber(Rect controlRect, Texture2DArray t) { int id = GUIUtility.GetControlID(kScrubberHash, FocusType.Keyboard); Rect prevFrameRect = controlRect; prevFrameRect.width = kSliceStepWidth; Rect nextFrameRect = prevFrameRect; nextFrameRect.x += nextFrameRect.width; var scrubberRect = controlRect; scrubberRect.xMin = nextFrameRect.xMax; var evt = Event.current; switch (evt.GetTypeForControl(id)) { case EventType.MouseDown: { if (scrubberRect.Contains(evt.mousePosition)) { GUIUtility.keyboardControl = id; GUIUtility.hotControl = id; m_MouseDrag = evt.mousePosition.x - scrubberRect.xMin; m_Slice = (int)(m_MouseDrag * t.depth / scrubberRect.width); evt.Use(); } break; } case EventType.MouseDrag: { if (GUIUtility.hotControl == id) { m_MouseDrag += evt.delta.x; m_Slice = (int)(Mathf.Clamp(m_MouseDrag, 0.0f, scrubberRect.width) * t.depth / scrubberRect.width); evt.Use(); } break; } case EventType.MouseUp: { if (GUIUtility.hotControl == id) { GUIUtility.hotControl = 0; evt.Use(); } break; } case EventType.KeyDown: { if (GUIUtility.keyboardControl == id) { if (evt.keyCode == KeyCode.LeftArrow) { if (m_Slice > 0) { --m_Slice; } evt.Use(); } if (evt.keyCode == KeyCode.RightArrow) { if (m_Slice < t.depth - 1) { ++m_Slice; } evt.Use(); } } break; } case EventType.Repaint: { Styles.sliceScrubber.Draw(controlRect, GUIContent.none, id); float normalizedPosition = Mathf.Lerp(scrubberRect.x, scrubberRect.xMax, m_Slice / (float)(t.depth - 1)); TimeArea.DrawPlayhead(normalizedPosition, scrubberRect.yMin, scrubberRect.yMax, 2f, (GUIUtility.keyboardControl == id) ? 1f : 0.5f); break; } } using (new EditorGUI.DisabledGroupScope(m_Slice <= 0)) { if (GUI.Button(prevFrameRect, Styles.prevSliceIcon, Styles.stepSlice)) { m_Slice--; } } using (new EditorGUI.DisabledGroupScope(m_Slice >= t.depth - 1)) { if (GUI.Button(nextFrameRect, Styles.nextSliceIcon, Styles.stepSlice)) { m_Slice++; } } }