Skip to content

Commit

Permalink
Merge pull request Eastrall#27 from Eastrall/feature/manual-generation
Browse files Browse the repository at this point in the history
Add manual generation support
  • Loading branch information
Eastrall committed Sep 23, 2023
2 parents 42f9bec + ac01cb2 commit 94f1957
Show file tree
Hide file tree
Showing 72 changed files with 1,240 additions and 889 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Rosalina CHANGELOG

## 4.0.0 - 2023-09-23

### 🚀 Enhancement

* Bindings/Script generators code cleaning
* Added manual generation (#26) (PR #27)
* Added UXML custom properties window for configuring how Rosalina should behave. (#26) (PR #27)
* Change bindings initialization method to generic methods. (#26) (PR #27)
* Add custom component support (#25) (PR #27)
* Rosalina settings improvement

## 3.0.1 - 2023-07-16

### 🐛Bug fixes
Expand Down

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/Abstractions/IRosalinaCodeGeneartor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#if UNITY_EDITOR

internal interface IRosalinaCodeGeneartor
{
RosalinaGenerationResult Generate(UIDocumentAsset document);
}

#endif

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

186 changes: 186 additions & 0 deletions Editor/Scripts/Editor/RosalinaPropertiesEditorWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#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; }

public Button GenerateBindingsButton { get; private set; }

public Button GenerateScriptButton { get; private set; }

public Button ClearBindingsButton { get; private set; }

private void OnEnable()
{
}

private void OnDestroy()
{
EnableFile.UnregisterValueChangedCallback(OnEnableFileChanged);
GeneratorTypeSelector.UnregisterValueChangedCallback(OnGeneratorTypeSelectionChanged);
GenerateBindingsButton.clicked -= OnGenerateBindings;
GenerateScriptButton.clicked -= OnGenerateScripts;
ClearBindingsButton.clicked -= OnClearBindings;
}

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");
GenerateBindingsButton = rootVisualElement.Q<Button>("GenerateBindingsButton");
GenerateScriptButton = rootVisualElement.Q<Button>("GenerateScriptButton");
ClearBindingsButton = rootVisualElement.Q<Button>("ClearBindingsButton");

OnSelectionChange();
EnableFile.RegisterValueChangedCallback(OnEnableFileChanged);
GeneratorTypeSelector.RegisterValueChangedCallback(OnGeneratorTypeSelectionChanged);
GenerateBindingsButton.clicked += OnGenerateBindings;
GenerateScriptButton.clicked += OnGenerateScripts;
ClearBindingsButton.clicked += OnClearBindings;
}

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 OnGenerateBindings()
{
string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
var document = new UIDocumentAsset(assetPath);

document.GenerateBindings();
AssetDatabase.Refresh();
}

private void OnGenerateScripts()
{
string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
var document = new UIDocumentAsset(assetPath);

bool bindingsGenerated = RosalinaScriptGeneratorUtilities.TryGenerateBindings(document);
bool scriptGenerated = RosalinaScriptGeneratorUtilities.TryGenerateScript(document);

if (bindingsGenerated || scriptGenerated)
{
AssetDatabase.Refresh();
}
}

private void OnClearBindings()
{
string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
var document = new UIDocumentAsset(assetPath);

document.ClearBindings();
AssetDatabase.Refresh();
}

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...", priority = 1200)]
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.

17 changes: 17 additions & 0 deletions Editor/Scripts/Editor/RosalinaPropertiesEditorWindow.uxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<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: 0; 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.eastylabs.rosalina" name="GeneratorTypeSelector" />
</ui:VisualElement>
</ui:VisualElement>
<ui:Label tabindex="-1" text="Tools" display-tooltip-when-elided="true" name="ToolsTitleLabel" style="-unity-font-style: bold; font-size: 12px; margin-top: 8px;" />
<ui:VisualElement name="ToolsContainer" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); margin-top: 8px;">
<ui:Button text="Generate bindings" display-tooltip-when-elided="true" name="GenerateBindingsButton" />
<ui:Button text="Generate script" display-tooltip-when-elided="true" name="GenerateScriptButton" />
<ui:Button text="Clear bindings" display-tooltip-when-elided="true" name="ClearBindingsButton" />
</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.

Loading

0 comments on commit 94f1957

Please sign in to comment.