Skip to content

Commit

Permalink
Add UXML file properties window editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Eastrall committed Sep 8, 2023
1 parent e07e486 commit d94def2
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 43 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

140 changes: 140 additions & 0 deletions Editor/Scripts/Editor/RosalinaPropertiesEditorWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#if UNITY_EDITOR

using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

public class RosalinaPropertiesEditorWindow : EditorWindow
{
[SerializeField]
private VisualTreeAsset _visualTreeAsset;
private RosalinaFileSetting _fileSettings = null;

public VisualElement Container { get; private set; }

public VisualElement BasicSettingsContainer { get; private set; }

public Toggle EnableFile { get; private set; }

public EnumField GeneratorTypeSelector { get; private set; }

private void OnEnable()
{
}

private void OnDestroy()
{
EnableFile.UnregisterValueChangedCallback(OnEnableFileChanged);
GeneratorTypeSelector.UnregisterValueChangedCallback(OnGeneratorTypeSelectionChanged);
}

private void OnSelectionChange()
{
bool isActive = Selection.activeObject != null && Selection.activeObject.GetType() == typeof(VisualTreeAsset);

Container.SetEnabled(isActive);

if (isActive)
{
string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
_fileSettings = RosalinaSettings.instance.GetFileSetting(assetPath);
}
else
{
_fileSettings = null;
}

RefreshFileSettings();
}

private void CreateGUI()
{
rootVisualElement.Add(_visualTreeAsset.Instantiate());

Container = rootVisualElement.Q<VisualElement>("Container");
BasicSettingsContainer = rootVisualElement.Q<VisualElement>("BasicSettingsContainer");
EnableFile = rootVisualElement.Q<Toggle>("EnableFile");
GeneratorTypeSelector = rootVisualElement.Q<EnumField>("GeneratorTypeSelector");

OnSelectionChange();
EnableFile.RegisterValueChangedCallback(OnEnableFileChanged);
GeneratorTypeSelector.RegisterValueChangedCallback(OnGeneratorTypeSelectionChanged);
}

private void RefreshFileSettings()
{
EnableFile.SetValueWithoutNotify(_fileSettings != null);
BasicSettingsContainer.SetEnabled(_fileSettings != null);

if (_fileSettings != null)
{
GeneratorTypeSelector.value = _fileSettings.Type;
}
else
{
GeneratorTypeSelector.value = null;
}
}

private void OnEnableFileChanged(ChangeEvent<bool> @event)
{
if (@event.newValue)
{
_fileSettings = new RosalinaFileSetting
{
Path = AssetDatabase.GetAssetPath(Selection.activeObject),
Type = RosalinaGenerationType.Document
};
RosalinaSettings.instance.Files.Add(_fileSettings);
}
else
{
RosalinaSettings.instance.Files.Remove(_fileSettings);
_fileSettings = null;
}

RefreshFileSettings();
OnSettingsChanged();
}

private void OnGeneratorTypeSelectionChanged(ChangeEvent<Enum> @event)
{
if (@event.newValue != null && @event.newValue != @event.previousValue)
{
_fileSettings.Type = (RosalinaGenerationType)@event.newValue;

RefreshFileSettings();
OnSettingsChanged();
}
}

private void OnSettingsChanged()
{
RosalinaSettings.instance.Save();

}

[MenuItem("Assets/Rosalina/Properties...", validate = true)]
public static bool ShowWindowValidation()
{
return RosalinaSettings.instance.IsEnabled && Selection.activeObject != null && Selection.activeObject.GetType() == typeof(VisualTreeAsset);
}

[MenuItem("Assets/Rosalina/Properties...")]
public static void ShowWindow()
{
if (HasOpenInstances<RosalinaPropertiesEditorWindow>())
{
FocusWindowIfItsOpen<RosalinaPropertiesEditorWindow>();
return;
}

Type inspectorWindowType = typeof(Editor).Assembly.GetType("UnityEditor.InspectorWindow");
EditorWindow window = CreateWindow<RosalinaPropertiesEditorWindow>(inspectorWindowType);

window.titleContent = new GUIContent("Rosalina", EditorGUIUtility.FindTexture("SettingsIcon"));
}
}

#endif
14 changes: 14 additions & 0 deletions Editor/Scripts/Editor/RosalinaPropertiesEditorWindow.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Editor/Scripts/Editor/RosalinaPropertiesEditorWindow.uxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<ui:VisualElement name="Container" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); padding-left: 12px; padding-right: 12px; padding-top: 12px; padding-bottom: 12px;">
<ui:Label tabindex="-1" text="Basic settings" display-tooltip-when-elided="true" name="BasicSettingsTitleLabel" style="-unity-font-style: bold; font-size: 12px;" />
<ui:VisualElement name="FileSettingsContainer" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); margin-top: 8px;">
<ui:Toggle label="Enable" name="EnableFile" tooltip="Includes the current UXML file into the Rosalina code generation pipeline." />
<ui:VisualElement name="BasicSettingsContainer" style="flex-grow: 0; background-color: rgba(0, 0, 0, 0); margin-top: 0;">
<ui:EnumField label="Generator type" type="RosalinaGenerationType, com.eastlylabs.rosalina" name="GeneratorTypeSelector" />
</ui:VisualElement>
</ui:VisualElement>
</ui:VisualElement>
</ui:UXML>
10 changes: 10 additions & 0 deletions Editor/Scripts/Editor/RosalinaPropertiesEditorWindow.uxml.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Editor/Scripts/Inspector.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions Editor/Scripts/Inspector/RosalinaUxmlDocumentInspectorEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

[CustomEditor(typeof(VisualTreeAsset), true)]
[CanEditMultipleObjects]
public class RosalinaUxmlDocumentInspectorEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();

VisualTreeAsset visualTreeAsset = (VisualTreeAsset)target;

string assetPath = AssetDatabase.GetAssetPath(visualTreeAsset.GetInstanceID());
var file = RosalinaSettings.instance.GetFileSetting(assetPath);

//EditorGUI.BeginDisabledGroup(false);
EditorGUILayout.BeginVertical();
EditorGUI.BeginChangeCheck();

GUILayout.Space(20);
EditorGUILayout.LabelField("Rosalina", EditorStyles.boldLabel);

EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Enable");
bool test = EditorGUILayout.Toggle(file != null);
EditorGUILayout.EndHorizontal();

if (file != null)
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Code generation type");
file.Type = (RosalinaGenerationType)EditorGUILayout.EnumPopup(file.Type);
EditorGUILayout.EndHorizontal();
}

EditorGUILayout.LinkButton("test");

if (EditorGUI.EndChangeCheck())
{

}
EditorGUILayout.EndVertical();
//EditorGUI.EndDisabledGroup();
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

2 changes: 1 addition & 1 deletion Editor/Scripts/Settings/RosalinaSettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ private void OnDrawElement(Rect rect, int index, bool isActive, bool isFocused)
if (_fileList.list[index] is RosalinaFileSetting element)
{
rect.y += 2;
EditorGUI.BeginDisabledGroup(true);
element.Type = (RosalinaGenerationType)EditorGUI.EnumPopup(
new Rect(rect.x, rect.y, 120, EditorGUIUtility.singleLineHeight),
element.Type);
EditorGUI.BeginDisabledGroup(true);
EditorGUI.TextField(
new Rect(rect.x + 130, rect.y, rect.width - 200, EditorGUIUtility.singleLineHeight),
element.Path);
Expand Down

0 comments on commit d94def2

Please sign in to comment.