lite layout

This commit is contained in:
2023-10-31 18:22:03 +01:00
parent fc86b31337
commit a7b49de6ac
7 changed files with 469 additions and 10 deletions

View File

@@ -0,0 +1,58 @@
export interface VisualLayout {
name: string
row: VisualLayoutRow[]
}
export interface VisualLayoutRow {
col: VisualLayoutKey[]
}
export interface VisualLayoutKey {
id: number
size?: [number, number]
}
export interface CompiledLayout {
name: string
size: [number, number]
keys: CompiledLayoutKey[]
}
export interface CompiledLayoutKey {
id: number
type: "key" | "dpad"
size: [number, number]
pos: [number, number]
}
export function compileLayout(layout: VisualLayout): CompiledLayout {
const compiled: CompiledLayout = {
name: layout.name,
size: [0, 0],
keys: [],
}
let y = 0
for (const {col} of layout.row) {
let x = 0
let maxHeight = 0
for (const {id, size} of col) {
const [width, height] = size ?? [1, 1]
compiled.keys.push({
id,
type: "key",
size: [width, height],
pos: [x, y],
})
x += width
maxHeight = Math.max(maxHeight, height)
}
y += maxHeight
compiled.size[0] = Math.max(compiled.size[0], x)
}
compiled.size[1] = y
return compiled
}