lunchboxjs 0.1.4016 → 0.2.1001-beta.0

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.
@@ -1,203 +1,16 @@
1
- import { allNodes, createNode, isMinidomNode, MiniDom } from '.'
2
- import { setupAutoRaycaster } from './interaction/setupAutoRaycaster'
3
- import { computed, reactive, ref, WritableComputedRef } from 'vue'
4
- import { Lunch } from '..'
1
+ import { ComputedRef, inject } from 'vue'
2
+ import * as Keys from '../keys'
5
3
 
6
- // ENSURE ROOT
7
- // ====================
8
- export const rootUuid = 'LUNCHBOX_ROOT'
9
- export let lunchboxRootNode: MiniDom.RendererRootNode
10
- export function ensureRootNode(options: Partial<Lunch.RootMeta> = {}) {
11
- if (!lunchboxRootNode) {
12
- lunchboxRootNode = new MiniDom.RendererRootNode(options)
13
- }
14
- return lunchboxRootNode
15
- }
16
-
17
- // This is used in `buildEnsured` below and `LunchboxWrapper`
18
- /** Search the overrides record and the node tree for a node in the given types */
19
- export function tryGetNodeWithInstanceType<T extends THREE.Object3D>(
20
- pascalCaseTypes: string | string[]
21
- ) {
22
- if (!Array.isArray(pascalCaseTypes)) {
23
- pascalCaseTypes = [pascalCaseTypes]
24
- }
25
-
26
- // default to override if we have one
27
- for (let singleType of pascalCaseTypes) {
28
- if (overrides[singleType]) return overrides[singleType] as Lunch.Node<T>
29
- }
30
-
31
- // look for auto-created node
32
- for (let singleType of pascalCaseTypes) {
33
- const found =
34
- autoCreated[singleType] ||
35
- allNodes.find(
36
- (node) =>
37
- (node as MiniDom.RendererBaseNode).type?.toLowerCase() ===
38
- singleType.toLowerCase()
39
- )
40
-
41
- // cancel if found example is marked !isDefault
42
- if (
43
- isMinidomNode(found) &&
44
- (found.props['is-default'] === false ||
45
- !found.props['isDefault'] === false)
46
- ) {
47
- return null
48
- }
49
-
50
- // if we have one, save and return
51
- if (found) {
52
- const createdAsNode = found as MiniDom.RendererStandardNode<T>
53
- autoCreated[singleType] = createdAsNode
54
- return createdAsNode
55
- }
56
- }
57
-
58
- return null
59
- }
60
-
61
- // GENERIC ENSURE FUNCTION
62
- // ====================
63
- // Problem:
64
- // I want to make sure an object of type Xyz exists in my Lunchbox app.
65
- // If it doesn't exist, I want to create it and add it to the root node.
66
- //
67
- // Solution:
68
- // export const ensuredXyz = buildEnsured<Xyz>('Xyz', 'FALLBACK_XYZ')
69
- //
70
- // Now in other components, you can do both:
71
- // import { ensuredXyz }
72
- // ensuredXyz.value (...)
73
- // and:
74
- // ensuredXyz.value = ...
75
- export const autoCreated: Record<string, Lunch.Node | null> = reactive({})
76
- export const overrides: Record<string, Lunch.Node | null> = reactive({})
77
-
78
- /**
79
- * Build a computed ensured value with a getter and setter.
80
- * @param pascalCaseTypes List of types this can be. Will autocreate first type if array provided.
81
- * @param fallbackUuid Fallback UUID to use.
82
- * @param props Props to pass to autocreated element
83
- * @returns Computed getter/setter for ensured object.
84
- */
85
- function buildEnsured<T extends THREE.Object3D>(
86
- pascalCaseTypes: string | string[],
87
- fallbackUuid: string,
88
- props: Record<string, any> = {},
89
- callback: ((node: MiniDom.RendererStandardNode<T>) => void) | null = null
90
- ) {
91
- // make sure we've got an array
92
- if (!Array.isArray(pascalCaseTypes)) {
93
- pascalCaseTypes = [pascalCaseTypes]
94
- }
95
-
96
- // add type for autoCreated and overrides
97
- for (let singleType of pascalCaseTypes) {
98
- if (!autoCreated[singleType]) {
99
- autoCreated[singleType] = null
100
- }
101
- if (!overrides[singleType]) {
102
- overrides[singleType] = null
103
- }
104
- }
105
-
106
- return computed({
107
- get(): MiniDom.RendererStandardNode<T> {
108
- // try to get existing type
109
- const existing = tryGetNodeWithInstanceType<T>(
110
- pascalCaseTypes as string[]
111
- )
112
- if (existing) return existing
113
-
114
- // otherwise, create a new node
115
- const root = ensureRootNode()
116
- const node = createNode<T>({
117
- type: pascalCaseTypes[0],
118
- uuid: fallbackUuid,
119
- props,
120
- })
121
- root.addChild(node)
122
- autoCreated[pascalCaseTypes[0]] = node
123
- if (callback) {
124
- callback(node)
125
- }
126
- return node
127
- },
128
- set(val: MiniDom.RendererStandardNode<T>) {
129
- const t = val.type ?? ''
130
- const pascalType = t[0].toUpperCase() + t.slice(1)
131
- overrides[pascalType] = val
132
- },
133
- })
134
- }
135
-
136
- // ENSURE CAMERA
137
- // ====================
138
- export const fallbackCameraUuid = 'FALLBACK_CAMERA'
139
- export const defaultCamera = buildEnsured(
140
- ['PerspectiveCamera', 'OrthographicCamera'],
141
- fallbackCameraUuid,
142
- { args: [45, 0.5625, 1, 1000] }
143
- ) as unknown as WritableComputedRef<Lunch.Node<THREE.Camera>>
144
- /** Special value to be changed ONLY in `LunchboxWrapper`.
145
- * Functions waiting for a Camera need to wait for this to be true. */
146
- export const cameraReady = ref(false)
147
-
148
- export const ensuredCamera = computed<Lunch.Node<THREE.Camera> | null>({
149
- get() {
150
- return (
151
- cameraReady.value ? (defaultCamera.value as any) : (null as any)
152
- ) as any
153
- },
154
- set(val: any) {
155
- const t = val.type ?? ''
156
- const pascalType = t[0].toUpperCase() + t.slice(1)
157
- overrides[pascalType] = val as any
158
- },
159
- })
4
+ export const ensuredCamera = <T extends THREE.Camera = THREE.Camera>() =>
5
+ inject<ComputedRef<T>>(Keys.appCameraKey)!
160
6
 
