@@ -3,12 +3,13 @@ import PropTypes from "prop-types";
33import classNames from "classnames" ;
44import Hex from "../models/Hex" ;
55import HexUtils from "../HexUtils" ;
6+ import Point from "../models/Point" ;
67import { withExpandedLayout } from "../Context" ;
78
89class Hexagon extends Component {
910 static propTypes = {
1011 children : PropTypes . node ,
11- className : PropTypes . string ,
12+ classes : PropTypes . objectOf ( PropTypes . any ) ,
1213 data : PropTypes . object ,
1314 highlighted : PropTypes . bool ,
1415 layout : PropTypes . objectOf ( PropTypes . any ) . isRequired ,
@@ -24,7 +25,8 @@ class Hexagon extends Component {
2425 q : PropTypes . number . isRequired ,
2526 r : PropTypes . number . isRequired ,
2627 s : PropTypes . number . isRequired ,
27- selected : PropTypes . bool
28+ selected : PropTypes . bool ,
29+ showCoordinates : PropTypes . bool
2830 } ;
2931
3032 static defaultProps = {
@@ -35,50 +37,60 @@ class Hexagon extends Component {
3537 highlighted : "" ,
3638 hovered : "" ,
3739 polygon : "" ,
40+ q : "" ,
41+ r : "" ,
42+ s : "" ,
3843 selected : ""
39- }
44+ } ,
45+ showCoordinates : false
4046 } ;
4147
42- static getDerivedStateFromProps ( nextProps , prevState ) {
43- const { layout, q, r, s } = nextProps ;
44- const hex = new Hex ( q , r , s ) ;
45- const pixel = HexUtils . hexToPixel ( hex , layout ) ;
46-
47- if (
48- ( ! prevState . hex && ! prevState . pixel ) ||
49- ! (
50- HexUtils . equals ( prevState . hex , hex ) ||
51- ( prevState . pixel . x === pixel . x && prevState . pixel . y === pixel . y )
52- )
53- ) {
54- return { hex, pixel } ;
55- }
56- return null ;
48+ static getCoordinateTextOffset (
49+ corner ,
50+ { orientation, size } ,
51+ offset = new Point ( 0 , 0 ) ,
52+ scale = new Point ( 0.75 , 0.75 )
53+ ) {
54+ const angle = ( 2.0 * Math . PI * ( corner + 0.5 ) ) / 6 ;
55+ return new Point (
56+ size . x * scale . x * Math . cos ( angle ) ,
57+ size . y * scale . y * Math . sin ( angle )
58+ ) ;
5759 }
5860
5961 state = {
6062 hex : { } ,
61- hovered : false ,
62- pixel : { }
63+ hovered : false
6364 } ;
6465
66+ constructor ( props ) {
67+ super ( props ) ;
68+ const hex = new Hex ( props . q , props . r , props . s ) ;
69+ const pixel = HexUtils . hexToPixel ( hex , props . layout ) ;
70+ this . state = {
71+ ...this . state ,
72+ hex,
73+ pixel
74+ } ;
75+ }
76+
6577 onClick ( e ) {
6678 if ( this . props . onClick ) {
67- this . props . onClick ( e , this ) ;
79+ this . props . onClick ( e , this . state . hex ) ;
6880 }
6981 }
7082
7183 onDragEnd ( e ) {
7284 if ( this . props . onDragEnd ) {
7385 e . preventDefault ( ) ;
7486 const success = e . dataTransfer . dropEffect !== "none" ;
75- this . props . onDragEnd ( e , this , success ) ;
87+ this . props . onDragEnd ( e , this . state . hex , success ) ;
7688 }
7789 }
7890
7991 onDragOver ( e ) {
8092 if ( this . props . onDragOver ) {
81- this . props . onDragOver ( e , this ) ;
93+ this . props . onDragOver ( e , this . state . hex ) ;
8294 }
8395 }
8496
@@ -87,45 +99,69 @@ class Hexagon extends Component {
8799 const targetProps = {
88100 ...this . state ,
89101 data : this . props . data ,
90- fill : this . props . fill ,
91- className : this . props . className
102+ classes : this . props . classes
92103 } ;
93104 e . dataTransfer . setData ( "hexagon" , JSON . stringify ( targetProps ) ) ;
94- this . props . onDragStart ( e , this ) ;
105+ this . props . onDragStart ( e , this . state . hex ) ;
95106 }
96107 }
97108
98109 onDrop ( e ) {
99110 if ( this . props . onDrop ) {
100111 e . preventDefault ( ) ;
101112 const target = JSON . parse ( e . dataTransfer . getData ( "hexagon" ) ) ;
102- this . props . onDrop ( e , this , target ) ;
113+ this . props . onDrop ( e , this . state . hex , target ) ;
103114 }
104115 }
105116
106117 onMouseEnter ( e ) {
107118 this . setState ( { hovered : true } ) ;
108119 if ( this . props . onMouseEnter ) {
109- this . props . onMouseEnter ( e , this ) ;
120+ this . props . onMouseEnter ( e , this . state . hex ) ;
110121 }
111122 }
112123
113124 onMouseLeave ( e ) {
114125 this . setState ( { hovered : false } ) ;
115126 if ( this . props . onMouseLeave ) {
116- this . props . onMouseLeave ( e , this ) ;
127+ this . props . onMouseLeave ( e , this . state . hex ) ;
117128 }
118129 }
119130
120131 onMouseOver ( e ) {
121132 if ( this . props . onMouseOver ) {
122- this . props . onMouseOver ( e , this ) ;
133+ this . props . onMouseOver ( e , this . state . hex ) ;
123134 }
124135 }
125136
126137 render ( ) {
127- const { classes, highlighted, points, selected } = this . props ;
128- const { hovered, pixel } = this . state ;
138+ const {
139+ classes,
140+ highlighted,
141+ layout,
142+ points,
143+ q,
144+ r,
145+ s,
146+ selected,
147+ showCoordinates
148+ } = this . props ;
149+ const { hex, hovered, pixel } = this . state ;
150+ let qPixel , rPixel , sPixel ;
151+ if ( showCoordinates ) {
152+ qPixel = Hexagon . getCoordinateTextOffset ( 3 , layout , {
153+ x : 0 ,
154+ y : 1
155+ } ) ;
156+ rPixel = Hexagon . getCoordinateTextOffset ( 1 , layout , {
157+ x : - 1 ,
158+ y : - 1
159+ } ) ;
160+ sPixel = Hexagon . getCoordinateTextOffset ( 5 , layout , {
161+ x : - 2 ,
162+ y : 1
163+ } ) ;
164+ }
129165 return (
130166 < g
131167 className = { classNames ( "hexagon-group" , classes . group ) }
@@ -149,6 +185,34 @@ class Hexagon extends Component {
149185 >
150186 < polygon className = { classes . polygon } points = { points } />
151187 { this . props . children }
188+ { showCoordinates && (
189+ < React . Fragment >
190+ < text
191+ { ...qPixel }
192+ className = { classes . q }
193+ fontSize = { 2 }
194+ textAnchor = "middle"
195+ >
196+ { q }
197+ </ text >
198+ < text
199+ { ...rPixel }
200+ className = { classes . r }
201+ fontSize = { 2 }
202+ textAnchor = "middle"
203+ >
204+ { r }
205+ </ text >
206+ < text
207+ { ...sPixel }
208+ className = { classes . s }
209+ fontSize = { 2 }
210+ textAnchor = "middle"
211+ >
212+ { s }
213+ </ text >
214+ </ React . Fragment >
215+ ) }
152216 </ g >
153217 </ g >
154218 ) ;
0 commit comments