@lhs7/wheel-selector
Version:
90 lines (89 loc) • 3.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WheelSelector = void 0;
const canvas_1 = require("../util/canvas");
class WheelSelector {
constructor(options) {
var _a;
this.isActive = false;
this.isLeftClicked = false;
this.position = null;
this.items = [];
this.selectedItemNo = null;
this.outerDistance = 200;
this.innerDistance = 100;
this.cursorCanvas = null;
this.theme = {
defaultColor: "rgba(0, 0, 0, 0.7)",
selectedColor: "rgba(125, 220, 0, 0.7)",
};
if (options !== undefined) {
let { outerDistance, innerDistance } = options;
if (outerDistance === undefined) {
if (innerDistance !== undefined)
throw new Error("give outerDistance value");
else {
outerDistance = this.outerDistance;
innerDistance = this.innerDistance;
}
}
else {
if (innerDistance === undefined) {
innerDistance = outerDistance / 2;
}
else if (innerDistance >= outerDistance) {
throw new Error("innerDistance should be smaller than outerDistance");
}
}
options.outerDistance = outerDistance;
options.innerDistance = innerDistance;
let { theme } = options;
if (theme) {
Object.assign(this.theme, theme);
}
}
options = Object.assign({
items: [],
}, options);
this.updateItems((_a = options.items) !== null && _a !== void 0 ? _a : []);
this.outerDistance = options.outerDistance;
this.innerDistance = options.innerDistance;
}
activateSelector(pos) {
this.position = pos;
WheelSelector.prototype.deactivateSelector.call(this);
this.cursorCanvas = (0, canvas_1.makeCanvas)(this.position, this.outerDistance * 2);
this.redraw();
this.isActive = true;
}
deactivateSelector() {
if (this.cursorCanvas !== null)
(0, canvas_1.removeCanvas)(this.cursorCanvas);
this.isActive = false;
}
triggerSelected() {
if (typeof this.selectedItemNo !== "number")
return;
const item = this.items[this.selectedItemNo];
this.selectedItemNo = null;
item.callback(item);
}
redraw() {
if (this.position === null)
return;
(0, canvas_1.drawItems)(this);
}
selectItem(itemno) {
if (itemno !== null && this.items[itemno] === undefined)
throw Error("invalid item number");
this.selectedItemNo = itemno;
this.redraw();
}
updateItems(items) {
this.selectedItemNo = null;
this.items = items ? items : [];
this.redraw();
return this.items;
}
}
exports.WheelSelector = WheelSelector;