/
danoni_main.js
9640 lines (8356 loc) · 321 KB
/
danoni_main.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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
/**
* Dancing☆Onigiri (CW Edition)
*
* Source by tickle
* Created : 2018/10/08
* Revised : 2021/10/04
*
* https://github.com/cwtickle/danoniplus
*/
const g_version = `Ver 21.5.4`;
const g_revisedDate = `2021/10/04`;
const g_alphaVersion = ``;
// カスタム用バージョン (danoni_custom.js 等で指定可)
let g_localVersion = ``;
let g_localVersion2 = ``;
// ショートカット用文字列(↓の文字列を検索することで対象箇所へジャンプできます)
// タイトル:melon 設定・オプション:lime キーコンフィグ:orange 譜面読込:strawberry メイン:banana 結果:grape
// シーンジャンプ:Scene
/**
* ▽ ソースコーディング
* https://github.com/cwtickle/danoniplus/blob/develop/.github/CONTRIBUTING.md
*
* - 定数・変数名
* -- 定数 : `C_(カテゴリ)_(名前)`の形式。全て英大文字、数字、アンダースコアのみを使用。
* -- グローバル変数: 変数の頭に`g_`をつける。
* -- 関数の引数 : アンダースコア始まりのキャメル表記。
*
* ▽ 画面の構成
* [タイトル]-[設定・オプション]-[キーコンフィグ]-[譜面読込]-[メイン]-[リザルト]
* ⇒ 各画面に Init がついたものが画面の基本構成(ルート)を表す。
*
* ▽ スプライトの親子関係
* 基本的にdiv要素で管理。最下層を[divRoot]とし、createEmptySprite()でdiv子要素を作成。
* clearWindow()で[divRoot]以外の全てのスプライトを削除。
* 特定のスプライトに限り削除する場合は deleteChildspriteAll() 。
*/
const current = _ => {
if (document.currentScript) {
return document.currentScript.src;
}
const scripts = document.getElementsByTagName(`script`);
const targetScript = scripts[scripts.length - 1];
return targetScript.src;
};
const g_rootPath = current().match(/(^.*\/)/)[0];
const g_remoteFlg = g_rootPath.match(`^https://cwtickle.github.io/danoniplus/`) !== null;
window.onload = _ => {
g_loadObj.main = true;
g_currentPage = `initial`;
// ロード直後に定数・初期化ファイル、旧バージョン定義関数を読込
const randTime = new Date().getTime();
loadScript(`${g_rootPath}../js/lib/danoni_localbinary.js?${randTime}`, _ => {
loadScript(`${g_rootPath}../js/lib/danoni_constants.js?${randTime}`, _ => {
loadScript(`${g_rootPath}../js/lib/danoni_legacy_function.js?${randTime}`, _ => {
initialControl();
}, false);
});
}, false);
};
/*-----------------------------------------------------------*/
/* Scene : COMMON [water] */
/*-----------------------------------------------------------*/
// fps(デフォルトは60)
let g_fps = 60;
// 譜面データの&区切りを有効にするか
let g_enableAmpersandSplit = true;
// 譜面データをdecodeURIするか
let g_enableDecodeURI = false;
// プリロード済ファイル
const g_preloadFiles = {
all: [],
image: [],
font: [],
};
// 矢印サイズ
const C_ARW_WIDTH = 50;
// ON/OFFスイッチ
const C_FLG_ON = `ON`;
const C_FLG_OFF = `OFF`;
const C_FLG_ALL = `ALL`;
const C_DIS_NONE = `none`;
const C_DIS_INHERIT = `inherit`;
// 初期化フラグ(ボタンアニメーション制御)
let g_initialFlg = false;
let g_currentPage = ``;
// キーコンフィグ初期設定
let g_kcType = `Main`;
let g_colorType = `Default`;
let g_baseDisp = `Settings`;
// ライフ・ゲームオーバー・曲終了管理
let g_maxScore = 1000000;
let g_gameOverFlg = false;
let g_finishFlg = true;
/** 共通オブジェクト */
const g_loadObj = {};
const g_rootObj = {};
let g_headerObj = {};
let g_scoreObj = {};
let g_attrObj = {};
let g_btnAddFunc = {};
let g_btnDeleteFlg = {};
let g_cxtAddFunc = {};
let g_cxtDeleteFlg = {};
const g_detailObj = {
arrowCnt: [],
frzCnt: [],
maxDensity: [],
densityData: [],
startFrame: [],
playingFrame: [],
playingFrameWithBlank: [],
speedData: [],
boostData: [],
toolDif: [],
};
const g_workObj = {
stepX: [],
stepRtn: [],
stepHitRtn: [],
arrowRtn: [],
keyCtrl: [],
keyCtrlN: [],
keyHitFlg: [],
scrollDir: [],
dividePos: [],
};
// 歌詞制御
let g_wordSprite;
const g_wordObj = {
wordDir: 0,
wordDat: ``,
fadeInFlg0: false,
fadeInFlg1: false,
fadeOutFlg0: false,
fadeOutFlg1: false
};
// オーディオ設定・タイマー管理
let g_audio = new Audio();
let g_timeoutEvtId = 0;
let g_timeoutEvtTitleId = 0;
let g_timeoutEvtResultId = 0;
let g_inputKeyBuffer = {};
// 音楽ファイル エンコードフラグ
let g_musicEncodedFlg = false;
// 外部dosデータ
let g_externalDos = ``;
let g_musicdata = ``;
// ローカルストレージ設定 (作品別)
let g_localStorage;
let g_localStorageUrl;
// ローカルストレージ設定 (ドメイン・キー別)
let g_checkKeyStorage;
let g_localKeyStorage;
let g_canLoadDifInfoFlg = false;
/**
* 複数のdiv子要素を親要素へ接続
* @param {object} _baseObj
* @param {...any} rest
*/
const multiAppend = (_baseObj, ...rest) => {
_baseObj.append(...rest);
};
/**
* 複数の属性をまとめて設定
* @param {object} _baseObj
* @param {object} rest
*/
const setAttrs = (_baseObj, { ...rest } = {}) => {
Object.keys(rest).forEach(property => _baseObj.setAttribute(property, rest[property]));
};
/**
* 属性値を数値に変換して取得
* @param {object} _baseObj
* @param {string} _attrkey
*/
const getNumAttr = (_baseObj, _attrkey) => parseFloat(_baseObj.getAttribute(_attrkey));
/**
* 変数が存在するかどうかをチェック
* @param {string} _data
*/
const hasVal = _data => _data !== undefined && _data !== ``;
/**
* 部分一致検索(リストのいずれかに合致、大小文字問わず)
* @param {string} _str 検索文字
* @param {array} _list 検索リスト (英字は小文字にする必要あり)
* @param {string} prefix 前方一致条件 (前方一致時は ^)
* @param {string} suffix 後方一致条件 (後方一致時は $)
*/
const listMatching = (_str, _list, { prefix = ``, suffix = `` } = {}) =>
_list.findIndex(value => _str.toLowerCase().match(new RegExp(String.raw`${prefix}${value}${suffix}`, 'i'))) !== -1;
const fuzzyListMatching = (_str, _headerList, _footerList) =>
listMatching(_str, _headerList, { prefix: `^` }) || listMatching(_str, _footerList, { suffix: `$` });
/**
* 対象のカラーコードが明暗どちらかを判定 (true: 明色, false: 暗色)
* @param {string} _colorStr
* @returns
*/
const checkLightOrDark = _colorStr => {
const r = parseInt(_colorStr.substring(1, 3), 16);
const g = parseInt(_colorStr.substring(3, 5), 16);
const b = parseInt(_colorStr.substring(5, 7), 16);
return ((((r * 299) + (g * 587) + (b * 114)) / 1000) < 128);
};
/**
* 二次元配列のコピー
* @param {array2} _array2d
* @returns
*/
const copyArray2d = _array2d => JSON.parse(JSON.stringify(_array2d));
/**
* 配列データを合計
* @param {array} _array
* @returns
*/
const sumData = _array => _array.reduce((p, x) => p + x);
/**
* イベントハンドラ用オブジェクト
* 参考: http://webkatu.com/remove-eventlistener/
*
* - イベントリスナー作成時にリスナーキー(key)を発行する
* - 削除時は発行したリスナーキーを指定して削除する
*/
const g_handler = (_ => {
const events = {};
let key = 0;
return {
addListener: (_target, _type, _listener, _capture = false) => {
_target.addEventListener(_type, _listener, _capture);
events[key] = {
target: _target,
type: _type,
listener: _listener,
capture: _capture
};
return key++;
},
removeListener: key => {
if (key in events) {
const e = events[key];
e.target.removeEventListener(e.type, e.listener, e.capture);
}
}
}
})();
/**
* 特定キーコードを置換する処理
* @param {string} _setCode
*/
const transCode = _setCode => {
if ([`Control`, `Shift`, `Alt`].includes(_setCode.slice(0, -5))) {
return _setCode.replace(`Right`, `Left`);
}
return _setCode;
}
/**
* 特定キーをブロックする処理
* @param {string} _setCode
*/
const blockCode = _setCode => !C_BLOCK_KEYS.map(key => g_kCdN[key]).includes(_setCode);
/**
* キーを押したときの動作(汎用)
* @param {object} _evt
* @param {string} _displayName
* @param {function} _func
*/
const commonKeyDown = (_evt, _displayName, _func = _code => { }) => {
const setCode = transCode(_evt.code);
if (_evt.repeat) {
return blockCode(setCode);
}
g_inputKeyBuffer[setCode] = true;
// 対象ボタンを検索
const scLists = Object.keys(g_shortcutObj[_displayName]).filter(keys => {
const keyset = keys.split(`_`);
return (keyset.length > 1 ? keyIsDown(keyset[0]) && keyIsDown(keyset[1]) : keyIsDown(keyset[0]));
});
if (scLists.length > 0) {
// リンク先にジャンプする場合はonkeyUpイベントが動かないため、事前にキー状態をリセット
if (g_shortcutObj[_displayName][scLists[0]].reset) {
g_inputKeyBuffer[setCode] = false;
}
// 対象ボタン処理を実行
const targetId = document.getElementById(g_shortcutObj[_displayName][scLists[0]].id);
if (targetId !== null && targetId.style.display !== C_DIS_NONE && targetId.style.pointerEvents !== C_DIS_NONE) {
targetId.click();
}
return blockCode(setCode);
}
_func(setCode);
return blockCode(setCode);
};
/**
* キーを離したときの動作(汎用)
* @param {object} _evt
*/
const commonKeyUp = _evt => {
g_inputKeyBuffer[`MetaLeft`] = false;
g_inputKeyBuffer[transCode(_evt.code)] = false;
};
/**
* 外部リンクを新規タブで開く
* @param {string} _url
*/
const openLink = _url => {
if (_url.match(`^(http|https):/`)) {
window.open(_url, `_blank`, `noopener`);
}
}
/**
* 改行コード区切りの配列展開
* @param {string} _str
* @returns
*/
const splitLF = _str => _str.split(`\r`).join(`\n`).split(`\n`);
/**
* 文字列を想定された型に変換
* - _type は `float`(小数)、`number`(整数)、`boolean`(真偽値)、
* `switch`(ON/OFF), `calc`(数式), `string`(文字列)から選択
* - 型に合わない場合は _default を返却するが、_default自体の型チェック・変換は行わない
* @param {string} _checkStr
* @param {string} _default
* @param {string} _type
*/
function setVal(_checkStr, _default, _type) {
let convertStr = _checkStr;
// 値がundefined相当の場合は無条件でデフォルト値を返却
if (_checkStr === undefined || _checkStr === null || _checkStr === ``) {
return _default;
}
if (_type === C_TYP_FLOAT) {
// 数値型(小数可)の場合
const toFloat = parseFloat(_checkStr);
convertStr = (isNaN(toFloat) ? _default : toFloat);
} else if (_type === C_TYP_NUMBER) {
// 数値型(整数のみ)の場合
const toInt = parseInt(_checkStr);
convertStr = (isNaN(toInt) ? _default : toInt);
} else if (_type === C_TYP_BOOLEAN) {
// 真偽値の場合
const lowerCase = _checkStr.toString().toLowerCase();
convertStr = (lowerCase === `true` ? true : (lowerCase === `false` ? false : _default));
} else if (_type === C_TYP_SWITCH) {
// ON/OFFスイッチの場合
const toSwtich = _checkStr.toString().toUpperCase();
convertStr = [C_FLG_OFF, C_FLG_ON].includes(toSwtich) ? toSwtich : _default;
} else if (_type === C_TYP_CALC) {
try {
convertStr = new Function(`return ${_checkStr}`)();
} catch (err) {
convertStr = _default;
}
}
// 文字列型の場合 (最初でチェック済みのためそのまま値を返却)
return convertStr;
}
/**
* div要素のstyleを取得
* @param {string} _id
*/
function $id(_id) {
return document.getElementById(`${_id}`).style;
}
/**
* 先頭のみ大文字に変換(それ以降はそのまま)
* @param {string} _str
*/
function toCapitalize(_str) {
if (!_str || typeof _str !== `string`) return _str;
return `${_str.charAt(0).toUpperCase()}${_str.slice(1)}`;
}
/**
* 0以上の数字に変換
* @param {number} _num
* @param {number} _init 0未満の場合に設定する値
*/
function roundZero(_num, _init = 0) {
return _num < 0 ? _init : _num;
}
/**
* プリロードするファイルの設定
* @param {string} _as
* @param {string} _href
* @param {string} _type
* @param {string} _crossOrigin
*/
function preloadFile(_as, _href, _type = ``, _crossOrigin = `anonymous`) {
const preloadFlg = g_preloadFiles.all.find(v => v === _href);
if (preloadFlg === undefined) {
g_preloadFiles.all.push(_href);
if (g_preloadFiles[_as] === undefined) {
g_preloadFiles[_as] = [];
}
g_preloadFiles[_as].push(_href);
if (g_userAgent.indexOf(`firefox`) !== -1 && _as === `image`) {
// Firefoxの場合のみpreloadが効かないため、画像読込形式にする
g_loadObj[_href] = false;
const img = new Image();
img.src = _href;
img.onload = _ => g_loadObj[_href] = true;
} else {
// それ以外のブラウザの場合はrel=preloadを利用
const link = document.createElement(`link`);
link.rel = `preload`;
link.as = _as;
link.href = _href;
if (_type !== ``) {
link.type = _type;
}
if (!g_isFile) {
link.crossOrigin = _crossOrigin;
}
document.head.appendChild(link);
}
}
}
/**
* CSSファイルの読み込み(danoni_main.css以外)
* デフォルトは danoni_skin_default.css を読み込む
* @param {url} _href
* @param {function} _func
*/
function importCssFile(_href, _func) {
const baseUrl = _href.split(`?`)[0];
g_loadObj[baseUrl] = false;
const link = document.createElement(`link`);
link.rel = `stylesheet`;
link.href = _href;
link.onload = _ => {
g_loadObj[baseUrl] = true;
_func();
};
link.onerror = _ => {
makeWarningWindow(g_msgInfoObj.E_0041.split(`{0}`).join(baseUrl), `title`);
_func();
};
document.head.appendChild(link);
}
/**
* 画面共通のフォント設定
* @param {string} _priorityFont
*/
function getBasicFont(_priorityFont = ``) {
return [_priorityFont, g_headerObj.customFont, C_LBL_BASICFONT].filter(value => value !== ``).join(`,`);
}
/**
* フォントサイズに応じた横幅を取得
* @param {string} _str
* @param {number} _fontsize
* @param {string} _font
*/
function getStrWidth(_str, _fontsize, _font) {
const ctx = document.createElement(`canvas`).getContext(`2d`);
ctx.font = `${_fontsize}px ${_font}`;
return ctx.measureText(unEscapeHtml(_str)).width;
}
/**
* 指定した横幅に合ったフォントサイズを取得
* @param {string} _str
* @param {number} _maxWidth
* @param {string} _font
* @param {number} _maxFontsize
*/
function getFontSize(_str, _maxWidth, _font = getBasicFont(), _maxFontsize = 64) {
const minSiz = 5;
for (let siz = _maxFontsize; siz >= minSiz; siz--) {
if (_maxWidth >= getStrWidth(_str, siz, _font)) {
return siz;
}
}
return minSiz;
}
/**
* クリップボードコピー関数
* 入力値をクリップボードへコピーする
* @param {string} _textVal 入力値
*/
function copyTextToClipboard(_textVal) {
// テキストエリアを用意する
const copyFrom = document.createElement(`textarea`);
// テキストエリアへ値をセット
copyFrom.textContent = _textVal;
// bodyタグの要素を取得
const bodyElm = document.getElementsByTagName(`body`)[0];
// 子要素にテキストエリアを配置
bodyElm.appendChild(copyFrom);
// テキストエリアの値を選択
copyFrom.select();
// コピーコマンド発行
const retVal = document.execCommand(`copy`);
// 追加テキストエリアを削除
bodyElm.removeChild(copyFrom);
// 処理結果を返却
return retVal;
}
/**
* 図形の描画
* - div子要素の作成。呼び出しただけでは使用できないので、親divよりappendChildすること。
* - 詳細は @see {@link createButton} も参照のこと。
* @param {string} _id
* @param {number} _x
* @param {number} _y
* @param {number} _width
* @param {number} _height
*/
function createDiv(_id, _x, _y, _width, _height) {
const div = document.createElement(`div`);
div.id = _id;
const style = div.style;
style.left = `${_x}px`;
style.top = `${_y}px`;
style.width = `${_width}px`;
style.height = `${_height}px`;
style.position = `absolute`;
style.userSelect = C_DIS_NONE;
style.webkitUserSelect = C_DIS_NONE;
style.msUserSelect = C_DIS_NONE;
style.mozUserSelect = C_DIS_NONE;
style.webkitTouchCallout = C_DIS_NONE;
return div;
}
/**
* 子div要素のラベル文字作成 (CSS版・拡張属性対応)
* @param {string} _id
* @param {string} _text
* @param {object} _obj (x, y, w, h, siz, align, ...rest)
* @param {...any} _classes
*/
function createDivCss2Label(_id, _text, { x = 0, y = 0, w = C_LEN_SETLBL_WIDTH, h = C_LEN_SETLBL_HEIGHT,
siz = C_SIZ_SETLBL, align = C_ALIGN_CENTER, ...rest } = {}, ..._classes) {
const div = createDiv(_id, x, y, w, h);
div.classList.add(g_cssObj.title_base, ..._classes);
const style = div.style;
style.fontSize = `${siz}px`;
style.fontFamily = getBasicFont();
style.textAlign = `${align}`;
div.innerHTML = _text;
Object.keys(rest).forEach(property => style[property] = rest[property]);
return div;
}
/**
* 画像表示
* @param {string} _id
* @param {string} _imgPath
* @param {number} _x
* @param {number} _y
* @param {number} _width
* @param {number} _height
*/
function createImg(_id, _imgPath, _x, _y, _width, _height) {
const div = createDiv(_id, _x, _y, _width, _height);
div.innerHTML = `<img id="${_id}img" src="${_imgPath}" style="width:${_width}px;height:${_height}px"${g_isFile ? `` : ` crossOrigin="anonimous"`}>`;
return div;
}
/**
* 色付きオブジェクトの作成 (拡張属性対応)
* @param {string} _id
* @param {object} _obj (x, y, w, h, color, rotate, styleName, ...rest)
* @param {...any} _classes
*/
function createColorObject2(_id,
{ x = 0, y = 0, w = C_ARW_WIDTH, h = C_ARW_WIDTH, rotate = ``, styleName = ``, ...rest } = {}, ..._classes) {
const div = createDiv(_id, x, y, w, h);
div.classList.add(..._classes);
const style = div.style;
// 矢印・オブジェクト判定
let charaStyle;
if (isNaN(parseFloat(rotate))) {
charaStyle = `${rotate}${styleName}`;
} else {
charaStyle = `arrow${styleName}`;
style.transform = `rotate(${rotate}deg)`;
}
style.maskImage = `url("${g_imgObj[charaStyle]}")`;
style.maskSize = `contain`;
style.webkitMaskImage = `url("${g_imgObj[charaStyle]}")`;
style.webkitMaskSize = `contain`;
Object.keys(rest).forEach(property => style[property] = rest[property]);
setAttrs(div, { color: rest.background || ``, type: charaStyle, cnt: 0, });
return div;
}
/**
* 空スプライト(ムービークリップ相当)の作成
* - 作成済みの場合はすでに作成済のスプライトを返却する
* - ここで作成したスプライトは clearWindow() により削除される
* @deprecated v21以降非推奨
* @param {string} _parentObjName 親スプライト名
* @param {string} _newObjName 作成する子スプライト名
* @param {number} _x 作成するスプライトのx座標(親スプライト基準)
* @param {number} _y 作成するスプライトのy座標(親スプライト基準)
* @param {number} _width 幅
* @param {number} _height 高さ
*/
function createSprite(_parentObjName, _newObjName, _x, _y, _width, _height, _options = {}) {
let newsprite;
if (document.getElementById(_newObjName) === null) {
newsprite = createDiv(_newObjName, _x, _y, _width, _height);
document.getElementById(_parentObjName).appendChild(newsprite);
} else {
newsprite = document.getElementById(_newObjName);
}
if (_options.description !== undefined) {
newsprite.title = _options.description;
}
return newsprite;
}
/**
* 空スプライト(ムービークリップ相当)の作成
* - 作成済みの場合はすでに作成済のスプライトを返却する
* @param {object} _parentObj 親スプライト
* @param {string} _newObjId 作成する子スプライト名
* @param {object} _obj (x, y, w, h, ...rest)
* @param {...any} _classes
* @returns
*/
function createEmptySprite(_parentObj, _newObjId, { x = 0, y = 0, w = g_sWidth, h = g_sHeight, title = ``, ...rest } = {}, ..._classes) {
if (document.getElementById(_newObjId) !== null) {
changeStyle(_newObjId, { x, y, w, h, title, ...rest });
return document.getElementById(_newObjId);
}
const div = createDiv(_newObjId, x, y, w, h);
div.classList.add(..._classes);
div.title = title;
const style = div.style;
Object.keys(rest).forEach(property => style[property] = rest[property]);
_parentObj.appendChild(div);
return div;
}
/**
* 階層スプライト(全体)の作成
* @param {string} _baseName
* @param {number} _num
*/
function createMultipleSprite(_baseName, _num) {
const sprite = createEmptySprite(divRoot, `${_baseName}`);
for (let j = 0; j <= _num; j++) {
createEmptySprite(sprite, `${_baseName}${j}`);
}
return sprite;
}
/**
* 親スプライト配下の子スプライトを全削除
* @param {object} _parentObjName 親スプライト名
*/
function deleteChildspriteAll(_parentObjName) {
const parentsprite = document.querySelector(`#${_parentObjName}`);
while (parentsprite.hasChildNodes()) {
g_handler.removeListener(parentsprite.firstChild.getAttribute(`lsnrkey`));
g_handler.removeListener(parentsprite.firstChild.getAttribute(`lsnrkeyTS`));
g_handler.removeListener(parentsprite.firstChild.getAttribute(`lsnrkeyTE`));
parentsprite.removeChild(parentsprite.firstChild);
}
}
/**
* ボタンの作成 (CSS版・拡張属性対応)
* @param {string} _id
* @param {string} _text
* @param {function} _func
* @param {object} _obj (x, y, w, h, siz, align, title, groupName, initDisabledFlg, ...rest)
* @param {...any} _classes
*/
function createCss2Button(_id, _text, _func = _ => true, { x = 0, y = g_sHeight - 100, w = g_sWidth / 3, h = C_BTN_HEIGHT,
siz = C_LBL_BTNSIZE, align = C_ALIGN_CENTER, title = ``, groupName = g_currentPage, initDisabledFlg = true,
resetFunc = _ => true, cxtFunc = _ => true, ...rest } = {}, ..._classes) {
const div = createDiv(_id, x, y, w, h);
div.classList.add(`button_common`, ..._classes);
div.innerHTML = _text;
div.title = title;
div.ontouchstart = ``;
const style = div.style;
style.textAlign = align;
style.fontSize = `${siz}px`;
style.fontFamily = getBasicFont();
if (rest.animationName !== undefined) {
style.animationDuration = `1s`;
}
Object.keys(rest).forEach(property => style[property] = rest[property]);
// ボタン有効化操作
if (initDisabledFlg) {
if (g_initialFlg && g_btnWaitFrame[groupName].initial) {
} else {
style.pointerEvents = C_DIS_NONE;
setTimeout(_ => style.pointerEvents = setVal(rest.pointerEvents, `auto`, C_TYP_STRING),
g_btnWaitFrame[groupName].b_frame * 1000 / g_fps);
}
}
// ボタンを押したときの動作
const lsnrkey = g_handler.addListener(div, `click`, evt => {
if (!setVal(g_btnDeleteFlg[_id], false, C_TYP_BOOLEAN)) {
_func(evt);
}
if (typeof g_btnAddFunc[_id] === C_TYP_FUNCTION) {
g_btnAddFunc[_id](evt, _func, resetFunc);
}
if (!setVal(g_btnDeleteFlg[_id], false, C_TYP_BOOLEAN)) {
resetFunc(evt);
}
});
// 右クリック時の処理
div.oncontextmenu = evt => {
if (typeof cxtFunc === C_TYP_FUNCTION) {
if (!setVal(g_cxtDeleteFlg[_id], false, C_TYP_BOOLEAN)) {
cxtFunc(evt);
}
if (typeof g_cxtAddFunc[_id] === C_TYP_FUNCTION) {
g_cxtAddFunc[_id](evt, cxtFunc);
}
} else if (typeof g_cxtAddFunc[_id] === C_TYP_FUNCTION) {
g_cxtAddFunc[_id](evt);
}
return false;
}
// イベントリスナー用のキーをセット
div.setAttribute(`lsnrkey`, lsnrkey);
return div;
}
/**
* オブジェクトのスタイル一括変更
* @param {string} _id
* @param {object} _obj (x, y, w, h, siz, align, title, ...rest)
*/
function changeStyle(_id, { x, y, w, h, siz, align, title, ...rest } = {}) {
const div = document.querySelector(`#${_id}`);
const style = div.style;
const obj = {
left: x,
top: y,
width: w,
height: h,
fontSize: siz,
};
Object.keys(obj).filter(property => setVal(obj[property], ``, C_TYP_FLOAT) !== ``)
.forEach(property => style[property] = `${obj[property]}px`);
if (align !== undefined) {
style.textAlign = `${align}`;
}
if (title !== undefined) {
div.title = title;
}
Object.keys(rest).forEach(property => style[property] = rest[property]);
}
/**
* タイトル文字描画
* @param {string} _id
* @param {string} _titlename
* @param {number} _x
* @param {number} _y
*/
function getTitleDivLabel(_id, _titlename, _x, _y, ..._classes) {
return createDivCss2Label(_id, _titlename, { x: _x, y: _y, w: g_sWidth, h: 50, siz: C_LBL_BTNSIZE }, ..._classes);
}
/**
* キーコントロールの初期化
*/
function resetKeyControl() {
document.onkeyup = _ => { };
document.onkeydown = evt => blockCode(transCode(evt.code));
g_inputKeyBuffer = {};
}
/**
* 画面上の描画・オブジェクトを全てクリアし、背景を再描画
* - divオブジェクト(ボタンなど)はdivRoot配下で管理しているため、子要素のみを全削除している。
* - dicRoot自体を削除しないよう注意すること。
* - 再描画時に共通で表示する箇所はここで指定している。
*/
function clearWindow(_redrawFlg = false, _customDisplayName = ``) {
resetKeyControl();
if (document.querySelector(`#layer0`) !== null) {
// レイヤー情報取得
const layer0 = document.querySelector(`#layer0`);
const l0ctx = layer0.getContext(`2d`);
const C_MARGIN = 0;
// 線画、図形をクリア
l0ctx.clearRect(0, 0, g_sWidth, g_sHeight);
if (document.querySelector(`#layer1`) !== null) {
const layer1 = document.querySelector(`#layer1`);
const l1ctx = layer1.getContext(`2d`);
l1ctx.clearRect(0, 0, g_sWidth, g_sHeight);
// 線画 (title-line)
l1ctx.beginPath();
l1ctx.strokeStyle = `#cccccc`;
l1ctx.moveTo(C_MARGIN, C_MARGIN);
l1ctx.lineTo(g_sWidth - C_MARGIN, C_MARGIN);
l1ctx.stroke();
l1ctx.beginPath();
l1ctx.strokeStyle = `#cccccc`;
l1ctx.moveTo(C_MARGIN, g_sHeight - C_MARGIN);
l1ctx.lineTo(g_sWidth - C_MARGIN, g_sHeight - C_MARGIN);
l1ctx.stroke();
}
if (document.querySelector(`#layer2`) !== null) {
const layer2 = document.querySelector(`#layer2`);
const l2ctx = layer2.getContext(`2d`);
l2ctx.clearRect(0, 0, g_sWidth, g_sHeight);
}
}
// ボタン、オブジェクトをクリア (divRoot配下のもの)
deleteChildspriteAll(`divRoot`);
// 背景を再描画
if (_redrawFlg) {
drawDefaultBackImage(_customDisplayName);
}
}
/**
* 外部jsファイルの読込
* 読込可否を g_loadObj[ファイル名] で管理 (true: 読込成功, false: 読込失敗)
* @param {string} _url
* @param {function} _callback
* @param {boolean} _requiredFlg (default : true / 読込必須)
* @param {string} _charset (default : UTF-8)
*/
function loadScript(_url, _callback, _requiredFlg = true, _charset = `UTF-8`) {
const baseUrl = _url.split(`?`)[0];
g_loadObj[baseUrl] = false;
const script = document.createElement(`script`);
script.type = `text/javascript`;
script.src = _url;
script.charset = _charset;
script.onload = _ => {
g_loadObj[baseUrl] = true;
_callback();
};
script.onerror = _ => {
if (_requiredFlg) {
makeWarningWindow(g_msgInfoObj.E_0041.split(`{0}`).join(_url.split(`?`)[0]));
} else {
_callback();
}
};
document.querySelector(`head`).appendChild(script);
}
// WebAudioAPIでAudio要素風に再生するクラス
class AudioPlayer {
constructor() {
const AudioContext = window.AudioContext || window.webkitAudioContext;
this._context = new AudioContext();
this._gain = this._context.createGain();
this._gain.connect(this._context.destination);
this._startTime = 0;
this._fadeinPosition = 0;
this._eventListeners = {};
this.playbackRate = 1;
}
async init(_arrayBuffer) {
this._arrayBuffer = _arrayBuffer;
await this._context.decodeAudioData(this._arrayBuffer, _buffer => {
this._duration = _buffer.duration;
this._buffer = _buffer;
});
if (this._eventListeners[`canplaythrough`] !== undefined) {
this._eventListeners[`canplaythrough`].forEach(_listener => _listener());
}
}
play() {
this._source = this._context.createBufferSource();
this._source.buffer = this._buffer;
this._source.playbackRate.value = this.playbackRate;
this._source.connect(this._gain);
this._startTime = this._context.currentTime;
this._source.start(this._context.currentTime, this._fadeinPosition);
}
pause() {
if (this._source) {
this._source.stop(0);
}
}
set currentTime(_currentTime) {
this._fadeinPosition = _currentTime;
}
get volume() {
return this._gain.gain.value;
}
set volume(_volume) {
this._gain.gain.value = _volume;
}
get duration() {
return this._duration;