-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ecs-browser): create useComponentValueStream hook and use it…
… in ecs-browser
- Loading branch information
Showing
10 changed files
with
125 additions
and
43 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
44 changes: 44 additions & 0 deletions
44
packages/ecs-browser/src/QueryBuilder/PositionFilterButton.tsx
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,44 @@ | ||
import { Component, Type } from "@latticexyz/recs"; | ||
import { useComponentValueStream } from "@latticexyz/std-client"; | ||
import React, { useEffect, useState } from "react"; | ||
import { ComponentBrowserButton } from "../StyledComponents"; | ||
|
||
export const PositionFilterButton: React.FC<{ | ||
editQuery: (queryText: string) => void; | ||
queryInputRef: React.RefObject<HTMLInputElement>; | ||
hoverHighlightComponent: Component<{ x: Type.OptionalNumber; y: Type.OptionalNumber }>; | ||
}> = ({ hoverHighlightComponent, editQuery, queryInputRef }) => { | ||
const [selectingPosition, setSelectingPosition] = useState(false); | ||
const hoverPosition = useComponentValueStream(hoverHighlightComponent); | ||
|
||
useEffect(() => { | ||
if (!selectingPosition) return; | ||
|
||
function onMouseDown() { | ||
if (!hoverPosition) return; | ||
|
||
setSelectingPosition(false); | ||
editQuery(`[q.HasValue(c.LocalPosition, { x: ${hoverPosition.x}, y: ${hoverPosition.y} })]`); | ||
queryInputRef.current?.focus(); | ||
} | ||
|
||
document.addEventListener("mousedown", onMouseDown); | ||
return () => { | ||
document.removeEventListener("mousedown", onMouseDown); | ||
}; | ||
}, [selectingPosition, hoverPosition]); | ||
|
||
return ( | ||
<ComponentBrowserButton | ||
style={{ margin: "8px auto" }} | ||
active={selectingPosition} | ||
onClick={() => { | ||
setSelectingPosition(true); | ||
}} | ||
> | ||
{selectingPosition | ||
? `[q.HasValue(c.LocalPosition, { x: ${hoverPosition?.x}, y: ${hoverPosition?.y} })]` | ||
: "Select Entities at a position"} | ||
</ComponentBrowserButton> | ||
); | ||
}; |
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,15 @@ | ||
import styled from "styled-components"; | ||
|
||
export const QueryBuilderForm = styled.form` | ||
padding: 8px; | ||
border-bottom: 2px grey solid; | ||
margin-bottom: 8px; | ||
width: 100%; | ||
`; | ||
|
||
export const QueryShortcutContainer = styled.div` | ||
flex: "row wrap"; | ||
margin-top: "8px"; | ||
height: 200px; | ||
overflow: auto; | ||
`; |
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 @@ | ||
export * from "./QueryBuilder"; |
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