/
ControllerPad.js
1338 lines (1197 loc) · 147 KB
/
ControllerPad.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
this.tabageos = this.tabageos || {};
(function() {
/**
*
* HTML5 ControllerPad with 10 possible buttons.
* Also handles keyboard input for when touches are not available.
* (for a game that is using tabageos and the tabageos.MouseController class)
* this class uses tabageos.GeometricMath and extends tabageos.EventDispatcher.
* Must instantiate and then call establish. The constructors params (besides the Numbers) are tabgeos.MoverPoints or Objects with x and y properties.
* Use the controller.keyboardEquivalents Object to define the keyboard key values that correspond to the buttons.
*
* Users may need to mash their touch screens to be sure and send a new touchstart event, when needing to press two buttons simultaineously
* one needs to be sure and make definite presses, then smooth normal nintendo like play is what you get.
* So your buttons should be big, at least all thumb size.
* The MoverPoints you pass during construction should define the middle point of each button.
*
* Includes usb game pad support; see .handleGamePad()
*
*
*/
function ControllerPad(holder, x, y, w, h, leftp, rightp, upp, downp, startp,backp,ap,bp,cp,dp, directionButtonsWidth, directionButtonsHeight, startAndBackWidth, startAndBackHeight, buttonWidth, buttonHeight) {
tabageos.EventDispatcher.call(this);
this.x = x; this.y = y;
this.width = w; this.height =h;
this._customSpecs = {};
this._customSpecs.leftp = leftp;
this._customSpecs.rightp = rightp;
this._customSpecs.upp = upp;
this._customSpecs.downp = downp;
this._customSpecs.startp = startp;
this._customSpecs.backp = backp;
this._customSpecs.ap = ap;
this._customSpecs.bp = bp;
this._customSpecs.cp = cp;
this._customSpecs.dp = dp;
this._customSpecs.directionButtonsWidth = directionButtonsWidth;
this._customSpecs.directionButtonsHeight = directionButtonsHeight;
this._customSpecs.startAndBackWidth = startAndBackWidth;
this._customSpecs.startAndBackHeight = startAndBackHeight;
this._customSpecs.buttonWidth = buttonWidth;
this._customSpecs.buttonHeight = buttonHeight;
this.__recurse = -1;
this._style = -1;
this.__mouseEnabled = 0;
this._buttonRectColor = "#6495edbb";
this.holderDiv = holder;
this._holderDivParent = holder ? holder.offsetParent : null;
this.avg = 55;
this.buttons = {"right":0, "left":0, "up":0, "down":0, "a":0, "b":0, "c":0, "d":0, "start":0, "back":0};
this.basicArrows = {"right":39, "left":37, "up":38, "down":40, "a":32, "b":66, "c":67, "d":77, "start":16, "back":27};
this.arrows = {"right":39, "left":37, "up":38, "down":40, "a":37, "b":38, "c":39, "d":40, "start":16, "back":27};
this.basicWasd = {"right":68, "left":65, "up":87, "down":83, "a":32, "b":66, "c":67, "d":77, "start":16, "back":27};
this.wasd = {"right":68, "left":65, "up":87, "down":83, "a":37, "b":38, "c":39, "d":40, "start":16, "back":27};
this.keyboardEquivalents = {"right":68, "left":65, "up":87, "down":83, "a":69, "b":32, "c":81, "d":66, "start":16, "back":27};
if(ControllerPad.instance) {
throw "ControllerPad constructed already.";
}
this._gpb = {a:-1, b:-1, c:-1, d:-1, s:-1, st:-1};
ControllerPad.instance = this;
}
ControllerPad.prototype = Object.create(tabageos.EventDispatcher.prototype);
ControllerPad.prototype.x = 0;
ControllerPad.prototype.y = 0;
ControllerPad.prototype.width = 0;
ControllerPad.prototype.height = 0;
ControllerPad.prototype.avg = 52;
ControllerPad.prototype._customSpecs;
ControllerPad.prototype.buttonA;
ControllerPad.prototype.buttonB;
ControllerPad.prototype.buttonC;
ControllerPad.prototype.buttonD;
ControllerPad.prototype.startButton;
ControllerPad.prototype.backButton;
ControllerPad.prototype.dLeft;
ControllerPad.prototype.dRight;
ControllerPad.prototype.dUp;
ControllerPad.prototype.dDown;
ControllerPad.prototype.holderDiv;
ControllerPad.prototype._holderDivParent;
ControllerPad.prototype._style = 3;
ControllerPad.prototype.customStyleTotalWidth = 0;
ControllerPad.prototype.customStyleTotalHeight = 0;
ControllerPad.prototype.basicArrows = {"right":39, "left":37, "up":38, "down":40, "a":32, "b":66, "c":67, "d":77, "start":16, "back":27};
ControllerPad.prototype.arrows = {"right":39, "left":37, "up":38, "down":40, "a":37, "b":38, "c":39, "d":40, "start":16, "back":27};
ControllerPad.prototype.basicWasd = {"right":68, "left":65, "up":87, "down":83, "a":32, "b":66, "c":67, "d":77, "start":16, "back":27};
ControllerPad.prototype.wasd = {"right":68, "left":65, "up":87, "down":83, "a":37, "b":38, "c":39, "d":40, "start":16, "back":27};
ControllerPad.prototype.keyboardEquivalents = {"right":68, "left":65, "up":87, "down":83, "a":69, "b":32, "c":81, "d":66, "start":16, "back":27};
ControllerPad.instance;
/*
* test as follows: if(controller.buttons.right) { move right code }
*
*/
ControllerPad.prototype.buttons = {"right":0, "left":0, "up":0, "down":0, "a":0, "b":0, "c":0, "d":0, "start":0, "back":0};
ControllerPad.prototype.canvasObject;
ControllerPad.prototype.firstGamePadInstance = null;
ControllerPad.prototype.__mouseEnabled = 0;
ControllerPad.prototype.rotation = 0;
ControllerPad.prototype.deg0;
ControllerPad.prototype.deg30;
ControllerPad.prototype.deg60;
ControllerPad.prototype.deg90;
ControllerPad.prototype.deg120;
ControllerPad.prototype.deg150;
ControllerPad.prototype.deg180;
ControllerPad.prototype.deg210;
ControllerPad.prototype.deg240;
ControllerPad.prototype.deg270;
ControllerPad.prototype.deg300;
ControllerPad.prototype.deg330;
ControllerPad.prototype.deg360;
ControllerPad.prototype.rotationPad;
ControllerPad.prototype.enableMouseControl = function() {
this.__mouseEnabled = 1;
};
/*
* If you put anything for addEvents, events will Not be added
* just don't pass anything, leave addEvents out, if you intended to use the controller as normal
*/
ControllerPad.prototype.establish = function(addEvents, recordPlay) {
var dontad = false;
if(addEvents || addEvents === false) { dontad = true; }
if(dontad != true) {
window.removeEventListener("keydown", ControllerPad.instance.handleKeys, false);
window.removeEventListener("keyup", ControllerPad.instance.releaseKeys, false);
window.addEventListener("keydown", ControllerPad.instance.handleKeys, false);
window.addEventListener("keyup", ControllerPad.instance.releaseKeys, false);
window.onfocus = ControllerPad.instance.keyEstablish;
this.dLeft = this._scriptDiv();
this.dRight = this._scriptDiv();
this.dUp = this._scriptDiv();
this.dDown = this._scriptDiv();
this.buttonA = this._scriptDiv();
this.buttonB = this._scriptDiv();
this.buttonC = this._scriptDiv();
this.buttonD = this._scriptDiv();
this.startButton = this._scriptDiv();
this.backButton = this._scriptDiv();
this.rotationPad = this._scriptDiv();
if(this._holderDivParent && this._holderDivParent.contains(this.holderDiv)) {
window.console.log(this.holderDiv.id + " " + this._holderDivParent);
try { this._holderDivParent.removeChild(this.holderDiv); } catch (e) {
window.console.log("caught e during controller.establish: " + e +" Most likely the controller is in a whole bunch of other divs and html, using position:absolute or position:relative for the container div style normally will solve this error.");
}
}
}
};
ControllerPad.prototype._scriptDiv = function() {
var dv = document.createElement("div");
if( !('ontouchstart' in window) && !this.__mouseEnabled) {
dv.removeEventListener("pointerdown", ControllerPad.instance.dispatch, false);
dv.removeEventListener("pointerup", ControllerPad.instance.dispatch, false);
dv.removeEventListener("pointermove", ControllerPad.instance.dispatch, false);
dv.addEventListener("pointerdown", ControllerPad.instance.dispatch, false);
dv.addEventListener("pointerup", ControllerPad.instance.dispatch, false);
dv.addEventListener("pointermove", ControllerPad.instance.dispatch, false);
} else {
dv.removeEventListener("touchstart", ControllerPad.instance.dispatch, false);
dv.removeEventListener("touchend", ControllerPad.instance.dispatch, false);
dv.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
dv.addEventListener("touchstart", ControllerPad.instance.dispatch, false);
dv.addEventListener("touchend", ControllerPad.instance.dispatch, false);
dv.addEventListener("touchmove", ControllerPad.instance.dispatch, false);
}
if(this.__mouseEnabled) {
dv.removeEventListener("mouseup", ControllerPad.instance.dispatch, false);
dv.removeEventListener("mousedown", ControllerPad.instance.dispatch, false);
dv.addEventListener("mouseup", ControllerPad.instance.dispatch, false);
dv.addEventListener("mousedown", ControllerPad.instance.dispatch, false);
}
return dv;
};
ControllerPad.prototype.removeTouchMove = function(nr) {
this.dLeft.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
this.dRight.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
this.dUp.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
this.dDown.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
this.buttonA.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
this.buttonB.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
this.buttonC.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
this.buttonD.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
this.startButton.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
this.backButton.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
if(!nr) {
this.rotationPad.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
}
};
ControllerPad.prototype._unscriptDiv = function(dv) {
dv.removeEventListener("touchstart", ControllerPad.instance.dispatch, false);
dv.removeEventListener("touchend", ControllerPad.instance.dispatch, false);
dv.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
dv.removeEventListener("mouseup", ControllerPad.instance.dispatch, false);
dv.removeEventListener("mousedown", ControllerPad.instance.dispatch, false);
dv.removeEventListener("pointerdown", ControllerPad.instance.dispatch, false);
dv.removeEventListener("pointerup", ControllerPad.instance.dispatch, false);
dv.removeEventListener("pointermove", ControllerPad.instance.dispatch, false);
};
ControllerPad.prototype._ccStore = null;
ControllerPad.prototype.addCustomImage = function(imgLocation, customStyleName, imgElement) {
//
if(imgLocation && !imgElement) {
ControllerPad._selfStyle(customStyleName, imgLocation);
} else {
this._ccStore = new tabageos.CanvasObject(null,imgElement.width,imgElement.height);
this._ccStore.copyPixels(imgElement, new tabageos.Rectangle(0,0,imgElement.width,imgElement.height),new tabageos.MoverPoint(),imgElement.width,imgElement.height);
ControllerPad._selfStyle(customStyleName, this._ccStore.canvas.toDataURL());
}
};
/*
* For use with ControllerPad.css, this setup does not include a specific up down or D buttons.
* You can still assign any one of the buttons to do anything, just that the specific 'up' 'down' and 'D' buttons are not set up via this method.
* This image has a big left and right button, and big A B and C buttons with start and back buttons in the middle.
*
* Used with .show(w,h) (no style param passed)
* These methods automagically assign the button positions,
* you need to also call tabageos.MouseController.defineMousePositionOffset during any resizing of the games container,
* or use the tabageos.Resize method passing in an intance of the controller.
*/
ControllerPad.prototype.basicControllerButtonSetup = function() {
this._configBasic("blue");
this.holderDiv.appendChild(this.dLeft);
this.holderDiv.appendChild(this.dRight);
this.holderDiv.appendChild(this.startButton);
this.holderDiv.appendChild(this.backButton);
this.holderDiv.appendChild(this.buttonA);
this.holderDiv.appendChild(this.buttonB);
this.holderDiv.appendChild(this.buttonC);
};
ControllerPad.prototype._basicControllerButtonTakedown = function() {
this.holderDiv.removeChild(this.dLeft);
this.holderDiv.removeChild(this.dRight);
this.holderDiv.removeChild(this.startButton);
this.holderDiv.removeChild(this.backButton);
this.holderDiv.removeChild(this.buttonA);
this.holderDiv.removeChild(this.buttonB);
this.holderDiv.removeChild(this.buttonC);
};
/*
* For use with ControllerPad.css and the .show method
* Use .show(w,h,2) to display the directionalsController image.
*
*/
ControllerPad.prototype.directionalsControllerButtonSetup = function(hAdjust) {
this._configDirectional("blue");
this.holderDiv.appendChild(this.dLeft);
this.holderDiv.appendChild(this.dRight);
this.holderDiv.appendChild(this.dUp);
this.holderDiv.appendChild(this.dDown);
this.holderDiv.appendChild(this.startButton);
this.holderDiv.appendChild(this.backButton);
this.holderDiv.appendChild(this.buttonA);
this.holderDiv.appendChild(this.buttonB);
this.holderDiv.appendChild(this.buttonC);
this.holderDiv.appendChild(this.buttonD);
};
ControllerPad.prototype.rotationalControllerButtonSetup = function(hAdjust) {
this._configRotational("blue");
this.holderDiv.appendChild(this.rotationPad);
this.holderDiv.appendChild(this.startButton);
this.holderDiv.appendChild(this.backButton);
this.holderDiv.appendChild(this.buttonA);
this.holderDiv.appendChild(this.buttonB);
this.holderDiv.appendChild(this.dLeft);
this.holderDiv.appendChild(this.dRight);
this.holderDiv.appendChild(this.dUp);
this.holderDiv.appendChild(this.dDown);
};
ControllerPad.prototype._rotationalControllerButtonTakedown = function(hAdjust) {
this.holderDiv.removeChild(this.dLeft);
this.holderDiv.removeChild(this.dRight);
this.holderDiv.removeChild(this.dUp);
this.holderDiv.removeChild(this.dDown);
this.holderDiv.removeChild(this.startButton);
this.holderDiv.removeChild(this.backButton);
this.holderDiv.removeChild(this.buttonA);
this.holderDiv.removeChild(this.buttonB);
this.holderDiv.removeChild(this.rotationPad);
};
ControllerPad.prototype._directionalsControllerButtonTakedown = function(hAdjust) {
this.holderDiv.removeChild(this.dLeft);
this.holderDiv.removeChild(this.dRight);
this.holderDiv.removeChild(this.dUp);
this.holderDiv.removeChild(this.dDown);
this.holderDiv.removeChild(this.startButton);
this.holderDiv.removeChild(this.backButton);
this.holderDiv.removeChild(this.buttonA);
this.holderDiv.removeChild(this.buttonB);
this.holderDiv.removeChild(this.buttonC);
this.holderDiv.removeChild(this.buttonD);
};
ControllerPad.prototype.standardControllerButtonSetup = function(hAdjust) {
this._configStandard("blue");
this.holderDiv.appendChild(this.dLeft);
this.holderDiv.appendChild(this.dRight);
this.holderDiv.appendChild(this.dUp);
this.holderDiv.appendChild(this.dDown);
this.holderDiv.appendChild(this.startButton);
this.holderDiv.appendChild(this.backButton);
this.holderDiv.appendChild(this.buttonA);
this.holderDiv.appendChild(this.buttonB);
this.holderDiv.appendChild(this.buttonC);
this.holderDiv.appendChild(this.buttonD);
};
ControllerPad.prototype._standardControllerButtonTakedown = function(hAdjust) {
this.holderDiv.removeChild(this.dLeft);
this.holderDiv.removeChild(this.dRight);
this.holderDiv.removeChild(this.dUp);
this.holderDiv.removeChild(this.dDown);
this.holderDiv.removeChild(this.startButton);
this.holderDiv.removeChild(this.backButton);
this.holderDiv.removeChild(this.buttonA);
this.holderDiv.removeChild(this.buttonB);
this.holderDiv.removeChild(this.buttonC);
this.holderDiv.removeChild(this.buttonD);
};
ControllerPad.prototype.customControllerButtonSetup = function(leftp, rightp, upp, downp, startp,backp,ap,bp,cp,dp, directionButtonsWidth, directionButtonsHeight, startAndBackWidth, startAndBackHeight, buttonWidth, buttonHeight, totalWidth, totalHeight) {
if(leftp) {
this._customSpecs.leftp = leftp;
this._customSpecs.rightp = rightp;
this._customSpecs.upp = upp;
this._customSpecs.downp = downp;
this._customSpecs.startp = startp;
this._customSpecs.backp = backp;
this._customSpecs.ap = ap;
this._customSpecs.bp = bp;
this._customSpecs.cp = cp;
this._customSpecs.dp = dp;
this._customSpecs.directionButtonsWidth = directionButtonsWidth;
this._customSpecs.directionButtonsHeight = directionButtonsHeight;
this._customSpecs.startAndBackWidth = startAndBackWidth;
this._customSpecs.startAndBackHeight = startAndBackHeight;
this._customSpecs.buttonWidth = buttonWidth;
this._customSpecs.buttonHeight = buttonHeight;
}
this.customStyleTotalWidth = totalWidth; this.customStyleTotalHeight = totalHeight;
this._configCustom("blue");
this.holderDiv.appendChild(this.dLeft);
this.holderDiv.appendChild(this.dRight);
this.holderDiv.appendChild(this.dUp);
this.holderDiv.appendChild(this.dDown);
this.holderDiv.appendChild(this.startButton);
this.holderDiv.appendChild(this.backButton);
this.holderDiv.appendChild(this.buttonA);
this.holderDiv.appendChild(this.buttonB);
this.holderDiv.appendChild(this.buttonC);
this.holderDiv.appendChild(this.buttonD);
};
ControllerPad.prototype._customControllerButtonTakedown = function() {
this.holderDiv.removeChild(this.dLeft);
this.holderDiv.removeChild(this.dRight);
this.holderDiv.removeChild(this.dUp);
this.holderDiv.removeChild(this.dDown);
this.holderDiv.removeChild(this.startButton);
this.holderDiv.removeChild(this.backButton);
this.holderDiv.removeChild(this.buttonA);
this.holderDiv.removeChild(this.buttonB);
this.holderDiv.removeChild(this.buttonC);
this.holderDiv.removeChild(this.buttonD);
};
ControllerPad.prototype.assignStartAndBackMethods = function(startMethodString, backMethodString, methodObjectRef) {
ControllerPad.instance.removeEventListener("padStart", startMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("keyStart", startMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("touchStart", startMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("padBack", backMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("keyBack", backMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("touchBack", backMethodString, methodObjectRef);
ControllerPad.instance.addEventListener("padStart", startMethodString, methodObjectRef);
ControllerPad.instance.addEventListener("keyStart", startMethodString, methodObjectRef);
ControllerPad.instance.addEventListener("touchStart", startMethodString, methodObjectRef);
ControllerPad.instance.addEventListener("padBack", backMethodString, methodObjectRef);
ControllerPad.instance.addEventListener("keyBack", backMethodString, methodObjectRef);
ControllerPad.instance.addEventListener("touchBack", backMethodString, methodObjectRef);
};
ControllerPad.prototype.removeStartAndBackMethods = function(startMethodString, backMethodString, methodObjectRef) {
ControllerPad.instance.removeEventListener("padStart", startMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("keyStart", startMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("touchStart", startMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("padBack", backMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("keyBack", backMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("touchBack", backMethodString, methodObjectRef);
};
ControllerPad.prototype.destroy = function(startMethodString, backMethodString, methodObjectRef) {
if(ControllerPad.instance) {
if(ControllerPad.instance._style == 1) {
ControllerPad.instance._basicControllerButtonTakedown();
}
if(ControllerPad.instance._style == 2) {
ControllerPad.instance._directionalsControllerButtonTakedown();
}
if(ControllerPad.instance._style == 3) {
ControllerPad.instance._standardControllerButtonTakedown();
}
if(ControllerPad.instance._style == 4) {
ControllerPad.instance._customControllerButtonTakedown();
}
if(ControllerPad.instance.dLeft) {
ControllerPad.instance._unscriptDiv(ControllerPad.instance.dLeft);
}
if(ControllerPad.instance.dRight) {
ControllerPad.instance._unscriptDiv(ControllerPad.instance.dRight);
}
if(ControllerPad.instance.dUp) {
ControllerPad.instance._unscriptDiv(ControllerPad.instance.dUp);
}
if(ControllerPad.instance.dDown) {
ControllerPad.instance._unscriptDiv(ControllerPad.instance.dDown);
}
if(ControllerPad.instance.startButton) {
ControllerPad.instance._unscriptDiv(ControllerPad.instance.startButton);
}
if(ControllerPad.instance.backButton) {
ControllerPad.instance._unscriptDiv(ControllerPad.instance.backButton);
}
if(ControllerPad.instance.buttonA) {
ControllerPad.instance._unscriptDiv(ControllerPad.instance.buttonA);
}
if(ControllerPad.instance.buttonB) {
ControllerPad.instance._unscriptDiv(ControllerPad.instance.buttonB);
}
if(ControllerPad.instance.buttonC) {
ControllerPad.instance._unscriptDiv(ControllerPad.instance.buttonC);
}
if(ControllerPad.instance.buttonD) {
ControllerPad.instance._unscriptDiv(ControllerPad.instance.buttonD);
}
if(startMethodString && backMethodString && methodObjectRef) {
ControllerPad.instance.removeEventListener("padStart", startMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("keyStart", startMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("touchStart", startMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("padBack", backMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("keyBack", backMethodString, methodObjectRef);
ControllerPad.instance.removeEventListener("touchBack", backMethodString, methodObjectRef);
}
if(ControllerPad.instance.canvasObject) {
ControllerPad.instance.canvasObject.canvas.removeEventListener("touchstart", ControllerPad.instance.dispatch, false);
ControllerPad.instance.canvasObject.canvas.removeEventListener("touchend", ControllerPad.instance.dispatch, false);
ControllerPad.instance.canvasObject.canvas.removeEventListener("touchmove", ControllerPad.instance.dispatch, false);
ControllerPad.instance.canvasObject = null;
}
window.removeEventListener("keydown", ControllerPad.instance.handleKeys, false);
window.removeEventListener("keyup", ControllerPad.instance.releaseKeys, false);
ControllerPad.instance = null;
}
};
ControllerPad.prototype._dontAbsolutePositionHolder = 0;
/*
* Default style is 'standardController' see ControllerPad.css
* The other built in style option is 'directionalsController'
* The ControllerPad.css stylesheet needs to be applied to the page before this method is called.
*
* @param w The width the controller should be, it will get scaled down if smaller than default
* @param h The height the controller should be, both w and h must be defined, otherwise it uses the default w/h.
* @param style The style id string, to define a custom style use the addCustomImag method and pass in your customStyleName here for this param.
* @param styleOriginalWidth
* @param styleOriginalHeight
* @memberof ControllerPad
*/
ControllerPad.prototype.show = function(w,h,style,styleOriginalWidth,styleOriginalHeight) {
if(this._style != -1) { //one of the setup methods must be called to establish inital style.
var sty; //select style, premade or custom string
if(style && typeof style == "number") {
sty = (style == 2 ? "directionalsController" : (style == 1 ? "basicController" : "standardController") );
} else {
sty = style || "standardController";
if(sty != "standardController") {
this._style = 4; //custom;
}
}
if(this._style != 4 && this._style != 5) {
this._style = (sty == "basicController" ? 1 : (sty == "directionalsController" ? 2 : 3));
}
if(this._style == 1) {
styleOriginalWidth = 640;
styleOriginalHeight = 144;
}
if(this._style == 2) {
styleOriginalWidth = 640;
styleOriginalHeight = 191;
}
if(this._style == 3) {
styleOriginalWidth = 737;
styleOriginalHeight = 144;
}
if(this._style == 5) {
sty = "rotationController";
styleOriginalWidth = 737;
styleOriginalHeight = 192;
}
if(!styleOriginalWidth) {
styleOriginalWidth = this.customStyleTotalWidth;
styleOriginalHeight = this.customStyleTotalHeight;
}
//ready the holder div to display background and hold button divs
if(!this._holderDivParent.contains(this.holderDiv)) {
this._holderDivParent.appendChild(this.holderDiv);
}
//assign the holder div an id to style it with the controllers image as its background, see ControllerPad.css.
this.holderDiv.setAttribute("id", sty);
var toposition = this._dontAbsolutePositionHolder ? "" : "position:absolute;";
//set the holder div to the actual width and height of the image
this.holderDiv.setAttribute("style", toposition+"width:"+styleOriginalWidth+"px;height:"+styleOriginalHeight+"px");
//alert("ffo");
//now scale the holderDiv to the page, relative to the known width and height of the image,
//and the given width height desired.
//passing bigger or smaller w,h values will stretch/scale the image more
var scaleX = ((w / window.innerWidth) * (w / styleOriginalWidth) * 10);
var scaleY = ((h / window.innerHeight) * (h / styleOriginalHeight) * 10);
var xOffset = ( w / window.innerWidth ) * 10;
var yOffset = ( h / window.innerHeight ) * 10;
//all the internal button divs will scale as well, (which does not happen correctly without transformations)
this.holderDiv.style.transformOrigin = "0 0";
this.holderDiv.style.transform = "scale(" + scaleX/xOffset + " , " + scaleY/yOffset + ")";
this.hideButtons();
}
};
ControllerPad.prototype.hide = function() {
if(this._holderDivParent && this._holderDivParent.contains(this.holderDiv)) {
try { this._holderDivParent.removeChild(this.holderDiv); } catch (e) {
window.console.log("caught e during controller.hide: " + e +" most likely the controller is in a whole bunch of other divs and html");
}
}
};
ControllerPad.prototype.keyEstablish = function() {
window.removeEventListener("keydown", this.handleKeys, false);
window.removeEventListener("keyup", this.releaseKeys, false);
window.addEventListener("keydown", this.handleKeys, false);
window.addEventListener("keyup", this.releaseKeys, false);
};
ControllerPad.prototype._configBasic = function(str, prs) { this._style = 1;
this.dLeft.setAttribute("style", "position:absolute;left:16px;top:16px;width:96px;height:112px;background:"+ (prs ? (this.buttons.left ? str : "transparent") : str) );
this.dRight.setAttribute("style", "position:absolute;left:128px;top:16px;width:96px;height:112px;background:"+ (prs ? (this.buttons.right ? str : "transparent") : str) );
this.startButton.setAttribute("style", "position:absolute;left:240px;top:16px;width:96px;height:48px;background:"+ (prs ? (this.buttons.start ? str : "transparent") : str) );
this.backButton.setAttribute("style", "position:absolute;left:240px;top:80px;width:96px;height:48px;background:"+ (prs ? (this.buttons.back ? str : "transparent") : str) );
this.buttonA.setAttribute("style", "position:absolute;left:352px;top:16px;width:80px;height:100px;background:"+ (prs ? (this.buttons.a ? str : "transparent") : str) );
this.buttonB.setAttribute("style", "position:absolute;left:448px;top:16px;width:80px;height:100px;background:"+ (prs ? (this.buttons.b ? str : "transparent") : str) );
this.buttonC.setAttribute("style", "position:absolute;left:544px;top:16px;width:80px;height:100px;background:"+ (prs ? (this.buttons.c ? str : "transparent") : str) );
};
ControllerPad.prototype._configDirectional = function(str, prs) { this._style = 2;
this.dLeft.setAttribute("style", "position:absolute;left:33px;top:63px;width:64px;height:64px;background:"+ (prs ? (this.buttons.left ? str : "transparent") : str) );
this.dRight.setAttribute("style", "position:absolute;left:161px;top:63px;width:64px;height:64px;background:"+ (prs ? (this.buttons.right ? str : "transparent") : str) );
this.dUp.setAttribute("style", "position:absolute;left:96px;top:2px;width:64px;height:64px;background:"+ (prs ? (this.buttons.up ? str : "transparent") : str) );
this.dDown.setAttribute("style", "position:absolute;left:96px;top:128px;width:64px;height:64px;background:"+ (prs ? (this.buttons.down ? str : "transparent") : str) );
this.startButton.setAttribute("style", "position:absolute;left:272px;top:32px;width:96px;height:48px;background:"+ (prs ? (this.buttons.start ? str : "transparent") : str) );
this.backButton.setAttribute("style", "position:absolute;left:272px;top:112px;width:96px;height:48px;background:"+ (prs ? (this.buttons.back ? str : "transparent") : str) );
this.buttonA.setAttribute("style", "position:absolute;left:416px;top:65px;width:64px;height:64px;background:"+ (prs ? (this.buttons.a ? str : "transparent") : str) );
this.buttonB.setAttribute("style", "position:absolute;left:480px;top:2px;width:64px;height:64px;background:"+ (prs ? (this.buttons.b ? str : "transparent") : str) );
this.buttonC.setAttribute("style", "position:absolute;left:544px;top:62px;width:64px;height:64px;background:"+ (prs ? (this.buttons.c ? str : "transparent") : str) );
this.buttonD.setAttribute("style", "position:absolute;left:480px;top:128px;width:64px;height:64px;background:"+ (prs ? (this.buttons.d ? str : "transparent") : str) );
};
ControllerPad.prototype._configRotational = function(str, prs) { this._style = 5;
this.rotationPad.setAttribute("style","position:absolute;left:32px;top:16px;width:160px;height:160px;background:"+ (prs ? (this.buttons.start ? str : "transparent") : str) );
this.startButton.setAttribute("style", "position:absolute;left:224px;top:32px;width:96px;height:48px;background:"+ (prs ? (this.buttons.start ? str : "transparent") : str) );
this.backButton.setAttribute("style", "position:absolute;left:224px;top:112px;width:96px;height:48px;background:"+ (prs ? (this.buttons.back ? str : "transparent") : str) );
this.buttonA.setAttribute("style", "position:absolute;left:368px;top:32px;width:96px;height:48px;background:"+str);
this.buttonB.setAttribute("style", "position:absolute;left:368px;top:112px;width:96px;height:48px;background:"+str);
this.dLeft.setAttribute("style", "position:absolute;left:504px;top:65px;width:64px;height:64px;background:"+ (prs ? (this.buttons.a ? str : "transparent") : str) );
this.dUp.setAttribute("style", "position:absolute;left:568px;top:2px;width:64px;height:64px;background:"+ (prs ? (this.buttons.b ? str : "transparent") : str) );
this.dRight.setAttribute("style", "position:absolute;left:632px;top:62px;width:64px;height:64px;background:"+ (prs ? (this.buttons.c ? str : "transparent") : str) );
this.dDown.setAttribute("style", "position:absolute;left:568px;top:128px;width:64px;height:64px;background:"+ (prs ? (this.buttons.d ? str : "transparent") : str) );
};
ControllerPad.prototype._configStandard = function(str, prs) { this._style = 3;
this.dLeft.setAttribute("style", "position:absolute;left:21px;top:42px;width:50px;height:50px;background:"+str);
this.dRight.setAttribute("style", "position:absolute;left:167px;top:42px;width:50px;height:50px;background:"+str);
this.dUp.setAttribute("style", "position:absolute;left:91px;top:8px;width:50px;height:50px;background:"+str);
this.dDown.setAttribute("style", "position:absolute;left:90px;top:85px;width:50px;height:50px;background:"+str);
this.startButton.setAttribute("style", "position:absolute;left:240px;top:16px;width:96px;height:48px;background:"+str);
this.backButton.setAttribute("style", "position:absolute;left:240px;top:80px;width:96px;height:48px;background:"+str);
this.buttonA.setAttribute("style", "position:absolute;left:352px;top:16px;width:80px;height:100px;background:"+str);
this.buttonB.setAttribute("style", "position:absolute;left:448px;top:16px;width:80px;height:100px;background:"+str);
this.buttonC.setAttribute("style", "position:absolute;left:544px;top:16px;width:80px;height:100px;background:"+str);
this.buttonD.setAttribute("style", "position:absolute;left:644px;top:16px;width:80px;height:100px;background:"+str);
};
ControllerPad.prototype._configCustom = function(str, prs) { this._style = 4;
this.dLeft.setAttribute("style", "position:absolute;left:"+this._customSpecs.leftp.x+"px;top:"+this._customSpecs.leftp.y+"px;width:"+this._customSpecs.directionButtonsWidth+"px;height:"+this._customSpecs.directionButtonsHeight+"px;background:"+ (prs ? (this.buttons.left ? str : "transparent") : str) );
this.dRight.setAttribute("style", "position:absolute;left:"+this._customSpecs.rightp.x+"px;top:"+this._customSpecs.rightp.y+"px;width:"+this._customSpecs.directionButtonsWidth+"px;height:"+this._customSpecs.directionButtonsHeight+"px;background:"+ (prs ? (this.buttons.right ? str : "transparent") : str) );
this.dUp.setAttribute("style", "position:absolute;left:"+this._customSpecs.upp.x+"px;top:"+this._customSpecs.upp.y+"px;width:"+this._customSpecs.directionButtonsWidth+"px;height:"+this._customSpecs.directionButtonsHeight+"px;background:"+ (prs ? (this.buttons.up ? str : "transparent") : str) );
this.dDown.setAttribute("style", "position:absolute;left:"+this._customSpecs.downp.x+"px;top:"+this._customSpecs.downp.y+"px;width:"+this._customSpecs.directionButtonsWidth+"px;height:"+this._customSpecs.directionButtonsHeight+"px;background:"+ (prs ? (this.buttons.down ? str : "transparent") : str) );
this.startButton.setAttribute("style", "position:absolute;left:"+this._customSpecs.startp.x+"px;top:"+this._customSpecs.startp.y+"px;width:"+this._customSpecs.startAndBackWidth+"px;height:"+this._customSpecs.startAndBackHeight+"px;background:"+ (prs ? (this.buttons.start ? str : "transparent") : str) );
this.backButton.setAttribute("style", "position:absolute;left:"+this._customSpecs.backp.x+"px;top:"+this._customSpecs.backp.y+"px;width:"+this._customSpecs.startAndBackWidth+"px;height:"+this._customSpecs.startAndBackHeight+"px;background:"+ (prs ? (this.buttons.back ? str : "transparent") : str) );
this.buttonA.setAttribute("style", "position:absolute;"+this._addedButtonRectStyle+"left:"+this._customSpecs.ap.x+"px;top:"+this._customSpecs.ap.y+"px;width:"+this._customSpecs.buttonWidth+"px;height:"+this._customSpecs.buttonHeight+"px;background:"+ (prs ? (this.buttons.a ? str : "transparent") : str) );
this.buttonB.setAttribute("style", "position:absolute;"+this._addedButtonRectStyle+"left:"+this._customSpecs.bp.x+"px;top:"+this._customSpecs.bp.y+"px;width:"+this._customSpecs.buttonWidth+"px;height:"+this._customSpecs.buttonHeight+"px;background:"+ (prs ? (this.buttons.b ? str : "transparent") : str) );
this.buttonC.setAttribute("style", "position:absolute;"+this._addedButtonRectStyle+"left:"+this._customSpecs.cp.x+"px;top:"+this._customSpecs.cp.y+"px;width:"+this._customSpecs.buttonWidth+"px;height:"+this._customSpecs.buttonHeight+"px;background:"+ (prs ? (this.buttons.c ? str : "transparent") : str) );
this.buttonD.setAttribute("style", "position:absolute;"+this._addedButtonRectStyle+"left:"+this._customSpecs.dp.x+"px;top:"+this._customSpecs.dp.y+"px;width:"+this._customSpecs.buttonWidth+"px;height:"+this._customSpecs.buttonHeight+"px;background:"+ (prs ? (this.buttons.d ? str : "transparent") : str) );
};
ControllerPad.prototype.__recurse = -1;
ControllerPad.prototype._buttonRectColor = "#6495edbb";
ControllerPad.prototype._addedButtonRectStyle = "";
ControllerPad.prototype.showButtons = function(ifPressed) {
if(this.__recurse == -1 && ifPressed) {
this.__recurse = 1;
}
if(ifPressed == -1) { this.__recurse = -1; return; }
if(this._style == 1) {
this._configBasic(this._buttonRectColor, ifPressed || 0);
}
if(this._style == 2) {
this._configDirectional(this._buttonRectColor, ifPressed || 0);
}
if(this._style == 3) {
this._configStandard(this._buttonRectColor, ifPressed || 0);
}
if(this._style == 4) {
this._configCustom(this._buttonRectColor, ifPressed || 0);
}
};
ControllerPad.prototype.hideButtons = function() {
if(this._style == 1) {
this._configBasic("transparent");
}
if(this._style == 2) {
this._configDirectional("transparent");
}
if(this._style == 3) {
this._configStandard("transparent");
}
if(this._style == 4) {
this._configCustom("transparent");
}
if(this._style == 5) {
this._configRotational("transparent");
}
};
ControllerPad.prototype.dispatch = function(e) {
if(e) e.preventDefault();
if(e.type == "touchstart" || e.type == "pointerdown") {
ControllerPad.instance.handleTouches(e);
if(ControllerPad.instance.__recurse >= 1) { ControllerPad.instance.showButtons(1); }
return;
}
if(e.type == "touchend" || e.type == "pointerup") {
ControllerPad.instance.releaseTouches(e);
if(ControllerPad.instance.__recurse === 1) { ControllerPad.instance.showButtons(1); }
return;
}
if(e.type == "touchmove" || e.type == "pointermove") {
ControllerPad.instance.changeTouches(e);
if(ControllerPad.instance.__recurse === 1) { ControllerPad.instance.showButtons(1); }
return;
}
if(e.type == "mousedown") {
ControllerPad.instance.handleTouches(e);
if(ControllerPad.instance.__recurse >= 1) { ControllerPad.instance.showButtons(1); }
return;
}
if(e.type == "mouseup") {
ControllerPad.instance.releaseTouches(e);
if(ControllerPad.instance.__recurse === 1) { ControllerPad.instance.showButtons(1); }
return;
}
};
ControllerPad.prototype.changeTouches = function(e) {
e.preventDefault();//touchmove
var tx,ty,i,touches;
if(e && e.targetTouches && e.targetTouches.length) {
//var i = 0;
//for (i; i < e.targetTouches.length; i++) {
//var touches = e.targetTouches[i];
//var tx = touches.pageX * tabageos.MouseController._oX;//oX/Y defined during resize
//var ty = touches.pageY * tabageos.MouseController._oY;
//var tpoint = new tabageos.MoverPoint(tx,ty); var tstri = "";
if(this._style == 5 && e.target == this.rotationPad) {
i = 0;
for (i; i < e.targetTouches.length; i++) {
touches = e.targetTouches[i];
tx = touches.pageX * tabageos.MouseController._oX;//oX/Y defined during resize
ty = touches.pageY * tabageos.MouseController._oY;
}
//window.console.log([tx,ty]);
this.rotateWithPoint(tx,ty);
}
if(e.target == this.dRight) {
this.buttons.right = 1; this.buttons.left = 0;
}
if(e.target == this.dLeft) {
this.buttons.left = 1; this.buttons.right = 0;
}
if(e.target == this.dUp) {
this.buttons.up = 1; this.buttons.down = 0;
}
if(e.target == this.dDown) {
this.buttons.down = 1; this.buttons.up = 0;
}
if(e.target == this.buttonA) {
this.buttons.a = 1; this.buttons.b = 0; this.buttons.c = 0; this.buttons.d = 0;
}
if(e.target == this.buttonB) {
this.buttons.b = 1; this.buttons.c = 0; this.buttons.a = 0;this.buttons.d = 0;
}
if(e.target == this.buttonC) {
this.buttons.c = 1; this.buttons.b = 0; this.buttons.a = 0;this.buttons.d = 0;
}
if(e.target == this.buttonD) {
this.buttons.c = 0; this.buttons.b = 0; this.buttons.a = 0; this.buttons.d = 1;
}
if(e.target == this.startButton) {
//if(this.buttons.a == 0 && this.buttons.right == 0 && this.buttons.left == 0) { this.buttons.start = 1; tstri = "Start"; }
}
if(e.target == this.backButton) {
//if(this.buttons.a == 0 && this.buttons.right == 0 && this.buttons.left == 0) { this.buttons.back = 1; tstri = "Back"; }
}
//}
} else if (e && e.pointerType && e.pointerType == "touch") {
if(this._style == 5 && e.target == this.rotationPad) {
tx = e.pageX * tabageos.MouseController._oX;
ty = e.pageY * tabageos.MouseController._oY;
this.rotateWithPoint(tx,ty);
}
if(e.target == this.dRight) {
this.buttons.right = 1; this.buttons.left = 0;
}
if(e.target == this.dLeft) {
this.buttons.left = 1; this.buttons.right = 0;
}
if(e.target == this.dUp) {
this.buttons.up = 1; this.buttons.down = 0;
}
if(e.target == this.dDown) {
this.buttons.down = 1; this.buttons.up = 0;
}
if(e.target == this.buttonA) {
this.buttons.a = 1; this.buttons.b = 0; this.buttons.c = 0; this.buttons.d = 0;
}
if(e.target == this.buttonB) {
this.buttons.b = 1; this.buttons.c = 0; this.buttons.a = 0;this.buttons.d = 0;
}
if(e.target == this.buttonC) {
this.buttons.c = 1; this.buttons.b = 0; this.buttons.a = 0;this.buttons.d = 0;
}
if(e.target == this.buttonD) {
this.buttons.c = 0; this.buttons.b = 0; this.buttons.a = 0; this.buttons.d = 1;
}
if(e.target == this.startButton) {
//if(this.buttons.a == 0 && this.buttons.right == 0 && this.buttons.left == 0) { this.buttons.start = 1; tstri = "Start"; }
}
if(e.target == this.backButton) {
//if(this.buttons.a == 0 && this.buttons.right == 0 && this.buttons.left == 0) { this.buttons.back = 1; tstri = "Back"; }
}
}
};
ControllerPad.prototype.handleTouches = function(e) {
e.preventDefault();var tstri = "";//touchstart
if((e && e.targetTouches && e.targetTouches.length) || e.type == "mousedown" || (e.pointerType && e.pointerType == "touch")) {
if(e.target == this.rotationPad && e.type == "touchstart" && this.buttons.start == 1) {
var i = 0;
for (i; i < e.targetTouches.length; i++) {
var touches = e.targetTouches[i];
var tx = touches.pageX * tabageos.MouseController._oX;//oX/Y defined during resize
var ty = touches.pageY * tabageos.MouseController._oY;
this.centerRotationX = tx+1-1;
this.centerRotationY = ty+1-1;
}
}
if(e.target == this.dRight) {
this.buttons.right = 1; this.buttons.left = 0;
}
if(e.target == this.dLeft) {
this.buttons.left = 1; this.buttons.right = 0;
}
if(e.target == this.dUp) {
this.buttons.up = 1; this.buttons.down = 0;
}
if(e.target == this.dDown) {
this.buttons.down = 1; this.buttons.up = 0;
}
if(e.target == this.buttonA) {
this.buttons.a = 1;this.buttons.b = 0;this.buttons.c = 0;this.buttons.d = 0;
}
if(e.target == this.buttonB) {
this.buttons.b = 1; this.buttons.c = 0;this.buttons.d = 0;this.buttons.a = 0;
}
if(e.target == this.buttonC) {
this.buttons.c = 1; this.buttons.b = 0;this.buttons.a = 0;this.buttons.d = 0;
}
if(e.target == this.buttonD) {
this.buttons.d = 1; this.buttons.b = 0; this.buttons.c = 0;this.buttons.a = 0;
}
if(e.target == this.startButton) {
if(this.buttons.a == 0 && this.buttons.right == 0 && this.buttons.left == 0) { this.buttons.start = 1; tstri = "Start"; }
}
if(e.target == this.backButton) {
if(this.buttons.a == 0 && this.buttons.right == 0 && this.buttons.left == 0) { this.buttons.back = 1; tstri = "Back"; }
}
}
if(tstri != "") { ControllerPad.instance.dispatchEvent(new tabageos.Event("touch"+tstri)); }
};
ControllerPad.prototype.addedRotationX = 0;
ControllerPad.prototype.addedRotationY = 0;
/**
*
* The center x of the rotation pads circle on the whole screen.
* For example, if the game has a width of 496, the middle of the rotation circle at full screen is about 80.
* If the game is not full screen you will need to calculate where the game edge starts and count from there plus about 80.
* This number should define the middle x of the rotation circle image when the controller is shown.
* This is pretty much a number arrived at by trial and error, unless the game is full screen.
* The controller should generally work in dev tools with your finger able to rotate around the black part of the rotation circle.
*
* For example:
* var fif = window.innerWidth/2;//50%
* var containerleft = fif - 248;//50% - half game width is what the container is positioned at.
*
* controller.centerRotationX = containerleft + 80;
*
*/
ControllerPad.prototype.centerRotationX = 360;
/**
*
*
* The center y of the rotation pads circle on the whole screen.
* see centerRotationX
*
*/
ControllerPad.prototype.centerRotationY = 360;
//for rotation the principle is to get the middle point and use atan2() * 180 / pi.
//the other things making this possible are in the MouseController Class and the static tabageos.Resize method. Together they translate the touch point into a point within gameWidth/Height.
//when set up for capturing rotation, the controller calls rotateWithPoint on touchmove with the translate cords.
//the Rotating[Traveler/Shooter] Classes then need only use .setRotation(controller.rotation) to rotate via the controller.
ControllerPad.prototype.rotateWithPoint = function(x,y, addedX, addedY, centerX,centerY) {
this.rotation = Math.atan2((y + (addedY || this.addedRotationY)) - (centerY||this.centerRotationY), (x + (addedX || this.addedRotationX)) - (centerX||this.centerRotationX)) * 180 / Math.PI;
};
ControllerPad.prototype.releaseTouches = function(e) {
e.preventDefault();//touch end
//var touches = e.changedTouches;
//var tx = touches[0].pageX * tabageos.MouseController._oX;//oX/Y defined during resize
//var ty = touches[0].pageY * tabageos.MouseController._oY;
//var tpoint = new tabageos.MoverPoint(tx,ty);
if(e.target == this.dRight) {
this.buttons.right = 0;
}
if(e.target == this.dLeft) {
this.buttons.left = 0;
}
if(e.target == this.dUp) {
this.buttons.up = 0;
}
if(e.target == this.dDown) {
this.buttons.down = 0;
}
if(e.target == this.buttonA) {
this.buttons.a = 0;
}
if(e.target == this.buttonB) {
this.buttons.b = 0;
}
if(e.target == this.buttonC) {
this.buttons.c = 0;
}
if(e.target == this.buttonD) {
this.buttons.d = 0;
}
this.buttons.start = 0; this.buttons.back = 0;
};
/*
* For override, called before keyCode checks during handleKeys.
* let's say you wanted users to be able to use either wasd or arrows
* then you can overide ._preHandleKeys and do for example;
* if(e.keyCode == 39 || e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 40) {
* controller.keyboardEquivalents = controller.arrows;
* } else {
* controller.keyboardEquivalents = controller.wasd;
* }
* or you wanted the keyboard equivalents to change based on the level
* or some other factor, then this overideable blank function is for that
* it gets called just before any checks of keyboardEquivalents are done.
*
* If you want both WASD and Arrows to be used, you can also
* just call .acceptWASDAndArrows, which overrides ._preHandleKeys for you.
*
*/
ControllerPad.prototype._preHandleKeys = function(e) {
};
ControllerPad.prototype.acceptWASDAndArrows = function() {
this._preHandleKeys = function(e) {
if(e.keyCode == 39 || e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 40) {
this.keyboardEquivalents = (this._style != 2 ? this.basicArrows : this.arrows);
} else {
this.keyboardEquivalents = (this._style != 2 ? this.basicWasd : this.wasd);
}
};
};
ControllerPad.prototype.handleKeys = function(e) {
if (typeof e == 'undefined') e = window.event;
if([37,38,39,40,32].indexOf(e.keyCode) != -1) {
e.preventDefault();
}
//var oe = new tabageos.Event("touchstart");