Compare commits
5 Commits
e44b73bebf
...
c7f808ce2d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c7f808ce2d
|
||
|
|
72d6cd6ea2
|
||
|
|
615f2d3c48
|
||
|
|
2fadb6802d
|
||
|
|
13c83efdb9
|
8
Cargo.lock
generated
8
Cargo.lock
generated
@@ -245,6 +245,14 @@ dependencies = [
|
|||||||
"zmij",
|
"zmij",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "shape"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"nodarium_macros",
|
||||||
|
"nodarium_utils",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "stem"
|
name = "stem"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|||||||
@@ -186,15 +186,21 @@ export class GraphState {
|
|||||||
if (!node?.inputs) {
|
if (!node?.inputs) {
|
||||||
return 5;
|
return 5;
|
||||||
}
|
}
|
||||||
const height = 5
|
let height = 5;
|
||||||
+ 10
|
|
||||||
* Object.keys(node.inputs).filter(
|
for (const key of Object.keys(node.inputs)) {
|
||||||
(p) =>
|
if (key === 'seed') continue;
|
||||||
p !== 'seed'
|
if (!node.inputs) continue;
|
||||||
&& node?.inputs
|
if (node?.inputs?.[key] === undefined) continue;
|
||||||
&& !(node?.inputs?.[p] !== undefined && 'setting' in node.inputs[p])
|
if ('setting' in node.inputs[key]) continue;
|
||||||
&& node.inputs[p].hidden !== true
|
if (node.inputs[key].hidden) continue;
|
||||||
).length;
|
if (node.inputs[key].type === 'shape') {
|
||||||
|
height += 20;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
height += 10;
|
||||||
|
}
|
||||||
|
|
||||||
this.nodeHeightCache[nodeTypeId] = height;
|
this.nodeHeightCache[nodeTypeId] = height;
|
||||||
return height;
|
return height;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -166,15 +166,14 @@ export class MouseEventManager {
|
|||||||
|
|
||||||
if (this.state.mouseDown) return;
|
if (this.state.mouseDown) return;
|
||||||
this.state.edgeEndPosition = null;
|
this.state.edgeEndPosition = null;
|
||||||
|
const target = event.target as HTMLElement;
|
||||||
|
|
||||||
if (event.target instanceof HTMLElement) {
|
if (
|
||||||
if (
|
target.nodeName !== 'CANVAS'
|
||||||
event.target.nodeName !== 'CANVAS'
|
&& !target.classList.contains('node')
|
||||||
&& !event.target.classList.contains('node')
|
&& !target.classList.contains('content')
|
||||||
&& !event.target.classList.contains('content')
|
) {
|
||||||
) {
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const mx = event.clientX - this.state.rect.x;
|
const mx = event.clientX - this.state.rect.x;
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
const inputType = $derived(node?.state?.type?.inputs?.[id]);
|
const inputType = $derived(node?.state?.type?.inputs?.[id]);
|
||||||
|
|
||||||
const socketId = $derived(`${node.id}-${id}`);
|
const socketId = $derived(`${node.id}-${id}`);
|
||||||
|
const height = $derived(input.type === 'shape' ? 200 : 100);
|
||||||
|
|
||||||
const graphState = getGraphState();
|
const graphState = getGraphState();
|
||||||
const graphId = graph?.id;
|
const graphId = graph?.id;
|
||||||
@@ -64,6 +65,7 @@
|
|||||||
class="wrapper"
|
class="wrapper"
|
||||||
data-node-type={node.type}
|
data-node-type={node.type}
|
||||||
data-node-input={id}
|
data-node-input={id}
|
||||||
|
style:height="{height}px"
|
||||||
class:possible-socket={graphState?.possibleSocketIds.has(socketId)}
|
class:possible-socket={graphState?.possibleSocketIds.has(socketId)}
|
||||||
>
|
>
|
||||||
{#key id && graphId}
|
{#key id && graphId}
|
||||||
@@ -95,8 +97,6 @@
|
|||||||
<svg
|
<svg
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
viewBox="0 0 100 100"
|
viewBox="0 0 100 100"
|
||||||
width="100"
|
|
||||||
height="100"
|
|
||||||
preserveAspectRatio="none"
|
preserveAspectRatio="none"
|
||||||
style={`
|
style={`
|
||||||
--path: path("${path}");
|
--path: path("${path}");
|
||||||
@@ -111,7 +111,6 @@
|
|||||||
.wrapper {
|
.wrapper {
|
||||||
position: relative;
|
position: relative;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100px;
|
|
||||||
transform: translateY(-0.5px);
|
transform: translateY(-0.5px);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,25 +22,10 @@
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
function detectCommitType(commit: string) {
|
function detectCommitType(commit: string) {
|
||||||
if (commit.startsWith('fix:') || commit.startsWith('fix(')) {
|
for (const key of typeMap.keys()) {
|
||||||
return 'fix';
|
if (commit.startsWith(key)) {
|
||||||
}
|
return key;
|
||||||
|
}
|
||||||
if (commit.startsWith('feat:') || commit.startsWith('feat(')) {
|
|
||||||
return 'feat';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (commit.startsWith('chore:') || commit.startsWith('chore(')) {
|
|
||||||
return 'chore';
|
|
||||||
}
|
|
||||||
if (commit.startsWith('docs:') || commit.startsWith('docs(')) {
|
|
||||||
return 'docs';
|
|
||||||
}
|
|
||||||
if (commit.startsWith('refactor:') || commit.startsWith('refactor(')) {
|
|
||||||
return 'refactor';
|
|
||||||
}
|
|
||||||
if (commit.startsWith('ci:') || commit.startsWith('ci(')) {
|
|
||||||
return 'ci';
|
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@@ -52,7 +37,7 @@
|
|||||||
|
|
||||||
const match = line.match(regex);
|
const match = line.match(regex);
|
||||||
if (!match) {
|
if (!match) {
|
||||||
throw new Error('Invalid commit line format');
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [, sha, link, description] = match;
|
const [, sha, link, description] = match;
|
||||||
|
|||||||
6
nodes/max/plantarium/shape/.gitignore
vendored
Normal file
6
nodes/max/plantarium/shape/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
/target
|
||||||
|
**/*.rs.bk
|
||||||
|
Cargo.lock
|
||||||
|
bin/
|
||||||
|
pkg/
|
||||||
|
wasm-pack.log
|
||||||
12
nodes/max/plantarium/shape/Cargo.toml
Normal file
12
nodes/max/plantarium/shape/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "shape"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Max Richter <jim-x@web.de>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
crate-type = ["cdylib", "rlib"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
nodarium_macros = { version = "0.1.0", path = "../../../../packages/macros" }
|
||||||
|
nodarium_utils = { version = "0.1.0", path = "../../../../packages/utils" }
|
||||||
26
nodes/max/plantarium/shape/src/input.json
Normal file
26
nodes/max/plantarium/shape/src/input.json
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"id": "max/plantarium/shape",
|
||||||
|
"outputs": [
|
||||||
|
"shape"
|
||||||
|
],
|
||||||
|
"inputs": {
|
||||||
|
"shape": {
|
||||||
|
"type": "shape",
|
||||||
|
"value": [
|
||||||
|
47.8,
|
||||||
|
100,
|
||||||
|
47.8,
|
||||||
|
82.8,
|
||||||
|
30.9,
|
||||||
|
69.1,
|
||||||
|
23.2,
|
||||||
|
40.7,
|
||||||
|
27.1,
|
||||||
|
14.5,
|
||||||
|
42.5,
|
||||||
|
0
|
||||||
|
],
|
||||||
|
"label": ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
nodes/max/plantarium/shape/src/lib.rs
Normal file
13
nodes/max/plantarium/shape/src/lib.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use nodarium_macros::nodarium_definition_file;
|
||||||
|
use nodarium_macros::nodarium_execute;
|
||||||
|
use nodarium_utils::{concat_args, log, split_args};
|
||||||
|
|
||||||
|
nodarium_definition_file!("src/input.json");
|
||||||
|
|
||||||
|
#[nodarium_execute]
|
||||||
|
pub fn execute(input: &[i32]) -> Vec<i32> {
|
||||||
|
let args = split_args(input);
|
||||||
|
log!("vec3 input: {:?}", input);
|
||||||
|
log!("vec3 args: {:?}", args);
|
||||||
|
concat_args(args)
|
||||||
|
}
|
||||||
@@ -26,7 +26,6 @@ const DefaultOptionsSchema = z.object({
|
|||||||
export const NodeInputFloatSchema = z.object({
|
export const NodeInputFloatSchema = z.object({
|
||||||
...DefaultOptionsSchema.shape,
|
...DefaultOptionsSchema.shape,
|
||||||
type: z.literal('float'),
|
type: z.literal('float'),
|
||||||
element: z.literal('slider').optional(),
|
|
||||||
value: z.number().optional(),
|
value: z.number().optional(),
|
||||||
min: z.number().optional(),
|
min: z.number().optional(),
|
||||||
max: z.number().optional(),
|
max: z.number().optional(),
|
||||||
@@ -36,12 +35,17 @@ export const NodeInputFloatSchema = z.object({
|
|||||||
export const NodeInputIntegerSchema = z.object({
|
export const NodeInputIntegerSchema = z.object({
|
||||||
...DefaultOptionsSchema.shape,
|
...DefaultOptionsSchema.shape,
|
||||||
type: z.literal('integer'),
|
type: z.literal('integer'),
|
||||||
element: z.literal('slider').optional(),
|
|
||||||
value: z.number().optional(),
|
value: z.number().optional(),
|
||||||
min: z.number().optional(),
|
min: z.number().optional(),
|
||||||
max: z.number().optional()
|
max: z.number().optional()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const NodeInputShapeSchema = z.object({
|
||||||
|
...DefaultOptionsSchema.shape,
|
||||||
|
type: z.literal('shape'),
|
||||||
|
value: z.array(z.number()).optional()
|
||||||
|
});
|
||||||
|
|
||||||
export const NodeInputBooleanSchema = z.object({
|
export const NodeInputBooleanSchema = z.object({
|
||||||
...DefaultOptionsSchema.shape,
|
...DefaultOptionsSchema.shape,
|
||||||
type: z.literal('boolean'),
|
type: z.literal('boolean'),
|
||||||
@@ -84,6 +88,7 @@ export const NodeInputSchema = z.union([
|
|||||||
NodeInputBooleanSchema,
|
NodeInputBooleanSchema,
|
||||||
NodeInputFloatSchema,
|
NodeInputFloatSchema,
|
||||||
NodeInputIntegerSchema,
|
NodeInputIntegerSchema,
|
||||||
|
NodeInputShapeSchema,
|
||||||
NodeInputSelectSchema,
|
NodeInputSelectSchema,
|
||||||
NodeInputSeedSchema,
|
NodeInputSeedSchema,
|
||||||
NodeInputVec3Schema,
|
NodeInputVec3Schema,
|
||||||
|
|||||||
@@ -103,6 +103,15 @@ pub struct NodeInputVec3 {
|
|||||||
pub value: Option<Vec<f64>>,
|
pub value: Option<Vec<f64>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct NodeInputShape {
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub default_options: DefaultOptions,
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub value: Option<Vec<f64>>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct NodeInputGeometry {
|
pub struct NodeInputGeometry {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
@@ -125,6 +134,7 @@ pub enum NodeInput {
|
|||||||
select(NodeInputSelect),
|
select(NodeInputSelect),
|
||||||
seed(NodeInputSeed),
|
seed(NodeInputSeed),
|
||||||
vec3(NodeInputVec3),
|
vec3(NodeInputVec3),
|
||||||
|
shape(NodeInputShape),
|
||||||
geometry(NodeInputGeometry),
|
geometry(NodeInputGeometry),
|
||||||
path(NodeInputPath),
|
path(NodeInputPath),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { NodeInput } from '@nodarium/types';
|
import type { NodeInput } from '@nodarium/types';
|
||||||
|
|
||||||
import { InputCheckbox, InputNumber, InputSelect, InputVec3 } from './index';
|
import { InputCheckbox, InputNumber, InputSelect, InputShape, InputVec3 } from './index';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
input: NodeInput;
|
input: NodeInput;
|
||||||
@@ -19,6 +19,8 @@
|
|||||||
max={input?.max}
|
max={input?.max}
|
||||||
step={input?.step}
|
step={input?.step}
|
||||||
/>
|
/>
|
||||||
|
{:else if input.type === 'shape'}
|
||||||
|
<InputShape bind:value={value as number[]} />
|
||||||
{:else if input.type === 'integer'}
|
{:else if input.type === 'integer'}
|
||||||
<InputNumber bind:value={value as number} min={input?.min} max={input?.max} step={1} />
|
<InputNumber bind:value={value as number} min={input?.min} max={input?.max} step={1} />
|
||||||
{:else if input.type === 'boolean'}
|
{:else if input.type === 'boolean'}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ export { default as Input } from './Input.svelte';
|
|||||||
export { default as InputCheckbox } from './inputs/InputCheckbox.svelte';
|
export { default as InputCheckbox } from './inputs/InputCheckbox.svelte';
|
||||||
export { default as InputNumber } from './inputs/InputNumber.svelte';
|
export { default as InputNumber } from './inputs/InputNumber.svelte';
|
||||||
export { default as InputSelect } from './inputs/InputSelect.svelte';
|
export { default as InputSelect } from './inputs/InputSelect.svelte';
|
||||||
|
export { default as InputShape } from './inputs/InputShape.svelte';
|
||||||
export { default as InputVec3 } from './inputs/InputVec3.svelte';
|
export { default as InputVec3 } from './inputs/InputVec3.svelte';
|
||||||
|
|
||||||
export { default as Details } from './Details.svelte';
|
export { default as Details } from './Details.svelte';
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
{id}
|
{id}
|
||||||
/>
|
/>
|
||||||
<span
|
<span
|
||||||
class="absolute opacity-0 peer-checked:opacity-100 transition-opacity duration-100 flex w-full h-full items-center justify-center"
|
class="absolute opacity-0 peer-checked:opacity-100 transition-opacity duration-50 flex w-full h-full items-center justify-center"
|
||||||
>
|
>
|
||||||
<svg
|
<svg
|
||||||
viewBox="0 0 19 14"
|
viewBox="0 0 19 14"
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
select {
|
select {
|
||||||
font-family: var(--font-family);
|
font-family: var(--font-family);
|
||||||
outline: solid 1px var(--color-outline);
|
outline: solid 1px var(--color-outline);
|
||||||
padding: 0.8em 1em;
|
padding: 0.5em 0.8em;
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
border: none;
|
border: none;
|
||||||
}
|
}
|
||||||
|
|||||||
241
packages/ui/src/lib/inputs/InputShape.svelte
Normal file
241
packages/ui/src/lib/inputs/InputShape.svelte
Normal file
@@ -0,0 +1,241 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
let mouseDown = $state<number[]>();
|
||||||
|
let draggingIndex = $state<number>();
|
||||||
|
let downCirclePosition = $state<number[]>();
|
||||||
|
let svgElement = $state<SVGElement>(null!);
|
||||||
|
let svgRect = $state<DOMRect>(null!);
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
value: number[];
|
||||||
|
mirror?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
function defaultPoints(): number[] {
|
||||||
|
return [47.8, 100, 47.8, 82.8, 30.9, 69.1, 23.2, 40.7, 27.1, 14.5, 42.5, 0];
|
||||||
|
}
|
||||||
|
|
||||||
|
let { value: points = $bindable(), mirror = true }: Props = $props();
|
||||||
|
|
||||||
|
const getPt = (pts: number[], i: number) => [pts[i * 2], pts[i * 2 + 1]] as [number, number];
|
||||||
|
const setPt = (pts: number[], i: number, x: number, y: number) => {
|
||||||
|
pts[i * 2] = x;
|
||||||
|
pts[i * 2 + 1] = y;
|
||||||
|
};
|
||||||
|
|
||||||
|
$effect(() => {
|
||||||
|
if (!points.length) {
|
||||||
|
points = defaultPoints();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function mirrorPoints(pts: number[]) {
|
||||||
|
const res: number[] = [];
|
||||||
|
for (let i = 0; i < pts.length; i += 2) {
|
||||||
|
res.push(100 - pts[i], pts[i + 1]);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
function calculatePath(pts: number[], mirror = false): string {
|
||||||
|
if (pts.length === 0) return '';
|
||||||
|
|
||||||
|
const arr = [...pts];
|
||||||
|
if (mirror) {
|
||||||
|
const sorted = [];
|
||||||
|
for (let i = 0; i < arr.length; i += 2) {
|
||||||
|
sorted.push({ x: arr[i], y: arr[i + 1], idx: i });
|
||||||
|
}
|
||||||
|
sorted.sort((a, b) => {
|
||||||
|
if (a.y !== b.y) return b.y - a.y;
|
||||||
|
return a.x - b.x;
|
||||||
|
});
|
||||||
|
for (let i = 0; i < sorted.length; i++) {
|
||||||
|
arr[i * 2] = sorted[i].x;
|
||||||
|
arr[i * 2 + 1] = sorted[i].y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let d = `M ${arr[0]} ${arr[1]}`;
|
||||||
|
for (let i = 2; i < arr.length; i += 2) {
|
||||||
|
d += ` L ${arr[i]} ${arr[i + 1]}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mirror) {
|
||||||
|
for (let i = arr.length - 2; i >= 0; i -= 2) {
|
||||||
|
const x = 100 - arr[i];
|
||||||
|
d += ` L ${x} ${arr[i + 1]}`;
|
||||||
|
}
|
||||||
|
d += ' Z';
|
||||||
|
}
|
||||||
|
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
const clamp = (v: number, min: number, max: number) => Math.min(Math.max(v, min), max);
|
||||||
|
const round = (v: number) => Math.floor(v * 10) / 10;
|
||||||
|
|
||||||
|
function handleMouseMove(ev: MouseEvent) {
|
||||||
|
if (mouseDown === undefined || draggingIndex === undefined || !downCirclePosition) return;
|
||||||
|
|
||||||
|
let vx = (mouseDown[0] - ev.clientX) * (100 / svgRect.width);
|
||||||
|
let vy = (mouseDown[1] - ev.clientY) * (100 / svgRect.height);
|
||||||
|
|
||||||
|
let x = downCirclePosition[0] - vx;
|
||||||
|
let y = downCirclePosition[1] - vy;
|
||||||
|
|
||||||
|
x = clamp(x, 0, mirror ? 50 : 100);
|
||||||
|
y = clamp(y, 0, 100);
|
||||||
|
|
||||||
|
setPt(points, draggingIndex, round(x), round(y));
|
||||||
|
}
|
||||||
|
|
||||||
|
function clientToSvg(ev: MouseEvent): [number, number] {
|
||||||
|
svgRect = svgElement.getBoundingClientRect();
|
||||||
|
|
||||||
|
const x = ((ev.clientX - svgRect.left) / svgRect.width) * 100;
|
||||||
|
const y = ((ev.clientY - svgRect.top) / svgRect.height) * 100;
|
||||||
|
|
||||||
|
return [round(clamp(x, 0, mirror ? 50 : 100)), round(clamp(y, 0, 100))];
|
||||||
|
}
|
||||||
|
|
||||||
|
function dist(a: [number, number], b: [number, number]) {
|
||||||
|
return Math.hypot(a[0] - b[0], a[1] - b[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function insertBetween(newPt: [number, number]) {
|
||||||
|
const count = points.length / 2;
|
||||||
|
if (count < 2) {
|
||||||
|
points = [...points, newPt[0], newPt[1]];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let minDist = Infinity;
|
||||||
|
let insertIdx = 0;
|
||||||
|
|
||||||
|
for (let i = 0; i < count - 1; i++) {
|
||||||
|
const a = getPt(points, i);
|
||||||
|
const b = getPt(points, i + 1);
|
||||||
|
const d = dist(newPt, a) + dist(newPt, b) - dist(a, b);
|
||||||
|
if (d < minDist) {
|
||||||
|
minDist = d;
|
||||||
|
insertIdx = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
points.splice(insertIdx * 2, 0, newPt[0], newPt[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleMouseDown(ev: MouseEvent) {
|
||||||
|
ev.preventDefault();
|
||||||
|
|
||||||
|
svgRect = svgElement.getBoundingClientRect();
|
||||||
|
mouseDown = [ev.clientX, ev.clientY];
|
||||||
|
|
||||||
|
const target = ev.target as SVGCircleElement;
|
||||||
|
const index = target.dataset?.index;
|
||||||
|
|
||||||
|
if (index !== undefined) {
|
||||||
|
draggingIndex = parseInt(index);
|
||||||
|
downCirclePosition = [...getPt(points, draggingIndex)];
|
||||||
|
} else {
|
||||||
|
const pt = clientToSvg(ev);
|
||||||
|
if (mirror && pt[0] > 50) return;
|
||||||
|
insertBetween(pt);
|
||||||
|
const count = points.length / 2;
|
||||||
|
draggingIndex = count - 1;
|
||||||
|
downCirclePosition = [...pt];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleMouseUp(ev: MouseEvent) {
|
||||||
|
mouseDown = undefined;
|
||||||
|
if ((ev.target as HTMLElement).dataset?.index === undefined) {
|
||||||
|
draggingIndex = undefined;
|
||||||
|
}
|
||||||
|
console.log('MouseUp', ev.target);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleKeyDown(ev: KeyboardEvent) {
|
||||||
|
if (draggingIndex === undefined) return;
|
||||||
|
if (ev.key === 'Delete' || ev.key === 'Backspace') {
|
||||||
|
ev.preventDefault();
|
||||||
|
ev.stopImmediatePropagation();
|
||||||
|
points.splice(draggingIndex * 2, 2);
|
||||||
|
points = [...points];
|
||||||
|
draggingIndex = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<svelte:window onmousemove={handleMouseMove} onmouseup={handleMouseUp} onkeydown={handleKeyDown} />
|
||||||
|
|
||||||
|
<div class="wrapper" class:mirrored={mirror}>
|
||||||
|
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
||||||
|
<svg
|
||||||
|
width="100"
|
||||||
|
height="100"
|
||||||
|
viewBox="0 0 100 100"
|
||||||
|
bind:this={svgElement}
|
||||||
|
aria-label="Interactive 2D Shape Editor"
|
||||||
|
onmousedown={handleMouseDown}
|
||||||
|
>
|
||||||
|
<path d={calculatePath(points, mirror)} style:fill="var(--color-layer-3)" style:opacity={0.5} />
|
||||||
|
<path d={calculatePath(points, mirror)} fill="none" stroke="var(--color-layer-2)" />
|
||||||
|
{#if mirror}
|
||||||
|
<!-- eslint-disable-next-line @typescript-eslint/no-unused-vars -->
|
||||||
|
{#each Array(points.length / 2) as _, i (i)}
|
||||||
|
{@const x = mirrorPoints(points)[i * 2]}
|
||||||
|
{@const y = mirrorPoints(points)[i * 2 + 1]}
|
||||||
|
<circle
|
||||||
|
class="disabled"
|
||||||
|
cx={x}
|
||||||
|
cy={y}
|
||||||
|
r={2}
|
||||||
|
>
|
||||||
|
</circle>
|
||||||
|
{/each}
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!-- eslint-disable-next-line @typescript-eslint/no-unused-vars -->
|
||||||
|
{#each Array(points.length / 2) as _, i (i)}
|
||||||
|
{@const x = points[i * 2]}
|
||||||
|
{@const y = points[i * 2 + 1]}
|
||||||
|
<circle
|
||||||
|
class:active={draggingIndex === i}
|
||||||
|
class="interactive"
|
||||||
|
data-index={i}
|
||||||
|
cx={x}
|
||||||
|
cy={y}
|
||||||
|
r={2}
|
||||||
|
>
|
||||||
|
</circle>
|
||||||
|
{/each}
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.wrapper {
|
||||||
|
width: 100%;
|
||||||
|
aspect-ratio: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
svg {
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
overflow: visible;
|
||||||
|
}
|
||||||
|
|
||||||
|
circle {
|
||||||
|
fill: var(--color-layer-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
circle.active {
|
||||||
|
stroke: var(--color-layer-3);
|
||||||
|
stroke-width: 0.5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
circle.disabled {
|
||||||
|
pointer-events: none;
|
||||||
|
z-index: -1;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -1,6 +1,14 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import '$lib/app.css';
|
import '$lib/app.css';
|
||||||
import { Details, InputCheckbox, InputNumber, InputSelect, InputVec3, ShortCut } from '$lib';
|
import {
|
||||||
|
Details,
|
||||||
|
InputCheckbox,
|
||||||
|
InputNumber,
|
||||||
|
InputSelect,
|
||||||
|
InputShape,
|
||||||
|
InputVec3,
|
||||||
|
ShortCut
|
||||||
|
} from '$lib';
|
||||||
import Section from './Section.svelte';
|
import Section from './Section.svelte';
|
||||||
|
|
||||||
let intValue = $state(0);
|
let intValue = $state(0);
|
||||||
@@ -10,10 +18,12 @@
|
|||||||
const options = ['strawberry', 'raspberry', 'chickpeas'];
|
const options = ['strawberry', 'raspberry', 'chickpeas'];
|
||||||
let selectValue = $state(0);
|
let selectValue = $state(0);
|
||||||
const d = $derived(options[selectValue]);
|
const d = $derived(options[selectValue]);
|
||||||
|
|
||||||
let checked = $state(false);
|
let checked = $state(false);
|
||||||
|
let mirrorShape = $state(false);
|
||||||
let detailsOpen = $state(false);
|
let detailsOpen = $state(false);
|
||||||
|
|
||||||
|
let points = $state([]);
|
||||||
|
|
||||||
const themes = [
|
const themes = [
|
||||||
'dark',
|
'dark',
|
||||||
'light',
|
'light',
|
||||||
@@ -90,6 +100,17 @@
|
|||||||
<InputCheckbox bind:value={checked} />
|
<InputCheckbox bind:value={checked} />
|
||||||
</Section>
|
</Section>
|
||||||
|
|
||||||
|
<Section title="Shape">
|
||||||
|
{#snippet header()}
|
||||||
|
<label class="flex gap-2">
|
||||||
|
<InputCheckbox bind:value={mirrorShape} />
|
||||||
|
<p>mirror</p>
|
||||||
|
</label>
|
||||||
|
<p>{JSON.stringify(points)}</p>
|
||||||
|
{/snippet}
|
||||||
|
<InputShape bind:value={points} mirror={mirrorShape} />
|
||||||
|
</Section>
|
||||||
|
|
||||||
<Section title="Details" value={detailsOpen}>
|
<Section title="Details" value={detailsOpen}>
|
||||||
<Details title="More Information" bind:open={detailsOpen}>
|
<Details title="More Information" bind:open={detailsOpen}>
|
||||||
<p>Here is some more information that was previously hidden.</p>
|
<p>Here is some more information that was previously hidden.</p>
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { type Snippet } from 'svelte';
|
import { type Snippet } from 'svelte';
|
||||||
let { title, value, children, class: _class } = $props<{
|
let { title, value, header, children, class: _class } = $props<{
|
||||||
title?: string;
|
title?: string;
|
||||||
value?: unknown;
|
value?: unknown;
|
||||||
|
header?: Snippet;
|
||||||
children?: Snippet;
|
children?: Snippet;
|
||||||
class?: string;
|
class?: string;
|
||||||
}>();
|
}>();
|
||||||
@@ -11,7 +12,13 @@
|
|||||||
<section class="border-outline border-1/2 bg-layer-1 rounded border mb-4 p-4 flex flex-col gap-4 {_class}">
|
<section class="border-outline border-1/2 bg-layer-1 rounded border mb-4 p-4 flex flex-col gap-4 {_class}">
|
||||||
<h3 class="flex gap-2 font-bold">
|
<h3 class="flex gap-2 font-bold">
|
||||||
{title}
|
{title}
|
||||||
<p class="font-normal! opacity-50!">{value}</p>
|
<div class="flex gap-4 w-full font-normal opacity-50 max-w-[75%] whitespace-pre overflow-hidden text-clip">
|
||||||
|
{#if header}
|
||||||
|
{@render header()}
|
||||||
|
{:else}
|
||||||
|
{value}
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
</h3>
|
</h3>
|
||||||
<div>
|
<div>
|
||||||
{@render children()}
|
{@render children()}
|
||||||
|
|||||||
Reference in New Issue
Block a user