-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from IcculusC/hex-engine
v1.0.0
- Loading branch information
Showing
19 changed files
with
342 additions
and
834 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,20 @@ | ||
import React from "react"; | ||
import Orientation from "./models/Orientation"; | ||
|
||
export const LayoutContext = React.createContext({ | ||
layout: Orientation.Flat, | ||
points: "" | ||
export const HexEngineContext = React.createContext({ | ||
layout: null, | ||
points: null, | ||
viewBox: null | ||
}); | ||
export const { | ||
Provider: LayoutProvider, | ||
Consumer: LayoutConsumer | ||
} = LayoutContext; | ||
|
||
export function withLayout(Component) { | ||
return props => ( | ||
<LayoutConsumer> | ||
{layout => <Component layout={layout} {...props} />} | ||
</LayoutConsumer> | ||
); | ||
} | ||
|
||
export function withExpandedLayout(Component) { | ||
return props => ( | ||
<LayoutConsumer> | ||
{({ layout, points }) => ( | ||
<Component layout={layout} points={points} {...props} /> | ||
)} | ||
</LayoutConsumer> | ||
); | ||
} | ||
|
||
export const ViewBoxContext = React.createContext({ | ||
x: -50, | ||
y: -50, | ||
width: 100, | ||
height: 100 | ||
}); | ||
export const { | ||
Provider: ViewBoxProvider, | ||
Consumer: ViewBoxConsumer | ||
} = ViewBoxContext; | ||
Consumer: HexEngineConsumer, | ||
Provider: HexEngineProvider | ||
} = HexEngineContext; | ||
|
||
export function withViewBox(Component) { | ||
export function withHexEngine(Component) { | ||
return props => ( | ||
<ViewBoxConsumer> | ||
{viewBox => <Component viewBox={viewBox} {...props} />} | ||
</ViewBoxConsumer> | ||
<HexEngineConsumer> | ||
{engine => <Component {...engine} {...props} />} | ||
</HexEngineConsumer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import React, { Component } from "react"; | ||
import PropTypes from "prop-types"; | ||
import classNames from "classnames"; | ||
import { HexEngineProvider } from "./Context"; | ||
import Point from "./models/Point"; | ||
import Orientation from "./models/Orientation"; | ||
|
||
class HexEngine extends Component { | ||
static propTypes = { | ||
children: PropTypes.node.isRequired, | ||
classes: PropTypes.objectOf(PropTypes.string), | ||
flat: PropTypes.bool, | ||
height: PropTypes.oneOfType([ | ||
PropTypes.string.isRequired, | ||
PropTypes.number.isRequired | ||
]), | ||
origin: PropTypes.oneOfType([ | ||
PropTypes.instanceOf(Point), | ||
PropTypes.shape({ x: PropTypes.number, y: PropTypes.number }) | ||
]), | ||
size: PropTypes.oneOfType([ | ||
PropTypes.instanceOf(Point), | ||
PropTypes.shape({ x: PropTypes.number, y: PropTypes.number }) | ||
]), | ||
spacing: PropTypes.number, | ||
width: PropTypes.oneOfType([ | ||
PropTypes.string.isRequired, | ||
PropTypes.number.isRequired | ||
]), | ||
viewBox: PropTypes.objectOf(PropTypes.number) | ||
}; | ||
|
||
static defaultProps = { | ||
classes: { grid: "", layout: "" }, | ||
flat: true, | ||
height: 480, | ||
origin: new Point(0, 0), | ||
size: new Point(10, 10), | ||
spacing: 1.0, | ||
width: 640, | ||
viewBox: { | ||
height: 100, | ||
width: 100, | ||
x: -50, | ||
y: -50 | ||
} | ||
}; | ||
|
||
static getPointOffset(corner, orientation, size) { | ||
let angle = (2.0 * Math.PI * (corner + orientation.startAngle)) / 6; | ||
return new Point(size.x * Math.cos(angle), size.y * Math.sin(angle)); | ||
} | ||
|
||
static calculateCoordinates(orientation, size) { | ||
const center = new Point(0, 0); | ||
return [...Array(6).keys()].map((_, i) => { | ||
const offset = HexEngine.getPointOffset(i, orientation, size); | ||
return new Point(center.x + offset.x, center.y + offset.y); | ||
}); | ||
} | ||
|
||
render() { | ||
const { | ||
children, | ||
classes, | ||
flat, | ||
height, | ||
size, | ||
viewBox, | ||
width, | ||
...rest | ||
} = this.props; | ||
|
||
const orientation = flat ? Orientation.Flat : Orientation.Pointy; | ||
const points = HexEngine.calculateCoordinates(orientation, size) | ||
.map(point => point.toString()) | ||
.join(" "); | ||
const layout = { orientation, size, ...rest }; | ||
|
||
return ( | ||
<svg | ||
className={classNames("grid", classes.grid)} | ||
height={height} | ||
version="1.1" | ||
viewBox={`${viewBox.x} ${viewBox.y} ${viewBox.width} ${viewBox.height}`} | ||
width={width} | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<HexEngineProvider | ||
value={{ | ||
layout, | ||
points, | ||
viewBox | ||
}} | ||
> | ||
<g className={classes.layout}>{children}</g> | ||
</HexEngineProvider> | ||
</svg> | ||
); | ||
} | ||
} | ||
|
||
export default HexEngine; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.