addConnectorsToCodeEditor() and checkConnectors()

draw2d-with-lines
Fabien Benetou 1 year ago
parent 7dbeeed9fd
commit 5a2794401a
  1. 51
      index.html

@ -3124,30 +3124,63 @@ function onNextPinchSplitReader(){
}, 50) // relatively cheap check, filtering on small array
}
function addConnectorsToCodeEditor( codeEditor ){
function addConnectorsToCodeEditor( codeEditor, input=true, output=true){
let el = codeEditor.element
el.addEventListener("object3dset", e => {
el.object3D.children[0].addEventListener("synccomplete", e => {
b = el.object3D.children[0]._textRenderInfo.blockBounds
w = b[2]-b[0]
h = b[3]-b[1]
el.setAttribute("line__input", `start: 0 0 0; end : -1 1 0; opacity: 1;`)
el.setAttribute("line__input__end", `start: -1 1 0; end : -2 1 0; opacity: 1;`)
el.setAttribute("line__output", `start: ${w} ${-h} 0; end : ${w+1} ${-h-1} 0; opacity: 1;`)
el.setAttribute("line__output__end", `start: ${w+1} ${-h-1} 0; end : ${w+2} ${-h-1} 0; opacity: 1;`)
// this assumes an axis aligned entity, which is correct until now
if (input){
el.setAttribute("line__input", `start: 0 0 0; end : -1 1 0; opacity: 1;`)
el.setAttribute("line__input__end", `start: -1 1 0; end : -2 1 0; opacity: 1;`)
}
if (output){
el.setAttribute("line__output", `start: ${w} ${-h} 0; end : ${w+1} ${-h-1} 0; opacity: 1;`)
el.setAttribute("line__output__end", `start: ${w+1} ${-h-1} 0; end : ${w+2} ${-h-1} 0; opacity: 1;`)
}
})
})
return el
}
function checkConnectors( a, b ){
const connectionThreshold = 1 // to adjust after tries in VR, should probably be much shorter
const far = 999
let i1 = new THREE.Vector3(far,far,far)
let i2 = new THREE.Vector3(far,far,far)
let o1 = new THREE.Vector3(far,far,far)
let o2 = new THREE.Vector3(far,far,far)
a.element.getObject3D('line__input')?.getWorldPosition(i1)
b.element.getObject3D('line__input')?.getWorldPosition(i2)
a.element.getObject3D('line__output__end')?.getWorldPosition(o1)
b.element.getObject3D('line__output__end')?.getWorldPosition(o2)
let links = []
if ( i2.distanceTo(o1) < connectionThreshold ){
links.push({source:a, target:b})
}
if ( i1.distanceTo(o2) < connectionThreshold ){
links.push({source:b, target:a})
}
return links
}
// used for testing
AFRAME.registerComponent('startfunctions', {
init: function () {
document.body.addEventListener( "highlighterready", (e) => {
let ed = addCodeEditor( 'function NodalTestSquare(x){ return x*x}', 'javascript', '-.3 2.2 -.8')
addConnectorsToCodeEditor( ed )
let ed2 = addCodeEditor( 'function NodalPrintToJXR(x){ addNewNote(x) }', 'javascript', '.4 1.9 -.8')
addConnectorsToCodeEditor( ed2 )
let ed1 = addCodeEditor( 'function NodalTestSquare(x){ return x*x}', 'javascript', '-.3 2.2 -.8')
addConnectorsToCodeEditor( ed1 )
// testing multiline
let ed2 = addCodeEditor( `function NodalPrintToJXR(x){
addNewNote(x)
}`, 'javascript', '.4 1.9 -.8')
addConnectorsToCodeEditor( ed2, true, false )
let ed3 = addCodeEditor( 'function NodalTestRandom(){ return Math.random()}', 'javascript', '-.8 2.5 -.8')
addConnectorsToCodeEditor( ed3, false, true)
setTimeout( _ => console.log( checkConnectors( editors[0], editors[2] ) ), 1000 )
// should be done after each codeEditors gets connectors added then on editor moves
}, false);
//startExperience()
//doublePinchToScale()

Loading…
Cancel
Save