One way to safely use ES6+ is using TypeScript(Babel might be one another), I think it is not hard as people think.
To enable typescript in your project is simple:
1. Download and install NodeJS
2. Install typescript globally by running: `npm i -g typescript`
3. Go to your project root folder and run: tsc -init
4. Config the newly generated tsconfig.json, well documented already.
5. run: tsc -w, and you will see output js file everytime you changed your ts file
Here is an example of a simple draggable plugin(or utils to be exactly)
I put the declaration files under the same folder too(not all the declaration are showing here, but you get the idea)
And below is what will be the outputs
if your are targeting ES5, the outpu will like this:
Or your prefer to have the most fancy JS so far, we have ESNext:
All this can be done with Babel too, but do you think that some types might make it easy for others to understand your code?
To enable typescript in your project is simple:
1. Download and install NodeJS
2. Install typescript globally by running: `npm i -g typescript`
3. Go to your project root folder and run: tsc -init
4. Config the newly generated tsconfig.json, well documented already.
5. run: tsc -w, and you will see output js file everytime you changed your ts file
Here is an example of a simple draggable plugin(or utils to be exactly)
JavaScript:
const setNullDefault = <T extends any>(value: Maybe<T>, defaultValue: T) =>
value === null ? defaultValue : value;
/**
* Use this global variable to track the dragging window
*/
let currentDraggable: Maybe<Window_Draggable> = null;
/**
* Make a new draggable window class based on a existing one
* @param WindowClass Based Window Class
* @usage
* const Window_Draggable = useDraggable(Window_Base)
* const myWin = new Window_Draggable(100, 100, 100, 100)
* myWin.setDraggable(true)
*/
const useDraggable = (WindowClass: typeof Window_Base, rect?: Rectangle): typeof Window_Base =>
class extends WindowClass implements Window_Draggable {
private _dragX: Maybe<number> = null;
private _dragY: Maybe<number> = null;
private _dragging: boolean = false;
private _isDraggable: boolean = false;
private _rect: Rectangle;
get dragableRect() {
const { bottom, top, left, right } = this._rect;
return new Rectangle(this.x + left, this.y + top, right, bottom);
}
get isDraggable() {
return this._isDraggable;
}
constructor(...args: any[]) {
super(...args);
this._rect = rect || new Rectangle(0, 0, this.width, this.height);
}
/**
* set the region of where the window can be dragged
* default to whole
* @param newRect new handle region
*/
resetRect(newRect?: Rectangle) {
this._rect = newRect || new Rectangle(0, 0, this.width, this.height);
}
/**
* update window position
*/
updateDrag() {
const isInside = this.dragableRect.contains(TouchInput.x, TouchInput.y);
const isTriggered = TouchInput.isTriggered();
const isReleased = TouchInput.isReleased();
const isPressed = TouchInput.isPressed();
const draggingWin = currentDraggable;
if (isTriggered && isInside) {
this._dragX = this.canvasToLocalX(TouchInput.x);
this._dragY = this.canvasToLocalY(TouchInput.y);
this._dragging = true;
const list = SceneManager._scene._windowLayer.children;
list.sort((a, b) => Number(a === this) - Number(b === this));
currentDraggable = this;
} else if (isPressed && this._dragging && draggingWin === this) {
this.x = TouchInput.x - setNullDefault(this._dragX, 0);
this.y = TouchInput.y - setNullDefault(this._dragY, 0);
} else if (isReleased) {
this._dragging = false;
}
}
/**
* Change if the window is draggable
* @param flag set true if the window can be dragged
*/
setDraggable(flag: boolean) {
this._isDraggable = flag;
}
/**
* override life cycle
*/
update() {
super.update();
if (this.isDraggable) this.updateDrag();
}
};
I put the declaration files under the same folder too(not all the declaration are showing here, but you get the idea)
JavaScript:
declare class Window_Draggable {
dragableRect: Rectangle;
isDraggable: boolean;
updateDrag(): void;
resetRect(newRect?: Rectangle): void;
setDraggable(flag: boolean): void;
}
And below is what will be the outputs
if your are targeting ES5, the outpu will like this:
JavaScript:
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var setNullDefault = function (value, defaultValue) {
return value === null ? defaultValue : value;
};
var currentDraggable = null;
var useDraggable = function (WindowClass, rect) {
return (function (_super) {
__extends(class_1, _super);
function class_1() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var _this = _super.apply(this, args) || this;
_this._dragX = null;
_this._dragY = null;
_this._dragging = false;
_this._isDraggable = false;
_this._rect = rect || new Rectangle(0, 0, _this.width, _this.height);
return _this;
}
Object.defineProperty(class_1.prototype, "dragableRect", {
get: function () {
var _a = this._rect, bottom = _a.bottom, top = _a.top, left = _a.left, right = _a.right;
return new Rectangle(this.x + left, this.y + top, right, bottom);
},
enumerable: false,
configurable: true
});
Object.defineProperty(class_1.prototype, "isDraggable", {
get: function () {
return this._isDraggable;
},
enumerable: false,
configurable: true
});
class_1.prototype.resetRect = function (newRect) {
this._rect = newRect || new Rectangle(0, 0, this.width, this.height);
};
class_1.prototype.updateDrag = function () {
var _this = this;
var isInside = this.dragableRect.contains(TouchInput.x, TouchInput.y);
var isTriggered = TouchInput.isTriggered();
var isReleased = TouchInput.isReleased();
var isPressed = TouchInput.isPressed();
var draggingWin = currentDraggable;
if (isTriggered && isInside) {
this._dragX = this.canvasToLocalX(TouchInput.x);
this._dragY = this.canvasToLocalY(TouchInput.y);
this._dragging = true;
var list = SceneManager._scene._windowLayer.children;
list.sort(function (a, b) { return Number(a === _this) - Number(b === _this); });
currentDraggable = this;
}
else if (isPressed && this._dragging && draggingWin === this) {
this.x = TouchInput.x - setNullDefault(this._dragX, 0);
this.y = TouchInput.y - setNullDefault(this._dragY, 0);
}
else if (isReleased) {
this._dragging = false;
}
};
class_1.prototype.setDraggable = function (flag) {
this._isDraggable = flag;
};
class_1.prototype.update = function () {
_super.prototype.update.call(this);
if (this.isDraggable)
this.updateDrag();
};
return class_1;
}(WindowClass));
};
Or your prefer to have the most fancy JS so far, we have ESNext:
JavaScript:
"use strict";
const setNullDefault = (value, defaultValue) => value === null ? defaultValue : value;
let currentDraggable = null;
const useDraggable = (WindowClass, rect) => class extends WindowClass {
constructor(...args) {
super(...args);
this._dragX = null;
this._dragY = null;
this._dragging = false;
this._isDraggable = false;
this._rect = rect || new Rectangle(0, 0, this.width, this.height);
}
get dragableRect() {
const { bottom, top, left, right } = this._rect;
return new Rectangle(this.x + left, this.y + top, right, bottom);
}
get isDraggable() {
return this._isDraggable;
}
resetRect(newRect) {
this._rect = newRect || new Rectangle(0, 0, this.width, this.height);
}
updateDrag() {
const isInside = this.dragableRect.contains(TouchInput.x, TouchInput.y);
const isTriggered = TouchInput.isTriggered();
const isReleased = TouchInput.isReleased();
const isPressed = TouchInput.isPressed();
const draggingWin = currentDraggable;
if (isTriggered && isInside) {
this._dragX = this.canvasToLocalX(TouchInput.x);
this._dragY = this.canvasToLocalY(TouchInput.y);
this._dragging = true;
const list = SceneManager._scene._windowLayer.children;
list.sort((a, b) => Number(a === this) - Number(b === this));
currentDraggable = this;
}
else if (isPressed && this._dragging && draggingWin === this) {
this.x = TouchInput.x - setNullDefault(this._dragX, 0);
this.y = TouchInput.y - setNullDefault(this._dragY, 0);
}
else if (isReleased) {
this._dragging = false;
}
}
setDraggable(flag) {
this._isDraggable = flag;
}
update() {
super.update();
if (this.isDraggable)
this.updateDrag();
}
};
All this can be done with Babel too, but do you think that some types might make it easy for others to understand your code?