private Vector3 GetLinkStartPosition(StateEditorGUI state, int linkIndex = 0)
                {
                    float fraction     = 1.0f / (state.GetEditableObject().GetEditorLinks().Length + 1.0f);
                    float edgeFraction = fraction * (1 + linkIndex);

                    Rect stateRect = GetScreenRect(state.GetBounds());

                    return(new Vector3(Mathf.Round(stateRect.x + stateRect.width * edgeFraction), Mathf.Round(stateRect.y + stateRect.height - StateEditorGUI.kShadowSize - 1.0f), 0));
                }
                protected override State CreateCopyFrom(SerializedObjectEditorGUI <State> editorGUI)
                {
                    StateEditorGUI timeLineGUI = (StateEditorGUI)editorGUI;
                    State          newState    = Serializer.CreateCopy(timeLineGUI.GetEditableObject());

                    newState._editorDescription = timeLineGUI.GetStateDescription() + " (Copy)";
                    newState._stateId           = GenerateNewStateId();

                    return(newState);
                }
                protected override void OnLeftMouseDown(UnityEngine.Event inputEvent)
                {
                    //Dragging state links
                    for (int i = 0; i < _editableObjects.Count; i++)
                    {
                        StateEditorGUI state = (StateEditorGUI)_editableObjects[i];

                        StateMachineEditorLink[] links = state.GetEditableObject().GetEditorLinks();

                        if (links != null)
                        {
                            float scale      = 1.0f;
                            float linkRadius = Mathf.Round(kLinkIconWidth * 0.5f * scale) + 2.0f;

                            for (int j = 0; j < links.Length; j++)
                            {
                                Vector3 startPos = GetLinkStartPosition(state, j);
                                Vector2 toField  = inputEvent.mousePosition - new Vector2(startPos.x, startPos.y);

                                if (toField.magnitude < linkRadius)
                                {
                                    _dragMode               = eDragType.Custom;
                                    _draggingState          = state;
                                    _draggingStateLink      = links[j];
                                    _draggingStateLinkIndex = j;
                                    _dragPos      = inputEvent.mousePosition;
                                    _dragAreaRect = new Rect(-1.0f, -1.0f, 0.0f, 0.0f);
                                    _selectedObjects.Clear();
                                    return;
                                }
                            }
                        }
                    }

                    //Normal input
                    base.OnLeftMouseDown(inputEvent);

                    //Double clicking
                    StateEditorGUI clickedOnState = _draggedObject as StateEditorGUI;

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

                        _lastClickedState = clickedOnState;
                        _lastClickTime    = EditorApplication.timeSinceStartup;
                    }
                }
                private void SetViewToStatemachine()
                {
                    if (_editedState != null)
                    {
                        ((TimelineState)_editedState.GetEditableObject())._timeline = _timelineEditor.ConvertToTimeline();
                        _timelineEditor.SetTimeline(new Timeline());
                        _editedState = null;
                    }

                    _editorPrefs._stateId = -1;
                    SaveEditorPrefs();

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

                    StateEditorGUI clickedOnState = _draggedObject as StateEditorGUI;

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

                        _lastClickedState = clickedOnState;
                        _lastClickTime    = EditorApplication.timeSinceStartup;
                    }
                }
                protected override void OnStopDragging(UnityEngine.Event inputEvent, bool cancelled)
                {
                    if (_dragMode == eDragType.Custom)
                    {
                        if (!cancelled)
                        {
                            Vector2 gridPos = GetEditorPosition(UnityEngine.Event.current.mousePosition);

                            StateEditorGUI draggedOnToState = null;

                            //Check mouse is over a state
                            foreach (StateEditorGUI editorGUI in _editableObjects)
                            {
                                if (editorGUI.GetBounds().Contains(gridPos))
                                {
                                    draggedOnToState = editorGUI;

                                    // Check its moved more than
                                    break;
                                }
                            }

                            if (draggedOnToState != null)
                            {
                                _draggingStateLink.SetStateRef(new StateRef(draggedOnToState.GetStateId()));
                            }
                            else
                            {
                                _draggingStateLink.SetStateRef(new StateRef());
                            }
                        }

                        inputEvent.Use();
                        _dragMode = eDragType.NotDragging;

                        _draggingState          = null;
                        _draggingStateLink      = new StateMachineEditorLink();
                        _draggingStateLinkIndex = 0;
                    }
                    else
                    {
                        base.OnStopDragging(inputEvent, cancelled);
                    }
                }
                private void RenderExternalLink(StateMachineEditorLink link, StateEditorGUI fromState, int linkIndex)
                {
                    StateMachineExternalStateEditorGUI externalState = null;

                    StateRef stateRef = link.GetStateRef();

                    //Find external link for this state
                    foreach (StateEditorGUI state in _editableObjects)
                    {
                        StateMachineExternalStateEditorGUI extState = state as StateMachineExternalStateEditorGUI;

                        if (extState != null)
                        {
                            StateRef extStateRef = extState.ExternalStateRef.GetStateRef();

                            if (extStateRef.GetStateID() == stateRef.GetStateID() && extStateRef.GetExternalFile().GetFileGUID() == stateRef.GetExternalFile().GetFileGUID())
                            {
                                externalState = extState;
                                break;
                            }
                        }
                    }

                    //If none exists, create a new one
                    if (externalState == null)
                    {
                        externalState = (StateMachineExternalStateEditorGUI)AddNewObject(new StateMachineExternalState());
                        externalState.ExternalStateRef = link;
                        _editableObjects.Add(externalState);
                    }

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

                    RenderLink(link.GetDescription(), fromState, externalState, linkIndex);
                }
                private void SwitchViewsIfNeeded()
                {
                    if (_requestSwitchViews)
                    {
                        if (_requestSwitchViewsStateId != -1)
                        {
                            StateEditorGUI state = GetStateGUI(_requestSwitchViewsStateId);

                            if (state != null)
                            {
                                if (state.GetEditableObject() is TimelineState)
                                {
                                    TimelineState timelineState = (TimelineState)state.GetEditableObject();

                                    _currentMode = eMode.ViewingTimelineState;
                                    _editedState = state;
                                    _timelineEditor.SetTimeline(timelineState._timeline);

                                    _editorPrefs._stateId = _requestSwitchViewsStateId;
                                    SaveEditorPrefs();

                                    foreach (StateEditorGUI stateView in _selectedObjects)
                                    {
                                        GetEditorWindow().OnDeselectObject(stateView);
                                    }
                                }
                                else
                                {
                                    SetViewToStatemachine();
                                    _selectedObjects.Clear();
                                    _selectedObjects.Add(state);
                                    Selection.activeObject = state;
                                }
                            }
                        }
                        else
                        {
                            SetViewToStatemachine();
                        }

                        _requestSwitchViews = false;
                    }
                }
                private void RenderLinksForState(StateEditorGUI state)
                {
                    StateMachineEditorLink[] links = state.GetEditableObject().GetEditorLinks();

                    if (links != null)
                    {
                        for (int j = 0; j < links.Length; j++)
                        {
                            StateRef stateRef = links[j].GetStateRef();

                            if (stateRef.IsInternal())
                            {
                                StateEditorGUI toState = FindStateForLink(stateRef);
                                RenderLink(links[j].GetDescription(), state, toState, j);
                            }
                            else
                            {
                                RenderExternalLink(links[j], state, j);
                            }
                        }
                    }
                }
                private void RenderLink(string description, StateEditorGUI fromState, StateEditorGUI toState, int linkIndex)
                {
                    Vector3 startPos = GetLinkStartPosition(fromState, linkIndex);

                    RenderLinkIcon(startPos, fromState.GetColor(_style));

                    if (toState != null)
                    {
                        Vector3 endPos = GetLinkEndPosition(toState, linkIndex);

                        RenderLinkLine(startPos, endPos, _dragMode == eDragType.Custom ? _style._linkInactiveColor : _style._linkColor);

                        Vector2 textSize     = _style._linkTextStyle.CalcSize(new GUIContent(description));
                        float   lineFraction = 0.5f;
                        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);

                        //Draw shadow
                        EditorUtils.DrawColoredRoundedBox(new Rect(labelPos.x + StateEditorGUI.kShadowSize, labelPos.y + StateEditorGUI.kShadowSize, labelPos.width, labelPos.height), new Color(0.0f, 0.0f, 0.0f, 0.35f));

                        //Draw white background
                        EditorUtils.DrawColoredRoundedBox(labelPos, _style._linkDescriptionColor);

                        Color origColor = GUI.backgroundColor;
                        GUI.backgroundColor = Color.clear;
                        GUI.Label(labelPos, description, _style._linkTextStyle);
                        GUI.backgroundColor = origColor;
                    }
                }
                private Vector3 GetLinkEndPosition(StateEditorGUI state, int linkIndex = 0)
                {
                    Rect stateRect = GetScreenRect(state.GetBounds());

                    return(new Vector3(Mathf.Round(stateRect.x + stateRect.width / 2.0f) + 0.5f, Mathf.Round(stateRect.y - kArrowHeight - 1.0f) + 0.5f, 0));
                }
                protected override void RenderObjectsOnGrid()
                {
                    _style._stateTextStyle.fontSize         = Mathf.RoundToInt(_style._stateTextStyleFontSize * _currentZoom);
                    _style._externalStateTextStyle.fontSize = Mathf.RoundToInt(_style._externalStateTextStyleFontSize * _currentZoom);
                    _style._linkTextStyle.fontSize          = Mathf.RoundToInt(_style._linkTextStyleFontSize * _currentZoom);
                    _style._noteTextStyle.fontSize          = Mathf.RoundToInt(_style._noteTextStyleFontSize * _currentZoom);

                    SetupExternalState();

                    List <StateEditorGUI> toRender = new List <StateEditorGUI>();

                    foreach (StateEditorGUI editorGUI in _editableObjects)
                    {
                        if (!(editorGUI is StateMachineExternalStateEditorGUI))
                        {
                            toRender.Add(editorGUI);
                        }
                    }

                    StateEditorGUI dragHighlightState = null;

                    //Update state bounds
                    foreach (StateEditorGUI state in toRender)
                    {
                        state.CalcBounds(_style);
                    }

                    //Check dragging a state link onto a state
                    if (_dragMode == eDragType.Custom)
                    {
                        Vector2 gridPos = GetEditorPosition(UnityEngine.Event.current.mousePosition);

                        //Check mouse is over a state
                        foreach (StateEditorGUI editorGUI in _editableObjects)
                        {
                            if (editorGUI.GetBounds().Contains(gridPos))
                            {
                                dragHighlightState = editorGUI;
                                break;
                            }
                        }
                    }

                    //Render each state
                    foreach (StateEditorGUI state in toRender)
                    {
                        bool  selected   = (_dragMode != eDragType.Custom && _selectedObjects.Contains(state)) || dragHighlightState == state;
                        float borderSize = state.GetBorderSize(selected);

                        Color borderColor = selected ? _style._stateBackgroundSelected : _style._stateBackground;
#if DEBUG
                        if (_debugging && _playModeHighlightedState != null && state.GetStateId() == _playModeHighlightedState._stateId)
                        {
                            borderColor = _style._debugCurrentStateColor;
                            borderSize  = 2.0f;
                        }
#endif
                        Rect renderedRect = GetScreenRect(state.GetBounds());

                        Color stateColor = state.GetColor(_style);
                        state.Render(renderedRect, borderColor, stateColor, _style, borderSize);

                        RenderLinksForState(state);
                    }

                    //Render dragging link
                    if (_dragMode == eDragType.Custom)
                    {
                        Vector3 startPos = GetLinkStartPosition(_draggingState, _draggingStateLinkIndex);
                        Vector3 endPos   = UnityEngine.Event.current.mousePosition + new Vector2(0, -5);

                        RenderLinkLine(startPos, endPos, _style._linkColor);
                    }


                    CleanupExternalState();
                }
 private void OnDoubleClickState(StateEditorGUI state)
 {
     state.OnDoubleClick();
 }
Beispiel #14
0
                private void RenderLink(string description, StateEditorGUI fromState, StateEditorGUI 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 - StateEditorGUI.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);

                    //Draw shadow
                    EditorUtils.DrawColoredRoundedBox(new Rect(labelPos.x + StateEditorGUI.kShadowSize, labelPos.y + StateEditorGUI.kShadowSize, labelPos.width, labelPos.height), new Color(0.0f, 0.0f, 0.0f, 0.35f));

                    //Draw white background
                    EditorUtils.DrawColoredRoundedBox(labelPos, _style._linkDescriptionColor);

                    Color origColor = GUI.backgroundColor;

                    GUI.backgroundColor = Color.clear;
                    GUI.Label(labelPos, description, _style._linkTextStyle);
                    GUI.backgroundColor = origColor;
                }