void Initialize(ArborFSMInternal stateMachine)
        {
            Undo.RecordObject( this,"Select StateMachine" );

            _CurrentWindow = this;

            FinalizeStateEditor();

            _StateMachine = stateMachine;

            if( _StateMachine != null )
            {
                _StateMachineInstanceID = _StateMachine.GetInstanceID();
            }
            else
            {
                _StateMachineInstanceID = 0;
            }

            _Selection.Clear();

            Repaint();
        }
 public static void Open(ArborFSMInternal stateMachine)
 {
     ArborEditorWindow window = EditorWindow.GetWindow<ArborEditorWindow>("Arbor Editor");
     window.Initialize(stateMachine);
 }
        void DrawToolbar()
        {
            EditorGUILayout.BeginHorizontal(EditorStyles.toolbar );

            if( _StateMachine == null && _StateMachineInstanceID != 0 )
            {
                _StateMachine = EditorUtility.InstanceIDToObject( _StateMachineInstanceID ) as ArborFSMInternal;
                _Selection.Clear();
            }

            EditorGUI.BeginChangeCheck();

            ArborFSMInternal stateMachine = EditorGUILayout.ObjectField( _StateMachine,typeof(ArborFSMInternal),true,GUILayout.Width ( 200 ) ) as ArborFSMInternal;

            if( EditorGUI.EndChangeCheck() )
            {
                Initialize( stateMachine );
            }

            Rect toolBarPosition = GUILayoutUtility.GetRect( 0.0f,20.0f );

            Rect helpButtonPosition = new Rect( toolBarPosition.x + toolBarPosition.width - 18.0f,toolBarPosition.y + 1.0f , 16.0f,toolBarPosition.height - 1.0f );

            Rect gridButtonPosition = new Rect( helpButtonPosition.x - 80.0f - 8.0f,toolBarPosition.y,80.0f,toolBarPosition.height );

            Rect languagePopupPosition = new Rect( gridButtonPosition.x - 100.0f - 8.0f,toolBarPosition.y,100.0f,toolBarPosition.height );

            List<string> languageLabel = new List<string>();
            languageLabel.Add( Localization.GetWord("Auto")+"("+Localization.GetWord(ArborSettings.GetAutoLanguage().ToString()) + ")" );
            int selectIndex = 0;
            SystemLanguage[] languages = Localization.GetLanguages();
            if( languages != null )
            {
                for( int i=0;i<languages.Length;i++ )
                {
                    SystemLanguage language = languages[i];
                    languageLabel.Add( Localization.GetWord(language.ToString()) );
                    if( !ArborSettings.autoLanguage && language == ArborSettings.language )
                    {
                        selectIndex = i+1;
                    }
                }
            }

            EditorGUI.BeginChangeCheck();
            selectIndex = EditorGUI.Popup( languagePopupPosition,selectIndex,languageLabel.ToArray(),EditorStyles.toolbarPopup );
            if( EditorGUI.EndChangeCheck() )
            {
                if( selectIndex == 0 )
                {
                    ArborSettings.autoLanguage = true;
                }
                else
                {
                    ArborSettings.autoLanguage = false;
                    ArborSettings.language = languages[selectIndex-1];
                }
            }

            if( GUI.Button( gridButtonPosition,Localization.GetWord("Grid"),EditorStyles.toolbarDropDown ) )
            {
                GridSettingsWindow.instance.Init( gridButtonPosition );
            }

            EditorGUITools.HelpButton( helpButtonPosition,"http://caitsithware.com/wordpress/assetstore/arbor","Open Reference" );

            EditorGUILayout.EndHorizontal();
        }
        private static State GetStateFromPosition( ArborFSMInternal stateMachine,Vector2 position )
        {
            foreach( State state in stateMachine.states )
            {
                if( !state.resident && state.position.Contains( position ) )
                {
                    return state;
                }
            }

            return null;
        }
 public static State[] PasteStates( ArborFSMInternal stateMachine,Vector2 position )
 {
     return DuplicateStates( stateMachine,position,_CopyStates.ToArray() );
 }
        public static State[] DuplicateStates( ArborFSMInternal stateMachine,Vector2 position,State[] sourceStates )
        {
            List<State> duplicateStates = new List<State>();

            Vector2 minPosition = new Vector2( float.MaxValue,float.MaxValue );

            foreach( State sourceState in sourceStates )
            {
                minPosition.x = Mathf.Min( sourceState.position.x,minPosition.x );
                minPosition.y = Mathf.Min( sourceState.position.y,minPosition.y );
            }

            position -= minPosition;

            foreach( State sourceState in sourceStates )
            {
                State state = stateMachine.CreateState( sourceState.resident );

                if( state != null )
                {
                    state.name = sourceState.name;
                    state.position = sourceState.position;
                    state.position.x += position.x;
                    state.position.y += position.y;

                    ComponentUtility.EditorAddComponent cachedAddComponent = ComponentUtility.editorAddComponent;
                    ComponentUtility.editorAddComponent = null;

                    foreach( StateBehaviour sourceBehaviour in sourceState.behaviours )
                    {
                        StateBehaviour behaviour = state.AddBehaviour( sourceBehaviour.GetType () );

                        if( behaviour != null )
                        {
                            CopyBehaviour( sourceBehaviour,behaviour,true );
                        }
                    }

                    ComponentUtility.editorAddComponent = cachedAddComponent;

                    duplicateStates.Add( state );
                }
            }

            return duplicateStates.ToArray();
        }
        public static void CopyStates( State[] states )
        {
            if( _StateClipboard != null )
            {
                Object.DestroyImmediate( _StateClipboard );
                _StateClipboard = null;
            }
            _StateClipboard = clipboard.AddComponent<ArborFSMInternal>();
            _StateClipboard.hideFlags |= HideFlags.HideAndDontSave;

            _CopyStates.Clear ();

            foreach( State state in states )
            {
                State copyState = _StateClipboard.CreateState( state.resident );

                copyState.name = state.name;
                copyState.position = state.position;

                foreach( StateBehaviour behaviour in state.behaviours )
                {
                    StateBehaviour copyBehaviour = copyState.AddBehaviour( behaviour.GetType () );

                    copyBehaviour.hideFlags |= HideFlags.HideAndDontSave;

                    EditorUtility.CopySerialized( behaviour,copyBehaviour );
                }

                _CopyStates.Add( copyState );
            }
        }