-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1414 lines (1128 loc) · 34.7 KB
/
script.js
File metadata and controls
1414 lines (1128 loc) · 34.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
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
// Libraries Used:
// - EaselJS for display and event models
// - GSAP for menu animation
// Instructions
// - Drag from an output connection (blue dot) to an input connection (grey dot) to create a wire.
// - You may have multiple wires branch off an output, but only one wire per input.
// - Click a connector (blue or grey dots) to remove all attached wires.
// - Click (+) button in top-right corner to add new circuits.
// - Note that some circuits are interactive
// Circuit Types
// INPUT / OUTPUT
// - IN: A light with an input to turn it on.
// - OUT: Interactive button with an output.
// LOGIC
// - NOT: Outputs inverted input signal.
// - OR: Output is true when any input is true.
// - AND: Output is only true when both inputs are true.
// - XOR: Output is only true when a single input is true.
// - NOR: Output is true when neither input is true.
// - MEM: One bit memory cell that can be switched on/off from inputs, and holds its state.
// - TCK: A set frequency ticker that pulses a single tick signal every `n` ticks (configurable)
// - DLY: Carries a signal after a delay of `n` ticks (configurable)
// TODO:
// - Zooming
// - Instructions / Examples
// - Improve Import / Export UI
// kick off init
document.addEventListener('DOMContentLoaded', function() {
window.app = AppFactory(Circuit, Wire);
// enable UI import / export
document.getElementById('importBtn').addEventListener('click', function importClickHandler() {
var json = window.prompt('Paste JSON and press enter to import - this will reset any current design!');
if (json) {
loadProject(json);
}
});
document.getElementById('exportBtn').addEventListener('click', function exportClickHandler() {
var btn = document.getElementById('exportBtn');
if (copyTextToClipboard(saveProject())) {
btn.classList.add('show-copy-msg');
setTimeout(function() {
btn.classList.remove('show-copy-msg');
}, 1000);
}
});
});
function AppFactory(Circuit, Wire) {
var App = {};
App.stage = new createjs.Stage('circuits-canvas');
App.stage.mouseMoveOutside = true;
createjs.Touch.enable(App.stage);
var ctx = App.stage.canvas.getContext('2d');
var devicePixelRatio = window.devicePixelRatio || 1;
var backingStoreRatio = ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio || 1;
App.scale = devicePixelRatio / backingStoreRatio;
App.stage.scaleX = App.scale;
App.stage.scaleY = App.scale;
// how long signal takes to move through a circuit
App.propagation_delay = 125;
// delete button functionality
App.delete_btn = {
element: document.getElementById('deleteBtn'),
show: function show() {
this.element.classList.add('show');
return this;
},
hide: function hide() {
this.element.classList.remove('show');
return this;
},
active: function active() {
this.element.classList.add('active');
return this;
},
inactive: function inactive() {
this.element.classList.remove('active');
return this;
}
};
// add menu functionlity
App.add_menu = (function menuFactory() {
var Menu = {};
Menu.is_open = false;
Menu.add_btn = document.getElementById('addBtn');
Menu.add_dropdown = document.getElementById('addDropdown');
Menu.add_btn.addEventListener('click', function() {
if (Menu.is_open) {
Menu.hide();
}
else{
Menu.show();
}
});
Menu.show = function show() {
this.is_open = true;
menu_tl.play();
};
Menu.hide = function hide() {
this.is_open = false;
menu_tl.reverse();
};
// helper function to build menu
var btns = [];
function addMenuBtn(name, factory) {
var btn = document.createElement('div');
Menu.add_dropdown.appendChild(btn);
btn.textContent = name;
btns.push(btn);
btn.addEventListener('click', function() {
var centerX = window.innerWidth / 2;
var centerY = window.innerHeight / 2;
factory(centerX - App.wires.x, centerY - App.wires.y);
});
}
addMenuBtn('IN', makeButton);
addMenuBtn('OUT', makeLight);
addMenuBtn('NOT', makeNot);
addMenuBtn('OR', makeOr);
addMenuBtn('AND', makeAnd);
addMenuBtn('XOR', makeXor);
addMenuBtn('NOR', makeNor);
addMenuBtn('MEM', makeMemory);
addMenuBtn('TCK', makeTicker);
addMenuBtn('DLY', makeDelay);
// create gsap animation to open/close menu
var menu_tl = new TimelineMax({
paused: true,
onStart: function() { Menu.add_dropdown.style.display = 'block'; },
onReverseComplete: function() { Menu.add_dropdown.style.display = 'none'; }
});
menu_tl.staggerFromTo(btns, 0.25, {
opacity: 0,
y: -28,
rotationX: -90,
scaleX: 0.5,
scaleY: 0.5
}, {
opacity: 1,
y: 0,
rotationX: 0,
scaleX: 1,
scaleY: 1
}, 0.025)
.to(Menu.add_btn, menu_tl.duration(), {
rotation: 225
}, 0);
// menu_tl.timeScale(0.2);
return Menu;
})();
// flag to redraw screen
App.needs_update = true;
// refresh stage
createjs.Ticker.addEventListener('tick', handleTick);
createjs.Ticker.framerate = 60;
function handleTick(evt) {
if (App.needs_update) {
App.stage.update();
App.needs_update = false;
}
}
// containers
App.circuits = new createjs.Container();
App.wires = new createjs.Container();
App.stage.addChild(App.wires);
App.stage.addChild(App.circuits);
// active wire drawing
// fake a connector at input end
App.new_wire = Wire.new({ globalX: 0, globalY: 0 }, { globalX: 0, globalY: 0 });
// mouse event handlers - scope is set to originating connector
App.startNewWire = function startNewWire(evt) {
var temp_pt = App.wires.localToGlobal(this.globalX, this.globalY);
temp_pt.x /= App.scale;
temp_pt.y /= App.scale;
App.new_wire.output.globalX = temp_pt.x;
App.new_wire.output.globalY = temp_pt.y;
App.new_wire.input.globalX = evt.stageX / App.scale;
App.new_wire.input.globalY = evt.stageY / App.scale;
App.new_wire.draw(true);
App.stage.addChild(App.new_wire.gfx);
};
App.dragNewWire = function dragNewWire(evt) {
App.new_wire.input.globalX = evt.stageX / App.scale;
App.new_wire.input.globalY = evt.stageY / App.scale;
App.new_wire.draw(true);
};
App.endNewWire = function endNewWire(evt) {
App.needs_update = true;
App.stage.removeChild(App.new_wire.gfx);
// loop through input connectors of other circuits to see if a connection was made
var connector;
var temp_pt = new createjs.Point();
circuitLoop:
for (var i = Circuit.active.length - 1; i >= 0; i--) {
var c = Circuit.active[i];
// don't check inputs on current circuit
if (this.circuit.id !== c.id) {
inputsLoop:
for (var n = c.inputs.length - 1; n >= 0; n--) {
var input_conn = c.inputs[n];
input_conn.gfx.globalToLocal(evt.stageX, evt.stageY, temp_pt);
if (input_conn.gfx.hitTest(temp_pt.x, temp_pt.y)) {
connector = input_conn;
break circuitLoop;
}
}
}
}
// create connection
if (connector) {
connector.recycleWires();
var wire = Wire.new(this, connector);
this.wires.push(wire);
connector.wires.push(wire);
App.wires.addChild(wire.gfx);
wire.powerChange(this.circuit.has_power);
}
};
// allow dragging stage
var dragging_stage = false;
var stage_offset = new createjs.Point();
App.stage.on('stagemousedown', function(evt) {
if (!evt.relatedTarget) {
dragging_stage = true;
App.wires.globalToLocal(evt.rawX, evt.rawY, stage_offset);
// stage_offset.x = evt.rawX - App.wires.x;
// stage_offset.y = evt.rawY - App.wires.y;
// console.log(stage_offset);
}
});
App.stage.on('stagemousemove', function(evt) {
if (dragging_stage) {
App.needs_update = true;
var new_x = (evt.rawX / App.scale) - stage_offset.x;
var new_y = (evt.rawY / App.scale) - stage_offset.y;
App.wires.x = new_x;
App.wires.y = new_y;
App.circuits.x = new_x;
App.circuits.y = new_y;
}
});
App.stage.on('stagemouseup', function(evt) {
if (dragging_stage) dragging_stage = false;
});
// handle resizing
function resizeStage() {
var width = window.innerWidth;
var height = window.innerHeight;
App.stage.canvas.width = width * App.scale;
App.stage.canvas.height = height * App.scale;
App.needs_update = true;
}
resizeStage();
window.addEventListener('resize', resizeStage);
setTimeout(function() {
var centerX = window.innerWidth / 2;
var centerY = window.innerHeight / 2;
makeButton(centerX - 100, centerY);
makeLight(centerX + 100, centerY);
}, 0);
return App;
}
// --------------------------------------------------
// CIRCUIT FACTORIES
// --------------------------------------------------
// can be used to make a circuit of any type - used by loadProject
function makeType(type, x, y, other_info) {
var c;
switch (type) {
case 'button':
c = makeButton(x, y);
break;
case 'light':
c = makeLight(x, y);
break;
case 'not':
c = makeNot(x, y);
break;
case 'or':
c = makeOr(x, y);
break;
case 'and':
c = makeAnd(x, y);
break;
case 'xor':
c = makeXor(x, y);
break;
case 'nor':
c = makeNor(x, y);
break;
case 'memory':
c = makeMemory(x, y, other_info.memory);
break;
case 'ticker':
c = makeTicker(x, y, other_info.off_time);
break;
case 'delay':
c = makeDelay(x, y, other_info.delay);
break;
default:
throw new Error('makeType: "' + type + '" is not a valid type');
break;
}
return c;
}
function makeButton(x, y) {
var c = new Circuit('button', 0, 1);
c.add(x, y);
// create toggle button
var toggle_btn = new createjs.Shape();
c.gfx.addChild(toggle_btn);
toggle_btn.on('mousedown', toggleGenerator);
toggle_btn.on('pressup', toggleGenerator);
// toggle power state and broadcast change
function toggleGenerator() {
c.has_power = !c.has_power;
renderButton(c);
c.broadcastPower();
}
// render toggle button to show current generator state
function renderButton() {
var btn_color = c.has_power ? Wire.on_color : Wire.off_color;
var icon_color = c.has_power ? Wire.off_color : 'white';
toggle_btn.graphics.clear();
toggle_btn.graphics.beginFill(btn_color);
toggle_btn.graphics.drawCircle(0, 0, 18);
toggle_btn.graphics.setStrokeStyle(2);
toggle_btn.graphics.beginStroke(icon_color);
toggle_btn.graphics.drawCircle(0, 0, 10);
toggle_btn.graphics.beginStroke(icon_color);
toggle_btn.graphics.moveTo(0, -4);
toggle_btn.graphics.lineTo(0, 4);
toggle_btn.graphics.endStroke();
c.gfx.updateCache();
}
// initial render
renderButton();
// no inputs
return c;
}
function makeLight(x, y) {
var c = new Circuit('light', 1, 0);
c.add(x, y);
// dummy determination function
c.determinePowerState = function determinePowerState(inputs) {
return inputs[0];
};
// create light
var light_gfx = new createjs.Shape();
c.draggable.addChild(light_gfx);
// render toggle button to show current generator state
c.powerChanged = renderLight;
function renderLight() {
var light_color = c.has_power ? Wire.on_color : Wire.off_color;
light_gfx.graphics.clear();
light_gfx.graphics.beginFill(light_color);
light_gfx.graphics.drawCircle(0, 0, 18);
c.gfx.updateCache();
}
renderLight();
return c;
}
function makeNot(x, y) {
var c = new Circuit('not', 1, 1, 'NOT');
c.add(x, y);
c.has_power = true;
c.determinePowerState = function determinePowerState(inputs) {
return !inputs[0];
};
return c;
}
function makeOr(x, y) {
var c = new Circuit('or', 2, 1, 'OR');
c.add(x, y);
c.determinePowerState = function determinePowerState(inputs) {
return inputs[0] || inputs[1];
};
return c;
}
function makeAnd(x, y) {
var c = new Circuit('and', 2, 1, 'AND');
c.add(x, y);
c.determinePowerState = function determinePowerState(inputs) {
return inputs[0] && inputs[1];
};
return c;
}
function makeXor(x, y) {
var c = new Circuit('xor', 2, 1, 'XOR');
c.add(x, y);
c.determinePowerState = function determinePowerState(inputs) {
return (inputs[0] || inputs[1]) && !(inputs[0] && inputs[1]);
};
return c;
}
function makeNor(x, y) {
var c = new Circuit('nor', 2, 1, 'NOR');
c.add(x, y);
c.has_power = true;
c.determinePowerState = function determinePowerState(inputs) {
return !inputs[0] && !inputs[1];
};
return c;
}
function makeMemory(x, y, state) {
var c = new Circuit('memory', 2, 1);
c.add(x, y);
// use a memory bit to track state
var memory = c.data.memory = !!state;
c.determinePowerState = function determinePowerState(inputs) {
if (inputs[1]) {
// power to top input turns bit on
memory = c.data.memory = true;
}
else {
// power to bottom input only turns bit off
if (inputs[0]) memory = c.data.memory = false;
}
return memory;
};
// create light
var light_gfx = new createjs.Shape();
c.draggable.addChild(light_gfx);
// render toggle button to show current generator state
c.powerChanged = renderLight;
function renderLight() {
var light_color = c.has_power ? Wire.on_color : Wire.off_color;
light_gfx.graphics.clear();
light_gfx.graphics.beginFill(light_color);
light_gfx.graphics.drawCircle(0, 0, 14);
c.gfx.updateCache();
}
// label inputs
var on_lbl = new createjs.Shape();
var off_lbl = new createjs.Shape();
on_lbl.x = c.inputs[1].gfx.x + 16;
on_lbl.y = c.inputs[1].gfx.y;
off_lbl.x = c.inputs[0].gfx.x + 16;
off_lbl.y = c.inputs[0].gfx.y;
c.draggable.addChild(on_lbl);
c.draggable.addChild(off_lbl);
on_lbl.graphics.setStrokeStyle(2);
on_lbl.graphics.beginStroke('white');
on_lbl.graphics.moveTo(0, -4);
on_lbl.graphics.lineTo(0, 4);
on_lbl.graphics.endStroke();
off_lbl.graphics.setStrokeStyle(2);
off_lbl.graphics.beginStroke('white');
off_lbl.graphics.drawCircle(0, 0, 4);
// switch memory on if needed
if (memory) {
c.inputChange();
}
// perform initial render
renderLight();
return c;
}
function makeTicker(x, y, set_off_time) {
var c = new Circuit('ticker', 0, 1);
c.add(x, y);
// create toggle button
var toggle_btn = new createjs.Shape();
c.gfx.addChild(toggle_btn);
toggle_btn.on('click', toggleGenerator);
// begin ticking!
var delay = app.propagation_delay;
var off_time = c.data.off_time = set_off_time || 10; // propagation ticks
var on_time = delay;
setTimeout(toggleGenerator, off_time * delay);
// toggle power state and broadcast change
function toggleGenerator() {
c.has_power = !c.has_power;
renderLight(c);
setTimeout(toggleGenerator, c.has_power ? on_time : off_time * delay);
c.broadcastPower();
}
// create light
var light_gfx = new createjs.Shape();
c.draggable.addChild(light_gfx);
function renderLight() {
var light_color = c.has_power ? Wire.on_color : Wire.off_color;
light_gfx.graphics.clear();
light_gfx.graphics.beginFill(light_color);
light_gfx.graphics.drawCircle(0, 0, 18);
c.gfx.updateCache();
}
// initial render
renderLight();
// create label
var label = new createjs.Text(off_time, '18px Arial', '#FFF');
label.textAlign = 'center';
label.cache(-c.chip_radius, 0, c.chip_radius * 2, 20);
c.draggable.addChild(label);
label.x = 0;
label.y = -10;
function renderLabel() {
label.text = off_time;
label.updateCache();
c.gfx.updateCache();
app.needs_update = true;
}
// create step buttons
var up_btn = new createjs.Shape();
up_btn.y = -28;
up_btn.graphics.beginFill(Wire.on_color);
up_btn.graphics.drawPolyStar(0, 0, 8, 3, 0, -90);
c.gfx.addChild(up_btn);
var down_btn = new createjs.Shape();
down_btn.y = 28;
down_btn.graphics.beginFill(Wire.on_color);
down_btn.graphics.drawPolyStar(0, 0, 8, 3, 0, 90);
c.gfx.addChild(down_btn);
up_btn.on('click', function() {
off_time++;
c.data.off_time = off_time;
renderLabel();
});
down_btn.on('click', function() {
off_time--;
if (off_time < 1) off_time = 1;
c.data.off_time = off_time;
renderLabel();
});
c.gfx.updateCache();
return c;
}
function makeDelay(x, y, delay) {
var c = new Circuit('delay', 1, 1);
c.add(x, y);
c.determinePowerState = function determinePowerState(inputs) {
return inputs[0];
};
// set delay if provided
if (typeof delay !== 'undefined') c.delay = delay;
// create label
var label = new createjs.Text(c.delay, '18px Arial', '#FFF');
label.textAlign = 'center';
label.cache(-c.chip_radius, 0, c.chip_radius * 2, 20);
c.draggable.addChild(label);
label.x = 0;
label.y = -10;
function renderLabel() {
label.text = c.delay;
label.updateCache();
c.gfx.updateCache();
app.needs_update = true;
}
// create step buttons
var up_btn = new createjs.Shape();
up_btn.y = -22;
up_btn.graphics.beginFill(Wire.on_color);
up_btn.graphics.drawPolyStar(0, 0, 8, 3, 0, -90);
c.gfx.addChild(up_btn);
var down_btn = new createjs.Shape();
down_btn.y = 22;
down_btn.graphics.beginFill(Wire.on_color);
down_btn.graphics.drawPolyStar(0, 0, 8, 3, 0, 90);
c.gfx.addChild(down_btn);
up_btn.on('click', function() {
c.delay++;
renderLabel();
});
down_btn.on('click', function() {
c.delay--;
if (c.delay < 1) c.delay = 1;
renderLabel();
});
c.gfx.updateCache();
return c;
}
// basic circuit
var Circuit = (function CircuitFactory() {
// track unique circuit ids
var next_circuit_id = 0;
// constructor
var Circuit = function Circuit(type, inputs, outputs, label) {
inputs = inputs || 0;
outputs = outputs || 0;
// unique circuit id
this.id = next_circuit_id++;
// type of circuit
this.type = type;
// wrapper helps with rendering cached graphics at high PPI
// this.gfx_wrap = new createjs.Container();
// container for all children elements
this.gfx = new createjs.Container();
// container for elements that allow dragging circuit
this.draggable = new createjs.Container();
this.draggable.mouseChildren = false;
// chip base (draggable part)
this.chip_radius = 40;
this.chip = new createjs.Shape();
this.chip.graphics.setStrokeStyle(2);
this.chip.graphics.beginStroke('#777');
this.chip.graphics.beginFill('#555');
this.chip.graphics.drawCircle(0, 0, this.chip_radius);
// optional label (also draggable)
this.label = null;
// assemble
this.draggable.addChild(this.chip);
this.gfx.addChild(this.draggable);
// create label if provided
if (label) {
this.label = new createjs.Text(label, 'bold 14px Arial', '#FFF');
this.label.textAlign = 'center';
var label_rect = this.label.getBounds();
this.label.cache(label_rect.x, label_rect.y, label_rect.width, label_rect.height);
this.label.x = 0;
this.label.y = -8;
this.draggable.addChild(this.label);
}
// whether circuit it broadcasting power
this.has_power = false;
// optional callback fired when circuit's power changes
// -> passed the current power state
this.powerChanged;
// connectors
this.inputs = [];
this.outputs = [];
// simple array of booleans corresponding to power states of inputs (initially all false)
// updated by inputChange()
this.simple_inputs = [];
// how many ticks required for signal to propagate through chip
this.delay = 1;
// arbitrary data store (saved)
this.data = {};
// handler to determine power state of circuit
// -> passed this.simple_inputs
// <- should return a boolean representing power state
this.determinePowerState;
// add connectors
var angle_space;
var angle_start;
// input
angle_space = Math.PI / 6;
angle_start = -angle_space * (inputs - 1) / 2 - Math.PI / 2;
for (var i = 0; i < inputs; i++) {
var current_angle = angle_space * i + angle_start;
var connector = new Connector('input', this);
this.inputs.push(connector);
connector.index = i;
connector.gfx.x = Math.sin(current_angle) * this.chip_radius;
connector.gfx.y = -Math.cos(current_angle) * this.chip_radius;
this.gfx.addChild(connector.gfx);
// build simple_inputs array as well
this.simple_inputs.push(false);
}
// output
angle_space = Math.PI / 6;
angle_start = -angle_space * (outputs - 1) / 2 + Math.PI / 2;
for (var i = 0; i < outputs; i++) {
var current_angle = angle_space * i + angle_start;
var connector = new Connector('output', this);
this.outputs.push(connector);
connector.index = i;
connector.gfx.x = Math.sin(current_angle) * this.chip_radius;
connector.gfx.y = -Math.cos(current_angle) * this.chip_radius;
this.gfx.addChild(connector.gfx);
}
// wire up events
this.draggable.on('mousedown', mouseDownHandler, this);
this.draggable.on('pressmove', pressMoveHandler, this);
this.draggable.on('pressup', pressUpHandler, this);
// cache circuit
var cache_radius = this.chip_radius + connector_radius + 1;
this.gfx.cache(-cache_radius, -cache_radius, cache_radius * 2, cache_radius * 2);
};
// static methods
// -----------------------
Circuit.findById = function findById(id) {
for (var i = this.active.length - 1; i >= 0; i--) {
var c = this.active[i];
if (c.id === id)
return c;
}
return null;
};
Circuit.setNextId = function setNextId(id) {
next_circuit_id = id;
};
// instance methods
// -----------------------
// updates position of circuit and redraws connected wires
Circuit.prototype.setPosition = function setPosition(x, y) {
var app = window.app;
app.needs_update = true;
// set position
this.gfx.x = x;
this.gfx.y = y;
// update global coordinates of all connectors and redraw wires
var temp_pt = new createjs.Point();
for (var i = 0, len = this.inputs.length; i < len; i++) {
var connector = this.inputs[i];
this.gfx.localToLocal(connector.gfx.x, connector.gfx.y, app.wires, temp_pt);
connector.globalX = temp_pt.x;
connector.globalY = temp_pt.y;
for (var n = connector.wires.length - 1; n >= 0; n--) {
connector.wires[n].draw();
}
}
for (var i = 0, len = this.outputs.length; i < len; i++) {
var connector = this.outputs[i];
this.gfx.localToLocal(connector.gfx.x, connector.gfx.y, app.wires, temp_pt);
connector.globalX = temp_pt.x;
connector.globalY = temp_pt.y;
for (var n = connector.wires.length - 1; n >= 0; n--) {
connector.wires[n].draw();
}
}
};
// input power state change
Circuit.prototype.inputChange = function inputChange() {
// update simple inputs array
for (var i = this.inputs.length - 1; i >= 0; i--) {
var c = this.inputs[i];
this.simple_inputs[i] = c.wires.length && c.wires[0].has_power;
}
// determine new power state
var new_power_state = this.determinePowerState(this.simple_inputs);
// broadcast power state if it changed
if (this.has_power !== new_power_state) {
this.has_power = new_power_state;
this.broadcastPower();
}
};
// broadcast current power state
Circuit.prototype.broadcastPower = function broadcastPower() {
app.needs_update = true;
// callback
if (this.powerChanged) this.powerChanged(this.has_power);
// send signal through wires
var delay = app.propagation_delay;
for (var i = this.outputs.length - 1; i >= 0; i--) {
var c = this.outputs[i];
for (var n = c.wires.length - 1; n >= 0; n--) {
var w = c.wires[n];
setTimeout(w.powerChange.bind(w, this.has_power), this.delay * delay);
}
}
};
// add circuit to stage (at specified position) and active array
Circuit.prototype.add = function add(x, y) {
app.needs_update = true;
app.circuits.addChild(this.gfx);
Circuit.active.push(this);
this.setPosition(x, y);
};
// remove circuit from stage and active array, recycling connected wires
Circuit.prototype.remove = function remove() {
app.needs_update = true;
// remove from display list
app.circuits.removeChild(this.gfx);
// remove from active circuits array
for (var i = Circuit.active.length - 1; i >= 0; i--) {
var c = Circuit.active[i];
if (c.id === this.id) {
Circuit.active.splice(i, 1);
// recycle connected wires
for (var n = this.inputs.length - 1; n >= 0; n--) {
this.inputs[n].recycleWires();
}
for (var n = this.outputs.length - 1; n >= 0; n--) {
this.outputs[n].recycleWires();
}
break;
}
}
};
// drag events
var drag_offset = new createjs.Point();
var remove_box_size = 50;
function mouseDownHandler(evt) {
app.delete_btn.show();
drag_offset.x = evt.localX;
drag_offset.y = evt.localY;
}
function pressMoveHandler(evt) {
if (evt.stageX < remove_box_size*app.scale && evt.stageY < remove_box_size*app.scale) {
app.delete_btn.active();
}
else{
app.delete_btn.inactive();
}
this.setPosition(this.gfx.x + evt.localX - drag_offset.x, this.gfx.y + evt.localY - drag_offset.y);
}
function pressUpHandler(evt) {
app.delete_btn.inactive().hide();
if (evt.stageX < remove_box_size*app.scale && evt.stageY < remove_box_size*app.scale) {
this.remove();
}