forked from robx/pzprjs
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathKeyInput.js
More file actions
756 lines (699 loc) · 21.6 KB
/
Copy pathKeyInput.js
File metadata and controls
756 lines (699 loc) · 21.6 KB
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
// KeyInput.js v3.4.1
//---------------------------------------------------------------------------
// ★KeyEventクラス キーボード入力に関する情報の保持とイベント処理を扱う
//---------------------------------------------------------------------------
// パズル共通 キーボード入力部
// KeyEventクラスを定義
pzpr.classmgr.makeCommon({
//---------------------------------------------------------
KeyEvent: {
initialize: function() {
this.cursor = this.puzzle.cursor;
this.enableKey = true; // キー入力は有効か
this.keyreset();
},
enablemake: false,
enableplay: false,
keyup_event: false /* keyupイベントでもパズル個別のキーイベント関数を呼び出す */,
//---------------------------------------------------------------------------
// kc.keyreset() キーボード入力に関する情報を初期化する
// kc.isenablemode() 現在のモードでキー入力が有効か判定する
//---------------------------------------------------------------------------
keyreset: function(isTest) {
this.isCTRL = false;
this.isMETA = false; // MacのCommandキーなど
this.isALT = false; // ALTはメニュー用なので基本的に使わない
this.isSHIFT = false;
this.isZ = false;
this.isX = false;
this.isY = false;
this.keydown = false;
this.keyup = false;
if (!isTest) {
this.ca = "";
this.prev = null;
}
},
isenablemode: function() {
return (
(this.puzzle.editmode && this.enablemake) ||
(this.puzzle.playmode && this.enableplay)
);
},
//---------------------------------------------------------------------------
// kc.e_keydown() キーを押した際のイベント共通処理
// kc.e_keyup() キーを離した際のイベント共通処理
//---------------------------------------------------------------------------
// この3つのキーイベントはwindowから呼び出される(kcをbindしている)
e_keydown: function(e) {
var c = this.getchar(e);
if (!this.enableKey) {
if (e.target === document.body && (c === "BS" || c === " ")) {
e.stopPropagation();
e.preventDefault();
}
return;
}
this.checkbutton(c, 0);
if (c) {
this.keyevent(c, 0);
}
if (e.target === this.puzzle.canvas || this.cancelDefault) {
e.stopPropagation();
e.preventDefault();
}
},
e_keyup: function(e) {
var c = this.getchar(e);
if (!this.enableKey) {
if (e.target === document.body && (c === "BS" || c === " ")) {
e.stopPropagation();
e.preventDefault();
}
return;
}
this.checkbutton(c, 1);
if (c) {
this.keyevent(c, 1);
}
if (e.target === this.puzzle.canvas || this.cancelDefault) {
e.stopPropagation();
e.preventDefault();
}
},
//---------------------------------------------------------------------------
// kc.checkmodifiers() Shift, Ctrl, Alt, Metaキーをチェックする
// kc.checkbutton() Z, X, Yキーの押下状況をチェックする
//---------------------------------------------------------------------------
checkmodifiers: function(e) {
this.isSHIFT = e.shiftKey;
this.isCTRL = e.ctrlKey;
this.isMETA = e.metaKey;
this.isALT = e.altKey;
},
checkbutton: function(charall, step) {
var c = charall.split(/\+/).pop();
if (c === "z") {
this.isZ = step === 0;
}
if (c === "x") {
this.isX = step === 0;
}
if (c === "y") {
this.isY = step === 0;
}
},
//---------------------------------------------------------------------------
// kc.getchar() 入力されたキーを表す文字列を返す
//---------------------------------------------------------------------------
// 48~57は0~9キー、65~90はa~z、96~105はテンキー、112~123はF1~F12キー
getchar: function(e) {
this.checkmodifiers(e);
var key = "",
keycode = !!e.keyCode ? e.keyCode : e.charCode;
if (keycode === 38) {
key = "up";
} else if (keycode === 40) {
key = "down";
} else if (keycode === 37) {
key = "left";
} else if (keycode === 39) {
key = "right";
} else if (keycode === 13) {
key = "enter";
} else if (48 <= keycode && keycode <= 57) {
key = (keycode - 48).toString(36);
} else if (65 <= keycode && keycode <= 90) {
key = (keycode - 55).toString(36);
} //アルファベット
else if (96 <= keycode && keycode <= 105) {
key = (keycode - 96).toString(36);
} //テンキー対応
else if (112 <= keycode && keycode <= 123) {
key = "F" + (keycode - 111).toString(10);
} /* 112~123はF1~F12キー */ else if (keycode === 32 || keycode === 46) {
key = " ";
} // 32はスペースキー 46はdelキー
else if (keycode === 8) {
key = "BS";
} else if (keycode === 109 || keycode === 189 || keycode === 173) {
key = "-";
} else if (keycode === 106) {
key = "*";
} else if (keycode === 107 || keycode === 59 || keycode === 61) {
key = "+";
}
var keylist = !!key ? [key] : [];
if (this.isMETA) {
keylist.unshift("meta");
}
if (this.isALT) {
keylist.unshift("alt");
}
if (this.isCTRL) {
keylist.unshift("ctrl");
}
if (this.isSHIFT && key !== "+") {
keylist.unshift("shift");
}
key = keylist.join("+");
if (key === "alt+h") {
key = "left";
} else if (key === "alt+k") {
key = "up";
} else if (key === "alt+j") {
key = "down";
} else if (key === "alt+l") {
key = "right";
}
return key;
},
//---------------------------------------------------------------------------
// kc.inputKeys() キーボードイベントを実行する
//---------------------------------------------------------------------------
inputKeys: function(array) {
this.cursor.isActive = true;
for (var i = 0; i < arguments.length; i++) {
var ca = arguments[i];
this.isSHIFT = ca.match(/shift\+/);
this.isCTRL = ca.match(/ctrl\+/);
this.isALT = ca.match(/alt\+/);
this.isMETA = ca.match(/meta\+/);
this.isX = ca.match(/x\+/);
this.isY = ca.match(/y\+/);
this.isZ = ca.match(/z\+/);
this.keyevent(ca, 0);
this.keyevent(ca, 1);
this.keyreset(true);
}
},
//---------------------------------------------------------------------------
// kc.keyevent() キーイベント処理
//---------------------------------------------------------------------------
keyevent: function(c, step) {
var puzzle = this.puzzle;
this.cancelEvent = false;
this.cancelDefault = false;
this.keydown = step === 0;
this.keyup = step === 1;
// always prevent backspace navigation, space scrolling
if (c === "BS" || c === " ") {
this.cancelDefault = true;
}
if (this.keydown) {
puzzle.opemgr.newOperation();
} else {
puzzle.opemgr.newChain();
}
if (this.keydown && !this.isZ) {
puzzle.errclear();
puzzle.opemgr.updateStarts();
}
if (this.keydown && c === "F4" && puzzle.playeronly) {
puzzle.togglePause();
return;
}
if (puzzle.pausetime !== null) {
return;
}
puzzle.emit("key", c);
if (this.cancelEvent) {
return;
}
if (!this.keyexec(c)) {
return;
}
if (!this.keyDispInfo(c)) {
return;
}
if (!this.isenablemode()) {
return;
}
if (this.keydown && !this.cursor.isActive) {
this.cursor.isActive = true;
this.cancelDefault = true;
this.cursor.draw();
return;
}
if (this.keydown && this.moveTarget(c)) {
this.cancelDefault = true;
return;
}
if (this.keydown || (this.keyup && this.keyup_event)) {
this.keyinput(c);
} /* 各パズルのルーチンへ */
},
//---------------------------------------------------------------------------
// kc.keyexec() モードに共通で行う処理を実行します
//---------------------------------------------------------------------------
keyexec: function(c) {
var puzzle = this.puzzle;
if (this.keydown && c === "alt+c" && !puzzle.playeronly) {
puzzle.setMode(
puzzle.playmode ? puzzle.MODE_EDITOR : puzzle.MODE_PLAYER
);
return false;
}
return true;
},
//---------------------------------------------------------------------------
// kc.keyDispInfo() 一時的に情報を表示する処理を追加します
//---------------------------------------------------------------------------
keyDispInfo: function(c) {
return true;
},
//---------------------------------------------------------------------------
// kc.keyinput() キーを押した/離した際の各パズルごとのイベント処理。
// 各パズルのファイルでオーバーライドされる。
//---------------------------------------------------------------------------
// オーバーライド用
keyinput: function(c) {
this.key_inputqnum(c); /* デフォルトはCell数字入力 */
},
//---------------------------------------------------------------------------
// kc.moveTarget() キーボードからの入力対象を矢印キーで動かす
// kc.moveTCell() Cellのキーボードからの入力対象を矢印キーで動かす
// kc.moveTCross() Crossのキーボードからの入力対象を矢印キーで動かす
// kc.moveTBorder() Borderのキーボードからの入力対象を矢印キーで動かす
// kc.moveTC() 上記3つの関数の共通処理
//---------------------------------------------------------------------------
moveTarget: function(ca) {
return this.moveTCell(ca);
},
moveTCell: function(ca) {
return this.moveTC(ca, 2);
},
moveTCross: function(ca) {
return this.moveTC(ca, 2);
},
moveTBorder: function(ca) {
return this.moveTC(ca, 1);
},
moveTC: function(ca, mv) {
var cursor = this.cursor,
editmode = this.puzzle.editmode,
pos0 = cursor.getaddr(),
flag = true,
dir = cursor.NDIR;
switch (ca) {
case "up":
if (
cursor.by - mv >= cursor.miny ||
(editmode && cursor.hasIndicator && cursor.by === cursor.miny)
) {
dir = cursor.UP;
}
break;
case "down":
if (cursor.by + mv <= cursor.maxy) {
dir = cursor.DN;
}
break;
case "left":
if (cursor.bx - mv >= cursor.minx) {
dir = cursor.LT;
}
break;
case "right":
if (cursor.bx + mv <= cursor.maxx) {
dir = cursor.RT;
}
break;
default:
flag = false;
break;
}
if (flag) {
cursor.movedir(dir, mv);
pos0.draw();
cursor.draw();
}
return flag;
},
//---------------------------------------------------------------------------
// kc.moveExCell() ExCellのキーボードからの入力対象を矢印キーで動かす
//---------------------------------------------------------------------------
moveExCell: function(ca) {
var cursor = this.cursor,
addr0 = cursor.getaddr(),
flag = true,
dir = addr0.NDIR;
switch (ca) {
case "up":
if (
cursor.by === cursor.maxy &&
cursor.minx < cursor.bx &&
cursor.bx < cursor.maxx
) {
cursor.by = cursor.miny;
} else if (cursor.by > cursor.miny) {
dir = addr0.UP;
} else {
flag = false;
}
break;
case "down":
if (
cursor.by === cursor.miny &&
cursor.minx < cursor.bx &&
cursor.bx < cursor.maxx
) {
cursor.by = cursor.maxy;
} else if (cursor.by < cursor.maxy) {
dir = addr0.DN;
} else {
flag = false;
}
break;
case "left":
if (
cursor.bx === cursor.maxx &&
cursor.miny < cursor.by &&
cursor.by < cursor.maxy
) {
cursor.bx = cursor.minx;
} else if (cursor.bx > cursor.minx) {
dir = addr0.LT;
} else {
flag = false;
}
break;
case "right":
if (
cursor.bx === cursor.minx &&
cursor.miny < cursor.by &&
cursor.by < cursor.maxy
) {
cursor.bx = cursor.maxx;
} else if (cursor.bx < cursor.maxx) {
dir = addr0.RT;
} else {
flag = false;
}
break;
default:
flag = false;
break;
}
if (flag) {
if (dir !== addr0.NDIR) {
cursor.movedir(dir, 2);
}
addr0.draw();
cursor.draw();
}
return flag;
}
},
//---------------------------------------------------------------------------
// ★TargetCursorクラス キー入力のターゲットを保持する
//---------------------------------------------------------------------------
"TargetCursor:Address": {
initialize: function() {
this.bx = 1;
this.by = 1;
this.bankpiece = null;
this.mode51 = this.puzzle.klass.ExCell.prototype.ques === 51;
this.modesnum = this.puzzle.klass.Cell.prototype.enableSubNumberArray;
this.disableAnum = this.puzzle.klass.Cell.prototype.disableAnum;
this.targetdirs = this.puzzle.klass.Cell.prototype.dirs51;
if (this.mode51 && this.puzzle.editmode) {
this.targetdir = 4; // right
} else if (this.disableAnum && this.puzzle.playmode) {
this.targetdir = 5;
}
this.isActive = !this.puzzle.playeronly;
},
init: function(bx, by) {
this.bx = bx;
this.by = by;
this.bankpiece = null;
if (!this.mode51) {
this.targetdir = this.disableAnum && this.puzzle.playmode ? 5 : 0;
}
return this;
},
getc: function() {
return this.bankpiece === null
? this.board.getc(this.bx, this.by)
: this.board.emptycell;
},
// 有効な範囲(minx,miny)-(maxx,maxy)
minx: null,
miny: null,
maxx: null,
maxy: null,
crosstype: false,
isActive: true,
//---------------------------------------------------------------------------
// tc.initCursor() 初期化時にカーソルの位置を設定する
//---------------------------------------------------------------------------
initCursor: function() {
if (this.crosstype) {
this.init(0, 0);
} else {
this.init(1, 1);
}
this.adjust_init();
},
//---------------------------------------------------------------------------
// tc.setminmax() 初期化時・モード変更時にプロパティを設定する
// tc.setminmax_customize() 初期化時・モード変更時のプロパティをパズルごとに調節する
//---------------------------------------------------------------------------
setminmax: function() {
var bd = this.board,
bm = !this.crosstype ? 1 : 0;
this.minx = bd.minbx + bm;
this.miny = bd.minby + bm;
this.maxx = bd.maxbx - bm;
this.maxy = bd.maxby - bm;
this.setminmax_customize();
this.adjust_init();
},
setminmax_customize: function() {},
//---------------------------------------------------------------------------
// tc.adjust_init() 初期化時にカーソルの位置がおかしい場合に調整する
// tc.adjust_modechange() モード変更時にカーソルの位置を調節する
// tc.adjust_cell_to_excell() モード変更時にカーソルの位置をCellからExCellへ移動する
//---------------------------------------------------------------------------
adjust_init: function() {
if (this.bx < this.minx) {
this.bx = this.minx;
}
if (this.by < this.miny) {
this.by = this.miny;
}
if (this.bx > this.maxx) {
this.bx = this.maxx;
}
if (this.by > this.maxy) {
this.by = this.maxy;
}
},
adjust_modechange: function() {
if (this.setminmax_customize !== this.common.setminmax_customize) {
this.setminmax();
} // editmode, playmodeでminmaxが異なるパズル
if (this.mode51 && this.puzzle.editmode) {
this.targetdir = 4;
} // right
else if (this.modesnum) {
this.targetdir = this.puzzle.playmode && this.disableAnum ? 5 : 0;
}
if (this.puzzle.playmode) {
this.bankpiece = null;
}
},
adjust_cell_to_excell: function() {
var bd = this.board;
var shortest = Math.min(
this.bx,
bd.cols * 2 - this.bx,
this.by,
bd.rows * 2 - this.by
);
if (shortest <= 0) {
return;
} else if (this.by === shortest) {
this.by = this.miny;
} else if (bd.rows * 2 - this.by === shortest) {
this.by = this.maxy;
} else if (this.bx === shortest) {
this.bx = this.minx;
} else if (bd.cols * 2 - this.bx === shortest) {
this.bx = this.maxx;
}
},
//---------------------------------------------------------------------------
// tc.checksnum() ターゲットの位置かどうか判定する (Cellのみ)
//---------------------------------------------------------------------------
checksnum: function(pos) {
var bx = ((((pos.bx + 12) / 2) | 0) - 6) * 2 + 1;
var by = ((((pos.by + 12) / 2) | 0) - 6) * 2 + 1;
var result = this.bx === bx && this.by === by;
if (result && this.modesnum && this.puzzle.playmode) {
if (this.disableAnum) {
var tmpx = pos.bx % 2 | 0;
var tmpy = pos.by % 2 | 0;
result = [5, 4, 2, 3][tmpy * 2 + tmpx] === this.targetdir;
} else {
var tmpx = (((pos.bx + 12) % 2) * 1.5) | 0;
var tmpy = (((pos.by + 12) % 2) * 1.5) | 0;
if (this.pid !== "factors") {
result =
[5, 0, 4, 0, 0, 0, 2, 0, 3][tmpy * 3 + tmpx] === this.targetdir;
} else {
result =
[0, 0, 4, 0, 0, 0, 2, 0, 3][tmpy * 3 + tmpx] === this.targetdir;
}
}
}
return result;
},
//---------------------------------------------------------------------------
// tc.getaddr() ターゲットの位置を移動する
//---------------------------------------------------------------------------
movedir: function(dir, mv) {
if (this.bankpiece !== null) {
return this;
}
this.puzzle.klass.Address.prototype.movedir.call(this, dir, mv);
if (this.modesnum && this.puzzle.playmode && !this.disableAnum) {
this.targetdir = 0;
}
return this;
},
//---------------------------------------------------------------------------
// tc.getaddr() ターゲットの位置をAddressクラスのオブジェクトで取得する
// tc.setaddr() ターゲットの位置をAddressクラス等のオブジェクトで設定する
//---------------------------------------------------------------------------
setaddr: function(pos) {
/* Address, Cellなどのオブジェクトいずれを入力しても良い */
if (
pos.bx < this.minx ||
this.maxx < pos.bx ||
pos.by < this.miny - (this.hasIndicator ? 2 : 0) ||
this.maxy < pos.by
) {
return;
}
this.bankpiece = null;
this.set(pos);
if (this.modesnum && this.puzzle.playmode && !this.disableAnum) {
this.targetdir = 0;
}
},
//---------------------------------------------------------------------------
// tc.moveTo() ターゲットの位置を指定した(bx,by)に設定する
//---------------------------------------------------------------------------
moveTo: function(bx, by) {
this.init(bx, by);
if (this.modesnum && this.puzzle.playmode && !this.disableAnum) {
this.targetdir = 0;
}
},
//---------------------------------------------------------------------------
// tc.chtarget() SHIFTを押した時に[\]の入力するところを選択する
// tc.detectTarget() [\]の右・下どちらに数字を入力するか判断する
// tc.getNumOfTarget() Cell上でtargetとして取りうる数を返す
//---------------------------------------------------------------------------
targetdir: 0,
chtarget: function(mouse, dx, dy) {
if (this.oncell() && this.modesnum && this.puzzle.playmode) {
if (this.disableAnum) {
this.targetdir = [5, 1, 3, 5, 2, 4][this.targetdir];
} else if (this.pid !== "factors") {
this.targetdir = [5, 1, 3, 0, 2, 4][this.targetdir];
} else {
this.targetdir = [4, 1, 3, 0, 2, 0][this.targetdir];
}
} else {
if (this.targetdirs === 4) {
if (mouse) {
var ur = dx - dy > 0,
ul = dx + dy < 0;
if (ur) {
if (ul) {
this.targetdir = 1;
} else {
this.targetdir = 4;
}
} else {
if (ul) {
this.targetdir = 3;
} else {
this.targetdir = 2;
}
}
} else {
this.targetdir = [4, 0, 3, 1, 2, 0][this.targetdir];
}
} else {
if (mouse) {
this.targetdir = dx - dy > 0 ? 4 : 2;
} else {
this.targetdir = this.targetdir === 2 ? 4 : 2;
}
}
}
this.draw();
},
detectTarget: function(piece) {
piece = piece || this.getobj();
var bd = this.board,
adc = piece.adjacent;
if (piece.isnull) {
return 0;
} else if (piece.group === "cell") {
if (piece.ques !== 51) {
return 0;
} else if (this.targetdirs === 2) {
var invalidRight = adc.right.isnull || adc.right.ques === 51;
var invalidBottom = adc.bottom.isnull || adc.bottom.ques === 51;
if (invalidRight && invalidBottom) {
return 0;
} else if (invalidBottom) {
return piece.RT;
} else if (invalidRight) {
return piece.DN;
}
}
} else if (piece.group === "excell") {
if (piece.id === bd.cols + bd.rows) {
return 0;
} else if (
(piece.by === -1 && adc.bottom.ques === 51) ||
(piece.bx === -1 && adc.right.ques === 51)
) {
return 0;
} else if (piece.by === -1) {
return piece.DN;
} else if (piece.bx === -1) {
return piece.RT;
}
} else {
return 0;
}
return this.targetdir;
},
getNumOfTarget: function(piece) {
var num = 1;
if (piece.isnull) {
num = 0;
} else if (piece.group === "cell") {
if (this.modesnum && this.puzzle.playmode) {
num = 4 - (this.pid === "factors" ? 1 : 0);
} else if (piece.ques === 51) {
var adc = piece.adjacent;
num =
(!adc.right.isnull && adc.right.ques !== 51 ? 1 : 0) +
(!adc.bottom.isnull && adc.bottom.ques !== 51 ? 1 : 0);
}
}
return num;
},
getDot: function() {
return this.board.getDot(this.bx, this.by);
}
}
});