-
Notifications
You must be signed in to change notification settings - Fork 3
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 #6 from clarkmcc/5-input-groups
Node Input Groups
- Loading branch information
Showing
10 changed files
with
968 additions
and
27 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,289 @@ | ||
import { NodeGraphEditor } from './NodeGraphEditor' | ||
import { Meta, StoryObj } from '@storybook/react' | ||
import { GraphConfigProvider } from './context/GraphConfigContext' | ||
import { | ||
Background, | ||
BackgroundVariant, | ||
Edge, | ||
Node, | ||
ReactFlowProvider, | ||
} from 'reactflow' | ||
import { useBuildGraphConfig } from './hooks/config.ts' | ||
import { InputProps } from './config.ts' | ||
import { Wheel } from '@uiw/react-color' | ||
import { useNodeFieldValue } from './hooks/node.ts' | ||
|
||
const meta = { | ||
title: 'Node Graph Editor', | ||
component: ({ nodes, edges }) => { | ||
function ColorPicker({ slots, ...config }: InputProps) { | ||
const [hsva, setHsva] = useNodeFieldValue(config.id, '#f87171') | ||
const Handle = slots?.Handle | ||
return ( | ||
<div | ||
style={{ | ||
position: 'relative', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
padding: '12px 12px', | ||
}} | ||
> | ||
{Handle && <Handle />} | ||
<Wheel | ||
width={140} | ||
height={140} | ||
color={hsva} | ||
onChange={(color) => setHsva(color.hex)} | ||
onFocus={config.onFocus} | ||
onBlur={config.onBlur} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
const config = useBuildGraphConfig( | ||
{ | ||
valueTypes: { | ||
string: { | ||
name: 'String', | ||
color: '#a1a1a1', | ||
inputType: 'value', | ||
defaultValue: '', | ||
}, | ||
number: { | ||
name: 'Number', | ||
color: '#a1a1a1', | ||
inputType: 'value', | ||
defaultValue: '0.000', | ||
}, | ||
boolean: { | ||
name: 'Boolean', | ||
color: '#a1a1a1', | ||
inputType: 'checkbox', | ||
defaultValue: true, | ||
}, | ||
specularDistribution: { | ||
name: 'Specular Distribution', | ||
color: '#06b6d4', | ||
inputType: 'options', | ||
options: [ | ||
{ name: 'GGX', value: 'ggx' }, | ||
{ name: 'Beckmann', value: 'beckmann' }, | ||
{ name: 'Phong', value: 'phong' }, | ||
], | ||
defaultValue: 'GET', | ||
}, | ||
}, | ||
nodeGroups: { | ||
default: { | ||
name: 'Default', | ||
color: '#CE4040', | ||
}, | ||
inputs: { | ||
name: 'Inputs', | ||
color: '#83324A', | ||
}, | ||
}, | ||
nodes: { | ||
number: { | ||
group: 'default', | ||
name: 'Number', | ||
inputs: [ | ||
{ | ||
name: 'Value', | ||
id: 'value', | ||
valueType: 'number', | ||
isConstant: true, | ||
}, | ||
], | ||
outputs: [ | ||
{ | ||
name: 'Value', | ||
id: 'value', | ||
valueType: 'number', | ||
}, | ||
], | ||
}, | ||
color: { | ||
group: 'inputs', | ||
name: 'Color', | ||
style: { | ||
width: '100px', | ||
}, | ||
inputs: [ | ||
{ | ||
name: 'Color', | ||
id: 'color', | ||
valueType: 'color', | ||
isConstant: true, | ||
}, | ||
], | ||
outputs: [ | ||
{ | ||
name: 'Color', | ||
id: 'color', | ||
valueType: 'color', | ||
}, | ||
], | ||
}, | ||
bsdf: { | ||
group: 'default', | ||
name: 'Principled BSDF', | ||
inputs: [ | ||
{ | ||
name: 'Metallic', | ||
id: 'metallic', | ||
valueType: 'number', | ||
}, | ||
{ | ||
name: 'Roughness', | ||
id: 'roughness', | ||
valueType: 'number', | ||
defaultValue: '0.550', | ||
}, | ||
{ | ||
name: 'IOR', | ||
id: 'ior', | ||
valueType: 'number', | ||
defaultValue: '1.450', | ||
}, | ||
{ | ||
name: 'Alpha', | ||
id: 'alpha', | ||
valueType: 'number', | ||
defaultValue: '1.000', | ||
}, | ||
{ | ||
name: 'Distribution', | ||
id: 'distribution', | ||
group: 'Specular', | ||
valueType: 'specularDistribution', | ||
}, | ||
{ | ||
name: 'IOR Level', | ||
id: 'iorLevel', | ||
group: 'Specular', | ||
valueType: 'number', | ||
}, | ||
{ | ||
name: 'Tint', | ||
id: 'tint', | ||
group: 'Specular', | ||
valueType: 'color', | ||
}, | ||
{ | ||
name: 'Anisotropic', | ||
id: 'anisotropic', | ||
group: 'Specular', | ||
valueType: 'number', | ||
}, | ||
{ | ||
name: 'Anisotropic Rotation', | ||
id: 'anisotropicRotation', | ||
group: 'Specular', | ||
valueType: 'number', | ||
}, | ||
{ | ||
name: 'Strength', | ||
id: 'strength', | ||
group: 'Emission', | ||
valueType: 'number', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
(config) => { | ||
config.registerInput('color', ColorPicker, { | ||
name: 'Color', | ||
color: '#C7C728', | ||
}) | ||
}, | ||
) | ||
return ( | ||
<GraphConfigProvider defaultConfig={config}> | ||
<ReactFlowProvider> | ||
<NodeGraphEditor defaultNodes={nodes} defaultEdges={edges}> | ||
<Background color="#52525b" variant={BackgroundVariant.Dots} /> | ||
</NodeGraphEditor> | ||
</ReactFlowProvider> | ||
</GraphConfigProvider> | ||
) | ||
}, | ||
decorators: (Story) => ( | ||
<div style={{ width: '100vw', height: '100vh' }}> | ||
<Story /> | ||
</div> | ||
), | ||
tags: ['autodocs'], | ||
} satisfies Meta<{ nodes: Node[]; edges: Edge[] }> | ||
|
||
export default meta | ||
|
||
type Story = StoryObj<typeof meta> | ||
|
||
export const InputGroups: Story = { | ||
parameters: { | ||
layout: 'fullscreen', | ||
}, | ||
args: { | ||
nodes: [ | ||
{ | ||
id: '1', | ||
type: 'bsdf', | ||
position: { x: 350, y: 100 }, | ||
data: { | ||
__inputGroupsExpanded: ['Specular'], | ||
}, | ||
}, | ||
{ | ||
id: '2', | ||
type: 'number', | ||
position: { x: 100, y: 100 }, | ||
data: {}, | ||
}, | ||
{ | ||
id: '3', | ||
type: 'number', | ||
position: { x: 100, y: 200 }, | ||
data: {}, | ||
}, | ||
{ | ||
id: '4', | ||
type: 'color', | ||
position: { x: 100, y: 300 }, | ||
data: {}, | ||
}, | ||
], | ||
edges: [ | ||
{ | ||
id: 'e1', | ||
source: '3', | ||
sourceHandle: 'value', | ||
target: '1', | ||
targetHandle: 'strength', | ||
}, | ||
{ | ||
id: 'e2', | ||
source: '2', | ||
sourceHandle: 'value', | ||
target: '1', | ||
targetHandle: 'anisotropicRotation', | ||
}, | ||
{ | ||
id: 'e3', | ||
source: '3', | ||
sourceHandle: 'value', | ||
target: '1', | ||
targetHandle: 'anisotropic', | ||
}, | ||
{ | ||
id: 'e4', | ||
source: '4', | ||
sourceHandle: 'value', | ||
target: '1', | ||
targetHandle: 'tint', | ||
}, | ||
], | ||
}, | ||
} |
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,79 @@ | ||
import { CSSProperties, ReactNode, useMemo } from 'react' | ||
import { GoTriangleDown } from 'react-icons/go' | ||
import { useNodeInputGroupState } from '../hooks/node.ts' | ||
|
||
type InputGroupProps = { | ||
label: string | ||
children: ReactNode | ||
style?: CSSProperties | ||
labelStyle?: CSSProperties | ||
handles: ReactNode | ||
} | ||
|
||
export const InputGroup = ({ | ||
label, | ||
children, | ||
labelStyle, | ||
handles, | ||
}: InputGroupProps) => { | ||
const [isOpen, setIsOpen] = useNodeInputGroupState(label) | ||
|
||
const toggleAccordion = () => { | ||
setIsOpen(!isOpen) | ||
} | ||
|
||
const defaultStyles: Record<string, CSSProperties> = useMemo( | ||
() => ({ | ||
label: { | ||
fontSize: '12px', | ||
cursor: 'pointer', | ||
padding: '3px 8px', | ||
display: 'flex', | ||
color: 'white', | ||
textShadow: '0 1px rgba(0,0,0,0.4)', | ||
transition: 'transform 0.1s ease', | ||
}, | ||
icon: { | ||
color: 'white', | ||
opacity: 0.4, | ||
fontSize: 18, | ||
cursor: 'pointer', | ||
transition: 'transform 0.1s ease', | ||
transform: isOpen ? 'rotate(0deg)' : 'rotate(-90deg)', | ||
}, | ||
content: { | ||
background: isOpen ? '#1e1e1e' : undefined, | ||
padding: '8px 0 8px', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
}, | ||
}), | ||
[isOpen], | ||
) | ||
|
||
return ( | ||
<div style={{ position: 'relative' }}> | ||
<div | ||
style={{ | ||
margin: '2px 0', | ||
padding: '0 12px', | ||
opacity: 0 /* using display:none makes edges go crazy*/, | ||
}} | ||
> | ||
{!isOpen && handles} | ||
</div> | ||
<div style={{ position: 'relative' }}> | ||
<div | ||
onClick={toggleAccordion} | ||
style={{ ...defaultStyles.label, ...labelStyle }} | ||
> | ||
<div style={{ position: 'relative', display: 'flex' }}> | ||
<GoTriangleDown style={defaultStyles.icon} /> | ||
<span style={{ marginLeft: 2, paddingTop: 2 }}>{label}</span> | ||
</div> | ||
</div> | ||
{isOpen && <div style={defaultStyles.content}>{children}</div>} | ||
</div> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.