Skip to content

Commit 953f2fa

Browse files
feat(imgui): update ramp component & RampOpts
1 parent ee866b1 commit 953f2fa

File tree

1 file changed

+28
-7
lines changed
  • packages/imgui/src/components

1 file changed

+28
-7
lines changed

packages/imgui/src/components/ramp.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,29 @@ import { tooltipRaw } from "./tooltip.js";
2121

2222
export interface RampOpts extends Omit<ComponentOpts, "label"> {
2323
ramp: Ramp<number>;
24+
/**
25+
* User defined interpolation mode. Only used to compute internal hash of
26+
* ramp curve geometry, i.e. if the {@link RampOpts.ramp} interpolation
27+
* method changes, so should this mode value.
28+
*/
2429
mode?: number;
30+
/**
31+
* Normalized snap/selection tolerance for ramp control point positions
32+
*
33+
* @defaultValue 0.05
34+
*/
35+
eps?: number;
2536
}
2637

27-
export const ramp = ({ gui, layout, id, ramp, mode = 0, info }: RampOpts) => {
38+
export const ramp = ({
39+
gui,
40+
layout,
41+
id,
42+
ramp,
43+
mode = 0,
44+
info,
45+
eps = 0.05,
46+
}: RampOpts) => {
2847
const { x, y, w, h } = layoutBox(layout);
2948
const maxX = x + w;
3049
const maxY = y + h;
@@ -42,7 +61,7 @@ export const ramp = ({ gui, layout, id, ramp, mode = 0, info }: RampOpts) => {
4261
const focused = gui.requestFocus(id);
4362
if (hover) {
4463
sel = clamp01_2(null, fit2([], gui.mouse, pos, maxPos, ZERO2, ONE2));
45-
selID = ramp.closestIndex(sel[0], 0.05);
64+
selID = ramp.closestIndex(sel[0], eps);
4665
if (gui.isMouseDown()) {
4766
gui.activeID = id;
4867
if (selID >= 0) {
@@ -51,12 +70,12 @@ export const ramp = ({ gui, layout, id, ramp, mode = 0, info }: RampOpts) => {
5170
ramp.setStopAt(
5271
roundTo(sel[0], 1e-3),
5372
roundTo(sel[1], 1e-3),
54-
0.05
73+
eps
5574
);
5675
}
5776
res = ramp;
5877
}
59-
if (focused && selID >= 0 && handleRampKeys(gui, ramp, selID)) {
78+
if (focused && selID >= 0 && __handleRampKeys(gui, ramp, selID)) {
6079
res = ramp;
6180
}
6281
info && gui.draw && tooltipRaw(gui, info);
@@ -73,7 +92,7 @@ export const ramp = ({ gui, layout, id, ramp, mode = 0, info }: RampOpts) => {
7392
[
7493
[x, maxY],
7594
mix2([], pos, maxPos, [0, stops[0][1]]),
76-
...rampVertices(ramp, pos, maxPos),
95+
...__rampVertices(ramp, pos, maxPos),
7796
mix2([], pos, maxPos, [1, stops[stops.length - 1][1]]),
7897
[maxX, maxY],
7998
],
@@ -106,14 +125,16 @@ export const ramp = ({ gui, layout, id, ramp, mode = 0, info }: RampOpts) => {
106125
return res;
107126
};
108127

109-
const rampVertices = (
128+
/** @internal */
129+
const __rampVertices = (
110130
ramp: Ramp<number>,
111131
pos: Vec,
112132
maxPos: Vec,
113133
numSamples = 100
114134
) => map((p) => mix2(p, pos, maxPos, p), ramp.samples(numSamples));
115135

116-
const handleRampKeys = (gui: IMGUI, ramp: Ramp<number>, selID: number) => {
136+
/** @internal */
137+
const __handleRampKeys = (gui: IMGUI, ramp: Ramp<number>, selID: number) => {
117138
switch (gui.key) {
118139
case Key.TAB:
119140
gui.switchFocus();

0 commit comments

Comments
 (0)