import BasicManager from "../../Engine/Basic/BasicManager"; import EventManager from "../../Engine/Event/EventManager"; import Tool from "../../Engine/Tool/Tool"; import GameEventName from "../Event/GameEventName"; import GameManager from "../Game/GameManager"; import { CMD, MOTION, SDK } from "./sdk"; export interface _ISDKButton { button: fgui.GComponent, color: Array, callback: Function; } /** * SDK管理器 */ export default class SDKManager extends BasicManager { /**按钮列表,栈结构 */ private static buttons: Array> = []; /**按钮索引 */ private static buttonIndex: number = 0; constructor () { super(); Tool.log("生成SDK管理器"); SDK.on(CMD.ENTER_KEY.toString(), this.confirm, this); SDK.on(CMD.CANCLE_KEY.toString(), this.cancel, this); SDK.on(CMD.BACK_LEFT.toString(), this.up, this); SDK.on(CMD.BACK_RIGHT.toString(), this.down, this); EventManager.onEvent(CMD.ENTER_KEY.toString(), this, this.confirm); EventManager.onEvent(CMD.CANCLE_KEY.toString(), this, this.cancel); EventManager.onEvent(CMD.BACK_LEFT.toString(), this, this.up); EventManager.onEvent(CMD.BACK_RIGHT.toString(), this, this.down); SDK.on(GameManager.ActionEvent, this.action, this); EventManager.onEvent(GameManager.ActionEvent, this, this.action); } /**添加按钮 */ public static addButton (buttons: Array<_ISDKButton>): void { if (SDKManager.buttons.length > 0 && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex] != null) { SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(1).visible = false; SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(2).asTextField.color = SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].color[0]; } SDKManager.buttons.push(buttons); SDKManager.buttonIndex = 0; SDKManager.updateButton(0); } /**删除按钮 */ public static removeButton (count: number = 1): void { SDKManager.buttons.splice(SDKManager.buttons.length-count, count); if (SDKManager.buttons.length > 0 && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex] != null) { SDKManager.updateButton(0); } } /**更新按钮 */ public static updateButton (value: number): void { if (SDKManager.buttons.length > 0 && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex] != null) { if (value != 0) { SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(1).visible = false; SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(2).asTextField.color = SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].color[0]; SDKManager.buttonIndex += value; if (SDKManager.buttonIndex < 0) { SDKManager.buttonIndex = SDKManager.buttons[SDKManager.buttons.length-1].length-1; } else if (SDKManager.buttonIndex >= SDKManager.buttons[SDKManager.buttons.length-1].length) { SDKManager.buttonIndex = 0; } } SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(1).visible = true; SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].button.getChildAt(2).asTextField.color = SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].color[1]; } } /**确认 */ private confirm (): void { if (SDKManager.buttons.length > 0 && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex] != null) { SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].callback && SDKManager.buttons[SDKManager.buttons.length-1][SDKManager.buttonIndex].callback(); } EventManager.sendEvent(GameEventName.SDK.SDK_Confirm); } /**取消 */ private cancel (): void { EventManager.sendEvent(GameEventName.SDK.SDK_Cancel); } /**上移 */ private up (): void { EventManager.sendEvent(GameEventName.SDK.SDK_Up); SDKManager.updateButton(-1); } /**下移 */ private down (): void { EventManager.sendEvent(GameEventName.SDK.SDK_Down); SDKManager.updateButton(1); } /**动作 */ private action (): void { EventManager.sendEvent(GameEventName.SDK.SDK_Action); } _destroy () { Tool.log("销毁SDK管理器"); SDKManager.buttons = null; SDK.onBackPressed(); super._destroy(); } }