Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Turn UI into a singleton to avoid so many separate instances

Scott Petrovicfa73880f766c
diff --git a/src/lib/UI.ts b/src/lib/UI.ts
index 821c326..aec9ade 100644
--- a/src/lib/UI.ts
+++ b/src/lib/UI.ts
@@ -1,4 +1,6 @@
export class UI {
+ private static instance: UI
+
dom_current_step_index: HTMLElement | null = null
dom_current_step_element: HTMLElement | null = null
dom_load_model_tools: HTMLElement | null = null
@@ -64,11 +66,18 @@ export class UI {
dom_build_version: HTMLElement | null = null
- constructor () {
+ private constructor () {
this.initialize_dom_elements()
// AnimationSearch will be initialized when needed
}
+ public static getInstance (): UI {
+ if (UI.instance === undefined) {
+ UI.instance = new UI()
+ }
+ return UI.instance
+ }
+
private initialize_dom_elements (): void {
// grab all UI Elements from page that we need to interact with
this.dom_current_step_index = document.querySelector('#current-step-index')
diff --git a/src/lib/processes/animations-listing/AnimationPlayer.ts b/src/lib/processes/animations-listing/AnimationPlayer.ts
index 9d4cdfa..c705e36 100644
--- a/src/lib/processes/animations-listing/AnimationPlayer.ts
+++ b/src/lib/processes/animations-listing/AnimationPlayer.ts
@@ -15,7 +15,7 @@ export class AnimationPlayer {
private readonly has_added_event_listeners: boolean = false
constructor () {
- this.ui = new UI()
+ this.ui = UI.getInstance()
if (!this.has_added_event_listeners) {
this.setup_event_listeners()
diff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.ts
index b5f7a84..5769567 100644
--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts
+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts
@@ -92,7 +92,7 @@ export class StepAnimationsListing extends EventTarget {
constructor (theme_manager: ThemeManager) {
super()
- this.ui = new UI()
+ this.ui = UI.getInstance()
this.animation_player = new AnimationPlayer()
this.theme_manager = theme_manager
}
diff --git a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
index c17b96b..b8f4809 100644
--- a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
+++ b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
@@ -38,7 +38,7 @@ export class StepEditSkeleton extends EventTarget {
constructor () {
super()
- this.ui = new UI()
+ this.ui = UI.getInstance()
this.undo_redo_system = new UndoRedoSystem(50) // Store up to 50 undo states
}
diff --git a/src/lib/processes/export-to-file/StepExportToFile.ts b/src/lib/processes/export-to-file/StepExportToFile.ts
index 6d2f0aa..56d2b88 100644
--- a/src/lib/processes/export-to-file/StepExportToFile.ts
+++ b/src/lib/processes/export-to-file/StepExportToFile.ts
@@ -4,7 +4,7 @@ import { type AnimationClip, Scene, type SkinnedMesh, type Object3D } from 'thre
// Note: EventTarget is a built-ininterface and do not need to import it
export class StepExportToFile extends EventTarget {
- private readonly ui: UI = new UI()
+ private readonly ui: UI = UI.getInstance()
private animation_clips_to_export: AnimationClip[] = []
public set_animation_clips_to_export (all_animations_clips: AnimationClip[], animation_checkboxes: number[]): void {
diff --git a/src/lib/processes/load-model/StepLoadModel.ts b/src/lib/processes/load-model/StepLoadModel.ts
index cabbc79..e59d5b6 100644
--- a/src/lib/processes/load-model/StepLoadModel.ts
+++ b/src/lib/processes/load-model/StepLoadModel.ts
@@ -14,7 +14,7 @@ import { type BufferGeometry, type Material, type Object3D, type SkinnedMesh } f
export class StepLoadModel extends EventTarget {
private readonly gltf_loader = new GLTFLoader()
private readonly fbx_loader = new FBXLoader()
- private readonly ui: UI = new UI()
+ private readonly ui: UI = UI.getInstance()
private original_model_data: Scene = new Scene()
private final_mesh_data: Scene = new Scene()
private debug_model_loading: boolean = false
diff --git a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
index 81c76ea..1f745b0 100644
--- a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
+++ b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
@@ -15,7 +15,7 @@ interface GLTFResult {
// Note: EventTarget is a built-ininterface and do not need to import it
export class StepLoadSkeleton extends EventTarget {
private readonly loader: GLTFLoader = new GLTFLoader()
- private readonly ui: UI = new UI()
+ private readonly ui: UI = UI.getInstance()
private loaded_armature: Object3D = new Object3D()
private skeleton_t: SkeletonType = SkeletonType.Human
private hand_skeleton_t: HandSkeletonType = HandSkeletonType.AllFingers
diff --git a/src/lib/processes/weight-skin/StepWeightSkin.ts b/src/lib/processes/weight-skin/StepWeightSkin.ts
index dd9d1a4..2a4eebb 100644
--- a/src/lib/processes/weight-skin/StepWeightSkin.ts
+++ b/src/lib/processes/weight-skin/StepWeightSkin.ts
@@ -16,7 +16,7 @@ import { type AbstractAutoSkinSolver } from '../../solvers/AbstractAutoSkinSolve
// Note: EventTarget is a built-ininterface and do not need to import it
export class StepWeightSkin extends EventTarget {
- private readonly ui: UI = new UI()
+ private readonly ui: UI = UI.getInstance()
private skinning_armature: Object3D | undefined
private bone_skinning_formula: AbstractAutoSkinSolver | undefined
private binding_skeleton: Skeleton | undefined
diff --git a/src/script.ts b/src/script.ts
index 2b080fa..0afa6c1 100644
--- a/src/script.ts
+++ b/src/script.ts
@@ -41,7 +41,7 @@ export class Bootstrap {
// has UI elements on the HTML page that we will reference/use
public readonly theme_manager = new ThemeManager()
- public readonly ui = new UI()
+ public readonly ui = UI.getInstance()
public readonly load_model_step = new StepLoadModel()
public readonly load_skeleton_step = new StepLoadSkeleton()
public readonly edit_skeleton_step = new StepEditSkeleton()