@pinkaphant/ecctrl 0.0.0-stage → 2.0.4-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE +10 -0
  4. package/dist/Color-V9u0UyNg.cjs +81 -0
  5. package/dist/Color-V9u0UyNg.cjs.map +1 -0
  6. package/dist/Color-rCfz0rNn.js +28 -0
  7. package/dist/Color-rCfz0rNn.js.map +1 -0
  8. package/dist/CurveLUT-BfMhx3LX.js +88 -0
  9. package/dist/CurveLUT-BfMhx3LX.js.map +1 -0
  10. package/dist/CurveLUT-Ck18ZlkK.cjs +99 -0
  11. package/dist/CurveLUT-Ck18ZlkK.cjs.map +1 -0
  12. package/dist/EcctrlAnimationStateController-41Cm1Tt1.cjs +111 -0
  13. package/dist/EcctrlAnimationStateController-41Cm1Tt1.cjs.map +1 -0
  14. package/dist/EcctrlAnimationStateController-Bb2fARWF.js +94 -0
  15. package/dist/EcctrlAnimationStateController-Bb2fARWF.js.map +1 -0
  16. package/dist/Math-BkrN2TZK.js +49 -0
  17. package/dist/Math-BkrN2TZK.js.map +1 -0
  18. package/dist/Math-ByyUpUHB.cjs +68 -0
  19. package/dist/Math-ByyUpUHB.cjs.map +1 -0
  20. package/dist/all.cjs +52 -0
  21. package/dist/all.cjs.map +1 -0
  22. package/dist/all.d.ts +16 -0
  23. package/dist/all.js +32 -0
  24. package/dist/all.js.map +1 -0
  25. package/dist/animation.cjs +23 -0
  26. package/dist/animation.cjs.map +1 -0
  27. package/dist/animation.d.ts +8 -0
  28. package/dist/animation.js +20 -0
  29. package/dist/animation.js.map +1 -0
  30. package/dist/camera-B_UgIm14.js +71 -0
  31. package/dist/camera-B_UgIm14.js.map +1 -0
  32. package/dist/camera-CzMKt9dI.cjs +79 -0
  33. package/dist/camera-CzMKt9dI.cjs.map +1 -0
  34. package/dist/camera.cjs +10 -0
  35. package/dist/camera.d.ts +9 -0
  36. package/dist/camera.js +9 -0
  37. package/dist/character/Ecctrl.d.ts +105 -0
  38. package/dist/character/animation/EcctrlAnimationStateController.d.ts +17 -0
  39. package/dist/character/animation/index.d.ts +13 -0
  40. package/dist/character/animation/resolveAnimationState.d.ts +9 -0
  41. package/dist/character/animation/types.d.ts +19 -0
  42. package/dist/character/animation/useEcctrlAnimationStore.d.ts +21 -0
  43. package/dist/character/types.d.ts +27 -0
  44. package/dist/chunk-D6vf50IK.cjs +35 -0
  45. package/dist/curves/CurveLUT.d.ts +27 -0
  46. package/dist/curves.cjs +22 -0
  47. package/dist/curves.cjs.map +1 -0
  48. package/dist/curves.d.ts +9 -0
  49. package/dist/curves.js +20 -0
  50. package/dist/curves.js.map +1 -0
  51. package/dist/gravity/EcctrlCameraControls.d.ts +28 -0
  52. package/dist/gravity/useCustomGravity.d.ts +23 -0
  53. package/dist/gravity.cjs +21 -0
  54. package/dist/gravity.cjs.map +1 -0
  55. package/dist/gravity.d.ts +9 -0
  56. package/dist/gravity.js +20 -0
  57. package/dist/gravity.js.map +1 -0
  58. package/dist/index.cjs +14 -0
  59. package/dist/index.d.ts +12 -0
  60. package/dist/index.js +10 -0
  61. package/dist/input/Joystick.d.ts +18 -0
  62. package/dist/input/VirtualButton.d.ts +17 -0
  63. package/dist/input/stores/useButtonStore.d.ts +21 -0
  64. package/dist/input/stores/useJoystickStore.d.ts +26 -0
  65. package/dist/input-CrNBMRxm.js +315 -0
  66. package/dist/input-CrNBMRxm.js.map +1 -0
  67. package/dist/input-DBhf3WZe.cjs +340 -0
  68. package/dist/input-DBhf3WZe.cjs.map +1 -0
  69. package/dist/input.cjs +13 -0
  70. package/dist/input.d.ts +15 -0
  71. package/dist/input.js +9 -0
  72. package/dist/leva--ZV58vPr.cjs +771 -0
  73. package/dist/leva--ZV58vPr.cjs.map +1 -0
  74. package/dist/leva-BjLuypTs.js +764 -0
  75. package/dist/leva-BjLuypTs.js.map +1 -0
  76. package/dist/leva.cjs +10 -0
  77. package/dist/leva.d.ts +9 -0
  78. package/dist/leva.js +9 -0
  79. package/dist/plugins/leva/CurveEditorPlugin.d.ts +72 -0
  80. package/dist/shared/Math.d.ts +11 -0
  81. package/dist/shared/constants/Color.d.ts +16 -0
  82. package/dist/shared/types.d.ts +16 -0
  83. package/dist/src-Cz0JLHDZ.js +1164 -0
  84. package/dist/src-Cz0JLHDZ.js.map +1 -0
  85. package/dist/src-Qj3Pzy6X.cjs +1172 -0
  86. package/dist/src-Qj3Pzy6X.cjs.map +1 -0
  87. package/dist/time/TimeControl.d.ts +16 -0
  88. package/dist/time-CHRDN3n2.cjs +48 -0
  89. package/dist/time-CHRDN3n2.cjs.map +1 -0
  90. package/dist/time-D3dS_ohx.js +43 -0
  91. package/dist/time-D3dS_ohx.js.map +1 -0
  92. package/dist/time.cjs +10 -0
  93. package/dist/time.d.ts +9 -0
  94. package/dist/time.js +9 -0
  95. package/dist/useCustomGravity-B35U0d4V.cjs +45 -0
  96. package/dist/useCustomGravity-B35U0d4V.cjs.map +1 -0
  97. package/dist/useCustomGravity-elzlTR6Y.js +38 -0
  98. package/dist/useCustomGravity-elzlTR6Y.js.map +1 -0
  99. package/dist/utils.cjs +23 -0
  100. package/dist/utils.cjs.map +1 -0
  101. package/dist/utils.d.ts +8 -0
  102. package/dist/utils.js +20 -0
  103. package/dist/utils.js.map +1 -0
  104. package/dist/vehicle-B8WqQHee.js +2069 -0
  105. package/dist/vehicle-B8WqQHee.js.map +1 -0
  106. package/dist/vehicle-DYwDt5rB.cjs +2089 -0
  107. package/dist/vehicle-DYwDt5rB.cjs.map +1 -0
  108. package/dist/vehicle.cjs +12 -0
  109. package/dist/vehicle.d.ts +14 -0
  110. package/dist/vehicle.js +9 -0
  111. package/dist/vehicles/EcctrlVehicle.d.ts +109 -0
  112. package/dist/vehicles/components/ShapeCastWheel.d.ts +107 -0
  113. package/dist/vehicles/components/ThrustPropeller.d.ts +50 -0
  114. package/dist/vehicles/types.d.ts +39 -0
  115. package/package.json +155 -4
  116. package/readme.md +737 -0
  117. package/README.md +0 -3
