You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
3.3 KiB

function createEtLogique(x, y, id) {
var imageObj = new Image();
imageObj.onload = function () {
var et = new Konva.Image({
x: x,
y: y,
image: imageObj,
width: 100,
height: 50,
id: id,
});
layer.add(et);
layer.batchDraw();
};
imageObj.src = '../img/logiqueet.png';
imageObj.id = id;
var logique = {
name: id,
x: x,
y: y,
type: "et",
id1: null,
id2: null,
id3: null,
}
logiques.push(logique);
}
function whatIsElement(element) {
if (findLogique(element.name) == null) {
return "switch";
} else {
return "logique";
}
}
function setLine(logiqueElement, lineId, lineName) {
logiques.forEach(function (element, index) {
if (element.name === logiqueElement.name) {
Object.keys(element).map(function(objectKey, index) {
if(objectKey == lineId){
element[objectKey] = lineName;
}
});
}
});
}
function createLink(entre, sortie) {
//entre est une porte logique
switch (whatIsElement(entre)) {
case "switch":
switch (whatIsElement(sortie)) {
case "logique":
switch (giveLineId(sortie.name)) {
case "id1":
let switchMidX = entre.getX() + entre.getWidth();
let switchMidY = entre.getY() + entre.getHeight() / 2;
let logiqueMidX = sortie.x;
let logiqueMidY = sortie.y + (50 / 3);
createLine([switchMidX, switchMidY, logiqueMidX, logiqueMidY], "line1");
setLine(sortie, "id1", "line1");
break;
case "id2":
createLine([entre.getX() + entre.getWidth(), entre.getY() + entre.getHeight() / 2, sortie.x, sortie.y + (50 / 3) * 2], "line2");
setLine(sortie, "id2", "line2");
break;
}
}
}
}
function createLine(points, id) {
let line = new Konva.Line({
points: points,
stroke: 'black',
strokeWidth: 3,
lineCap: 'round',
lineJoin: 'round',
id: id,
});
layer.add(line);
}
function createSwitch(num, id, x, y) {
var rect1 = new Konva.Rect({
x: x,
y: y,
width: 100,
height: 50,
stroke: 'black',
strokeWidth: 4,
id: id,
});
layer.add(rect1);
var simpleText = new Konva.Text({
x: x,
y: y,
text: '0',
fontSize: 30,
fontFamily: 'Calibri',
fill: 'black',
id: "text" + num,
});
layer.add(simpleText);
rect1.on('click', function () {
var et = stage.findOne("#text" + num);
var line = stage.findOne("#line" + num);
console.log("#line" + num);
var text = et.text() == '1' ? '0' : '1';
var colorline = et.text() == '1' ? 'black' : '#f6cd61';
line.stroke(colorline);
et.text(text);
layer.draw();
});
}
function isElementExisting(elementId) {
if (stage.findOne("#" + elementId) != null) {
return true;
}
return false;
}
function checkLineSwitch(switchId) {
if (stage.findOne("#" + switchId) != null) {
return true;
}
return false;
}
function initLayer() {
var imageObj2 = new Image();
imageObj2.onload = function () {
var yoda = new Konva.Image({
x: 500,
y: 50,
image: imageObj2,
width: 50,
height: 50,
id: "et",
});
layer.add(yoda);
layer.batchDraw();
};
imageObj2.src = '../img/idea_white.png';
}