2021-02-17 01:47:12 +00:00
|
|
|
import React, { useContext, useEffect, useRef, useState } from "react";
|
2021-02-15 13:34:22 +00:00
|
|
|
import { SvgVocabulary } from "../svg-vocabulary";
|
2021-02-15 14:56:02 +00:00
|
|
|
import {
|
|
|
|
createSvgSymbolContext,
|
|
|
|
SvgSymbolContent,
|
2021-02-15 22:19:07 +00:00
|
|
|
SvgSymbolContext,
|
2021-02-15 14:56:02 +00:00
|
|
|
SvgSymbolData,
|
|
|
|
} from "../svg-symbol";
|
2021-02-15 21:40:47 +00:00
|
|
|
import { AttachmentPointType, PointWithNormal } from "../specs";
|
2021-02-16 16:52:52 +00:00
|
|
|
import { getAttachmentTransforms } from "../attach";
|
|
|
|
import { scalePointXY } from "../point";
|
2021-02-16 21:43:47 +00:00
|
|
|
import { Point } from "../../vendor/bezier-js";
|
2021-02-16 22:42:19 +00:00
|
|
|
import { Random } from "../random";
|
2021-02-17 13:07:04 +00:00
|
|
|
import { SymbolContextWidget } from "../symbol-context-widget";
|
2021-02-15 14:56:02 +00:00
|
|
|
|
|
|
|
const SYMBOL_MAP = new Map(
|
|
|
|
SvgVocabulary.map((symbol) => [symbol.name, symbol])
|
|
|
|
);
|
|
|
|
|
|
|
|
function getSymbol(name: string): SvgSymbolData {
|
|
|
|
const symbol = SYMBOL_MAP.get(name);
|
|
|
|
if (!symbol) {
|
|
|
|
throw new Error(`Unable to find the symbol "${name}"!`);
|
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
2021-02-15 13:34:22 +00:00
|
|
|
|
2021-02-15 21:40:47 +00:00
|
|
|
function getAttachmentPoint(
|
|
|
|
s: SvgSymbolData,
|
|
|
|
type: AttachmentPointType,
|
|
|
|
idx: number = 0
|
|
|
|
): PointWithNormal {
|
|
|
|
const { specs } = s;
|
|
|
|
if (!specs) {
|
|
|
|
throw new Error(`Symbol ${s.name} has no specs!`);
|
|
|
|
}
|
|
|
|
const points = specs[type];
|
|
|
|
if (!(points && points.length > idx)) {
|
|
|
|
throw new Error(
|
|
|
|
`Symbol ${s.name} must have at least ${
|
|
|
|
idx + 1
|
|
|
|
} ${type} attachment point(s)!`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return points[idx];
|
|
|
|
}
|
|
|
|
|
2021-02-15 22:19:07 +00:00
|
|
|
type AttachmentChildren = JSX.Element | JSX.Element[];
|
|
|
|
|
|
|
|
type CreatureContextType = SvgSymbolContext & {
|
|
|
|
attachmentScale: number;
|
|
|
|
parent: SvgSymbolData | null;
|
|
|
|
};
|
|
|
|
|
2021-02-15 22:22:59 +00:00
|
|
|
const DEFAULT_ATTACHMENT_SCALE = 0.5;
|
|
|
|
|
2021-02-15 22:19:07 +00:00
|
|
|
const CreatureContext = React.createContext<CreatureContextType>({
|
|
|
|
...createSvgSymbolContext(),
|
2021-02-15 22:22:59 +00:00
|
|
|
attachmentScale: DEFAULT_ATTACHMENT_SCALE,
|
2021-02-15 22:19:07 +00:00
|
|
|
parent: null,
|
|
|
|
});
|
|
|
|
|
2021-02-16 22:11:41 +00:00
|
|
|
type AttachmentIndices = {
|
|
|
|
left?: boolean;
|
|
|
|
right?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type CreatureSymbolProps = AttachmentIndices & {
|
2021-02-15 22:19:07 +00:00
|
|
|
data: SvgSymbolData;
|
|
|
|
children?: AttachmentChildren;
|
|
|
|
attachTo?: AttachmentPointType;
|
|
|
|
};
|
|
|
|
|
2021-02-16 22:11:41 +00:00
|
|
|
function getAttachmentIndices(ai: AttachmentIndices): number[] {
|
|
|
|
const result: number[] = [];
|
|
|
|
|
|
|
|
if (ai.left) {
|
|
|
|
result.push(0);
|
|
|
|
}
|
|
|
|
if (ai.right) {
|
|
|
|
result.push(1);
|
|
|
|
}
|
|
|
|
if (result.length === 0) {
|
|
|
|
result.push(0);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-02-15 22:19:07 +00:00
|
|
|
const CreatureSymbol: React.FC<CreatureSymbolProps> = (props) => {
|
|
|
|
const ctx = useContext(CreatureContext);
|
2021-02-16 22:11:41 +00:00
|
|
|
const { data, attachTo } = props;
|
2021-02-15 22:19:07 +00:00
|
|
|
const ourSymbol = (
|
|
|
|
<>
|
|
|
|
{props.children && (
|
2021-02-16 18:36:14 +00:00
|
|
|
<CreatureContext.Provider
|
|
|
|
value={{
|
|
|
|
...ctx,
|
|
|
|
parent: data,
|
|
|
|
}}
|
|
|
|
>
|
2021-02-15 22:19:07 +00:00
|
|
|
{props.children}
|
|
|
|
</CreatureContext.Provider>
|
|
|
|
)}
|
2021-02-16 00:28:03 +00:00
|
|
|
<SvgSymbolContent data={data} {...ctx} />
|
2021-02-15 22:19:07 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!attachTo) {
|
|
|
|
return ourSymbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parent = ctx.parent;
|
|
|
|
if (!parent) {
|
|
|
|
throw new Error(
|
|
|
|
`Cannot attach ${props.data.name} because it has no parent!`
|
|
|
|
);
|
|
|
|
}
|
2021-02-16 16:52:52 +00:00
|
|
|
|
2021-02-16 22:11:41 +00:00
|
|
|
const attachmentIndices = getAttachmentIndices(props);
|
|
|
|
const children: JSX.Element[] = [];
|
|
|
|
|
|
|
|
for (let attachIndex of attachmentIndices) {
|
|
|
|
const parentAp = getAttachmentPoint(parent, attachTo, attachIndex);
|
|
|
|
const ourAp = getAttachmentPoint(data, "tail");
|
|
|
|
|
|
|
|
// If we're being attached as a tail, we want to actually rotate
|
|
|
|
// the attachment an extra 180 degrees, as the tail attachment
|
|
|
|
// point is facing the opposite direction that we actually
|
|
|
|
// want to orient the tail in.
|
|
|
|
const extraRot = attachTo === "tail" ? 180 : 0;
|
|
|
|
|
|
|
|
// If we're attaching something oriented towards the left, horizontally flip
|
|
|
|
// the attachment image.
|
|
|
|
let xFlip = parentAp.normal.x < 0 ? -1 : 1;
|
|
|
|
|
|
|
|
// Er, things look weird if we don't inverse the flip logic for
|
|
|
|
// the downward-facing attachments, like legs...
|
|
|
|
if (parentAp.normal.y > 0) {
|
|
|
|
xFlip *= -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const t = getAttachmentTransforms(parentAp, {
|
|
|
|
point: ourAp.point,
|
|
|
|
normal: scalePointXY(ourAp.normal, xFlip, 1),
|
|
|
|
});
|
|
|
|
|
|
|
|
children.push(
|
|
|
|
<AttachmentTransform
|
|
|
|
key={attachIndex}
|
|
|
|
transformOrigin={ourAp.point}
|
|
|
|
translate={t.translation}
|
|
|
|
scale={{ x: ctx.attachmentScale * xFlip, y: ctx.attachmentScale }}
|
|
|
|
rotate={xFlip * t.rotation + extraRot}
|
|
|
|
>
|
|
|
|
{ourSymbol}
|
|
|
|
</AttachmentTransform>
|
|
|
|
);
|
2021-02-16 00:28:03 +00:00
|
|
|
}
|
2021-02-15 22:19:07 +00:00
|
|
|
|
2021-02-16 22:11:41 +00:00
|
|
|
return <>{children}</>;
|
2021-02-15 22:19:07 +00:00
|
|
|
};
|
|
|
|
|
2021-02-16 21:43:47 +00:00
|
|
|
type AttachmentTransformProps = {
|
|
|
|
transformOrigin: Point;
|
|
|
|
translate: Point;
|
|
|
|
scale: Point;
|
|
|
|
rotate: number;
|
|
|
|
children: JSX.Element;
|
|
|
|
};
|
|
|
|
|
|
|
|
const AttachmentTransform: React.FC<AttachmentTransformProps> = (props) => (
|
|
|
|
<g transform={`translate(${props.translate.x} ${props.translate.y})`}>
|
|
|
|
<g
|
|
|
|
transform-origin={`${props.transformOrigin.x} ${props.transformOrigin.y}`}
|
|
|
|
transform={`scale(${props.scale.x} ${props.scale.y}) rotate(${props.rotate})`}
|
|
|
|
>
|
|
|
|
{props.children}
|
|
|
|
</g>
|
|
|
|
</g>
|
|
|
|
);
|
|
|
|
|
2021-02-16 22:42:19 +00:00
|
|
|
type CreatureSymbolWithDefaultProps = Omit<CreatureSymbolProps, "data"> & {
|
|
|
|
data?: SvgSymbolData;
|
|
|
|
};
|
|
|
|
|
2021-02-15 22:19:07 +00:00
|
|
|
function createCreatureSymbol(
|
|
|
|
name: string
|
2021-02-16 22:42:19 +00:00
|
|
|
): React.FC<CreatureSymbolWithDefaultProps> {
|
2021-02-15 22:19:07 +00:00
|
|
|
const data = getSymbol(name);
|
2021-02-16 22:11:41 +00:00
|
|
|
return (props) => <CreatureSymbol data={props.data || data} {...props} />;
|
2021-02-15 22:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Eye = createCreatureSymbol("eye");
|
|
|
|
|
|
|
|
const Hand = createCreatureSymbol("hand");
|
|
|
|
|
2021-02-16 00:28:03 +00:00
|
|
|
const Arm = createCreatureSymbol("arm");
|
2021-02-15 22:19:07 +00:00
|
|
|
|
2021-02-16 01:12:14 +00:00
|
|
|
const Antler = createCreatureSymbol("antler");
|
|
|
|
|
|
|
|
const Crown = createCreatureSymbol("crown");
|
|
|
|
|
2021-02-16 01:20:41 +00:00
|
|
|
const Wing = createCreatureSymbol("wing");
|
|
|
|
|
|
|
|
const MuscleArm = createCreatureSymbol("muscle arm");
|
|
|
|
|
2021-02-16 03:51:35 +00:00
|
|
|
const Leg = createCreatureSymbol("leg");
|
|
|
|
|
2021-02-16 01:20:41 +00:00
|
|
|
const Tail = createCreatureSymbol("tail");
|
|
|
|
|
2021-02-16 22:11:41 +00:00
|
|
|
const EYE_CREATURE = (
|
|
|
|
<Eye>
|
|
|
|
<Arm attachTo="arm" left>
|
|
|
|
<Wing attachTo="arm" left right />
|
|
|
|
</Arm>
|
|
|
|
<Arm attachTo="arm" right>
|
|
|
|
<MuscleArm attachTo="arm" left right />
|
|
|
|
</Arm>
|
|
|
|
<Antler attachTo="horn" left right />
|
|
|
|
<Crown attachTo="crown">
|
|
|
|
<Hand attachTo="horn" left right>
|
|
|
|
<Arm attachTo="arm" left />
|
|
|
|
</Hand>
|
|
|
|
</Crown>
|
|
|
|
<Leg attachTo="leg" left right />
|
|
|
|
<Tail attachTo="tail" />
|
|
|
|
</Eye>
|
|
|
|
);
|
|
|
|
|
2021-02-16 22:42:19 +00:00
|
|
|
function randomlyReplaceParts(rng: Random, creature: JSX.Element): JSX.Element {
|
|
|
|
return React.cloneElement<CreatureSymbolWithDefaultProps>(creature, {
|
|
|
|
data: rng.choice(SvgVocabulary),
|
|
|
|
children: React.Children.map(creature.props.children, (child, i) => {
|
|
|
|
return randomlyReplaceParts(rng, child);
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:46:30 +00:00
|
|
|
function getSvgMarkup(el: SVGSVGElement): string {
|
|
|
|
return [
|
|
|
|
`<?xml version="1.0" encoding="utf-8"?>`,
|
|
|
|
"<!-- Generator: https://github.com/toolness/mystic-symbolic -->",
|
|
|
|
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">',
|
|
|
|
el.outerHTML,
|
|
|
|
].join("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
function exportSvg(filename: string, svgEl: SVGSVGElement) {
|
|
|
|
const dataURL = `data:image/svg+xml;utf8,${encodeURIComponent(
|
|
|
|
getSvgMarkup(svgEl)
|
|
|
|
)}`;
|
|
|
|
const anchor = document.createElement("a");
|
|
|
|
anchor.href = dataURL;
|
|
|
|
anchor.download = filename;
|
|
|
|
document.body.append(anchor);
|
|
|
|
anchor.click();
|
|
|
|
document.body.removeChild(anchor);
|
|
|
|
}
|
|
|
|
|
2021-02-17 01:54:05 +00:00
|
|
|
const AutoSizingSvg: React.FC<{
|
|
|
|
padding: number;
|
|
|
|
children: JSX.Element | JSX.Element[];
|
2021-02-17 13:46:30 +00:00
|
|
|
downloadFilename?: string;
|
|
|
|
}> = ({ downloadFilename, ...props }) => {
|
2021-02-17 01:47:12 +00:00
|
|
|
const ref = useRef<SVGSVGElement>(null);
|
|
|
|
const [x, setX] = useState(0);
|
|
|
|
const [y, setY] = useState(0);
|
|
|
|
const [width, setWidth] = useState(1);
|
|
|
|
const [height, setHeight] = useState(1);
|
2021-02-17 13:46:30 +00:00
|
|
|
const svgEl = ref.current;
|
2021-02-17 01:54:05 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (ref.current) {
|
|
|
|
const bbox = ref.current.getBBox();
|
|
|
|
setX(bbox.x - props.padding);
|
|
|
|
setY(bbox.y - props.padding);
|
|
|
|
setWidth(bbox.width + props.padding * 2);
|
|
|
|
setHeight(bbox.height + props.padding * 2);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2021-02-17 13:46:30 +00:00
|
|
|
<>
|
|
|
|
<svg
|
|
|
|
version="1.1"
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
width={`${width}px`}
|
|
|
|
height={`${height}px`}
|
|
|
|
viewBox={`${x} ${y} ${width} ${height}`}
|
|
|
|
ref={ref}
|
|
|
|
>
|
|
|
|
{props.children}
|
|
|
|
</svg>
|
|
|
|
{downloadFilename && svgEl && (
|
|
|
|
<p>
|
|
|
|
<button onClick={() => exportSvg(downloadFilename, svgEl)}>
|
|
|
|
Export SVG
|
|
|
|
</button>
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
</>
|
2021-02-17 01:54:05 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const CreaturePage: React.FC<{}> = () => {
|
2021-02-16 22:42:19 +00:00
|
|
|
const [randomSeed, setRandomSeed] = useState<number | null>(null);
|
2021-02-17 13:07:04 +00:00
|
|
|
const [symbolCtx, setSymbolCtx] = useState(createSvgSymbolContext());
|
2021-02-16 03:36:18 +00:00
|
|
|
const defaultCtx = useContext(CreatureContext);
|
|
|
|
const ctx: CreatureContextType = {
|
|
|
|
...defaultCtx,
|
2021-02-17 13:07:04 +00:00
|
|
|
...symbolCtx,
|
|
|
|
fill: symbolCtx.showSpecs ? "none" : symbolCtx.fill,
|
2021-02-16 03:36:18 +00:00
|
|
|
};
|
2021-02-16 22:42:19 +00:00
|
|
|
const creature =
|
|
|
|
randomSeed === null
|
|
|
|
? EYE_CREATURE
|
|
|
|
: randomlyReplaceParts(new Random(randomSeed), EYE_CREATURE);
|
2021-02-17 13:46:30 +00:00
|
|
|
let downloadBasename = "mystic-symbolic-creature";
|
|
|
|
|
|
|
|
if (randomSeed !== null) {
|
|
|
|
downloadBasename += `-${randomSeed}`;
|
|
|
|
}
|
2021-02-16 03:36:18 +00:00
|
|
|
|
2021-02-15 13:34:22 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<h1>Creature!</h1>
|
2021-02-17 13:07:04 +00:00
|
|
|
<SymbolContextWidget ctx={symbolCtx} onChange={setSymbolCtx} />
|
2021-02-17 01:47:12 +00:00
|
|
|
<p>
|
2021-02-17 13:10:20 +00:00
|
|
|
<button onClick={() => setRandomSeed(Date.now())}>Randomize!</button>{" "}
|
|
|
|
<button onClick={() => window.location.reload()}>Reset</button>
|
2021-02-17 01:47:12 +00:00
|
|
|
</p>
|
2021-02-16 03:36:18 +00:00
|
|
|
<CreatureContext.Provider value={ctx}>
|
2021-02-17 13:46:30 +00:00
|
|
|
<AutoSizingSvg padding={5} downloadFilename={`${downloadBasename}.svg`}>
|
|
|
|
<g transform="scale(0.5 0.5)">{creature}</g>
|
2021-02-17 01:54:05 +00:00
|
|
|
</AutoSizingSvg>
|
2021-02-16 03:36:18 +00:00
|
|
|
</CreatureContext.Provider>
|
2021-02-15 13:34:22 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|