-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ecs-browser): Allow spawning of prototypes at arbitrary locations (
#117) * fix: clear dev highlights when mouse leaves entity section * feat(ecs-browser): allow spawning of prototypes at arbitrary locations
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Component, EntityID, Has, Layers, Type } from "@latticexyz/recs"; | ||
import React, { useEffect, useState } from "react"; | ||
import { ComponentBrowserButton, ComponentBrowserSelect } from "../StyledComponents"; | ||
import { Coord } from "@latticexyz/phaserx"; | ||
import { useComponentValueStream, useQuery } from "@latticexyz/std-client"; | ||
|
||
export const PrototypeCreator: React.FC<{ | ||
layers: Layers; | ||
spawnPrototypeAt: (prototypeId: EntityID, position: Coord) => void; | ||
prototypeComponent: Component<{ value: Type.StringArray }>; | ||
hoverHighlightComponent: Component<{ x: Type.OptionalNumber; y: Type.OptionalNumber }>; | ||
}> = ({ layers, prototypeComponent, hoverHighlightComponent, spawnPrototypeAt }) => { | ||
const [selectingPosition, setSelectingPosition] = useState(false); | ||
const [selectedPrototype, setSelectedPrototype] = useState<string | undefined>(undefined); | ||
const hoverHighlight = useComponentValueStream(hoverHighlightComponent); | ||
|
||
const prototypes = useQuery([Has(prototypeComponent)]); | ||
|
||
useEffect(() => { | ||
if (!selectingPosition) return; | ||
|
||
function onMouseDown() { | ||
if (!hoverHighlight) return; | ||
if (hoverHighlight.x == null || hoverHighlight.y == null) return; | ||
|
||
setSelectingPosition(false); | ||
spawnPrototypeAt(selectedPrototype as EntityID, { x: hoverHighlight.x, y: hoverHighlight.y }); | ||
} | ||
|
||
document.addEventListener("mousedown", onMouseDown); | ||
return () => { | ||
document.removeEventListener("mousedown", onMouseDown); | ||
}; | ||
}, [selectingPosition, hoverHighlight]); | ||
|
||
if (!prototypes) return null; | ||
|
||
return ( | ||
<div | ||
style={{ | ||
display: "flex", | ||
flexDirection: "row", | ||
}} | ||
> | ||
<ComponentBrowserSelect | ||
style={{ margin: "8px auto" }} | ||
value={selectedPrototype} | ||
onChange={(e) => { | ||
setSelectedPrototype(e.target.value); | ||
}} | ||
> | ||
<option value="">None</option> | ||
{[...prototypes].map((entityIndex) => { | ||
const entityId = layers.network.world.entities[entityIndex]; | ||
return ( | ||
<option key={entityId} value={entityId}> | ||
{entityId} | ||
</option> | ||
); | ||
})} | ||
</ComponentBrowserSelect> | ||
<ComponentBrowserButton | ||
style={{ margin: "8px auto" }} | ||
active={selectingPosition} | ||
onClick={() => { | ||
setSelectingPosition(true); | ||
}} | ||
> | ||
{selectingPosition ? `Spawn prototype at (${hoverHighlight?.x}, ${hoverHighlight?.y})` : 'Begin spawning prototype' } | ||
</ComponentBrowserButton> | ||
</div> | ||
); | ||
}; |