Skip to content

Commit 7d69077

Browse files
committed
v0.2.1
1 parent 8e16d03 commit 7d69077

14 files changed

Lines changed: 397 additions & 100 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-hex-engine",
33
"description": "Hexagon Map Editor and Game Engine",
4-
"version": "0.2.0",
4+
"version": "0.2.1",
55
"main": "lib/index.js",
66
"author": "IcculusC",
77
"repository": "IcculusC/react-hex-engine",
@@ -47,6 +47,7 @@
4747
},
4848
"dependencies": {
4949
"classnames": "^2.2.5",
50+
"flatbush": "^3.1.0",
5051
"memoize-one": "^4.0.2"
5152
},
5253
"files": [

src/HexGrid.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class HexGrid extends Component {
3131
};
3232

3333
render() {
34-
const { classes, height, viewBox, width } = this.props;
34+
const { children, classes, height, viewBox, width } = this.props;
3535
return (
3636
<svg
3737
className={classNames("grid", classes.grid)}
@@ -41,7 +41,7 @@ class HexGrid extends Component {
4141
width={width}
4242
xmlns="http://www.w3.org/2000/svg"
4343
>
44-
<ViewBoxProvider value={viewBox}>{this.props.children}</ViewBoxProvider>
44+
<ViewBoxProvider value={viewBox}>{children}</ViewBoxProvider>
4545
</svg>
4646
);
4747
}
Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React, { Component } from "react";
22
import PropTypes from "prop-types";
33
import classNames from "classnames";
4-
import Hex from "../models/Hex";
5-
import HexUtils from "../HexUtils";
6-
import Point from "../models/Point";
7-
import { withExpandedLayout } from "../Context";
4+
import Hex from "./models/Hex";
5+
import HexUtils from "./HexUtils";
6+
import Point from "./models/Point";
7+
import Text from "./Text";
8+
import { withExpandedLayout } from "./Context";
89

910
class Hexagon extends Component {
1011
static propTypes = {
@@ -26,7 +27,9 @@ class Hexagon extends Component {
2627
r: PropTypes.number.isRequired,
2728
s: PropTypes.number.isRequired,
2829
selected: PropTypes.bool,
29-
showCoordinates: PropTypes.bool
30+
showCoordinates: PropTypes.bool,
31+
text: PropTypes.string,
32+
TextProps: PropTypes.objectOf(PropTypes.any)
3033
};
3134

3235
static defaultProps = {
@@ -40,9 +43,12 @@ class Hexagon extends Component {
4043
q: "",
4144
r: "",
4245
s: "",
43-
selected: ""
46+
selected: "",
47+
text: ""
4448
},
45-
showCoordinates: false
49+
showCoordinates: false,
50+
text: "",
51+
TextProps: {}
4652
};
4753

4854
static getCoordinateTextOffset(
@@ -58,10 +64,22 @@ class Hexagon extends Component {
5864
);
5965
}
6066

67+
static getDerivedStateFromProps(nextProps, prevState) {
68+
if (prevState.points !== nextProps.points) {
69+
const { layout, points, q, r, s } = nextProps;
70+
const hex = new Hex(q, r, s);
71+
const pixel = HexUtils.hexToPixel(hex, nextProps.layout);
72+
return { hex, pixel, layout, points };
73+
}
74+
return null;
75+
}
76+
6177
state = {
6278
hex: {},
6379
hovered: false,
64-
pixel: {}
80+
layout: {},
81+
pixel: {},
82+
points: ""
6583
};
6684

6785
constructor(props) {
@@ -145,9 +163,11 @@ class Hexagon extends Component {
145163
r,
146164
s,
147165
selected,
148-
showCoordinates
166+
showCoordinates,
167+
text,
168+
TextProps
149169
} = this.props;
150-
const { hex, hovered, pixel } = this.state;
170+
const { hovered, pixel } = this.state;
151171
let qPixel, rPixel, sPixel;
152172
if (showCoordinates) {
153173
qPixel = Hexagon.getCoordinateTextOffset(3, layout, {
@@ -186,7 +206,12 @@ class Hexagon extends Component {
186206
>
187207
<polygon className={classes.polygon} points={points} />
188208
{this.props.children}
189-
{showCoordinates && (
209+
{text ? (
210+
<Text className={classes.text} {...TextProps}>
211+
{text}
212+
</Text>
213+
) : null}
214+
{showCoordinates ? (
190215
<React.Fragment>
191216
<text
192217
{...qPixel}
@@ -213,7 +238,7 @@ class Hexagon extends Component {
213238
{s}
214239
</text>
215240
</React.Fragment>
216-
)}
241+
) : null}
217242
</g>
218243
</g>
219244
);

src/Layout.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { Component } from "react";
22
import PropTypes from "prop-types";
33
import classNames from "classnames";
44
import memoize from "memoize-one";
5-
import Hexagon from "./Hexagon/Hexagon";
5+
import Hex from "./models/Hex";
66
import HexUtils from "./HexUtils";
77
import Orientation from "./models/Orientation";
88
import Point from "./models/Point";
@@ -33,48 +33,51 @@ export class Layout extends Component {
3333
return new Point(size.x * Math.cos(angle), size.y * Math.sin(angle));
3434
}
3535

36-
// TODO Refactor
3736
static calculateCoordinates(orientation, size) {
38-
const corners = [];
3937
const center = new Point(0, 0);
40-
41-
Array.from(new Array(6), (x, i) => {
38+
return [...Array(6).keys()].map((_, i) => {
4239
const offset = Layout.getPointOffset(i, orientation, size);
43-
const point = new Point(center.x + offset.x, center.y + offset.y);
44-
return corners.push(point);
40+
return new Point(center.x + offset.x, center.y + offset.y);
4541
});
42+
}
43+
44+
static filterViewBox(layout, viewBox) {
45+
const { height, width, x, y } = viewBox;
46+
const x_ = x - layout.size.x + layout.spacing + 1;
47+
const y_ = y - layout.size.y + layout.spacing + 1;
48+
const width_ = width + layout.size.x + layout.spacing + 1;
49+
const height_ = height + layout.size.y + layout.spacing + 1;
4650

47-
return corners;
51+
return {
52+
x: x_,
53+
y: y_,
54+
width: width_,
55+
height: height_
56+
};
4857
}
4958

5059
filterChildren = memoize((children, layout, viewBox) => {
5160
const childrenArray = React.Children.toArray(children);
52-
const { height, width, x, y } = viewBox;
53-
const cornerCoords = Layout.calculateCoordinates(
54-
layout.orientation,
55-
layout.size
56-
);
61+
const { x, y, width, height } = Layout.filterViewBox(layout, viewBox);
62+
5763
return childrenArray.filter(child => {
5864
if (!child.props) {
5965
return true;
6066
}
6167
if (
62-
child.type === Hexagon ||
63-
(child.props.q !== undefined &&
64-
child.props.r !== undefined &&
65-
child.props.s !== undefined)
68+
child.props !== undefined &&
69+
child.props.q !== undefined &&
70+
child.props.r !== undefined &&
71+
child.props.s !== undefined
6672
) {
67-
const point = HexUtils.hexToPixel(child.props, layout);
68-
const corners = cornerCoords.map(
69-
coord => new Point(coord.x + point.x, coord.y + point.y)
73+
const { q, r, s } = child.props;
74+
const point = HexUtils.hexToPixel(new Hex(q, r, s), layout);
75+
return (
76+
point.x > x &&
77+
point.x < width + x &&
78+
point.y > y &&
79+
point.y < height + y
7080
);
71-
return corners.filter(
72-
corner =>
73-
corner.x > x &&
74-
corner.x < width + x &&
75-
corner.y > y &&
76-
corner.y < +height + y
77-
).length;
7881
}
7982
return true;
8083
});
File renamed without changes.

src/index.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import GridGenerator from "./GridGenerator";
2-
import HexGrid from "./HexGrid";
3-
import HexUtils from "./HexUtils";
4-
import Layout from "./Layout";
5-
import Path from "./Path";
6-
import Pattern from "./Pattern";
7-
8-
import Hexagon from "./Hexagon/Hexagon";
9-
import Text from "./Hexagon/Text";
10-
11-
import Hex from "./models/Hex";
12-
131
export {
14-
GridGenerator,
15-
HexGrid,
16-
HexUtils,
17-
Layout,
18-
Path,
19-
Pattern,
20-
Hexagon,
21-
Text,
22-
Hex
23-
};
2+
LayoutConsumer,
3+
LayoutContext,
4+
LayoutProvider,
5+
ViewBoxConsumer,
6+
ViewBoxContext,
7+
ViewBoxProvider,
8+
withExpandedLayout,
9+
withLayout,
10+
withViewBox
11+
} from "./Context";
12+
export { default as GridGenerator } from "./GridGenerator";
13+
export { default as Hex } from "./models/Hex";
14+
export { default as Hexagon } from "./Hexagon";
15+
export { default as HexGrid } from "./HexGrid";
16+
export { default as HexUtils } from "./HexUtils";
17+
export { default as Layout } from "./Layout";
18+
export { default as Orientation } from "./models/Orientation";
19+
export { default as Path } from "./Path";
20+
export { default as Pattern } from "./Pattern";
21+
export { default as Point } from "./models/Point";
22+
export { default as Text } from "./Text";

src/models/Hex.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ class Hex {
99
const { q, r, s } = this;
1010
return `${q},${r},${s}`;
1111
}
12+
13+
toJSON() {
14+
const { q, r, s } = this;
15+
return { q, r, s };
16+
}
17+
18+
static fromJSON(obj) {
19+
const { q, r, s } = obj;
20+
return new Hex(q, r, s);
21+
}
1222
}
1323

1424
export default Hex;

src/models/Point.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ class Point {
88
const { x, y } = this;
99
return `${x},${y}`;
1010
}
11+
12+
toJSON() {
13+
const { x, y } = this;
14+
return { x, y };
15+
}
16+
17+
static fromJSON(obj) {
18+
const { x, y } = obj;
19+
return new Point(x, y);
20+
}
1121
}
1222

1323
export default Point;
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import React from "react";
22
import renderer from "react-test-renderer";
33
import { mount } from "enzyme";
44

5-
import Layout from "../../../src/Layout";
6-
import Hexagon from "../../../src/Hexagon/Hexagon";
5+
import Layout from "../../src/Layout";
6+
import Hexagon from "../../src/Hexagon";
77

88
test("Hexagon should render correctly with default props", () => {
99
const tree = renderer
@@ -24,6 +24,23 @@ test("Hexagon should render correctly with default props", () => {
2424
expect(tree).toMatchSnapshot();
2525
});
2626

27+
test("Hexagon should render correctly with text", () => {
28+
const tree = renderer
29+
.create(
30+
<Layout
31+
className={"test1"}
32+
size={{ x: 6, y: 6 }}
33+
flat={false}
34+
spacing={1.1}
35+
origin={{ x: 0, y: 0 }}
36+
>
37+
<Hexagon q={0} r={0} s={0} text="test" />
38+
</Layout>
39+
)
40+
.toJSON();
41+
expect(tree).toMatchSnapshot();
42+
});
43+
2744
test("Hexagon should work", () => {
2845
let playDoh;
2946
const wrapper = mount(

test/src/Hexagon/__snapshots__/Hexagon.test.js.snap

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)