LibraStack / UnityMvvmToolkit

Brings data-binding to your Unity project
MIT License
461 stars 28 forks source link

GetBindableChilds ignores bindable children from template files #20

Closed Hellfim closed 1 year ago

Hellfim commented 1 year ago

Method GetBindableChilds from VisualElementExtensions incorrectly handles cases with bindable elements in templates. Suppose there is an empty scene with 1 GameObject called View and 2 attached components (UI Document and TestCaseView). If one would run such scene and click THE BUTTON he would see a "Normal button pressed" in the console, however if he clicks on a pink background there is no "Background button pressed" in the console. Listing of the files is provided below.

WindowContainer.uxml:

<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="window-root" style="flex-grow: 1;">
        <UnityMvvmToolkit.UITK.BindableUIElements.BindableButton command="BackgroundButtonCommand" name="window-closer" class="0; 0; 0;" style="position: absolute; top: 0; left: 0; bottom: 0; right: 0; background-color: rgb(236, 166, 166);" />
        <ui:VisualElement name="content-container" contentContainer="true" picking-mode="Ignore" style="flex-grow: 1; align-items: center; justify-content: space-around;" />
    </ui:VisualElement>
</ui:UXML>

TestCaseWindow.uxml:

<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:Template name="WindowContainer" src="project://database/Assets/TestCase/WindowContainer.uxml?fileID=9197481963319205126&amp;guid=54cc8b8584dd8494a83f70ca4058d5b4&amp;type=3#WindowContainer" />
    <ui:Instance template="WindowContainer" style="flex-grow: 1;">
        <UnityMvvmToolkit.UITK.BindableUIElements.BindableButton command="NormalButtonCommand" text="THE BUTTON" name="ok-button" style="width: 300px; height: 200px; font-size: 50px;" />
    </ui:Instance>
</ui:UXML>

TestCaseViewModel.cs:

    public class TestCaseViewModel : IBindingContext
    {
        public ICommand NormalButtonCommand { get; }
        public ICommand BackgroundButtonCommand { get; }

        public TestCaseViewModel()
        {
            NormalButtonCommand = new Command(() => Debug.Log("Normal button pressed"));
            BackgroundButtonCommand = new Command(() => Debug.Log("Background button pressed"));
        }
    }

TestCaseView.cs:

    public class TestCaseView : DocumentView<TestCaseViewModel>
    {
    }
Hellfim commented 1 year ago

Fix should be applied to logic which evaluates VisualElement's children in VisualElementExtensions:

- 27            var itemChildCount = visualElement.childCount;
+ 27            var itemChildCount = visualElement.hierarchy.childCount;

29            for (var i = 0; i < itemChildCount; i++)
30            {
- 31                GetBindableElements(visualElement[i], bindableChilds);
+ 31                GetBindableElements(visualElement.hierarchy[i], bindableChilds);
32            }

- 124            var itemChildCount = visualElement.childCount;
+ 124            var itemChildCount = visualElement.hierarchy.childCount;

126            for (var i = 0; i < itemChildCount; i++)
127            {
- 128                GetBindableElements(visualElement[i], elements);
+ 128                GetBindableElements(visualElement.hierarchy[i], elements);
129            }