Movement/Editor/DefinitionCreatorEditor.cs

154 lines
6.2 KiB
C#
Raw Permalink Normal View History

2022-11-16 20:58:14 +03:00
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using Syntriax.Modules.Movement.Config;
2022-11-16 21:28:55 +03:00
using System;
2022-11-21 14:01:01 +03:00
using Syntriax.Modules.Factory;
2022-11-16 20:58:14 +03:00
namespace Syntriax.Modules.Movement.Editor
{
public class DefinitionCreatorEditor : EditorWindow
{
ReorderableList list;
public MovementDefinition movementDefinition = new MovementDefinition();
private SerializedObject serializedObject;
[MenuItem("Syntriax/Modules/Movement/Definition Creator")]
private static void ShowWindow()
{
var window = GetWindow<DefinitionCreatorEditor>();
window.titleContent = new GUIContent("Definition Creator");
window.Reset();
window.Show();
}
2022-11-16 21:28:55 +03:00
private void Reset() => LoadPresetQuick();
2022-11-16 20:58:14 +03:00
2022-11-16 21:28:55 +03:00
private void OnEnable() => UpdateSerializedObject();
2022-11-16 20:58:14 +03:00
private void OnGUI()
{
GUILayout.Space(10);
2022-11-16 21:28:55 +03:00
GUILayout.BeginHorizontal();
bool quickPresetButtonPressed = GUILayout.Button("Quick Preset");
GUILayout.Space(10);
bool longerPresetButtonPressed = GUILayout.Button("Longer Preset");
GUILayout.EndHorizontal();
GUILayout.Space(10);
2022-11-16 20:58:14 +03:00
SerializedProperty movementDefinitionProperty = serializedObject.FindProperty("movementDefinition");
SerializedProperty movementName = movementDefinitionProperty.FindPropertyRelative("Name");
SerializedProperty movementMovementConfigs = movementDefinitionProperty.FindPropertyRelative("MovementConfigs");
SerializedProperty movementMonoBehaviours = movementDefinitionProperty.FindPropertyRelative("MonoBehaviours");
// EditorGUILayout.PropertyField(movementDefinitionProperty, new GUIContent("New Movement Definition"), true);
EditorGUILayout.PropertyField(movementName);
EditorGUILayout.PropertyField(movementMovementConfigs, true);
EditorGUILayout.PropertyField(movementMonoBehaviours);
serializedObject.ApplyModifiedProperties();
GUILayout.Space(10);
bool createButtonPressed = GUILayout.Button("Create");
GUILayout.Label($"Will be saved to Assets/Resources/{MovementDefinitionFactory.ResourceDirectoryToDefinitions}{movementName.stringValue}.json");
GUILayout.Space(10);
2022-11-21 14:01:01 +03:00
GUILayout.Label("Tip: MovementConfigs->TypeName and MonoBehaviours use the type.FullName value");
2022-11-16 20:58:14 +03:00
GUILayout.Label("If under a namespace: \"Namespace.ClassName\"");
GUILayout.Label("If not under a namespace: \"ClassName\"");
2022-11-16 21:28:55 +03:00
GUILayout.Space(5);
2022-11-21 14:01:01 +03:00
GUILayout.Label($"Tip: Apply these to your GameObjects by using {nameof(MovementDefinitionFactory)} or the {nameof(MovementDefinitionApplier)} component");
GUILayout.Label($"{nameof(FactorySingleton<MovementDefinitionFactory>)}<{nameof(MovementDefinitionFactory)}>.Instance.ApplyDefinitionToGameObject(\"{movementName.stringValue}\");");
2022-11-16 20:58:14 +03:00
2022-11-16 21:28:55 +03:00
if (quickPresetButtonPressed) LoadPresetQuick();
if (longerPresetButtonPressed) LoadPresetLonger();
if (createButtonPressed) CreateDefinition(movementName.stringValue);
}
private void LoadPresetQuick()
{
movementDefinition.Name = "Example Movement Definition (Quick)";
movementDefinition.MovementConfigs = new MovementConfig[]
{
new MovementConfig(typeof(Samples.GroundMovement1D).FullName, 5f)
2022-11-16 21:28:55 +03:00
};
2022-11-16 20:58:14 +03:00
2022-11-16 21:28:55 +03:00
movementDefinition.MonoBehaviours = new string[]
{
2022-11-21 14:01:01 +03:00
typeof(MovementController).FullName
2022-11-16 21:28:55 +03:00
};
UpdateSerializedObject();
}
private void LoadPresetLonger()
{
movementDefinition.Name = "Example Movement Definition (Longer)";
movementDefinition.MovementConfigs = new MovementConfig[]
{
new MovementConfig(typeof(Samples.AirMovement1D).FullName, 5f),
new MovementConfig(typeof(Samples.GroundMovement1D).FullName, 5f)
2022-11-16 21:28:55 +03:00
};
movementDefinition.MonoBehaviours = new string[]
{
2022-11-21 14:01:01 +03:00
typeof(MovementController).FullName,
2023-03-20 22:39:21 +03:00
typeof(State.StateEnableMonoBehaviour).FullName
2022-11-16 21:28:55 +03:00
};
UpdateSerializedObject();
}
private void UpdateSerializedObject()
=> serializedObject = new SerializedObject(this);
private void CreateDefinition(string definitionName)
{
if (string.IsNullOrWhiteSpace(definitionName))
2022-11-16 20:58:14 +03:00
{
EditorUtility.DisplayDialog("Empty Name Field", "Name field must be not empty", "OK");
return;
}
string resourceDirectoryToDefinitions = MovementDefinitionFactory.ResourceDirectoryToDefinitions;
string[] folders = resourceDirectoryToDefinitions.Split('/');
string currentPath = "Assets/Resources";
CreateSubFolder("Assets", "Resources");
foreach (string folder in folders)
{
CreateSubFolder(currentPath, folder);
currentPath += $"/{folder}";
}
2022-11-16 21:28:55 +03:00
currentPath += $"/{definitionName}.json";
2022-11-16 20:58:14 +03:00
SaveMovementDefinition(movementDefinition);
}
private void CreateSubFolder(string parentFolder, string subFolder)
{
if (!AssetDatabase.IsValidFolder($"{parentFolder}/{subFolder}"))
AssetDatabase.CreateFolder(parentFolder, subFolder);
}
public void SaveMovementDefinition(MovementDefinition definition)
{
string jsonText = JsonUtility.ToJson(definition, true);
string path = $"Assets/Resources/{MovementDefinitionFactory.ResourceDirectoryToDefinitions}{definition.Name}.json";
2022-12-18 23:51:27 +03:00
if (System.IO.File.Exists($"{Application.dataPath}/Resources/{MovementDefinitionFactory.ResourceDirectoryToDefinitions}{definition.Name}.json"))
if (!EditorUtility.DisplayDialog("Conflict", $"\"{path}\" already exists, do you want to overwrite?", "Overwrite", "Cancel"))
return;
2022-11-16 20:58:14 +03:00
System.IO.File.WriteAllText(path, jsonText);
AssetDatabase.Refresh();
}
}
}