public override GameObject CreateObject(Transform parent)
        {
            RectTransform container     = new GameObject("BSMLListContainer", typeof(RectTransform)).transform as RectTransform;
            LayoutElement layoutElement = container.gameObject.AddComponent <LayoutElement>();

            container.SetParent(parent, false);

            GameObject gameObject = new GameObject();

            gameObject.transform.SetParent(container, false);
            gameObject.name = "BSMLList";
            gameObject.SetActive(false);

            if (canvasTemplate == null)
            {
                canvasTemplate = Resources.FindObjectsOfTypeAll <Canvas>().First(x => x.name == "DropdownTableView");
            }

            gameObject.AddComponent <ScrollRect>();
            gameObject.AddComponent(canvasTemplate);
            gameObject.AddComponent <VRGraphicRaycaster>().SetField("_physicsRaycaster", BeatSaberUI.PhysicsRaycasterWithCache);
            gameObject.AddComponent <Touchable>();
            gameObject.AddComponent <EventSystemListener>();
            ScrollView scrollView = gameObject.AddComponent <ScrollView>();

            TableView           tableView = gameObject.AddComponent <BSMLTableView>();
            CustomListTableData tableData = container.gameObject.AddComponent <CustomListTableData>();

            tableData.tableView = tableView;

            tableView.SetField <TableView, TableView.CellsGroup[]>("_preallocatedCells", new TableView.CellsGroup[0]);
            tableView.SetField <TableView, bool>("_isInitialized", false);
            tableView.SetField("_scrollView", scrollView);

            RectTransform viewport = new GameObject("Viewport").AddComponent <RectTransform>();

            viewport.SetParent(gameObject.GetComponent <RectTransform>(), false);
            viewport.gameObject.AddComponent <RectMask2D>();
            gameObject.GetComponent <ScrollRect>().viewport = viewport;

            RectTransform content = new GameObject("Content").AddComponent <RectTransform>();

            content.SetParent(viewport, false);

            scrollView.SetField("_contentRectTransform", content);
            scrollView.SetField("_viewport", viewport);

            (viewport.transform as RectTransform).anchorMin        = new Vector2(0f, 0f);
            (viewport.transform as RectTransform).anchorMax        = new Vector2(1f, 1f);
            (viewport.transform as RectTransform).sizeDelta        = new Vector2(0f, 0f);
            (viewport.transform as RectTransform).anchoredPosition = new Vector3(0f, 0f);

            (tableView.transform as RectTransform).anchorMin        = new Vector2(0f, 0f);
            (tableView.transform as RectTransform).anchorMax        = new Vector2(1f, 1f);
            (tableView.transform as RectTransform).sizeDelta        = new Vector2(0f, 0f);
            (tableView.transform as RectTransform).anchoredPosition = new Vector3(0f, 0f);

            tableView.SetDataSource(tableData, false);
            return(container.gameObject);
        }
        private void FillList(CustomListTableData list, IEnumerable <Texture2D> textures)
        {
            var cells = new List <CustomListTableData.CustomCellInfo>();

            foreach (var tex in textures)
            {
                var cell = new CustomListTableData.CustomCellInfo(tex.name, null, Utilities.LoadSpriteFromTexture(tex));
                cells.Add(cell);
            }

            list.data = cells;
            list.tableView.ReloadData();
        }
        public override GameObject CreateObject(Transform parent)
        {
            RectTransform container     = new GameObject("BSMLListContainer", typeof(RectTransform)).transform as RectTransform;
            LayoutElement layoutElement = container.gameObject.AddComponent <LayoutElement>();

            container.SetParent(parent, false);

            GameObject gameObject = new GameObject();

            gameObject.name = "BSMLList";
            gameObject.SetActive(false);

            TableView           tableView = gameObject.AddComponent <BSMLTableView>();
            CustomListTableData tableData = container.gameObject.AddComponent <CustomListTableData>();

            tableData.tableView = tableView;
            tableView.transform.SetParent(container, false);

            //temp
            FieldInfo fieldInfo = typeof(TableView).GetField("_preallocatedCells", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

            fieldInfo.SetValue(tableView, new TableView.CellsGroup[0]);
            fieldInfo = typeof(TableView).GetField("_isInitialized", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            fieldInfo.SetValue(tableView, false);
            //

            /*
             * tableView.SetPrivateField("_preallocatedCells", new TableView.CellsGroup[0]);
             * tableView.SetPrivateField("_isInitialized", false);
             */

            RectTransform viewport = new GameObject("Viewport").AddComponent <RectTransform>();

            viewport.SetParent(gameObject.GetComponent <RectTransform>(), false);
            viewport.gameObject.AddComponent <RectMask2D>();
            gameObject.GetComponent <ScrollRect>().viewport = viewport;

            (viewport.transform as RectTransform).anchorMin        = new Vector2(0f, 0f);
            (viewport.transform as RectTransform).anchorMax        = new Vector2(1f, 1f);
            (viewport.transform as RectTransform).sizeDelta        = new Vector2(0f, 0f);
            (viewport.transform as RectTransform).anchoredPosition = new Vector3(0f, 0f);

            (tableView.transform as RectTransform).anchorMin        = new Vector2(0f, 0f);
            (tableView.transform as RectTransform).anchorMax        = new Vector2(1f, 1f);
            (tableView.transform as RectTransform).sizeDelta        = new Vector2(0f, 0f);
            (tableView.transform as RectTransform).anchoredPosition = new Vector3(0f, 0f);

            tableView.dataSource = tableData;
            return(container.gameObject);
        }
        public override GameObject CreateObject(Transform parent)
        {
            RectTransform container     = new GameObject("BSMLListContainer", typeof(RectTransform)).transform as RectTransform;
            LayoutElement layoutElement = container.gameObject.AddComponent <LayoutElement>();

            container.SetParent(parent, false);

            GameObject gameObject = new GameObject();

            gameObject.name = "BSMLList";
            gameObject.SetActive(false);

            gameObject.AddComponent <ScrollRect>();
            gameObject.AddComponent <Touchable>();

            TableView           tableView = gameObject.AddComponent <BSMLTableView>();
            CustomListTableData tableData = container.gameObject.AddComponent <CustomListTableData>();

            tableData.tableView = tableView;
            tableView.transform.SetParent(container, false);

            tableView.SetField <TableView, TableView.CellsGroup[]>("_preallocatedCells", new TableView.CellsGroup[0]);
            tableView.SetField <TableView, bool>("_isInitialized", false);

            RectTransform viewport = new GameObject("Viewport").AddComponent <RectTransform>();

            viewport.SetParent(gameObject.GetComponent <RectTransform>(), false);
            viewport.gameObject.AddComponent <RectMask2D>();
            gameObject.GetComponent <ScrollRect>().viewport = viewport;

            (viewport.transform as RectTransform).anchorMin        = new Vector2(0f, 0f);
            (viewport.transform as RectTransform).anchorMax        = new Vector2(1f, 1f);
            (viewport.transform as RectTransform).sizeDelta        = new Vector2(0f, 0f);
            (viewport.transform as RectTransform).anchoredPosition = new Vector3(0f, 0f);

            (tableView.transform as RectTransform).anchorMin        = new Vector2(0f, 0f);
            (tableView.transform as RectTransform).anchorMax        = new Vector2(1f, 1f);
            (tableView.transform as RectTransform).sizeDelta        = new Vector2(0f, 0f);
            (tableView.transform as RectTransform).anchoredPosition = new Vector3(0f, 0f);

            tableView.SetDataSource(tableData, false);
            return(container.gameObject);
        }
        public override void HandleType(ComponentTypeWithData componentType, BSMLParserParams parserParams)
        {
            CustomListTableData tableData  = componentType.component as CustomListTableData;
            ScrollView          scrollView = tableData.tableView.GetField <ScrollView, TableView>("_scrollView");

            if (componentType.data.TryGetValue("selectCell", out string selectCell))
            {
                tableData.tableView.didSelectCellWithIdxEvent += delegate(TableView table, int index)
                {
                    if (!parserParams.actions.TryGetValue(selectCell, out BSMLAction action))
                    {
                        throw new Exception("select-cell action '" + componentType.data["onClick"] + "' not found");
                    }

                    action.Invoke(table, index);
                };
            }

            bool verticalList = true;

            if (componentType.data.TryGetValue("listDirection", out string listDirection))
            {
                tableData.tableView.SetField <TableView, TableType>("_tableType", (TableType)Enum.Parse(typeof(TableType), listDirection));
                scrollViewDirectionField.SetValue(scrollView, Enum.Parse(scrollViewDirectionField.FieldType, listDirection));
                verticalList = listDirection.ToLower() != "horizontal";
            }

            if (componentType.data.TryGetValue("listStyle", out string listStyle))
            {
                tableData.Style = (ListStyle)Enum.Parse(typeof(ListStyle), listStyle);
            }

            if (componentType.data.TryGetValue("cellSize", out string cellSize))
            {
                tableData.cellSize = Parse.Float(cellSize);
            }

            if (componentType.data.TryGetValue("expandCell", out string expandCell))
            {
                tableData.expandCell = Parse.Bool(expandCell);
            }

            if (componentType.data.TryGetValue("alignCenter", out string alignCenter))
            {
                tableData.tableView.SetField <TableView, bool>("_alignToCenter", Parse.Bool(alignCenter));
            }

            if (componentType.data.TryGetValue("stickScrolling", out string stickScrolling))
            {
                if (Parse.Bool(stickScrolling))
                {
                    scrollView.SetField("_platformHelper", BeatSaberUI.PlatformHelper);
                }
            }

            // We can only show the scroll bar for vertical lists
            if (verticalList && componentType.data.TryGetValue("showScrollbar", out string showScrollbar))
            {
                if (Parse.Bool(showScrollbar))
                {
                    TextPageScrollView textScrollView = UnityEngine.Object.Instantiate(ScrollViewTag.ScrollViewTemplate, componentType.component.transform);

                    Button pageUpButton   = textScrollView.GetField <Button, ScrollView>("_pageUpButton");
                    Button pageDownButton = textScrollView.GetField <Button, ScrollView>("_pageDownButton");
                    VerticalScrollIndicator verticalScrollIndicator = textScrollView.GetField <VerticalScrollIndicator, ScrollView>("_verticalScrollIndicator");
                    RectTransform           scrollBar = verticalScrollIndicator.transform.parent as RectTransform;

                    scrollView.SetField("_pageUpButton", pageUpButton);
                    scrollView.SetField("_pageDownButton", pageDownButton);
                    scrollView.SetField("_verticalScrollIndicator", verticalScrollIndicator);
                    scrollBar.SetParent(componentType.component.transform);
                    GameObject.Destroy(textScrollView.gameObject);

                    // Need to adjust scroll bar positioning
                    scrollBar.anchorMin = new Vector2(1, 0);
                    scrollBar.anchorMax = Vector2.one;
                    scrollBar.offsetMin = Vector2.zero;
                    scrollBar.offsetMax = new Vector2(8, 0);
                }
            }

            if (componentType.data.TryGetValue("data", out string value))
            {
                if (!parserParams.values.TryGetValue(value, out BSMLValue contents))
                {
                    throw new Exception("value '" + value + "' not found");
                }


                var tableDataValue = contents.GetValue();
                if (!(tableDataValue is List <CustomCellInfo> tableDataList))
                {
                    throw new Exception($"Value '{value}' is not a List<CustomCellInfo>, which is required for custom-list");
                }

                tableData.data = tableDataList;
                tableData.tableView.ReloadData();
            }

            switch (tableData.tableView.tableType)
            {
            case TableType.Vertical:
                (componentType.component.gameObject.transform as RectTransform).sizeDelta = new Vector2(componentType.data.TryGetValue("listWidth", out string vListWidth) ? Parse.Float(vListWidth) : 60, tableData.cellSize * (componentType.data.TryGetValue("visibleCells", out string vVisibleCells) ? Parse.Float(vVisibleCells) : 7));
                break;

            case TableType.Horizontal:
                (componentType.component.gameObject.transform as RectTransform).sizeDelta = new Vector2(tableData.cellSize * (componentType.data.TryGetValue("visibleCells", out string hVisibleCells) ? Parse.Float(hVisibleCells) : 4), componentType.data.TryGetValue("listHeight", out string hListHeight) ? Parse.Float(hListHeight) : 40);
                break;
            }

            componentType.component.gameObject.GetComponent <LayoutElement>().preferredHeight = (componentType.component.gameObject.transform as RectTransform).sizeDelta.y;
            componentType.component.gameObject.GetComponent <LayoutElement>().preferredWidth  = (componentType.component.gameObject.transform as RectTransform).sizeDelta.x;

            tableData.tableView.gameObject.SetActive(true);
            tableData.tableView.LazyInit();

            if (componentType.data.TryGetValue("id", out string id))
            {
                parserParams.AddEvent(id + "#PageUp", scrollView.PageUpButtonPressed);
                parserParams.AddEvent(id + "#PageDown", scrollView.PageDownButtonPressed);
            }
        }
示例#6
0
        public override void HandleType(ComponentTypeWithData componentType, BSMLParserParams parserParams)
        {
            CustomListTableData tableData = componentType.component as CustomListTableData;

            if (componentType.data.TryGetValue("selectCell", out string selectCell))
            {
                tableData.tableView.didSelectCellWithIdxEvent += delegate(TableView table, int index)
                {
                    if (!parserParams.actions.TryGetValue(selectCell, out BSMLAction action))
                    {
                        throw new Exception("select-cell action '" + componentType.data["onClick"] + "' not found");
                    }

                    action.Invoke(table, index);
                };
            }

            if (componentType.data.TryGetValue("listDirection", out string listDirection))
            {
                tableData.tableView.SetField <TableView, TableType>("_tableType", (TableType)Enum.Parse(typeof(TableType), listDirection));
            }

            if (componentType.data.TryGetValue("listStyle", out string listStyle))
            {
                tableData.Style = (ListStyle)Enum.Parse(typeof(ListStyle), listStyle);
            }

            if (componentType.data.TryGetValue("cellSize", out string cellSize))
            {
                tableData.cellSize = Parse.Float(cellSize);
            }

            if (componentType.data.TryGetValue("expandCell", out string expandCell))
            {
                tableData.expandCell = Parse.Bool(expandCell);
            }

            if (componentType.data.TryGetValue("alignCenter", out string alignCenter))
            {
                tableData.tableView.SetField <TableView, bool>("_alignToCenter", Parse.Bool(alignCenter));
            }


            if (componentType.data.TryGetValue("data", out string value))
            {
                if (!parserParams.values.TryGetValue(value, out BSMLValue contents))
                {
                    throw new Exception("value '" + value + "' not found");
                }
                tableData.data = contents.GetValue() as List <CustomCellInfo>;
                tableData.tableView.ReloadData();
            }

            switch (tableData.tableView.tableType)
            {
            case TableType.Vertical:
                (componentType.component.gameObject.transform as RectTransform).sizeDelta = new Vector2(componentType.data.TryGetValue("listWidth", out string vListWidth) ? Parse.Float(vListWidth) : 60, tableData.cellSize * (componentType.data.TryGetValue("visibleCells", out string vVisibleCells) ? Parse.Float(vVisibleCells) : 7));
                break;

            case TableType.Horizontal:
                (componentType.component.gameObject.transform as RectTransform).sizeDelta = new Vector2(tableData.cellSize * (componentType.data.TryGetValue("visibleCells", out string hVisibleCells) ? Parse.Float(hVisibleCells) : 4), componentType.data.TryGetValue("listHeight", out string hListHeight) ? Parse.Float(hListHeight) : 40);
                break;
            }

            componentType.component.gameObject.GetComponent <LayoutElement>().preferredHeight = (componentType.component.gameObject.transform as RectTransform).sizeDelta.y;
            componentType.component.gameObject.GetComponent <LayoutElement>().preferredWidth  = (componentType.component.gameObject.transform as RectTransform).sizeDelta.x;

            tableData.tableView.gameObject.SetActive(true);
            tableData.tableView.LazyInit();

            if (componentType.data.TryGetValue("id", out string id))
            {
                TableViewScroller scroller = tableData.tableView.GetField <TableViewScroller, TableView>("scroller");
                parserParams.AddEvent(id + "#PageUp", scroller.PageScrollUp);
                parserParams.AddEvent(id + "#PageDown", scroller.PageScrollDown);
            }
        }
示例#7
0
        private static IEnumerator FixList(CustomListTableData list)
        {
            yield return(_delay);

            list.tableView.GetField <TableViewScroller, TableView>("scroller").RefreshScrollableSize();
        }
示例#8
0
 public static void ReloadListSize(this CustomListTableData list)
 {
     SharedCoroutineStarter.instance.StartCoroutine(FixList(list));
 }