-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnaplps.js
1658 lines (1446 loc) · 48.6 KB
/
naplps.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";
/*
+ + + NAPLPS for JavaScript + + +
+ + + (part of the TelidonP5 Project) + + +
+ + + Nick Fox-Gieg https://fox-gieg.com + + +
Note: Uses no p5.js-specific code
Binary utilities adapted from the methods in:
https://github.com/processing-js/processing-js
*/
/*
* binary()
* Converts a byte, char, int, or color to a String containing the equivalent binary
* notation. For example color(0, 102, 153, 255) will convert to the String
* "11111111000000000110011010011001".
*
* @param {byte|char|int|color} num byte, char, int, color: value to convert
* @param {int} numBits number of digits to return
* @returns {String}
*/
function binary(num, numBits) {
let bit;
if (numBits > 0) {
bit = numBits;
} else if(num instanceof Char) {
bit = 16;
num |= 0; // making it int
} else {
// autodetect, skipping zeros
bit = 32;
while (bit > 1 && !((num >>> (bit - 1)) & 1)) {
bit--;
}
}
let result = "";
while (bit > 0) {
result += ((num >>> (--bit)) & 1) ? "1" : "0";
}
return result;
}
/*
* unbinary()
* Converts a String representation of a binary number to its equivalent integer value.
* For example, unbinary("00001000") will return 8.
*
* @param {String} binaryString String
* @returns {Int}
*/
function unbinary(binaryString) {
let i = binaryString.length - 1, mask = 1, result = 0;
while (i >= 0) {
let ch = binaryString[i--];
if (ch !== '0' && ch !== '1') {
throw "the value passed into unbinary was not an 8 bit binary number";
}
if (ch === '1') {
result += mask;
}
mask <<= 1;
}
return result;
}
function unbinaryToFloat(binaryString) {
let bytes = unbinary(binaryString);
var sign = (bytes & 0x80000000) ? -1 : 1;
var exponent = ((bytes >> 23) & 0xFF) - 127;
var significand = (bytes & ~(-1 << 23));
if (exponent == 128)
return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);
if (exponent == -127) {
if (significand == 0) return sign * 0.0;
exponent = -126;
significand /= (1 << 22);
} else significand = (significand | (1 << 23)) / (1 << 23);
return sign * significand * Math.pow(2, exponent);
}
function decimalToHex(d, padding) {
//if there is no padding value added, default padding to 8 else go into while statement.
padding = (padding === undefined || padding === null) ? padding = 8 : padding;
if (d < 0) {
d = 0xFFFFFFFF + d + 1;
}
let hex = Number(d).toString(16).toUpperCase();
while (hex.length < padding) {
hex = "0" + hex;
}
if (hex.length >= padding) {
hex = hex.substring(hex.length - padding, hex.length);
}
return hex;
}
/*
* hex()
* Converts a byte, char, int, or color to a String containing the equivalent hexadecimal notation.
* For example color(0, 102, 153, 255) will convert to the String "FF006699".
*
* Note: since we cannot keep track of byte, int types by default the returned string is 8 chars long
* if no 2nd argument is passed. closest compromise we can use to match java implementation Feb 5 2010
* also the char parser has issues with chars that are not digits or letters IE: !@#$%^&*
*
* @param {byte|char|int|Color} value the value to turn into a hex string
* @param {int} digits the number of digits to return
* @returns {String}
*/
function hex(value, len) {
if (arguments.length === 1) {
if (value instanceof Char) {
len = 4;
} else { // int or byte, indistinguishable at the moment, default to 8
len = 8;
}
}
return decimalToHex(value, len);
}
function unhexScalar(hex) {
let value = parseIntAlt("0x" + hex, 16);
// correct for int overflow java expectation
if (value > 2147483647) {
value -= 4294967296;
}
return value;
}
/*
* unhex()
* Converts a String representation of a hexadecimal number to its equivalent integer value.
*
* @param {String} hex the hex string to convert to an int
* @returns {int}
*/
function unhex(hex) {
if (hex instanceof Array) {
let arr = [];
for (let i = 0; i < hex.length; i++) {
arr.push(unhexScalar(hex[i]));
}
return arr;
}
return unhexScalar(hex);
}
function remap(value, min1, max1, min2, max2) {
let range1 = max1 - min1;
let range2 = max2 - min2;
let valueScaled = (value - min1) / range1;
return min2 + (valueScaled * range2);
}
function getDistance(v1, v2) {
if (v1.z !== undefined && v2.z !== undefined) {
return Math.sqrt((v1.x - v2.x)**2 + (v1.y - v2.y)**2 + (v1.z - v2.z)**2);
} else {
return Math.sqrt((v1.x - v2.x)**2 + (v1.y - v2.y)**2);
}
}
function parseIntAlt(val, radix) {
if (val instanceof Array) {
let ret = [];
for (let i = 0; i < val.length; i++) {
if (typeof val[i] === 'string' && !/^\s*[+\-]?\d+\s*$/.test(val[i])) {
ret.push(0);
} else {
ret.push(intScalar(val[i], radix));
}
}
return ret;
}
return intScalar(val, radix);
}
function intScalar(val, radix) {
if (typeof val === 'number') {
return val & 0xFFFFFFFF;
}
if (typeof val === 'boolean') {
return val ? 1 : 0;
}
if (typeof val === 'string') {
let number = parseIntAlt(val, radix || 10); // Default to decimal radix.
return number & 0xFFFFFFFF;
}
if (val instanceof Char) {
return val.code;
}
}
function removeCharAt(s, index) { // string, int
let returns = "";
for (let i=0; i<s.length; i++) {
if (i != index) returns += s.charAt(i);
}
return returns;
}
function doEncode(input) {
input = input.charAt(input.length-2) + input.charAt(input.length-1);
let returns = "";
for (let i = 0; i < input.length; i += 2) {
returns += String.fromCharCode(parseInt(input.substr(i, 2), 16));
}
return returns;
}
function floatToBinary(input) {
let buffer = new ArrayBuffer(4);
let intView = new Int32Array(buffer);
let floatView = new Float32Array(buffer);
floatView[0] = input;
return intView[0].toString(2); // bits of the 32 bit float
}
function intToBinary(input) {
let buffer = new ArrayBuffer(4);
let intView = new Int32Array(buffer);
let floatView = new Float32Array(buffer);
intView[0] = parseInt(input);
return intView[0].toString(2); // bits of the 32 bit float
}
class Char {
constructor(chr) {
if (typeof chr === 'string' && chr.length === 1) {
this.code = chr.charCodeAt(0);
} else if (typeof chr === 'number') {
this.code = chr;
} else if (chr instanceof Char) {
this.code = chr;
} else {
this.code = NaN;
}
return this.code;
}
toString() {
return String.fromCharCode(this.code);
}
valueOf() {
return this.code;
}
}
class Vector2 {
constructor(_x, _y) {
this.x = _x;
this.y = _y;
}
add(input) {
this.x += input.x;
this.y += input.y;
return new Vector2(this.x, this.y);
}
sub(input) {
this.x -= input.x;
this.y -= input.y;
return new Vector2(this.x, this.y);
}
mul(input) {
this.x *= input.x;
this.y *= input.y;
return new Vector2(this.x, this.y);
}
div(input) {
this.x /= input.x;
this.y /= input.y;
return new Vector2(this.x, this.y);
}
}
class Vector3 {
constructor(_x, _y, _z) {
this.x = _x;
this.y = _y;
this.z = _z;
}
add(input) {
this.x += input.x;
this.y += input.y;
this.z += input.z;
return new Vector3(this.x, this.y, this.z);
}
sub(input) {
this.x -= input.x;
this.y -= input.y;
this.z -= input.z;
return new Vector3(this.x, this.y, this.z);
}
mul(input) {
this.x *= input.x;
this.y *= input.y;
this.z *= input.z;
return new Vector3(this.x, this.y, this.z);
}
div(input) {
this.x /= input.x;
this.y /= input.y;
this.z /= input.z;
return new Vector3(this.x, this.y, this.z);
}
}
const naplps_black = new Vector3(0, 0, 0);
const naplps_gray1 = new Vector3(32, 32, 32);
const naplps_gray2 = new Vector3(64, 64, 64);
const naplps_gray3 = new Vector3(96, 96, 96);
const naplps_gray4 = new Vector3(128, 128, 128);
const naplps_gray5 = new Vector3(160, 160, 160);
const naplps_gray6 = new Vector3(192, 192, 192);
const naplps_gray7 = new Vector3(224, 224, 224);
const naplps_blue = new Vector3(0, 0, 255); // index 60
const naplps_blue_magenta = new Vector3(5*36, 0, 7*36);
const naplps_pinkish_red = new Vector3(7*36, 0, 4*36);
const naplps_orange_red = new Vector3(7*36, 2*36, 0);
const naplps_yellow = new Vector3(255, 255, 0);
const naplps_yellow_green = new Vector3(2*36, 7*36, 0);
const naplps_greenish = new Vector3(0, 7*36, 4*36);
const naplps_bluegreen = new Vector3(0, 5*36, 7*36);
const naplps_white = new Vector3(255, 255, 255); // not part of the default palette
const naplps_defaultColorMap = [ naplps_black, naplps_gray1, naplps_gray2, naplps_gray3, naplps_gray4, naplps_gray5, naplps_gray6, naplps_gray7, naplps_blue, naplps_blue_magenta, naplps_pinkish_red, naplps_orange_red, naplps_yellow, naplps_yellow_green, naplps_greenish, naplps_bluegreen ];
const naplps_defaultColorIndices1 = [ "40", "44", "49", "4D", "52", "56", "5B", "5F", "60", "64", "68", "6C", "70", "74", "78", "7C"];
const naplps_defaultColorIndices2 = [ "40", "60", "40", "60", "50", "70", "50", "70", "40", "40", "40", "40", "40", "40", "40", "40"];
// + + + D E C O D E R + + +
let naplps_drawingCursor = new Vector2(0.0, 0.0);
let naplps_colorMap = [ naplps_black, naplps_gray1, naplps_gray2, naplps_gray3, naplps_gray4, naplps_gray5, naplps_gray6, naplps_gray7, naplps_blue, naplps_blue_magenta, naplps_pinkish_red, naplps_orange_red, naplps_yellow, naplps_yellow_green, naplps_greenish, naplps_bluegreen ];
let naplps_colorMode = 0;
let naplps_lastColor = naplps_white;
let naplps_lastIndex = 0;
let naplps_backgroundColor = naplps_black;
let naplps_drawBackground = true;
let naplps_singleValLength = 1;
let naplps_multiValLength = 3;
let naplps_minVal = 40; // 64
let naplps_is3D = false;
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// 1. SINGLE-BYTE data classes
//
// 1.1. The NapChar class is the smallest component of a NAPLPS file.
// It just contains a few methods for decoding one byte.
class NapChar {
constructor(_c) {
this.c = _c; // char or string
this.ascii = this.getAscii(); // int
this.binary = this.getBinary(); // string
this.rbinary = this.getRBinary(); // string
this.hex = this.getHex(); // string
}
getAscii() {
return new Char(this.c);
}
getBinary() {
let returns = "";
let b = binary(this.ascii);
for (let i=b.length-7; i<b.length; i++) {
returns += b.charAt(i);
}
return returns;
}
getRBinary() { // reverse binary
return this.binary.split("").reverse().join("");
}
getHex() {
let returns = "";
let h = hex(parseIntAlt(this.ascii));
for (let i=h.length-2; i<h.length; i++) {
returns += h.charAt(i);
}
return returns;
}
}
// 1.2. Some NapChars contain opcodes, or drawing commands.
// The NapOpcode class decodes the command.
class NapOpcode extends NapChar {
constructor(_c) { // char or string
super(_c);
this.id = this.getId();
}
// *** IMPORTANT STEP 1 of 3 ***
// This is the first step, where we match the hex code to a command.
// The second step happens later on in this decoder.
getId() {
let returns = "";
switch(this.hex) {
//~ ~ ~ ~ ~ CONTROL CODES ~ ~ ~ ~ ~
case("0E"):
returns = "Shift-Out"; // graphics mode, we're here by default. Handled in step 2.
break;
case("0F"):
returns = "Shift-In"; // text mode, data that follows is text. Handled in step 2.
break;
case("18"):
returns = "CANCEL";
break;
case("1B"):
returns = "ESC";
break;
case("1F"):
returns = "NSR"; // Non-Selective Reset
break;
//~ ~ ~ ~ ~ PDI (PICTURE DESCRIPTION INSTRUCTION) CODES ~ ~ ~ ~ ~
//~ ~ ~ ENVIRONMENT, part 1 ~ ~ ~
case("20"):
returns = "RESET";
break;
case("21"):
returns = "DOMAIN"; // header information
break;
case("22"):
returns = "TEXT"; // formats text, doesn't contain text itself
break;
case("23"):
returns = "TEXTURE";
break;
//~ ~ ~ POINTS ~ ~ ~
case("24"):
returns = "POINT SET ABS";
break;
case("25"):
returns = "POINT SET REL";
break;
case("26"):
returns = "POINT ABS";
break;
case("27"):
returns = "POINT REL";
break;
//~ ~ ~ LINES ~ ~ ~
case("28"):
returns = "LINE ABS";
break;
case("29"):
returns = "LINE REL";
break;
case("2A"):
returns = "SET & LINE ABS";
break;
case("2B"):
returns = "SET & LINE REL";
break;
//~ ~ ~ ARCS ~ ~ ~
case("2C"):
returns = "ARC OUTLINED";
break;
case("2D"):
returns = "ARC FILLED";
break;
case("2E"):
returns = "SET & ARC OUTLINED";
break;
case("2F"):
returns = "SET & ARC FILLED";
break;
//~ ~ ~ RECTANGLES ~ ~ ~
case("30"):
returns = "RECT OUTLINED";
break;
case("31"):
returns = "RECT FILLED";
break;
case("32"):
returns = "SET & RECT OUTLINED";
break;
case("33"):
returns = "SET & RECT FILLED";
break;
//~ ~ ~ POLYGONS ~ ~ ~
case("34"):
returns = "POLY OUTLINED";
break;
case("35"):
returns = "POLY FILLED";
break;
case("36"):
returns = "SET & POLY OUTLINED";
break;
case("37"):
returns = "SET & POLY FILLED";
break;
//~ ~ ~ INCREMENTALS ~ ~ ~
case("38"):
returns = "FIELD";
break;
case("39"):
returns = "INCREMENTAL POINT";
break;
case("3A"):
returns = "INCREMENTAL LINE";
break;
case("3B"):
returns = "INCREMENTAL POLY FILLED";
break;
//~ ~ ~ ENVIRONMENT, part 2 ~ ~ ~
case("3C"):
returns = "SET COLOR";
break;
case("3D"):
returns = "WAIT";
break;
case("3E"):
returns = "SELECT COLOR";
break
case("3F"):
returns = "BLINK";
break;
default:
break;
}
return returns;
}
}
// 1.3. The NapChars following an opcode contain the data that
// the command will use. A separate data class is used in case
// we need data-specific methods later.
class NapData extends NapChar {
constructor(_c) { // char or string
super(_c);
//this.f = this.getNormFloat(); // float
}
getNormFloat() {
let returns = parseFloat(this.ascii / 127.0);
return returns;
}
}
// 2. MULTI-BYTE data classes
//
// 2.1. NapDataArray objects need to combine multiple NapData pieces for decoding.
class NapDataArray {
constructor(n) { // NapData[]
// defaults for coordinate type.
// TODO set programatically from domain info based on XY (3 bits) or XYZ (2 bits).
// However in almost all cases we can assume XY.
this.bitsPerByte = 3; // int
this.firstBitSign = true; // bool, should be true for all domain options?
this.bitVals = this.getBitValsSigned(n);//pow(2, (n.length * this.bitsPerByte) - int(this.firstBitSign)); // float
}
getBitValsUnsigned(n) {
const returns = pow(2, (n.length * this.bitsPerByte));
console.log("Bitvals unsigned: " + returns);
return returns;
}
getBitValsSigned(n) {
const returns = pow(2, (n.length * this.bitsPerByte) - int(this.firstBitSign))
console.log("Bitvals signed: " + returns);
return returns;
}
getSign(c) { // char or string
if (c === '1') {
return -1.0;
} else {
return 1.0;
}
}
binaryConv(n, loc) { // NapData, int
let returns = "";
for (let i=loc; i<loc+this.bitsPerByte; i++) {
returns += n.binary.charAt(i);
}
return returns;
}
}
// 2.2. XY and XYZ position are handled by the NapVector class.
class NapVector extends NapDataArray {
constructor(n) { // NapData[]
super(n);
this.x = this.getCoordFromBytes(n, "x"); // float
this.y = this.getCoordFromBytes(n, "y"); // float
//this.z = this.getCoordFromBytes(n, "z"); // float
}
/*
X Y X Y Z
8 7|6 5 4|3 2 1| 8 7|6 5|4 3|2 1|
----------------- -----------------
|?|1|S| | |S| | | |?|1|S| |S| |S| |
----------------- -----------------
|?|1| | | | | | | |?|1| | | | | | |
----------------- -----------------
. . . . . .
----------------- -----------------
|?|1| | | | | | | |?|1| | | | | | |
----------------- -----------------
G R B G R B
8 7|6 5 4|3 2 1|
-----------------
|?|1| | | | | | |
-----------------
|?|1| | | | | | |
-----------------
. . .
-----------------
|?|1| | | | | | |
-----------------
*/
getSingleByteVal(n, axis) { // NapData, string
let returns = "";
if (axis === "x") {
returns = "" + this.binaryConv(n, 1);
} else if (axis === "y") {
returns = "" + this.binaryConv(n, this.bitsPerByte + 1);//4);
} else if (axis === "z") {
returns = "" + this.binaryConv(n, (2 * this.bitsPerByte) + 1); // ? untested
}
return returns;
}
getCoordFromBytes(n, axis) { // NapData[], string
let returns = "";
for (let i=0; i<n.length; i++) {
returns += this.getSingleByteVal(n[i], axis);
}
let sign = 1;
if (this.firstBitSign) {
sign = this.getSign(returns.charAt(0));
returns = removeCharAt(returns, 0);
}
let finalReturns = 0;
if (axis === "x") {
finalReturns = (unbinary(returns) / this.bitVals) * sign;
} else if (axis === "y") {
finalReturns = ((this.bitVals - unbinary(returns)) / this.bitVals) * sign;
} else if (axis === "z") {
finalReturns = (unbinary(returns) / this.bitVals) * sign; // ? untested
}
return finalReturns;
}
}
// 2.3. Text
class NapText extends NapDataArray {
// TODO figure out text formatting
constructor(n) { // NapData[]
super(n);
this.text = this.setTextFromBytes(n);
}
setTextFromBytes(n) {
let returns = "";
for (let i=0; i<n.length; i++) {
returns += "" + n[i].c;
}
return returns;
}
}
// 3. COMMAND: One decoded drawing command.
// Assembled from the opcode and data bytes.
class NapCmd {
constructor(_cmd, _index) { // string, int
this.pointBytes = naplps_multiValLength; // int
this.singleBytes = naplps_singleValLength; // int
//this.pointRelative = true; // bool, TODO set programatically from header info...if this is in header?
this.cmdRaw = _cmd; // string
this.index = _index; // int
this.data = []; // NapData[]
this.points = []; // PVector[]
this.col = naplps_lastColor;
this.text = "";
this.opcode = new NapOpcode(this.cmdRaw.charAt(0)); // NapOpcode
if (this.cmdRaw.length > 1) {
for (let i=1; i<this.cmdRaw.length; i++) {
this.data.push(new NapData(this.cmdRaw.charAt(i)));
}
}
// *** IMPORTANT STEP 2 of 3 ***
// The second step is where we find out what kind of command it is,
// which tells us how we handle the data.
// The third and final step is done separately, in the drawing code.
switch(this.opcode.id) {
//~ ~ ~ ~ ~ CONTROL CODES ~ ~ ~ ~ ~
case("Shift-Out"): // graphics mode, we're here by default
// no effect?
break;
case("Shift-In"): // text mode, data that follows is text
this.setText();
break;
case("CANCEL"):
// no effect?
break;
case("ESC"):
// no effect?
break;
case("NSR"): // Non-Selective Reset
this.sendNsr();
break;
//~ ~ ~ ~ ~ PDI (PICTURE DESCRIPTION INSTRUCTION) CODES ~ ~ ~ ~ ~
//~ ~ ~ ENVIRONMENT, part 1 ~ ~ ~
case("RESET"):
this.sendReset();
break;
case("DOMAIN"): // header information
this.setDomain();
break;
case("TEXT"): // formats text, doesn't contain text itself
// TODO
break;
case("TEXTURE"):
// TODO
break;
//~ ~ ~ POINTS ~ ~ ~
case("POINT SET ABS"):
this.setPoints(false, true); // relative, set cursor
break;
case("POINT SET REL"):
this.setPoints(true, true);
break;
case("POINT ABS"):
this.setPoints(false, false);
break;
case("POINT REL"):
this.setPoints(true, false);
break;
//~ ~ ~ LINES ~ ~ ~
case("LINE ABS"):
this.setPoints(true, false); // TODO why is this broken?
break;
case("LINE REL"):
this.setPoints(true, false);
break;
case("SET & LINE ABS"):
this.setPoints(true, true); // TODO why is this broken?
break;
case("SET & LINE REL"):
this.setPoints(true, true);
break;
//~ ~ ~ ARCS ~ ~ ~
case("ARC OUTLINED"):
this.setPoints(true, false);
break;
case("ARC FILLED"):
this.setPoints(true, false);
break;
case("SET & ARC OUTLINED"):
this.setPoints(false, true);
break;
case("SET & ARC FILLED"):
this.setPoints(false, true);
break;
//~ ~ ~ RECTANGLES ~ ~ ~
case("RECT OUTLINED"):
this.setPoints(true, false);
break;
case("RECT FILLED"):
this.setPoints(true, false);
break;
case("SET & RECT OUTLINED"):
this.setPoints(false, true);
break;
case("SET & RECT FILLED"):
this.setPoints(false, true);
break;
//~ ~ ~ POLYGONS ~ ~ ~
case("POLY OUTLINED"):
this.setPoints(true, false);
break;
case("POLY FILLED"):
this.setPoints(true, false);
break;
case("SET & POLY OUTLINED"): // relative points after first
this.setPoints(false, true);
break;
case("SET & POLY FILLED"): // relative points after first
this.setPoints(false, true);
break;
//~ ~ ~ INCREMENTALS ~ ~ ~
case("FIELD"):
// TODO
break;
case("INCREMENTAL POINT"):
this.setPoints(true, true);
break;
case("INCREMENTAL LINE"):
this.setPoints(true, true);
break;
case("INCREMENTAL POLY FILLED"):
this.setPoints(true, true);
break;
//~ ~ ~ ENVIRONMENT, part 2 ~ ~ ~
case("SET COLOR"):
this.setColor();
break;
case("WAIT"):
// TODO
break;
case("SELECT COLOR"):
this.selectColor(); // palette color
break
case("BLINK"):
// TODO
break;
default:
break;
}
}
printCmd(mode) {
console.log(this.formatCmd(mode));
}
// This prints out the command contents in various formats
// Helpful for debugging
formatCmd(mode) {
let returns = "(" + (this.index + 1) + ") " + this.opcode.id;
if (this.data.length > 0) returns += ": ";
if (this.opcode.id === "") {
switch(mode) {
case("char"):
returns += this.opcode.c;
break;
case("binary"):
returns += this.opcode.binary;
break;
case("rbinary"):
returns += this.opcode.rbinary;
break;
case("ascii"):
returns += this.opcode.ascii;
break;
case("hex"):
returns += this.opcode.hex;
break;
default:
break;
}
}
if (this.data.length > 0) {
if (this.opcode.id === "") returns += ", ";
for (let i=0; i<this.data.length; i++) {
switch(mode) {
case("char"):
returns += "" + this.data[i].c;
break;
case("binary"):
returns += "" + this.data[i].binary;
break;
case("rbinary"):
returns += "" + this.data[i].rbinary;
break;
case("ascii"):
returns += "" + this.data[i].ascii;
break;
case("hex"):
returns += "" + this.data[i].hex;
break;
default:
break;
}
if (i < this.data.length - 1) returns += ", ";
}
}
return returns;
}
// ~ ~ ~ Parsing methods begin here ~ ~ ~
setColor() {
let r = 0, g = 0, b = 0;
let r2 = 0, g2 = 0, b2 = 0;
let colorValLength = this.data.length;
let shift = 8 - (2 * colorValLength);
naplps_lastColor = naplps_yellow; // default
try {
let c = this.data[0].ascii;
if (c < naplps_minVal) {
this.col = naplps_lastColor;
return false;
}
g = c & parseInt('040', 8);
r = c & parseInt('020', 8);
b = c & parseInt('010', 8);
c <<= 2;
g |= c & parseInt('020', 8);
r |= c & parseInt('010', 8);
b |= c & parseInt('004', 8);
g >>= 4;
r >>= 3;
b >>= 2;
for (let i = 1; i < colorValLength; i++ ) {
c = this.data[i].ascii;
if (c < naplps_minVal) {
this.col = naplps_lastColor;
return false;
}
g2 = c & parseInt('040', 8);
r2 = c & parseInt('020', 8);
b2 = c & parseInt('010', 8);
c <<= 2;
g2 |= c & parseInt('020', 8);
r2 |= c & parseInt('010', 8);
b2 |= c & parseInt('004', 8);
g2 >>= 4;
r2 >>= 3;
b2 >>= 2;
g <<= 2;
r <<= 2;
b <<= 2;
g |= g2;
r |= r2;
b |= b2;
}
let fill = 0; //(2 << shift) - 1;
r <<= shift;
g <<= shift;
b <<= shift;
naplps_lastColor = new Vector3(r+fill, g+fill, b+fill);
if (naplps_colorMode !== 0) {
naplps_colorMap[naplps_lastIndex] = naplps_lastColor;
console.log("<palette write>\nindex: " + naplps_lastIndex + ", color: " + naplps_lastColor.x + " " + naplps_lastColor.y + " " + naplps_lastColor.z);
} else {
console.log("<palette read>\nindex: " + naplps_lastIndex + ", color: " + naplps_lastColor.x + " " + naplps_lastColor.y + " " + naplps_lastColor.z);
}
this.col = naplps_lastColor;
return true;
} catch (e) {
this.col = naplps_lastColor;
return false;
}
}
selectColor() {
try {
let c = this.data[0].ascii;
if (c < naplps_minVal) {
naplps_colorMode = 0;