Needle Engine

Changes between version 3.5.10-beta and 3.5.11-beta
Files changed (7) hide show
  1. plugins/vite/reload.js +4 -4
  2. src/engine/api.ts +1 -1
  3. src/engine-components/Component.ts +16 -13
  4. src/engine/engine_hot_reload.ts +5 -5
  5. src/engine/engine_license.ts +1 -0
  6. src/engine/engine_typestore.ts +14 -1
  7. src/engine-components/TransformGizmo.ts +1 -1
plugins/vite/reload.js CHANGED
@@ -274,7 +274,7 @@
274
274
  // this code injects a register call into the component method
275
275
  const code = `
276
276
 
277
- import { register, unregister } from "./engine/engine_hot_reload";
277
+ import { registerHotReloadType as register, unregisterHotReloadType as unregister } from "./engine/engine_hot_reload";
278
278
  import { Component as ComponentType } from "./engine-components/Component";
279
279
 
280
280
  const prototype = ComponentType.prototype;
@@ -334,14 +334,14 @@
334
334
 
335
335
  // ${HOT_RELOAD_START_MARKER}
336
336
  // Inserted by needle reload plugin (vite)
337
- import { applyChanges } from "${importPath}";
337
+ import { applyHMRChanges } from "${importPath}";
338
338
  //@ts-ignore
339
339
  if (import.meta.hot) {
340
340
  //@ts-ignore
341
- import.meta.hot.accept((newModule) => {
341
+ import.meta.hot.accept((newModule, oldModule) => {
342
342
  if (newModule) {
343
343
 
344
- const success = applyChanges(newModule);
344
+ const success = applyHMRChanges(newModule, oldModule);
345
345
  if(success === false){
346
346
  //@ts-ignore
347
347
  import.meta.hot.invalidate()
src/engine/api.ts CHANGED
@@ -44,7 +44,7 @@
44
44
  export * from "./engine_web_api";
45
45
  export * from "./engine_utils";
46
46
 
47
- export { TypeStore } from "./engine_typestore";
47
+ export { TypeStore, registerType } from "./engine_typestore";
48
48
 
49
49
  export { InstancingUtil } from "./engine_instancing";
50
50
  export { validate, prefix } from "./engine_util_decorator"
src/engine-components/Component.ts CHANGED
@@ -185,7 +185,7 @@
185
185
  * @param go component to move the component to
186
186
  * @param instance component to move to the GO
187
187
  */
188
- public static addComponent(go: IGameObject | Object3D, instance: Component): void {
188
+ public static addComponent<T extends IComponent>(go: IGameObject | Object3D, instance: T) {
189
189
  return this.moveComponent(go, instance);
190
190
  }
191
191
 
@@ -194,24 +194,24 @@
194
194
  * @param go component to move the component to
195
195
  * @param instance component to move to the GO
196
196
  */
197
- public static moveComponent(go: IGameObject | Object3D, instance: Component): void {
198
- moveComponentInstance(go, instance as any);
197
+ public static moveComponent<T extends IComponent>(go: IGameObject | Object3D, instance: T) {
198
+ return moveComponentInstance(go, instance);
199
199
  }
200
200
 
201
201
  /** Removes a component from its object
202
202
  * @param instance component to remove
203
203
  */
204
- public static removeComponent(instance: Component): Component {
204
+ public static removeComponent<T extends IComponent>(instance: T): T {
205
205
  removeComponent(instance.gameObject, instance as any);
206
206
  return instance;
207
207
  }
208
208
 
209
- public static getOrAddComponent<T>(go: IGameObject | Object3D, typeName: ConstructorConcrete<T>): T {
209
+ public static getOrAddComponent<T extends IComponent>(go: IGameObject | Object3D, typeName: ConstructorConcrete<T>): T {
210
210
  return getOrAddComponent<any>(go, typeName);
211
211
  }
212
212
 
213
213
  /** Gets a component on the provided object */
214
- public static getComponent<T>(go: IGameObject | Object3D | null, typeName: Constructor<T> | null): T | null {
214
+ public static getComponent<T extends IComponent>(go: IGameObject | Object3D | null, typeName: Constructor<T> | null): T | null {
215
215
  if (go === null) return null;
216
216
  // if names are minified we could also use the type store and work with strings everywhere
217
217
  // not ideal, but I dont know a good/sane way to do this otherwise
@@ -220,7 +220,7 @@
220
220
  return getComponent(go, typeName as any);
221
221
  }
222
222
 
223
- public static getComponents<T>(go: IGameObject | Object3D | null, typeName: Constructor<T>, arr: T[] | null = null): T[] {
223
+ public static getComponents<T extends IComponent>(go: IGameObject | Object3D | null, typeName: Constructor<T>, arr: T[] | null = null): T[] {
224
224
  if (go === null) return arr ?? [];
225
225
  return getComponents(go, typeName, arr);
226
226
  }
@@ -230,29 +230,29 @@
230
230
  return res as GameObject | Component | null | undefined;
231
231
  }
232
232
 
233
- public static findObjectOfType<T>(typeName: Constructor<T>, context?: Context | Object3D, includeInactive: boolean = true): T | null {
233
+ public static findObjectOfType<T extends IComponent>(typeName: Constructor<T>, context?: Context | Object3D, includeInactive: boolean = true): T | null {
234
234
  return findObjectOfType(typeName, context ?? Context.Current, includeInactive);
235
235
  }
236
236
 
237
- public static findObjectsOfType<T>(typeName: Constructor<T>, context?: Context | Object3D): Array<T> {
237
+ public static findObjectsOfType<T extends IComponent>(typeName: Constructor<T>, context?: Context | Object3D): Array<T> {
238
238
  const arr = [];
239
239
  findObjectsOfType(typeName, arr, context);
240
240
  return arr;
241
241
  }
242
242
 
243
- public static getComponentInChildren<T>(go: IGameObject | Object3D, typeName: Constructor<T>): T | null {
243
+ public static getComponentInChildren<T extends IComponent>(go: IGameObject | Object3D, typeName: Constructor<T>): T | null {
244
244
  return getComponentInChildren(go, typeName);
245
245
  }
246
246
 
247
- public static getComponentsInChildren<T>(go: IGameObject | Object3D, typeName: Constructor<T>, arr: T[] | null = null): Array<T> {
247
+ public static getComponentsInChildren<T extends IComponent>(go: IGameObject | Object3D, typeName: Constructor<T>, arr: T[] | null = null): Array<T> {
248
248
  return getComponentsInChildren<T>(go, typeName, arr ?? undefined) as T[]
249
249
  }
250
250
 
251
- public static getComponentInParent<T>(go: IGameObject | Object3D, typeName: Constructor<T>): T | null {
251
+ public static getComponentInParent<T extends IComponent>(go: IGameObject | Object3D, typeName: Constructor<T>): T | null {
252
252
  return getComponentInParent(go, typeName);
253
253
  }
254
254
 
255
- public static getComponentsInParent<T>(go: IGameObject | Object3D, typeName: Constructor<T>, arr: Array<T> | null = null): Array<T> {
255
+ public static getComponentsInParent<T extends IComponent>(go: IGameObject | Object3D, typeName: Constructor<T>, arr: Array<T> | null = null): Array<T> {
256
256
  return getComponentsInParent(go, typeName, arr);
257
257
  }
258
258
 
@@ -307,6 +307,9 @@
307
307
  }
308
308
 
309
309
  get name(): string {
310
+ if (this.gameObject?.name) {
311
+ return this.gameObject.name;
312
+ }
310
313
  return this.gameObject?.userData.name;
311
314
  }
312
315
  private __name?: string;
src/engine/engine_hot_reload.ts CHANGED
@@ -33,7 +33,7 @@
33
33
  return isApplyingChanges;
34
34
  }
35
35
 
36
- export function register(instance: object) {
36
+ export function registerHotReloadType(instance: object) {
37
37
  if (isApplyingChanges) return;
38
38
  const type = instance.constructor;
39
39
  const name = type.name;
@@ -45,7 +45,7 @@
45
45
  }
46
46
  }
47
47
 
48
- export function unregister(instance: object) {
48
+ export function unregisterHotReloadType(instance: object) {
49
49
  if (isApplyingChanges) return;
50
50
  const type = instance.constructor;
51
51
  const name = type.name;
@@ -81,10 +81,10 @@
81
81
  }
82
82
 
83
83
 
84
- export function applyChanges(newModule): boolean {
84
+ export function applyHMRChanges(newModule): boolean {
85
85
 
86
86
  if (debug)
87
- console.log("Hot Reload - apply changes");
87
+ console.log("[HMR] Apply changes", newModule, Object.keys(newModule));
88
88
 
89
89
  reloadPageOnHotReloadError();
90
90
 
@@ -96,6 +96,7 @@
96
96
 
97
97
  const typeToUpdate = TypeStore.get(key);
98
98
  if (!typeToUpdate) {
99
+ if(debug) console.log("[HMR] Type not found: " + key)
99
100
  continue;
100
101
  }
101
102
  const newType = newModule[key];
@@ -194,4 +195,3 @@
194
195
  return true;
195
196
  }
196
197
 
197
-
src/engine/engine_license.ts CHANGED
@@ -142,6 +142,7 @@
142
142
  font-weight: 300;
143
143
  transition: all 0.1s ease-in-out !important;
144
144
  pointer-events: all;
145
+ overflow: hidden;
145
146
  }
146
147
 
147
148
  ${selector}:hover {
src/engine/engine_typestore.ts CHANGED
@@ -20,4 +20,17 @@
20
20
 
21
21
  export const $BuiltInTypeFlag = Symbol("BuiltInType");
22
22
 
23
- export const TypeStore = new _TypeStore();
23
+ export const TypeStore = new _TypeStore();
24
+
25
+
26
+ /**
27
+ * add to a class declaration to automatically register it to the TypeStore (required for HMR right now)
28
+ *
29
+ * `@registerType`
30
+ *
31
+ * `export class MyType extends Behaviour { ... }`
32
+ */
33
+ export const registerType = function (constructor: Function) {
34
+ if (!TypeStore.get(constructor.name))
35
+ TypeStore.add(constructor.name, constructor);
36
+ }
src/engine-components/TransformGizmo.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  import * as params from "../engine/engine_default_parameters";
5
5
  import { Mesh, MathUtils, EventListener } from "three";
6
6
  import { TransformControls } from "three/examples/jsm/controls/TransformControls";
7
- import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
7
+ import { OrbitControls } from "./OrbitControls";
8
8
 
9
9
  export class TransformGizmo extends Behaviour {