123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using UnityEngine;
- using UnityEngine.UI;
- using System;
- using System.Collections;
- public class AlertPanel : PopUpPanel {
- public const uint YES = 0x1;
- public const uint NO = 0x2;
- public const uint OK = 0x4;
- public const uint CANCEL = 0x8;
- public uint flags;
- protected string _okLabel;
- protected string _yesLabel;
- protected string _noLabel;
- protected string _cancelLabel;
- public Button okBtn;
- public Button yesBtn;
- public Button noBtn;
- public Button cancelBtn;
- public Text yesBtnText;
- public Text noBtnText;
- public Text cancelBtnText;
- public Text titleText;
- public Text contentText;
- protected float oldTextHeight = 0;
- protected event AlertPanelCloseDelegate closeEvent;
- public object data;
- void Awake()
- {
- Transform panelTrans = transform.FindChild("Panel");
- yesBtn = panelTrans.FindChild("BtnContainer/YesBtn").GetComponent<Button>();
- noBtn = panelTrans.FindChild("BtnContainer/NoBtn").GetComponent<Button>();
- cancelBtn = panelTrans.FindChild("BtnContainer/CancelBtn").GetComponent<Button>();
- if (panelTrans.FindChild("BtnContainer/OkBtn") != null)
- okBtn = panelTrans.FindChild("BtnContainer/OkBtn").GetComponent<Button>();
- yesBtnText = yesBtn.transform.FindChild("Text").GetComponent<Text>();
- noBtnText = noBtn.transform.FindChild("Text").GetComponent<Text>();
- cancelBtnText = cancelBtn.transform.FindChild("Text").GetComponent<Text>();
- titleText = panelTrans.FindChild("Title/Text").GetComponent<Text>();
- contentText = panelTrans.FindChild("Text").GetComponent<Text>();
- //redefine font
- // if(okBtn != null)
- // okBtn.GetComponentInChildren<Text>().font = Resources.Load<Font>(Language.GetFont());
- // contentText.font = Resources.Load<Font>(Language.GetFont());
- // titleText.font = Resources.Load<Font>(Language.GetFont());
- // yesBtnText.font = Resources.Load<Font>(Language.GetFont());
- // noBtnText.font = Resources.Load<Font>(Language.GetFont());
- // cancelBtnText.font = Resources.Load<Font>(Language.GetFont());
- //init sound
- // if (okBtn != null)
- // {
- // UISound okBtnSound = okBtn.gameObject.AddComponent<UISound>();
- // okBtnSound.soundType = UISound.Type.Normal;
- // }
- // UISound yesBtnSound = yesBtn.gameObject.AddComponent<UISound>();
- // yesBtnSound.soundType = UISound.Type.Normal;
- // UISound noBtnSound = noBtn.gameObject.AddComponent<UISound>();
- // noBtnSound.soundType = UISound.Type.Back;
- // UISound cancelBtnSound = cancelBtn.gameObject.AddComponent<UISound>();
- // cancelBtnSound.soundType = UISound.Type.Back;
- if(okBtn != null)
- {
- okBtn.onClick.AddListener(
- delegate
- {
- OnClick((int)OK);
- });
- }
- yesBtn.onClick.AddListener(
- delegate
- {
- OnClick((int)YES);
- });
- noBtn.onClick.AddListener(
- delegate
- {
- OnClick((int)NO);
- });
- cancelBtn.onClick.AddListener(
- delegate
- {
- OnClick((int)CANCEL);
- });
- }
- public string okLabel
- {
- set
- {
- _okLabel = value;
- if ((flags & OK) != 0)
- {
- yesBtnText.text = value;
- }
- }
- get
- {
- return _okLabel;
- }
- }
- public string yesLabel
- {
- set
- {
- _yesLabel = value;
- if ((flags & YES) != 0)
- {
- yesBtnText.text = value;
- }
- }
- get
- {
- return _yesLabel;
- }
- }
- public string noLabel
- {
- set
- {
- _noLabel = value;
- if ((flags & NO) != 0)
- {
- noBtnText.text = value;
- }
- }
- get
- {
- return _noLabel;
- }
- }
- public string cancelLabel
- {
- set
- {
- _cancelLabel = value;
- if ((flags & CANCEL) != 0)
- {
- cancelBtnText.text = value;
- }
- }
- get
- {
- return _cancelLabel;
- }
- }
- public virtual void OnClick(int detail)
- {
- if((flags & OK) != 0 && detail == YES)
- detail = (int)OK;
- AlertCloseEvent evt = new AlertCloseEvent();
- evt.detail = (uint)detail;
- evt.target = this;
- if(closeEvent != null)
- {
- closeEvent(evt);
- }
- PopUpManager.RemovePopUp(this);
- }
- void OnDestroy()
- {
- yesBtn.onClick.RemoveAllListeners();
- noBtn.onClick.RemoveAllListeners();
- cancelBtn.onClick.RemoveAllListeners();
- }
- public delegate void AlertPanelCloseDelegate(AlertCloseEvent evt);
- public static AlertPanel Show(string title, string content, uint flags=0x4, AlertPanelCloseDelegate closeHandler=null)
- {
- AlertPanel alertPanel = (AlertPanel)PopUpManager.AddPopUp((GameObject)Resources.Load("Prefabs/UI/AlertPanel"), null, true, typeof(AlertPanel));
- alertPanel.flags = flags;
- alertPanel.closeEvent += closeHandler;
- alertPanel.contentText.text = content;
- if(title != null)
- alertPanel.titleText.text = title;
- if((flags & OK) == 0 && (flags & YES) == 0)
- {
- GameObject.Destroy(alertPanel.yesBtn.gameObject);
- }
- else
- {
- if((flags & OK) != 0)
- alertPanel.yesBtnText.text = Language.GetStr("Public", "ok");
- else if((flags & YES) != 0)
- alertPanel.yesBtnText.text = Language.GetStr("Public", "yes");
- }
- if((flags & NO) == 0)
- GameObject.Destroy(alertPanel.noBtn.gameObject);
- else
- alertPanel.noBtnText.text = Language.GetStr("Public", "no");
- if((flags & CANCEL) == 0)
- GameObject.Destroy(alertPanel.cancelBtn.gameObject);
- else
- alertPanel.cancelBtnText.text = Language.GetStr("Public", "cancel");
- return alertPanel;
- }
- public static AlertPanel Show(string content, uint flags=0x4, AlertPanelCloseDelegate closeHandler=null)
- {
- return AlertPanel.Show (null, content, flags, closeHandler);
- }
- }
|