Example #1
0
        private bool LoadFrom(AIStorage data, bool refreshState)
        {
            _aiStorage = data;

            try
            {
                if (EditorApplication.isPlaying)
                {
                    _ai = _visualizedAI = AIManager.GetAI(new Guid(data.aiId));
                }
                else
                {
                    _ai = _visualizedAI = SerializationMaster.Deserialize <UtilityAI>(_aiStorage.configuration);
                }

                this.canvas = GuiSerializer.Deserialize(this, _aiStorage.editorConfiguration);
            }
            catch (Exception e)
            {
                if (EditorUtility.DisplayDialog("Load Error", "The AI could not be loaded, deserialization failed - see the console for details.\n\nDo you wish to open the AI repair tool?.", "Yes", "No"))
                {
                    RepairWindow.ShowWindow(data.name, data.aiId);
                }

                Debug.LogWarning("Failed to load AI: " + e.Message);
                return(false);
            }

            var selectorViews = this.canvas.selectorViews.ToArray();
            int selectorCount = _ai.selectorCount;

            if (!VerifyCountMatch(selectorCount, selectorViews.Length))
            {
                return(false);
            }

            for (int i = 0; i < selectorCount; i++)
            {
                if (!selectorViews[i].Reconnect(_ai[i]))
                {
                    return(false);
                }
            }

            if (refreshState)
            {
                this.inspectorState.Refresh();
            }

            return(true);
        }
 internal static void CopyToClipboard(AIUI sourceUI)
 {
     if (sourceUI.currentAction != null)
     {
         EditorGUIUtility.systemCopyBuffer = GuiSerializer.Serialize(sourceUI.currentAction);
     }
     else if (sourceUI.currentQualifier != null)
     {
         EditorGUIUtility.systemCopyBuffer = GuiSerializer.Serialize(sourceUI.currentQualifier);
     }
     else if (sourceUI.selectedViews.Count > 0)
     {
         EditorGUIUtility.systemCopyBuffer = GuiSerializer.Serialize(sourceUI.selectedViews);
     }
 }
Example #3
0
        internal void Save(string newName)
        {
            if (_ai == null || _ai.rootSelector == null)
            {
                return;
            }

            // new name is not null or empty when using 'Save As'
            if (!string.IsNullOrEmpty(newName))
            {
                this.name = StoredAIs.EnsureValidName(newName, _aiStorage);

                // If we are saving under a new name (as opposed to saving a new AI), we need to save copy of the current AI with new Ids.
                if (_aiStorage != null)
                {
                    _aiStorage = null;
                    _ai.RegenerateIds();
                }
            }

            bool saveNew = (_aiStorage == null);

            if (saveNew)
            {
                _aiStorage = AIStorage.Create(_ai.id.ToString(), this.name);
                StoredAIs.AIs.Add(_aiStorage);
                AssetPath.EnsurePath(AIGeneralSettings.instance.storagePath);
                var assetPath = AssetPath.Combine(AIGeneralSettings.instance.storagePath, this.name + ".asset");
                AssetDatabase.CreateAsset(_aiStorage, assetPath);
            }

            _aiStorage.version             = _aiVersion.version;
            _aiStorage.configuration       = SerializationMaster.Serialize(_ai);
            _aiStorage.editorConfiguration = GuiSerializer.Serialize(this.canvas);

            EditorUtility.SetDirty(_aiStorage);
            AssetDatabase.SaveAssets();
            this.isDirty = false;
            _undoRedo.SetSavePoint();

            if (saveNew && UserSettings.instance.autoGenerateNameMap)
            {
                AINameMapGenerator.WriteNameMapFile();
            }
        }
        internal static void PasteFromClipboard(AIUI targetUi, Vector2 mousePos)
        {
            if (string.IsNullOrEmpty(EditorGUIUtility.systemCopyBuffer))
            {
                // nothing in the clipboard
                Debug.LogWarning("Could not paste from clipboard; clipboard is empty.");
                return;
            }

            try
            {
                GuiSerializer.DeserializeSnippet(EditorGUIUtility.systemCopyBuffer, targetUi, mousePos, true);
                targetUi.isDirty = true;
            }
            catch
            {
                Debug.LogWarning("Could not paste from clipboard; invalid data detected.");
            }
        }
        internal static void CutToClipboard(AIUI sourceUI)
        {
            if (sourceUI.currentAction != null)
            {
                EditorGUIUtility.systemCopyBuffer = GuiSerializer.Serialize(sourceUI.currentAction);
                sourceUI.RemoveAction(sourceUI.currentAction);
            }
            else if (sourceUI.currentQualifier != null)
            {
                if (sourceUI.currentQualifier.isDefault)
                {
                    EditorUtility.DisplayDialog("Invalid Action", "The default qualifier cannot be cut. Use copy instead.", "Ok");
                    return;
                }

                EditorGUIUtility.systemCopyBuffer = GuiSerializer.Serialize(sourceUI.currentQualifier);
                sourceUI.RemoveQualifier(sourceUI.currentQualifier);
            }
            else if (sourceUI.selectedViews.Count > 0)
            {
                var selectedViews = sourceUI.selectedViews;
                var cutCount      = selectedViews.Count;
                for (int i = 0; i < cutCount; i++)
                {
                    var sv = selectedViews[i] as SelectorView;
                    if (sv != null && object.ReferenceEquals(sourceUI.rootSelector, sv.selector))
                    {
                        EditorUtility.DisplayDialog("Invalid Action", "The root selector can not be part of a cut operation.", "Ok");
                        return;
                    }
                }

                EditorGUIUtility.systemCopyBuffer = GuiSerializer.Serialize(sourceUI.selectedViews);

                using (sourceUI.undoRedo.bulkOperation)
                {
                    for (int i = cutCount - 1; i >= 0; i--)
                    {
                        sourceUI.RemoveView(selectedViews[i]);
                    }
                }
            }
        }