forked from joshmarinacci/aminogfx-gl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
2949 lines (2339 loc) · 56.7 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const DEBUG = false;
const DEBUG_ERRORS = true;
const DEBUG_SVG = false;
if (DEBUG) {
console.log('inside of the aminogfx main.js');
}
//load native module
const binary = require('node-pre-gyp');
const path = require('path');
const binding_path = binary.find(path.resolve(path.join(__dirname, 'package.json')));
const native = require(binding_path);
let request = require('request');
const fs = require('fs');
const sharp = require('sharp');
const packageInfo = require('./package.json');
/**
* Use custom request handler.
*
* Has to implement handler(opts, callback) and to support abort().
*/
function setRequestHandler(handler) {
request = handler;
}
exports.setRequestHandler = setRequestHandler;
/**
* Get the request handler.
*/
function getRequestHandler() {
return request;
}
exports.getRequestHandler = getRequestHandler;
//
// AminoGfx
//
const AminoGfx = native.AminoGfx;
/**
* Initialize AminoGfx instance.
*/
AminoGfx.prototype.init = function () {
if (DEBUG) {
console.log('AminoGfx.init()');
}
//initialize bindings
this.amino = this;
makeProps(this, {
//position (auto set)
x: -1,
y: -1,
//QHD size
w: 640,
h: 360,
//color
opacity: 1,
fill: '#000000',
r: 0,
g: 0,
b: 0,
//title
title: 'AminoGfx OpenGL',
//stats
showFPS: true //opt-out
});
this.fill.watch(watchFill);
//root wrapper
this.setRoot(this.createGroup());
//input handler
this.inputHandler = input.createEventHandler(this);
//process handler
/*
process.on('SIGINT', () => {
console.log('-> CTRL-C');
//stop
process.exit();
});
*/
};
/**
* Fill value has changed.
*/
function watchFill(value, prop, obj) {
const color = parseRGBString(value);
obj.r(color.r);
obj.g(color.g);
obj.b(color.b);
}
/**
* Convert RGB expression to object.
*
* Supports:
*
* - hex strings
* - array: r, g, b (0..1)
* - objects: r, g, b (0..1)
*/
function parseRGBString(fill) {
if (typeof fill === 'string') {
//strip off any leading #
if (fill.substring(0, 1) === '#') {
fill = fill.substring(1);
}
//pull out the components
const r = parseInt(fill.substring(0, 2), 16);
const g = parseInt(fill.substring(2, 4), 16);
const b = parseInt(fill.substring(4, 6), 16);
return {
r: r / 255,
g: g / 255,
b: b / 255
};
} else if (Array.isArray(fill)) {
return {
r: fill[0],
g: fill[1],
b: fill[2]
};
}
return fill;
}
/**
* Start renderer.
*/
AminoGfx.prototype.start = function (done) {
const promise = new Promise((resolve, reject) => {
//pass to native code
this._start(err => {
if (err) {
reject(err);
return;
}
//runtime info
this.runtime.aminogfx = packageInfo.version;
//ready
resolve(this);
//check root
if (!this.root) {
throw new Error('Missing root!');
}
});
});
//promise version
if (arguments.length === 0) {
return promise;
}
//callback version
promise.then(() => {
//ready (Note: this points to the instance)
done.call(this, null);
}).catch(err => {
done.call(this, err);
});
};
/**
* Set the root node.
*/
AminoGfx.prototype.setRoot = function (root) {
this.root = root;
this._setRoot(root);
};
/**
* Get the root node.
*/
AminoGfx.prototype.getRoot = function () {
return this.root;
};
/**
* Create group element.
*/
AminoGfx.prototype.createGroup = function (attrs) {
const group = new AminoGfx.Group(this);
if (attrs) {
group.attr(attrs);
}
return group;
};
/**
* Create rect element.
*/
AminoGfx.prototype.createRect = function (attrs) {
const rect = new AminoGfx.Rect(this);
if (attrs) {
rect.attr(attrs);
}
return rect;
};
/**
* Create image view element.
*/
AminoGfx.prototype.createImageView = function (attrs) {
const iv = new AminoGfx.ImageView(this);
if (attrs) {
iv.attr(attrs);
}
return iv;
};
/**
* Create pixel view element.
*/
AminoGfx.prototype.createPixelView = function (attrs) {
const pv = new AminoGfx.PixelView(this);
if (attrs) {
pv.attr(attrs);
}
return pv;
};
/**
* Create texture element.
*/
AminoGfx.prototype.createTexture = function (attrs) {
const texture = new AminoGfx.Texture(this);
if (attrs) {
texture.attr(attrs);
}
return texture;
};
/**
* Create polygon element.
*/
AminoGfx.prototype.createPolygon = function (attrs) {
const polygon = new AminoGfx.Polygon(this);
if (attrs) {
polygon.attr(attrs);
}
return polygon;
};
/**
* Create model element.
*/
AminoGfx.prototype.createModel = function (attrs) {
const model = new AminoGfx.Model(this);
if (attrs) {
model.attr(attrs);
}
return model;
};
/**
* Create circle element.
*/
AminoGfx.prototype.createCircle = function (attrs) {
const circle = new AminoGfx.Circle(this);
if (attrs) {
circle.attr(attrs);
}
return circle;
};
/**
* Create text element.
*/
AminoGfx.prototype.createText = function (attrs) {
const text = new AminoGfx.Text(this);
if (attrs) {
text.attr(attrs);
}
return text;
};
/**
* Handle an event.
*/
AminoGfx.prototype.handleEvent = function (evt) {
//debug
//console.log('Event: ' + JSON.stringify(evt));
//add timestamp
evt.time = Date.now();
//pass to event processor
this.inputHandler.processEvent(evt);
};
/**
* Destroy renderer.
*/
AminoGfx.prototype.destroy = function () {
//clear root copy
this.root = null;
this._destroy();
/*
//call async (outside event handler callbacks)
setImmediate(() => {
this._destroy();
});
*/
};
/**
* Set position.
*/
AminoGfx.prototype.setPosition = setPosition;
function setPosition(x, y, z) {
this.x(x).y(y);
if (z !== undefined) {
this.z(z);
}
}
/**
* Set size.
*/
AminoGfx.prototype.setSize = setSize;
function setSize(w, h) {
//debug
//console.log('setSize()');
this.w(w).h(h);
}
/**
* Get runtime system info.
*/
AminoGfx.prototype.getStats = function () {
const stats = this._getStats();
const mem = process.memoryUsage();
stats.heapUsed = mem.heapUsed;
stats.heapTotal = mem.heapTotal;
return stats;
};
/**
* Find node with id.
*/
AminoGfx.prototype.find = function (id) {
function findNodeById(id, node) {
if (node.id && node.id === id) {
return node;
}
if (node.isGroup) {
const count = node.getChildCount();
for (let i = 0; i < count; i++) {
const ret = findNodeById(id, node.getChild(i));
if (ret != null) {
return ret;
}
}
}
return null;
}
return findNodeById(id, this.getRoot());
};
/**
* Find a node at a certain position with an optional filter callback.
*/
AminoGfx.prototype.findNodesAtXY = function (pt, filter) {
//debug
//console.log('findNodesAtXY()');
return findNodesAtXY(this.root, pt, filter, '');
};
function findNodesAtXY(root, pt, filter, tab) {
//verify
if (!root || !root.visible()) {
return [];
}
//debug
//console.log(tab + ' xy', pt.x, pt.y, root.id());
//convert to root coordinates
const tpt = pt.minus(root.x(), root.y()).divide(root.sx(), root.sy());
//handle children first, then the parent/root
let res = [];
if (filter) {
//debug
//console.log('-> using filter on root');
if (!filter(root)) {
//debug
//console.log('-> root skipped');
return res;
}
}
//check children
const childCount = root.children?.length;
if (childCount) {
//debug
//console.log('-> checking ' + childCount + ' children');
for (let i = childCount - 1; i >= 0; i--) {
const node = root.children[i];
const found = findNodesAtXY(node, tpt, filter, tab + ' ');
res = res.concat(found);
}
}
//debug
//console.log('-> root ' + root.x() + ' ' + root.y() + ' ' + root.w() + ' ' + root.h());
//console.log('-> point ' + tpt.x + ' ' + tpt.y);
//check root
if (root.contains?.(tpt)) {
//debug
//console.log('-> added root');
res.push(root);
}
return res;
}
/**
* Find a node at a certain position.
*/
AminoGfx.prototype.findNodeAtXY = function (x, y) {
return findNodeAtXY(this.root, x, y, '');
};
function findNodeAtXY(root, x, y, tab) {
if (!root) {
return null;
}
if (!root.visible()) {
return null;
}
let tx = x - root.x();
let ty = y - root.y();
tx /= root.sx();
ty /= root.sy();
//console.log(tab + " xy="+tx+","+ty);
if (root.cliprect && root.clipRect()) {
if (tx < 0) {
return false;
}
if (tx > root.w()) {
return false;
}
if (ty < 0) {
return false;
}
if (ty > root.h()) {
return false;
}
}
if (root.children) {
//console.log(tab+"children = ",root.children.length);
for (let i = root.children.length - 1; i >= 0; i--) {
const node = root.children[i];
const found = this.findNodeAtXY(node, tx, ty, tab+ ' ');
if (found) {
return found;
}
}
}
//console.log(tab+"contains " + tx+' '+ty);
if (root.contains && root.contains(tx, ty)) {
//console.log(tab,"inside!",root.getId());
return root;
}
return null;
}
/**
* Convert screen coordinate to local node coordinate.
*
* Note: only supports translations & scaling (rotation & 3D are not support)
*/
AminoGfx.prototype.globalToLocal = function (pt, node) {
return convertGlobalToLocal(pt, node);
};
function convertGlobalToLocal(pt, node) {
//check parent
if (node.parent) {
pt = convertGlobalToLocal(pt, node.parent);
}
return input.makePoint(
(pt.x - node.x()) / node.sx(),
(pt.y - node.y()) / node.sy()
);
}
/*
function calcGlobalToLocalTransform(node) {
if (node.parent) {
const trans = calcGlobalToLocalTransform(node.parent);
if (node.getScalex() != 1) {
trans.x /= node.sx();
trans.y /= node.sy();
}
trans.x -= node.x();
trans.y -= node.y();
return trans;
}
return {
x: -node.x(),
y: -node.y()
};
}
*/
AminoGfx.prototype.localToGlobal = function (pt, node) {
pt = {
x: pt.x + node.x() * node.sx(),
y: pt.y + node.y() * node.sx()
};
if (node.parent) {
return this.localToGlobal(pt, node.parent);
} else {
return pt;
}
};
AminoGfx.prototype.on = function (name, target, listener) {
this.inputHandler.on(name, target, listener);
};
exports.AminoGfx = AminoGfx;
//
// AminoGfx.Group
//
const Group = AminoGfx.Group;
/**
* Initializer.
*/
Group.prototype.init = function () {
if (DEBUG) {
console.log('Group.init()');
}
//bindings
makeProps(this, {
id: '',
//visibility
visible: true,
opacity: 1,
//position
x: 0,
y: 0,
z: 0,
//size
w: 100,
h: 100,
//origin
originX: 0,
originY: 0,
//scaling
sx: 1,
sy: 1,
//rotation
rx: 0,
ry: 0,
rz: 0,
//clipping
clipRect: false,
//3D rendering (depth test)
depth: false
});
this.isGroup = true;
this.children = [];
};
/**
* Check if point is inside of rect.
*/
Group.prototype.contains = contains;
/**
* Set position.
*/
Group.prototype.setPosition = setPosition;
/**
* Set size.
*/
Group.prototype.setSize = setSize;
/**
* Scale.
*/
Group.prototype.scale = scaleFunc;
function scaleFunc(sx, sy) {
this.sx(sx).sy(sy);
}
/**
* Rotate.
*/
Group.prototype.rotate = rotateFunc;
function rotateFunc(rx, ry, rz) {
this.rx(rx).ry(ry).rz(rz);
}
/**
* Add one or more children to this group.
*/
Group.prototype.add = function () {
const count = arguments.length;
if (count === 0) {
throw new Error('missing parameter');
}
for (let i = 0; i < count; i++) {
const node = arguments[i];
if (!node) {
throw new Error('can\'t add a null child to a group');
}
if (this.children.indexOf(node) !== -1) {
throw new Error('child was added before');
}
if (node === this || node.parent) {
throw new Error('already added to different group');
}
this._add(node);
this.children.push(node);
node.parent = this;
}
return this;
};
/**
* Insert a child at a certain position.
*/
Group.prototype.insertAt = function (item, pos) {
if (!item) {
throw new Error('can\'t add a null child to a group');
}
if (this.children.indexOf(item) !== -1) {
throw new Error('child was added before');
}
if (item === this || item.parent) {
throw new Error('already added to different group');
}
this._insert(item, pos);
this.children.splice(pos, 0, item);
item.parent = this;
return this;
};
/**
* Insert before a sibling.
*/
Group.prototype.insertBefore = function (item, sibling) {
const pos = this.children.indexOf(sibling);
if (pos === -1) {
//add at end
this.add(item);
return false;
}
return this.insertAt(item, pos);
};
/**
* Insert after a sibling.
*/
Group.prototype.insertAfter = function (item, sibling) {
const pos = this.children.indexOf(sibling);
if (pos === -1) {
//add at end
this.add(item);
return false;
}
return this.insertAt(item, pos + 1);
};
/**
* Remove one or more children from this group.
*/
Group.prototype.remove = function () {
const count = arguments.length;
if (count === 0) {
//special case: remove from parent
if (this.parent) {
this.parent.remove(this);
}
return;
}
for (let i = 0; i < count; i++) {
const child = arguments[i];
const pos = this.children.indexOf(child);
if (pos >= 0) {
this._remove(child);
this.children.splice(pos, 1);
child.parent = null;
} else {
throw new Error('not a child');
}
}
return this;
};
/**
* Remove all children.
*/
Group.prototype.clear = function () {
const count = this.children.length;
for (let i = 0; i < count; i++) {
const child = this.children[i];
this._remove(child);
child.parent = null;
}
this.children = [];
return this;
};
/**
* Bring child to top.
*/
Group.prototype.raiseToTop = function (node) {
if (!node) {
throw new Error('can\'t move a null child');
}
this.remove(node);
this.add(node);
return this;
};
/**
* Free OpenGL resources.
*
* Attention: do not call if shared textures are used by any child.
*/
Group.prototype.destroy = function () {
this.children.forEach(node => {
if (node.destroy) {
node.destroy();
}
});
};
/**
* Find children.
*/
Group.prototype.find = function (pattern) {
const results = new FindResults();
if (pattern.indexOf('#') === 0) {
//id
const id = pattern.substring(1);
results.children = treeSearch(this, false, function (child) {
return (child.id().toLowerCase() === id);
});
} else {
//look for class name (e.g. AminoRect)
pattern = 'amino' + pattern.toLowerCase();
results.children = treeSearch(this, false, function (child) {
return child.constructor.name.toLowerCase() === pattern;
});
}
return results;
};
function treeSearch (root, considerRoot, filter) {
let res = [];
if (root.isGroup) {
const count = root.children.length;
for (let i = 0; i < count; i++) {
res = res.concat(treeSearch(root.children[i], true, filter));
}
}
if (considerRoot && filter(root)) {
return res.concat([root]);
}
return res;
}
function FindResults() {
this.children = [];
function makefindprop(obj, name) {
obj[name] = function (val) {
this.children.forEach(child => {
if (child[name]) {
child[name](val);
}
});
return this;
};
}
//TODO review
makefindprop(this, 'visible');
makefindprop(this, 'fill');
makefindprop(this, 'filled');
makefindprop(this, 'x');
makefindprop(this, 'y');
makefindprop(this, 'w');
makefindprop(this, 'h');
this.length = function () {
return this.children.length;
};
}
//
// Rect
//
const Rect = AminoGfx.Rect;
Rect.prototype.init = function () {
if (DEBUG) {
console.log('Rect.init()');
}
//properties
makeProps(this, {
id: '',
visible: true,
//position
x: 0,
y: 0,
z: 0,
//size
w: 100,
h: 100,
//origin
originX: 0,
originY: 0,
//rotation
rx: 0,
ry: 0,
rz: 0,
//scaling
sx: 1,
sy: 1,
//white
fill: '#ffffff',
r: 1,
g: 1,
b: 1,
opacity: 1.0
});
//special
this.fill.watch(watchFill);
};
/**
* Check if point is inside of rect.
*/
Rect.prototype.contains = contains;
/**
* Set position.
*/
Rect.prototype.setPosition = setPosition;
/**
* Set size.
*/