@@ -236,7 +236,8 @@ function clearCanvas(canvas) {
236236
237237function drawHighlightsOnLayer ( canvasdict ) {
238238 clearCanvas ( canvasdict . highlight ) ;
239- drawModules ( canvasdict . highlight , canvasdict . layer , canvasdict . transform . s , highlightedRefs ) ;
239+ drawModules ( canvasdict . highlight , canvasdict . layer ,
240+ canvasdict . transform . s , highlightedRefs ) ;
240241}
241242
242243function drawHighlights ( ) {
@@ -268,7 +269,7 @@ function prepareCanvas(canvas, flip, transform) {
268269}
269270
270271function prepareLayer ( canvasdict ) {
271- flip = ( canvasdict . layer == "B" ) ;
272+ var flip = ( canvasdict . layer == "B" ) ;
272273 for ( c of [ "bg" , "silk" , "highlight" ] ) {
273274 prepareCanvas ( canvasdict [ c ] , flip , canvasdict . transform ) ;
274275 }
@@ -290,7 +291,7 @@ function recalcLayerScale(canvasdict) {
290291 scalefactor = 1 ;
291292 }
292293 canvasdict . transform . s = scalefactor ;
293- flip = ( canvasdict . layer == "B" ) ;
294+ var flip = ( canvasdict . layer == "B" ) ;
294295 if ( flip ) {
295296 canvasdict . transform . x = - ( ( bbox . maxx + bbox . minx ) * scalefactor + width ) * 0.5 ;
296297 } else {
@@ -311,9 +312,6 @@ function redrawCanvas(layerdict) {
311312 prepareLayer ( layerdict ) ;
312313 drawBackground ( layerdict ) ;
313314 drawHighlights ( layerdict ) ;
314- t = layerdict . transform ;
315- dbgdiv . innerHTML = "x: " + t . x + "</br>y: " + t . y + "</br>s: " + t . s +
316- "</br>panx: " + t . panx + "</br>pany: " + t . pany + "</br>zoom: " + t . zoom ;
317315}
318316
319317function resizeCanvas ( layerdict ) {
@@ -326,6 +324,21 @@ function resizeAll() {
326324 resizeCanvas ( allcanvas . back ) ;
327325}
328326
327+ function bboxScan ( layer , x , y ) {
328+ var result = [ ] ;
329+ for ( var i in pcbdata . modules ) {
330+ var module = pcbdata . modules [ i ] ;
331+ if ( module . layer == layer ) {
332+ var b = module . bbox ;
333+ if ( b . pos [ 0 ] <= x && b . pos [ 0 ] + b . size [ 0 ] >= x &&
334+ b . pos [ 1 ] <= y && b . pos [ 1 ] + b . size [ 1 ] >= y ) {
335+ result . push ( module . ref ) ;
336+ }
337+ }
338+ }
339+ return result ;
340+ }
341+
329342function handleMouseDown ( e , layerdict ) {
330343 if ( e . which != 1 ) {
331344 return ;
@@ -334,12 +347,37 @@ function handleMouseDown(e, layerdict) {
334347 e . stopPropagation ( ) ;
335348 layerdict . transform . mousestartx = e . offsetX ;
336349 layerdict . transform . mousestarty = e . offsetY ;
350+ layerdict . transform . mousedownx = e . offsetX ;
351+ layerdict . transform . mousedowny = e . offsetY ;
337352 layerdict . transform . mousedown = true ;
338353}
339354
355+ function handleMouseClick ( e , layerdict ) {
356+ var x = e . offsetX ;
357+ var y = e . offsetY ;
358+ var t = layerdict . transform ;
359+ if ( layerdict . layer == "B" ) {
360+ x = ( 2 * x / t . zoom - t . panx + t . x ) / - t . s ;
361+ } else {
362+ x = ( 2 * x / t . zoom - t . panx - t . x ) / t . s ;
363+ }
364+ y = ( 2 * y / t . zoom - t . y - t . pany ) / t . s ;
365+ var reflist = bboxScan ( layerdict . layer , x , y ) ;
366+ if ( reflist . length > 0 ) {
367+ modulesClicked ( reflist ) ;
368+ }
369+ }
370+
340371function handleMouseUp ( e , layerdict ) {
341372 e . preventDefault ( ) ;
342373 e . stopPropagation ( ) ;
374+ if ( e . which == 1 &&
375+ layerdict . transform . mousedown &&
376+ layerdict . transform . mousedownx == e . offsetX &&
377+ layerdict . transform . mousedowny == e . offsetY ) {
378+ // This is just a click
379+ handleMouseClick ( e , layerdict ) ;
380+ }
343381 layerdict . transform . mousedown = false ;
344382 if ( e . which == 3 ) {
345383 // Reset pan and zoom on right click.
0 commit comments