Ejemplo n.º 1
0
        public static int TimeScrubber(Rect rect, int t, int frameRate, FrameRange range)
        {
            //		Rect actualRect = rect;
            //		actualRect.xMax -= 20; // buffer on the right

            Rect clickRect = rect;

            clickRect.yMin = clickRect.yMax - TIMELINE_SCRUBBER_HEIGHT;

            int length = range.Length;

            int controlId = EditorGUIUtility.GetControlID(FocusType.Passive);

            switch (Event.current.type)
            {
            case EventType.Repaint:
                int frames = range.Start;

                float width = rect.width;

                int maxFramesBetweenSteps = Mathf.Max(1, Mathf.FloorToInt(width / MIN_PIXELS_BETWEEN_FRAMES));

                int numFramesPerStep = Mathf.Max(1, length / maxFramesBetweenSteps);

                // multiple of 60 fps?
                if (numFramesPerStep < 30)
                {
                    if (numFramesPerStep <= 12)
                    {
                        if (numFramesPerStep != 5)
                        {
                            if (12 % numFramesPerStep != 0)
                            {
                                numFramesPerStep = 12;
                            }
                        }
                    }
                    else
                    {
                        numFramesPerStep = 30;
                    }
                }
                else if (numFramesPerStep < 60)
                {
                    numFramesPerStep = 60;
                }
                else
                {
                    int multiplesOf60 = numFramesPerStep / 60;
                    numFramesPerStep = (multiplesOf60 + 1) * 60;
                }

                int numFramesIter = numFramesPerStep < 30 ? 1 : numFramesPerStep / 10;

                Vector3 pt = new Vector3(rect.x, rect.yMax - TIMELINE_SCRUBBER_TEXT_HEIGHT, 0);

                Rect backgroundRect = clickRect;
                backgroundRect.xMin  = 0;
                backgroundRect.xMax += FSequenceEditor.RIGHT_BORDER;

//				GUI.color = new Color( 0.15f, 0.15f, 0.15f, 1f );//FGUI.GetTimelineColor();
//				GUI.DrawTexture( backgroundRect, EditorGUIUtility.whiteTexture );
//				GUI.color = Color.white;
                GUI.color = GetTextColor();                 // a little darker than it is originally to stand out
                GUI.Label(backgroundRect, GUIContent.none, FGUI.GetTimeScrubberStyle());

                Handles.color = GetLineColor();

                //			int framesBetweenSteps = maxFramesBetweenSteps / (maxFramesBetweenSteps * 10 / MIN_PIXELS_BETWEEN_FRAMES);
                //			float pixelsBetweenSteps = minPixelsBetweenSteps / framesBetweenSteps;
                //
                //			Debug.Log ( maxFramesBetweenSteps + " " + minPixelsBetweenSteps + " vs " + framesBetweenSteps + " " + pixelsBetweenSteps );

                GUIStyle labelStyle = new GUIStyle(EditorStyles.boldLabel);
                labelStyle.normal.textColor = Color.white;
                labelStyle.alignment        = TextAnchor.UpperCenter;

                GUI.contentColor = FGUI.GetLineColor();

                frames = (frames / numFramesIter) * numFramesIter;
                while (frames <= range.End)
                {
                    pt.x = rect.x + (width * ((float)(frames - range.Start) / length));

                    if (pt.x >= rect.x)
                    {
                        if (frames % numFramesPerStep == 0)
                        {
                            Handles.DrawLine(pt, pt - new Vector3(0, rect.height - TIMELINE_SCRUBBER_TEXT_HEIGHT, 0));

                            GUI.Label(new Rect(pt.x - 30, pt.y, 60, TIMELINE_SCRUBBER_TEXT_HEIGHT), FUtility.GetTime(frames, frameRate), labelStyle);
                        }
                        else
                        {
                            Vector3 smallTickPt = pt;
                            smallTickPt.y -= TIMELINE_SCRUBBER_TICK_HEIGHT;
                            Handles.DrawLine(smallTickPt, smallTickPt - new Vector3(0, TIMELINE_SCRUBBER_TICK_HEIGHT, 0));
                        }
                    }

                    frames += numFramesIter;
                }

                if (t >= 0 && range.Contains(t))
                {
                    Vector3 tStart = new Vector3(rect.x + (width * ((float)(t - range.Start) / length)), rect.yMin, 0);
                    Vector3 tEnd   = tStart;
                    tEnd.y = rect.yMax - TIMELINE_SCRUBBER_TEXT_HEIGHT;

                    Handles.color = Color.red;
                    Handles.DrawLine(tStart, tEnd);

                    GUI.contentColor = Color.red;
                    GUI.Label(new Rect(tEnd.x - 30, tEnd.y, 60, TIMELINE_SCRUBBER_TEXT_HEIGHT), FUtility.GetTime(t, frameRate), labelStyle);
                    GUI.contentColor = FGUI.GetTextColor();
                }

                GUI.color        = Color.white;
                GUI.contentColor = Color.white;

                Handles.color = GetLineColor();

                Handles.DrawLine(new Vector3(rect.x, rect.yMin, 0), new Vector3(rect.x, rect.yMax - TIMELINE_SCRUBBER_HEIGHT, 0));

                break;

            case EventType.MouseDown:
                if (EditorGUIUtility.hotControl == 0 && clickRect.Contains(Event.current.mousePosition))
                {
                    EditorGUIUtility.hotControl = controlId;
                }
                goto case EventType.MouseDrag;

            case EventType.MouseDrag:
                if (EditorGUIUtility.hotControl == controlId)
                {
                    Rect touchRect = rect;
                    touchRect.yMin = touchRect.yMax - TIMELINE_SCRUBBER_HEIGHT;
                    //			if( touchRect.Contains( Event.current.mousePosition ) )
                    {
                        //				Debug.Log( (Event.current.mousePosition.x - touchRect.xMin /
                        t = Mathf.Clamp(range.Start + Mathf.RoundToInt(((Event.current.mousePosition.x - touchRect.xMin) / touchRect.width) * range.Length), range.Start, range.End);
                        Event.current.Use();
                    }
                }
                break;

            case EventType.MouseUp:
                if (EditorGUIUtility.hotControl == controlId)
                {
                    EditorGUIUtility.hotControl = 0;
                    Event.current.Use();
                }
                break;
            }
            rect.height = TIMELINE_SCRUBBER_HEIGHT;

            return(t);
        }