|
| 1 | +import {AnimationFrame} from "./AnimationFrame"; |
| 2 | +import {ChromaInstance} from "./ChromaInstance"; |
| 3 | +import Color from "./Color"; |
| 4 | +import {DeviceRequestData} from "./DeviceRequestData"; |
| 5 | +import DeviceContainer from "./Devices"; |
| 6 | +import {IDevice, IDeviceData} from "./Devices/Base"; |
| 7 | +import ChromaLink from "./Devices/ChromaLink"; |
| 8 | +import Headset from "./Devices/Headset"; |
| 9 | +import Keyboard from "./Devices/Keyboard"; |
| 10 | +import Keypad from "./Devices/Keypad"; |
| 11 | +import Mouse from "./Devices/Mouse"; |
| 12 | +import Mousepad from "./Devices/Mousepad"; |
| 13 | +import Effect from "./Effect"; |
| 14 | + |
| 15 | +function sleep(time: number) { |
| 16 | + return new Promise((resolve) => setTimeout(resolve, time)); |
| 17 | +} |
| 18 | + |
| 19 | +export interface IPlayInstance { |
| 20 | + send(container: DeviceContainer): void; |
| 21 | + deleteEffect(effects: string[]): Promise<any>; |
| 22 | + sendDeviceUpdate(devices: IDeviceData[], store: boolean): Promise<any>; |
| 23 | +} |
| 24 | + |
| 25 | +export class Animation { |
| 26 | + public Frames: AnimationFrame[] = []; |
| 27 | + public isPlaying: boolean = false; |
| 28 | + public Instance: IPlayInstance = null; |
| 29 | + public currentFrame: number = 0; |
| 30 | + private isInit: boolean= false; |
| 31 | + |
| 32 | + public async play(instance: IPlayInstance) { |
| 33 | + if (!this.isInit) { |
| 34 | + this.isInit = true; |
| 35 | + await this.createFrames(); |
| 36 | + } |
| 37 | + this.Instance = instance; |
| 38 | + this.isPlaying = true; |
| 39 | + this.currentFrame = 0; |
| 40 | + await this.createEffects(instance); |
| 41 | + |
| 42 | + this.playLoop(instance); |
| 43 | + } |
| 44 | + |
| 45 | + public async playLoop(instance: IPlayInstance) { |
| 46 | + for (const i of this.Frames){ |
| 47 | + await instance.send(i); |
| 48 | + await sleep(i.delay); |
| 49 | + if (!this.isPlaying) { |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + if (this.isPlaying) { |
| 54 | + this.playLoop(instance); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public async stop() { |
| 59 | + this.isPlaying = false; |
| 60 | + const effectIds: string[] = []; |
| 61 | + for (const frame of this.Frames){ |
| 62 | + if (frame.Keyboard.effectId !== "") { |
| 63 | + effectIds.push(frame.Keyboard.effectId); |
| 64 | + } |
| 65 | + frame.Keyboard.effectId = ""; |
| 66 | + } |
| 67 | + |
| 68 | + await this.Instance.deleteEffect(effectIds); |
| 69 | + } |
| 70 | + |
| 71 | + public async createEffects(instance: IPlayInstance) { |
| 72 | + this.Instance = instance; |
| 73 | + const keyboardEffectData: any = []; |
| 74 | + const device = new DeviceRequestData(); |
| 75 | + device.device = "keyboard"; |
| 76 | + |
| 77 | + for (const frame of this.Frames){ |
| 78 | + keyboardEffectData.push(frame.Keyboard.effectData); |
| 79 | + } |
| 80 | + |
| 81 | + device.effectData = { |
| 82 | + effects: keyboardEffectData, |
| 83 | + }; |
| 84 | + |
| 85 | + const response = await instance.sendDeviceUpdate([device], true); |
| 86 | + const keyboardids = response[0]; |
| 87 | + |
| 88 | + for (let i = 0; i < keyboardids.length; i++) { |
| 89 | + this.Frames[i].Keyboard.effectId = keyboardids[i] !== null ? keyboardids[i].id : ""; |
| 90 | + } |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + public async createFrames() { |
| 95 | + for (let i = 0; i < 10; i++) { |
| 96 | + const frame = new AnimationFrame(); |
| 97 | + frame.Keyboard.setAll(new Color("ff0000")); |
| 98 | + this.Frames.push(frame); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments