// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using Doozy.Editor.EditorUI; using Doozy.Editor.EditorUI.Components; using Doozy.Editor.EditorUI.ScriptableObjects.Colors; using Doozy.Editor.EditorUI.Windows.Internal; using Doozy.Editor.Nody.Nodes.Internal; using Doozy.Editor.UIElements; using Doozy.Runtime.UIElements.Extensions; using UnityEngine; using UnityEngine.UIElements; using EditorStyles = Doozy.Editor.EditorUI.EditorStyles; // ReSharper disable MemberCanBePrivate.Global namespace Doozy.Editor.Nody { public class NodyInspectorWindow : FluidWindow { private const string k_WindowTitle = "Nody Inspector"; public const string WINDOW_MENU_PATH = "Tools/Doozy/Nody/"; // [MenuItem(k_WindowMenuPath + "Inspector", priority = -850)] public static void Open() => InternalOpenWindow(k_WindowTitle); public static Color accentColor => EditorColors.Nody.Color; public static EditorSelectableColorInfo selectableAccentColor => EditorSelectableColors.Nody.Color; private TemplateContainer templateContainer { get; set; } private VisualElement layoutContainer { get; set; } private ScrollView inspectorScrollView { get; set; } private VisualElement placeholderContainer { get; set; } private FluidPlaceholder emptySelectionPlaceholder { get; set; } public UnityEditor.Editor editor { get; private set; } public FlowNodeView nodeView { get; private set; } protected override void OnEnable() { base.OnEnable(); titleContent = new GUIContent(k_WindowTitle); minSize = new Vector2(200, 200); } protected override void CreateGUI() { root .RecycleAndClear() .Add(templateContainer = EditorLayouts.Nody.NodyInspectorWindow.CloneTree()); templateContainer .SetStyleFlexGrow(1) .AddStyle(EditorStyles.Nody.NodyInspectorWindow); layoutContainer = templateContainer.Q(nameof(layoutContainer)); inspectorScrollView = layoutContainer.Q(nameof(inspectorScrollView)); placeholderContainer = layoutContainer.Q(nameof(placeholderContainer)); inspectorScrollView.viewDataKey = $"{nameof(NodyInspectorWindow)}_{nameof(inspectorScrollView)}"; emptySelectionPlaceholder = FluidPlaceholder.Get() .SetStyleAlignSelf(Align.Center) .SetIcon(EditorSpriteSheets.Nody.Icons.Nody); placeholderContainer .AddChild(emptySelectionPlaceholder); } /// Clear the inspector and show the placeholder /// True to show the placeholder public void ClearInspector(bool showPlaceholder = true) { if (inspectorScrollView == null) return; inspectorScrollView.contentContainer.RecycleAndClear(); if (editor != null) DestroyImmediate(editor); placeholderContainer.SetStyleDisplay(showPlaceholder ? DisplayStyle.Flex : DisplayStyle.None); if (showPlaceholder) emptySelectionPlaceholder.Play(); } public void UpdateSelection(FlowNodeView view) { if (view != null && nodeView != null && view == nodeView) return; nodeView = view; bool showPlaceholder = view == null; //node view is null -> nothing selected -> show placeholder ClearInspector(showPlaceholder); if (showPlaceholder) return; try { editor = UnityEditor.Editor.CreateEditor(view.flowNode); //TARGET OBJECT CUSTOM EDITOR ((FlowNodeEditor)editor).nodeView = view; //inject node view reference VisualElement visualElement = editor.CreateInspectorGUI(); // <<< EDITOR GETS INJECTED HERE inspectorScrollView.contentContainer.Add(visualElement); //add editor to the inspector } catch { // ignored } } public void Refresh() { UpdateSelection(nodeView); } } }