@ -2,7 +2,7 @@ function createEnd() {
//find X and Y
var X = stage . width ( ) - stage . width ( ) / 100 * 5 ;
var X = stage . width ( ) - stage . width ( ) / 100 * 5 ;
var Y = stage . height ( ) / 2 ;
var endShape = new Konva . RegularPolygon ( {
@ -13,7 +13,7 @@ function createEnd() {
fill : '#9a9a9a' ,
stroke : 'black' ,
strokeWidth : 4 ,
id : "end" ,
id : "end" ,
} ) ;
end = {
name : "end" ,
@ -21,17 +21,17 @@ function createEnd() {
y : Y ,
type : "end" ,
position : 1 ,
let : 1 ,
let : 1 ,
}
end . x = end . x - endShape . width ( ) / 2 ;
layer . add ( endShape ) ;
}
}
function createSwitch ( id , x , y ) {
function createSwitch ( id , x , y ) {
var switche = {
id : id ,
x : x ,
y : y ,
id : id ,
x : x ,
y : y ,
} ;
switchsInfo . push ( switche ) ;
switchsInfoCopy . push ( switche ) ;
@ -50,24 +50,24 @@ function createEnd() {
id3 : null ,
} ) ;
layer . add ( rect1 ) ;
}
}
function createLine ( points , id ) {
function createLine ( points , id ) {
let startX = points [ 0 ] , startY = points [ 1 ] , endX = points [ 2 ] , endY = points [ 3 ] ;
let distance20 = ( endX - startX ) / 100 * pourcentageBreak ;
let newPoints = [ startX , startY , startX + distance20 , startY , endX - distance20 , endY , endX , endY ] ;
let distance20 = ( endX - startX ) / 100 * pourcentageBreak ;
let newPoints = [ startX , startY , startX + distance20 , startY , endX - distance20 , endY , endX , endY ] ;
let line = new Konva . Line ( {
points : newPoints ,
fill : 'blue' ,
fill : 'blue' ,
stroke : colorLineInnactive ,
strokeWidth : lineStroke ,
id : id ,
} ) ;
layer . add ( line ) ;
}
}
function createLink ( entre , sortie ) {
function createLink ( entre , sortie ) {
//entre est une porte logique
lineId = lineCount . length ;
lineCount . push ( 1 ) ;
@ -75,23 +75,26 @@ function createEnd() {
case "switch" :
switch ( whatIsElement ( sortie ) ) {
case "logique" :
switch ( giveLineId ( sortie . name , entre ) ) {
switch ( giveLineId ( sortie . name , entre ) ) {
case "id1" :
let switchMidX = entre . getX ( ) + entre . getWidth ( ) ;
let switchMidY = entre . getY ( ) + entre . getHeight ( ) / 2 ;
let logiqueMidX = sortie . x ;
if ( sortie . type === "inv" ) {
if ( sortie . type === "inv" ) {
var logiqueMidY = sortie . y + ( imageHeight / 2 ) ;
} else {
} else {
var logiqueMidY = sortie . y + ( imageHeight / 3 ) ;
}
createLine ( [ switchMidX , switchMidY , logiqueMidX , logiqueMidY ] , "line" + lineId ) ;
setLine ( sortie , "id1" , "line" + lineId ) ;
let truc = entre . x ;
entre . id3 = "line" + lineId ;
break ;
case "id2" :
createLine ( [ entre . getX ( ) + entre . getWidth ( ) , entre . getY ( ) + entre . getHeight ( ) / 2 , sortie . x , sortie . y + ( imageHeight / 3 ) * 2 ] , "line" + lineId ) ;
setLine ( sortie , "id2" , "line" + lineId ) ;
entre . id3 = "line" + lineId ;
@ -102,15 +105,15 @@ function createEnd() {
case "logique" :
switch ( whatIsElement ( sortie ) ) {
case "logique" :
switch ( giveLineId ( sortie . name , entre ) ) {
switch ( giveLineId ( sortie . name , entre ) ) {
case "id1" :
let switchMidX = entre . x + imageWidth ;
let switchMidY = entre . y + imageHeight / 2 ;
let logiqueMidX = sortie . x ;
if ( sortie . type === "inv" ) {
if ( sortie . type === "inv" ) {
var logiqueMidY = sortie . y + ( imageHeight / 2 ) ;
} else {
} else {
var logiqueMidY = sortie . y + ( imageHeight / 3 ) ;
}
createLine ( [ switchMidX , switchMidY , logiqueMidX , logiqueMidY ] , "line" + lineId ) ;
@ -132,10 +135,13 @@ function createEnd() {
break ;
}
break ;
default :
console . log ( "error" ) ;
break ;
}
}
function createLogique ( x , y , id , type ) {
}
function createLogique ( x , y , id , type ) {
var imageObj = new Image ( ) ;
imageObj . onload = function ( ) {
var et = new Konva . Image ( {
@ -153,8 +159,8 @@ function createEnd() {
layer . add ( et ) ;
layer . batchDraw ( ) ;
} ;
switch ( type ) {
case "et" :
switch ( type ) {
case "et" :
imageObj . src = pathImg + imageLogiqueEt ;
break ;
case "ou" :
@ -182,62 +188,69 @@ function createEnd() {
type : type ,
id1 : null ,
id2 : null ,
id3 : null ,
id3 : [ ] ,
}
logiques . push ( logique ) ;
}
}
function switchCreator ( number ) {
function switchCreator ( number ) {
numberOfSwitch = number ;
let split = number + 1 ;
let space = ( stage . height ( ) - stage . height ( ) / 100 * 25 ) / split ;
for ( let i = 1 ; i < number + 1 ; i ++ ) {
createSwitch ( "s" + i , 20 , ( i ) * space ) ;
}
let space = ( stage . height ( ) - stage . height ( ) / 100 * 25 ) / split ;
for ( let i = 1 ; i < number + 1 ; i ++ ) {
createSwitch ( "s" + i , 20 , ( i ) * space ) ;
}
}
function insertLogiqueColonne ( nomLogique , TypeLogique , colonne ) {
var X = stage . width ( ) / ( colonneTot + 1 ) * ( colonne + 1 ) ;
function insertLogiqueColonne ( nomLogique , TypeLogique , colonne ) {
var X = stage . width ( ) / ( colonneTot + 1 ) * ( colonne + 1 ) ;
//calcul position Y
if ( numberPerColonne [ colonne ] == 1 ) {
if ( numberPerColonne [ colonne ] == 1 ) {
var Y = stage . height ( ) / 3 ;
} else {
let numberAlreadyAdded = liveColonneNumber [ colonne ] . length + 1 ;
let split = ( stage . height ( ) - stage . height ( ) / 100 * 25 ) / ( numberPerColonne [ colonne ] + 1 ) ;
} else {
let numberAlreadyAdded = liveColonneNumber [ colonne ] . length + 1 ;
let split = ( stage . height ( ) - stage . height ( ) / 100 * 25 ) / ( numberPerColonne [ colonne ] + 1 ) ;
var Y = split * numberAlreadyAdded ;
}
createLogique ( X , Y , nomLogique , TypeLogique ) ;
createLogique ( X , Y , nomLogique , TypeLogique ) ;
liveColonneNumber [ colonne ] . push ( 1 ) ;
}
}
function createLinkSwitch ( ) {
function createLinkSwitch ( ) {
var logique = null ;
var distance = stage . width ( ) ;
//Trouver la porte logique la plus proche des switchs
logiques . forEach ( function ( element ) {
if ( element . id1 == null || element . id2 == null ) {
if ( element . x < distance ) {
logiques . forEach ( function ( element ) {
if ( element . type != "inv" ) {
if ( element . id1 == null || element . id2 == null ) {
if ( element . x < distance ) {
logique = element ;
distance = element . x ;
}
}
} else {
if ( element . id1 == null ) {
if ( element . x < distance ) {
logique = element ;
distance = element . x ;
}
}
}
} ) ;
var distanceSwitch = stage . height ( ) ;
var switchs = null ;
if ( switchsInfo . length > 0 ) {
switchsInfo . forEach ( function ( element ) {
if ( Math . abs ( logique . y - element . y ) < distanceSwitch ) {
if ( switchsInfo . length > 0 ) {
switchsInfo . forEach ( function ( element ) {
if ( Math . abs ( logique . y - element . y ) < distanceSwitch ) {
distanceSwitch = Math . abs ( logique . y - element . y ) ;
switchs = element ;
}
} ) ;
}
else {
switchsInfoCopy . forEach ( function ( element ) {
if ( stage . findOne ( "#" + element . id ) . id3 == null ) {
console . log ( "le switch " + element . id ) ;
if ( Math . abs ( logique . y - element . y ) < distanceSwitch ) {
console . log ( "ai" ) ;
switchsInfoCopy . forEach ( function ( element ) {
if ( stage . findOne ( "#" + element . id ) . id3 . length === 0 ) {
if ( Math . abs ( logique . y - element . y ) < distanceSwitch ) {
distanceSwitch = Math . abs ( logique . y - element . y ) ;
switchs = element ;
}
@ -245,52 +258,93 @@ function createEnd() {
} ) ;
}
switchsInfo . splice ( switchsInfo . indexOf ( switchs ) , 1 ) ;
createLink ( stage . findOne ( "#" + switchs . id ) , findLogique ( logique . name ) ) ;
}
switchsInfo . splice ( switchsInfo . indexOf ( switchs ) , 1 ) ;
createLink ( stage . findOne ( "#" + switchs . id ) , findLogique ( logique . name ) ) ;
}
function getNumberOfSwitchEmpty ( ) {
function getNumberOfSwitchEmpty ( ) {
let number = 0 ;
switchsInfoCopy . forEach ( function ( element ) {
if ( stage . findOne ( "#" + element . id ) . id3 == null )
switchsInfoCopy . forEach ( function ( element ) {
if ( stage . findOne ( "#" + element . id ) . id3 . length === 0 )
number ++ ;
} ) ;
return number ;
}
}
function createAllLinkSwitch ( ) {
while ( getNumberOfSwitchEmpty ( ) > 0 ) {
function createAllLinkSwitch ( ) {
for ( let i = 0 ; i < numberOfSwitch ; i ++ ) {
createLinkSwitch ( ) ;
}
}
/ * w h i l e ( g e t N u m b e r O f S w i t c h E m p t y ( ) > 0 ) {
createLinkSwitch ( ) ;
} * /
}
function createLinkAuto ( logiqueId ) {
function createLinkAuto ( logiqueId ) {
var logique = findLogique ( logiqueId ) ;
var ligneSupCoor = null ;
var stop = false ;
//on cherche la ligne supérieur
logiques . forEach ( function ( element ) {
if ( ! stop ) {
if ( logique . x < element . x ) {
logiques . forEach ( function ( element ) {
if ( ! stop ) {
if ( logique . x < element . x ) {
stop = true ;
ligneSupCoor = element . x ;
}
}
} ) ;
if ( ligneSupCoor != null ) {
if ( ligneSupCoor != null ) {
//on stock les portes logiques de la lignes supérieur (si il reste de la place dans leur entrees)
var ligneSup = [ ] ;
logiques . forEach ( function ( element ) {
logiques . forEach ( function ( element ) {
if ( element . x == ligneSupCoor ) {
if ( checkLogiqueLines ( element . name , "id1" ) == false || checkLogiqueLines ( element . name , "id2" ) == false ) {
if ( element . x == ligneSupCoor ) {
if ( checkLogiqueLines ( element . name , "id1" ) == false || checkLogiqueLines ( element . name , "id2" ) == false ) {
ligneSup . push ( element ) ;
}
}
} ) ;
//on tire au sort
let sortie = getRandomArbitrary ( 0 , ligneSup . length ) ;
let sortie = getRandomArbitrary ( 0 , ligneSup . length ) ;
sortie = ligneSup [ sortie ] ;
createLink ( findLogique ( logiqueId ) , findLogique ( sortie . name ) ) ;
createLink ( findLogique ( logiqueId ) , findLogique ( sortie . name ) ) ;
}
}
function createStage ( container ) {
height = document . querySelector ( "#" + container ) . offsetHeight ;
width = document . querySelector ( "#" + container ) . offsetWidth ;
if ( window . innerWidth <= 900 ) {
if ( ! mobile ) {
mobile = true ;
stage = new Konva . Stage ( {
container : container ,
width : height ,
height : width ,
rotation : 90 ,
x : height ,
} ) ;
}
stage = new Konva . Stage ( {
container : container ,
width : height ,
height : width ,
rotation : 90 ,
x : height ,
} ) ;
} else {
if ( mobile ) {
mobile = false ;
} else {
stage = new Konva . Stage ( {
container : container ,
width : width ,
height : height ,
} ) ;
}
}
stage . draw ( ) ;
}