11import React , { Component } from 'react' ;
22import chunk from 'lodash/chunk' ;
3- import { select , event , mouse } from 'd3-selection' ;
3+ import { select , mouse } from 'd3-selection' ;
44import { drag } from 'd3-drag' ;
55import { mappingState } from './state' ;
66import { environment } from '#store/environment' ;
77import { observer } from 'mobx-react' ;
88import { Mapping } from './mapping' ;
99import { Spring } from 'react-spring/renderprops' ;
10+ import { Checkbox } from '#ui' ;
1011
1112const baseConfig = {
1213 hflip : false ,
@@ -18,90 +19,109 @@ const baseConfig = {
1819} ;
1920
2021
22+ const AutoDismiss = observer ( ( ) => (
23+ < div className = "autodismiss" >
24+ < span onClick = { mappingState . toggleAutodismiss } > autodismiss</ span >
25+ < Checkbox
26+ checked = { mappingState . autodismiss }
27+ onChange = { mappingState . toggleAutodismiss }
28+ />
29+ </ div >
30+ ) ) ;
31+
2132@observer
2233export class NewMapping extends Component {
23-
2434 onRef = ( node ) => {
2535 this . node = node ;
26- }
36+ } ;
2737
2838 pos = [ void 0 , void 0 ] ;
2939 mapDefz = [ ] ;
3040
3141 dragPlacementFactory = ( index ) => {
3242 return ( node ) => {
3343 if ( node ) {
34- select ( node ) . call ( drag ( ) . filter ( ( ) => true )
44+ select ( node ) . call (
45+ drag ( )
46+ . filter ( ( ) => true )
3547 . on ( 'start' , ( ) => {
3648 const { newMapping, scale } = mappingState ;
37- const [ x , y ] = this . pos = mouse ( this . node ) ;
49+ const [ x , y ] = ( this . pos = mouse ( this . node ) ) ;
3850 const [ pieceX , pieceY ] = mouse ( node ) ;
3951
4052 newMapping . piece = Object . assign (
4153 { } ,
4254 this . mapDefz [ index ] ,
4355 {
44- top : ( y / scale ) - ( pieceY / 4 ) | 0 ,
45- left : ( x / scale ) - ( pieceX / 4 ) | 0 ,
56+ top : ( y / scale - pieceY / 4 ) | 0 ,
57+ left : ( x / scale - pieceX / 4 ) | 0 ,
4658 } ,
4759 ) ;
48-
4960 } )
5061 . on ( 'drag' , ( ) => {
51- const { newMapping : { piece } , scale } = mappingState ;
62+ const {
63+ newMapping : { piece } ,
64+ scale,
65+ } = mappingState ;
5266 const [ x , y ] = mouse ( this . node ) ;
53- const [ dx , dy ] = [ x - this . pos [ 0 ] , y - this . pos [ 1 ] ] ;
67+ const [ dx , dy ] = [ x - this . pos [ 0 ] , y - this . pos [ 1 ] ] ;
5468 this . pos = [ x , y ] ;
5569
56- Object . assign (
57- piece ,
58- {
59- top : piece . top + ( dy / scale ) ,
60- left : piece . left + ( dx / scale ) ,
61- } ,
62- ) ;
70+ Object . assign ( piece , {
71+ top : piece . top + dy / scale ,
72+ left : piece . left + dx / scale ,
73+ } ) ;
6374 } )
6475 . on ( 'end' , ( ) => {
6576 mappingState . placeNewMapping ( ) ;
66- } )
77+ } ) ,
6778 ) ;
68-
6979 }
7080 } ;
7181 } ;
7282
7383 getLeft = ( ) => {
74- const { newMapping : { active, piece } } = mappingState ;
75- if ( active && piece || ! active ) return - 325 ;
84+ const {
85+ newMapping : { active, piece } ,
86+ } = mappingState ;
87+ if ( ( active && piece ) || ! active ) return - 325 ;
7688 else if ( active ) return 15 ;
7789 } ;
7890
7991 getOpacity = ( ) => {
80- const { newMapping : { active } } = mappingState ;
92+ const {
93+ newMapping : { active } ,
94+ } = mappingState ;
8195 return active ? 1 : 0 ;
8296 } ;
8397
8498 render ( ) {
85- const { tiles, config : { currentTile } } = environment ;
86- const { scale, newMapping : { active, piece } } = mappingState ;
87-
88- this . mapDefz = Array . from ( { length : 0x10 } , ( _ , i ) => ( {
99+ const {
100+ tiles,
101+ config : { currentTile } ,
102+ } = environment ;
103+ const {
104+ scale,
105+ newMapping : { active, piece } ,
106+ } = mappingState ;
107+
108+ this . mapDefz = Array . from ( { length : 0x10 } , ( _ , i ) => ( {
89109 art : currentTile ,
90- width : ( i % 4 ) + 1 ,
91- height : 0 | ( i / 4 ) + 1 ,
110+ width : ( i % 4 ) + 1 ,
111+ height : 0 | ( i / 4 + 1 ) ,
92112 ...baseConfig ,
93113 } ) ) ;
94114
95115 return (
96116 < div ref = { this . onRef } >
97117 { piece && (
98- < div className = "new-floating-piece" >
99- < Mapping
100- data = { piece }
101- scale = { mappingState . scale }
102- tileBuffer = { tiles }
118+ < div className = "new-floating-piece" >
119+ < Mapping
120+ data = { piece }
121+ scale = { mappingState . scale }
122+ tileBuffer = { tiles }
103123 />
104- </ div >
124+ </ div >
105125 ) }
106126 < Spring
107127 from = { {
@@ -113,34 +133,34 @@ export class NewMapping extends Component {
113133 opacity : this . getOpacity ( ) ,
114134 } }
115135 >
116- { ( { left, opacity} ) => (
117- < div
118- className = "new-mapping"
119- style = { { left, opacity } }
120- >
121- { opacity > .01 &&
122- chunk ( this . mapDefz , 4 ) . map ( ( group , gIndex ) => (
123- < div key = { gIndex } className = "group" >
124- { group . map ( ( def , lineIndex ) => {
125- const index = ( gIndex * 4 ) + lineIndex ;
126- const dragPlacement = this . dragPlacementFactory ( index ) ;
127- return < Mapping
128- key = { index }
129- wrapRef = { dragPlacement }
130- data = { def }
131- scale = { 4 }
132- tileBuffer = { tiles }
133- /> ;
134- } ) }
135- </ div >
136- )
137- ) }
138- TODO: autodismiss
136+ { ( { left, opacity } ) => (
137+ < div className = "new-mapping" style = { { left, opacity } } >
138+ { opacity > 0.01 &&
139+ chunk ( this . mapDefz , 4 ) . map ( ( group , gIndex ) => (
140+ < div key = { gIndex } className = "group" >
141+ { group . map ( ( def , lineIndex ) => {
142+ const index =
143+ gIndex * 4 + lineIndex ;
144+ const dragPlacement = this . dragPlacementFactory (
145+ index ,
146+ ) ;
147+ return (
148+ < Mapping
149+ key = { index }
150+ wrapRef = { dragPlacement }
151+ data = { def }
152+ scale = { 4 }
153+ tileBuffer = { tiles }
154+ />
155+ ) ;
156+ } ) }
157+ </ div >
158+ ) ) }
159+ < AutoDismiss />
139160 </ div >
140161 ) }
141162 </ Spring >
142163 </ div >
143164 ) ;
144165 }
145-
146166}
0 commit comments