Skip to content

Commit

Permalink
feat: add format support to graph controls
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnsgn committed Nov 22, 2024
1 parent 1373b6b commit 506553a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
11 changes: 6 additions & 5 deletions examples/all-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default async function addAllControls(gui, ctx) {
(name, value) => ({
name,
value,
})
}),
),
checkboxValue: false,
message: "Message",
Expand Down Expand Up @@ -55,7 +55,7 @@ export default async function addAllControls(gui, ctx) {
mipmap: true,
min: ctx.Filter.LinearMipmapLinear,
aniso: 16,
})
}),
)
: images,
};
Expand All @@ -72,7 +72,7 @@ export default async function addAllControls(gui, ctx) {
"Radio list",
State,
"currentRadioListChoice",
State.radioListChoices
State.radioListChoices,
);

gui.addSeparator();
Expand Down Expand Up @@ -117,7 +117,7 @@ export default async function addAllControls(gui, ctx) {
State.textures.map((texture, value) => ({
texture,
value,
}))
})),
);
if (isPexGl) gui.addTextureCube("Cube", State.cubeTexture, { level: 2 }); // gui.addParam("Cube", State, "cubeTexture", { level: 2 });

Expand All @@ -129,8 +129,9 @@ export default async function addAllControls(gui, ctx) {
item.options.t += 0.01;
},
redraw(item) {
item.values.push(+Math.sin(item.options.t).toFixed(3));
item.values.push(Math.sin(item.options.t));
},
format: (value) => value?.toFixed(3) || "",
});
gui.addFPSMeeter();
gui.addHeader("Stats");
Expand Down
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,10 @@ class GUI {
const ctrl = new GUIControl({
type: "graph",
title,
options,
options: {
format: (value) => value,
...options,
},
activeArea: [
[0, 0],
[0, 0],
Expand Down Expand Up @@ -1021,8 +1024,9 @@ class GUI {
item.options.time.update(now);
},
redraw(item) {
item.values.push(Math.round(item.options.time.fps));
item.values.push(item.options.time.fps);
},
format: (value) => (Number.isFinite(value) ? Math.round(value) : ""),
});

return ctrl;
Expand Down
10 changes: 7 additions & 3 deletions renderers/CanvasRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,14 @@ class CanvasRenderer {
ctx.textAlign = "right";
const textX = x + width - padding;
if (item.max !== undefined) {
ctx.fillText(item.max, textX, y + padding * 2.5);
ctx.fillText(item.options.format(item.max), textX, y + padding * 2.5);
}
if (item.min !== undefined) {
ctx.fillText(item.min, textX, y + height - padding * 2.5);
ctx.fillText(
item.options.format(item.min),
textX,
y + height - padding * 2.5,
);
}
ctx.restore();

Expand All @@ -505,7 +509,7 @@ class CanvasRenderer {
ctx.stroke();

ctx.fillText(
`${item.title}: ${item.values[item.values.length - 1] ?? ""}`,
`${item.title}: ${item.options.format(item.values[item.values.length - 1])}`,
x + textPadding,
dy + textY,
);
Expand Down

0 comments on commit 506553a

Please sign in to comment.