-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
226 lines (197 loc) · 6.63 KB
/
index.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
import type { Application } from 'pixi.js';
import { isNumber, mergeDeep } from 'tianjie';
import { Menus } from './menus.js';
import { Model } from './model.js';
import { Stage } from './stage.js';
import { StatusBar, SystemState } from './status-bar.js';
import { Tips } from './tips.js';
import { DEFAULT_OPTIONS } from '../config/index.js';
import { WindowSizeType } from '../constants/index.js';
import type {
ApplicationType,
DefaultOptions,
HitAreaFramesType,
LoadMethod,
ModelOptions,
Options,
PixiLive2dDisplayModule
} from '../types/index.js';
import { checkVersion, handleCommonStyle, printProjectInfo } from '../utils/index.js';
export class OhMyLive2D {
private stage: Stage;
private statusBar: StatusBar;
private tips: Tips;
private menus: Menus;
private application: Application;
private model?: Model; // 当前模型的实例
private modelIndex = 0; // 当前模型索引
private windowSizeType: WindowSizeType = WindowSizeType.pc; // 当前窗口大小
private mediaQuery = window.matchMedia('screen and (max-width: 768px)'); // 窗口大小的媒体查询
constructor(
private options: DefaultOptions,
private pixiLive2dDisplayModule: PixiLive2dDisplayModule,
// eslint-disable-next-line @typescript-eslint/naming-convention
private Application: ApplicationType,
// eslint-disable-next-line @typescript-eslint/naming-convention
private HitAreaFrames: HitAreaFramesType
) {
this.options.sayHello && this.sayHello();
this.stage = new Stage(this.options.parentElement, options); // 实例化舞台
this.statusBar = new StatusBar(this.options.parentElement); // 实例化状态条
this.tips = new Tips(this.stage.element, this.options.tips); // 提示框
this.menus = new Menus(this.stage.element); // 菜单
this.application = this.createApplication();
this.initialize();
void checkVersion();
}
// 初始化
initialize(): void {
this.verifyWindowSizeType();
if (this.windowSizeType !== WindowSizeType.pc) {
this.statusBar.popup('看板娘休息中', SystemState.info, 8000);
return;
}
this.loadModel();
this.registerEvents();
}
// 校验当前窗口大小
verifyWindowSizeType(): void {
if (this.mediaQuery.matches) {
this.windowSizeType = WindowSizeType.mobile;
} else {
this.windowSizeType = WindowSizeType.pc;
}
this.mediaQuery.addEventListener('change', (e) => {
if (e.matches) {
this.windowSizeType = WindowSizeType.mobile;
} else {
this.windowSizeType = WindowSizeType.pc;
}
});
}
/**
* 加载模型
*/
loadModel(showLoading = true): void {
showLoading && this.statusBar.showLoading();
this.model = new Model(this.pixiLive2dDisplayModule.Live2DModel, this.currentModelOption, this.application, this.HitAreaFrames);
this.model?.setScale(this.currentModelOption?.scale, this.currentModelOption?.scale);
this.model?.setPosition(...(this.currentModelOption?.position || []));
// 模型所有资源加载完毕
this.model?.onLoaded(({ width, height }) => {
this.setStageStyle({
width: this.currentModelOption.stageStyle?.width || width,
height: this.currentModelOption.stageStyle?.height || height,
backgroundColor: this.currentModelOption.stageStyle?.backgroundColor || 'rgba(0, 0, 0, 0)'
});
void this.stage.slideIn(this.options?.transitionTime);
this.statusBar.hideLoading();
});
// 加载失败
this.model?.onFail((e) => {
this.statusBar.loadingError(this.loadModel.bind(this));
console.error(e);
});
}
setStageStyle(style: Record<string, string | number>): void {
const newStyle = handleCommonStyle(style);
this.stage.setStyle(newStyle);
this.application.resize();
}
/**
* 获取当前的模型配置选项
*/
get currentModelOption(): ModelOptions {
return this.options.models[this.modelIndex];
}
/**
* 加载下一个模型
*/
async loadNextModel(): Promise<void> {
if (this.options.models.length <= 1) {
this.tips.notification('没找到其他模型哦...', 3000, 9);
return;
}
this.tips.clear();
this.statusBar.showLoading();
await this.stage.slideOut(this.options.transitionTime);
if (this.application.stage.children.length >= 1) {
this.application.stage.removeChildAt(0);
} // 从舞台移除上一个模型
if (isNumber(this.options.models?.length)) {
if (this.modelIndex < this.options.models.length - 1) {
this.modelIndex++;
} else {
this.modelIndex = 0;
}
this.loadModel(false);
}
}
sayHello(): void {
printProjectInfo();
}
registerEvents(): void {
// 点击菜单按钮
this.menus.onClickItem((name) => {
switch (name) {
case 'Rest':
void this.stage.slideOut(this.options.transitionTime);
this.statusBar.popup('休息中', SystemState.info, false, () => {
void this.stage.slideIn(this.options.transitionTime);
this.statusBar.popup('闪亮登场');
void this.tips.idlePlayer?.start();
});
this.tips.clear();
return;
// 切换模型
case 'SwitchModel':
void this.loadNextModel();
return;
case 'About':
window.open('https://oml2d.com');
return;
}
});
// copy 事件
window.addEventListener('copy', () => {
// console.log('copy!');
this.tips.copy();
});
// 出场入场动画执行结束之后的事件回调
this.stage.onSlideChangeEnd((status) => {
if (status) {
this.tips.welcome();
}
});
}
/**
* 创建pixi应用实例
* @returns
*/
private createApplication(): Application {
return new this.Application({
view: this.stage.canvasElement,
resolution: 2,
autoStart: true,
autoDensity: true,
backgroundAlpha: 0,
resizeTo: this.stage.element
});
}
}
export const setup = (loadMethod: LoadMethod): ((options: Options) => Promise<unknown>) => {
let oml2d;
const loadOml2d = async (options: Options): Promise<void> => {
const { parentElement } = options;
const finalOptions = mergeDeep(DEFAULT_OPTIONS, options);
finalOptions.parentElement = parentElement || document.body;
// if (!finalOptions.models?.length) {
// throw new Error('至少需要配置一个模型');
// }
const { PixiLive2dDisplay, PIXI, HitAreaFrames } = await loadMethod(finalOptions.importType, finalOptions.libraryUrls);
if (!oml2d) {
oml2d = new OhMyLive2D(finalOptions, PixiLive2dDisplay, PIXI.Application, HitAreaFrames);
}
};
return loadOml2d;
};