//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Saturday, September 23, 2017 //-------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace Snxxz.UI { public class FitterWin : Window { private ScrollerController fitterCtrl; private RectTransform fitterFrame; public static Dictionary fitterTypes; public static RectTransform target; private static Action fitterAction; public static Action FitterAction { set { fitterAction = value; } } public static Action fitterClose; private ClickScreenOtherSpace spaceClick; #region Built-in protected override void BindController() { fitterFrame = transform.Find("FitterFrame") as RectTransform; fitterCtrl = this.GetComponent("FitterFrame/Controller"); spaceClick = this.GetComponent("FitterFrame"); } protected override void AddListeners() { fitterCtrl.OnRefreshCell += OnRefreshFitterCell; spaceClick.AddListener(CloseClick); } protected override void OnPreOpen() { fitterFrame.gameObject.SetActive(false); InitFitter(); } protected override void OnActived() { base.OnActived(); SnxxzGame.Instance.StartCoroutine(Co_DelayDisplay()); } protected override void OnAfterOpen() { } protected override void OnPreClose() { } protected override void OnAfterClose() { fitterFrame.gameObject.SetActive(false); if (fitterClose != null) { fitterClose(); } } #endregion private void OnRefreshFitterCell(ScrollerDataType type, CellView cell) { int index = cell.index; if (fitterTypes != null && fitterTypes.ContainsKey(index)) { Text text = cell.GetComponent("Text"); text.text = fitterTypes[index]; } } private void InitFitter() { Vector2 nSize = fitterFrame.sizeDelta; if (fitterTypes != null && fitterTypes.Count < 6) { nSize.y = fitterTypes.Count * fitterCtrl.m_CellHeaderPrefab.height + (fitterTypes.Count - 1) * fitterCtrl.m_Scorller.spacing + 24; fitterCtrl.maxCellCnt = fitterTypes.Count; } else { nSize.y = 5 * fitterCtrl.m_CellHeaderPrefab.height + 4 * fitterCtrl.m_Scorller.spacing + 24; fitterCtrl.maxCellCnt = 5; } fitterFrame.sizeDelta = nSize; fitterCtrl.Refresh(); if (fitterTypes != null && fitterTypes.Count > 0) { foreach (int key in fitterTypes.Keys) { fitterCtrl.AddCell(ScrollerDataType.Header, key, OnFitterCellClick); } } fitterCtrl.Restart(); } private void OnFitterCellClick(CellView cell) { int index = cell.index; if (fitterAction != null) { fitterAction(index); CloseImmediately(); } } private void SetPos() { fitterFrame.gameObject.SetActive(true); Transform rt = target.parent; Vector3 pos = target.localPosition; pos.y -= target.sizeDelta.y / 2; if (rt != null) { pos = rt.TransformPoint(pos); } fitterFrame.localPosition = transform.InverseTransformPoint(pos); } IEnumerator Co_DelayDisplay() { yield return null; SetPos(); } } }