@@ -59,12 +59,13 @@ const GLOBAL_STATE = {
59
59
holdingKeyZ : false ,
60
60
holdingMeta : false ,
61
61
} ,
62
- selectedElements : [ ] ,
63
62
canvasColor : "#c4b9a5" ,
64
63
65
- // brushing metadata/status
64
+ // active actions
65
+ freeDragging : false ,
66
66
brushingActive : false ,
67
67
usingSecondary : false ,
68
+ selectedElements : [ ] ,
68
69
69
70
layers : {
70
71
BASE : {
@@ -175,7 +176,7 @@ document.addEventListener("keyup", e => {
175
176
176
177
// when window loses focus, reset - otherwise, Cmd+Tab to change windows
177
178
// will continue having holdingMeta after coming back
178
- document . addEventListener ( "blur" , ( ) => {
179
+ document . addEventListener ( "blur" , e => {
179
180
GLOBAL_STATE . keyState . holdingKeyZ = false ;
180
181
GLOBAL_STATE . keyState . holdingMeta = false ;
181
182
} ) ;
@@ -262,11 +263,50 @@ document.getElementById("saveBtn").addEventListener("click", (e) => {
262
263
} ) ;
263
264
264
265
// SVG events listeners
266
+ // mousedown is the big one that coordinates most of the page
267
+ SVG . addEventListener ( "mousedown" , ( e ) => {
268
+ e . preventDefault ( ) ;
269
+ console . log ( e ) ;
270
+ if ( GLOBAL_STATE . keyState . holdingKeyZ ) {
271
+ const zoomFactor = .5 * ( e . button == 2 ? 1 : - 1 ) ;
272
+ zoom ( zoomFactor , e . clientX , e . clientY ) ;
273
+ return ;
274
+ } else if ( e . buttons < 3 ) { // single left/right click
275
+ GLOBAL_STATE . brushingActive = true ;
276
+ // a right mouse down means paint with secondary colors
277
+ GLOBAL_STATE . usingSecondary = ( e . button == 2 ) ;
278
+ const hexAtMouse = document . elementsFromPoint ( e . clientX , e . clientY ) . find ( el => el . classList . contains ( "hex" ) ) ;
279
+ if ( hexAtMouse ) {
280
+ handleHexInteraction ( hexAtMouse . getAttribute ( "c" ) , hexAtMouse . getAttribute ( "r" ) , e . clientX , e . clientY , true ) ;
281
+ }
282
+ } else if ( e . buttons == 4 ) {
283
+ GLOBAL_STATE . freeDragging = true ;
284
+ SVG . addEventListener ( "mousemove" , freeDragScroll ) ;
285
+ switchToCursor ( "move" ) ;
286
+ }
287
+ } ) ;
288
+ SVG . addEventListener ( "mouseover" , ( e ) => {
289
+ console . log ( e ) ;
290
+ if ( GLOBAL_STATE . brushingActive ) {
291
+ const hexAtMouse = document . elementsFromPoint ( e . clientX , e . clientY ) . find ( el => el . classList . contains ( "hex" ) ) ;
292
+ console . log ( hexAtMouse ) ;
293
+ if ( hexAtMouse ) {
294
+ handleHexInteraction ( hexAtMouse . getAttribute ( "c" ) , hexAtMouse . getAttribute ( "r" ) , e . x , e . y , false ) ;
295
+ }
296
+ }
297
+ } ) ;
265
298
SVG . addEventListener ( "mouseup" , ( ) => {
299
+ if ( GLOBAL_STATE . layers . BOUNDARY . lastBoundaryPoint ) {
300
+ SVG . removeEventListener ( "mousemove" , drawBoundary ) ;
301
+ }
302
+ if ( GLOBAL_STATE . freeDragging ) {
303
+ SVG . removeEventListener ( "mousemove" , freeDragScroll ) ;
304
+ switchToCursor ( GLOBAL_STATE . currentTool ) ;
305
+ }
266
306
GLOBAL_STATE . brushingActive = false ;
267
307
GLOBAL_STATE . layers . PATH . activePath = null ;
308
+ GLOBAL_STATE . freeDragging = false ;
268
309
GLOBAL_STATE . layers . BOUNDARY . lastBoundaryPoint = null ;
269
- SVG . removeEventListener ( "mousemove" , drawBoundary ) ;
270
310
} ) ;
271
311
272
312
SVG . addEventListener ( "contextmenu" , e => e . preventDefault ( ) ) ;
@@ -281,6 +321,7 @@ SVG.addEventListener("wheel", e => {
281
321
}
282
322
} ) ;
283
323
324
+
284
325
/*********************************
285
326
* INTERACTING WITH GLOBAL STATE *
286
327
*********************************/
@@ -345,12 +386,17 @@ function switchToLayer(layer) {
345
386
setSecondaryColor ( GLOBAL_STATE . layers [ GLOBAL_STATE . currentLayer ] . secondaryColor ) ;
346
387
}
347
388
389
+ function switchToCursor ( name ) {
390
+ Object . keys ( Tools ) . forEach ( t => SVG . classList . remove ( `${ t . toLowerCase ( ) } cursor` ) ) ;
391
+ SVG . classList . remove ( "movecursor" ) ;
392
+ SVG . classList . add ( `${ name . toLowerCase ( ) } cursor` ) ;
393
+ }
394
+
348
395
function switchToTool ( tool ) {
349
396
if ( ! LAYER_TOOL_COMPATIBILITY [ GLOBAL_STATE . currentLayer ] . includes ( tool ) ) {
350
397
return ;
351
398
}
352
- Object . keys ( Tools ) . forEach ( t => SVG . classList . remove ( `${ t . toLowerCase ( ) } cursor` ) ) ;
353
- SVG . classList . add ( `${ tool . toLowerCase ( ) } cursor` ) ;
399
+ switchToCursor ( tool ) ;
354
400
TOOL_PICKER_BUTTONS . forEach ( b => {
355
401
if ( b . dataset . tool == tool ) {
356
402
b . classList . add ( "selected" )
@@ -375,6 +421,10 @@ function switchToTool(tool) {
375
421
/*************
376
422
* SVG UTILS *
377
423
*************/
424
+ function freeDragScroll ( e ) {
425
+ scroll ( e . movementX , e . movementY ) ;
426
+ }
427
+
378
428
function scroll ( xdiff , ydiff ) {
379
429
const viewBox = SVG . getAttribute ( "viewBox" ) || `0 0 ${ window . innerWidth } ${ window . innerHeight } ` ;
380
430
const [ x , y , width , height ] = viewBox . split ( " " ) . map ( Number ) ;
@@ -409,7 +459,9 @@ function hexIndexToPixel(c, r) {
409
459
return { x, y} ;
410
460
}
411
461
412
- function getHexNeighbors ( c , r ) {
462
+ function getHexNeighbors ( _c , _r ) {
463
+ const c = parseInt ( _c ) ;
464
+ const r = parseInt ( _r ) ;
413
465
if ( GLOBAL_STATE . useVerticalAxes ) {
414
466
const offset = ( r % 2 == 0 ) ? - 1 : 1 ;
415
467
return [
@@ -463,6 +515,7 @@ function floodFill(startC, startR, fn) {
463
515
while ( queue . length > 0 ) {
464
516
const [ c , r ] = queue . shift ( ) ;
465
517
getHexNeighbors ( c , r ) . forEach ( n => {
518
+ console . log ( n ) ;
466
519
const stringedCoords = `${ n [ 0 ] } ,${ n [ 1 ] } ` ;
467
520
if ( visited . includes ( stringedCoords ) ) return ;
468
521
const nhexentry = GLOBAL_STATE . hexes [ stringedCoords ] ;
@@ -477,6 +530,8 @@ function floodFill(startC, startR, fn) {
477
530
}
478
531
479
532
function eraseHex ( c , r ) {
533
+ hexObject . setAttribute ( "c" , c ) ;
534
+ hexObject . setAttribute ( "r" , r ) ;
480
535
GLOBAL_STATE . layers . OBJECT . objectsOnHexes [ `${ c } ,${ r } ` ] . textContent = "" ;
481
536
GLOBAL_STATE . hexes [ `${ c } ,${ r } ` ] . hex . setAttribute ( "fill" , GLOBAL_STATE . canvasColor ) ;
482
537
}
@@ -658,6 +713,7 @@ function placeTextAtPoint(pt) {
658
713
}
659
714
660
715
function handleHexInteraction ( c , r , mouseX , mouseY , isClick ) {
716
+ console . log ( c , r , mouseX , mouseY , isClick ) ;
661
717
const hexEntry = GLOBAL_STATE . hexes [ `${ c } ,${ r } ` ] ;
662
718
const { hex, x, y} = hexEntry ;
663
719
if ( GLOBAL_STATE . currentLayer == Layers . BASE ) {
@@ -730,27 +786,10 @@ function drawHex(c, r) {
730
786
hex . setAttribute ( "fill" , GLOBAL_STATE . canvasColor ) ;
731
787
hex . setAttribute ( "stroke" , "black" ) ;
732
788
hex . setAttribute ( "stroke-width" , "5px" ) ;
789
+ hex . setAttribute ( "c" , c ) ;
790
+ hex . setAttribute ( "r" , r ) ;
733
791
hex . classList . add ( "hex" ) ;
734
792
735
- hex . addEventListener ( "mousedown" , ( e ) => {
736
- e . preventDefault ( ) ;
737
-
738
- if ( GLOBAL_STATE . keyState . holdingKeyZ ) {
739
- const zoomFactor = .5 * ( e . button == 2 ? 1 : - 1 ) ;
740
- zoom ( zoomFactor , e . clientX , e . clientY ) ;
741
- return ;
742
- }
743
- GLOBAL_STATE . brushingActive = true ;
744
- // a right mouse down means paint with secondary colors
745
- GLOBAL_STATE . usingSecondary = ( e . button == 2 ) ;
746
- handleHexInteraction ( c , r , e . x , e . y , true ) ;
747
- } ) ;
748
- hex . addEventListener ( "mouseover" , ( e ) => {
749
- if ( GLOBAL_STATE . brushingActive ) {
750
- handleHexInteraction ( c , r , e . x , e . y , false ) ;
751
- }
752
- } ) ;
753
-
754
793
const hexObject = document . createElementNS ( "http://www.w3.org/2000/svg" , "text" ) ;
755
794
hexObject . setAttribute ( "x" , x ) ;
756
795
hexObject . setAttribute ( "y" , y ) ;
0 commit comments