@@ -0,0 +1,764 @@
1
+ /*!
2
+ * Ecctrl
3
+ * https://github.com/pmndrs/ecctrl
4
+ *
5
+ * SPDX-FileCopyrightText: 2023-2026 Erdong Chen
6
+ * SPDX-License-Identifier: MIT
7
+ */
8
+ import { n as evaluateCurveLUT, t as bakeCurveLUT } from "./CurveLUT-BfMhx3LX.js";
9
+ import { i as EC_GRAY, l as EC_RED, o as EC_MED_GRAY, r as EC_CORNFLOWER_BLUE, t as EC_AZURE } from "./Color-rCfz0rNn.js";
10
+ import React, { useCallback, useEffect, useMemo, useRef } from "react";
11
+ import { Components, createPlugin, useCanvas2d, useInputContext, useInputSetters } from "leva/plugin";
12
+ //#region src/plugins/leva/CurveEditorPlugin.tsx
13
+ /*!
14
+ * Ecctrl
15
+ * https://github.com/pmndrs/ecctrl
16
+ *
17
+ * SPDX-FileCopyrightText: 2023-2026 Erdong Chen
18
+ * SPDX-License-Identifier: MIT
19
+ */
20
+ var TOTAL_GRIDS = 12;
21
+ var INNER_GRIDS = 10;
22
+ var MARGIN_GRIDS = 1;
23
+ var DEFAULT_SAMPLES = 50;
24
+ var DEFAULT_POINTS = [{
25
+ x: 0,
26
+ y: 0,
27
+ r_out: 0
28
+ }, {
29
+ x: 1,
30
+ y: 1,
31
+ r_in: 0
32
+ }];
33
+ var POINT_RADIUS = 6;
34
+ var ACTIVE_POINT_RADIUS = 8;
35
+ var POINT_HIT_RADIUS = 14;
36
+ var TANGENT_LEN = .18;
37
+ var TANGENT_HANDLE_RADIUS = 5;
38
+ var ACTIVE_TANGENT_HANDLE_RADIUS = 7;
39
+ var TANGENT_HIT_RADIUS = 12;
40
+ var CURVE_X_EPSILON = 1e-4;
41
+ var { Label, Row, Number: LevaNumber } = Components;
42
+ var clamp = (value, min, max) => Math.max(min, Math.min(max, value));
43
+ var getCurvePointValue = (value) => typeof value === "object" && value !== null ? value.value : value;
44
+ var isSameTangentHandle = (a, b) => (a === null || a === void 0 ? void 0 : a.pointIndex) === (b === null || b === void 0 ? void 0 : b.pointIndex) && (a === null || a === void 0 ? void 0 : a.type) === (b === null || b === void 0 ? void 0 : b.type);
45
+ /**
46
+ * Generate single Leva number input with unique id
47
+ */
48
+ var CurveNumberInput = ({ id, label, value, settings, innerLabelTrim, onUpdate }) => {
49
+ const valueRef = useRef(value);
50
+ valueRef.current = value;
51
+ const number = useInputSetters({
52
+ type: "NUMBER",
53
+ value,
54
+ settings,
55
+ setValue: useCallback((nextValue) => {
56
+ const resolvedValue = typeof nextValue === "function" ? nextValue(valueRef.current) : nextValue;
57
+ onUpdate(clamp(globalThis.Number.isFinite(resolvedValue) ? resolvedValue : valueRef.current, settings.min, settings.max));
58
+ }, [
59
+ onUpdate,
60
+ settings.max,
61
+ settings.min
62
+ ])
63
+ });
64
+ return /* @__PURE__ */ React.createElement(LevaNumber, {
65
+ id,
66
+ label,
67
+ value,
68
+ displayValue: number.displayValue,
69
+ onUpdate: number.onUpdate,
70
+ onChange: number.onChange,
71
+ settings,
72
+ innerLabelTrim
73
+ });
74
+ };
75
+ /**
76
+ * Generate Leva number inputs row without sharing parent id
77
+ */
78
+ var CurveVectorInput = ({ id, value, settings, innerLabelTrim, onUpdate }) => /* @__PURE__ */ React.createElement("div", { style: {
79
+ display: "grid",
80
+ gridAutoFlow: "column dense",
81
+ alignItems: "center",
82
+ columnGap: 7
83
+ } }, Object.keys(value).map((key) => /* @__PURE__ */ React.createElement(CurveNumberInput, {
84
+ key,
85
+ id: `${id}.${key}`,
86
+ label: key,
87
+ value: value[key],
88
+ settings: settings[key],
89
+ innerLabelTrim,
90
+ onUpdate: (nextValue) => onUpdate({ [key]: nextValue })
91
+ })));
92
+ var CurveDisabledField = ({ label }) => /* @__PURE__ */ React.createElement("div", { style: {
93
+ alignItems: "center",
94
+ background: "#373c4b",
95
+ borderRadius: 3,
96
+ boxSizing: "border-box",
97
+ color: "inherit",
98
+ display: "grid",
99
+ fontSize: "0.8em",
100
+ gridTemplateColumns: "auto 1fr",
101
+ height: 24,
102
+ minWidth: 0,
103
+ overflow: "hidden",
104
+ padding: "0 6px",
105
+ pointerEvents: "none",
106
+ textTransform: "uppercase",
107
+ width: "100%"
108
+ } }, /* @__PURE__ */ React.createElement("span", { style: { opacity: .3 } }, label), /* @__PURE__ */ React.createElement("span", { style: {
109
+ opacity: .25,
110
+ textAlign: "right"
111
+ } }, "--"));
112
+ var CurvePairInput = ({ left, right }) => /* @__PURE__ */ React.createElement("div", { style: {
113
+ display: "grid",
114
+ gridTemplateColumns: "minmax(0, 1fr) minmax(0, 1fr)",
115
+ columnGap: 7,
116
+ width: "100%"
117
+ } }, /* @__PURE__ */ React.createElement("div", { style: { minWidth: 0 } }, left), /* @__PURE__ */ React.createElement("div", { style: { minWidth: 0 } }, right));
118
+ var CurveEditorCanvasComponent = () => {
119
+ const { id, label, value, settings, onUpdate } = useInputContext();
120
+ const { points, samples } = value;
121
+ const hoveredPointIndex = useRef(null);
122
+ const draggingPointIndex = useRef(null);
123
+ const hoveredTangentHandle = useRef(null);
124
+ const draggingTangentHandle = useRef(null);
125
+ const curveLUT = useMemo(() => bakeCurveLUT(points, samples), [points, samples]);
126
+ /**
127
+ * Generate centered square region for graph
128
+ */
129
+ const createViewport = useCallback((canvas) => {
130
+ const rect = canvas.getBoundingClientRect();
131
+ const gridSizeX = rect.width / TOTAL_GRIDS;
132
+ const gridSizeY = rect.height / TOTAL_GRIDS;
133
+ const maxGraphWidth = INNER_GRIDS * gridSizeX;
134
+ const maxGraphHeight = INNER_GRIDS * gridSizeY;
135
+ const graphSize = Math.min(maxGraphWidth, maxGraphHeight);
136
+ return {
137
+ gridSizeX,
138
+ gridSizeY,
139
+ graphLeft: MARGIN_GRIDS * gridSizeX + (maxGraphWidth - graphSize) * .5,
140
+ graphTop: MARGIN_GRIDS * gridSizeY + (maxGraphHeight - graphSize) * .5,
141
+ graphWidth: graphSize,
142
+ graphHeight: graphSize
143
+ };
144
+ }, [
145
+ TOTAL_GRIDS,
146
+ INNER_GRIDS,
147
+ MARGIN_GRIDS
148
+ ]);
149
+ /**
150
+ * Generate overall hermite graph within canvas
151
+ */
152
+ const drawCurve = useCallback(() => {
153
+ const canvas = canvasRef.current;
154
+ if (!canvas) return;
155
+ const ctx = canvas.getContext("2d");
156
+ if (!ctx) return;
157
+ const rect = canvas.getBoundingClientRect();
158
+ const dpr = window.devicePixelRatio || 1;
159
+ const displayWidth = rect.width * dpr;
160
+ const displayHeight = rect.height * dpr;
161
+ if (canvas.width !== displayWidth || canvas.height !== displayHeight) {
162
+ canvas.width = displayWidth;
163
+ canvas.height = displayHeight;
164
+ }
165
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
166
+ ctx.clearRect(0, 0, rect.width, rect.height);
167
+ const viewport = createViewport(canvas);
168
+ const toX = (x) => viewport.graphLeft + x * viewport.graphWidth;
169
+ const toY = (y) => viewport.graphTop + (1 - y) * viewport.graphHeight;
170
+ /**
171
+ * Draw grid
172
+ */
173
+ ctx.strokeStyle = EC_MED_GRAY;
174
+ ctx.lineWidth = 1;
175
+ for (let i = 0; i <= TOTAL_GRIDS; i++) {
176
+ const x = i * viewport.gridSizeX;
177
+ const y = i * viewport.gridSizeY;
178
+ ctx.beginPath();
179
+ ctx.moveTo(x, 0);
180
+ ctx.lineTo(x, rect.height);
181
+ ctx.stroke();
182
+ ctx.beginPath();
183
+ ctx.moveTo(0, y);
184
+ ctx.lineTo(rect.width, y);
185
+ ctx.stroke();
186
+ }
187
+ ctx.strokeStyle = EC_GRAY;
188
+ ctx.lineWidth = 2;
189
+ ctx.strokeRect(viewport.graphLeft, viewport.graphTop, viewport.graphWidth, viewport.graphHeight);
190
+ /**
191
+ * Draw axis labels
192
+ */
193
+ ctx.fillStyle = EC_AZURE;
194
+ ctx.textAlign = "center";
195
+ [
196
+ 0,
197
+ .5,
198
+ 1
199
+ ].forEach((v) => {
200
+ const px = toX(v);
201
+ const py = viewport.graphTop + viewport.graphHeight + 14;
202
+ ctx.fillText(v.toString(), px, py);
203
+ });
204
+ ctx.textAlign = "right";
205
+ [
206
+ 0,
207
+ .5,
208
+ 1
209
+ ].forEach((v) => {
210
+ const px = viewport.graphLeft - 7;
211
+ const py = toY(v);
212
+ ctx.fillText(v.toString(), px, py);
213
+ });
214
+ /**
215
+ * Draw Hermite curve
216
+ */
217
+ ctx.strokeStyle = EC_AZURE;
218
+ ctx.lineWidth = 1.5;
219
+ ctx.beginPath();
220
+ for (let s = 0; s <= samples; s++) {
221
+ const x = s / samples;
222
+ const y = evaluateCurveLUT(x, curveLUT);
223
+ const px = toX(x);
224
+ const py = toY(y);
225
+ if (s === 0) ctx.moveTo(px, py);
226
+ else ctx.lineTo(px, py);
227
+ }
228
+ ctx.stroke();
229
+ /**
230
+ * Draw tangent lines
231
+ */
232
+ points.forEach((p, i) => {
233
+ const px = toX(p.x);
234
+ const py = toY(p.y);
235
+ if (p.r_out !== void 0) {
236
+ var _p$w_out, _p$w_out2;
237
+ const dx = Math.cos(p.r_out) * TANGENT_LEN * ((_p$w_out = p.w_out) !== null && _p$w_out !== void 0 ? _p$w_out : 1);
238
+ const dy = Math.sin(p.r_out) * TANGENT_LEN * ((_p$w_out2 = p.w_out) !== null && _p$w_out2 !== void 0 ? _p$w_out2 : 1);
239
+ const hx = toX(p.x + dx);
240
+ const hy = toY(p.y + dy);
241
+ const isActive = isSameTangentHandle(hoveredTangentHandle.current, {
242
+ pointIndex: i,
243
+ type: "out"
244
+ }) || isSameTangentHandle(draggingTangentHandle.current, {
245
+ pointIndex: i,
246
+ type: "out"
247
+ });
248
+ ctx.strokeStyle = isActive ? EC_AZURE : EC_CORNFLOWER_BLUE;
249
+ ctx.lineWidth = isActive ? 3 : 2;
250
+ ctx.beginPath();
251
+ ctx.moveTo(px, py);
252
+ ctx.lineTo(hx, hy);
253
+ ctx.stroke();
254
+ ctx.fillStyle = isActive ? EC_AZURE : EC_CORNFLOWER_BLUE;
255
+ ctx.beginPath();
256
+ ctx.arc(hx, hy, isActive ? ACTIVE_TANGENT_HANDLE_RADIUS : TANGENT_HANDLE_RADIUS, 0, Math.PI * 2);
257
+ ctx.fill();
258
+ }
259
+ if (p.r_in !== void 0) {
260
+ var _p$w_in, _p$w_in2;
261
+ const dx = Math.cos(p.r_in) * TANGENT_LEN * ((_p$w_in = p.w_in) !== null && _p$w_in !== void 0 ? _p$w_in : 1);
262
+ const dy = Math.sin(p.r_in) * TANGENT_LEN * ((_p$w_in2 = p.w_in) !== null && _p$w_in2 !== void 0 ? _p$w_in2 : 1);
263
+ const hx = toX(p.x - dx);
264
+ const hy = toY(p.y - dy);
265
+ const isActive = isSameTangentHandle(hoveredTangentHandle.current, {
266
+ pointIndex: i,
267
+ type: "in"
268
+ }) || isSameTangentHandle(draggingTangentHandle.current, {
269
+ pointIndex: i,
270
+ type: "in"
271
+ });
272
+ ctx.strokeStyle = isActive ? EC_AZURE : EC_CORNFLOWER_BLUE;
273
+ ctx.lineWidth = isActive ? 3 : 2;
274
+ ctx.beginPath();
275
+ ctx.moveTo(hx, hy);
276
+ ctx.lineTo(px, py);
277
+ ctx.stroke();
278
+ ctx.fillStyle = isActive ? EC_AZURE : EC_CORNFLOWER_BLUE;
279
+ ctx.beginPath();
280
+ ctx.arc(hx, hy, isActive ? ACTIVE_TANGENT_HANDLE_RADIUS : TANGENT_HANDLE_RADIUS, 0, Math.PI * 2);
281
+ ctx.fill();
282
+ }
283
+ });
284
+ /**
285
+ * Draw points
286
+ */
287
+ points.forEach((p, i) => {
288
+ const px = toX(p.x);
289
+ const py = toY(p.y);
290
+ const isActive = hoveredPointIndex.current === i || draggingPointIndex.current === i;
291
+ ctx.fillStyle = isActive ? EC_AZURE : EC_RED;
292
+ ctx.beginPath();
293
+ ctx.arc(px, py, isActive ? ACTIVE_POINT_RADIUS : POINT_RADIUS, 0, Math.PI * 2);
294
+ ctx.fill();
295
+ if (isActive) {
296
+ ctx.strokeStyle = EC_RED;
297
+ ctx.lineWidth = 2;
298
+ ctx.stroke();
299
+ }
300
+ ctx.fillStyle = isActive ? EC_AZURE : EC_RED;
301
+ ctx.textAlign = "left";
302
+ ctx.textBaseline = "bottom";
303
+ ctx.fillText(`P${i}`, px + 8, py - 6);
304
+ });
305
+ }, [
306
+ points,
307
+ samples,
308
+ curveLUT
309
+ ]);
310
+ const [canvasRef] = useCanvas2d(() => {
311
+ drawCurve();
312
+ });
313
+ useEffect(() => {
314
+ drawCurve();
315
+ }, [drawCurve]);
316
+ const getPointerInfo = useCallback((event) => {
317
+ const canvas = canvasRef.current;
318
+ if (!canvas) return null;
319
+ const rect = canvas.getBoundingClientRect();
320
+ const viewport = createViewport(canvas);
321
+ const canvasX = event.clientX - rect.left;
322
+ const canvasY = event.clientY - rect.top;
323
+ return {
324
+ x: clamp((canvasX - viewport.graphLeft) / viewport.graphWidth, 0, 1),
325
+ y: clamp(1 - (canvasY - viewport.graphTop) / viewport.graphHeight, 0, 1),
326
+ canvasX,
327
+ canvasY,
328
+ viewport
329
+ };
330
+ }, [canvasRef, createViewport]);
331
+ const findPointIndex = useCallback((event) => {
332
+ const pointerInfo = getPointerInfo(event);
333
+ if (!pointerInfo) return null;
334
+ let closestIndex = null;
335
+ let closestDistanceSq = POINT_HIT_RADIUS * POINT_HIT_RADIUS;
336
+ points.forEach((point, index) => {
337
+ const px = pointerInfo.viewport.graphLeft + point.x * pointerInfo.viewport.graphWidth;
338
+ const py = pointerInfo.viewport.graphTop + (1 - point.y) * pointerInfo.viewport.graphHeight;
339
+ const dx = pointerInfo.canvasX - px;
340
+ const dy = pointerInfo.canvasY - py;
341
+ const distanceSq = dx * dx + dy * dy;
342
+ if (distanceSq <= closestDistanceSq) {
343
+ closestDistanceSq = distanceSq;
344
+ closestIndex = index;
345
+ }
346
+ });
347
+ return closestIndex;
348
+ }, [getPointerInfo, points]);
349
+ const findTangentHandle = useCallback((event) => {
350
+ const pointerInfo = getPointerInfo(event);
351
+ if (!pointerInfo) return null;
352
+ let closestHandle = null;
353
+ let closestDistanceSq = TANGENT_HIT_RADIUS * TANGENT_HIT_RADIUS;
354
+ points.forEach((point, pointIndex) => {
355
+ if (point.r_out !== void 0) {
356
+ var _point$w_out, _point$w_out2;
357
+ const handleX = point.x + Math.cos(point.r_out) * TANGENT_LEN * ((_point$w_out = point.w_out) !== null && _point$w_out !== void 0 ? _point$w_out : 1);
358
+ const handleY = point.y + Math.sin(point.r_out) * TANGENT_LEN * ((_point$w_out2 = point.w_out) !== null && _point$w_out2 !== void 0 ? _point$w_out2 : 1);
359
+ const px = pointerInfo.viewport.graphLeft + handleX * pointerInfo.viewport.graphWidth;
360
+ const py = pointerInfo.viewport.graphTop + (1 - handleY) * pointerInfo.viewport.graphHeight;
361
+ const dx = pointerInfo.canvasX - px;
362
+ const dy = pointerInfo.canvasY - py;
363
+ const distanceSq = dx * dx + dy * dy;
364
+ if (distanceSq <= closestDistanceSq) {
365
+ closestDistanceSq = distanceSq;
366
+ closestHandle = {
367
+ pointIndex,
368
+ type: "out"
369
+ };
370
+ }
371
+ }
372
+ if (point.r_in !== void 0) {
373
+ var _point$w_in, _point$w_in2;
374
+ const handleX = point.x - Math.cos(point.r_in) * TANGENT_LEN * ((_point$w_in = point.w_in) !== null && _point$w_in !== void 0 ? _point$w_in : 1);
375
+ const handleY = point.y - Math.sin(point.r_in) * TANGENT_LEN * ((_point$w_in2 = point.w_in) !== null && _point$w_in2 !== void 0 ? _point$w_in2 : 1);
376
+ const px = pointerInfo.viewport.graphLeft + handleX * pointerInfo.viewport.graphWidth;
377
+ const py = pointerInfo.viewport.graphTop + (1 - handleY) * pointerInfo.viewport.graphHeight;
378
+ const dx = pointerInfo.canvasX - px;
379
+ const dy = pointerInfo.canvasY - py;
380
+ const distanceSq = dx * dx + dy * dy;
381
+ if (distanceSq <= closestDistanceSq) {
382
+ closestDistanceSq = distanceSq;
383
+ closestHandle = {
384
+ pointIndex,
385
+ type: "in"
386
+ };
387
+ }
388
+ }
389
+ });
390
+ return closestHandle;
391
+ }, [getPointerInfo, points]);
392
+ const updateDraggedPoint = useCallback((event) => {
393
+ const pointIndex = draggingPointIndex.current;
394
+ if (pointIndex === null) return;
395
+ const pointerInfo = getPointerInfo(event);
396
+ if (!pointerInfo) return;
397
+ const pointSettings = settings.points[pointIndex];
398
+ const minX = pointIndex > 0 ? points[pointIndex - 1].x + CURVE_X_EPSILON : pointSettings.x.min;
399
+ const maxX = pointIndex < points.length - 1 ? points[pointIndex + 1].x - CURVE_X_EPSILON : pointSettings.x.max;
400
+ const nextX = clamp(pointerInfo.x, minX, maxX);
401
+ const nextY = clamp(pointerInfo.y, pointSettings.y.min, pointSettings.y.max);
402
+ const nextPoints = [...points];
403
+ nextPoints[pointIndex] = {
404
+ ...nextPoints[pointIndex],
405
+ x: nextX,
406
+ y: nextY
407
+ };
408
+ onUpdate({
409
+ ...value,
410
+ points: nextPoints
411
+ });
412
+ }, [
413
+ getPointerInfo,
414
+ onUpdate,
415
+ points,
416
+ settings.points,
417
+ value
418
+ ]);
419
+ const updateDraggedTangentHandle = useCallback((event) => {
420
+ const tangentHandle = draggingTangentHandle.current;
421
+ if (!tangentHandle) return;
422
+ const pointerInfo = getPointerInfo(event);
423
+ if (!pointerInfo) return;
424
+ const point = points[tangentHandle.pointIndex];
425
+ const pointSettings = settings.points[tangentHandle.pointIndex];
426
+ const dx = tangentHandle.type === "out" ? pointerInfo.x - point.x : point.x - pointerInfo.x;
427
+ const dy = tangentHandle.type === "out" ? pointerInfo.y - point.y : point.y - pointerInfo.y;
428
+ const angle = Math.atan2(dy, dx);
429
+ const weight = Math.sqrt(dx * dx + dy * dy) / TANGENT_LEN;
430
+ const nextPoints = [...points];
431
+ if (tangentHandle.type === "out") nextPoints[tangentHandle.pointIndex] = {
432
+ ...nextPoints[tangentHandle.pointIndex],
433
+ r_out: clamp(angle, pointSettings.r_out.min, pointSettings.r_out.max),
434
+ w_out: clamp(weight, pointSettings.w_out.min, pointSettings.w_out.max)
435
+ };
436
+ else nextPoints[tangentHandle.pointIndex] = {
437
+ ...nextPoints[tangentHandle.pointIndex],
438
+ r_in: clamp(angle, pointSettings.r_in.min, pointSettings.r_in.max),
439
+ w_in: clamp(weight, pointSettings.w_in.min, pointSettings.w_in.max)
440
+ };
441
+ onUpdate({
442
+ ...value,
443
+ points: nextPoints
444
+ });
445
+ }, [
446
+ getPointerInfo,
447
+ onUpdate,
448
+ points,
449
+ settings.points,
450
+ value
451
+ ]);
452
+ const handlePointerDown = useCallback((event) => {
453
+ const tangentHandle = findTangentHandle(event);
454
+ if (tangentHandle) {
455
+ hoveredPointIndex.current = null;
456
+ hoveredTangentHandle.current = tangentHandle;
457
+ draggingTangentHandle.current = tangentHandle;
458
+ event.currentTarget.style.cursor = "grabbing";
459
+ event.currentTarget.setPointerCapture(event.pointerId);
460
+ drawCurve();
461
+ event.preventDefault();
462
+ return;
463
+ }
464
+ const pointIndex = findPointIndex(event);
465
+ if (pointIndex === null) return;
466
+ hoveredPointIndex.current = pointIndex;
467
+ hoveredTangentHandle.current = null;
468
+ draggingPointIndex.current = pointIndex;
469
+ event.currentTarget.style.cursor = "grabbing";
470
+ event.currentTarget.setPointerCapture(event.pointerId);
471
+ drawCurve();
472
+ event.preventDefault();
473
+ }, [
474
+ drawCurve,
475
+ findPointIndex,
476
+ findTangentHandle
477
+ ]);
478
+ const handlePointerMove = useCallback((event) => {
479
+ if (draggingTangentHandle.current !== null) {
480
+ event.currentTarget.style.cursor = "grabbing";
481
+ updateDraggedTangentHandle(event);
482
+ event.preventDefault();
483
+ return;
484
+ }
485
+ if (draggingPointIndex.current !== null) {
486
+ event.currentTarget.style.cursor = "grabbing";
487
+ updateDraggedPoint(event);
488
+ event.preventDefault();
489
+ return;
490
+ }
491
+ const tangentHandle = findTangentHandle(event);
492
+ if (tangentHandle) {
493
+ event.currentTarget.style.cursor = "grab";
494
+ if (!isSameTangentHandle(hoveredTangentHandle.current, tangentHandle) || hoveredPointIndex.current !== null) {
495
+ hoveredTangentHandle.current = tangentHandle;
496
+ hoveredPointIndex.current = null;
497
+ drawCurve();
498
+ }
499
+ return;
500
+ }
501
+ const pointIndex = findPointIndex(event);
502
+ event.currentTarget.style.cursor = pointIndex === null ? "default" : "grab";
503
+ if (hoveredPointIndex.current !== pointIndex || hoveredTangentHandle.current !== null) {
504
+ hoveredPointIndex.current = pointIndex;
505
+ hoveredTangentHandle.current = null;
506
+ drawCurve();
507
+ }
508
+ }, [
509
+ drawCurve,
510
+ findPointIndex,
511
+ findTangentHandle,
512
+ updateDraggedPoint,
513
+ updateDraggedTangentHandle
514
+ ]);
515
+ const handlePointerUp = useCallback((event) => {
516
+ if (draggingPointIndex.current === null && draggingTangentHandle.current === null) return;
517
+ draggingPointIndex.current = null;
518
+ draggingTangentHandle.current = null;
519
+ if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId);
520
+ const tangentHandle = findTangentHandle(event);
521
+ const pointIndex = tangentHandle ? null : findPointIndex(event);
522
+ hoveredTangentHandle.current = tangentHandle;
523
+ hoveredPointIndex.current = pointIndex;
524
+ event.currentTarget.style.cursor = tangentHandle || pointIndex !== null ? "grab" : "default";
525
+ drawCurve();
526
+ event.preventDefault();
527
+ }, [
528
+ drawCurve,
529
+ findPointIndex,
530
+ findTangentHandle
531
+ ]);
532
+ const handlePointerLeave = useCallback((event) => {
533
+ if (draggingPointIndex.current !== null || draggingTangentHandle.current !== null) return;
534
+ if (hoveredPointIndex.current === null && hoveredTangentHandle.current === null) return;
535
+ hoveredPointIndex.current = null;
536
+ hoveredTangentHandle.current = null;
537
+ event.currentTarget.style.cursor = "default";
538
+ drawCurve();
539
+ }, [drawCurve]);
540
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Row, null, /* @__PURE__ */ React.createElement(Label, null, label), /* @__PURE__ */ React.createElement("canvas", {
541
+ ref: canvasRef,
542
+ style: {
543
+ width: "100%",
544
+ height: "250px",
545
+ touchAction: "none"
546
+ },
547
+ onPointerDown: handlePointerDown,
548
+ onPointerMove: handlePointerMove,
549
+ onPointerUp: handlePointerUp,
550
+ onPointerCancel: handlePointerUp,
551
+ onPointerLeave: handlePointerLeave
552
+ })), /* @__PURE__ */ React.createElement("br", null), /* @__PURE__ */ React.createElement(Row, { input: true }, /* @__PURE__ */ React.createElement(Label, null, "Smaples"), /* @__PURE__ */ React.createElement(CurveVectorInput, {
553
+ id,
554
+ value: { samples },
555
+ settings: { samples: {
556
+ ...settings.samples,
557
+ pad: 0
558
+ } },
559
+ innerLabelTrim: 10,
560
+ onUpdate: (v) => onUpdate({
561
+ ...value,
562
+ samples: v.samples
563
+ })
564
+ })), /* @__PURE__ */ React.createElement("br", null), points.map((p, i) => /* @__PURE__ */ React.createElement(React.Fragment, { key: i }, (() => {
565
+ var _p$r_in, _p$r_out, _p$w_in3, _p$w_out3;
566
+ const canUseIn = i > 0;
567
+ const canUseOut = i < points.length - 1;
568
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Row, { input: true }, /* @__PURE__ */ React.createElement(Label, null, "P", i, " pos"), /* @__PURE__ */ React.createElement(CurveVectorInput, {
569
+ id: `${id}.p${i}.pos`,
570
+ value: {
571
+ x: p.x,
572
+ y: p.y
573
+ },
574
+ settings: {
575
+ x: {
576
+ ...settings.points[i].x,
577
+ pad: 2
578
+ },
579
+ y: {
580
+ ...settings.points[i].y,
581
+ pad: 2
582
+ }
583
+ },
584
+ innerLabelTrim: 4,
585
+ onUpdate: (v) => {
586
+ const newPoints = [...points];
587
+ newPoints[i] = {
588
+ ...newPoints[i],
589
+ ...v
590
+ };
591
+ onUpdate({
592
+ ...value,
593
+ points: newPoints
594
+ });
595
+ }
596
+ })), /* @__PURE__ */ React.createElement(Row, { input: true }, /* @__PURE__ */ React.createElement(Label, null, "P", i, " angle"), /* @__PURE__ */ React.createElement(CurvePairInput, {
597
+ left: canUseIn ? /* @__PURE__ */ React.createElement(CurveNumberInput, {
598
+ id: `${id}.p${i}.r_in`,
599
+ label: "r_in",
600
+ value: (_p$r_in = p.r_in) !== null && _p$r_in !== void 0 ? _p$r_in : 0,
601
+ settings: {
602
+ ...settings.points[i].r_in,
603
+ pad: 2
604
+ },
605
+ innerLabelTrim: 4,
606
+ onUpdate: (r_in) => {
607
+ const newPoints = [...points];
608
+ const updatedPoint = {
609
+ ...newPoints[i],
610
+ r_in
611
+ };
612
+ if (p.r_in === void 0 && r_in === 0) delete updatedPoint.r_in;
613
+ newPoints[i] = updatedPoint;
614
+ onUpdate({
615
+ ...value,
616
+ points: newPoints
617
+ });
618
+ }
619
+ }) : /* @__PURE__ */ React.createElement(CurveDisabledField, { label: "R_IN" }),
620
+ right: canUseOut ? /* @__PURE__ */ React.createElement(CurveNumberInput, {
621
+ id: `${id}.p${i}.r_out`,
622
+ label: "r_out",
623
+ value: (_p$r_out = p.r_out) !== null && _p$r_out !== void 0 ? _p$r_out : 0,
624
+ settings: {
625
+ ...settings.points[i].r_out,
626
+ pad: 2
627
+ },
628
+ innerLabelTrim: 4,
629
+ onUpdate: (r_out) => {
630
+ const newPoints = [...points];
631
+ const updatedPoint = {
632
+ ...newPoints[i],
633
+ r_out
634
+ };
635
+ if (p.r_out === void 0 && r_out === 0) delete updatedPoint.r_out;
636
+ newPoints[i] = updatedPoint;
637
+ onUpdate({
638
+ ...value,
639
+ points: newPoints
640
+ });
641
+ }
642
+ }) : /* @__PURE__ */ React.createElement(CurveDisabledField, { label: "R_OUT" })
643
+ })), /* @__PURE__ */ React.createElement(Row, { input: true }, /* @__PURE__ */ React.createElement(Label, null, "P", i, " weight"), /* @__PURE__ */ React.createElement(CurvePairInput, {
644
+ left: canUseIn ? /* @__PURE__ */ React.createElement(CurveNumberInput, {
645
+ id: `${id}.p${i}.w_in`,
646
+ label: "w_in",
647
+ value: (_p$w_in3 = p.w_in) !== null && _p$w_in3 !== void 0 ? _p$w_in3 : 1,
648
+ settings: {
649
+ ...settings.points[i].w_in,
650
+ pad: 2
651
+ },
652
+ innerLabelTrim: 4,
653
+ onUpdate: (w_in) => {
654
+ const newPoints = [...points];
655
+ const updatedPoint = {
656
+ ...newPoints[i],
657
+ w_in
658
+ };
659
+ if (p.w_in === void 0 && w_in === 1) delete updatedPoint.w_in;
660
+ newPoints[i] = updatedPoint;
661
+ onUpdate({
662
+ ...value,
663
+ points: newPoints
664
+ });
665
+ }
666
+ }) : /* @__PURE__ */ React.createElement(CurveDisabledField, { label: "W_IN" }),
667
+ right: canUseOut ? /* @__PURE__ */ React.createElement(CurveNumberInput, {
668
+ id: `${id}.p${i}.w_out`,
669
+ label: "w_out",
670
+ value: (_p$w_out3 = p.w_out) !== null && _p$w_out3 !== void 0 ? _p$w_out3 : 1,
671
+ settings: {
672
+ ...settings.points[i].w_out,
673
+ pad: 2
674
+ },
675
+ innerLabelTrim: 4,
676
+ onUpdate: (w_out) => {
677
+ const newPoints = [...points];
678
+ const updatedPoint = {
679
+ ...newPoints[i],
680
+ w_out
681
+ };
682
+ if (p.w_out === void 0 && w_out === 1) delete updatedPoint.w_out;
683
+ newPoints[i] = updatedPoint;
684
+ onUpdate({
685
+ ...value,
686
+ points: newPoints
687
+ });
688
+ }
689
+ }) : /* @__PURE__ */ React.createElement(CurveDisabledField, { label: "W_OUT" })
690
+ })));
691
+ })(), /* @__PURE__ */ React.createElement("br", null))));
692
+ };
693
+ var getSetting = (field, defMin, defMax, defStep) => {
694
+ var _field$min, _field$max, _field$step, _field$pad;
695
+ const isObj = typeof field === "object" && field !== null;
696
+ return {
697
+ min: isObj ? (_field$min = field.min) !== null && _field$min !== void 0 ? _field$min : defMin : defMin,
698
+ max: isObj ? (_field$max = field.max) !== null && _field$max !== void 0 ? _field$max : defMax : defMax,
699
+ step: isObj ? (_field$step = field.step) !== null && _field$step !== void 0 ? _field$step : defStep : defStep,
700
+ pad: isObj ? (_field$pad = field.pad) !== null && _field$pad !== void 0 ? _field$pad : 2 : 2
701
+ };
702
+ };
703
+ var CurveEditorPlugin = createPlugin({
704
+ component: CurveEditorCanvasComponent,
705
+ normalize: (input) => {
706
+ var _input$points, _input$samples;
707
+ const rawPoints = (input === null || input === void 0 || (_input$points = input.points) === null || _input$points === void 0 ? void 0 : _input$points.length) ? input.points : DEFAULT_POINTS;
708
+ const rawSamples = (_input$samples = input === null || input === void 0 ? void 0 : input.samples) !== null && _input$samples !== void 0 ? _input$samples : DEFAULT_SAMPLES;
709
+ return {
710
+ value: {
711
+ samples: typeof rawSamples === "object" ? rawSamples.value : rawSamples,
712
+ points: rawPoints.map((p, i) => ({
713
+ x: getCurvePointValue(p.x),
714
+ y: getCurvePointValue(p.y),
715
+ ...i > 0 && p.r_in !== void 0 && { r_in: getCurvePointValue(p.r_in) },
716
+ ...i < rawPoints.length - 1 && p.r_out !== void 0 && { r_out: getCurvePointValue(p.r_out) },
717
+ ...i > 0 && p.w_in !== void 0 && { w_in: getCurvePointValue(p.w_in) },
718
+ ...i < rawPoints.length - 1 && p.w_out !== void 0 && { w_out: getCurvePointValue(p.w_out) }
719
+ }))
720
+ },
721
+ settings: {
722
+ samples: getSetting(rawSamples, 2, 500, 1),
723
+ points: rawPoints.map((p) => ({
724
+ x: getSetting(p.x, 0, 1, .01),
725
+ y: getSetting(p.y, 0, 1, .01),
726
+ r_in: getSetting(p.r_in, -Math.PI / 2, Math.PI / 2, .01),
727
+ r_out: getSetting(p.r_out, -Math.PI / 2, Math.PI / 2, .01),
728
+ w_in: getSetting(p.w_in, 0, 3, .01),
729
+ w_out: getSetting(p.w_out, 0, 3, .01)
730
+ }))
731
+ }
732
+ };
733
+ },
734
+ sanitize: (value, settings) => {
735
+ return {
736
+ samples: Math.max(settings.samples.min, Math.min(settings.samples.max, value.samples)),
737
+ points: value.points.map((p, i) => {
738
+ const s = settings.points[i] || settings.points[settings.points.length - 1];
739
+ const sanitizedPoint = {
740
+ x: Math.max(s.x.min, Math.min(s.x.max, p.x)),
741
+ y: Math.max(s.y.min, Math.min(s.y.max, p.y))
742
+ };
743
+ if (i > 0 && p.r_in !== void 0) sanitizedPoint.r_in = Math.max(s.r_in.min, Math.min(s.r_in.max, p.r_in));
744
+ if (i < value.points.length - 1 && p.r_out !== void 0) sanitizedPoint.r_out = Math.max(s.r_out.min, Math.min(s.r_out.max, p.r_out));
745
+ if (i > 0 && p.w_in !== void 0) sanitizedPoint.w_in = Math.max(s.w_in.min, Math.min(s.w_in.max, p.w_in));
746
+ if (i < value.points.length - 1 && p.w_out !== void 0) sanitizedPoint.w_out = Math.max(s.w_out.min, Math.min(s.w_out.max, p.w_out));
747
+ return sanitizedPoint;
748
+ })
749
+ };
750
+ }
751
+ });
752
+ //#endregion
753
+ //#region src/leva.ts
754
+ /*!
755
+ * Ecctrl
756
+ * https://github.com/pmndrs/ecctrl
757
+ *
758
+ * SPDX-FileCopyrightText: 2023-2026 Erdong Chen
759
+ * SPDX-License-Identifier: MIT
760
+ */
761
+ //#endregion
762
+ export { CurveEditorPlugin as t };
763
+
764
+ //# sourceMappingURL=leva-BjLuypTs.js.map