@things-factory/figure-ui 10.1.40 → 10.1.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/graphql/index.ts +1 -0
- package/client/modeller/figure-v3-material.ts +248 -0
- package/client/modeller/figure-v3-parts.ts +16 -1
- package/client/modeller/modeller-route.ts +25 -0
- package/client/modeller/v3-align.ts +84 -0
- package/client/modeller/v3-canvas.ts +140 -2
- package/client/modeller/v3-editor.ts +213 -39
- package/client/modeller/v3-gizmo.ts +80 -0
- package/client/pages/figure-list-page.ts +5 -3
- package/client/pages/figure-modeller-page.ts +9 -0
- package/client/types.ts +2 -0
- package/dist-client/graphql/index.js +1 -0
- package/dist-client/graphql/index.js.map +1 -1
- package/dist-client/modeller/figure-v3-material.d.ts +36 -0
- package/dist-client/modeller/figure-v3-material.js +249 -0
- package/dist-client/modeller/figure-v3-material.js.map +1 -0
- package/dist-client/modeller/figure-v3-parts.js +16 -1
- package/dist-client/modeller/figure-v3-parts.js.map +1 -1
- package/dist-client/modeller/modeller-route.d.ts +16 -0
- package/dist-client/modeller/modeller-route.js +27 -0
- package/dist-client/modeller/modeller-route.js.map +1 -0
- package/dist-client/modeller/v3-align.d.ts +26 -0
- package/dist-client/modeller/v3-align.js +65 -0
- package/dist-client/modeller/v3-align.js.map +1 -0
- package/dist-client/modeller/v3-canvas.d.ts +18 -0
- package/dist-client/modeller/v3-canvas.js +154 -2
- package/dist-client/modeller/v3-canvas.js.map +1 -1
- package/dist-client/modeller/v3-editor.d.ts +29 -0
- package/dist-client/modeller/v3-editor.js +225 -36
- package/dist-client/modeller/v3-editor.js.map +1 -1
- package/dist-client/modeller/v3-gizmo.d.ts +48 -0
- package/dist-client/modeller/v3-gizmo.js +40 -0
- package/dist-client/modeller/v3-gizmo.js.map +1 -0
- package/dist-client/pages/figure-list-page.d.ts +1 -0
- package/dist-client/pages/figure-list-page.js +5 -3
- package/dist-client/pages/figure-list-page.js.map +1 -1
- package/dist-client/pages/figure-modeller-page.js +9 -0
- package/dist-client/pages/figure-modeller-page.js.map +1 -1
- package/dist-client/route.d.ts +1 -1
- package/dist-client/tsconfig.tsbuildinfo +1 -1
- package/dist-client/types.d.ts +2 -0
- package/dist-client/types.js.map +1 -1
- package/package.json +4 -4
- package/test/modeller-route.test.ts +42 -0
- package/test/v3-align.test.ts +64 -0
- package/test/v3-gizmo.test.ts +97 -0
- package/test/v3-material.test.ts +68 -0
- package/test/v3-parts-wiring.test.ts +66 -0
- package/translations/en.json +7 -0
- package/translations/ja.json +7 -0
- package/translations/ko.json +7 -0
- package/translations/ms.json +7 -0
- package/translations/zh.json +7 -0
package/client/graphql/index.ts
CHANGED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { css, html, LitElement, nothing } from 'lit'
|
|
2
|
+
import { customElement, property } from 'lit/decorators.js'
|
|
3
|
+
|
|
4
|
+
import { i18next, localize } from '@operato/i18n'
|
|
5
|
+
import { MATERIAL_PRESETS, SEGMENT_PRESETS, v3TrianglesOf } from '@hatiolab/figure-model'
|
|
6
|
+
import { activePalette } from '@hatiolab/things-scene'
|
|
7
|
+
import type { V3AssetGeometry, V3Material, V3PartEdit } from '@hatiolab/figure-model'
|
|
8
|
+
|
|
9
|
+
/** The purpose each palette role is for, by its token — the V2 inspector's words for the same roles. */
|
|
10
|
+
const PURPOSE: Record<string, string> = {
|
|
11
|
+
'palette.primary': 'primary',
|
|
12
|
+
'palette.accent': 'accent',
|
|
13
|
+
'palette.neutral': 'neutral',
|
|
14
|
+
'palette.dark': 'dark',
|
|
15
|
+
'palette.light': 'light',
|
|
16
|
+
'palette.surface': 'surface',
|
|
17
|
+
'palette.status-danger': 'danger',
|
|
18
|
+
'palette.status-warning': 'warning',
|
|
19
|
+
'palette.status-info': 'info',
|
|
20
|
+
'palette.status-success': 'success'
|
|
21
|
+
}
|
|
22
|
+
export const purposeOf = (token?: string) => i18next.t(`figure.label.palette-purpose-${PURPOSE[token ?? ''] ?? 'registered'}`)
|
|
23
|
+
|
|
24
|
+
/** Bodies with a rim — the ones `set-segments` accepts. */
|
|
25
|
+
const ROUND = new Set(['cylinder', 'ellipticCylinder', 'sphere', 'frustum'])
|
|
26
|
+
const DEFAULT_SEGMENTS = 12
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 재질 — a V3 part's material, drawn as the V2 inspector draws it (V3 parity row ③).
|
|
30
|
+
*
|
|
31
|
+
* The V3 editor showed the colour role as a raw token (`palette.dark`) in a select and nothing else. The V3 material
|
|
32
|
+
* holds the colour role, a preset, transparency and a glow; a round body also has how finely its rim is drawn. This
|
|
33
|
+
* shows those, with the V2 words: painted colours and lit colours as two groups of swatches, each named for what it
|
|
34
|
+
* is for.
|
|
35
|
+
*
|
|
36
|
+
* Not here, because a V3 material has no place for them: flat shading and casting shadows (V2 fields the converter
|
|
37
|
+
* does not carry). The glow's colour comes from the lit colours, as in V2.
|
|
38
|
+
*
|
|
39
|
+
* Every change leaves as one `v3-part-edit`; the editor runs it through the session, one step of the undo history.
|
|
40
|
+
*/
|
|
41
|
+
@customElement('figure-v3-material')
|
|
42
|
+
export class FigureV3Material extends localize(i18next)(LitElement) {
|
|
43
|
+
static styles = css`
|
|
44
|
+
:host {
|
|
45
|
+
display: grid;
|
|
46
|
+
gap: 10px;
|
|
47
|
+
}
|
|
48
|
+
h3 {
|
|
49
|
+
font-size: var(--ui-t-body, 13px);
|
|
50
|
+
margin: 12px 0 0;
|
|
51
|
+
}
|
|
52
|
+
div[field] {
|
|
53
|
+
display: grid;
|
|
54
|
+
gap: 4px;
|
|
55
|
+
}
|
|
56
|
+
div[field] > label {
|
|
57
|
+
font-weight: 600;
|
|
58
|
+
}
|
|
59
|
+
div[swatches] {
|
|
60
|
+
display: flex;
|
|
61
|
+
flex-wrap: wrap;
|
|
62
|
+
gap: 4px;
|
|
63
|
+
}
|
|
64
|
+
button[swatch] {
|
|
65
|
+
width: 22px;
|
|
66
|
+
height: 22px;
|
|
67
|
+
padding: 0;
|
|
68
|
+
border-radius: 50%;
|
|
69
|
+
border: 1px solid var(--md-sys-color-outline-variant);
|
|
70
|
+
background: var(--swatch);
|
|
71
|
+
cursor: pointer;
|
|
72
|
+
}
|
|
73
|
+
button[swatch][on] {
|
|
74
|
+
outline: 2px solid var(--md-sys-color-primary);
|
|
75
|
+
outline-offset: 1px;
|
|
76
|
+
}
|
|
77
|
+
small {
|
|
78
|
+
color: var(--md-sys-color-on-surface-variant);
|
|
79
|
+
}
|
|
80
|
+
select,
|
|
81
|
+
input[type='range'] {
|
|
82
|
+
font: inherit;
|
|
83
|
+
min-width: 0;
|
|
84
|
+
}
|
|
85
|
+
label[toggle] {
|
|
86
|
+
display: flex;
|
|
87
|
+
align-items: center;
|
|
88
|
+
gap: 6px;
|
|
89
|
+
}
|
|
90
|
+
div[segments] {
|
|
91
|
+
display: grid;
|
|
92
|
+
grid-template-columns: repeat(3, 1fr);
|
|
93
|
+
gap: 4px;
|
|
94
|
+
}
|
|
95
|
+
div[segments] button {
|
|
96
|
+
display: grid;
|
|
97
|
+
padding: 4px;
|
|
98
|
+
font: inherit;
|
|
99
|
+
border: 1px solid var(--md-sys-color-outline-variant);
|
|
100
|
+
border-radius: 6px;
|
|
101
|
+
background: var(--md-sys-color-surface);
|
|
102
|
+
color: var(--md-sys-color-on-surface);
|
|
103
|
+
cursor: pointer;
|
|
104
|
+
}
|
|
105
|
+
div[segments] button[on] {
|
|
106
|
+
background: var(--md-sys-color-secondary-container);
|
|
107
|
+
color: var(--md-sys-color-on-secondary-container);
|
|
108
|
+
border-color: var(--md-sys-color-primary);
|
|
109
|
+
}
|
|
110
|
+
`
|
|
111
|
+
|
|
112
|
+
/** The selected body as evaluated — its material, its segments and its shape. */
|
|
113
|
+
@property({ attribute: false }) body?: V3AssetGeometry & { id: string }
|
|
114
|
+
/** The asset's palette, token → colour. */
|
|
115
|
+
@property({ attribute: false }) palette: Record<string, string> = {}
|
|
116
|
+
@property({ type: Boolean }) locked = false
|
|
117
|
+
|
|
118
|
+
render() {
|
|
119
|
+
const part = this.body
|
|
120
|
+
if (!part) return nothing
|
|
121
|
+
const material = part.material
|
|
122
|
+
/*
|
|
123
|
+
The roles offered are the app's palette, as in V2, with any the asset carries beyond it. A new asset carries
|
|
124
|
+
only its primary colour; a role it lacks is sent with its colour and added in the same edit (`put`).
|
|
125
|
+
*/
|
|
126
|
+
const entries = Object.entries(this.offered())
|
|
127
|
+
const painted = entries.filter(([token]) => !token.startsWith('palette.status-'))
|
|
128
|
+
const lit = entries.filter(([token]) => token.startsWith('palette.status-'))
|
|
129
|
+
const glow = material.emissive
|
|
130
|
+
return html`
|
|
131
|
+
<h3>${i18next.t('figure.label.part-material')}</h3>
|
|
132
|
+
<div field>
|
|
133
|
+
<label>${i18next.t('figure.label.palette-role')}</label>
|
|
134
|
+
<div swatches>${this.swatches(painted, material.token, token => this.put({ token }))}</div>
|
|
135
|
+
${lit.length
|
|
136
|
+
? html`<small>${i18next.t('figure.label.status-colors')}</small>
|
|
137
|
+
<div swatches>${this.swatches(lit, material.token, token => this.put({ token }))}</div>`
|
|
138
|
+
: nothing}
|
|
139
|
+
<small><b>${purposeOf(material.token)}</b> — ${i18next.t('figure.text.palette-purpose-explained')}</small>
|
|
140
|
+
</div>
|
|
141
|
+
<div field>
|
|
142
|
+
<label>${i18next.t('figure.label.preset')}</label>
|
|
143
|
+
<select
|
|
144
|
+
?disabled=${this.locked}
|
|
145
|
+
@change=${(e: Event) => this.put({ preset: ((e.target as HTMLSelectElement).value || undefined) as V3Material['preset'] })}
|
|
146
|
+
>
|
|
147
|
+
<option value="" ?selected=${!material.preset}>${i18next.t('figure.label.none')}</option>
|
|
148
|
+
${MATERIAL_PRESETS.map(
|
|
149
|
+
preset => html`<option value=${preset} ?selected=${preset === material.preset}>${i18next.t('figure.label.preset-' + preset)}</option>`
|
|
150
|
+
)}
|
|
151
|
+
</select>
|
|
152
|
+
</div>
|
|
153
|
+
<label toggle>
|
|
154
|
+
<input
|
|
155
|
+
type="checkbox"
|
|
156
|
+
?disabled=${this.locked}
|
|
157
|
+
.checked=${!!material.transparent}
|
|
158
|
+
@change=${(e: Event) => this.put({ transparent: (e.target as HTMLInputElement).checked || undefined })}
|
|
159
|
+
/>
|
|
160
|
+
${i18next.t('figure.label.transparent')}
|
|
161
|
+
</label>
|
|
162
|
+
<div field>
|
|
163
|
+
<label>${i18next.t('figure.label.emissive')}</label>
|
|
164
|
+
<input
|
|
165
|
+
type="range"
|
|
166
|
+
min="0"
|
|
167
|
+
max="1"
|
|
168
|
+
step="0.05"
|
|
169
|
+
?disabled=${this.locked}
|
|
170
|
+
.value=${String(glow?.intensity ?? 0)}
|
|
171
|
+
@change=${(e: Event) => this.glow(Number((e.target as HTMLInputElement).value))}
|
|
172
|
+
/>
|
|
173
|
+
<small>${(glow?.intensity ?? 0).toFixed(2)}</small>
|
|
174
|
+
</div>
|
|
175
|
+
${glow
|
|
176
|
+
? html`<div field>
|
|
177
|
+
<label>${i18next.t('figure.label.emissive-color')}</label>
|
|
178
|
+
<div swatches>${this.swatches(lit.length ? lit : painted, glow.token, token => this.put({ emissive: { ...glow, token } }))}</div>
|
|
179
|
+
</div>`
|
|
180
|
+
: nothing}
|
|
181
|
+
${this.renderSegments(part)}
|
|
182
|
+
`
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private renderSegments(part: V3AssetGeometry & { id: string }) {
|
|
186
|
+
const provider = (part as { shape?: { provider?: string } }).shape?.provider ?? ''
|
|
187
|
+
if (!ROUND.has(provider)) return nothing
|
|
188
|
+
const current = part.segments ?? DEFAULT_SEGMENTS
|
|
189
|
+
return html`
|
|
190
|
+
<div field>
|
|
191
|
+
<label>${i18next.t('figure.label.segments')}</label>
|
|
192
|
+
<div segments>
|
|
193
|
+
${SEGMENT_PRESETS.map(
|
|
194
|
+
preset => html`<button
|
|
195
|
+
?on=${preset === current}
|
|
196
|
+
?disabled=${this.locked}
|
|
197
|
+
@click=${() => this.edit({ kind: 'set-segments', target: part.id, segments: preset })}
|
|
198
|
+
>
|
|
199
|
+
<b>${preset}</b>
|
|
200
|
+
<small>${i18next.t('figure.text.n-triangles', { count: v3TrianglesOf({ ...part, segments: preset }) })}</small>
|
|
201
|
+
</button>`
|
|
202
|
+
)}
|
|
203
|
+
</div>
|
|
204
|
+
</div>
|
|
205
|
+
`
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private swatches(entries: [string, string][], chosen: string | undefined, pick: (token: string) => void) {
|
|
209
|
+
return entries.map(
|
|
210
|
+
([token, colour]) => html`<button
|
|
211
|
+
swatch
|
|
212
|
+
?on=${token === chosen}
|
|
213
|
+
?disabled=${this.locked}
|
|
214
|
+
title=${purposeOf(token)}
|
|
215
|
+
aria-label=${purposeOf(token)}
|
|
216
|
+
style="--swatch:${colour}"
|
|
217
|
+
@click=${() => pick(token)}
|
|
218
|
+
></button>`
|
|
219
|
+
)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private offered(): Record<string, string> {
|
|
223
|
+
return { ...(activePalette() as Record<string, string>), ...this.palette }
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Intensity 0 is no glow at all, as in V2; a new glow takes the part's own colour until another is picked. */
|
|
227
|
+
private glow(intensity: number) {
|
|
228
|
+
const material = this.body!.material
|
|
229
|
+
this.put({ emissive: intensity > 0 ? { token: material.emissive?.token ?? material.token, intensity, on: true } : undefined })
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private put(patch: Partial<V3Material>) {
|
|
233
|
+
const material: V3Material = { ...this.body!.material, ...patch }
|
|
234
|
+
for (const key of Object.keys(material) as (keyof V3Material)[]) if (material[key] === undefined) delete material[key]
|
|
235
|
+
/* The colours this material names that the asset does not carry yet. */
|
|
236
|
+
const offered = this.offered()
|
|
237
|
+
const palette = Object.fromEntries(
|
|
238
|
+
[material.token, material.emissive?.token]
|
|
239
|
+
.filter((token): token is string => !!token && this.palette[token] === undefined && offered[token] !== undefined)
|
|
240
|
+
.map(token => [token, offered[token]!])
|
|
241
|
+
)
|
|
242
|
+
this.edit({ kind: 'set-material', target: this.body!.id, material, ...(Object.keys(palette).length ? { palette } : {}) })
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private edit(edit: V3PartEdit) {
|
|
246
|
+
this.dispatchEvent(new CustomEvent('v3-part-edit', { detail: { edit }, bubbles: true, composed: true }))
|
|
247
|
+
}
|
|
248
|
+
}
|
|
@@ -47,7 +47,7 @@ const PROVIDER_ICON: Record<string, string> = {
|
|
|
47
47
|
const RECIPES: Array<{ kind: V3PrimitiveCreation['kind']; icon: string; label: string }> = [
|
|
48
48
|
{ kind: 'box', icon: 'deployed_code', label: 'figure.label.primitive-cube' },
|
|
49
49
|
{ kind: 'cylinder', icon: 'database', label: 'figure.label.primitive-cylinder' },
|
|
50
|
-
{ kind: 'rounded-box', icon: 'crop_square', label: 'figure.label.primitive-
|
|
50
|
+
{ kind: 'rounded-box', icon: 'crop_square', label: 'figure.label.primitive-rounded-box' },
|
|
51
51
|
{ kind: 'portal', icon: 'door_front', label: 'figure.label.primitive-portal' }
|
|
52
52
|
]
|
|
53
53
|
|
|
@@ -66,6 +66,8 @@ export class FigureV3Parts extends localize(i18next)(LitElement) {
|
|
|
66
66
|
color: var(--md-sys-color-on-surface);
|
|
67
67
|
font: var(--label-font, inherit);
|
|
68
68
|
font-size: 0.76rem;
|
|
69
|
+
/* Folds by its own width: the editor gives this column 140 px when it has two columns (UX, 2026-09-24). */
|
|
70
|
+
container-type: inline-size;
|
|
69
71
|
}
|
|
70
72
|
section {
|
|
71
73
|
padding: var(--spacing-medium, 8px) var(--spacing-large, 12px);
|
|
@@ -232,6 +234,19 @@ export class FigureV3Parts extends localize(i18next)(LitElement) {
|
|
|
232
234
|
--md-icon-size: 15px;
|
|
233
235
|
color: inherit;
|
|
234
236
|
}
|
|
237
|
+
/*
|
|
238
|
+
Under 200 px the selected row's four buttons pushed its name out and the last one past the edge (seen at 140,
|
|
239
|
+
2026-09-24). They go to a line of their own under the name.
|
|
240
|
+
*/
|
|
241
|
+
@container (max-width: 199px) {
|
|
242
|
+
li[selected] {
|
|
243
|
+
flex-wrap: wrap;
|
|
244
|
+
}
|
|
245
|
+
li[selected] div[ops] {
|
|
246
|
+
flex-basis: 100%;
|
|
247
|
+
justify-content: flex-end;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
235
250
|
p[quiet] {
|
|
236
251
|
margin: 0;
|
|
237
252
|
font-size: 0.76rem;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which modeller opens a figure — by the format of its source, which the server reports (`format`).
|
|
3
|
+
*
|
|
4
|
+
* The catalogue sent every figure to the V2 modeller, so a converted figure could only be opened by typing the V3
|
|
5
|
+
* page's address (2026-09-24). A new figure starts in V3: new V2 figures would only add to what the cutover has to
|
|
6
|
+
* convert (ADR-0087). A figure is opened in V3 only when the server says it is V3 — an unknown format goes to the
|
|
7
|
+
* V2 page, which reports what it could not read.
|
|
8
|
+
*/
|
|
9
|
+
export const V3_MODELLER = 'figure-v3-modeller'
|
|
10
|
+
export const V2_MODELLER = 'figure-modeller'
|
|
11
|
+
|
|
12
|
+
export function modellerPathOf(figure?: { id?: string; format?: string | null }): string {
|
|
13
|
+
if (!figure?.id) return V3_MODELLER
|
|
14
|
+
return figure.format === 'v3' ? `${V3_MODELLER}/${figure.id}` : `${V2_MODELLER}/${figure.id}`
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The version a V3 asset stamps on itself. The V2 page forwards such a figure rather than failing on it. */
|
|
18
|
+
export function isV3Source(source?: string | null): boolean {
|
|
19
|
+
if (!source) return false
|
|
20
|
+
try {
|
|
21
|
+
return (JSON.parse(source) as { version?: unknown })?.version === 'figure-v3-asset-1'
|
|
22
|
+
} catch {
|
|
23
|
+
return false
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { compileV3Asset, v3OccupiedBoxesOf, v3WritersOf } from '@hatiolab/figure-model'
|
|
2
|
+
import type { V3Asset, V3InputChange, V3WorldBox } from '@hatiolab/figure-model'
|
|
3
|
+
|
|
4
|
+
import { niceStep } from './part-edits.js'
|
|
5
|
+
import type { GizmoReach } from './v3-gizmo.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 맞추기 — the V2 inspector's align tools on a V3 part (parity row ③): centre on X, centre on Z, stand on the floor,
|
|
9
|
+
* mirror on X or Z, snap to the grid.
|
|
10
|
+
*
|
|
11
|
+
* V2 measured against its base box, which ran from 0 to its size on each axis. A V3 asset's frame has the floor at
|
|
12
|
+
* y 0 and X and Z centred on 0, so centring puts the part's box centre on 0 and the floor puts its lowest point on
|
|
13
|
+
* y 0. The part's box is its evaluated, rotated extent (`v3OccupiedBoxesOf`), so a turned part lands by what it
|
|
14
|
+
* actually covers, not by its unturned size.
|
|
15
|
+
*
|
|
16
|
+
* Every tool writes the part's own position (and, for snap, size) inputs — the same ones the gizmo writes, and only
|
|
17
|
+
* where the gizmo could (`GizmoReach`).
|
|
18
|
+
*/
|
|
19
|
+
export type AlignTool = 'centre-x' | 'centre-z' | 'floor' | 'snap'
|
|
20
|
+
|
|
21
|
+
const AXES = ['x', 'y', 'z'] as const
|
|
22
|
+
|
|
23
|
+
export function boxOf(asset: V3Asset, part: string): V3WorldBox | undefined {
|
|
24
|
+
const geometry = compileV3Asset(asset).evaluate().geometry
|
|
25
|
+
return v3OccupiedBoxesOf(geometry, v3WritersOf(asset)).find(box => box.part === part)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The grid V2 snaps to: about five steps across the figure, on 1 · 2 · 5 multiples. */
|
|
29
|
+
export function gridStepOf(asset: V3Asset): number {
|
|
30
|
+
const geometry = compileV3Asset(asset).evaluate().geometry
|
|
31
|
+
const boxes = v3OccupiedBoxesOf(geometry, v3WritersOf(asset))
|
|
32
|
+
if (!boxes.length) return 1
|
|
33
|
+
const span = Math.max(
|
|
34
|
+
...AXES.map(axis => {
|
|
35
|
+
const lo = Math.min(...boxes.map(b => b.centre[axis] - b.extent[axis] / 2))
|
|
36
|
+
const hi = Math.max(...boxes.map(b => b.centre[axis] + b.extent[axis] / 2))
|
|
37
|
+
return hi - lo
|
|
38
|
+
})
|
|
39
|
+
)
|
|
40
|
+
return niceStep(span)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function alignChanges(asset: V3Asset, part: string, reach: GizmoReach, tool: AlignTool): V3InputChange[] {
|
|
44
|
+
if (!reach.translate) return []
|
|
45
|
+
const box = boxOf(asset, part)
|
|
46
|
+
if (!box) return []
|
|
47
|
+
const value = (id: string) => asset.designInputs[id] ?? 0
|
|
48
|
+
const set = (input: string, next: number): V3InputChange => ({ kind: 'set-design', input, unit: 'mm', value: Math.round(next) })
|
|
49
|
+
const at = (axis: 'x' | 'y' | 'z') => `${part}.position.${axis}`
|
|
50
|
+
switch (tool) {
|
|
51
|
+
case 'centre-x':
|
|
52
|
+
return [set(at('x'), value(at('x')) - box.centre.x)]
|
|
53
|
+
case 'centre-z':
|
|
54
|
+
return [set(at('z'), value(at('z')) - box.centre.z)]
|
|
55
|
+
case 'floor':
|
|
56
|
+
return [set(at('y'), value(at('y')) - (box.centre.y - box.extent.y / 2))]
|
|
57
|
+
case 'snap': {
|
|
58
|
+
const step = gridStepOf(asset)
|
|
59
|
+
const round = (n: number) => Math.round(n / step) * step
|
|
60
|
+
const changes = AXES.map(axis => set(at(axis), round(value(at(axis)))))
|
|
61
|
+
/* A size never snaps to 0 — the part would look gone. */
|
|
62
|
+
if (reach.scale && reach.sizes)
|
|
63
|
+
for (const id of reach.sizes) changes.push(set(id, Math.max(step, round(value(id)))))
|
|
64
|
+
return changes
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Where a mirrored copy goes: the same distance on the other side of the centre plane. The copy is made by
|
|
71
|
+
* `duplicate-part` first; this is the position the copy is then given.
|
|
72
|
+
*/
|
|
73
|
+
export function mirrorChanges(asset: V3Asset, part: string, copy: string, axis: 'x' | 'z'): V3InputChange[] {
|
|
74
|
+
const box = boxOf(asset, part)
|
|
75
|
+
if (!box) return []
|
|
76
|
+
const id = `${part}.position.${axis}`
|
|
77
|
+
return [{ kind: 'set-design', input: `${copy}.position.${axis}`, unit: 'mm', value: Math.round((asset.designInputs[id] ?? 0) - 2 * box.centre[axis]) }]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Mirroring a part that already sits on the centre plane would put the copy inside it — V2 refuses it too. */
|
|
81
|
+
export function canMirror(asset: V3Asset, part: string, axis: 'x' | 'z', reach: GizmoReach): boolean {
|
|
82
|
+
const box = reach.translate ? boxOf(asset, part) : undefined
|
|
83
|
+
return !!box && Math.abs(box.centre[axis]) >= 0.5
|
|
84
|
+
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { LitElement, html, css, type PropertyValues } from 'lit'
|
|
2
2
|
import { customElement, property } from 'lit/decorators.js'
|
|
3
|
+
import { i18next } from '@operato/i18n'
|
|
3
4
|
import * as THREE from 'three'
|
|
4
5
|
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
|
|
6
|
+
import { TransformControls } from 'three/examples/jsm/controls/TransformControls.js'
|
|
7
|
+
import type { GizmoReach, GizmoResult } from './v3-gizmo.js'
|
|
5
8
|
import { replaceV3Meshes, disposeV3Meshes, type V3RenderPart } from './v3-renderer.js'
|
|
6
9
|
|
|
7
10
|
/** Renders evaluated V3 geometry directly. There is no parts/base or things-scene conversion. */
|
|
@@ -25,6 +28,35 @@ export class FigureV3Canvas extends LitElement {
|
|
|
25
28
|
width: 100%;
|
|
26
29
|
height: 100%;
|
|
27
30
|
}
|
|
31
|
+
div[gizmo] {
|
|
32
|
+
position: absolute;
|
|
33
|
+
top: 8px;
|
|
34
|
+
left: 8px;
|
|
35
|
+
display: flex;
|
|
36
|
+
gap: 2px;
|
|
37
|
+
padding: 2px;
|
|
38
|
+
border-radius: 6px;
|
|
39
|
+
background: var(--md-sys-color-surface);
|
|
40
|
+
border: 1px solid var(--md-sys-color-outline-variant);
|
|
41
|
+
}
|
|
42
|
+
div[gizmo] button {
|
|
43
|
+
font: inherit;
|
|
44
|
+
font-size: 12px;
|
|
45
|
+
padding: 3px 8px;
|
|
46
|
+
border: none;
|
|
47
|
+
border-radius: 4px;
|
|
48
|
+
background: none;
|
|
49
|
+
color: var(--md-sys-color-on-surface);
|
|
50
|
+
cursor: pointer;
|
|
51
|
+
}
|
|
52
|
+
div[gizmo] button[on] {
|
|
53
|
+
background: var(--md-sys-color-secondary-container);
|
|
54
|
+
color: var(--md-sys-color-on-secondary-container);
|
|
55
|
+
}
|
|
56
|
+
div[gizmo] button:disabled {
|
|
57
|
+
opacity: 0.4;
|
|
58
|
+
cursor: default;
|
|
59
|
+
}
|
|
28
60
|
p {
|
|
29
61
|
position: absolute;
|
|
30
62
|
inset: 45% 1rem auto;
|
|
@@ -36,6 +68,17 @@ export class FigureV3Canvas extends LitElement {
|
|
|
36
68
|
@property({ attribute: false }) parts: V3RenderPart[] = []
|
|
37
69
|
@property({ attribute: false }) palette: Record<string, string> = {}
|
|
38
70
|
@property() context = ''
|
|
71
|
+
/** The selected part, and what the gizmo may write for it (`gizmoReachOf`). Nothing reachable, no gizmo. */
|
|
72
|
+
@property() selected = ''
|
|
73
|
+
@property({ attribute: false }) reach: GizmoReach = { translate: false, rotate: false, scale: false }
|
|
74
|
+
/** Move · turn · size — W · E · R, as in the V2 modeller. */
|
|
75
|
+
@property() mode: 'translate' | 'rotate' | 'scale' = 'translate'
|
|
76
|
+
private transform?: TransformControls
|
|
77
|
+
/** What the gizmo holds: a stand-in at the body's origin, so the body's own matrix stays the evaluation's. */
|
|
78
|
+
private readonly proxy = new THREE.Object3D()
|
|
79
|
+
private dragStart?: { proxy: THREE.Matrix4; mesh: THREE.Matrix4; body: THREE.Mesh }
|
|
80
|
+
/** The pointerup that ends a drag is not a pick — releasing off the body would otherwise deselect it. */
|
|
81
|
+
private justDragged = false
|
|
39
82
|
private renderer?: THREE.WebGLRenderer
|
|
40
83
|
private readonly scene = new THREE.Scene()
|
|
41
84
|
private readonly group = new THREE.Group()
|
|
@@ -46,8 +89,26 @@ export class FigureV3Canvas extends LitElement {
|
|
|
46
89
|
private initialized = false
|
|
47
90
|
private contextAtRender = ''
|
|
48
91
|
render() {
|
|
49
|
-
|
|
50
|
-
|
|
92
|
+
const any = this.selected && (this.reach.translate || this.reach.rotate || this.reach.scale)
|
|
93
|
+
return html`<canvas aria-label="Figure 3D 모델" tabindex="0" @pointerup=${this.pick} @keydown=${this.key}></canvas
|
|
94
|
+
>${any
|
|
95
|
+
? html`<div gizmo>
|
|
96
|
+
${(['translate', 'rotate', 'scale'] as const).map(
|
|
97
|
+
(mode, i) => html`<button
|
|
98
|
+
?on=${this.mode === mode}
|
|
99
|
+
?disabled=${!this.reach[mode]}
|
|
100
|
+
title=${`${i18next.t('figure.label.gizmo-' + mode)} (${'WER'[i]})`}
|
|
101
|
+
@click=${() => (this.mode = mode)}
|
|
102
|
+
>
|
|
103
|
+
${i18next.t('figure.label.gizmo-' + mode)}
|
|
104
|
+
</button>`
|
|
105
|
+
)}
|
|
106
|
+
</div>`
|
|
107
|
+
: null}${this.parts.length ? null : html`<p>도형을 추가해 모델링을 시작하세요.</p>`}`
|
|
108
|
+
}
|
|
109
|
+
private key = (event: KeyboardEvent) => {
|
|
110
|
+
const mode = ({ w: 'translate', e: 'rotate', r: 'scale' } as const)[event.key.toLowerCase() as 'w' | 'e' | 'r']
|
|
111
|
+
if (mode && this.reach[mode]) this.mode = mode
|
|
51
112
|
}
|
|
52
113
|
protected firstUpdated() {
|
|
53
114
|
this.initialize()
|
|
@@ -77,6 +138,12 @@ export class FigureV3Canvas extends LitElement {
|
|
|
77
138
|
this.scene.add(light)
|
|
78
139
|
this.controls = new OrbitControls(this.camera, canvas)
|
|
79
140
|
this.controls.addEventListener('change', this.draw)
|
|
141
|
+
this.transform = new TransformControls(this.camera, canvas)
|
|
142
|
+
this.transform.setSpace('world')
|
|
143
|
+
this.transform.addEventListener('change', this.draw)
|
|
144
|
+
this.transform.addEventListener('dragging-changed', this.dragging)
|
|
145
|
+
this.transform.addEventListener('objectChange', this.follow)
|
|
146
|
+
this.scene.add(this.proxy, this.transform.getHelper())
|
|
80
147
|
this.observer = new ResizeObserver(() => {
|
|
81
148
|
const r = this.getBoundingClientRect()
|
|
82
149
|
if (r.width && r.height) {
|
|
@@ -93,6 +160,73 @@ export class FigureV3Canvas extends LitElement {
|
|
|
93
160
|
protected updated(changed: PropertyValues) {
|
|
94
161
|
if (this.initialized && (changed.has('parts') || changed.has('palette') || changed.has('context')))
|
|
95
162
|
this.updateGeometry(this.contextAtRender !== this.context)
|
|
163
|
+
if (this.initialized && ['parts', 'selected', 'reach', 'mode', 'context'].some(key => changed.has(key))) this.placeGizmo()
|
|
164
|
+
}
|
|
165
|
+
/** Puts the gizmo on the selected body, in a mode it may write — or takes it away. */
|
|
166
|
+
private placeGizmo() {
|
|
167
|
+
const transform = this.transform
|
|
168
|
+
if (!transform || this.dragStart) return
|
|
169
|
+
if (!this.reach[this.mode]) {
|
|
170
|
+
const first = (['translate', 'rotate', 'scale'] as const).find(mode => this.reach[mode])
|
|
171
|
+
if (first && first !== this.mode) {
|
|
172
|
+
this.mode = first
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const body = this.group.children.find(child => child.name === this.selected) as THREE.Mesh | undefined
|
|
177
|
+
if (!body || !this.reach[this.mode]) {
|
|
178
|
+
transform.detach()
|
|
179
|
+
this.draw()
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
const position = new THREE.Vector3(),
|
|
183
|
+
quaternion = new THREE.Quaternion(),
|
|
184
|
+
scale = new THREE.Vector3()
|
|
185
|
+
body.matrix.decompose(position, quaternion, scale)
|
|
186
|
+
this.proxy.position.copy(position)
|
|
187
|
+
this.proxy.quaternion.copy(quaternion)
|
|
188
|
+
this.proxy.scale.set(1, 1, 1)
|
|
189
|
+
this.proxy.updateMatrixWorld(true)
|
|
190
|
+
transform.setMode(this.mode)
|
|
191
|
+
/* Size follows the body's own axes; moving and turning read in the asset's axes, which the inputs are in. */
|
|
192
|
+
transform.setSpace(this.mode === 'scale' ? 'local' : 'world')
|
|
193
|
+
transform.attach(this.proxy)
|
|
194
|
+
this.draw()
|
|
195
|
+
}
|
|
196
|
+
private dragging = (event: { value: unknown }) => {
|
|
197
|
+
const on = !!event.value
|
|
198
|
+
if (this.controls) this.controls.enabled = !on
|
|
199
|
+
const body = this.group.children.find(child => child.name === this.selected) as THREE.Mesh | undefined
|
|
200
|
+
if (on && body) {
|
|
201
|
+
this.proxy.updateMatrix()
|
|
202
|
+
this.dragStart = { proxy: this.proxy.matrix.clone(), mesh: body.matrix.clone(), body }
|
|
203
|
+
return
|
|
204
|
+
}
|
|
205
|
+
const start = this.dragStart
|
|
206
|
+
this.dragStart = undefined
|
|
207
|
+
if (!start) return
|
|
208
|
+
/* Either listener may see the release first; the flag lasts past both, then clears. */
|
|
209
|
+
this.justDragged = true
|
|
210
|
+
setTimeout(() => (this.justDragged = false))
|
|
211
|
+
const result: GizmoResult = {}
|
|
212
|
+
if (this.mode === 'translate') result.position = this.proxy.position.toArray() as [number, number, number]
|
|
213
|
+
if (this.mode === 'rotate') {
|
|
214
|
+
/* rigid@1 composes Rx·Ry·Rz — three's 'XYZ' order — so the angles come back as the inputs mean them. */
|
|
215
|
+
const euler = new THREE.Euler().setFromQuaternion(this.proxy.quaternion, 'XYZ')
|
|
216
|
+
result.rotation = [euler.x, euler.y, euler.z].map(THREE.MathUtils.radToDeg) as [number, number, number]
|
|
217
|
+
}
|
|
218
|
+
if (this.mode === 'scale') result.scale = this.proxy.scale.toArray() as [number, number, number]
|
|
219
|
+
this.dispatchEvent(new CustomEvent('part-transformed', { detail: { id: this.selected, result }, bubbles: true, composed: true }))
|
|
220
|
+
}
|
|
221
|
+
/** While dragging, the body follows the stand-in; the next evaluation replaces it with where the inputs put it. */
|
|
222
|
+
private follow = () => {
|
|
223
|
+
const start = this.dragStart
|
|
224
|
+
if (!start) return
|
|
225
|
+
this.proxy.updateMatrix()
|
|
226
|
+
const delta = this.proxy.matrix.clone().multiply(start.proxy.clone().invert())
|
|
227
|
+
start.body.matrix.copy(delta.multiply(start.mesh))
|
|
228
|
+
start.body.matrixWorldNeedsUpdate = true
|
|
229
|
+
this.group.updateMatrixWorld(true)
|
|
96
230
|
}
|
|
97
231
|
private updateGeometry(fit: boolean) {
|
|
98
232
|
try {
|
|
@@ -140,6 +274,7 @@ export class FigureV3Canvas extends LitElement {
|
|
|
140
274
|
if (this.renderer) this.renderer.render(this.scene, this.camera)
|
|
141
275
|
}
|
|
142
276
|
private pick = (event: PointerEvent) => {
|
|
277
|
+
if (this.justDragged || this.transform?.dragging) return
|
|
143
278
|
const r = (event.currentTarget as HTMLCanvasElement).getBoundingClientRect(),
|
|
144
279
|
ray = new THREE.Raycaster()
|
|
145
280
|
ray.setFromCamera(
|
|
@@ -159,6 +294,9 @@ export class FigureV3Canvas extends LitElement {
|
|
|
159
294
|
this.observer?.disconnect()
|
|
160
295
|
this.themeObserver?.disconnect()
|
|
161
296
|
this.controls?.dispose()
|
|
297
|
+
this.transform?.detach()
|
|
298
|
+
this.transform?.dispose()
|
|
299
|
+
this.transform = undefined
|
|
162
300
|
disposeV3Meshes(this.group)
|
|
163
301
|
this.renderer?.dispose()
|
|
164
302
|
this.renderer = undefined
|