UNPKG

toosoon-prng-controllers

Version:

This project provides PRNG functions with a set of Controllers for generating pseudo-random values using a seed-based approach and various algorithms

579 lines (578 loc) 21.2 kB
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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import prng from './prng.js'; /** * Utility abstract class for generating pseudo-random values * * @class BasePRNGController * @implements PRNGController * @abstract */ var BasePRNGController = /** @class */ (function () { function BasePRNGController(seed) { this.seed = seed; prng.addController(this); } BasePRNGController.prototype.dispose = function () { var _a; prng.removeController(this); (_a = this.gui) === null || _a === void 0 ? void 0 : _a.dispose(); }; return BasePRNGController; }()); /** * Utility abstract class for managing multiple instances of individual controllers * * @class BasePRNGGroupController * @implements PRNGGroupController * @abstract */ var BasePRNGGroupController = /** @class */ (function () { function BasePRNGGroupController(seed) { this.controllers = []; this.seed = seed; } BasePRNGGroupController.prototype.addGUI = function (gui, params) { if (params === void 0) { params = {}; } this.gui = gui.addFolder(__assign({ title: this.seed, expanded: false }, params)); this.guiParams = params; return this.gui; }; BasePRNGGroupController.prototype.getValueAt = function (index) { var controller = this.controllers[index]; if (!controller) { controller = this.createController(index); if (this.gui) { controller.addGUI(this.gui, __assign({ label: "".concat(this.gui.title, "-").concat(index) }, this.guiParams)); } this.controllers[index] = controller; } return controller.value; }; BasePRNGGroupController.prototype.dispose = function () { var _a; this.controllers.forEach(function (controller) { return controller.dispose(); }); this.controllers = []; (_a = this.gui) === null || _a === void 0 ? void 0 : _a.dispose(); }; Object.defineProperty(BasePRNGGroupController.prototype, "title", { get: function () { return this.seed; }, enumerable: false, configurable: true }); return BasePRNGGroupController; }()); // ********************* // Controllers // ********************* /** * Utility class for generating pseudo-random boolean value * * @exports * @class BooleanController * @extends BasePRNGController */ var BooleanController = /** @class */ (function (_super) { __extends(BooleanController, _super); function BooleanController(seed, probability) { if (probability === void 0) { probability = 0.5; } var _this = _super.call(this, seed) || this; _this.probability = probability; _this.value = _this.getValue(); return _this; } BooleanController.prototype.addGUI = function (gui, params) { if (params === void 0) { params = {}; } this.gui = gui.addBinding(this, 'value', __assign({ label: this.seed }, params)); return this.gui; }; BooleanController.prototype.getValue = function () { var _a; this.value = prng.randomBoolean(this.seed, this.probability); (_a = this.gui) === null || _a === void 0 ? void 0 : _a.refresh(); return this.value; }; return BooleanController; }(BasePRNGController)); export { BooleanController }; /** * Utility class for generating pseudo-random sign value (-1 or 1) * * @exports * @class SignController * @extends BasePRNGController */ var SignController = /** @class */ (function (_super) { __extends(SignController, _super); function SignController(seed, probability) { if (probability === void 0) { probability = 0.5; } var _this = _super.call(this, seed) || this; _this.probability = probability; _this.value = _this.getValue(); return _this; } SignController.prototype.addGUI = function (gui, params) { if (params === void 0) { params = {}; } this.gui = gui.addBinding(this, 'value', __assign({ label: this.seed, options: [-1, 1] }, params)); return this.gui; }; SignController.prototype.getValue = function () { var _a; this.value = prng.randomSign(this.seed, this.probability); (_a = this.gui) === null || _a === void 0 ? void 0 : _a.refresh(); return this.value; }; return SignController; }(BasePRNGController)); export { SignController }; /** * Utility class for generating pseudo-random floating-point number within a specified range * * @exports * @class FloatController * @extends BasePRNGController */ var FloatController = /** @class */ (function (_super) { __extends(FloatController, _super); function FloatController(seed, min, max) { if (min === void 0) { min = 0; } if (max === void 0) { max = 1; } var _this = _super.call(this, seed) || this; _this.min = min; _this.max = max; _this.value = _this.getValue(); return _this; } FloatController.prototype.addGUI = function (gui, _a) { if (_a === void 0) { _a = {}; } var min = _a.min, max = _a.max, _b = _a.step, step = _b === void 0 ? 0.01 : _b, params = __rest(_a, ["min", "max", "step"]); this.gui = gui.addBinding(this, 'value', __assign({ label: this.seed, min: min, max: max, step: step }, params)); return this.gui; }; FloatController.prototype.getValue = function () { var _a; this.value = prng.randomFloat(this.seed, this.min, this.max); (_a = this.gui) === null || _a === void 0 ? void 0 : _a.refresh(); return this.value; }; return FloatController; }(BasePRNGController)); export { FloatController }; /** * Utility class for generating pseudo-random integer number within a specified range * * @exports * @class IntController * @extends BasePRNGController */ var IntController = /** @class */ (function (_super) { __extends(IntController, _super); function IntController(seed, min, max) { var _this = _super.call(this, seed) || this; _this.min = min; _this.max = max; _this.value = prng.randomInt(_this.seed, min, max); return _this; } IntController.prototype.addGUI = function (gui, _a) { if (_a === void 0) { _a = {}; } var min = _a.min, max = _a.max, _b = _a.step, step = _b === void 0 ? 1 : _b, params = __rest(_a, ["min", "max", "step"]); this.gui = gui.addBinding(this, 'value', __assign({ label: this.seed, min: min, max: max, step: step }, params)); return this.gui; }; IntController.prototype.getValue = function () { var _a; this.value = prng.randomInt(this.seed, this.min, this.max); (_a = this.gui) === null || _a === void 0 ? void 0 : _a.refresh(); return this.value; }; return IntController; }(BasePRNGController)); export { IntController }; /** * Utility class for generating pseudo-random hexadecimal color * * @exports * @class HexColorController * @extends BasePRNGController */ var HexColorController = /** @class */ (function (_super) { __extends(HexColorController, _super); function HexColorController(seed) { var _this = _super.call(this, seed) || this; _this.value = _this.getValue(); return _this; } HexColorController.prototype.addGUI = function (gui, _a) { if (_a === void 0) { _a = {}; } var _b = _a.view, view = _b === void 0 ? 'color' : _b, params = __rest(_a, ["view"]); this.gui = gui.addBinding(this, 'value', __assign({ label: this.seed, view: view }, params)); return this.gui; }; HexColorController.prototype.getValue = function () { var _a; this.value = prng.randomHexColor(this.seed); (_a = this.gui) === null || _a === void 0 ? void 0 : _a.refresh(); return this.value; }; return HexColorController; }(BasePRNGController)); export { HexColorController }; /** * Utility class for picking a pseudo-random item from a given array * * @exports * @class ItemController * @extends BasePRNGController */ var ItemController = /** @class */ (function (_super) { __extends(ItemController, _super); function ItemController(seed, items) { var _this = _super.call(this, seed) || this; _this.items = items; _this.value = _this.getValue(); return _this; } ItemController.prototype.addGUI = function (gui, params) { if (params === void 0) { params = {}; } this.gui = gui.addBinding(this, 'value', __assign({ label: this.seed, options: this.items }, params)); return this.gui; }; ItemController.prototype.getValue = function () { var _a; this.value = prng.randomItem(this.seed, this.items); (_a = this.gui) === null || _a === void 0 ? void 0 : _a.refresh(); return this.value; }; return ItemController; }(BasePRNGController)); export { ItemController }; /** * Utility class for picking a pseudo-random property value from a given object * * @exports * @class ObjectPropertyController * @extends BasePRNGController */ var ObjectPropertyController = /** @class */ (function (_super) { __extends(ObjectPropertyController, _super); function ObjectPropertyController(seed, object) { var _this = _super.call(this, seed) || this; _this.object = object; _this.value = _this.getValue(); return _this; } ObjectPropertyController.prototype.addGUI = function (gui, params) { if (params === void 0) { params = {}; } this.gui = gui.addBinding(this, 'value', __assign({ label: this.seed, options: this.object }, params)); return this.gui; }; ObjectPropertyController.prototype.getValue = function () { var _a; this.value = prng.randomObjectProperty(this.seed, this.object); (_a = this.gui) === null || _a === void 0 ? void 0 : _a.refresh(); return this.value; }; return ObjectPropertyController; }(BasePRNGController)); export { ObjectPropertyController }; /** * Utility class for selecting a pseudo-random index from an array of weighted items * * @exports * @class WeightsController * @extends BasePRNGController */ var WeightsController = /** @class */ (function (_super) { __extends(WeightsController, _super); function WeightsController(seed, items) { var _this = _super.call(this, seed) || this; _this.items = items; _this.weights = _this.items.map(function (item) { return item.weight; }); _this.value = _this.getValue(); return _this; } WeightsController.prototype.addGUI = function (gui, params) { if (params === void 0) { params = {}; } this.gui = gui.addBinding(this, 'value', __assign({ label: this.seed, options: this.items.map(function (item) { return item.value; }) }, params)); return this.gui; }; WeightsController.prototype.getValue = function () { var _a; var index = prng.randomIndex(this.seed, this.weights); this.value = this.items[index].value; (_a = this.gui) === null || _a === void 0 ? void 0 : _a.refresh(); return this.value; }; return WeightsController; }(BasePRNGController)); export { WeightsController }; /** * Utility class for generating a pseudo-random number fitting a Gaussian (normal) distribution * * @exports * @class GaussianController * @extends BasePRNGController */ var GaussianController = /** @class */ (function (_super) { __extends(GaussianController, _super); function GaussianController(seed, mean, spread) { if (mean === void 0) { mean = 0; } if (spread === void 0) { spread = 1; } var _this = _super.call(this, seed) || this; _this.mean = mean; _this.spread = spread; _this.value = _this.getValue(); return _this; } GaussianController.prototype.addGUI = function (gui, _a) { if (_a === void 0) { _a = {}; } var _b = _a.min, min = _b === void 0 ? this.mean - this.spread : _b, _c = _a.max, max = _c === void 0 ? this.mean + this.spread : _c, step = _a.step, params = __rest(_a, ["min", "max", "step"]); this.gui = gui.addBinding(this, 'value', __assign({ label: this.seed, min: min, max: max, step: step }, params)); return this.gui; }; GaussianController.prototype.getValue = function () { var _a; this.value = prng.randomGaussian(this.seed, this.mean, this.spread); (_a = this.gui) === null || _a === void 0 ? void 0 : _a.refresh(); return this.value; }; return GaussianController; }(BasePRNGController)); export { GaussianController }; // ********************* // Group Controllers // ********************* /** * Utility class for managing multiple `BooleanController` * * @exports * @class BooleanGroupController * @extends BasePRNGGroupController */ var BooleanGroupController = /** @class */ (function (_super) { __extends(BooleanGroupController, _super); function BooleanGroupController(seed, probability) { var _this = _super.call(this, seed) || this; _this.controllers = []; _this.probability = probability; return _this; } BooleanGroupController.prototype.createController = function (index) { return new BooleanController("".concat(this.seed, "-").concat(index), this.probability); }; return BooleanGroupController; }(BasePRNGGroupController)); export { BooleanGroupController }; /** * Utility class for managing multiple `SignController` * * @exports * @class SignGroupController * @extends BasePRNGGroupController */ var SignGroupController = /** @class */ (function (_super) { __extends(SignGroupController, _super); function SignGroupController(seed, probability) { var _this = _super.call(this, seed) || this; _this.controllers = []; _this.probability = probability; return _this; } SignGroupController.prototype.createController = function (index) { return new SignController("".concat(this.seed, "-").concat(index), this.probability); }; return SignGroupController; }(BasePRNGGroupController)); export { SignGroupController }; /** * Utility class for managing multiple `FloatController` * * @exports * @class FloatGroupController * @extends BasePRNGGroupController */ var FloatGroupController = /** @class */ (function (_super) { __extends(FloatGroupController, _super); function FloatGroupController(seed, min, max) { var _this = _super.call(this, seed) || this; _this.controllers = []; _this.min = min; _this.max = max; return _this; } FloatGroupController.prototype.createController = function (index) { return new FloatController("".concat(this.seed, "-").concat(index), this.min, this.max); }; return FloatGroupController; }(BasePRNGGroupController)); export { FloatGroupController }; /** * Utility class for managing multiple `IntController` * * @exports * @class IntGroupController * @extends BasePRNGGroupController */ var IntGroupController = /** @class */ (function (_super) { __extends(IntGroupController, _super); function IntGroupController(seed, min, max) { var _this = _super.call(this, seed) || this; _this.controllers = []; _this.min = min; _this.max = max; return _this; } IntGroupController.prototype.createController = function (index) { return new IntController("".concat(this.seed, "-").concat(index), this.min, this.max); }; return IntGroupController; }(BasePRNGGroupController)); export { IntGroupController }; /** * Utility class for managing multiple `HexColorController` * * @exports * @class HexColorGroupController * @extends BasePRNGGroupController */ var HexColorGroupController = /** @class */ (function (_super) { __extends(HexColorGroupController, _super); function HexColorGroupController() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.controllers = []; return _this; } // constructor(seed: string) { // super(seed); // } HexColorGroupController.prototype.createController = function (index) { return new HexColorController("".concat(this.seed, "-").concat(index)); }; return HexColorGroupController; }(BasePRNGGroupController)); export { HexColorGroupController }; /** * Utility class for managing multiple `ItemController` * * @exports * @class ItemGroupController * @extends BasePRNGGroupController */ var ItemGroupController = /** @class */ (function (_super) { __extends(ItemGroupController, _super); function ItemGroupController(seed, items) { var _this = _super.call(this, seed) || this; _this.controllers = []; _this.items = items; return _this; } ItemGroupController.prototype.createController = function (index) { return new ItemController("".concat(this.seed, "-").concat(index), this.items); }; return ItemGroupController; }(BasePRNGGroupController)); export { ItemGroupController }; /** * Utility class for managing multiple `ObjectPropertyController` * * @exports * @class ObjectPropertyGroupController * @extends BasePRNGGroupController */ var ObjectPropertyGroupController = /** @class */ (function (_super) { __extends(ObjectPropertyGroupController, _super); function ObjectPropertyGroupController(seed, object) { var _this = _super.call(this, seed) || this; _this.controllers = []; _this.object = object; return _this; } ObjectPropertyGroupController.prototype.createController = function (index) { return new ObjectPropertyController("".concat(this.seed, "-").concat(index), this.object); }; return ObjectPropertyGroupController; }(BasePRNGGroupController)); export { ObjectPropertyGroupController }; /** * Utility class for managing multiple `WeightsController` * * @exports * @class WeightsGroupController * @extends BasePRNGGroupController */ var WeightsGroupController = /** @class */ (function (_super) { __extends(WeightsGroupController, _super); function WeightsGroupController(seed, items) { var _this = _super.call(this, seed) || this; _this.controllers = []; _this.items = items; return _this; } WeightsGroupController.prototype.createController = function (index) { return new WeightsController("".concat(this.seed, "-").concat(index), this.items); }; return WeightsGroupController; }(BasePRNGGroupController)); export { WeightsGroupController }; /** * Utility class for managing multiple `GaussianController` * * @exports * @class GaussianGroupController * @extends BasePRNGGroupController */ var GaussianGroupController = /** @class */ (function (_super) { __extends(GaussianGroupController, _super); function GaussianGroupController(seed, mean, spread) { if (mean === void 0) { mean = 0; } if (spread === void 0) { spread = 1; } var _this = _super.call(this, seed) || this; _this.controllers = []; _this.mean = mean; _this.spread = spread; return _this; } GaussianGroupController.prototype.createController = function (index) { return new GaussianController("".concat(this.seed, "-").concat(index), this.mean, this.spread); }; return GaussianGroupController; }(BasePRNGGroupController)); export { GaussianGroupController };