using FairyGUI; using System.Collections; using UI.Launcher; namespace GFGGame.Launcher { public delegate void AlertCallback(object data); //不要直接创建此类,通过AlertSystem调用 public class AlertWindow : Window, IAlert { private UI_LauncherAlertUI _ui; private float m_xLeftButton = 0; private float m_xRightButton = 0; private string m_content = ""; private string m_textLeft = "取消"; private string m_textRight = "确认"; private bool m_isShowLeftButton = false; private bool m_isShowRightButton = false; private object m_data = null; private AlertCallback m_leftButtonCallback = null; private AlertCallback m_rightButtonCallback = null; public AlertWindow() : base() { } protected override void OnInit() { base.OnInit(); _ui = UI_LauncherAlertUI.Create(); this.contentPane = _ui.target; this.Center(); this.modal = true; //this.clickBlankToClose = false; m_xLeftButton = _ui.m_btnLeft.x; m_xRightButton = _ui.m_btnRight.x; // this.sortingOrder = ConstSortingOrder.FLOATING_PROMPT; this.SetMessage(m_content); this.SetLeftButton(m_isShowLeftButton, m_textLeft); this.SetRightButton(m_isShowRightButton, m_textRight); updateButtonPosition(); _ui.m_btnLeft.onClick.Add(() => { this.Hide(); m_leftButtonCallback?.Invoke(m_data); }); _ui.m_btnRight.onClick.Add(() => { this.Hide(); m_rightButtonCallback?.Invoke(m_data); }); } protected override void OnShown() { base.OnShown(); } protected override void OnHide() { base.OnHide(); } protected override void OnUpdate() { base.OnUpdate(); if (this.root != null) { int i = this.parent.GetChildIndex(this); int iMax = this.parent.numChildren - 1; if (i < iMax) { this.parent.SetChildIndex(this, iMax); } } } private void updateButtonPosition() { float xLeftButton = m_xLeftButton; float xRightButton = m_xRightButton; float xCenter = (m_xLeftButton + m_xRightButton) / 2; if (!m_isShowLeftButton && m_isShowRightButton) { xRightButton = xCenter; } else if (m_isShowLeftButton && !m_isShowRightButton) { xLeftButton = xCenter; } if (_ui != null) { _ui.m_btnLeft.x = xLeftButton; this._ui.m_btnLeft.visible = m_isShowLeftButton; } if (_ui != null) { _ui.m_btnRight.x = xRightButton; this._ui.m_btnRight.visible = m_isShowRightButton; } } public IAlert Reset() { m_content = ""; m_textLeft = "取消"; m_textRight = "确认"; m_leftButtonCallback = null; m_rightButtonCallback = null; m_isShowLeftButton = false; m_isShowRightButton = false; //clickBlankToClose = false; m_data = null; if (this._ui != null) { this._ui.m_txtContent.text = m_content; } if (this._ui != null) { this._ui.m_btnLeft.text = m_textLeft; this._ui.m_btnLeft.visible = m_isShowLeftButton; } if (this._ui != null) { this._ui.m_btnRight.text = m_textRight; this._ui.m_btnRight.visible = m_isShowRightButton; } updateButtonPosition(); return this; } public IAlert SetMessage(string message) { this.m_content = message; if (this._ui != null) { this._ui.m_txtContent.text = m_content; // Debug.Log("alert height=" + this._ui.m_txtContent.textHeight + " size=" + this._ui.m_txtContent.textFormat.size); if (this._ui.m_txtContent.textHeight > this._ui.m_txtContent.textFormat.size * 2) { this._ui.m_txtContent.align = AlignType.Left; } else { this._ui.m_txtContent.align = AlignType.Center; } this._ui.target.Center(); } return this; } public IAlert SetLeftButton(bool isShow, string textLeft = "取消", AlertCallback callback = null) { this.m_isShowLeftButton = isShow; this.m_leftButtonCallback = callback; this.m_textLeft = textLeft; if (this._ui != null) { this._ui.m_btnLeft.text = m_textLeft; this._ui.m_btnLeft.visible = m_isShowLeftButton; } updateButtonPosition(); return this; } public IAlert SetRightButton(bool isShow, string textRight = "确认", AlertCallback callback = null) { this.m_isShowRightButton = isShow; this.m_rightButtonCallback = callback; this.m_textRight = textRight; if (this._ui != null) { this._ui.m_btnRight.text = m_textRight; this._ui.m_btnRight.visible = m_isShowRightButton; } updateButtonPosition(); return this; } public IAlert SetData(object data) { this.m_data = data; return this; } public IAlert SetClickBlankToClose(bool value) { //this.clickBlankToClose = value; return this; } public object Current => null; public bool MoveNext() { return this.isShowing; } void IEnumerator.Reset() { } } }