SDKManager.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import BasicManager from "../../Engine/Basic/BasicManager";
  2. import EventManager from "../../Engine/Event/EventManager";
  3. import Tool from "../../Engine/Tool/Tool";
  4. import GameEventName from "../Event/GameEventName";
  5. import GameManager from "../Game/GameManager";
  6. import { CMD, SDK } from "./sdk";
  7. export interface _ISDKButton {
  8. button: fgui.GComponent,
  9. color: Array<cc.Color>,
  10. callback: Function;
  11. }
  12. /**
  13. * SDK管理器
  14. */
  15. export default class SDKManager extends BasicManager {
  16. /**按钮列表,栈结构 */
  17. private static buttons: Array<Array<_ISDKButton>> = [];
  18. /**按钮索引 */
  19. private static buttonIndex: number = 0;
  20. constructor () {
  21. super();
  22. Tool.log("生成SDK管理器");
  23. SDK.on(CMD.ENTER_KEY.toString(), this.confirm, this);
  24. SDK.on(CMD.CANCLE_KEY.toString(), this.cancel, this);
  25. SDK.on(CMD.BACK_LEFT.toString(), this.up, this);
  26. SDK.on(CMD.BACK_RIGHT.toString(), this.down, this);
  27. EventManager.onEvent(CMD.ENTER_KEY.toString(), this, this.confirm);
  28. EventManager.onEvent(CMD.CANCLE_KEY.toString(), this, this.cancel);
  29. EventManager.onEvent(CMD.BACK_LEFT.toString(), this, this.up);
  30. EventManager.onEvent(CMD.BACK_RIGHT.toString(), this, this.down);
  31. let values: Array<string> = Object.values(GameManager.ActionEvent);
  32. for (let index = 0; index < values.length; index++) {
  33. SDK.on(values[index], () => {
  34. EventManager.sendEvent(values[index]);
  35. }, this);
  36. }
  37. }
  38. /**添加按钮 */
  39. public static addButton (buttons: Array<_ISDKButton>): void {
  40. if (SDKManager.buttons.length > 0 && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex] != null) {
  41. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(1).visible = false;
  42. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(2).asTextField.color =
  43. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].color[0];
  44. }
  45. SDKManager.buttons.push(buttons);
  46. SDKManager.buttonIndex = 0;
  47. SDKManager.updateButton(0);
  48. }
  49. /**删除按钮 */
  50. public static removeButton (count: number = 1): void {
  51. SDKManager.buttons.splice(SDKManager.buttons.length-count, count);
  52. if (SDKManager.buttons.length > 0 && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex] != null) {
  53. SDKManager.updateButton(0);
  54. }
  55. }
  56. /**更新按钮 */
  57. public static updateButton (value: number): void {
  58. if (SDKManager.buttons.length > 0 && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex] != null) {
  59. if (value != 0) {
  60. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(1).visible = false;
  61. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(2).asTextField.color =
  62. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].color[0];
  63. SDKManager.buttonIndex += value;
  64. if (SDKManager.buttonIndex < 0) {
  65. SDKManager.buttonIndex = SDKManager.buttons[SDKManager.buttons.length-1].length-1;
  66. }
  67. else if (SDKManager.buttonIndex >= SDKManager.buttons[SDKManager.buttons.length-1].length) {
  68. SDKManager.buttonIndex = 0;
  69. }
  70. }
  71. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(1).visible = true;
  72. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(2).asTextField.color =
  73. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].color[1];
  74. }
  75. }
  76. /**确认 */
  77. private confirm (): void {
  78. if (SDKManager.buttons.length > 0 && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex] != null) {
  79. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].callback && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].callback();
  80. }
  81. EventManager.sendEvent(GameEventName.SDK.SDK_Confirm);
  82. }
  83. /**取消 */
  84. private cancel (): void {
  85. EventManager.sendEvent(GameEventName.SDK.SDK_Cancel);
  86. }
  87. /**上移 */
  88. private up (): void {
  89. EventManager.sendEvent(GameEventName.SDK.SDK_Up);
  90. SDKManager.updateButton(-1);
  91. }
  92. /**下移 */
  93. private down (): void {
  94. EventManager.sendEvent(GameEventName.SDK.SDK_Down);
  95. SDKManager.updateButton(1);
  96. }
  97. _destroy () {
  98. Tool.log("销毁SDK管理器");
  99. SDKManager.buttons = null;
  100. SDK.onBackPressed();
  101. super._destroy();
  102. }
  103. }