float MousePositionToTime(Event evt)
        {
            float width = m_ContentRect.width;
            float time  = Mathf.Max(evt.mousePosition.x / width * state.VisibleTimeSpan + state.MinVisibleTime, 0);

            time = TimelineState.SnapToFrame(time, TimelineState.SnapMode.SnapToFrame);
            return(time);
        }
 public TracingTimeline(State vsWindowState, IMGUIContainer imguiContainer)
 {
     m_VSWindowState  = vsWindowState;
     m_ImguiContainer = imguiContainer;
     m_TimeArea       = new TimeArea();
     m_Overlay        = new AnimEditorOverlay();
     m_State          = new TimelineState(m_TimeArea);
     m_Overlay.state  = m_State;
 }
        public void OnGUI(Rect timeRect)
        {
            // sync timeline and tracing toolbar state both ways
            m_State.CurrentTime = TimelineState.FrameToTime(m_VSWindowState.currentTracingFrame);

            if (EditorApplication.isPlaying && !EditorApplication.isPaused)
            {
                if (m_State.MaxVisibleTime < m_State.CurrentTime + k_MinTimeVisibleOnTheRight)
                {
                    m_TimeArea.SetShownRange(m_State.CurrentTime + k_MinTimeVisibleOnTheRight - m_State.MaxVisibleTime);
                }
            }

            m_Overlay.HandleEvents();
            int timeChangedByTimeline = TimelineState.TimeToFrame(m_State.CurrentTime);

            // force graph update
            if (timeChangedByTimeline != m_VSWindowState.currentTracingFrame)
            {
                m_VSWindowState.currentTracingStep = -1;
            }
            m_VSWindowState.currentTracingFrame = timeChangedByTimeline;

            // time scales
            GUILayout.BeginArea(timeRect);
            m_TimeArea.Draw(timeRect);
            GUILayout.EndArea();
            GUI.BeginGroup(timeRect);

            DebuggerTracer.GraphTrace trace = DebuggerTracer.GetGraphData((m_VSWindowState?.AssetModel as Object)?.GetInstanceID() ?? -1, false);
            var frameHasDataColor           = new Color32(68, 192, 255, 255);

            if (trace != null && trace.AllFrames.Count > 0)
            {
                float frameDeltaToPixel        = m_TimeArea.FrameDeltaToPixel();
                DebuggerTracer.FrameData first = trace.AllFrames[0];
                DebuggerTracer.FrameData last  = trace.AllFrames[trace.AllFrames.Count - 1];
                float start = m_TimeArea.FrameToPixel(first.Frame);
                float width = frameDeltaToPixel * Mathf.Max(last.Frame - first.Frame, 1);

                EditorGUI.DrawRect(new Rect(start, k_FrameRectangleHeight, width, k_FrameRectangleWidth), frameHasDataColor);
            }

            // playing head
            m_Overlay.OnGUI(timeRect, timeRect);
            GUI.EndGroup();
        }
        public void OnGUI(Rect timeRect)
        {
            // sync timeline and tracing toolbar state both ways
            m_State.CurrentTime = TimelineState.FrameToTime(m_VSWindowState.CurrentTracingFrame);

            m_Overlay.HandleEvents();
            int timeChangedByTimeline = TimelineState.TimeToFrame(m_State.CurrentTime);

            // force graph update
            if (timeChangedByTimeline != m_VSWindowState.CurrentTracingFrame)
            {
                m_VSWindowState.CurrentTracingStep = -1;
            }
            m_VSWindowState.CurrentTracingFrame = timeChangedByTimeline;

            GUI.BeginGroup(timeRect);

            var         debugger = m_VSWindowState?.AssetModel?.GraphModel?.Stencil?.Debugger;
            IGraphTrace trace    = debugger?.GetGraphTrace(m_VSWindowState?.AssetModel?.GraphModel, m_VSWindowState.CurrentTracingTarget);

            if (trace?.AllFrames != null && trace.AllFrames.Count > 0)
            {
                float frameDeltaToPixel = m_TimeArea.FrameDeltaToPixel();
                int   firstFrame        = trace.AllFrames[0].Frame;
                int   lastFrame         = trace.AllFrames[trace.AllFrames.Count - 1].Frame;
                float start             = m_TimeArea.FrameToPixel(firstFrame);
                float width             = frameDeltaToPixel * Mathf.Max(lastFrame - firstFrame, 1);

                // draw active range
                EditorGUI.DrawRect(new Rect(start,
                                            timeRect.yMax - k_FrameRectangleHeight, width, k_FrameRectangleHeight), k_FrameHasDataColor);

                // draw per-node active ranges
                var framesPerNode = IndexFramesPerNode(m_VSWindowState?.AssetModel?.GraphModel, trace, firstFrame, lastFrame, m_VSWindowState.CurrentTracingTarget, out bool invalidated);

                // while recording in unpaused playmode, adjust the timeline to show all data
                // same if the cached data changed (eg. Load a trace dump)
                if (EditorApplication.isPlaying && !EditorApplication.isPaused || invalidated)
                {
                    m_TimeArea.SetShownRange(
                        firstFrame - k_MinTimeVisibleOnTheRight,
                        lastFrame + k_MinTimeVisibleOnTheRight);
                }

                if (framesPerNode != null)
                {
                    INodeModel nodeModelSelected = m_VseGraphView.selection
                                                   .OfType <IHasGraphElementModel>()
                                                   .Select(x => x.GraphElementModel)
                                                   .OfType <INodeModel>()
                                                   .FirstOrDefault();

                    if (nodeModelSelected != null && framesPerNode.TryGetValue(nodeModelSelected.Guid, out HashSet <(int, int)> frames))
                    {
                        foreach ((int, int)frameInterval in frames)
                        {
                            float xStart = m_TimeArea.FrameToPixel(frameInterval.Item1);
                            float xEnd   = m_TimeArea.FrameToPixel(frameInterval.Item2) - frameDeltaToPixel * 0.1f;
                            Rect  rect   = new Rect(
                                xStart,
                                timeRect.yMin,
                                xEnd - xStart,
                                timeRect.yMax);
                            EditorGUI.DrawRect(rect, k_FrameHasNodeColor);
                        }
                    }
                }
            }
            GUI.EndGroup();


            // time scales
            GUILayout.BeginArea(timeRect);
            m_TimeArea.Draw(timeRect);
            GUILayout.EndArea();

            // playing head
            m_Overlay.OnGUI(timeRect, timeRect);
        }