UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

80 lines (79 loc) 2.77 kB
"use strict"; import { TypedParam } from "./_Base"; import { ExpressionController } from "./utils/ExpressionController"; import { ParamEvent } from "../poly/ParamEvent"; import { isArray, isString } from "../../core/Type"; export class TypedNumericParam extends TypedParam { isNumeric() { return true; } isDefault() { return this._raw_input == this._default_value; } _prefilterInvalidRawInput(raw_input) { if (isArray(raw_input)) { return raw_input[0]; } else { return raw_input; } } processRawInput() { const converted = this.convert(this._raw_input); if (converted != null) { if (this._expression_controller) { this._expression_controller.setExpression(void 0, false); this.emitController.emit(ParamEvent.EXPRESSION_UPDATED); } const wasErrored = this.states.error.active(); this.states.error.clear(); if (converted != this._value || wasErrored) { this._updateValue(converted); this.setSuccessorsDirty(this); } } else { if (isString(this._raw_input)) { this.states.error.clear(); this._expression_controller = this._expression_controller || new ExpressionController(this); if (this._raw_input != this._expression_controller.expression()) { this._expression_controller.setExpression(this._raw_input); this.emitController.emit(ParamEvent.EXPRESSION_UPDATED); } } else { this.states.error.set(`param input is invalid (${this.path()})`); } } } async processComputation() { var _a; if (((_a = this.expressionController) == null ? void 0 : _a.active()) && !this.expressionController.entitiesDependent()) { const expression_result = await this.expressionController.computeExpression(); if (this.expressionController.isErrored()) { this.states.error.set( `expression error: "${this.expressionController.expression()}" (${this.expressionController.errorMessage()})` ); } else { const converted = this.convert(expression_result); if (converted != null) { if (this.states.error.active()) { this.states.error.clear(); } this._updateValue(converted); } else { this.states.error.set( `expression returns an invalid type (${expression_result}) (${this.expressionController.expression()})` ); } } } } _updateValue(new_value) { this._value = new_value; const parentParam = this.parentParam(); if (parentParam) { parentParam.setValueFromComponents(); } this.options.executeCallback(); this.emitController.emit(ParamEvent.VALUE_UPDATED); this.removeDirtyState(); } }