Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add manual generation support #27

Merged
merged 7 commits into from
Sep 23, 2023
Merged

Add manual generation support #27

merged 7 commits into from
Sep 23, 2023

Conversation

Eastrall
Copy link
Owner

@Eastrall Eastrall commented Sep 3, 2023

This PR introduces features based on user suggestions, code cleaning and improves usage experience.

Manual generation

Suggested in #26

When creating a UXML document, the Rosalina automatic binding generator is disable. To enable it, you must right-click on the UXML file > Rosalina > Properties...

image

A new editor window opens next to the Inspector window and you can enable the code generation for this file by checking the Enable checkbox.
Additionnaly, you can now select which generator you want to use for the UXML file.

Generic usage

Suggestd in #26

Bindings generators now use Q<T>() method instead of (T)Q().

public void InitializeDocument()
{
    InformationLabel = Root?.Q<Label>("InformationLabel");
    ConfirmButton = Root?.Q<Button>("confirm-button");
    CancelButton = Root?.Q<Button>("CancelButton");
    TitleLabel = Root?.Q<Label>("TitleLabel");
    BodyContainer = Root?.Q<VisualElement>("BodyContainer");
}

Custom component

Suggested in #25

Added new UXML generator for custom components. Custom components are built by passing a VisualElement instance representing the root element of the component.

// <auto-generated>
using UnityEngine.UIElements;

public partial class CustomComponent
{
    public Label Title { get; private set; }

    public Button NewButton { get; private set; }

    public VisualElement Root { get; }

    public CustomComponent(VisualElement root)
    {
        Root = root;
        Title = Root?.Q<Label>("Title");
        NewButton = Root?.Q<Button>("NewButton");
    }
}

Then if you add your custom component to a document, you will get the following generated code:

// <auto-generated>
using UnityEngine;
using UnityEngine.UIElements;

public partial class SampleDocument
{
    [SerializeField]
    private UIDocument _document;

    public Label InformationLabel { get; private set; }

    public CustomComponent CustomLabel { get; private set; }

    public VisualElement Root => _document?.rootVisualElement;

    public void InitializeDocument()
    {
        InformationLabel = Root?.Q<Label>("InformationLabel");
        // Create a new CustomComponent instance using the `Q<T>()` method.
        CustomLabel = new CustomComponent(Root?.Q<VisualElement>("CustomLabel"));
    }
}

Settings improvement

Setting file was originaly placed in /Assets/Rosalina folder. It has been moved to the /ProjectSettings folder and doesn't belong to the assets anymore.
Also, you can view which files is acutally in the Rosalina is being processed by the AssetProcessor for bindings generation:

image

@Eastrall Eastrall added enhancement New feature or request area: code-generator The issue is related to the code generator engine labels Sep 3, 2023
@Eastrall Eastrall merged commit c71f096 into main Sep 23, 2023
@Eastrall Eastrall deleted the feature/manual-generation branch September 23, 2023 10:42
crowdoka pushed a commit to crowdoka/Rosalina that referenced this pull request Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: code-generator The issue is related to the code generator engine enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant