private void RenderLink(string description, TimelineStateEditorGUI fromState, TimelineStateEditorGUI toState, float edgeFraction)
                {
                    Rect fromStateRect = GetScreenRect(fromState.GetBounds());
                    Rect toStateRect   = GetScreenRect(toState.GetBounds());

                    Vector3 startPos     = new Vector3(fromStateRect.x + fromStateRect.width * edgeFraction, fromStateRect.y + fromStateRect.height - TimelineStateEditorGUI.kShadowSize - 1.0f, 0);
                    Vector3 endPos       = new Vector3(Mathf.Round(toStateRect.x + toStateRect.width / 2.0f) + 0.5f, Mathf.Round(toStateRect.y - kArrowHeight - 1.0f) + 0.5f, 0);
                    Vector3 startTangent = startPos + Vector3.up * 50.0f;
                    Vector3 endTangent   = endPos - Vector3.up * 50.0f;

                    Handles.BeginGUI();
                    Handles.color = _style._linkColor;
                    Handles.DrawBezier(startPos, endPos, startTangent, endTangent, _style._linkColor, EditorUtils.BezierAATexture, 2f);
                    Handles.DrawAAConvexPolygon(new Vector3[] { new Vector3(endPos.x, endPos.y + kArrowHeight, 0.0f), new Vector3(endPos.x + kArrowWidth, endPos.y, 0.0f), new Vector3(endPos.x - kArrowWidth, endPos.y, 0.0f) });
                    Handles.EndGUI();

                    Vector2 textSize     = _style._linkTextStyle.CalcSize(new GUIContent(description));
                    float   lineFraction = edgeFraction;
                    Rect    labelPos     = new Rect(startPos.x + ((endPos.x - startPos.x) * lineFraction) - (textSize.x * 0.5f), startPos.y + ((endPos.y - startPos.y) * lineFraction) - (textSize.y * 0.5f), textSize.x, textSize.y);

                    Color origColor = GUI.backgroundColor;

                    GUI.backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.35f);
                    GUI.Label(new Rect(labelPos.x + TimelineStateEditorGUI.kShadowSize, labelPos.y + TimelineStateEditorGUI.kShadowSize, labelPos.width, labelPos.height), GUIContent.none, _style._linkTextStyle);

                    GUI.backgroundColor = _style._linkDescriptionColor;
                    GUI.Label(labelPos, description, _style._linkTextStyle);

                    GUI.backgroundColor = origColor;
                }
                private void RenderExternalState(EditorStateLink link, TimelineStateEditorGUI fromState, float edgeFraction)
                {
                    TimelineStateEditorGUI externalState = null;

                    //Find external link for this state
                    foreach (TimelineStateEditorGUI state in _editableObjects)
                    {
                        if (state.IsExternal && state.ExternalStateRef._stateId == link._timeline._stateId && state.ExternalStateRef._file._fileGUID == link._timeline._file._fileGUID)
                        {
                            externalState = state;
                            break;
                        }
                    }

                    //If none exists, create a new one
                    if (externalState == null)
                    {
                        externalState                  = (TimelineStateEditorGUI)AddNewObject(new TimelineState());
                        externalState.IsExternal       = true;
                        externalState.ExternalStateRef = link._timeline;
                        _editableObjects.Add(externalState);
                    }

                    if (!externalState.ExternalHasRendered)
                    {
                        externalState.CalcBounds(_style._noteTextStyle, _style._externalStateTextStyle);
                        bool  selected     = _selectedObjects.Contains(externalState);
                        Color borderColor  = selected ? _style._stateBackgroundSelected : _style._stateBackground;
                        Rect  renderedRect = GetScreenRect(externalState.GetBounds());
                        externalState.Render(renderedRect, borderColor, _style._externalStateColor, _style._noteTextStyle, _style._externalStateTextStyle, selected ? 2.0f : 1.0f);
                        externalState.ExternalHasRendered = true;
                    }

                    RenderLink(link._description, fromState, externalState, edgeFraction);
                }
                protected override SerializedObjectEditorGUI <TimelineState> CreateObjectEditorGUI(TimelineState state)
                {
                    TimelineStateEditorGUI editorGUI = TimelineStateEditorGUI.CreateInstance <TimelineStateEditorGUI>();

                    editorGUI.Init(this, state);
                    editorGUI.CalcBounds(_style._noteTextStyle, _style._stateTextStyle);
                    return(editorGUI);
                }
 public void LoadExternalState(TimelineStateEditorGUI state)
 {
     if (ShowOnLoadSaveChangesDialog())
     {
         string fileName = AssetDatabase.GetAssetPath(state.ExternalStateRef._file._editorAsset);
         LoadFile(fileName);
     }
 }
                private void UpdateInPlayMode()
                {
                    _debugging = false;

                    if (_editorPrefs._debug)
                    {
                        StateMachine stateMachine = _editorPrefs._debugObject.GetComponent();
                        TimelineStateMachineDebug.StateInfo stateInfo = TimelineStateMachineDebug.GetStateInfo(stateMachine != null ? stateMachine.gameObject : null);

                        if (stateInfo != null)
                        {
                            _debugging = true;

                            if (_currentFileName != stateInfo._fileName && stateInfo._fileName != null)
                            {
                                _currentFileName = stateInfo._fileName;
                                SetStateMachine(stateInfo._stateMachine);
                                _stateEditor.SetTimeline(null);
                            }

                            switch (_currentMode)
                            {
                            case eMode.ViewingStateMachine:
                            {
                                if (_playModeHighlightedState != stateInfo._state)
                                {
                                    GetEditorWindow().DoRepaint();
                                }

                                _playModeHighlightedState = stateInfo._state;

                                if (_editorPrefs._debugLockFocus)
                                {
                                    CenterCameraOn(GetTimelineStateGUI(_playModeHighlightedState._stateId));
                                }
                            }
                            break;

                            case eMode.ViewingState:
                            {
                                if (stateInfo._state._stateId != _editedState.GetStateId())
                                {
                                    _editedState = GetTimelineStateGUI(stateInfo._state._stateId);
                                    _stateEditor.SetTimeline(stateInfo._state._timeline);
                                }

                                _stateEditor.SetPlayModeCursorTime(stateInfo._time);
                                GetEditorWindow().DoRepaint();
                            }
                            break;
                            }
                        }
                    }
                }
 private void OnDoubleClickState(TimelineStateEditorGUI state)
 {
     if (!state.IsNote)
     {
         if (state.IsExternal)
         {
             LoadExternalState(state);
         }
         else
         {
             SwitchToStateView(state.GetStateId());
         }
     }
 }
                public void SwitchToStatemachineView()
                {
                    if (_editedState != null)
                    {
                        _editedState.GetEditableObject()._timeline = _stateEditor.ConvertToTimeline();
                        _stateEditor.SetTimeline(new Timeline());
                        _editedState = null;
                    }

                    _editorPrefs._stateId = -1;
                    SaveEditorPrefs();

                    _currentMode = eMode.ViewingStateMachine;
                }
                protected override void OnLeftMouseDown(UnityEngine.Event inputEvent)
                {
                    base.OnLeftMouseDown(inputEvent);

                    TimelineStateEditorGUI clickedOnState = _draggedObject as TimelineStateEditorGUI;

                    if (clickedOnState != null)
                    {
                        if (_lastClickedState == clickedOnState && (EditorApplication.timeSinceStartup - _lastClickTime) < kDoubleClickTime)
                        {
                            OnDoubleClickState(clickedOnState as TimelineStateEditorGUI);
                        }

                        _lastClickedState = clickedOnState;
                        _lastClickTime    = EditorApplication.timeSinceStartup;
                    }
                }
                protected override TimelineState CreateCopyFrom(SerializedObjectEditorGUI <TimelineState> editorGUI)
                {
                    TimelineStateEditorGUI timeLineGUI = (TimelineStateEditorGUI)editorGUI;
                    TimelineState          newState    = SerializeConverter.CreateCopy(timeLineGUI.GetEditableObject());

                    if (timeLineGUI.IsNote)
                    {
                        newState._editorDescription = timeLineGUI.GetEditorDescrition();
                    }
                    else
                    {
                        newState._editorDescription = timeLineGUI.GetEditorDescrition() + " (Copy)";
                        newState._stateId           = GenerateNewStateId();
                    }

                    return(newState);
                }
                private void RenderLinksForState(TimelineStateEditorGUI state)
                {
                    Timeline timeline = state.GetEditableObject()._timeline;

                    if (timeline != null)
                    {
                        Event[] events = timeline._events;

                        List <EditorStateLink> stateLinks = new List <EditorStateLink>();

                        foreach (IStateMachineEvent evnt in events)
                        {
                            EditorStateLink[] links = evnt.GetEditorLinks();

                            if (links != null)
                            {
                                stateLinks.AddRange(links);
                            }
                        }

                        float fraction = 1.0f / (stateLinks.Count + 1.0f);
                        float current  = fraction;

                        foreach (EditorStateLink link in stateLinks)
                        {
                            if (link._timeline.IsInternal())
                            {
                                TimelineStateEditorGUI toState = FindStateForLink(link);

                                if (toState != null)
                                {
                                    RenderLink(link._description, state, toState, current);
                                }
                            }
                            else
                            {
                                RenderExternalState(link, state, current);
                            }

                            current += fraction;
                        }
                    }
                }
                public void SwitchToStateView(int stateId)
                {
                    TimelineStateEditorGUI state = GetTimelineStateGUI(stateId);

                    if (state != null)
                    {
                        _currentMode = eMode.ViewingState;
                        _editedState = state;
                        _stateEditor.SetTimeline(_editedState.GetEditableObject()._timeline);

                        _editorPrefs._stateId = stateId;
                        SaveEditorPrefs();

                        foreach (TimelineStateEditorGUI stateView in _selectedObjects)
                        {
                            GetEditorWindow().OnDeselectObject(stateView);
                        }
                    }
                }