Skip to content

Commit

Permalink
Add Rosalina settings and provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Eastrall committed Jul 8, 2023
1 parent f08b5a1 commit e2a3034
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 5 deletions.
10 changes: 9 additions & 1 deletion Editor/Scripts/MenuItems/RosalinaClearAllBindingsMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@

public class RosalinaClearAllBindingsMenuItem
{
[MenuItem("Assets/Rosalina/Clear All Bindings", priority = 2)]
private const string MenuItemPath = "Assets/Rosalina/Clear All Bindings";

[MenuItem(MenuItemPath, true)]
public static bool ClearAllBindingsValidation()
{
return RosalinaSettings.Current.IsEnabled;
}

[MenuItem(MenuItemPath, priority = 2)]
public static void ClearAllBindings()
{
try
Expand Down
2 changes: 1 addition & 1 deletion Editor/Scripts/MenuItems/RosalinaClearBindingsMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class RosalinaClearBindingsMenuItem
[MenuItem(MenuItemPath, true)]
public static bool ClearBindingsValidation()
{
return Selection.activeObject != null && Selection.activeObject.GetType() == typeof(VisualTreeAsset);
return RosalinaSettings.Current.IsEnabled && Selection.activeObject != null && Selection.activeObject.GetType() == typeof(VisualTreeAsset);
}

[MenuItem(MenuItemPath, priority = 20)]
Expand Down
10 changes: 9 additions & 1 deletion Editor/Scripts/MenuItems/RosalinaGenerateAllBindingsMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@

public class RosalinaGenerateAllBindingsMenuItem
{
[MenuItem("Assets/Rosalina/Generate All UI Bindings", priority = 1)]
private const string MenuItemPath = "Assets/Rosalina/Generate All UI Bindings";

[MenuItem(MenuItemPath, true)]
public static bool GenerateAllBindingsValidation()
{
return RosalinaSettings.Current.IsEnabled;
}

[MenuItem(MenuItemPath, priority = 1)]
public static void GenerateAllBindings()
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class RosalinaGenerateBindingsMenuItem
[MenuItem(MenuItemPath, true)]
private static bool GenerateUIBindingsValidation()
{
return Selection.activeObject != null && Selection.activeObject.GetType() == typeof(VisualTreeAsset);
return RosalinaSettings.Current.IsEnabled && Selection.activeObject != null && Selection.activeObject.GetType() == typeof(VisualTreeAsset);
}

[MenuItem(MenuItemPath, priority = 22)]
Expand Down
2 changes: 1 addition & 1 deletion Editor/Scripts/MenuItems/RosalinaGenerateScriptMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class RosalinaGenerateScriptMenuItem
[MenuItem(MenuItemPath, true)]
public static bool GenerateUIScriptValidation()
{
return Selection.activeObject != null && Selection.activeObject.GetType() == typeof(VisualTreeAsset);
return RosalinaSettings.Current.IsEnabled && Selection.activeObject != null && Selection.activeObject.GetType() == typeof(VisualTreeAsset);
}

[MenuItem(MenuItemPath, priority = 21)]
Expand Down
5 changes: 5 additions & 0 deletions Editor/Scripts/RosalinaAssetProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public class RosalinaAssetProcessor : AssetPostprocessor

private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPath)
{
if (!RosalinaSettings.Current.IsEnabled)
{
return;
}

string[] uiFilesChanged = importedAssets
.Where(x => x.StartsWith("Assets"))
.Where(x => Path.GetExtension(x) == UIDocumentExtension)
Expand Down
1 change: 1 addition & 0 deletions Editor/Scripts/RosalinaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ public static void GenerateScript(UIDocumentAsset document, string outputFile)
Debug.Log($"[Rosalina]: Done generating: {document.Name} (output: {outputFile})");
}
}

#endif
8 changes: 8 additions & 0 deletions Editor/Scripts/Settings.meta

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

39 changes: 39 additions & 0 deletions Editor/Scripts/Settings/RosalinaSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
/// <summary>
/// Provides a data structure for Rosalina settings.
/// </summary>
public class RosalinaSettings : ScriptableObject
{
/// <summary>
/// Gets the current Rosalina settings.
/// </summary>
public static RosalinaSettings Current => AssetDatabase.LoadAssetAtPath<RosalinaSettings>("Assets/Rosalina/RosalinaSettings.asset");

[SerializeField]
private bool _isEnabled;

[SerializeField]
private string _defaultNamespace;

/// <summary>
/// Gets or sets a boolean value that indicates if Rosalina is enabled.
/// </summary>
public bool IsEnabled
{
get => _isEnabled;
set => _isEnabled = value;
}

/// <summary>
/// Gets or sets the default namespace to use during code generation.
/// </summary>
public string DefaultNamespace
{
get => _defaultNamespace;
set => _defaultNamespace = value;
}
}

#endif
11 changes: 11 additions & 0 deletions Editor/Scripts/Settings/RosalinaSettings.cs.meta

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

68 changes: 68 additions & 0 deletions Editor/Scripts/Settings/RosalinaSettingsProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

public class RosalinaSettingsProvider : SettingsProvider
{
private RosalinaSettings _settings;

public RosalinaSettingsProvider(string path, SettingsScope scopes, IEnumerable<string> keywords = null)
: base(path, scopes, keywords)
{
}

public override void OnActivate(string searchContext, VisualElement rootElement)
{
_settings = RosalinaSettings.Current != null ? RosalinaSettings.Current : CreateRosalinaSettingsAsset();
}

public override void OnGUI(string searchContext)
{
using (CreateSettingsWindowGUIScope())
{
EditorGUI.BeginChangeCheck();

_settings.IsEnabled = EditorGUILayout.Toggle("Is Enabled", _settings.IsEnabled);
_settings.DefaultNamespace = EditorGUILayout.TextField("Default Namespace", _settings.DefaultNamespace);

if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(_settings);
}
}
}

private static RosalinaSettings CreateRosalinaSettingsAsset()
{
string resourcePath = "Assets/Rosalina";

if (!AssetDatabase.IsValidFolder(resourcePath))
{
AssetDatabase.CreateFolder("Assets", "Rosalina");
}

RosalinaSettings newSettings = ScriptableObject.CreateInstance<RosalinaSettings>();
AssetDatabase.CreateAsset(newSettings, $"{resourcePath}/RosalinaSettings.asset");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();

return RosalinaSettings.Current;
}

[SettingsProvider]
public static SettingsProvider CreateRosalinaSettingsProvider() => new RosalinaSettingsProvider("Project/Rosalina", SettingsScope.Project);

private static IDisposable CreateSettingsWindowGUIScope()
{
var unityEditorAssembly = Assembly.GetAssembly(typeof(EditorWindow));
var type = unityEditorAssembly.GetType("UnityEditor.SettingsWindow+GUIScope");

return Activator.CreateInstance(type) as IDisposable;
}
}

#endif
11 changes: 11 additions & 0 deletions Editor/Scripts/Settings/RosalinaSettingsProvider.cs.meta

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

0 comments on commit e2a3034

Please sign in to comment.