1
0

DWAlert.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var DWAlert = {
  2. _alert: null,
  3. _title: null,
  4. _detailLabel: null,
  5. _cancelButton: null,
  6. _confirmButton: null,
  7. _confirmCallBack: null,
  8. _animDuration: 0.3
  9. };
  10. /**
  11. * 显示弹窗
  12. * @param {string} titleString
  13. * @param {string} detailString
  14. * @param {function} confirmCallBack
  15. * @param {bool} needCancel
  16. * @param {float} animDuration
  17. */
  18. DWAlert.show = function (titleString = "", detailString, confirmCallBack, needCancel = true, animDuration) {
  19. if (DWAlert._alert != undefined) { return; }
  20. DWAlert._animDuration = animDuration ? animDuration : DWAlert._animDuration;
  21. cc.loader.loadRes("./prefabs/DWAlert", cc.Prefab, (error, prefab) => {
  22. if (error) {
  23. console.log(error);
  24. return;
  25. }
  26. var alert = cc.instantiate(prefab);
  27. DWAlert._alert = alert;
  28. // 动画
  29. var cbFadeOut = cc.callFunc(DWAlert.onFadeOutFinish, this);
  30. var cbFadeIn = cc.callFunc(DWAlert.onFadeInFinish, this);
  31. this.actionFadeIn = cc.sequence(cc.spawn(cc.fadeTo(DWAlert._animDuration, 255), cc.scaleTo(DWAlert._animDuration, 1.0)), cbFadeIn);
  32. this.actionFadeOut = cc.sequence(cc.spawn(cc.fadeTo(DWAlert._animDuration, 0), cc.scaleTo(DWAlert._animDuration, 2.0)), cbFadeOut);
  33. DWAlert._title = cc.find("alertBackground/titleLabel", alert);
  34. DWAlert._detailLabel = cc.find("alertBackground/detailLabel", alert);
  35. DWAlert._cancelButton = cc.find("alertBackground/cancelButton", alert);
  36. DWAlert._confirmButton = cc.find("alertBackground/confirmButton", alert);
  37. DWAlert._cancelButton.on(cc.Node.EventType.TOUCH_END, DWAlert.clickCancelButton, this);
  38. DWAlert._confirmButton.on(cc.Node.EventType.TOUCH_END, DWAlert.clickConfirmButton, this);
  39. DWAlert._alert.parent = cc.find("Canvas");
  40. DWAlert.startFadeIn();
  41. DWAlert.config(titleString, detailString, confirmCallBack, needCancel);
  42. });
  43. };
  44. DWAlert.config = function (titleString, detailString, confirmCallBack, needCancel) {
  45. DWAlert._confirmCallBack = confirmCallBack || function () {};
  46. DWAlert._title.getComponent(cc.Label).string = titleString;
  47. if (detailString === undefined || detailString === null) {
  48. DWAlert._detailLabel.active = false;
  49. } else {
  50. DWAlert._detailLabel.active = true;
  51. DWAlert._detailLabel.getComponent(cc.Label).string = detailString;
  52. }
  53. if (needCancel) {
  54. DWAlert._cancelButton.active = true;
  55. } else {
  56. DWAlert._cancelButton.active = false;
  57. DWAlert._confirmButton.x = 0;
  58. }
  59. };
  60. // 显示弹窗动画
  61. DWAlert.startFadeIn = function () {
  62. DWAlert._alert.position = cc.p(0, 0);
  63. DWAlert._alert.setScale(2);
  64. DWAlert._alert.opacity = 0;
  65. DWAlert._alert.runAction(this.actionFadeIn);
  66. };
  67. // 关闭弹窗动画
  68. DWAlert.startFadeOut = function () {
  69. DWAlert._alert.runAction(this.actionFadeOut);
  70. };
  71. // 显示弹窗动画完成回调
  72. DWAlert.onFadeInFinish = function () {
  73. };
  74. // 关闭弹窗动画完成回调
  75. DWAlert.onFadeOutFinish = function () {
  76. DWAlert._cancelButton.off(cc.Node.EventType.TOUCH_END, DWAlert.clickCancelButton, this);
  77. DWAlert._confirmButton.off(cc.Node.EventType.TOUCH_END, DWAlert.clickConfirmButton, this);
  78. DWAlert.onDestory();
  79. };
  80. DWAlert.clickConfirmButton = function () {
  81. DWAlert._confirmCallBack();
  82. DWAlert.startFadeOut();
  83. };
  84. DWAlert.clickCancelButton = function () {
  85. DWAlert.startFadeOut();
  86. };
  87. DWAlert.onDestory = function() {
  88. DWAlert._alert = null;
  89. DWAlert._title = null;
  90. DWAlert._detailLabel = null;
  91. DWAlert._cancelButton = null;
  92. DWAlert._confirmButton = null;
  93. DWAlert._confirmCallBack = null;
  94. DWAlert._animDuration = 0.3;
  95. };
  96. module.exports = DWAlert;