Beginning transition to TypeScript

This commit is contained in:
Lucas Dower 2021-08-02 21:52:58 +01:00
parent 3b18c28b9d
commit 35c399932b
9 changed files with 118 additions and 101 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
package-lock.json
ObjToSchematic-win32-x64
/resources/*.obj
/resources/*.mtl
/resources/*.mtl
/dist

View File

@ -66,5 +66,5 @@
<script src="./src/vendor/jquery-3.3.1.slim.min.js"></script>
<script src="./src/vendor/popper.min.js"></script>
<script src="./src/vendor/bootstrap.min.js"></script>
<script type="module" src="./src/client.js"></script>
<script type="module" src="./dist/client.js"></script>
</html>

View File

@ -2,13 +2,14 @@
"name": "objtoschematic",
"version": "0.2.0",
"description": "A tool to convert .obj files into voxels and then into Minecraft Schematic files",
"main": "main.js",
"main": "./dist/main.js",
"engines": {
"node": ">=14.0.0"
},
"scripts": {
"start": "electron .",
"build": "electron-packager . ObjToSchematic --platform=win32 --arch=x64"
"build": "tsc",
"start": "npm run build && electron ./dist/main.js",
"export": "electron-packager . ObjToSchematic --platform=win32 --arch=x64"
},
"repository": {
"type": "git",
@ -23,11 +24,13 @@
"devDependencies": {
"electron": "^13.1.4",
"electron-packager": "^15.2.0",
"twgl.js": "^4.19.1",
"wavefront-obj-parser": "^2.0.1",
"expand-vertex-data": "^1.1.2",
"pngjs": "^6.0.0",
"prismarine-nbt": "^1.6.0",
"pngjs": "^6.0.0"
"ts-node": "^10.1.0",
"twgl.js": "^4.19.1",
"typescript": "^4.3.5",
"wavefront-obj-parser": "^2.0.1"
},
"dependencies": {
"twgl.js": "^4.19.1",

View File

@ -1,6 +1,6 @@
const twgl = require('twgl.js');
const fs = require('fs');
const { Vector3 } = require('./vector');
const { Vector3 } = require('./vector.js');
const { HashMap } = require('./hash_map.js');
class BlockAtlas {

View File

@ -1,4 +1,4 @@
const { AppContext } = require('./src/app_context.js');
const { AppContext } = require('./dist/app_context.js');
const context = new AppContext();

View File

@ -1,61 +0,0 @@
const electron = require('electron');
const fs = require('fs');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const path = require('path');
const url = require('url');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
function createWindow () {
// Create the browser window.
const {width, height} = electron.screen.getPrimaryDisplay().workAreaSize;
mainWindow = new BrowserWindow({width, height, webPreferences: {
nodeIntegration: true,
contextIsolation: false
}});
// Load index.html
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, '../index.html'),
protocol: 'file:',
slashes: true
}));
// Open the DevTools.
//mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', function () {
mainWindow = null;
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});

View File

@ -1,15 +1,10 @@
const electron = require('electron');
//const fs = require('fs');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const path = require('path');
const url = require('url');
import { app, BrowserWindow, inAppPurchase } from "electron";
import * as path from "path";
import * as url from "url";
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let mainWindow: BrowserWindow;
function createWindow () {
// Create the browser window.
@ -24,7 +19,7 @@ function createWindow () {
// Load index.html
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, './index.html'),
pathname: path.join(__dirname, '../index.html'),
protocol: 'file:',
slashes: true
}));
@ -34,7 +29,7 @@ function createWindow () {
// Emitted when the window is closed.
mainWindow.on('closed', function () {
mainWindow = null;
app.quit();
});
}

View File

@ -1,6 +1,10 @@
class Vector3 {
export class Vector3 {
constructor(x, y, z) {
x: number;
y: number;
z: number;
constructor(x: number, y: number, z: number) {
this.x = x;
this.y = y;
this.z = z;
@ -10,7 +14,7 @@ class Vector3 {
return [this.x, this.y, this.z];
}
static add(vecA, vecB) {
static add(vecA: Vector3, vecB: Vector3) {
return new Vector3(
vecA.x + vecB.x,
vecA.y + vecB.y,
@ -18,7 +22,7 @@ class Vector3 {
);
}
static addScalar(vec, scalar) {
static addScalar(vec: Vector3, scalar: number) {
return new Vector3(
vec.x + scalar,
vec.y + scalar,
@ -26,7 +30,7 @@ class Vector3 {
);
}
static sub(vecA, vecB) {
static sub(vecA: Vector3, vecB: Vector3) {
return new Vector3(
vecA.x - vecB.x,
vecA.y - vecB.y,
@ -34,7 +38,7 @@ class Vector3 {
);
}
static subScalar(vec, scalar) {
static subScalar(vec: Vector3, scalar: number) {
return new Vector3(
vec.x - scalar,
vec.y - scalar,
@ -42,11 +46,11 @@ class Vector3 {
);
}
static dot(vecA, vecB) {
static dot(vecA: Vector3, vecB: Vector3) {
return vecA.x * vecB.x + vecA.y * vecB.y + vecA.z * vecB.z;
}
static copy(vec) {
static copy(vec: Vector3) {
return new Vector3(
vec.x,
vec.y,
@ -54,7 +58,7 @@ class Vector3 {
);
}
static mulScalar(vec, scalar) {
static mulScalar(vec: Vector3, scalar: number) {
return new Vector3(
scalar * vec.x,
scalar * vec.y,
@ -62,7 +66,7 @@ class Vector3 {
);
}
static divScalar(vec, scalar) {
static divScalar(vec: Vector3, scalar: number) {
return new Vector3(
vec.x / scalar,
vec.y / scalar,
@ -70,11 +74,11 @@ class Vector3 {
);
}
static lessThanEqualTo(vecA, vecB) {
static lessThanEqualTo(vecA: Vector3, vecB: Vector3) {
return vecA.x <= vecB.x && vecA.y <= vecB.y && vecA.z <= vecB.z;
}
static round(vec) {
static round(vec: Vector3) {
return new Vector3(
Math.round(vec.x),
Math.round(vec.y),
@ -82,7 +86,7 @@ class Vector3 {
);
}
static abs(vec) {
static abs(vec: Vector3) {
return new Vector3(
Math.abs(vec.x),
Math.abs(vec.y),
@ -90,7 +94,7 @@ class Vector3 {
);
}
static cross(vecA, vecB) {
static cross(vecA: Vector3, vecB: Vector3) {
return new Vector3(
vecA.y * vecB.z - vecA.z * vecB.y,
vecA.z * vecB.x - vecA.x * vecB.z,
@ -105,7 +109,7 @@ class Vector3 {
return (this.x * p0) ^ (this.y * p1) ^ (this.z * p2);
}
equals(vec) {
equals(vec: Vector3) {
return this.x == vec.x && this.y == vec.y && this.z == vec.z;
}
@ -121,7 +125,5 @@ class Vector3 {
return this;
}
}
module.exports.Vector3 = Vector3;
}

77
tsconfig.json Normal file
View File

@ -0,0 +1,77 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": ".", /* Base directory to resolve non-absolute module names. */
"paths": {
"*": ["node_modules/*"]
}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": [
"src/**/*"
]
}