@pinkaphant/ecctrl 0.0.0-stage → 2.0.3

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