SDKManager.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, MOTION, 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. SDK.on(GameManager.ActionEvent, this.action, this);
  32. EventManager.onEvent(GameManager.ActionEvent, this, this.action);
  33. }
  34. /**添加按钮 */
  35. public static addButton (buttons: Array<_ISDKButton>): void {
  36. if (SDKManager.buttons.length > 0 && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex] != null) {
  37. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(1).visible = false;
  38. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(2).asTextField.color =
  39. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].color[0];
  40. }
  41. SDKManager.buttons.push(buttons);
  42. SDKManager.buttonIndex = 0;
  43. SDKManager.updateButton(0);
  44. }
  45. /**删除按钮 */
  46. public static removeButton (count: number = 1): void {
  47. SDKManager.buttons.splice(SDKManager.buttons.length-count, count);
  48. if (SDKManager.buttons.length > 0 && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex] != null) {
  49. SDKManager.updateButton(0);
  50. }
  51. }
  52. /**更新按钮 */
  53. public static updateButton (value: number): void {
  54. if (SDKManager.buttons.length > 0 && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex] != null) {
  55. if (value != 0) {
  56. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(1).visible = false;
  57. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(2).asTextField.color =
  58. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].color[0];
  59. SDKManager.buttonIndex += value;
  60. if (SDKManager.buttonIndex < 0) {
  61. SDKManager.buttonIndex = SDKManager.buttons[SDKManager.buttons.length-1].length-1;
  62. }
  63. else if (SDKManager.buttonIndex >= SDKManager.buttons[SDKManager.buttons.length-1].length) {
  64. SDKManager.buttonIndex = 0;
  65. }
  66. }
  67. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(1).visible = true;
  68. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(2).asTextField.color =
  69. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].color[1];
  70. }
  71. }
  72. /**确认 */
  73. private confirm (): void {
  74. if (SDKManager.buttons.length > 0 && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex] != null) {
  75. SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].callback && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].callback();
  76. }
  77. EventManager.sendEvent(GameEventName.SDK.SDK_Confirm);
  78. }
  79. /**取消 */
  80. private cancel (): void {
  81. EventManager.sendEvent(GameEventName.SDK.SDK_Cancel);
  82. }
  83. /**上移 */
  84. private up (): void {
  85. EventManager.sendEvent(GameEventName.SDK.SDK_Up);
  86. SDKManager.updateButton(-1);
  87. }
  88. /**下移 */
  89. private down (): void {
  90. EventManager.sendEvent(GameEventName.SDK.SDK_Down);
  91. SDKManager.updateButton(1);
  92. }
  93. /**动作 */
  94. private action (): void {
  95. EventManager.sendEvent(GameEventName.SDK.SDK_Action);
  96. }
  97. _destroy () {
  98. Tool.log("销毁SDK管理器");
  99. SDKManager.buttons = null;
  100. SDK.onBackPressed();
  101. super._destroy();
  102. }
  103. }