@@ -37,7 +37,7 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
3737 private editorPixels : HTMLElement [ ] [ ] | null = null ;
3838 private blockDisplayPixels : SVGElement [ ] [ ] | null = null ;
3939 /** Stateful variables */
40- private mouseIsDown = false ;
40+ private pointerIsDown = false ;
4141 private valToPaintWith ?: number ;
4242 buttonOptions : Buttons ;
4343 pixelSize : number ;
@@ -293,14 +293,15 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
293293 Blockly . DropDownDiv . getContentDiv ( ) . classList . add ( 'contains-bitmap-editor' ) ;
294294
295295 this . bindEvent ( dropdownEditor , 'pointermove' , this . onPointerMove ) ;
296- this . bindEvent ( dropdownEditor , 'pointerup' , this . onPointerUp ) ;
297- this . bindEvent ( dropdownEditor , 'pointerleave' , this . onPointerUp ) ;
298- this . bindEvent ( dropdownEditor , 'pointerdown' , this . onPointerDown ) ;
299- this . bindEvent ( dropdownEditor , 'pointercancel' , this . onPointerUp ) ;
300- // Stop the browser from intercepting touch events and cancelling the event
296+ this . bindEvent ( dropdownEditor , 'pointerup' , this . onPointerEnd ) ;
297+ this . bindEvent ( dropdownEditor , 'pointerleave' , this . onPointerEnd ) ;
298+ this . bindEvent ( dropdownEditor , 'pointerdown' , this . onPointerStart ) ;
299+ this . bindEvent ( dropdownEditor , 'pointercancel' , this . onPointerEnd ) ;
300+ // Stop the browser from handling touch events and cancelling the event.
301301 this . bindEvent ( dropdownEditor , 'touchmove' , ( e : Event ) => {
302302 e . preventDefault ( ) ;
303- } ) ;
303+ } ) ;
304+
304305
305306 this . editorPixels = [ ] ;
306307 for ( let r = 0 ; r < this . imgHeight ; r ++ ) {
@@ -321,20 +322,6 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
321322 // Set the custom data attributes for row and column indices
322323 button . setAttribute ( 'data-row' , r . toString ( ) ) ;
323324 button . setAttribute ( 'data-col' , c . toString ( ) ) ;
324-
325- // // Handle clicking a pixel
326- // this.bindEvent(button, 'mousedown', () => {
327- // console.log("handling pointer down")
328- // this.onMouseDownInPixel(r, c);
329- // return true;
330- // });
331-
332- // // Handle dragging into a pixel when mouse is down
333- // this.bindEvent(button, 'mouseenter', () => {
334- // console.log("handling pointer enter");
335- // this.onMouseEnterPixel(r, c);
336- // });
337-
338325 }
339326 pixelContainer . appendChild ( rowDiv ) ;
340327 }
@@ -487,13 +474,14 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
487474 *
488475 * @param e
489476 */
490- private onPointerDown ( e : PointerEvent ) {
477+ private onPointerStart ( e : PointerEvent ) {
491478 const currentElement = document . elementFromPoint ( e . clientX , e . clientY ) ;
492479 const rowIndex = currentElement ?. getAttribute ( 'data-row' ) ;
493480 const colIndex = currentElement ?. getAttribute ( 'data-col' ) ;
494- console . log ( "pointer down on " + rowIndex + ", " + colIndex + " element " + currentElement ) ;
495481 if ( rowIndex && colIndex ) {
496- this . onMouseDownInPixel ( parseInt ( rowIndex ) , parseInt ( colIndex ) ) ;
482+ this . onPointerDownInPixel ( parseInt ( rowIndex ) , parseInt ( colIndex ) ) ;
483+ this . pointerIsDown = true ;
484+ e . preventDefault ( ) ;
497485 }
498486 }
499487
@@ -502,40 +490,40 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
502490 * @param e
503491 */
504492 private onPointerMove ( e : PointerEvent ) {
493+ if ( ! this . pointerIsDown ) {
494+ return ;
495+ }
505496 const currentElement = document . elementFromPoint ( e . clientX , e . clientY ) ;
506497 const rowIndex = currentElement ?. getAttribute ( 'data-row' ) ;
507498 const colIndex = currentElement ?. getAttribute ( 'data-col' ) ;
508- console . log ( "pointer moving on " + rowIndex + ", " + colIndex ) ;
509499 if ( rowIndex && colIndex ) {
510- this . onMouseEnterPixel ( parseInt ( rowIndex ) , parseInt ( colIndex ) ) ;
500+ this . updatePixelValue ( parseInt ( rowIndex ) , parseInt ( colIndex ) ) ;
511501 }
512502 e . preventDefault ( ) ;
513503 }
514504
515505 /**
516- * Called when a mousedown event occurs within the bounds of a pixel.
506+ * Starts an interaction with the bitmap dropdown when there's a pointerdown
507+ * within one of the pixels in the editor.
517508 *
518509 * @param r Row number of grid.
519510 * @param c Column number of grid.
520511 */
521- private onMouseDownInPixel ( r : number , c : number ) {
512+ private onPointerDownInPixel ( r : number , c : number ) {
522513 // Toggle that pixel to the opposite of its value
523514 const newPixelValue = 1 - this . getPixel ( r , c ) ;
524515 this . setPixel ( r , c , newPixelValue ) ;
525- this . mouseIsDown = true ;
516+ this . pointerIsDown = true ;
526517 this . valToPaintWith = newPixelValue ;
527518 }
528519
529520 /**
530- * Called when the mouse drags over a pixel in the editor.
521+ * Sets the specified pixel in the editor to the current value being painted .
531522 *
532523 * @param r Row number of grid.
533524 * @param c Column number of grid.
534525 */
535- private onMouseEnterPixel ( r : number , c : number ) {
536- if ( ! this . mouseIsDown ) {
537- return ;
538- }
526+ private updatePixelValue ( r : number , c : number ) {
539527 if (
540528 this . valToPaintWith !== undefined &&
541529 this . getPixel ( r , c ) !== this . valToPaintWith
@@ -545,12 +533,11 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
545533 }
546534
547535 /**
548- * Resets mouse state (e.g. After either a mouseup event or if the mouse
549- * leaves the editor area ).
536+ * Resets pointer state (e.g. After either a pointerup event or if the
537+ * gesture is canceled ).
550538 */
551- private onPointerUp ( e : PointerEvent ) {
552- console . log ( "onMouseUp " + e . pointerType ) ;
553- this . mouseIsDown = false ;
539+ private onPointerEnd ( e : PointerEvent ) {
540+ this . pointerIsDown = false ;
554541 this . valToPaintWith = undefined ;
555542 }
556543
0 commit comments