Updated file importing to support non-obj files

This commit is contained in:
Lucas Dower 2022-04-23 20:24:21 +01:00
parent 9d760b1b84
commit 1a7c33b9b1
5 changed files with 31 additions and 12 deletions

View File

@ -18,6 +18,8 @@ import { StatusHandler } from './status';
import { UIMessageBuilder } from './ui/misc';
import { OutputStyle } from './ui/elements/output';
import path from 'path';
/* eslint-disable */
export enum EAction {
Import = 0,
@ -137,11 +139,19 @@ export class AppContext {
const uiElements = this._ui.layout.import.elements;
const filePath = uiElements.input.getCachedValue();
const importer = new ObjImporter();
importer.parseFile(filePath);
this._loadedMesh = importer.toMesh();
this._loadedMesh.processMesh();
Renderer.Get.useMesh(this._loadedMesh);
const parsedPath = path.parse(filePath);
const importers = [new ObjImporter()];
for (const importer of importers) {
if (importer.supports(parsedPath.ext.toLowerCase())) {
this._loadedMesh = importer.toMesh();
this._loadedMesh.processMesh();
Renderer.Get.useMesh(this._loadedMesh);
return;
}
}
throw new AppError(`Could not load file-type '${parsedPath.ext.toLowerCase()}' when loading ${parsedPath.base}`);
}
private _simplify() {

View File

@ -1,6 +1,11 @@
import { Mesh } from './mesh';
export abstract class IImporter {
abstract parseFile(filePath: string): void;
abstract toMesh(): Mesh;
public abstract parseFile(filePath: string): void;
public abstract toMesh(): Mesh;
public supports(fileExtension: string) {
return this.getSupportedFileExtensions().includes(fileExtension);
}
protected abstract getSupportedFileExtensions(): string[];
}

View File

@ -363,4 +363,8 @@ export class ObjImporter extends IImporter {
}
}
}
public override getSupportedFileExtensions(): string[] {
return ['.obj'];
}
}

View File

@ -5,12 +5,12 @@ import { remote } from 'electron';
import * as path from 'path';
export class FileInputElement extends LabelledElement<string> {
private _fileExtension: string;
private _fileExtensions: string[];
private _loadedFilePath: string;
public constructor(label: string, fileExtension: string) {
public constructor(label: string, fileExtensions: string[]) {
super(label);
this._fileExtension = fileExtension;
this._fileExtensions = fileExtensions;
this._loadedFilePath = '';
}
@ -36,7 +36,7 @@ export class FileInputElement extends LabelledElement<string> {
buttonLabel: 'Load',
filters: [{
name: 'Waveform obj file',
extensions: [`${this._fileExtension}`],
extensions: this._fileExtensions,
}],
});
if (files && files.length === 1) {

View File

@ -34,7 +34,7 @@ export class UI {
'import': {
label: 'Import',
elements: {
'input': new FileInputElement('Wavefront .obj file', 'obj'),
'input': new FileInputElement('Model file', ['obj', 'fbx']),
},
elementsOrder: ['input'],
submitButton: new ButtonElement('Load mesh', () => {