/// <summary> /// Open coroutine. /// </summary> /// <param name="item">Item.</param> /// <returns>The coroutine.</returns> protected virtual IEnumerator OpenCoroutine(AccordionItem item) { item.ContentObject.SetActive(true); item.Open = true; if (ResizeMethod == ResizeMethods.Size) { yield return(StartCoroutine(Animations.Open(item.ContentObjectRect, AnimationDuration, IsHorizontal(), UnscaledTime, () => { if (IsHorizontal()) { item.ContentLayoutElement.preferredWidth = -1; } else { item.ContentLayoutElement.preferredHeight = -1; } LayoutUtilites.UpdateLayout(item.ContentObjectRect.parent.GetComponent <LayoutGroup>()); item.ContentObjectWidth = item.ContentObjectRect.rect.width; item.ContentObjectHeight = item.ContentObjectRect.rect.height; }))); } else if (ResizeMethod == ResizeMethods.Flexible) { yield return(StartCoroutine(Animations.OpenFlexible(item.ContentObjectRect, AnimationDuration, IsHorizontal()))); } UpdateLayout(); OnToggleItem.Invoke(item); }
/// <summary> /// Init this instance. /// </summary> public void Init() { canvas = Utilites.FindTopmostCanvas(transform).GetComponent <Canvas>(); LayoutUtilites.UpdateLayout(transform.parent.GetComponent <LayoutGroup>()); transform.parent.GetComponentsInChildren <Splitter>().ForEach(x => x.InitSizes()); }
/// <summary> /// Gets the height of the item. /// </summary> /// <returns>The item height.</returns> /// <param name="item">Item.</param> float CalculateItemHeight(TItem item) { if (defaultItemLayoutGroup == null) { DefaultItemCopy.gameObject.SetActive(true); defaultItemLayoutGroup = DefaultItemCopy.GetComponent <LayoutGroup>(); } float height = 0f; if (!IsCanCalculateHeight || ForceAutoHeightCalculation) { if (defaultItemLayoutGroup != null) { SetData(DefaultItemCopy, item); var lg = DefaultItemCopy.GetComponentsInChildren <LayoutGroup>(); Array.Reverse(lg); lg.ForEach(LayoutUtilites.UpdateLayout); LayoutUtilites.UpdateLayout(defaultItemLayoutGroup); height = LayoutUtility.GetPreferredHeight(DefaultItemCopyRect); } } else { SetData(DefaultItemCopy, item); height = (DefaultItemCopy as IListViewItemHeight).Height; } return(height); }
/// <summary> /// Updates the layout. /// </summary> public void UpdateLayout() { LayoutUtilites.UpdateLayout(Layout); if (fitter != null) { fitter.SetLayoutHorizontal(); fitter.SetLayoutVertical(); } }
/// <summary> /// Start this instance. /// </summary> protected virtual void Start() { var layout = GetComponent <HorizontalOrVerticalLayoutGroup>(); if (layout != null) { LayoutUtilites.UpdateLayout(layout); } Init(); }
/// <summary> /// Update item size. /// </summary> /// <param name="item">Item.</param> protected static void UpdateItemSize(AccordionItem item) { LayoutUtilites.UpdateLayoutsRecursive(item.ContentObjectRect.transform); item.ContentObjectWidth = LayoutUtilites.IsWidthControlled(item.ContentObjectRect) ? LayoutUtility.GetPreferredWidth(item.ContentObjectRect) : item.ContentObjectRect.rect.width; item.ContentObjectHeight = LayoutUtilites.IsHeightControlled(item.ContentObjectRect) ? LayoutUtility.GetPreferredHeight(item.ContentObjectRect) : item.ContentObjectRect.rect.height; }
/// <summary> /// Calculates the size of the component for the specified item. /// </summary> /// <returns>The component size.</returns> /// <param name="item">Item.</param> protected virtual Vector2 CalculateComponentSize(TItem item) { if (Owner.DefaultItemLayout == null) { return(Owner.ItemSize); } Owner.SetData(Owner.DefaultItemCopy, item); LayoutUtilites.UpdateLayoutsRecursive(Owner.Container); return(Owner.DefaultItemCopyRect.rect.size); }
/// <summary> /// Refresh header. /// </summary> public void Refresh() { if (layout == null) { layout = GetComponent <LayoutGroup>(); } if (layout != null) { LayoutUtilites.UpdateLayout(layout); } Init(); Resize(); }
/// <summary> /// Update the DisplayedIndices. /// </summary> /// <param name="newIndices">New indices.</param> /// <param name="action">Action.</param> public void DisplayedIndicesUpdate(List <int> newIndices, Action <TComponent> action) { if (IndicesEqual(newIndices)) { return; } FindIndicesDiff(newIndices); if (IndicesRemoved.Count > 0) { for (int i = Components.Count - 1; i >= 0; i--) { var component = Components[i]; if (IndicesRemoved.Contains(component.Index)) { DeactivateComponent(component); Components.RemoveAt(i); ComponentsCache.Add(component); } } } for (int i = 0; i < IndicesAdded.Count; i++) { var component = CreateComponent(); Components.Add(component); } Owner.Items = Components.Convert(x => x as ListViewItem); var start = Components.Count - IndicesAdded.Count; for (int i = 0; i < IndicesAdded.Count; i++) { var component = Components[start + i]; component.Index = IndicesAdded[i]; action(component); } DisplayedIndices.Clear(); DisplayedIndices.AddRange(newIndices); Components.Sort(ComponentsComparer); Components.ForEach(SetComponentAsLastSibling); LayoutUtilites.UpdateLayoutsRecursive(Owner.Container); }
/// <summary> /// Updates the page buttons. /// </summary> protected virtual void UpdatePageButtons() { if (SRDefaultPage == null) { return; } DefaultPages.RemoveAll(IsNullComponent); if (DefaultPages.Count == Pages) { return; } if (DefaultPages.Count < Pages) { DefaultPagesCache.RemoveAll(IsNullComponent); for (int i = DefaultPages.Count; i < Pages; i++) { AddComponent(i); } if (SRNextPage != null) { SRNextPage.SetPage(Pages - 1); SRNextPage.transform.SetAsLastSibling(); } } else { for (int i = Pages; i < DefaultPages.Count; i++) { DefaultPages[i].gameObject.SetActive(false); DefaultPagesCache.Add(DefaultPages[i]); } DefaultPages.RemoveRange(Pages, DefaultPages.Count - Pages); if (SRNextPage != null) { SRNextPage.SetPage(Pages - 1); } } LayoutUtilites.UpdateLayout(DefaultPage.parent.GetComponent <LayoutGroup>()); }
/// <summary> /// Set the DisplayedIndices. /// </summary> /// <param name="newIndices">New indices.</param> /// <param name="action">Action.</param> public void DisplayedIndicesSet(List <int> newIndices, Action <TComponent> action) { SetCount(newIndices.Count); Components.ForEach((x, i) => { x.Index = newIndices[i]; action(x); LayoutUtilites.UpdateLayoutsRecursive(x); }); DisplayedIndices.Clear(); DisplayedIndices.AddRange(newIndices); Components.Sort(ComponentsComparer); Components.ForEach(SetComponentAsLastSibling); }
/// <summary> /// Collapse animation. /// </summary> /// <returns>Animation.</returns> /// <param name="rectTransform">RectTransform.</param> /// <param name="time">Time.</param> /// <param name="isHorizontal">Is Horizontal animated width changes; otherwise height.</param> /// <param name="unscaledTime">Use unscaled time.</param> /// <param name="actionAfter">Action to run after animation.</param> public static IEnumerator Collapse(RectTransform rectTransform, float time = 0.5f, bool isHorizontal = false, bool unscaledTime = false, Action actionAfter = null) { var layoutElement = Utilites.GetOrAddComponent <LayoutElement>(rectTransform); var size = isHorizontal ? (LayoutUtilites.IsWidthControlled(rectTransform) ? LayoutUtility.GetPreferredWidth(rectTransform) : rectTransform.rect.width) : (LayoutUtilites.IsHeightControlled(rectTransform) ? LayoutUtility.GetPreferredHeight(rectTransform) : rectTransform.rect.height); var end_time = Utilites.GetTime(unscaledTime) + time; while (Utilites.GetTime(unscaledTime) <= end_time) { var t = 1 - ((end_time - Utilites.GetTime(unscaledTime)) / time); var new_size = Mathf.Lerp(size, 0, t); if (isHorizontal) { rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, new_size); layoutElement.preferredWidth = new_size; } else { rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, new_size); layoutElement.preferredHeight = new_size; } yield return(null); } // return size back for future use if (isHorizontal) { rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size); layoutElement.preferredWidth = size; } else { rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size); layoutElement.preferredHeight = size; } if (actionAfter != null) { actionAfter(); } }
/// <summary> /// Calculates the size of the component for the specified item. /// </summary> /// <returns>The component size.</returns> /// <param name="item">Item.</param> protected virtual Vector2 CalculateComponentSize(TItem item) { if (Owner.DefaultItemLayout == null) { return(Owner.ItemSize); } Owner.SetData(Owner.DefaultItemCopy, item); Owner.DefaultItemNestedLayouts.ForEach(LayoutUtilites.UpdateLayout); LayoutUtilites.UpdateLayout(Owner.DefaultItemLayout); var size = new Vector2( LayoutUtility.GetPreferredWidth(Owner.DefaultItemCopyRect), LayoutUtility.GetPreferredHeight(Owner.DefaultItemCopyRect)); return(size); }
/// <summary> /// Updates the page buttons. /// </summary> protected virtual void UpdatePageButtons() { if (SRDefaultPage == null) { return; } DefaultPages.RemoveAll(IsNullComponent); if (DefaultPages.Count == Pages) { return; } if (DefaultPages.Count < Pages) { DefaultPagesCache.RemoveAll(IsNullComponent); Enumerable.Range(DefaultPages.Count, Pages - DefaultPages.Count).ForEach(AddComponent); if (SRNextPage != null) { SRNextPage.SetPage(Pages - 1); SRNextPage.transform.SetAsLastSibling(); } } else { var to_cache = DefaultPages.GetRange(Pages, DefaultPages.Count - Pages); to_cache.ForEach(x => x.gameObject.SetActive(false)); DefaultPagesCache.AddRange(to_cache); DefaultPages.RemoveRange(Pages, DefaultPages.Count - Pages); if (SRNextPage != null) { SRNextPage.SetPage(Pages - 1); } } LayoutUtilites.UpdateLayout(DefaultPage.parent.GetComponent <LayoutGroup>()); }
/// <summary> /// Remove header cell. /// </summary> /// <param name="cell">Cell.</param> /// <param name="parent">Parent.</param> public void RemoveCell(GameObject cell, RectTransform parent = null) { var index = CellsInfo.FindIndex(x => x.Rect.gameObject == cell); var cell_info = CellsInfo[index]; if (index == -1) { Debug.LogWarning("Cell not in header", cell); return; } cell.SetActive(false); cell.transform.SetParent(parent, false); if (parent == null) { Destroy(cell); } // remove from cells CellsInfo.RemoveAt(index); // remove events var events = Utilites.GetOrAddComponent <TableHeaderCell>(cell); events.OnInitializePotentialDragEvent.RemoveListener(OnInitializePotentialDrag); events.OnBeginDragEvent.RemoveListener(OnBeginDrag); events.OnDragEvent.RemoveListener(OnDrag); events.OnEndDragEvent.RemoveListener(OnEndDrag); // decrease position for cells where >cell_position CellsInfo.ForEach(x => { if (x.Position > cell_info.Position) { x.Position -= 1; } }); // update list widths LayoutUtilites.UpdateLayout(layout); Resize(); }
/// <summary> /// Raises the drag event. /// </summary> /// <param name="eventData">Event data.</param> public void OnDrag(PointerEventData eventData) { if (!processDrag) { return; } if (canvas == null) { throw new MissingComponentException(gameObject.name + " not in Canvas hierarchy."); } Cursor.SetCursor(CursorTexture, CursorHotSpot, Compatibility.GetCursorMode()); Vector2 p1; RectTransformUtility.ScreenPointToLocalPointInRectangle(RectTransform, eventData.position, GetCamera(), out p1); Vector2 p2; RectTransformUtility.ScreenPointToLocalPointInRectangle(RectTransform, eventData.position - eventData.delta, GetCamera(), out p2); var delta = p1 - p2; if (delta.x > 0) { leftTarget.preferredWidth = Mathf.Min(leftTarget.preferredWidth + delta.x, widthLimit - rightTarget.minWidth); rightTarget.preferredWidth = widthLimit - leftTarget.preferredWidth; } else { rightTarget.preferredWidth = Mathf.Min(rightTarget.preferredWidth - delta.x, widthLimit - leftTarget.minWidth); leftTarget.preferredWidth = widthLimit - rightTarget.preferredWidth; } LayoutUtilites.UpdateLayout(layout); if (OnDragUpdate) { Resize(); } }
/// <summary> /// Initializes a new instance of the <see cref="UIWidgets.StandardLayoutBridge"/> class. /// </summary> /// <param name="layout">Layout.</param> /// <param name="defaultItem">Default item.</param> /// <param name="updateContentSizeFitter">Update ContentSizeFitter on direction change.</param> public StandardLayoutBridge(HorizontalOrVerticalLayoutGroup layout, RectTransform defaultItem, bool updateContentSizeFitter = true) { LayoutUtilites.UpdateLayout(layout); Layout = layout; DefaultItem = defaultItem; UpdateContentSizeFitter = updateContentSizeFitter; isHorizontal = layout is HorizontalLayoutGroup; var firstFillerGO = new GameObject("FirstFiller"); var firstFillerTransform = Utilites.GetOrAddComponent <RectTransform>(firstFillerGO); firstFillerTransform.SetParent(Layout.transform, false); firstFillerTransform.localScale = Vector3.one; FirstFiller = firstFillerGO.AddComponent <LayoutElement>(); var lastFillerGO = new GameObject("LastFiller"); var lastFillerTransform = Utilites.GetOrAddComponent <RectTransform>(lastFillerGO); lastFillerTransform.SetParent(Layout.transform, false); lastFillerTransform.localScale = Vector3.one; LastFiller = lastFillerGO.AddComponent <LayoutElement>(); var size = GetItemSize(); if (IsHorizontal) { firstFillerTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y); lastFillerTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y); } else { firstFillerTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x); lastFillerTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x); } fitter = Layout.GetComponent <ContentSizeFitter>(); }
Vector2 CalculateComponentSize(TItem item) { if (defaultItemLayoutGroup == null) { return(Vector2.zero); } SetData(DefaultItemCopy, item); var lg = DefaultItemCopy.GetComponentsInChildren <LayoutGroup>(); Array.Reverse(lg); lg.ForEach(LayoutUtilites.UpdateLayout); LayoutUtilites.UpdateLayout(defaultItemLayoutGroup); var size = new Vector2( LayoutUtility.GetPreferredWidth(DefaultItemCopyRect), LayoutUtility.GetPreferredHeight(DefaultItemCopyRect) ); return(size); }
/// <summary> /// Gets the height of the item. /// </summary> /// <returns>The item height.</returns> /// <param name="item">Item.</param> float CalculateItemHeight(string item) { if (defaultItemLayoutGroup == null) { defaultItemLayoutGroup = DefaultItemCopy.GetComponent <LayoutGroup>(); } float height = 0f; if (defaultItemLayoutGroup != null) { DefaultItemCopy.gameObject.SetActive(true); DefaultItemCopy.SetData(item); LayoutUtilites.UpdateLayout(defaultItemLayoutGroup); height = LayoutUtility.GetPreferredHeight(DefaultItemCopyRect); DefaultItemCopy.gameObject.SetActive(false); } return(height); }
/// <summary> /// Gets the width of the item. /// </summary> /// <returns>The item width.</returns> /// <param name="item">Item.</param> float CalculateItemWidth(TItem item) { if (defaultItemLayoutGroup == null) { defaultItemLayoutGroup = DefaultItemCopy.GetComponent <LayoutGroup>(); } float width = 0f; if (!IsCanCalculateWidth || ForceAutoWidthCalculation) { if (defaultItemLayoutGroup != null) { DefaultItemCopy.gameObject.SetActive(true); SetData(DefaultItemCopy, item); var lg = DefaultItemCopy.GetComponentsInChildren <LayoutGroup>(); Array.Reverse(lg); lg.ForEach(LayoutUtilites.UpdateLayout); LayoutUtilites.UpdateLayout(defaultItemLayoutGroup); width = LayoutUtility.GetPreferredWidth(DefaultItemCopyRect); DefaultItemCopy.gameObject.SetActive(false); } } else { SetData(DefaultItemCopy, item); width = (DefaultItemCopy as IListViewItemWidth).Width; } return(width); }
/// <summary> /// Updates the layout. /// </summary> protected void UpdateLayout() { LayoutUtilites.UpdateLayout(GetComponent <LayoutGroup>()); }
/// <summary> /// Get component size. /// </summary> /// <param name="defaultItemComponent">Default item component.</param> /// <returns>Component size.</returns> protected virtual Vector2 GetComponentSize(TComponent defaultItemComponent) { LayoutUtilites.UpdateLayoutsRecursive(defaultItemComponent); return((defaultItemComponent.transform as RectTransform).rect.size); }
public static void UpdateLayout(LayoutGroup layout) { LayoutUtilites.UpdateLayout(layout); }