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.tsindex 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 = nulldom_current_step_element: HTMLElement | null = nulldom_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 withthis.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.tsindex 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 = falseconstructor () {- 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.tsindex 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.tsindex 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.tsindex 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 itexport 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.tsindex 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 } fexport 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 = falsediff --git a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts b/src/lib/processes/load-skeleton/StepLoadSkeleton.tsindex 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 itexport 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.Humanprivate hand_skeleton_t: HandSkeletonType = HandSkeletonType.AllFingersdiff --git a/src/lib/processes/weight-skin/StepWeightSkin.ts b/src/lib/processes/weight-skin/StepWeightSkin.tsindex 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 itexport class StepWeightSkin extends EventTarget {- private readonly ui: UI = new UI()+ private readonly ui: UI = UI.getInstance()private skinning_armature: Object3D | undefinedprivate bone_skinning_formula: AbstractAutoSkinSolver | undefinedprivate binding_skeleton: Skeleton | undefineddiff --git a/src/script.ts b/src/script.tsindex 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/usepublic 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()