161
7
  // ENSURE RENDERER
162
8
  // ====================
163
- export const fallbackRendererUuid = 'FALLBACK_RENDERER'
164
- export const ensuredRenderer = buildEnsured(
165
- // TODO: ensure support for css/svg renderers
166
- ['WebGLRenderer'], //, 'CSS2DRenderer', 'CSS3DRenderer', 'SVGRenderer'],
167
- fallbackRendererUuid,
168
- {}
169
- ) as unknown as WritableComputedRef<Lunch.Node<THREE.WebGLRenderer>>
170
- /** Special value to be changed ONLY in `LunchboxWrapper`.
171
- * Functions waiting for a Renderer need to wait for this to be true. */
172
- export const rendererReady = ref(false)
173
-
174
- export const ensureRenderer = computed<Lunch.Node<THREE.WebGLRenderer> | null>({
175
- get() {
176
- return (
177
- rendererReady.value ? (ensuredRenderer.value as any) : (null as any)
178
- ) as any
179
- },
180
- set(val: any) {
181
- const t = val.type ?? ''
182
- const pascalType = t[0].toUpperCase() + t.slice(1)
183
- overrides[pascalType] = val as any
184
- },
185
- })
9
+ export const ensureRenderer = <
10
+ T extends THREE.Renderer = THREE.WebGLRenderer
11
+ >() => inject<ComputedRef<T>>(Keys.appRenderersKey)
186
12
 
187
13
  // ENSURE SCENE
188
14
  // ====================
189
- export const fallbackSceneUuid = 'FALLBACK_SCENE'
190
- export const ensuredScene = buildEnsured<THREE.Scene>(
191
- 'Scene',
192
- fallbackSceneUuid
193
- )
194
-
195
- // ENSURE AUTO-RAYCASTER
196
- export const autoRaycasterUuid = 'AUTO_RAYCASTER'
197
- // `unknown` is intentional here - we need to typecast the node since Raycaster isn't an Object3D
198
- export const ensuredRaycaster = buildEnsured(
199
- 'Raycaster',
200
- autoRaycasterUuid,
201
- {},
202
- (node) => setupAutoRaycaster(node as unknown as Lunch.Node<THREE.Raycaster>)
203
- ) as unknown as WritableComputedRef<Lunch.Node<THREE.Raycaster>>
15
+ export const ensuredScene = <T extends THREE.Scene = THREE.Scene>() =>
16
+ inject<ComputedRef<T>>(Keys.appSceneKey)
package/src/core/index.ts CHANGED
@@ -1,10 +1,8 @@
1
- export * from './allNodes'
2
1
  export * from './createNode'
3
2
  export * from './ensure'
4
3
  export * from './interaction'
5
4
  export * from './extend'
6
5
  export * from './instantiateThreeObject'
7
6
  export * from './minidom'
8
- export * from './start'
9
7
  export * from './update'
10
8
  export * from './updateObjectProp'
@@ -8,7 +8,12 @@ export function instantiateThreeObject<T>(node: Lunch.StandardMeta<T>) {
8
8
 
9
9
  // what class will we be instantiating?
10
10
  const uppercaseType = node.type[0].toUpperCase() + node.type.slice(1)
11
- const targetClass = catalogue[node.type] || (THREE as any)[uppercaseType]
11
+ const translatedType = uppercaseType.replace(/Lunchbox$/, '')
12
+ const targetClass =
13
+ catalogue[node.type] ||
14
+ (THREE as any)[uppercaseType] ||
15
+ catalogue[translatedType] ||
16
+ (THREE as any)[translatedType]
12
17
  if (!targetClass)
13
18
  throw `${uppercaseType} is not part of the THREE namespace! Did you forget to extend? import {extend} from 'lunchbox'; extend({app, YourComponent, ...})`
14
19
 
@@ -4,7 +4,7 @@ import {
4
4
  interactables,
5
5
  removeInteractable,
6
6
  } from './interactables'
7
- import { ensuredRaycaster } from '..'
7
+ // import { ensuredRaycaster } from '..'
8
8
  import { inputActive } from './input'
9
9
  import { currentIntersections } from '.'
10
10
  import { Lunch } from '../..'
@@ -38,7 +38,7 @@ export function addEventListener({
38
38
  if (interactionsRequiringRaycaster.includes(key)) {
39
39
  // we're not using `v` here, we're just making sure the raycaster has been created
40
40
  // TODO: is this necessary?
41
- const v = ensuredRaycaster.value
41
+ // const v = ensuredRaycaster.value
42
42
 
43
43
  if (node.instance && !interactables.includes(node)) {
44
44
  addInteractable(node)