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

EditorWindow support #20

Closed
kant2002 opened this issue Sep 5, 2022 · 9 comments
Closed

EditorWindow support #20

kant2002 opened this issue Sep 5, 2022 · 9 comments
Labels
area: code-generator The issue is related to the code generator engine area: editor-extension The issue is related to Rosalina's editor extension discussion

Comments

@kant2002
Copy link

kant2002 commented Sep 5, 2022

I think about using this package inside EditorWindow but cannot find a way how I can map UIDocument and EditorWindow.

@Eastrall
Copy link
Owner

Eastrall commented Sep 6, 2022

Hi, what are you trying to achieve using an EditorWindow? Do you want for example a button that triggers the Rosalina code generator?
Please provide more information so we can help you out with this.

@Eastrall Eastrall added the question Further information is requested label Sep 6, 2022
@kant2002
Copy link
Author

kant2002 commented Sep 6, 2022

I would like to define EditorWindow using UXML. But current generator only assume that whole tree is from UIDocument.

@kant2002
Copy link
Author

kant2002 commented Sep 6, 2022

Basically I think would be beneficial to have something like this
SampleDocument.g.cs

// <autogenerated />
using UnityEngine;
using UnityEngine.UIElements;

public partial class SampleDocument : EditorWindow /* Derived class is not needed in codegen, that's for illustration */
{
    public Label TitleLabel { get; private set; }

    public Button Button { get; private set; }

    public VisualElement Root
    {
        get
        {
            return rootVisualElement;
        }
    }

    public void InitializeDocument()
    {
        TitleLabel = (Label)Root?.Q("TitleLabel");
        Button = (Button)Root?.Q("Button");
    }
}

@Eastrall Eastrall added area: code-generator The issue is related to the code generator engine area: editor-extension The issue is related to Rosalina's editor extension discussion and removed question Further information is requested labels Sep 6, 2022
@Eastrall
Copy link
Owner

Eastrall commented Sep 6, 2022

That is a very interesting usecase you got there. At the moment Rosalina doesn't support code generation for EditorWindow, but that's definitly something I'll add up to the project's roadmap!

An alternative, would be to load the VisualTreeAsset on the ShowWindow() method, like shown here: https://docs.unity3d.com/Manual/UIE-LoadingUXMLcsharp.html

@kant2002
Copy link
Author

kant2002 commented Sep 6, 2022

but that's definitly something I'll add up to the project's roadmap!

If you give me hints, I maybe can try to implement this. but I'm not Unity developer, just regular developer, even if experience one.

An alternative, would be to load the VisualTreeAsset on the

That's what I have right now. It's a bit of boilerplate and that's the reason want to get rid of rootVisualElement.Q<XXXX> calls.

@Eastrall
Copy link
Owner

I must think of a way to handle properly both cases (UI Document using MonoBehavior and UI Document with EditorWindow) so it doesn't break anything within the generator. Also, I believe we should work on issue #15 first and verify if it could work for EditorWindows too.
No worries, we'll find a solution to support EditorWindow ASAP, because I believe that Rosalina is a great alternative to remove the cumbersome code using the rootVisualElement.Q<T>() method to get the elements.

@Eastrall Eastrall added this to To Do in Rosalina roadmap Sep 12, 2022
@Eastrall
Copy link
Owner

I have been doing some research on how we could implement this feature and I might have found something.
In the Unity UI Builder, there is an option on the Document Settings section named Editor Extension Authoring which can be True or False.

image

I believe we can use this option to tell Rosalina how to behave when generating the binding script since this option generates an XML attribute at the document root node:

<ui:UXML ... editor-extension-mode="True">
</ui:UXML>

We will need to create a new bindings generator for the EditorWindow support and have to study how the EditorWindow initializes its objects and VTA.

@Eastrall Eastrall moved this from To Do to In Progress in Rosalina roadmap Nov 27, 2022
@Eastrall
Copy link
Owner

I have been doing some research during the past couple of days about how we could implement the EditorWindow support within Rosalina generator. In commit ... I have prepared the code to genearte the correct bindings and script based on the editor-extension-mode UXML attribute. If it's set to false the Default Rosalina generator will be used, otherwhise, the RosalinaEditorWindow* generator will be used.

As an idea, there is a example of how the generated and user's code will look like:

Auto-Generated code

public partial class TestEditorWindow : EditorWindow
{
    // Element properties
    public Button SampleButton { get; private set; }

    public void CreateGUI()
    {
        // VTA loading from assets
        VisualTreeAsset uiAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/...");
        VisualElement ui = uiAsset.CloneTree();

        // Add to EditorWindow root visual element
        rootVisualElement.Add(ui);

        // Apply bindings
        SampleButton = (Button)rootVisualElement?.Q("SampleButton");

        // Call user specific code for event setup
        OnCreateGUI();
    }

    private partial void OnCreateGUI();
}

The initialization process of an EditorWindow will be done inside the CreateGUI() method. Then, in the user's script, the user will have to implement the partial void OnCreateGUI() method in order to define the element events.

User script code

public partial class TestEditorWindow : EditorWindow
{
    [MenuItem("Window/My Window")]
    public static void ShowTestEditorWindow()
    {
        EditorWindow.CreateWindow<TestEditorWindow>();
    }

    private partial void OnCreateGUI()
    {
        SampleButton.clicked += SampleButtonClicked;
    }

    private void SampleButtonClicked() => Debug.Log("Button clicked");
}

Note, the [MenuItem] part is up to the user to control how the EditorWindow will be shown.

@Eastrall
Copy link
Owner

Shipped in version 2.0.0.

Closing issue.

@Eastrall Eastrall moved this from In Progress to Done in Rosalina roadmap Nov 28, 2022
@Eastrall Eastrall removed the planned label Nov 28, 2022
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 area: editor-extension The issue is related to Rosalina's editor extension discussion
Projects
Development

No branches or pull requests

2 participants