UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 3.38 kB
import{Color as e}from"../../core/math/color.js";import{Curve as t}from"../../core/math/curve.js";import{CurveSet as r}from"../../core/math/curve-set.js";import{Vec2 as n}from"../../core/math/vec2.js";import{Vec3 as s}from"../../core/math/vec3.js";import{Vec4 as i}from"../../core/math/vec4.js";import{GraphNode as a}from"../../scene/graph-node.js";import{Asset as o}from"../asset/asset.js";const c=["x","y","z","w"],f=[void 0,void 0,n,s,i];function l(n,s,i,u){switch(s.type){case"boolean":return!!i;case"number":if("number"==typeof i)return i;if("string"==typeof i){const e=parseInt(i,10);return isNaN(e)?null:e}return"boolean"==typeof i?0+i:null;case"json":{const e={};if(Array.isArray(s.schema)){i&&"object"==typeof i||(i={});for(let t=0;t<s.schema.length;t++){const r=s.schema[t];if(r.name)if(r.array){e[r.name]=[];const t=Array.isArray(i[r.name])?i[r.name]:[];for(let s=0;s<t.length;s++)e[r.name].push(l(n,r,t[s]))}else{const t=i.hasOwnProperty(r.name)?i[r.name]:r.default;e[r.name]=l(n,r,t)}}}return e}case"asset":return i instanceof o?i:"number"==typeof i?n.assets.get(i)||null:"string"==typeof i&&n.assets.get(parseInt(i,10))||null;case"entity":return i instanceof a?i:"string"==typeof i?n.getEntityFromIndex(i):null;case"rgb":case"rgba":if(i instanceof e)return u instanceof e?(u.copy(i),u):i.clone();if(i instanceof Array&&i.length>=3&&i.length<=4){for(let e=0;e<i.length;e++)if("number"!=typeof i[e])return null;return u||(u=new e),u.r=i[0],u.g=i[1],u.b=i[2],u.a=3===i.length?1:i[3],u}return"string"==typeof i&&/#(?:[0-9a-f]{2}){3,4}/i.test(i)?(u||(u=new e),u.fromString(i),u):null;case"vec2":case"vec3":case"vec4":{const e=parseInt(s.type.slice(3),10),t=f[e];if(i instanceof t)return u instanceof t?(u.copy(i),u):i.clone();if(i instanceof Array&&i.length===e){for(let e=0;e<i.length;e++)if("number"!=typeof i[e])return null;u||(u=new t);for(let t=0;t<e;t++)u[c[t]]=i[t];return u}return null}case"curve":if(i){let e;if(i instanceof t||i instanceof r)e=i.clone();else{e=new(i.keys[0]instanceof Array?r:t)(i.keys),e.type=i.type}return e}}return i}function u(e,t,r,n){return t.array?r.map(((r,s)=>l(e,t,r,n?n[s]:null))):l(e,t,r,n)}function p(e,t,r,n){if(r)for(const s in t){const i=t[s],a=r[s];void 0!==a&&(n[s]=u(e,i,a,n[s]))}}class h{constructor(e){this.scriptType=e,this.index={}}add(e,t){this.index[e]||h.reservedNames.has(e)||(this.index[e]=t,Object.defineProperty(this.scriptType.prototype,e,{get:function(){return this.__attributes[e]},set:function(r){const n="attr",s=`attr:${e}`,i=this.__attributes[e];let a=i;if(i&&"json"!==t.type&&"entity"!==t.type&&i.clone&&(this.hasEvent(n)||this.hasEvent(s))&&(a=i.clone()),t.array){if(this.__attributes[e]=[],r)for(let n=0,s=r.length;n<s;n++)this.__attributes[e].push(l(this.app,t,r[n],i?i[n]:null))}else this.__attributes[e]=l(this.app,t,r,i);this.fire(n,e,this.__attributes[e],a),this.fire(s,this.__attributes[e],a)}}))}remove(e){return!!this.index[e]&&(delete this.index[e],delete this.scriptType.prototype[e],!0)}has(e){return!!this.index[e]}get(e){return this.index[e]||null}}h.assignAttributesToScript=p,h.attributeToValue=u,h.reservedNames=new Set(["app","entity","enabled","_enabled","_enabledOld","_destroyed","__attributes","__attributesRaw","__scriptType","__executionOrder","_callbacks","_callbackActive","has","get","on","off","fire","once","hasEvent"]);export{h as ScriptAttributes,p as assignAttributesToScript};