Skip to content

Commit c105002

Browse files
committed
fix: #2463 update event handling for field-bitmap to support touch
This includes debug logging. A followup commit will remove the debugging.
1 parent e3057fc commit c105002

1 file changed

Lines changed: 61 additions & 20 deletions

File tree

plugins/field-bitmap/src/field-bitmap.ts

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
4141
private valToPaintWith?: number;
4242
buttonOptions: Buttons;
4343
pixelSize: number;
44-
pixelColours: {empty: string; filled: string};
44+
pixelColours: { empty: string; filled: string };
4545
fieldHeight?: number;
4646

4747
/**
@@ -60,8 +60,8 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
6060

6161
this.SERIALIZABLE = true;
6262
this.CURSOR = 'default';
63-
this.buttonOptions = {...DEFAULT_BUTTONS, ...config?.buttons};
64-
this.pixelColours = {...DEFAULT_PIXEL_COLOURS, ...config?.colours};
63+
this.buttonOptions = { ...DEFAULT_BUTTONS, ...config?.buttons };
64+
this.pixelColours = { ...DEFAULT_PIXEL_COLOURS, ...config?.colours };
6565

6666
// Configure value, height, and width
6767
const currentValue = this.getValue();
@@ -292,11 +292,15 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
292292
// This prevents the normal max-height from adding a scroll bar for large images.
293293
Blockly.DropDownDiv.getContentDiv().classList.add('contains-bitmap-editor');
294294

295-
this.bindEvent(dropdownEditor, 'mouseup', this.onMouseUp);
296-
this.bindEvent(dropdownEditor, 'mouseleave', this.onMouseUp);
297-
this.bindEvent(dropdownEditor, 'dragstart', (e: Event) => {
298-
e.preventDefault();
299-
});
295+
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
301+
this.bindEvent(dropdownEditor, 'touchmove', (e: Event) => {
302+
e.preventDefault();
303+
});
300304

301305
this.editorPixels = [];
302306
for (let r = 0; r < this.imgHeight; r++) {
@@ -314,16 +318,23 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
314318
? this.pixelColours.filled
315319
: this.pixelColours.empty;
316320

317-
// Handle clicking a pixel
318-
this.bindEvent(button, 'mousedown', () => {
319-
this.onMouseDownInPixel(r, c);
320-
return true;
321-
});
321+
// Set the custom data attributes for row and column indices
322+
button.setAttribute('data-row', r.toString());
323+
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+
// });
322337

323-
// Handle dragging into a pixel when mouse is down
324-
this.bindEvent(button, 'mouseenter', () => {
325-
this.onMouseEnterPixel(r, c);
326-
});
327338
}
328339
pixelContainer.appendChild(rowDiv);
329340
}
@@ -472,6 +483,35 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
472483
return newVal;
473484
}
474485

486+
/**
487+
*
488+
* @param e
489+
*/
490+
private onPointerDown(e: PointerEvent) {
491+
const currentElement = document.elementFromPoint(e.clientX, e.clientY);
492+
const rowIndex = currentElement?.getAttribute('data-row');
493+
const colIndex = currentElement?.getAttribute('data-col');
494+
console.log("pointer down on " + rowIndex + ", " + colIndex + " element " + currentElement);
495+
if (rowIndex && colIndex) {
496+
this.onMouseDownInPixel(parseInt(rowIndex), parseInt(colIndex));
497+
}
498+
}
499+
500+
/**
501+
*
502+
* @param e
503+
*/
504+
private onPointerMove(e: PointerEvent) {
505+
const currentElement = document.elementFromPoint(e.clientX, e.clientY);
506+
const rowIndex = currentElement?.getAttribute('data-row');
507+
const colIndex = currentElement?.getAttribute('data-col');
508+
console.log("pointer moving on " + rowIndex + ", " + colIndex);
509+
if (rowIndex && colIndex) {
510+
this.onMouseEnterPixel(parseInt(rowIndex), parseInt(colIndex));
511+
}
512+
e.preventDefault();
513+
}
514+
475515
/**
476516
* Called when a mousedown event occurs within the bounds of a pixel.
477517
*
@@ -508,7 +548,8 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
508548
* Resets mouse state (e.g. After either a mouseup event or if the mouse
509549
* leaves the editor area).
510550
*/
511-
private onMouseUp() {
551+
private onPointerUp(e: PointerEvent) {
552+
console.log("onMouseUp " + e.pointerType);
512553
this.mouseIsDown = false;
513554
this.valToPaintWith = undefined;
514555
}
@@ -594,10 +635,10 @@ export class FieldBitmap extends Blockly.Field<number[][]> {
594635
private bindEvent(
595636
element: HTMLElement,
596637
eventName: string,
597-
callback: (e: Event) => void,
638+
callback: (e: PointerEvent) => void,
598639
) {
599640
this.boundEvents.push(
600-
Blockly.browserEvents.conditionalBind(element, eventName, this, callback),
641+
Blockly.browserEvents.bind(element, eventName, this, callback),
601642
);
602643
}
603644

0 commit comments

Comments
 (0)