|
| 1 | +import React, { Component } from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import classNames from "classnames"; |
| 4 | +import { HexEngineProvider } from "./Context"; |
| 5 | +import Point from "./models/Point"; |
| 6 | +import Orientation from "./models/Orientation"; |
| 7 | + |
| 8 | +class HexEngine extends Component { |
| 9 | + static propTypes = { |
| 10 | + children: PropTypes.node.isRequired, |
| 11 | + classes: PropTypes.objectOf(PropTypes.string), |
| 12 | + flat: PropTypes.bool, |
| 13 | + height: PropTypes.oneOfType([ |
| 14 | + PropTypes.string.isRequired, |
| 15 | + PropTypes.number.isRequired |
| 16 | + ]), |
| 17 | + origin: PropTypes.oneOfType([ |
| 18 | + PropTypes.instanceOf(Point), |
| 19 | + PropTypes.shape({ x: PropTypes.number, y: PropTypes.number }) |
| 20 | + ]), |
| 21 | + size: PropTypes.oneOfType([ |
| 22 | + PropTypes.instanceOf(Point), |
| 23 | + PropTypes.shape({ x: PropTypes.number, y: PropTypes.number }) |
| 24 | + ]), |
| 25 | + spacing: PropTypes.number, |
| 26 | + width: PropTypes.oneOfType([ |
| 27 | + PropTypes.string.isRequired, |
| 28 | + PropTypes.number.isRequired |
| 29 | + ]), |
| 30 | + viewBox: PropTypes.objectOf(PropTypes.number) |
| 31 | + }; |
| 32 | + |
| 33 | + static defaultProps = { |
| 34 | + classes: { grid: "", layout: "" }, |
| 35 | + flat: true, |
| 36 | + height: 480, |
| 37 | + origin: new Point(0, 0), |
| 38 | + size: new Point(10, 10), |
| 39 | + spacing: 1.0, |
| 40 | + width: 640, |
| 41 | + viewBox: { |
| 42 | + height: 100, |
| 43 | + width: 100, |
| 44 | + x: -50, |
| 45 | + y: -50 |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + static getPointOffset(corner, orientation, size) { |
| 50 | + let angle = (2.0 * Math.PI * (corner + orientation.startAngle)) / 6; |
| 51 | + return new Point(size.x * Math.cos(angle), size.y * Math.sin(angle)); |
| 52 | + } |
| 53 | + |
| 54 | + static calculateCoordinates(orientation, size) { |
| 55 | + const center = new Point(0, 0); |
| 56 | + return [...Array(6).keys()].map((_, i) => { |
| 57 | + const offset = HexEngine.getPointOffset(i, orientation, size); |
| 58 | + return new Point(center.x + offset.x, center.y + offset.y); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + render() { |
| 63 | + const { |
| 64 | + children, |
| 65 | + classes, |
| 66 | + flat, |
| 67 | + height, |
| 68 | + size, |
| 69 | + viewBox, |
| 70 | + width, |
| 71 | + ...rest |
| 72 | + } = this.props; |
| 73 | + |
| 74 | + const orientation = flat ? Orientation.Flat : Orientation.Pointy; |
| 75 | + const points = HexEngine.calculateCoordinates(orientation, size) |
| 76 | + .map(point => point.toString()) |
| 77 | + .join(" "); |
| 78 | + const layout = { orientation, size, ...rest }; |
| 79 | + |
| 80 | + return ( |
| 81 | + <svg |
| 82 | + className={classNames("grid", classes.grid)} |
| 83 | + height={height} |
| 84 | + version="1.1" |
| 85 | + viewBox={`${viewBox.x} ${viewBox.y} ${viewBox.width} ${viewBox.height}`} |
| 86 | + width={width} |
| 87 | + xmlns="http://www.w3.org/2000/svg" |
| 88 | + > |
| 89 | + <HexEngineProvider |
| 90 | + value={{ |
| 91 | + layout, |
| 92 | + points, |
| 93 | + viewBox |
| 94 | + }} |
| 95 | + > |
| 96 | + <g className={classes.layout}>{children}</g> |
| 97 | + </HexEngineProvider> |
| 98 | + </svg> |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +export default HexEngine; |
0 commit comments