AlertPanel.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System;
  4. using System.Collections;
  5. public class AlertPanel : PopUpPanel {
  6. public const uint YES = 0x1;
  7. public const uint NO = 0x2;
  8. public const uint OK = 0x4;
  9. public const uint CANCEL = 0x8;
  10. public uint flags;
  11. protected string _okLabel;
  12. protected string _yesLabel;
  13. protected string _noLabel;
  14. protected string _cancelLabel;
  15. public Button okBtn;
  16. public Button yesBtn;
  17. public Button noBtn;
  18. public Button cancelBtn;
  19. public Text yesBtnText;
  20. public Text noBtnText;
  21. public Text cancelBtnText;
  22. public Text titleText;
  23. public Text contentText;
  24. protected float oldTextHeight = 0;
  25. protected event AlertPanelCloseDelegate closeEvent;
  26. public object data;
  27. void Awake()
  28. {
  29. Transform panelTrans = transform.FindChild("Panel");
  30. yesBtn = panelTrans.FindChild("BtnContainer/YesBtn").GetComponent<Button>();
  31. noBtn = panelTrans.FindChild("BtnContainer/NoBtn").GetComponent<Button>();
  32. cancelBtn = panelTrans.FindChild("BtnContainer/CancelBtn").GetComponent<Button>();
  33. if (panelTrans.FindChild("BtnContainer/OkBtn") != null)
  34. okBtn = panelTrans.FindChild("BtnContainer/OkBtn").GetComponent<Button>();
  35. yesBtnText = yesBtn.transform.FindChild("Text").GetComponent<Text>();
  36. noBtnText = noBtn.transform.FindChild("Text").GetComponent<Text>();
  37. cancelBtnText = cancelBtn.transform.FindChild("Text").GetComponent<Text>();
  38. titleText = panelTrans.FindChild("Title/Text").GetComponent<Text>();
  39. contentText = panelTrans.FindChild("Text").GetComponent<Text>();
  40. //redefine font
  41. // if(okBtn != null)
  42. // okBtn.GetComponentInChildren<Text>().font = Resources.Load<Font>(Language.GetFont());
  43. // contentText.font = Resources.Load<Font>(Language.GetFont());
  44. // titleText.font = Resources.Load<Font>(Language.GetFont());
  45. // yesBtnText.font = Resources.Load<Font>(Language.GetFont());
  46. // noBtnText.font = Resources.Load<Font>(Language.GetFont());
  47. // cancelBtnText.font = Resources.Load<Font>(Language.GetFont());
  48. //init sound
  49. // if (okBtn != null)
  50. // {
  51. // UISound okBtnSound = okBtn.gameObject.AddComponent<UISound>();
  52. // okBtnSound.soundType = UISound.Type.Normal;
  53. // }
  54. // UISound yesBtnSound = yesBtn.gameObject.AddComponent<UISound>();
  55. // yesBtnSound.soundType = UISound.Type.Normal;
  56. // UISound noBtnSound = noBtn.gameObject.AddComponent<UISound>();
  57. // noBtnSound.soundType = UISound.Type.Back;
  58. // UISound cancelBtnSound = cancelBtn.gameObject.AddComponent<UISound>();
  59. // cancelBtnSound.soundType = UISound.Type.Back;
  60. if(okBtn != null)
  61. {
  62. okBtn.onClick.AddListener(
  63. delegate
  64. {
  65. OnClick((int)OK);
  66. });
  67. }
  68. yesBtn.onClick.AddListener(
  69. delegate
  70. {
  71. OnClick((int)YES);
  72. });
  73. noBtn.onClick.AddListener(
  74. delegate
  75. {
  76. OnClick((int)NO);
  77. });
  78. cancelBtn.onClick.AddListener(
  79. delegate
  80. {
  81. OnClick((int)CANCEL);
  82. });
  83. }
  84. public string okLabel
  85. {
  86. set
  87. {
  88. _okLabel = value;
  89. if ((flags & OK) != 0)
  90. {
  91. yesBtnText.text = value;
  92. }
  93. }
  94. get
  95. {
  96. return _okLabel;
  97. }
  98. }
  99. public string yesLabel
  100. {
  101. set
  102. {
  103. _yesLabel = value;
  104. if ((flags & YES) != 0)
  105. {
  106. yesBtnText.text = value;
  107. }
  108. }
  109. get
  110. {
  111. return _yesLabel;
  112. }
  113. }
  114. public string noLabel
  115. {
  116. set
  117. {
  118. _noLabel = value;
  119. if ((flags & NO) != 0)
  120. {
  121. noBtnText.text = value;
  122. }
  123. }
  124. get
  125. {
  126. return _noLabel;
  127. }
  128. }
  129. public string cancelLabel
  130. {
  131. set
  132. {
  133. _cancelLabel = value;
  134. if ((flags & CANCEL) != 0)
  135. {
  136. cancelBtnText.text = value;
  137. }
  138. }
  139. get
  140. {
  141. return _cancelLabel;
  142. }
  143. }
  144. public virtual void OnClick(int detail)
  145. {
  146. if((flags & OK) != 0 && detail == YES)
  147. detail = (int)OK;
  148. AlertCloseEvent evt = new AlertCloseEvent();
  149. evt.detail = (uint)detail;
  150. evt.target = this;
  151. if(closeEvent != null)
  152. {
  153. closeEvent(evt);
  154. }
  155. PopUpManager.RemovePopUp(this);
  156. }
  157. void OnDestroy()
  158. {
  159. yesBtn.onClick.RemoveAllListeners();
  160. noBtn.onClick.RemoveAllListeners();
  161. cancelBtn.onClick.RemoveAllListeners();
  162. }
  163. public delegate void AlertPanelCloseDelegate(AlertCloseEvent evt);
  164. public static AlertPanel Show(string title, string content, uint flags=0x4, AlertPanelCloseDelegate closeHandler=null)
  165. {
  166. AlertPanel alertPanel = (AlertPanel)PopUpManager.AddPopUp((GameObject)Resources.Load("Prefabs/UI/AlertPanel"), null, true, typeof(AlertPanel));
  167. alertPanel.flags = flags;
  168. alertPanel.closeEvent += closeHandler;
  169. alertPanel.contentText.text = content;
  170. if(title != null)
  171. alertPanel.titleText.text = title;
  172. if((flags & OK) == 0 && (flags & YES) == 0)
  173. {
  174. GameObject.Destroy(alertPanel.yesBtn.gameObject);
  175. }
  176. else
  177. {
  178. if((flags & OK) != 0)
  179. alertPanel.yesBtnText.text = Language.GetStr("Public", "ok");
  180. else if((flags & YES) != 0)
  181. alertPanel.yesBtnText.text = Language.GetStr("Public", "yes");
  182. }
  183. if((flags & NO) == 0)
  184. GameObject.Destroy(alertPanel.noBtn.gameObject);
  185. else
  186. alertPanel.noBtnText.text = Language.GetStr("Public", "no");
  187. if((flags & CANCEL) == 0)
  188. GameObject.Destroy(alertPanel.cancelBtn.gameObject);
  189. else
  190. alertPanel.cancelBtnText.text = Language.GetStr("Public", "cancel");
  191. return alertPanel;
  192. }
  193. public static AlertPanel Show(string content, uint flags=0x4, AlertPanelCloseDelegate closeHandler=null)
  194. {
  195. return AlertPanel.Show (null, content, flags, closeHandler);
  196. }
  197. }