// /*=============================================================================== // Copyright (C) 2020 PhantomsXR Ltd. All Rights Reserved. // // This file is part of the AR-MOD SDK. // // The AR-MOD SDK cannot be copied, distributed, or made available to // third-parties for commercial purposes without written permission of PhantomsXR Ltd. // // Contact info@phantomsxr.com for licensing requests. // ===============================================================================*/ using System.Collections.Generic; using System.IO; using Phantom.XRMOD.ActionNotification.Runtime; using UnityEditor; using UnityEditor.UIElements; using UnityEngine; using UnityEngine.UIElements; namespace Phantom.XRMOD.XRMODPackageTools.Editor { public class PackageToolsEditor : EditorWindow { private static PackageToolsEditor PACKAGE_TOOLS_EDITOR; private static AllProjectsCache INSTANCE; public static AllProjectsCache ALL_PROJECT_CACHE { get { if (INSTANCE) return INSTANCE; LoadAllProjectCache(); return INSTANCE; } set => INSTANCE = value; } private VisualElement root; readonly List toolbarToggles = new List(); readonly StyleEnum displayFlex = new StyleEnum(DisplayStyle.Flex); readonly StyleEnum displayNone = new StyleEnum(DisplayStyle.None); // [InitializeOnLoadMethod] // private static void PrepareData() // { // LoadAllProjectCache(); // } [MenuItem("Tools/XR-MOD/Package Tools Editor")] public static void ShowPackageToolsEditor() { PACKAGE_TOOLS_EDITOR = GetWindow(); PACKAGE_TOOLS_EDITOR.titleContent = new GUIContent(ConstKey.CONST_PACKAGE_TOOLS_WINDOW_TITLE); var tmp_WindowPosition = PACKAGE_TOOLS_EDITOR.position; PACKAGE_TOOLS_EDITOR.position = new Rect(tmp_WindowPosition.position, new Vector2(600, 400)); // PACKAGE_TOOLS_EDITOR.Close(); } private void OnDisable() { //Remove all of commands ActionNotificationCenter.DefaultCenter.RemoveObserver(nameof(EditingProjectCommand)); ActionNotificationCenter.DefaultCenter.RemoveObserver(nameof(OpenProjectCommand)); ActionNotificationCenter.DefaultCenter.RemoveObserver(nameof(CreateProjectCommand)); ActionNotificationCenter.DefaultCenter.RemoveObserver(nameof(ExitEditingProjectCommand)); if (INSTANCE == null) return; //Save all changed. EditorUtility.SetDirty(INSTANCE); //`SetDirty` only saves the current data. //It will not automatically save other scriptable object contained in it(Changed will be lost) //So we need to save each object manually foreach (ProjectCacheData tmp_ProjectCache in INSTANCE.ProjectCacheDataList) { if (tmp_ProjectCache.DetailCacheData.Configures == null) { var tmp_ProjectConfigures = ScriptableObject.CreateInstance(); tmp_ProjectConfigures.name = nameof(Configures); AssetDatabase.AddObjectToAsset(tmp_ProjectConfigures, tmp_ProjectCache); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(tmp_ProjectConfigures)); } EditorUtility.SetDirty(tmp_ProjectCache.DetailCacheData.BuildSettingData); EditorUtility.SetDirty(tmp_ProjectCache.DetailCacheData.Configures); EditorUtility.SetDirty(tmp_ProjectCache); } } public void CreateGUI() { //Register editing event for showing project details and hided not editing view ActionNotificationCenter.DefaultCenter.AddObserver(_data => { GenerateDetailViews(); var tmp_NoEditingView = root.Q(ConstKey.CONST_NO_EDITING_VIEW); tmp_NoEditingView.style.display = displayNone; var tmp_EditingView = root.Q(ConstKey.CONST_EDITING_VIEW); tmp_EditingView.style.display = displayFlex; toolbarToggles[INSTANCE.SeletedId].value = true; }, nameof(EditingProjectCommand)); //Register editing event for hiding project details and showed not editing view ActionNotificationCenter.DefaultCenter.AddObserver(_data => { var tmp_NoEditingView = root.Q(ConstKey.CONST_NO_EDITING_VIEW); tmp_NoEditingView.style.display = displayFlex; var tmp_EditingView = root.Q(ConstKey.CONST_EDITING_VIEW); tmp_EditingView.style.display = displayNone; INSTANCE.SeletedId = 0; }, nameof(ExitEditingProjectCommand)); //Avoid Null data after scripts re-compiler // if (ALL_PROJECT_CACHE == null) LoadAllProjectCache(); // Each editor window contains a root VisualElement object root = rootVisualElement; // Import UXML var tmp_UXML = Path.Combine(ConstKey.CONST_UASSETS_PATH, ConstKey.CONST_PACKAGE_TOOLS_EDITOR_UXML); var visualTree = AssetDatabase.LoadAssetAtPath(tmp_UXML); visualTree.CloneTree(root); // A stylesheet can be added to a VisualElement. // The style will be applied to the VisualElement and all of its children. var tmp_USS = Path.Combine(ConstKey.CONST_UASSETS_PATH, ConstKey.CONST_PACKAGE_TOOLS_EDITOR_USS); var styleSheet = AssetDatabase.LoadAssetAtPath(tmp_USS); root.styleSheets.Add(styleSheet); CreateToolbarTabView(); //Avoid display error when script re-compiler or re-open this editor there have project editing CheckProjectState(); } private static void LoadAllProjectCache() { var tmp_CachePath = Utility.GetRootDataPath(ConstKey.CONST_ALL_PROJECT_CACHE_FILE); INSTANCE = AssetDatabase.LoadAssetAtPath(tmp_CachePath); if (INSTANCE) { INSTANCE.Initialization(); } else { INSTANCE = CreateInstance(); INSTANCE.Initialization(); var tmp_SaveCachePath = Utility.GetRootDataPath(ConstKey.CONST_ALL_PROJECT_CACHE_FILE); AssetDatabase.CreateAsset(INSTANCE, tmp_SaveCachePath); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } foreach (ProjectCacheData tmp_ProjectCacheData in INSTANCE.ProjectCacheDataList) { var tmp_ProjectPath = tmp_ProjectCacheData.DetailCacheData.GetProjectPath(); Utility.CreateRuntimeAssetReferenceDatabase(tmp_ProjectPath); } } private static void CheckProjectState() { if (EditorApplication.isCompiling || INSTANCE == null) return; var tmp_EditingProjectData = INSTANCE.GetEditingProjectData(); if (tmp_EditingProjectData) { ActionNotificationCenter.DefaultCenter.PostNotification(nameof(EditingProjectCommand), null); } } private void GenerateDetailViews() { if (root.Q(nameof(PropertiesView)) != null) return; var tmp_View = root.Q(ConstKey.CONST_VIEW_TAG); var tmp_PropertiesView = new PropertiesView() {name = nameof(PropertiesView)}; var tmp_ContentView = new ContentView() {name = nameof(ContentView)}; var tmp_BuildView = new BuildView() {name = nameof(BuildView)}; tmp_View.Add(tmp_PropertiesView); tmp_View.Add(tmp_ContentView); tmp_View.Add(tmp_BuildView); tmp_PropertiesView.style.display = displayFlex; tmp_ContentView.style.display = displayNone; tmp_BuildView.style.display = displayNone; } /// /// Create a toolbar toggle group /// private void CreateToolbarTabView() { toolbarToggles.Add(root.Q(ConstKey.CONST_PROPERTIES_TOGGLE)); toolbarToggles.Add(root.Q(ConstKey.CONST_CONTENTS_TOGGLE)); toolbarToggles.Add(root.Q(ConstKey.CONST_BUILD_TOGGLE)); foreach (ToolbarToggle tmp_Toggle in toolbarToggles) { //Apply style to label tmp_Toggle.Q