2018-03-27 19:20:22 +00:00
|
|
|
function ConnectorCutter(config){
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
self.config = config;
|
|
|
|
self.sim = config.sim;
|
|
|
|
|
|
|
|
// Connecting/Cutting
|
|
|
|
self.connectFrom = null;
|
|
|
|
self.connectTo = null;
|
|
|
|
self.isCutting = false;
|
|
|
|
self.cutTrail = [];
|
|
|
|
|
|
|
|
// Update!
|
|
|
|
self.state = 0; // 0-nothing | 1-connecting | 2-cutting
|
|
|
|
self.update = function(){
|
|
|
|
|
|
|
|
var mouse = self.sim.mouse;
|
|
|
|
|
2018-04-03 17:32:26 +00:00
|
|
|
// only if sim is NOT RUNNING
|
|
|
|
if(!Simulations.IS_RUNNING){
|
|
|
|
|
|
|
|
// JUST CLICKED, and state=0... can either start connecting or cutting!
|
|
|
|
if(mouse.justPressed && self.state===0){
|
|
|
|
|
|
|
|
// Clicked on a peep?
|
|
|
|
var peepClicked = self.sim.getHoveredPeep(0);
|
|
|
|
if(peepClicked){
|
|
|
|
self.state = 1; // START CONNECTING
|
|
|
|
self.connectFrom = peepClicked;
|
|
|
|
}else{
|
|
|
|
self.state = 2; // START ERASING
|
|
|
|
}
|
2018-03-27 19:20:22 +00:00
|
|
|
|
2018-04-03 17:32:26 +00:00
|
|
|
}
|
2018-03-27 19:20:22 +00:00
|
|
|
|
2018-04-03 17:32:26 +00:00
|
|
|
// JUST RELEASED, and state!=0... can either stop connecting or cutting!
|
|
|
|
if(mouse.justReleased && self.state!==0){
|
2018-03-27 19:20:22 +00:00
|
|
|
|
2018-04-03 17:32:26 +00:00
|
|
|
// End connect?
|
|
|
|
if(self.state==1){
|
|
|
|
var peepReleased = self.sim.getHoveredPeep(20);
|
|
|
|
if(peepReleased){
|
|
|
|
self.sim.addConnection(self.connectFrom, peepReleased);
|
|
|
|
}
|
2018-03-27 19:20:22 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 17:32:26 +00:00
|
|
|
// back to normal
|
|
|
|
self.state = 0;
|
|
|
|
|
|
|
|
}
|
2018-03-27 19:20:22 +00:00
|
|
|
|
2018-04-03 17:32:26 +00:00
|
|
|
}else{
|
|
|
|
self.state = 0;
|
2018-03-27 19:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// In "NORMAL" state... tell Pencil what frame to go to
|
|
|
|
if(self.state==0){
|
2018-04-03 17:32:26 +00:00
|
|
|
if(!Simulations.IS_RUNNING){
|
|
|
|
var peepHovered = self.sim.getHoveredPeep(0);
|
|
|
|
pencil.gotoFrame( peepHovered ? 1 : 0 );
|
|
|
|
}else{
|
|
|
|
pencil.gotoFrame(0);
|
|
|
|
}
|
2018-03-27 19:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// In "CONNECTING" state... show where to connect to
|
|
|
|
if(self.state==1){
|
|
|
|
|
|
|
|
// Connect to a nearby hovered peep?
|
2018-03-28 14:57:19 +00:00
|
|
|
var peepHovered = self.sim.getHoveredPeep(20);
|
2018-03-27 19:20:22 +00:00
|
|
|
if(peepHovered==self.connectFrom) peepHovered=null; // if same, nah
|
|
|
|
self.connectTo = peepHovered ? peepHovered : mouse;
|
|
|
|
|
|
|
|
// Pencil's always DARK
|
|
|
|
pencil.gotoFrame(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// In "CUTTING" state... cut intersected lines! & add to trail
|
|
|
|
if(self.state==2){
|
|
|
|
|
|
|
|
// Try cutting
|
|
|
|
var line = [mouse.lastX, mouse.lastY, mouse.x, mouse.y];
|
|
|
|
self.sim.tryCuttingConnections(line);
|
|
|
|
|
|
|
|
// Add to trail
|
|
|
|
self.cutTrail.unshift([mouse.x,mouse.y]); // add to start
|
|
|
|
if(self.cutTrail.length>10){
|
|
|
|
self.cutTrail.pop(); // remove from end
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pencil's always RED
|
|
|
|
pencil.gotoFrame(2);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
self.cutTrail.pop(); // oh, and if not cutting, pop from end anyway
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
self.draw = function(ctx){
|
|
|
|
|
|
|
|
ctx.lineJoin = "round";
|
|
|
|
ctx.lineCap = "round";
|
|
|
|
|
|
|
|
// Connecting!
|
|
|
|
if(self.state==1){
|
2018-04-01 11:50:29 +00:00
|
|
|
var tempConnection = new Connection({
|
2018-04-01 16:34:52 +00:00
|
|
|
from:self.connectFrom, to:self.connectTo,
|
|
|
|
sim:self.sim
|
2018-04-01 11:50:29 +00:00
|
|
|
});
|
|
|
|
ctx.save();
|
|
|
|
ctx.globalAlpha = 0.5;
|
|
|
|
tempConnection.draw(ctx);
|
|
|
|
ctx.restore();
|
2018-03-27 19:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cutting!
|
|
|
|
if(self.cutTrail.length>0){
|
|
|
|
ctx.strokeStyle = "#dd4040";
|
|
|
|
ctx.lineWidth = 2;
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(self.cutTrail[0][0], self.cutTrail[0][1]);
|
|
|
|
for(var i=1; i<self.cutTrail.length; i++){
|
|
|
|
ctx.lineTo(self.cutTrail[i][0], self.cutTrail[i][1]);
|
|
|
|
}
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|