AlertPanel.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System;
  4. using System.Collections;
  5. public class AlertPanel : PopUpPanel {
  6. public enum AlertPanelType
  7. {
  8. Bordered,
  9. Fluted
  10. }
  11. public Animator panelType;
  12. public const uint TIME_OUT = 0;
  13. public const uint YES = 0x1;
  14. public const uint NO = 0x2;
  15. public const uint OK = 0x4;
  16. public const uint CANCEL = 0x8;
  17. public uint flags;
  18. protected string _okLabel;
  19. protected string _yesLabel;
  20. protected string _noLabel;
  21. protected string _cancelLabel;
  22. public Button okBtn;
  23. public Button yesBtn;
  24. public Button noBtn;
  25. public Button cancelBtn;
  26. public Button closeBtn;
  27. public Text yesBtnText;
  28. public Text noBtnText;
  29. public Text cancelBtnText;
  30. public Text titleText;
  31. public Sprite redBtn, greenBtn;
  32. public Text contentText;
  33. protected float oldTextHeight = 0;
  34. protected event AlertPanelCloseDelegate closeEvent;
  35. public object data;
  36. void Awake()
  37. {
  38. InitBackGround(transform);
  39. Transform panelTrans = transform.FindChild("Panel");
  40. panelType = panelTrans.GetComponent<Animator>();
  41. yesBtn = panelTrans.FindChild("BtnContainer/YesBtn").GetComponent<Button>();
  42. noBtn = panelTrans.FindChild("BtnContainer/NoBtn").GetComponent<Button>();
  43. cancelBtn = panelTrans.FindChild("BtnContainer/CancelBtn").GetComponent<Button>();
  44. if (panelTrans.FindChild("BtnContainer/OkBtn") != null)
  45. okBtn = panelTrans.FindChild("BtnContainer/OkBtn").GetComponent<Button>();
  46. yesBtnText = yesBtn.transform.FindChild("Text").GetComponent<Text>();
  47. noBtnText = noBtn.transform.FindChild("Text").GetComponent<Text>();
  48. cancelBtnText = cancelBtn.transform.FindChild("Text").GetComponent<Text>();
  49. titleText = panelTrans.FindChild("TitleContainer/Title").GetComponent<Text>();
  50. closeBtn = panelTrans.FindChild("CloseButton").GetComponent<Button>();
  51. if (panelTrans.FindChild("ButtonImage") != null)
  52. {
  53. redBtn = panelTrans.FindChild("ButtonImage").GetChild(0).GetComponent<Image>().sprite;
  54. greenBtn = panelTrans.FindChild("ButtonImage").GetChild(1).GetComponent<Image>().sprite;
  55. }
  56. contentText = panelTrans.FindChild("Line/Text").GetComponent<Text>();
  57. //redefine font
  58. if (Language.initialized)
  59. {
  60. if (okBtn != null)
  61. okBtn.GetComponentInChildren<Text>().font = Language.GetFont();
  62. contentText.font = Language.GetFont();
  63. titleText.font = Language.GetFont();
  64. yesBtnText.font = Language.GetFont();
  65. noBtnText.font = Language.GetFont();
  66. cancelBtnText.font = Language.GetFont();
  67. }
  68. else
  69. {
  70. if (okBtn != null)
  71. okBtn.GetComponentInChildren<Text>().font = Language.GetSystemFont();
  72. contentText.font = Language.GetSystemFont();
  73. titleText.font = Language.GetSystemFont();
  74. yesBtnText.font = Language.GetSystemFont();
  75. noBtnText.font = Language.GetSystemFont();
  76. cancelBtnText.font = Language.GetSystemFont();
  77. }
  78. //init sound
  79. if (okBtn != null)
  80. {
  81. //UISound okBtnSound = okBtn.gameObject.AddComponent<UISound>();
  82. //okBtnSound.soundType = UISound.Type.Normal;
  83. SoundManager.RegisterUISound(okBtn.gameObject, UISound.Type.Normal);
  84. }
  85. //UISound yesBtnSound = yesBtn.gameObject.AddComponent<UISound>();
  86. //yesBtnSound.soundType = UISound.Type.Normal;
  87. //UISound noBtnSound = noBtn.gameObject.AddComponent<UISound>();
  88. //noBtnSound.soundType = UISound.Type.Back;
  89. //UISound cancelBtnSound = cancelBtn.gameObject.AddComponent<UISound>();
  90. //cancelBtnSound.soundType = UISound.Type.Back;
  91. SoundManager.RegisterUISound(yesBtn.gameObject, UISound.Type.Normal);
  92. SoundManager.RegisterUISound(noBtn.gameObject, UISound.Type.Back);
  93. SoundManager.RegisterUISound(cancelBtn.gameObject, UISound.Type.Back);
  94. SoundManager.RegisterUISound(closeBtn, UISound.Type.Close);
  95. if(okBtn != null)
  96. {
  97. okBtn.onClick.AddListener(
  98. delegate
  99. {
  100. OnClick((int)OK);
  101. });
  102. }
  103. yesBtn.onClick.AddListener(
  104. delegate
  105. {
  106. OnClick((int)YES);
  107. });
  108. noBtn.onClick.AddListener(
  109. delegate
  110. {
  111. OnClick((int)NO);
  112. });
  113. cancelBtn.onClick.AddListener(
  114. delegate
  115. {
  116. OnClick((int)CANCEL);
  117. });
  118. closeBtn.onClick.AddListener(Close);
  119. }
  120. public string okLabel
  121. {
  122. set
  123. {
  124. _okLabel = value;
  125. if ((flags & OK) != 0)
  126. {
  127. yesBtnText.text = value;
  128. //yesBtn.GetComponent<Image>().sprite = redBtn;
  129. }
  130. }
  131. get
  132. {
  133. return _okLabel;
  134. }
  135. }
  136. public string yesLabel
  137. {
  138. set
  139. {
  140. _yesLabel = value;
  141. if ((flags & YES) != 0)
  142. {
  143. yesBtnText.text = value;
  144. //yesBtn.GetComponent<Image>().sprite = redBtn;
  145. }
  146. }
  147. get
  148. {
  149. return _yesLabel;
  150. }
  151. }
  152. public string noLabel
  153. {
  154. set
  155. {
  156. _noLabel = value;
  157. if ((flags & NO) != 0)
  158. {
  159. noBtnText.text = value;
  160. //noBtnText.GetComponent<Image>().sprite = greenBtn;
  161. }
  162. }
  163. get
  164. {
  165. return _noLabel;
  166. }
  167. }
  168. public string cancelLabel
  169. {
  170. set
  171. {
  172. _cancelLabel = value;
  173. if ((flags & CANCEL) != 0)
  174. {
  175. cancelBtnText.text = value;
  176. //cancelBtnText.GetComponent<Image>().sprite = greenBtn;
  177. }
  178. }
  179. get
  180. {
  181. return _cancelLabel;
  182. }
  183. }
  184. public virtual void OnClick(int detail)
  185. {
  186. if((flags & OK) != 0 && detail == YES)
  187. detail = (int)OK;
  188. AlertCloseEvent evt = new AlertCloseEvent();
  189. evt.detail = (uint)detail;
  190. evt.target = this;
  191. GameTime.timeScale = 1;
  192. if(closeEvent != null)
  193. {
  194. closeEvent(evt);
  195. }
  196. Close();
  197. }
  198. override public void Close()
  199. {
  200. OnClose();
  201. //PopUpManager.RemovePopUp(this);
  202. }
  203. void OnDestroy()
  204. {
  205. if (yesBtn != null) yesBtn.onClick.RemoveAllListeners();
  206. if (noBtn != null) noBtn.onClick.RemoveAllListeners();
  207. if (cancelBtn != null) cancelBtn.onClick.RemoveAllListeners();
  208. if(closeBtn != null)
  209. closeBtn.onClick.RemoveAllListeners();
  210. }
  211. public delegate void AlertPanelCloseDelegate(AlertCloseEvent evt);
  212. public static AlertPanel Show(string txt="", uint flags=0x4, AlertPanelCloseDelegate closeHandler=null, string title = null, bool showClose = false, bool pauseGame = false)
  213. {
  214. AlertPanel alertPanel = (AlertPanel)PopUpManager.AddPopUp(ResourcesUtil.GetInstance().GetGameObject("UI/AlertPanel/AlertPanel" + Language.lanForUI), null, true, typeof(AlertPanel));
  215. alertPanel.flags = flags;
  216. if(closeHandler != null)
  217. alertPanel.closeEvent += closeHandler;
  218. alertPanel.contentText.text = txt;
  219. if (title == null)
  220. alertPanel.titleText.text = Language.GetStr("AlertPanel", "title");
  221. else if (title != "")
  222. alertPanel.titleText.text = title;
  223. else
  224. alertPanel.titleText.gameObject.SetActive(false);
  225. if((flags & OK) == 0 && (flags & YES) == 0)
  226. {
  227. GameObject.Destroy(alertPanel.yesBtn.gameObject);
  228. }
  229. else
  230. {
  231. if((flags & OK) != 0)
  232. {
  233. if (Language.IsLanguageInit())
  234. alertPanel.yesBtnText.text = Language.GetStr("Public", "ok");
  235. else
  236. alertPanel.yesBtnText.text = "OK";
  237. }
  238. else if((flags & YES) != 0)
  239. alertPanel.yesBtnText.text = Language.GetStr("Public", "yes");
  240. }
  241. if((flags & NO) == 0)
  242. GameObject.Destroy(alertPanel.noBtn.gameObject);
  243. else
  244. alertPanel.noBtnText.text = Language.GetStr("Public", "no");
  245. if((flags & CANCEL) == 0)
  246. GameObject.Destroy(alertPanel.cancelBtn.gameObject);
  247. else
  248. alertPanel.cancelBtnText.text = Language.GetStr("Public", "cancel");
  249. alertPanel.closeBtn.gameObject.SetActive(showClose);
  250. //alertPanel.panelType.Play(type.ToString());
  251. if (pauseGame)
  252. {
  253. if (Session.GetInstance().networkMode == Session.NetworkMode.OffLine)
  254. GameTime.timeScale = 0;
  255. }
  256. return alertPanel;
  257. }
  258. }