dazscript-framework 1.0.35 → 1.0.37
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/package.json +1 -1
- package/src/helpers/node-helper.test.ts +29 -0
- package/src/helpers/node-helper.ts +1 -1
- package/src/helpers/primitive-helper.ts +67 -0
- package/src/helpers/scene-helper.ts +35 -0
- package/src/helpers/scene-node-helper.ts +1 -37
- package/src/helpers/string-helper.test.ts +12 -0
- package/src/helpers/string-helper.ts +15 -1
package/package.json
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { isHairType } from './node-helper'
|
|
3
|
+
|
|
4
|
+
const getTypeForNode = vi.fn()
|
|
5
|
+
const isHairTypeAsset = vi.fn()
|
|
6
|
+
|
|
7
|
+
vi.mock('@dsf/core/global', () => ({
|
|
8
|
+
app: {
|
|
9
|
+
getAssetMgr: () => ({
|
|
10
|
+
getTypeForNode,
|
|
11
|
+
isHairType: isHairTypeAsset
|
|
12
|
+
})
|
|
13
|
+
},
|
|
14
|
+
sceneHelper: {}
|
|
15
|
+
}))
|
|
16
|
+
|
|
17
|
+
describe('node helper hair classification', () => {
|
|
18
|
+
it('treats eyelash content types as hair regardless of case', () => {
|
|
19
|
+
getTypeForNode.mockReturnValue('Follower/Attachment/Head/Face/Eyelashes')
|
|
20
|
+
isHairTypeAsset.mockReturnValue(false)
|
|
21
|
+
|
|
22
|
+
const node = {
|
|
23
|
+
iskindof: (type: string) => type === 'DzSkeleton',
|
|
24
|
+
getSkeleton: () => node
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
expect(isHairType(node as unknown as DzNode)).toBe(true)
|
|
28
|
+
})
|
|
29
|
+
})
|
|
@@ -120,7 +120,7 @@ export const isHairType = (node: DzNode): boolean => {
|
|
|
120
120
|
const figure = getFigure(node)!
|
|
121
121
|
const assetMgr = app.getAssetMgr()
|
|
122
122
|
const type = assetMgr.getTypeForNode(figure)
|
|
123
|
-
return assetMgr.isHairType(type) || contains(type.valueOf(), 'Hair')
|
|
123
|
+
return assetMgr.isHairType(type) || contains(type.valueOf(), ['Hair', 'Eyelashes'], true)
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
export const isGeoShell = (node: DzNode): boolean => {
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export type PrimitiveKind = 'Sphere' | 'Cube' | 'Cone' | 'Plane' | 'Torus' | 'Cylinder'
|
|
2
|
+
|
|
3
|
+
export type CreatePrimitiveOptions = {
|
|
4
|
+
kind: PrimitiveKind
|
|
5
|
+
name?: string
|
|
6
|
+
label?: string
|
|
7
|
+
size?: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type CreatePrimitiveResult = {
|
|
11
|
+
ok: boolean
|
|
12
|
+
node: DzNode | null
|
|
13
|
+
message: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare const Geometry: DzGeometryUtil | undefined
|
|
17
|
+
|
|
18
|
+
export const createPrimitive = (options: CreatePrimitiveOptions): CreatePrimitiveResult => {
|
|
19
|
+
const geometryUtil = getGeometryUtil()
|
|
20
|
+
if (!geometryUtil) {
|
|
21
|
+
return { ok: false, node: null, message: 'Global Geometry utility is not available' }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const primitiveType = getPrimitiveType(geometryUtil, options.kind)
|
|
25
|
+
if (primitiveType === null) {
|
|
26
|
+
return { ok: false, node: null, message: `Unsupported primitive kind: ${options.kind}` }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const settings = new DzSettings()
|
|
30
|
+
geometryUtil.getDefaultPrimitiveOptions(primitiveType, settings)
|
|
31
|
+
|
|
32
|
+
if (typeof options.size === 'number' && isFinite(options.size) && options.size > 0) {
|
|
33
|
+
settings.setFloatValue('size', options.size)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const node = geometryUtil.createPrimitive(settings)
|
|
37
|
+
if (!node) {
|
|
38
|
+
return { ok: false, node: null, message: `DzGeometryUtil did not create a ${options.kind}` }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (options.name) node.setName(options.name)
|
|
42
|
+
if (options.label) node.setLabel(options.label)
|
|
43
|
+
if (!isNodeInScene(node)) Scene.addNode(node)
|
|
44
|
+
|
|
45
|
+
return { ok: true, node, message: '' }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const getGeometryUtil = (): DzGeometryUtil | null =>
|
|
49
|
+
typeof Geometry !== 'undefined' ? Geometry : null
|
|
50
|
+
|
|
51
|
+
const getPrimitiveType = (geometryUtil: DzGeometryUtil, kind: PrimitiveKind): number | null => {
|
|
52
|
+
const primitiveTypes = geometryUtil as any
|
|
53
|
+
if (kind === 'Sphere') return primitiveTypes.Sphere
|
|
54
|
+
if (kind === 'Cube') return primitiveTypes.Cube
|
|
55
|
+
if (kind === 'Cone') return primitiveTypes.Cone
|
|
56
|
+
if (kind === 'Plane') return primitiveTypes.Plane
|
|
57
|
+
if (kind === 'Torus') return primitiveTypes.Torus
|
|
58
|
+
if (kind === 'Cylinder') return primitiveTypes.Cylinder
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const isNodeInScene = (node: DzNode): boolean => {
|
|
63
|
+
for (let i = 0; i < Scene.getNumNodes(); i++) {
|
|
64
|
+
if (Scene.getNode(i) === node) return true
|
|
65
|
+
}
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
@@ -2,6 +2,7 @@ import { scene } from '@dsf/core/global'
|
|
|
2
2
|
import { contains, distinct, group } from './array-helper'
|
|
3
3
|
import { getRoot, isFigure } from './node-helper'
|
|
4
4
|
import { getPaneNodeEditor, getParametersPane, getParametersPaneNodeEditor } from './pane-helper'
|
|
5
|
+
import { startsWith } from './string-helper'
|
|
5
6
|
|
|
6
7
|
export const getSelectedOf = <T>(typeName: string): DzNode[] => {
|
|
7
8
|
let nodes: DzNode[] = []
|
|
@@ -143,3 +144,37 @@ export const timeToFrame = (time: DzTime): number => {
|
|
|
143
144
|
export const frameToTime = (frame: number): DzTime => {
|
|
144
145
|
return new DzTime(scene.getTimeStep().valueOf() * frame)
|
|
145
146
|
}
|
|
147
|
+
|
|
148
|
+
export const collectSceneNodeIds = (): number[] => {
|
|
149
|
+
const ids: number[] = []
|
|
150
|
+
for (let i = 0; i < Scene.getNumNodes(); i++) {
|
|
151
|
+
ids.push(Scene.getNode(i).elementID)
|
|
152
|
+
}
|
|
153
|
+
return ids
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export const getNodesAddedAfter = (beforeIds: number[]): DzNode[] => {
|
|
157
|
+
const nodes: DzNode[] = []
|
|
158
|
+
for (let i = 0; i < Scene.getNumNodes(); i++) {
|
|
159
|
+
const node = Scene.getNode(i)
|
|
160
|
+
if (beforeIds.indexOf(node.elementID) < 0) nodes.push(node)
|
|
161
|
+
}
|
|
162
|
+
return nodes
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export const removeNodesByNameOrLabelPrefix = (prefix: string): number => {
|
|
166
|
+
const nodes: DzNode[] = []
|
|
167
|
+
|
|
168
|
+
for (let i = 0; i < Scene.getNumNodes(); i++) {
|
|
169
|
+
const node = Scene.getNode(i)
|
|
170
|
+
if (startsWith(String(node.getName()), prefix) || startsWith(String(node.getLabel()), prefix)) {
|
|
171
|
+
nodes.push(node)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
176
|
+
Scene.removeNode(nodes[i])
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return nodes.length
|
|
180
|
+
}
|
|
@@ -1,37 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
const ids: number[] = []
|
|
3
|
-
for (let i = 0; i < Scene.getNumNodes(); i++) {
|
|
4
|
-
ids.push(Scene.getNode(i).elementID)
|
|
5
|
-
}
|
|
6
|
-
return ids
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const getNodesAddedAfter = (beforeIds: number[]): DzNode[] => {
|
|
10
|
-
const nodes: DzNode[] = []
|
|
11
|
-
for (let i = 0; i < Scene.getNumNodes(); i++) {
|
|
12
|
-
const node = Scene.getNode(i)
|
|
13
|
-
if (beforeIds.indexOf(node.elementID) < 0) nodes.push(node)
|
|
14
|
-
}
|
|
15
|
-
return nodes
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const removeNodesByNameOrLabelPrefix = (prefix: string): number => {
|
|
19
|
-
const nodes: DzNode[] = []
|
|
20
|
-
|
|
21
|
-
for (let i = 0; i < Scene.getNumNodes(); i++) {
|
|
22
|
-
const node = Scene.getNode(i)
|
|
23
|
-
if (startsWith(String(node.getName()), prefix) || startsWith(String(node.getLabel()), prefix)) {
|
|
24
|
-
nodes.push(node)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
for (let i = 0; i < nodes.length; i++) {
|
|
29
|
-
Scene.removeNode(nodes[i])
|
|
30
|
-
nodes[i].deleteLater?.()
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return nodes.length
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const startsWith = (value: string, prefix: string): boolean =>
|
|
37
|
-
value.substr(0, prefix.length) === prefix
|
|
1
|
+
export { collectSceneNodeIds, getNodesAddedAfter, removeNodesByNameOrLabelPrefix } from './scene-helper'
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { contains } from './string-helper'
|
|
3
|
+
|
|
4
|
+
describe('string contains helper', () => {
|
|
5
|
+
it('matches any requested token case-insensitively when requested', () => {
|
|
6
|
+
expect(contains('Follower/Attachment/Head/Face/Eyelashes', ['hair', 'eyelashes'], true)).toBe(true)
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('keeps matching case-sensitive by default', () => {
|
|
10
|
+
expect(contains('Follower/Attachment/Head/Face/Eyelashes', 'eyelashes')).toBe(false)
|
|
11
|
+
})
|
|
12
|
+
})
|
|
@@ -4,7 +4,17 @@ export const isNumeric = (value: string | number): boolean => {
|
|
|
4
4
|
!isNaN(Number(value.toString())))
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
export const contains = (source: string, search: string | string[]): boolean => {
|
|
7
|
+
export const contains = (source: string, search: string | string[], caseInsensitive: boolean = false): boolean => {
|
|
8
|
+
if (caseInsensitive) {
|
|
9
|
+
source = source.toLowerCase()
|
|
10
|
+
if (Array.isArray(search)) {
|
|
11
|
+
search = search.map(s => s.toLowerCase())
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
search = search.toLowerCase()
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
8
18
|
if (Array.isArray(search)) {
|
|
9
19
|
for (const s of search) {
|
|
10
20
|
if (source.indexOf(s) >= 0) return true
|
|
@@ -14,6 +24,10 @@ export const contains = (source: string, search: string | string[]): boolean =>
|
|
|
14
24
|
return source.indexOf(search) >= 0
|
|
15
25
|
}
|
|
16
26
|
|
|
27
|
+
export const startsWith = (source: string, search: string): boolean => {
|
|
28
|
+
return source.substr(0, search.length) === search
|
|
29
|
+
}
|
|
30
|
+
|
|
17
31
|
export const remove = (source: string, search: string): string => {
|
|
18
32
|
return source.replace(search, "")
|
|
19
33
|
}
|