-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPlayer.ts
332 lines (261 loc) · 8.84 KB
/
Player.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import { Application, ApplicationOptions } from 'pixi.js';
import { MainContainer } from './MainContainer';
import { AnimationContainer } from './AnimationContainer';
import { SS6Project } from 'ss6player-pixi';
import { SspkgLoader } from './SspkgLoader';
import { SsfbDataUtil } from './SsfbDataUtil';
const PREVIEW_POSITION_MARGIN: number = 30;
export class Player {
public projectData: SS6Project;
public onComplete: () => void;
private animePackMap: { [key: string]: any } = null;
public infinityFlag: boolean = true;
public getAnimePackMap() {
return this.animePackMap;
}
public pixiApplication: Application = null;
public canvasWidth: number = null;
public canvasHeight: number = null;
public mainContainer: MainContainer = null;
public textureContainer: AnimationContainer = null;
private currentAnimePack = null;
public getCurrentAnimePack() {
return this.currentAnimePack;
}
private currentAnimation = null;
public getCurrentAnimation() {
return this.currentAnimation;
}
public onUserDataCallback: (data: any) => void = null;
public playEndCallback: (player: AnimationContainer) => void = null;
public onUpdateCallback: (player: AnimationContainer) => void = null;
public onPlayStateChangeCallback: (isPlaying: boolean, isPausing: boolean) => void = null;
private frameDataMap = null;
public getFrameDataMap() {
return this.frameDataMap;
}
public constructor() {
}
public async init(canvasWrapperElement: any, options?: Partial<ApplicationOptions>) {
this.canvasWidth = canvasWrapperElement.clientWidth;
this.canvasHeight = canvasWrapperElement.clientHeight;
const pixiApplication = new Application();
const pixiOptions: Partial<ApplicationOptions> =
(options === undefined || options === null) ?
{
// default options
preference: 'webgpu',
width: this.canvasWidth,
height: this.canvasHeight,
backgroundAlpha: 0
} : options;
await pixiApplication.init(pixiOptions);
const canvasElement = pixiApplication.canvas;
canvasWrapperElement.appendChild(canvasElement);
const mainContainer = new MainContainer();
pixiApplication.stage.addChild(mainContainer);
this.mainContainer = mainContainer;
this.pixiApplication = pixiApplication;
}
public getTextureContainer() {
// console.log('SpriteStudioWebPlayer.createPlayer');
return this.textureContainer;
}
/**
* Download ssfb file and dependencies image files, and load.
* @param {string} url
*/
public loadSsfb(url: string) {
const self = this;
let ss6Project = new SS6Project(url, () => {
self.setupForLoadComplete(ss6Project);
});
}
/**
* Download sspkg file, decompress sspkg and load.
* @param {string} url
*/
public loadSspkg(url: string) {
const self = this;
let sspkgLoader = new SspkgLoader();
sspkgLoader.load(url, (ssfbFileName, ssfbFileData, imageBinaryMap, error) => {
if (error !== null) {
return;
}
let ss6Project = new SS6Project(ssfbFileName, ssfbFileData, imageBinaryMap, () => {
self.setupForLoadComplete(ss6Project);
});
});
}
private setupForLoadComplete(ss6Project: SS6Project) {
this.projectData = ss6Project;
this.animePackMap = SsfbDataUtil.createAnimePackMap(this.projectData);
// console.log('setupForLoadComplete', this.animePackMap);
if (this.onComplete !== null) {
this.onComplete();
}
}
/**
* アニメーションを再生する
* @param animePackName アニメパック名
* @param animeName アニメーション名
*/
public loadAnimation(animePackName: string, animeName: string) {
let isSetupTextureContainer = false;
if (this.textureContainer == null) {
isSetupTextureContainer = true;
this.textureContainer = new AnimationContainer(this);
}
// animePackMap から animation の情報を取得
const animePackData = this.animePackMap[animePackName];
const animePack = animePackData.animePack;
const animationMap = animePackData.animationMap;
const animation = animationMap[animeName];
// currentAnimePack, currentAnimation を記録
this.currentAnimation = animation;
this.currentAnimePack = animePack;
// textureContainer にて Animation を再生
this.textureContainer.Setup(animePackName, animeName);
// ラベルデータの取得
const labelDataLength = animation.labelDataLength();
// console.log('labelDataLength', labelDataLength);
for (let i = 0; i < labelDataLength; i++) {
const labelData = animation.labelData(i);
}
if (isSetupTextureContainer) {
this.mainContainer.addChild(this.textureContainer);
}
// 現状のアニメーションからポジション設定を取得し、 setPosition を行う
// デフォルトのポジションを設定
this.setupDefaultPosition();
// 現状のアニメーションからサイズ設定を取得し、 setDefaultScaleRatio を行う
// スケール値自動調整
this.setupDefaultScaleRatio();
// FrameDataの情報を取得
this.frameDataMap = this.getCurrentAnimationFrameDataMap();
}
public play() {
this.textureContainer.Play();
}
public pause() {
this.textureContainer.Pause();
}
public stop() {
this.textureContainer.Stop();
}
public resume() {
this.textureContainer.Resume();
}
public movePosition(movementX: number, movementY: number) {
this.mainContainer.movePosition(movementX, movementY);
}
public zoom(zoomPercent: number) {
this.mainContainer.zoom(zoomPercent);
}
public zoomIn() {
this.mainContainer.zoomIn();
}
public zoomOut() {
this.mainContainer.zoomOut();
}
public switchGridDisplay() {
this.mainContainer.switchGridDisplay();
}
public setFrame(frameNumber) {
this.textureContainer.SetFrame(frameNumber);
}
public nextFrame() {
// console.log('nextFrame');
this.textureContainer.NextFrame();
}
public prevFrame() {
this.textureContainer.PrevFrame();
}
public get startFrame(): number {
return this.currentAnimation.startFrame;
}
public get endFrame(): number {
return this.currentAnimation.endFrames();
}
public get totalFrame(): number {
return this.currentAnimation.totalFrames();
}
public get fps(): number {
return this.currentAnimation.fps();
}
public get frameNo(): number {
return this.textureContainer.frameNo;
}
public get isPlaying(): boolean {
return this.textureContainer.isPlaying;
}
public get isPausing(): boolean {
return this.textureContainer.isPausing;
}
public setAnimationSpeed(value: number) {
this.textureContainer.SetAnimationSpeed(value);
}
public switchLoop(isInfinity: boolean) {
this.textureContainer.loop = (isInfinity) ? -1 : 1; // Changing loop status at being playing an animation.
this.infinityFlag = isInfinity;
}
public setAnimationSection(_startframe: number = -1, _endframe: number = -1, _loops: number = -1) {
this.textureContainer.SetAnimationSection(_startframe, _endframe, _loops);
}
/**
* デフォルトのポジションを設定する
*/
public setupDefaultPosition() {
const currentAnimation = this.currentAnimation;
// ポジション設定
const canvasPvotX = currentAnimation.canvasPvotX();
const canvasPvotY = currentAnimation.canvasPvotY();
let positionX;
switch (canvasPvotX) {
case 0.5:
positionX = PREVIEW_POSITION_MARGIN;
break;
case 0:
positionX = this.canvasWidth * 0.5;
break;
case -0.5:
positionX = this.canvasWidth - PREVIEW_POSITION_MARGIN;
break;
}
let positionY;
switch (canvasPvotY) {
case 0.5:
positionY = PREVIEW_POSITION_MARGIN;
break;
case 0:
positionY = this.canvasHeight * 0.5;
break;
case -0.5:
positionY = this.canvasHeight - PREVIEW_POSITION_MARGIN;
}
this.mainContainer.setPosition(positionX, positionY);
}
/**
* デフォルトのスケール率を設定
*/
public setupDefaultScaleRatio() {
const currentAnimation = this.currentAnimation;
const playerHeight = currentAnimation.canvasSizeH();
const playerWidth = currentAnimation.canvasSizeW();
const widthRatio = this.canvasWidth / playerWidth;
const heightRatio = this.canvasHeight / playerHeight;
let scaleRatio = null;
if (widthRatio < heightRatio) {
scaleRatio = widthRatio;
} else if (heightRatio < widthRatio) {
scaleRatio = heightRatio;
}
// scaleRatio *= 0.5;
// this.spriteStudioWebPlayer.defaultScaleRatio = scaleRatio;
this.mainContainer.setDefaultScaleRatio(scaleRatio);
this.mainContainer.zoom(100);
}
public getCurrentAnimationFrameDataMap() {
return this.textureContainer.getCurrentAnimationFrameDataMap();
}
}