Import a 3D Model and automatically assign and export animations
TypeScript
better UI component for picking props
Scott Petrovic99584013e3e5
diff --git a/src/create.html b/src/create.htmlindex 674d1b7..3e8f0e4 100644--- a/src/create.html+++ b/src/create.html@@ -425,13 +425,17 @@class="secondary-button"id="props-toggle-button"aria-expanded="false"- aria-controls="props-panel-mount"+ aria-controls="props-panel"data-tippy-content="Attach props to the hands"aria-label="Attach props to the hands"><img src="images/icons/props.svg" alt="Props" width="32" height="32" /></button>- <div id="props-panel-mount"></div>+ <div id="props-panel" class="props-panel" role="region" aria-label="Props" hidden>+ <div id="props-left-hand" class="prop-picker"></div>+ <div id="props-right-hand" class="prop-picker"></div>+ <div>Prop</div>+ </div></div><div id="animation-player-mount"></div>diff --git a/src/index.html b/src/index.htmlindex ce634b8..4a8acf7 100644--- a/src/index.html+++ b/src/index.html@@ -178,10 +178,14 @@<!-- floating props toggle and panel docked to the left of the tool panel --><div id="props-dock" style="display: none">- <button class="secondary-button" id="props-toggle-button" aria-expanded="false" aria-controls="props-panel-mount" data-tippy-content="Attach props to the hands" aria-label="Attach props to the hands">+ <button class="secondary-button" id="props-toggle-button" aria-expanded="false" aria-controls="props-panel" data-tippy-content="Attach props to the hands" aria-label="Attach props to the hands"><img src="images/icons/props.svg" alt="Props" width="32" height="32" /></button>- <div id="props-panel-mount"></div>+ <div id="props-panel" class="props-panel" role="region" aria-label="Props" hidden>+ <div id="props-left-hand" class="prop-picker"></div>+ <div id="props-right-hand" class="prop-picker"></div>+ <div>Prop</div>+ </div></div><div id="animation-player-mount"></div>diff --git a/src/lib/UI.ts b/src/lib/UI.tsindex e557b6b..cf9831e 100644--- a/src/lib/UI.ts+++ b/src/lib/UI.ts@@ -101,7 +101,7 @@ export class UI {dom_import_animations_button: HTMLButtonElement | null = nulldom_import_animations_input: HTMLInputElement | null = nulldom_props_toggle_button: HTMLButtonElement | null = null- dom_props_panel_mount: HTMLElement | null = null+ dom_props_panel: HTMLElement | null = nulldom_props_dock: HTMLElement | null = nulldom_extend_arm_range_input: HTMLInputElement | null = nulldom_extend_arm_numeric_input: HTMLInputElement | null = null@@ -227,7 +227,7 @@ export class UI {this.dom_export_button = document.querySelector('#export-button')this.dom_import_animations_button = document.querySelector('#import-animations-button')this.dom_props_toggle_button = document.querySelector('#props-toggle-button')- this.dom_props_panel_mount = document.querySelector('#props-panel-mount')+ this.dom_props_panel = document.querySelector('#props-panel')this.dom_props_dock = document.querySelector('#props-dock')this.dom_import_animations_input = document.querySelector('#import-animations-input')this.dom_mirror_animations_checkbox = document.querySelector('#mirror-animations-checkbox')diff --git a/src/lib/processes/animations-listing/props/PropPicker.ts b/src/lib/processes/animations-listing/props/PropPicker.tsnew file mode 100644index 0000000..c41f53e--- /dev/null+++ b/src/lib/processes/animations-listing/props/PropPicker.ts@@ -0,0 +1,281 @@+import { PropCatalog, type PropDefinition } from './PropCatalog.ts'+import { PropType } from './PropType.ts'++/**+ * Custom dropdown for choosing a prop. A native <select> cannot render images+ * inside its options, so this implements the ARIA listbox pattern: a trigger+ * button that shows the current prop, and a popup grid of preview tiles.+ */+export class PropPicker extends EventTarget {+ private readonly id: string+ private readonly trigger: HTMLButtonElement+ private readonly trigger_preview: HTMLElement+ private readonly trigger_name: HTMLElement+ private readonly listbox: HTMLElement+ private readonly options: HTMLElement[] = []+ private current_value: PropType = PropType.None+ private active_index: number = 0++ constructor (private readonly element: HTMLElement, label: string) {+ super()++ const id = element.id+ this.id = id+ this.element.innerHTML = `+ <span class="prop-picker-label" id="${id}-label">${label}</span>+ <button type="button" class="prop-picker-trigger" id="${id}-trigger"+ aria-haspopup="listbox" aria-expanded="false" aria-controls="${id}-listbox"+ aria-labelledby="${id}-label ${id}-trigger">+ <span class="prop-picker-thumb"></span>+ <span class="prop-picker-name"></span>+ <span class="prop-picker-chevron" aria-hidden="true"></span>+ </button>+ <div class="prop-picker-popup" id="${id}-listbox" role="listbox" tabindex="-1"+ aria-labelledby="${id}-label" hidden>+ ${this.options_html()}+ </div>+ `++ this.trigger = this.element.querySelector<HTMLButtonElement>('.prop-picker-trigger') as HTMLButtonElement+ this.trigger_preview = this.element.querySelector<HTMLElement>('.prop-picker-trigger .prop-picker-thumb') as HTMLElement+ this.trigger_name = this.element.querySelector<HTMLElement>('.prop-picker-name') as HTMLElement+ this.listbox = this.element.querySelector<HTMLElement>('.prop-picker-popup') as HTMLElement+ this.options.push(...this.listbox.querySelectorAll<HTMLElement>('[role="option"]'))++ this.add_event_listeners()+ this.render_value()+ }++ public get value (): PropType {+ return this.current_value+ }++ public set value (value: PropType) {+ this.current_value = PropCatalog.find(value) !== undefined ? value : PropType.None+ this.render_value()+ }++ public set_enabled (is_enabled: boolean, disabled_reason: string = ''): void {+ this.trigger.disabled = !is_enabled+ this.trigger.title = is_enabled ? '' : disabled_reason+ if (!is_enabled) {+ this.close()+ }+ }++ public is_open (): boolean {+ return !this.listbox.hidden+ }++ public open (): void {+ if (this.trigger.disabled || this.is_open()) {+ return+ }++ this.listbox.hidden = false+ this.trigger.setAttribute('aria-expanded', 'true')+ this.set_active(this.index_of(this.current_value))+ this.listbox.focus()+ }++ public close (restore_focus: boolean = false): void {+ if (!this.is_open()) {+ return+ }++ this.listbox.hidden = true+ this.trigger.setAttribute('aria-expanded', 'false')+ if (restore_focus) {+ this.trigger.focus()+ }+ }++ private add_event_listeners (): void {+ this.trigger.addEventListener('click', () => {+ this.is_open() ? this.close() : this.open()+ })++ this.trigger.addEventListener('keydown', (event: KeyboardEvent) => {+ if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {+ event.preventDefault()+ this.open()+ }+ })++ this.listbox.addEventListener('keydown', (event: KeyboardEvent) => {+ this.handle_listbox_key(event)+ })++ this.listbox.addEventListener('click', (event: MouseEvent) => {+ const option = (event.target as HTMLElement).closest<HTMLElement>('[role="option"]')+ if (option !== null) {+ this.choose(this.options.indexOf(option))+ }+ })++ this.listbox.addEventListener('mousemove', (event: MouseEvent) => {+ const option = (event.target as HTMLElement).closest<HTMLElement>('[role="option"]')+ if (option !== null) {+ this.set_active(this.options.indexOf(option), false)+ }+ })++ // close when focus or a click lands anywhere outside the picker+ this.element.addEventListener('focusout', (event: FocusEvent) => {+ if (!this.element.contains(event.relatedTarget as Node | null)) {+ this.close()+ }+ })+ document.addEventListener('pointerdown', (event: PointerEvent) => {+ if (!this.element.contains(event.target as Node)) {+ this.close()+ }+ })+ }++ private handle_listbox_key (event: KeyboardEvent): void {+ let next_index: number | null = null++ switch (event.key) {+ case 'ArrowRight': next_index = Math.min(this.active_index + 1, this.options.length - 1); break+ case 'ArrowLeft': next_index = Math.max(this.active_index - 1, 0); break+ case 'ArrowDown': next_index = this.vertical_neighbor(1); break+ case 'ArrowUp': next_index = this.vertical_neighbor(-1); break+ case 'Home': next_index = 0; break+ case 'End': next_index = this.options.length - 1; break+ case 'Enter':+ case ' ':+ event.preventDefault()+ this.choose(this.active_index)+ return+ case 'Escape':+ event.preventDefault()+ event.stopPropagation()+ this.close(true)+ return+ case 'Tab':+ this.close()+ return+ default:+ return+ }++ event.preventDefault()+ this.set_active(next_index)+ }++ // the grid wraps per category, so find the tile in the adjacent row by layout position+ private vertical_neighbor (direction: 1 | -1): number {+ const current = this.options[this.active_index].getBoundingClientRect()+ const current_center = current.left + current.width / 2++ let best_index = this.active_index+ let best_row_top: number | null = null+ let best_distance = Infinity++ this.options.forEach((option, index) => {+ const rect = option.getBoundingClientRect()+ const is_in_direction = direction === 1 ? rect.top > current.top + 1 : rect.top < current.top - 1+ if (!is_in_direction) {+ return+ }++ const is_closer_row = best_row_top === null || (direction === 1 ? rect.top < best_row_top - 1 : rect.top > best_row_top + 1)+ const is_same_row = best_row_top !== null && Math.abs(rect.top - best_row_top) <= 1+ const distance = Math.abs(rect.left + rect.width / 2 - current_center)++ if (is_closer_row || (is_same_row && distance < best_distance)) {+ best_index = index+ best_row_top = rect.top+ best_distance = distance+ }+ })++ return best_index+ }++ private set_active (index: number, scroll_into_view: boolean = true): void {+ this.options[this.active_index]?.classList.remove('active')+ this.active_index = Math.max(0, index)++ const option = this.options[this.active_index]+ option.classList.add('active')+ this.listbox.setAttribute('aria-activedescendant', option.id)+ if (scroll_into_view) {+ option.scrollIntoView({ block: 'nearest' })+ }+ }++ private choose (index: number): void {+ const value = this.options[index]?.dataset.value as PropType | undefined+ if (value === undefined) {+ return+ }++ const has_changed = value !== this.current_value+ this.current_value = value+ this.render_value()+ this.close(true)++ if (has_changed) {+ this.dispatchEvent(new Event('change'))+ }+ }++ private render_value (): void {+ const definition = PropCatalog.find(this.current_value)+ this.trigger_preview.innerHTML = this.thumb_html(definition)+ this.trigger_name.textContent = definition?.display_name ?? 'None'++ this.options.forEach((option) => {+ option.setAttribute('aria-selected', String(option.dataset.value === this.current_value))+ })+ }++ private index_of (value: PropType): number {+ return Math.max(0, this.options.findIndex((option) => option.dataset.value === value))+ }++ private thumb_html (definition: PropDefinition | undefined): string {+ if (definition === undefined) {+ return '<span class="prop-picker-empty" aria-hidden="true"></span>'+ }++ return `<img src="${definition.preview_path}" alt="" loading="lazy">`+ }++ private option_html (value: PropType, name: string, definition?: PropDefinition): string {+ return `+ <div class="prop-picker-option" role="option" id="${this.id}-option-${value}"+ data-value="${value}" aria-selected="false" title="${name}">+ <span class="prop-picker-thumb">${this.thumb_html(definition)}</span>+ <span class="prop-picker-option-name">${name}</span>+ </div>+ `+ }++ private options_html (): string {+ const definitions = PropCatalog.all()+ const categories = Array.from(new Set(definitions.map((definition) => definition.category)))++ const groups_html = categories.map((category, category_index) => {+ const header_id = `${this.id}-group-${category_index}`+ const category_options = definitions+ .filter((definition) => definition.category === category)+ .map((definition) => this.option_html(definition.type, definition.display_name, definition))+ .join('')++ return `+ <div class="prop-picker-group" role="group" aria-labelledby="${header_id}">+ <div class="prop-picker-group-label" id="${header_id}" role="presentation">${category}</div>+ <div class="prop-picker-grid">${category_options}</div>+ </div>+ `+ }).join('')++ return `+ <div class="prop-picker-grid">${this.option_html(PropType.None, 'None')}</div>+ ${groups_html}+ `+ }+}diff --git a/src/lib/processes/animations-listing/props/PropsPanel.ts b/src/lib/processes/animations-listing/props/PropsPanel.tsindex f5d982b..e07dc60 100644--- a/src/lib/processes/animations-listing/props/PropsPanel.ts+++ b/src/lib/processes/animations-listing/props/PropsPanel.ts@@ -1,14 +1,13 @@import { UI } from '../../../UI.ts'-import { PropCatalog } from './PropCatalog.ts'+import { PropPicker } from './PropPicker.ts'import { PropSide } from './PropSide.ts'import { PropType } from './PropType.ts'import { type PropSelection } from './PropSelection.ts'export class PropsPanel extends EventTarget {private readonly ui: UI = UI.getInstance()- private readonly selects = new Map<PropSide, HTMLSelectElement>()- private readonly previews = new Map<PropSide, HTMLImageElement>()- private dom_panel: HTMLElement | null = null+ private left_picker: PropPicker | null = null+ private right_picker: PropPicker | null = nullprivate added_event_listeners: boolean = falsepublic initialize (): void {@@ -16,41 +15,19 @@ export class PropsPanel extends EventTarget {return}- const mount = this.ui.dom_props_panel_mount- if (mount === null) {+ const dom_left_hand = document.querySelector<HTMLElement>('#props-left-hand')+ const dom_right_hand = document.querySelector<HTMLElement>('#props-right-hand')+ if (dom_left_hand === null || dom_right_hand === null) {return}- mount.innerHTML = `- <div class="props-panel" role="region" aria-label="Props" hidden>- <div class="props-panel-header">Props</div>- ${this.side_row_html(PropSide.Left, 'Left hand')}- ${this.side_row_html(PropSide.Right, 'Right hand')}- </div>- `-- this.dom_panel = mount.querySelector('.props-panel')-- Object.values(PropSide).forEach((side) => {- const select = mount.querySelector<HTMLSelectElement>(`#props-${side}-hand-select`)- if (select === null) {- return- }-- this.selects.set(side, select)- const preview = mount.querySelector<HTMLImageElement>(`#props-${side}-hand-preview`)- if (preview !== null) {- this.previews.set(side, preview)- }-- select.addEventListener('change', () => {- this.update_preview(side)- this.dispatch_selection_changed()- })- })+ this.left_picker = new PropPicker(dom_left_hand, 'Left hand')+ this.right_picker = new PropPicker(dom_right_hand, 'Right hand')+ this.left_picker.addEventListener('change', () => { this.dispatch_selection_changed() })+ this.right_picker.addEventListener('change', () => { this.dispatch_selection_changed() })this.ui.dom_props_toggle_button?.addEventListener('click', () => {- this.set_expanded(this.dom_panel?.hidden === true)+ this.set_expanded(this.ui.dom_props_panel?.hidden === true)})// keep the floating panel docked beside the tool panel as its width changes@@ -76,48 +53,18 @@ export class PropsPanel extends EventTarget {}public set_side_enabled (side: PropSide, is_enabled: boolean): void {- const select = this.selects.get(side)- if (select === undefined) {- return- }-- select.disabled = !is_enabled- select.title = is_enabled ? '' : 'No hand bone was found for this side'+ const picker = side === PropSide.Left ? this.left_picker : this.right_picker+ picker?.set_enabled(is_enabled, 'No hand bone was found for this side')}public set_selection (selection: PropSelection): void {- const left_select = this.selects.get(PropSide.Left)- const right_select = this.selects.get(PropSide.Right)-- if (left_select !== undefined) { left_select.value = selection.left }- if (right_select !== undefined) { right_select.value = selection.right }-- this.update_preview(PropSide.Left)- this.update_preview(PropSide.Right)- }-- private update_preview (side: PropSide): void {- const preview = this.previews.get(side)- if (preview === undefined) {- return- }-- const definition = PropCatalog.find(this.selected_prop(side))- if (definition === undefined) {- preview.removeAttribute('src')- preview.alt = ''- preview.hidden = true- return- }-- preview.src = definition.preview_path- preview.alt = definition.display_name- preview.hidden = false+ if (this.left_picker !== null) { this.left_picker.value = selection.left }+ if (this.right_picker !== null) { this.right_picker.value = selection.right }}private set_expanded (is_expanded: boolean): void {- if (this.dom_panel !== null) {- this.dom_panel.hidden = !is_expanded+ if (this.ui.dom_props_panel !== null) {+ this.ui.dom_props_panel.hidden = !is_expanded}const toggle_button = this.ui.dom_props_toggle_button@@ -125,44 +72,17 @@ export class PropsPanel extends EventTarget {toggle_button.setAttribute('aria-expanded', String(is_expanded))toggle_button.classList.toggle('active', is_expanded)}- }-- private side_row_html (side: PropSide, label: string): string {- const definitions = PropCatalog.all()- const categories = Array.from(new Set(definitions.map((definition) => definition.category)))- const options_html = [- `<option value="${PropType.None}">None</option>`,- ...categories.map((category) => {- const category_options = definitions- .filter((definition) => definition.category === category)- .map((definition) => `<option value="${definition.type}">${definition.display_name}</option>`)- .join('')- return `<optgroup label="${category}">${category_options}</optgroup>`- })- ].join('')-- return `- <label class="props-panel-row" for="props-${side}-hand-select">- <span>${label}</span>- <span class="props-panel-control">- <span class="props-panel-preview">- <img id="props-${side}-hand-preview" alt="" hidden>- </span>- <select id="props-${side}-hand-select">${options_html}</select>- </span>- </label>- `- }- private selected_prop (side: PropSide): PropType {- const value = this.selects.get(side)?.value- return Object.values(PropType).includes(value as PropType) ? value as PropType : PropType.None+ if (!is_expanded) {+ this.left_picker?.close()+ this.right_picker?.close()+ }}private dispatch_selection_changed (): void {const selection: PropSelection = {- left: this.selected_prop(PropSide.Left),- right: this.selected_prop(PropSide.Right)+ left: this.left_picker?.value ?? PropType.None,+ right: this.right_picker?.value ?? PropType.None}this.dispatchEvent(new CustomEvent<PropSelection>('props-selection-changed', { detail: selection }))diff --git a/src/lib/processes/animations-listing/props/props.css b/src/lib/processes/animations-listing/props/props.cssindex ca6ffc6..2d40a5d 100644--- a/src/lib/processes/animations-listing/props/props.css+++ b/src/lib/processes/animations-listing/props/props.css@@ -10,11 +10,16 @@right: 26rem;z-index: 10;display: flex;- flex-direction: column;- align-items: flex-end;+ flex-direction: row;+ align-items: flex-start;gap: 0.5rem;}+/* panel sits to the left of the toggle button */+#props-panel {+ order: -1;+}+#props-toggle-button {display: flex;align-items: center;@@ -24,10 +29,9 @@.props-panel {display: flex;- flex-direction: column;- gap: 0.5rem;- min-width: 14rem;- padding: 0.75rem;+ flex-direction: row;+ gap: 1rem;+ padding: 0.5rem 0.75rem;border: 1px solid var(--bg-tertiary);border-radius: 6px;background: color-mix(in srgb, var(--bg-primary) 90%, black);@@ -38,52 +42,161 @@display: none;}-.props-panel-header {- color: var(--text-primary);- font-size: 1.1rem;-}+/* prop picker: custom dropdown with preview images */-.props-panel-row {+.prop-picker {+ position: relative;display: flex;- align-items: center;- justify-content: space-between;- gap: 1rem;+ flex-direction: column;+ gap: 0.25rem;color: var(--text-alternate);}-.props-panel-control {+.prop-picker-label {+ font-size: 0.85rem;+}++.prop-picker-trigger {display: flex;align-items: center;gap: 0.5rem;+ width: 11rem;+ padding: 0.25rem 0.5rem 0.25rem 0.25rem;+ border: 1px solid var(--text-alternate);+ border-radius: 4px;+ background: var(--bg-alternate);+ box-shadow: none;+ color: var(--text-primary);+ text-align: left;+ cursor: pointer;+}++.prop-picker-trigger:hover:not(:disabled),+.prop-picker-trigger[aria-expanded="true"] {+ border-color: var(--text-primary);+}++.prop-picker-trigger:disabled {+ opacity: 0.5;+ cursor: not-allowed;}-.props-panel-preview {+.prop-picker-name {+ flex: 1;+ overflow: hidden;+ white-space: nowrap;+ text-overflow: ellipsis;+}++.prop-picker-chevron {+ width: 0.4rem;+ height: 0.4rem;+ margin-right: 0.15rem;+ border-right: 2px solid currentColor;+ border-bottom: 2px solid currentColor;+ transform: translateY(-25%) rotate(45deg);+ transition: transform 0.15s ease;+}++.prop-picker-trigger[aria-expanded="true"] .prop-picker-chevron {+ transform: translateY(25%) rotate(-135deg);+}++.prop-picker-thumb {display: flex;align-items: center;justify-content: center;flex-shrink: 0;width: 2.5rem;height: 2.5rem;- border: 1px solid var(--bg-tertiary);- border-radius: 4px;- background: var(--bg-alternate);+ border-radius: 3px;+ /* background: color-mix(in srgb, var(--bg-primary) 70%, transparent); */}-.props-panel-preview img {+.prop-picker-thumb img {width: 100%;height: 100%;object-fit: contain;}-.props-panel-preview img[hidden] {+/* "None" slot: a circle with a slash */+.prop-picker-empty {+ width: 50%;+ height: 50%;+ border: 2px solid var(--text-alternate);+ border-radius: 50%;+ background: linear-gradient(45deg, transparent calc(50% - 1px), var(--text-alternate) calc(50% - 1px), var(--text-alternate) calc(50% + 1px), transparent calc(50% + 1px));+ opacity: 0.6;+}++.prop-picker-popup {+ position: absolute;+ top: calc(100% + 0.25rem);+ right: 0;+ z-index: 20;+ width: 22rem;+ max-height: min(34rem, calc(100vh - var(--navigation-height) - 8rem));+ overflow-y: auto;+ padding: 0.5rem;+ border: 1px solid var(--bg-tertiary);+ border-radius: 6px;+ background: var(--bg-primary);+ box-shadow: 0 6px 16px rgba(0, 0, 0, 0.35);+ outline: none;+}++.prop-picker-popup[hidden] {display: none;}-.props-panel-row select {- min-width: 8rem;+.prop-picker-group {+ margin-top: 0.5rem;}-.props-panel-row select:disabled {- opacity: 0.5;- cursor: not-allowed;+.prop-picker-group-label {+ margin: 0 0 0.25rem 0.15rem;+ color: var(--text-alternate);+ font-size: 0.75rem;+ text-transform: uppercase;+ letter-spacing: 0.05em;+}++.prop-picker-grid {+ display: grid;+ grid-template-columns: repeat(4, 1fr);+ gap: 0.25rem;+}++.prop-picker-option {+ display: flex;+ flex-direction: column;+ align-items: center;+ gap: 0.2rem;+ min-width: 0;+ padding: 0.3rem 0.2rem;+ border: 1px solid transparent;+ border-radius: 4px;+ color: var(--text-primary);+ cursor: pointer;+}++.prop-picker-option .prop-picker-thumb {+ width: 3.5rem;+ height: 3.5rem;+}++.prop-picker-option-name {+ max-width: 100%;+ overflow: hidden;+ font-size: 0.75rem;+ white-space: nowrap;+ text-overflow: ellipsis;+}++.prop-picker-option.active {+ background: var(--bg-alternate);+}++.prop-picker-option[aria-selected="true"] {+ border-color: var(--text-primary);}