forked from triacontane/RPGMakerMV
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApngPicture.js
619 lines (571 loc) · 19.7 KB
/
ApngPicture.js
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*=============================================================================
ApngPicture.js
----------------------------------------------------------------------------
(C)2019 Triacontane
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
----------------------------------------------------------------------------
Version
1.3.1 2019/12/31 画像を登録せずゲーム開始するとローディングが完了しない問題を修正
1.3.0 2019/12/31 APNGのキャッシュ機能を追加
1.2.1 2019/12/31 シーン追加画像でgifが表示されない問題を修正
1.2.0 2019/12/31 APNGのみツクール本体の暗号化機能に対応
1.1.0 2019/12/29 シーン追加画像の表示優先度を設定できる機能を追加
1.0.0 2019/12/27 初版
----------------------------------------------------------------------------
[Blog] : https://triacontane.blogspot.jp/
[Twitter]: https://twitter.com/triacontane/
[GitHub] : https://github.com/triacontane/
=============================================================================*/
/*:
* @plugindesc ApngSupportPlugin
* @author triacontane
*
* @param PictureList
* @desc List of picture images to be handled as APNG.
* @default []
* @require 1
* @dir img/pictures/
* @type file[]
*
* @param EnemyList
* @desc List of enemy images to be handled as APNG.
* @default []
* @require 1
* @dir img/enemies/
* @type file[]
*
* @param SceneApngList
* @desc This is a list of APNG to be displayed for each scene.
* @default []
* @type struct<SceneApngRecord>[]
*
* @help ApngPicture.js
* APNG or GIF is treated as a picture and animated on the screen.
* From the parameters, select the file in which the APNG picture file is registered.
* It will be animated if it is displayed in "Display Picture".
*
* The following libraries are required for use.
* https://github.com/sbfkcel/pixi-apngAndGif
*
* Download the target file and import it from the plugin management screen.
* https://github.com/sbfkcel/pixi-apngAndGif/blob/master/dist/PixiApngAndGif.js
*
* In addition, the color tone change of the picture is not reflected.
* Also, extensions by other plug-ins may not work.
*
* In addition, APNG registered from parameters can be additionally displayed for each scene.
* Display can be controlled by switch.
*
* There is also a function to display APNG on the enemy character image, but this function
* The image is incomplete because no flash is performed.
*
* This plugin is released under the MIT License.
*/
/*:ja
* @plugindesc APNGピクチャプラグイン
* @author トリアコンタン
*
* @param PictureList
* @text APNGのピクチャリスト
* @desc APNGとして扱うピクチャ画像のリストです。GIFを指定したい場合は拡張子付きで直接入力してください。
* @default []
* @type struct<PictureApngRecord>[]
*
* @param EnemyList
* @text APNGの敵キャラリスト
* @desc APNGとして扱う敵キャラ画像のリストです。GIFを指定したい場合は拡張子付きで直接入力してください。この機能は不完全です。
* @default []
* @require 1
* @dir img/enemies/
* @type struct<EnemyApngRecord>[]
*
* @param SceneApngList
* @text シーンAPNGのリスト
* @desc シーンごとに表示するAPNGのリストです。GIFを指定したい場合は拡張子付きで直接入力してください。
* @default []
* @type struct<SceneApngRecord>[]
*
* @help ApngPicture.js
* APNG、もしくはGIFアニメをピクチャとして画面上にアニメ表示します。
* パラメータからAPNGのピクチャとして登録したファイルを
* 「ピクチャの表示」で表示すればアニメーションされます。
*
* 使用には以下のライブラリが必要です。
* https://github.com/sbfkcel/pixi-apngAndGif
*
* 対象ファイルをダウンロードしてプラグイン管理画面から取り込んでください。
* https://github.com/sbfkcel/pixi-apngAndGif/blob/master/dist/PixiApngAndGif.js
*
* なお、ピクチャの色調変更は反映されません。
* また、他のプラグインによる拡張が機能しない場合があります。
*
* さらに、各シーンでパラメータから登録したAPNGを追加表示できます。
* スイッチによる表示制御が可能です。
*
* 敵キャラ画像にAPNGを表示する機能もありますが、この機能は
* 画像のフラッシュが一切行われないため不完全です。
* また、画像サイズの大きいAPNGを読み込むと、表示が遅くなる場合があります。
* 表示が遅い場合はGIFアニメもお試しください。
*
* GIFを使用したい場合、拡張子がgifのファイルはツクールMVで認識されないので
* パラメータに拡張子付きのファイル名を直接入力してください。
* また、ピクチャを表示するときはスクリプトから表示するか
* 同名のダミーpngファイルを使って指定してください。
* また、GIFはツクールMVの暗号化機能の対象外となります。
*
* このプラグインにはプラグインコマンドはありません。
*
* 利用規約:
* 作者に無断で改変、再配布が可能で、利用形態(商用、18禁利用等)
* についても制限はありません。
* このプラグインはもうあなたのものです。
*/
/*~struct~SceneApngRecord:
*
* @param SceneName
* @text 対象シーン
* @desc 画像を追加するシーンです。
* @type select
* @default Scene_Title
* @option タイトル
* @value Scene_Title
* @option ゲームオーバー
* @value Scene_Gameover
* @option バトル
* @value Scene_Battle
* @option メインメニュー
* @value Scene_Menu
* @option アイテム
* @value Scene_Item
* @option スキル
* @value Scene_Skill
* @option 装備
* @value Scene_Equip
* @option ステータス
* @value Scene_Status
* @option オプション
* @value Scene_Options
* @option セーブ
* @value Scene_Save
* @option ロード
* @value Scene_Load
* @option ゲーム終了
* @value Scene_End
* @option ショップ
* @value Scene_Shop
* @option 名前入力
* @value Scene_Name
* @option デバッグ
* @value Scene_Debug
* @option サウンドテスト
* @value Scene_SoundTest
* @option 用語辞典
* @value Scene_Glossary
*
* @param FileName
* @text ファイル名
* @desc 追加するAPNGのファイル名です。
* @default
* @require 1
* @dir img/system/
* @type file
*
* @param CachePolicy
* @text キャッシュ方針
* @desc 画像のキャッシュ方針です。大量にキャッシュするとメモリ使用量に影響が出る場合があります。
* @default 0
* @type select
* @option キャッシュしない
* @value 0
* @option 初回表示時にキャッシュ
* @value 1
* @option ゲーム起動時にキャッシュ
* @value 2
*
* @param X
* @text X座標
* @desc 追加するAPNGのX座標です。
* @default 0
* @type number
*
* @param Y
* @text Y座標
* @desc 追加するAPNGのY座標です。
* @default 0
* @type number
*
* @param Origin
* @text 原点
* @desc 追加するAPNGの原点です。
* @default 0
* @type select
* @option 左上
* @value 0
* @option 中央
* @value 1
*
* @param Priority
* @text 優先度
* @desc 画像の表示優先度です。最背面は画面上に表示されないことが多いので通常の使用では推奨しません。
* @default 0
* @type select
* @option 最前面
* @value 0
* @option ウィンドウの下
* @value 1
* @option 最背面
* @value 2
*
* @param Switch
* @text 出現条件スイッチ
* @desc 指定したスイッチがONのときのみ表示されます。指定しない場合、常に表示されます。
* @default 0
* @type switch
*/
/*~struct~PictureApngRecord:
*
* @param FileName
* @text ファイル名
* @desc 追加するAPNGのファイル名です。
* @default
* @require 1
* @dir img/pictures/
* @type file
*
* @param CachePolicy
* @text キャッシュ方針
* @desc 画像のキャッシュ方針です。大量にキャッシュするとメモリ使用量に影響が出る場合があります。
* @default 0
* @type select
* @option キャッシュしない
* @value 0
* @option 初回表示時にキャッシュ
* @value 1
* @option ゲーム起動時にキャッシュ
* @value 2
*/
/*~struct~EnemyApngRecord:
*
* @param FileName
* @text ファイル名
* @desc 追加するAPNGのファイル名です。
* @default
* @require 1
* @dir img/enemies/
* @type file
*
* @param CachePolicy
* @text キャッシュ方針
* @desc 画像のキャッシュ方針です。大量にキャッシュするとメモリ使用量に影響が出る場合があります。
* @default 0
* @type select
* @option キャッシュしない
* @value 0
* @option 初回表示時にキャッシュ
* @value 1
* @option ゲーム起動時にキャッシュ
* @value 2
*/
(function() {
'use strict';
var getClassName = function(object) {
return object.constructor.toString().replace(/function\s+(.*)\s*\([\s\S]*/m, '$1');
};
/**
* Create plugin parameter. param[paramName] ex. param.commandPrefix
* @param script currentScript
* @returns {Object} Created parameter
*/
var createPluginParameter = function(script) {
var pluginName = script.src.replace(/^.*\/(.*).js$/, function() {
return arguments[1];
});
var paramReplacer = function(key, value) {
if (value === 'null') {
return value;
}
if (value[0] === '"' && value[value.length - 1] === '"') {
return value;
}
try {
return JSON.parse(value);
} catch (e) {
return value;
}
};
var parameter = JSON.parse(JSON.stringify(PluginManager.parameters(pluginName), paramReplacer));
PluginManager.setParameters(pluginName, parameter);
return parameter;
};
var param = createPluginParameter(document.currentScript);
/**
* ApngLoader
* APNGもしくはGIF画像を読み込んで保持します。
*/
class ApngLoader {
constructor(folder, paramList) {
this._folder = folder;
this._fileHash = {};
this._cachePolicy = {};
this._paramList = paramList;
if (this._paramList && this._paramList.length > 0) {
this.addAllImage();
}
this._spriteCache = {};
}
addAllImage() {
var option = this.getLoadOption();
this._paramList.forEach(function(item) {
this.addImage(item, option);
}, this);
PIXI.loader.onComplete.add(this.cacheStartup.bind(this));
ApngLoader.startLoading();
}
addImage(item, option) {
var name = item.FileName;
var ext = Decrypter.hasEncryptedImages ? 'rpgmvp' : 'png';
name = name.replace(/\.gif$/gi, function() {
ext = 'gif';
return '';
});
var path = name.match(/http:/) ? name : `img/${this._folder}/${name}.${ext}`;
if (!this._fileHash.hasOwnProperty(name)) {
this._fileHash[name] = ApngLoader.convertDecryptExt(path);
this._cachePolicy[name] = item.CachePolicy;
PIXI.loader.add(path, option);
}
}
getLoadOption() {
return {
loadType : PIXI.loaders.Resource.LOAD_TYPE.XHR,
xhrType : PIXI.loaders.Resource.XHR_RESPONSE_TYPE.BUFFER,
crossOrigin: ''
}
}
createSprite(name) {
if (!this.isApng(name)) {
return null;
}
if (this._isNeedCache(name)) {
if (this._spriteCache[name]) {
return this._spriteCache[name];
}
var sprite = this._createPixiApngAndGif(name);
this._spriteCache[name] = sprite;
return sprite;
} else {
return this._createPixiApngAndGif(name);
}
}
_createPixiApngAndGif(name) {
return new PixiApngAndGif(this._fileHash[name], ApngLoader._resource).sprite;
}
_isNeedCache(name) {
return this._cachePolicy[name] > 0;
}
isApng(name) {
return !!this._fileHash[name];
}
cacheStartup() {
Object.keys(this._cachePolicy).forEach(function(name) {
if (this._cachePolicy[name] === 2) {
this.createSprite(name)
}
}, this);
}
static startLoading() {
this._loading = true;
};
static onLoadResource(progress, resource) {
this._resource = resource;
Object.keys(this._resource).forEach(function(key) {
if (this._resource[key].extension === 'rpgmvp') {
ApngLoader.decryptResource(key);
}
}, this);
}
static decryptResource(key) {
var resource = this._resource[key];
resource.data = Decrypter.decryptArrayBuffer(resource.data);
var newKey = ApngLoader.convertDecryptExt(key);
resource.name = newKey;
resource.url = newKey;
resource.extension = 'png';
this._resource[newKey] = resource;
delete this._resource[key];
};
static isReady() {
return !!this._resource || !this._loading;
}
static convertDecryptExt(key) {
return key.replace(/\.rpgmvp$/, '.png');
}
}
ApngLoader._resource = null;
var _Scene_Boot_isReady = Scene_Boot.prototype.isReady;
Scene_Boot.prototype.isReady = function() {
var result = _Scene_Boot_isReady.apply(this, arguments);
if (result) {
SceneManager.setupApngLoaderIfNeed();
}
return result && ApngLoader.isReady();
};
var _Scene_Base_create = Scene_Base.prototype.create;
Scene_Base.prototype.create = function() {
_Scene_Base_create.apply(this, arguments);
this.createSceneApng();
};
Scene_Base.prototype.createSceneApng = function() {
this._apngList = this.findSceneApngList().map(function(item) {
return new SpriteSceneApng(item);
}, this);
};
var _Scene_Base_start = Scene_Base.prototype.start;
Scene_Base.prototype.start = function() {
_Scene_Base_start.apply(this, arguments);
this.setApngPriority();
};
Scene_Base.prototype.setApngPriority = function() {
var windowLayerIndex = this._windowLayer ? this.getChildIndex(this._windowLayer) : 0;
this._apngList.forEach(function(sprite) {
switch (sprite.getPriority()) {
case 0:
this.addChild(sprite);
break;
case 1:
this.addChildAt(sprite, windowLayerIndex);
windowLayerIndex++;
break;
default:
this.addChildAt(sprite, 0);
}
}, this);
};
Scene_Base.prototype.findSceneApngList = function() {
var currentSceneName = getClassName(this);
return (param.SceneApngList || []).filter(function(data) {
return data.SceneName === currentSceneName;
}, this);
};
/**
* SceneManager
* APNGのローダを管理します。
*/
SceneManager.setupApngLoaderIfNeed = function() {
if (this._apngLoaderPicture) {
return;
}
PIXI.loader.onComplete.add(ApngLoader.onLoadResource.bind(ApngLoader));
this._apngLoaderPicture = new ApngLoader('pictures', param.PictureList);
this._apngLoaderEnemy = new ApngLoader('enemies', param.EnemyList);
this._apngLoaderSystem = new ApngLoader('system', param.SceneApngList);
PIXI.loader.load();
};
SceneManager.tryLoadApngPicture = function(name) {
return this._apngLoaderPicture.createSprite(name);
};
SceneManager.tryLoadApngEnemy = function(name) {
return this._apngLoaderEnemy.createSprite(name);
};
SceneManager.tryLoadApngSystem = function(name) {
return this._apngLoaderSystem.createSprite(name);
};
/**
* Sprite
* APNGの読み込み処理を追加します。
*/
Sprite.prototype.addApngChild = function(name) {
if (this._apngSprite) {
this.removeChild(this._apngSprite);
}
this._apngSprite = this.loadApngSprite(name);
if (this._apngSprite) {
this.addChild(this._apngSprite);
this.bitmap = ImageManager.loadPicture('');
this.updateApngAnchor();
this.updateApngBlendMode();
}
};
Sprite.prototype.loadApngSprite = function() {
return null;
};
Sprite.prototype.updateApngAnchor = function() {
if (this._apngSprite) {
this._apngSprite.anchor.x = this.anchor.x;
this._apngSprite.anchor.y = this.anchor.y;
}
};
Sprite.prototype.updateApngBlendMode = function() {
if (this._apngSprite) {
this._apngSprite.blendMode = this.blendMode;
}
};
/**
* Sprite_Picture
* APNGとして登録されているピクチャの読み込みを追加します。
*/
var _Sprite_Picture_loadBitmap = Sprite_Picture.prototype.loadBitmap;
Sprite_Picture.prototype.loadBitmap = function() {
_Sprite_Picture_loadBitmap.apply(this, arguments);
this.addApngChild(this._pictureName);
};
Sprite_Picture.prototype.loadApngSprite = function(name) {
return SceneManager.tryLoadApngPicture(name);
};
var _Sprite_Picture_updateOrigin = Sprite_Picture.prototype.updateOrigin;
Sprite_Picture.prototype.updateOrigin = function() {
_Sprite_Picture_updateOrigin.apply(this, arguments);
this.updateApngAnchor();
};
var _Sprite_Picture_updateOther = Sprite_Picture.prototype.updateOther;
Sprite_Picture.prototype.updateOther = function() {
_Sprite_Picture_updateOther.apply(this, arguments);
this.updateApngBlendMode();
};
/**
* Sprite_Enemy
* APNGとして登録されている敵キャラの読み込みを追加します。
*/
var _Sprite_Enemy_loadBitmap = Sprite_Enemy.prototype.loadBitmap;
Sprite_Enemy.prototype.loadBitmap = function(name, hue) {
_Sprite_Enemy_loadBitmap.apply(this, arguments);
this.addApngChild(name);
};
Sprite_Enemy.prototype.loadApngSprite = function(name) {
return SceneManager.tryLoadApngEnemy(name);
};
/**
* SpriteSceneApng
* シーンに追加表示するAPNGスプライトです。
*/
class SpriteSceneApng extends Sprite {
constructor(item) {
super();
this.setup(item)
}
setup(item) {
this.addApngChild(item.FileName);
this.x = item.X;
this.y = item.Y;
if (item.Origin === 1) {
this.anchor.x = 0.5;
this.anchor.y = 0.5;
}
this._switch = item.Switch;
this._priority = item.Priority;
}
loadApngSprite(name) {
return SceneManager.tryLoadApngSystem(name.replace(/\..*/, ''));
}
update() {
this.visible = this.isValid();
}
isValid() {
return !this._switch || $gameSwitches.value(this._switch);
}
getPriority() {
return this._priority;
}
}
})();