forked from Blendman974/faceclaw
Use animated menus in a bunch more places
This commit is contained in:
1 parent
123c1142d6
commit
71c1971992
37 files changed
+1457
-1005
No files matched your search
@@ -2,8 +2,8 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { drawSelectionHighlight } from "../../ui/menu";
|
||||
import { tightRowHeight } from "../../ui/metrics";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { centeredTextY, tightRowHeight } from "../../ui/metrics";
|
||||
import {
|
||||
cancelBandwidthBenchmark,
|
||||
getBandwidthBenchmarkStatus,
|
||||
@@ -29,6 +29,7 @@ const LIST_X = 20;
|
||||
const VALUE_X = 150;
|
||||
|
||||
type Phase = "config" | "arming" | "running" | "done";
|
||||
type ConfigRow = "size" | "window" | "link" | "start";
|
||||
|
||||
/**
|
||||
* BLE bandwidth benchmark: pick a message size and pipeline window, stream
|
||||
@@ -38,7 +39,14 @@ type Phase = "config" | "arming" | "running" | "done";
|
||||
*/
|
||||
export class BandwidthBenchmarkLayer implements Layer {
|
||||
private phase: Phase = "config";
|
||||
private selectedRow = 0; // size, window, link preference, start
|
||||
private readonly menu = new Menu<ConfigRow>({
|
||||
items: ["size", "window", "link", "start"],
|
||||
wrap: false,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 4 },
|
||||
getHeight: () => tightRowHeight(getDefaultSmallFont()) + 4,
|
||||
draw: (args) => this.drawRow(args),
|
||||
});
|
||||
private sizeIndex = 2;
|
||||
private windowIndex = 2;
|
||||
private linkModeIndex = 0;
|
||||
@@ -134,26 +142,14 @@ export class BandwidthBenchmarkLayer implements Layer {
|
||||
}
|
||||
|
||||
const rowH = tightRowHeight(font) + 4;
|
||||
const rows: Array<[string, string]> = [
|
||||
["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`],
|
||||
["Window size", `${WINDOW_SIZES[this.windowIndex]}`],
|
||||
["Link request", LINK_MODES[this.linkModeIndex]!],
|
||||
[this.phase === "done" ? "Run again" : "Start", ""],
|
||||
];
|
||||
for (let index = 0; index < rows.length; index++) {
|
||||
const [label, value] = rows[index]!;
|
||||
const y = HEADER_HEIGHT + index * rowH;
|
||||
const selected = index === this.selectedRow;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4);
|
||||
}
|
||||
image.drawText(font, LIST_X, y + 2, label, selected ? 255 : 200);
|
||||
if (value) {
|
||||
image.drawText(font, VALUE_X, y + 2, value, selected ? 235 : 160);
|
||||
}
|
||||
}
|
||||
const rowCount = this.menu.items.length;
|
||||
this.menu.paint(
|
||||
image,
|
||||
{ x: LIST_X - 6, y: HEADER_HEIGHT - 1, width: width - 2 * LIST_X + 12, height: rowCount * rowH },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
|
||||
let y = HEADER_HEIGHT + rows.length * rowH + 10;
|
||||
let y = HEADER_HEIGHT + rowCount * rowH + 10;
|
||||
if (this.error) {
|
||||
image.drawText(font, LIST_X, y, this.error, 180);
|
||||
} else if (this.result) {
|
||||
@@ -176,6 +172,21 @@ export class BandwidthBenchmarkLayer implements Layer {
|
||||
return image;
|
||||
}
|
||||
|
||||
private drawRow({ image, item, x, y, height, selected }: MenuDrawArgs<ConfigRow>): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const [label, value]: [string, string] =
|
||||
item === "size" ? ["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`]
|
||||
: item === "window" ? ["Window size", `${WINDOW_SIZES[this.windowIndex]}`]
|
||||
: item === "link" ? ["Link request", LINK_MODES[this.linkModeIndex]!]
|
||||
: [this.phase === "done" ? "Run again" : "Start", ""];
|
||||
const textY = centeredTextY(font, y, height);
|
||||
// Text sits 6px in from the row box (LIST_X), values at VALUE_X.
|
||||
image.drawText(font, x + 6, textY, label, selected ? 255 : 200);
|
||||
if (value) {
|
||||
image.drawText(font, x + 6 + VALUE_X - LIST_X, textY, value, selected ? 235 : 160);
|
||||
}
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (this.phase === "arming" || this.phase === "running") {
|
||||
// A static screen during the run: only allow bailing out.
|
||||
@@ -186,22 +197,22 @@ export class BandwidthBenchmarkLayer implements Layer {
|
||||
}
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedRow = Math.max(0, this.selectedRow - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedRow = Math.min(3, this.selectedRow + 1);
|
||||
await this.menu.handleInput(event);
|
||||
return;
|
||||
case "click":
|
||||
if (this.selectedRow === 0) {
|
||||
case "click": {
|
||||
const row = this.menu.selectedItem;
|
||||
if (row === "size") {
|
||||
this.sizeIndex = (this.sizeIndex + 1) % MESSAGE_SIZES.length;
|
||||
} else if (this.selectedRow === 1) {
|
||||
} else if (row === "window") {
|
||||
this.windowIndex = (this.windowIndex + 1) % WINDOW_SIZES.length;
|
||||
} else if (this.selectedRow === 2) {
|
||||
} else if (row === "link") {
|
||||
this.linkModeIndex = (this.linkModeIndex + 1) % LINK_MODES.length;
|
||||
} else {
|
||||
} else if (row === "start") {
|
||||
this.startRun();
|
||||
}
|
||||
return;
|
||||
}
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { Layer, type LayerActions, type LayerContext } from "../../ui/layers";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/gestures";
|
||||
import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu";
|
||||
import { tightRowHeight } from "../../ui/metrics";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { centeredTextY, tightRowHeight } from "../../ui/metrics";
|
||||
import {
|
||||
buildSoundSequencePayload,
|
||||
CFW_SEQ_MAX,
|
||||
@@ -28,8 +27,14 @@ function sleep(ms: number): Promise<void> {
|
||||
* sequencer message lands as the previous phrase finishes.
|
||||
*/
|
||||
export class BuzzerDemoLayer implements Layer {
|
||||
private selectedIndex = 0;
|
||||
private scrollRow = 0;
|
||||
private readonly menu = new Menu<SoundEffect>({
|
||||
items: SOUND_EFFECTS,
|
||||
wrap: false,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 4 },
|
||||
getHeight: () => tightRowHeight(getDefaultSmallFont()),
|
||||
draw: (args) => drawEffectRow(args),
|
||||
});
|
||||
private playing: string | null = null;
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
@@ -42,21 +47,11 @@ export class BuzzerDemoLayer implements Layer {
|
||||
image.drawText(font, width - 24 - font.measureText(status), 8, status, 140);
|
||||
|
||||
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
|
||||
const rowH = tightRowHeight(font);
|
||||
const visibleRows = Math.max(1, (listHeight / rowH) | 0);
|
||||
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, SOUND_EFFECTS.length);
|
||||
|
||||
const lastVisible = Math.min(SOUND_EFFECTS.length, this.scrollRow + visibleRows);
|
||||
for (let index = this.scrollRow; index < lastVisible; index++) {
|
||||
const effect = SOUND_EFFECTS[index]!;
|
||||
const y = HEADER_HEIGHT + (index - this.scrollRow) * rowH;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4);
|
||||
}
|
||||
image.drawText(font, LIST_X, y + 1, effect.name, selected ? 255 : 200);
|
||||
image.drawText(font, LIST_X + 110, y + 1, effect.desc, selected ? 180 : 120);
|
||||
}
|
||||
this.menu.paint(
|
||||
image,
|
||||
{ x: LIST_X - 6, y: HEADER_HEIGHT - 1, width: width - 2 * LIST_X + 12, height: listHeight },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
|
||||
return image;
|
||||
}
|
||||
@@ -64,13 +59,11 @@ export class BuzzerDemoLayer implements Layer {
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedIndex = Math.min(SOUND_EFFECTS.length - 1, this.selectedIndex + 1);
|
||||
await this.menu.handleInput(event);
|
||||
return;
|
||||
case "click": {
|
||||
const effect = SOUND_EFFECTS[clamp(this.selectedIndex, 0, SOUND_EFFECTS.length - 1)];
|
||||
const effect = this.menu.selectedItem;
|
||||
if (effect && !this.playing) {
|
||||
// Fire-and-forget: playback paces itself with sleeps between
|
||||
// phrases; awaiting it here would stall input for seconds.
|
||||
@@ -106,3 +99,11 @@ export class BuzzerDemoLayer implements Layer {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Effect name, then its description in a second column 110px to the right. */
|
||||
function drawEffectRow({ image, item: effect, x, y, height, selected }: MenuDrawArgs<SoundEffect>): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const textY = centeredTextY(font, y, height);
|
||||
image.drawText(font, x + 6, textY, effect.name, selected ? 255 : 200);
|
||||
image.drawText(font, x + 6 + 110, textY, effect.desc, selected ? 180 : 120);
|
||||
}
|
||||
Reference in new issue
Block a user