Files
KSP-MGA-Planner/dist/main/editor/integer-input.js
Krafpy 8da935b047 2D physics fix and error handling consistency
- Reimplemented 2D orbital mechanics functions in physics-2d.ts
and encaspulated inside a `Physics2D` namespace.
- Deleted physics.ts and moved the required functions into physics-3d.ts
Further cleaning and reimplemntation will be done on the physics-3d.ts
content.
- Forced consistency on error handling : every raised error throws
an `Error` object.
- Commented sections relative to 2D physics and sequence generation.
2021-12-19 17:19:30 +01:00

38 lines
1.0 KiB
JavaScript

export class IntegerInput {
constructor(id) {
this.element = document.getElementById(id);
this.min = parseInt(this.element.min);
this.max = parseInt(this.element.max);
this.element.onchange = () => this.validate();
}
get value() {
return parseInt(this.element.value);
}
set value(num) {
this.element.value = num.toString();
}
validate() {
let num = parseInt(this.element.value);
if (!isNaN(num)) {
num = this._clamp(num);
this.element.value = num.toString();
return true;
}
return false;
}
_clamp(num) {
if (!isNaN(this.min) && !isNaN(this.max))
return THREE.MathUtils.clamp(num, this.min, this.max);
return num;
}
assertValidity() {
if (!this.validate())
throw new Error("Invalid numeric input.");
}
setMinMax(min, max) {
this.min = min;
this.max = max;
this.value = this._clamp(this.value);
}
}