@@ -18,6 +18,7 @@ import diffGrid from './utils/diffGrid';
1818import { getGuides } from './utils/getGuides' ;
1919import { getOutline } from './utils/getOutline' ;
2020import { getGridColorIndexes } from './utils/getGridColorIndexes' ;
21+ import { getFloodFill } from './utils/getFloodFill' ;
2122
2223type Color = [ number , number , number , number ] ;
2324
@@ -297,6 +298,14 @@ export default class PgInputPixelEditor extends HTMLElement {
297298 } ) ;
298299 }
299300
301+ clearSelection ( ) {
302+ this . #selectionPixels. forEach ( ( [ x , y ] ) => {
303+ this . #selection[ y ] [ x ] = 0 ;
304+ } ) ;
305+ this . #selectionPixels. clear ( ) ;
306+ this . $selectionPath . classList . toggle ( 'hide' , true ) ;
307+ }
308+
300309 #setSelectionPixel( x : number , y : number ) {
301310 this . #selectionPixels. set ( `${ x } ,${ y } ` , [ x , y ] ) ;
302311 this . #selection[ y ] [ x ] = 1 ;
@@ -531,19 +540,20 @@ export default class PgInputPixelEditor extends HTMLElement {
531540
532541 handleKeyDown ( event : KeyboardEvent ) {
533542 console . log ( event . shiftKey , event . ctrlKey , event . altKey , event . key ) ;
543+ this . #isShift = true ;
534544 switch ( event . key ) {
535545 case ' ' :
536546 console . log ( 'space' ) ;
537547 break ;
538548 case 'Escape' :
539549 console . log ( 'escape' ) ;
540- // Cancel editing
550+ this . clearSelection ( ) ;
541551 break ;
542552 }
543553 }
544554
545555 handleKeyUp ( event : KeyboardEvent ) {
546-
556+ this . #isShift = false ;
547557 }
548558
549559 handleContextMenu ( event : MouseEvent ) {
@@ -618,28 +628,64 @@ export default class PgInputPixelEditor extends HTMLElement {
618628 // return;
619629 //}
620630 // Single Tap
621- if ( newX === this . #startX && newY === this . #startY && this . #startColor === 1 ) {
631+ if ( newX === this . #startX && newY === this . #startY) {
622632 switch ( this . #inputMode) {
633+ case InputMode . SelectMagicWand :
634+ if ( ! event . shiftKey ) {
635+ this . clearSelection ( ) ;
636+ }
637+ const color = this . #data[ this . #layer] [ newY ] [ newX ] ;
638+ console . log ( color ) ;
639+ const pixels = getFloodFill ( this . #data[ this . #layer] , newX , newY , [ color ] ) ;
640+ pixels . forEach ( ( [ x , y ] ) => {
641+ this . #setSelectionPixel( x , y ) ;
642+ } ) ;
643+ this . $selectionPathPreview . classList . toggle ( 'hide' , true ) ;
644+ this . $selectionPath . classList . toggle ( 'hide' , false ) ;
645+ this . $selectionPath . setAttribute ( 'd' , bitmaskToPath ( this . #selection, { scale : this . size } ) ) ;
646+ break ;
623647 case InputMode . Pixel :
624- this . #setPixel( newX , newY , 0 ) ;
625- this . #data[ this . #layer] [ newY ] [ newX ] = 0 ;
648+ if ( this . #startColor === 1 ) {
649+ this . #setPixel( newX , newY , 0 ) ;
650+ this . #data[ this . #layer] [ newY ] [ newX ] = 0 ;
651+ }
626652 break ;
627653 case InputMode . Stamp :
628- this . #inputStamp. forEach ( ( point ) => {
629- this . #setPixel( newX + point [ 0 ] , newY + point [ 1 ] , 0 ) ;
630- } ) ;
654+ if ( this . #startColor === 1 ) {
655+ this . #inputStamp. forEach ( ( point ) => {
656+ this . #setPixel( newX + point [ 0 ] , newY + point [ 1 ] , 0 ) ;
657+ } ) ;
658+ }
631659 break ;
632660 }
633661 } else {
634662 switch ( this . #inputMode) {
635663 case InputMode . SelectRectangle :
636664 this . #clearSelectionPreview( ) ;
665+ if ( ! event . shiftKey ) {
666+ this . clearSelection ( ) ;
667+ }
637668 getRectanglePixels ( this . #startX, this . #startY, newX , newY ) . forEach ( ( { x, y } ) => {
638669 this . #setSelectionPixel( x , y ) ;
639670 } ) ;
640671 this . $selectionPathPreview . classList . toggle ( 'hide' , true ) ;
641672 this . $selectionPath . classList . toggle ( 'hide' , false ) ;
642673 this . $selectionPath . setAttribute ( 'd' , bitmaskToPath ( this . #selection, { scale : this . size } ) ) ;
674+ break ;
675+ case InputMode . SelectEllipse :
676+ this . #clearSelectionPreview( ) ;
677+ if ( ! event . shiftKey ) {
678+ this . clearSelection ( ) ;
679+ }
680+ getEllipsePixels ( this . #startX, this . #startY, newX , newY ) . forEach ( ( { x, y } ) => {
681+ this . #setSelectionPixel( x , y ) ;
682+ } ) ;
683+ this . $selectionPathPreview . classList . toggle ( 'hide' , true ) ;
684+ this . $selectionPath . classList . toggle ( 'hide' , false ) ;
685+ this . $selectionPath . setAttribute ( 'd' , bitmaskToPath ( this . #selection, { scale : this . size } ) ) ;
686+ break ;
687+ case InputMode . SelectLasso :
688+
643689 break ;
644690 case InputMode . Line :
645691 getLinePixels ( this . #startX, this . #startY, newX , newY ) . forEach ( ( { x, y } ) => {
@@ -862,6 +908,10 @@ export default class PgInputPixelEditor extends HTMLElement {
862908 this . #redoHistory = [ ] ;
863909 }
864910
911+ getHistory ( ) {
912+ return this . #undoHistory;
913+ }
914+
865915 applyTemplate ( template : number [ ] [ ] ) {
866916 this . #data = [ template ] ;
867917 this . #setPixelAll( ) ;
0 commit comments