UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 14.1 kB
var t,i;t=void 0,i=function(){var t=Phaser.Renderer.WebGL.Utils,i=Phaser.Renderer.WebGL.Utils;const e=Phaser.GameObjects.GetCalcMatrix,s=Phaser.Renderer.Canvas.SetTransform;var r={renderWebGL:function(s,r,o,n){r.dirty&&(r.updateData(),r.dirty=!1),o.addToRenderList(r);var h=s.pipelines.set(r.pipeline),a=e(r,o,n),l=h.calcMatrix.copyFrom(a.calc),d=r._displayOriginX,u=r._displayOriginY,g=o.alpha*r.alpha;s.pipelines.preBatch(r),r.isFilled&&function(i,e,s,r,o,n){for(var h=t.getTintAppendFloatAlpha(s.fillColor,s.fillAlpha*r),a=s.pathData,l=s.pathIndexes,d=0;d<l.length;d+=3){var u=2*l[d],g=2*l[d+1],f=2*l[d+2],P=a[u+0]-o,c=a[u+1]-n,y=a[g+0]-o,p=a[g+1]-n,v=a[f+0]-o,S=a[f+1]-n,m=e.getX(P,c),b=e.getY(P,c),x=e.getX(y,p),k=e.getY(y,p),A=e.getX(v,S),T=e.getY(v,S);i.batchTri(s,m,b,x,k,A,T,0,0,1,1,h,h,h,2)}}(h,l,r,g,d,u),r.isStroked&&function(t,e,s,r,o){var n=t.strokeTint,h=i.getTintAppendFloatAlpha(e.strokeColor,e.strokeAlpha*s);n.TL=h,n.TR=h,n.BL=h,n.BR=h;var a,l,d=e.isDashed&&!!e.strokePathData,u=d?e.strokePathData:e.pathData,g=u.length-1,f=e.lineWidth,P=f/2,c=u[0]-r,y=u[1]-o;if(e.closePath||(g-=2),d){var p=e.strokePathMask,v=0,S=0;for(m=2;m<g;m+=2)a=u[m]-r,l=u[m+1]-o,p[v]?(t.batchLine(c,y,a,l,P,P,f,S,!1),S++):S=0,c=a,y=l,v++}else for(var m=2;m<g;m+=2)a=u[m]-r,l=u[m+1]-o,t.batchLine(c,y,a,l,P,P,f,m-2,!!e.closePath&&m===g-1),c=a,y=l}(h,r,g,d,u),s.pipelines.postBatch(r)},renderCanvas:function(t,i,e,r){i.dirty&&(i.updateData(),i.dirty=!1),e.addToRenderList(i);var o=t.currentContext;if(s(t,o,i,e,r)){var n=i._displayOriginX,h=i._displayOriginY;i.isFilled&&function(t,i,e,s){var r=i.pathData;if(r&&!(r.length<4)){var o=r.length-1,n=r[0]-e,h=r[1]-s;t.beginPath(),t.moveTo(n,h),i.closePath||(o-=2);for(var a=2;a<o;a+=2){var l=r[a]-e,d=r[a+1]-s;t.lineTo(l,d)}i.closePath&&t.closePath(),function(t,i,e,s){var r=i.fillColor,o=s||i.fillAlpha,n=(16711680&r)>>>16,h=(65280&r)>>>8,a=255&r;t.fillStyle="rgba("+n+","+h+","+a+","+o+")"}(t,i),t.fill()}}(o,i,n,h),i.isStroked&&function(t,i,e,s){var r=i.isDashed&&!!i.strokePathData,o=r?i.strokePathData:i.pathData;if(o&&!(o.length<4)){var n=o.length-1,h=o[0]-e,a=o[1]-s;if(function(t,i,e,s){var r=i.strokeColor,o=s||i.strokeAlpha,n=(16711680&r)>>>16,h=(65280&r)>>>8,a=255&r;t.strokeStyle="rgba("+n+","+h+","+a+","+o+")",t.lineWidth=i.lineWidth}(t,i),t.beginPath(),i.closePath||(n-=2),r){var l=i.strokePathMask,d=0;for(u=2;u<n;u+=2)g=o[u]-e,f=o[u+1]-s,l[d]&&(t.moveTo(h,a),t.lineTo(g,f)),h=g,a=f,d++}else{t.moveTo(h,a);for(var u=2;u<n;u+=2){var g=o[u]-e,f=o[u+1]-s;t.lineTo(g,f)}i.closePath&&t.closePath()}t.stroke()}}(o,i,n,h),o.restore()}}};const o=1e-6,n=10,h=.5;var a=function(t,i){return function(t){if(!Array.isArray(t))return null;for(var i=[],e=0,s=t.length;e<s;e++){var r=Number(t[e]);isFinite(r)&&r>0&&i.push(r)}return i.length>0?i:null}(t)||function(t,i){var{segments:e=n,drawRatio:s=h}=t;if(e=Math.round(e),!isFinite(e)||e<=0)return null;if(!(i>o))return null;var r=i/e;if(!(r>o))return null;if((s=Math.max(0,Math.min(1,s)))>=.999999)return null;var a=r*s;a<=o&&(a=o);var l=r-a;return l<=o?null:[a,l]}(t,i)},l=function(t,i,e){if(t&&!(t.length<4)){var s=t.length-1;if(i||(s-=2),!(s<2))for(var r=t[0],o=t[1],n=2;n<s;n+=2){var h=t[n],a=t[n+1];e(r,o,h,a),r=h,o=a}}},d=function(t,i,e){void 0===i&&(i={}),void 0===e&&(e={});var{closePath:s=!1,dashPattern:r,dashOffset:n=0}=i,h=function(t,i){var e=0;return l(t,i,(function(t,i,s,r){var o=s-t,n=r-i;e+=Math.sqrt(o*o+n*n)})),e}(t,s);if(null===(r=a(r,h)))return null;for(var d=[],u=[],g=0,f=0,P=r.length;f<P;f++)g+=r[f];if(g<=o)return e.strokePathData=t?t.slice():[],e.strokePathMask=void 0,e;for(var c=0,y=!0,p=r[c],v=function(){c=(c+1)%r.length,y=!y,p=r[c]},S=function(t,i){return isFinite(t)||(t=0),(t%=i)<0&&(t+=i),t}(n,g);S>o;)S<p-o?(p-=S,S=0):(S-=p,v());var m=function(t,i,e,s,r){if(0===d.length)d.push(t,i);else{var o=d[d.length-2],n=d[d.length-1];o===t&&n===i||d.push(t,i)}d.push(e,s),u.push(r?1:0)};return l(t,s,(function(t,i,e,s){var r=e-t,n=s-i,h=Math.sqrt(r*r+n*n);if(!(h<=o))for(var a=0;a<h-o;){var l=Math.min(p,h-a);if(l<=o)v();else{var d=a/h,u=(a+l)/h;m(t+r*d,i+n*d,t+r*u,i+n*u,y),a+=l,(p-=l)<=o&&v()}}})),!s&&d.length>=2&&d.push(d[d.length-2],d[d.length-1]),e.strokePathData=d,e.strokePathMask=u,e},u={setDashPattern:function(t,i){return this.dashPattern=t,this.dashOffset=i||0,this.isDashed=!!t,this},clearDashPattern:function(){return this.setDashPattern(),this},setDashed:function(t){return void 0===t&&(t=!0),this.isDashed=t,this}},g={buildStrokePath:function(){if(this.isDashed){var t=d(this.pathData,{closePath:this.closePath,dashPattern:this.dashPattern,dashOffset:this.dashOffset},this);t?(this.strokePathData=t.strokePathData,this.strokePathMask=t.strokePathMask):this.isDashed=!1}return this}};Object.assign(g,u);const f=Phaser.GameObjects.Shape;class P extends f{init(){this.isDashed=!1,this.strokePathData=void 0,this.strokePathMask=void 0,this.dashPattern=void 0,this.dashOffset=0}get fillColor(){return this._fillColor}set fillColor(t){this._fillColor=t,this.isFilled=null!=t&&this._fillAlpha>0}get fillAlpha(){return this._fillAlpha}set fillAlpha(t){this._fillAlpha=t,this.isFilled=t>0&&null!=this._fillColor}setFillStyle(t,i){return void 0===i&&(i=1),this.fillColor=t,this.fillAlpha=i,this}get strokeColor(){return this._strokeColor}set strokeColor(t){this._strokeColor=t,this.isStroked=null!=t&&this._strokeAlpha>0&&this._lineWidth>0}get strokeAlpha(){return this._strokeAlpha}set strokeAlpha(t){this._strokeAlpha=t,this.isStroked=t>0&&null!=this._strokeColor&&this._lineWidth>0}get lineWidth(){return this._lineWidth}set lineWidth(t){this._lineWidth=t,this.isStroked=t>0&&null!=this._strokeColor}setStrokeStyle(t,i,e){return void 0===e&&(e=1),this.lineWidth=t,this.strokeColor=i,this.strokeAlpha=e,this}updateData(){return this.buildStrokePath(),this}get width(){return this.geom.width}set width(t){this.resize(t,this.height)}get height(){return this.geom.height}set height(t){this.resize(this.width,t)}setSize(t,i){var e=this.input;return e&&!e.customHitArea&&(e.hitArea.width=t,e.hitArea.height=i),this}resize(t,i){return this.setSize(t,i),this}}Object.assign(P.prototype,g,r);var c=function(t,i,e){i&&!function(t,i){if(!t)return!1;if(t.hasOwnProperty(i))return!0;for(;t;){if(Object.getOwnPropertyDescriptor(t,i))return!0;t=t.__proto__}return!1}(t,`${i}X`)&&(Object.defineProperty(t,`${i}X`,{get:function(){return e.x},set:function(i){e.x=i,t.dirty=!0}}),Object.defineProperty(t,`${i}Y`,{get:function(){return e.y},set:function(i){e.y=i,t.dirty=!0}}),Object.defineProperty(t,`${i}T`,{get:function(){return e.t},set:function(i){e.t=i,t.dirty=!0}}))},y={setTLPosition(t,i){return this.geom.setTLPosition(t,i),this.dirty=!0,this},setTRPosition(t,i){return this.geom.setTRPosition(t,i),this.dirty=!0,this},setBLPosition(t,i){return this.geom.setBLPosition(t,i),this.dirty=!0,this},setBRPosition(t,i){return this.geom.setBRPosition(t,i),this.dirty=!0,this},resetCornerPosition(){return this.geom.resetCornerPosition(),this.dirty=!0,this},insertTopSidePoint(t,i,e,s){var r=this.geom.topSidePoints;if(Array.isArray(t))for(var o,n=0,h=(r=t).length;n<h;n++)o=r[n],this.geom.insertTopSidePoint(o.t,o.x,o.y),c(this,o.key,r[r.length-1]);else this.geom.insertTopSidePoint(t,i,e),c(this,s,r[r.length-1]);return this.dirty=!0,this},insertRightSidePoint(t,i,e,s){var r=this.geom.rightSidePoints;if(Array.isArray(t))for(var o,n=0,h=(r=t).length;n<h;n++)o=r[n],this.geom.insertRightSidePoint(o.t,o.x,o.y),c(this,o.key,r[r.length-1]);else this.geom.insertRightSidePoint(t,i,e),c(this,s,r[r.length-1]);return this.dirty=!0,this},insertBottomSidePoint(t,i,e,s){var r=this.geom.bottomSidePoints;if(Array.isArray(t))for(var o,n=0,h=(r=t).length;n<h;n++)o=r[n],this.geom.insertBottomSidePoint(o.t,o.x,o.y),c(this,o.key,r[r.length-1]);else this.geom.insertBottomSidePoint(t,i,e),c(this,s,r[r.length-1]);return this.dirty=!0,this},insertLeftSidePoint(t,i,e,s){var r=this.geom.leftSidePoints;if(Array.isArray(t))for(var o,n=0,h=(r=t).length;n<h;n++)o=r[n],this.geom.insertLeftSidePoint(o.t,o.x,o.y),c(this,o.key,r[r.length-1]);else this.geom.insertLeftSidePoint(t,i,e),c(this,s,r[r.length-1]);return this.dirty=!0,this},clearTopSidePoints(){return this.geom.clearTopSidePoints(),this.dirty=!0,this},clearRightSidePoints(){return this.geom.clearRightSidePoints(),this.dirty=!0,this},clearBottomSidePoints(){return this.geom.clearBottomSidePoints(),this.dirty=!0,this},clearLeftSidePoints(){return this.geom.clearLeftSidePoints(),this.dirty=!0,this},clearAllSidesPoints(){return this.geom.clearAllSidesPoints(),this.dirty=!0,this}};class p{constructor(t,i,e,s){void 0===t&&(t=0),void 0===i&&(i=t),void 0===e&&(e=0),void 0===s&&(s=0),this.setTo(t,i,e,s),this.tlx=0,this.tly=0,this.trx=0,this.try=0,this.blx=0,this.bly=0,this.brx=0,this.bry=0,this.topSidePoints=[],this.rightSidePoints=[],this.bottomSidePoints=[],this.leftSidePoints=[]}setTo(t,i,e,s){return this.setPosition(t,i),this.setSize(e,s),this}setPosition(t,i){return this.x=t,this.y=i,this}setSize(t,i){return this.width=t,this.height=i,this}setTLPosition(t,i){return this.tlx=t,this.tly=i,this}setTRPosition(t,i){return this.trx=t,this.try=i,this}setBLPosition(t,i){return this.blx=t,this.bly=i,this}setBRPosition(t,i){return this.brx=t,this.bry=i,this}resetCornerPosition(){return this.setTLPosition(0,0).setTRPosition(0,0).setBLPosition(0,0).setBRPosition(0,0),this}insertTopSidePoint(t,i,e){return v(this.topSidePoints,t,i,e),this}insertRightSidePoint(t,i,e){return v(this.rightSidePoints,t,i,e),this}insertBottomSidePoint(t,i,e){return v(this.bottomSidePoints,t,i,e),this}insertLeftSidePoint(t,i,e){return v(this.leftSidePoints,t,i,e),this}clearTopSidePoints(){return this.topSidePoints.length=0,this}clearRightSidePoints(){return this.rightSidePoints.length=0,this}clearBottomSidePoints(){return this.bottomSidePoints.length=0,this}clearLeftSidePoints(){return this.leftSidePoints.length=0,this}clearAllSidesPoints(){return this.clearTopSidePoints().clearRightSidePoints().clearBottomSidePoints().clearLeftSidePoints(),this}}var v=function(t,i,e,s){if("number"!=typeof i){var r=i;i=r.t,e=r.x,s=r.y}t.push({t:i,x:e,y:s})},S=function(t,i,e){var s=e.length;if(s>=2){var r=e[s-2],o=e[s-1];if(t===r&&i===o)return e}return e.push(t,i),e};const m=Phaser.Utils.Objects.IsPlainObject,b=Phaser.Utils.Objects.GetValue,x=Phaser.Math.Linear,k=Phaser.Geom.Polygon.Earcut;class A extends P{constructor(t,i,e,s,r,o,n){var h,a,l;if(m(i)){var d=i;i=d.x,e=d.y,s=d.width,r=d.height,o=d.color,n=d.alpha,h=d.strokeColor,a=d.strokeAlpha,l=d.strokeWidth}void 0===i&&(i=0),void 0===e&&(e=0),void 0===s&&(s=1),void 0===r&&(r=s);var u=new p;super(t,"rexQuadShape",u),this.init(),u.setTo(0,0,s,r),this.setPosition(i,e),this.setFillStyle(o,n),void 0!==h&&void 0===l&&(l=2),this.setStrokeStyle(l,h,a),this.setTLPosition(b(d,"tlx",0),b(d,"tly",0)).setTRPosition(b(d,"trx",0),b(d,"try",0)).setBLPosition(b(d,"blx",0),b(d,"bly",0)).setBRPosition(b(d,"brx",0),b(d,"bry",0));var g=b(d,"leftSidePoints");g&&this.insertLeftSidePoint(g);var f=b(d,"topSidePoints");f&&this.insertTopSidePoint(f);var P=b(d,"rightSidePoints");P&&this.insertRightSidePoint(P);var c=b(d,"bottomSidePoints");c&&this.insertBottomSidePoint(c),this.updateDisplayOrigin(),this.dirty=!0}updateData(){var t=this.geom,i=this.pathData;i.length=0;var e=t.width,s=t.height,r=0+t.tlx,o=0+t.tly,n=e+t.trx,h=0+t.try,a=e+t.brx,l=s+t.bry,d=0+t.blx,u=s+t.bly,g=t.topSidePoints,f=t.rightSidePoints,P=t.bottomSidePoints,c=t.leftSidePoints;S(r,o,i),T(g);for(var y=0,p=g.length;y<p;y++){var v=g[y],m=x(r,n,v.t)+v.x,b=x(o,h,v.t)+v.y;S(m,b,i)}for(S(n,h,i),T(f),y=0,p=f.length;y<p;y++)v=f[y],m=x(n,a,v.t)+v.x,b=x(h,l,v.t)+v.y,S(m,b,i);for(S(a,l,i),T(P),y=P.length-1;y>=0;y--)v=P[y],m=x(d,a,v.t)+v.x,b=x(u,l,v.t)+v.y,S(m,b,i);for(S(d,u,i),T(c),y=c.length-1;y>=0;y--)v=c[y],m=x(r,d,v.t)+v.x,b=x(o,u,v.t)+v.y,S(m,b,i);return i.push(i[0],i[1]),this.pathIndexes=k(i),super.updateData(),this}get tlx(){return this.geom.tlx}set tlx(t){this.geom.tlx=t,this.dirty=!0}get tly(){return this.geom.tly}set tly(t){this.geom.tly=t,this.dirty=!0}get trx(){return this.geom.trx}set trx(t){this.geom.trx=t,this.dirty=!0}get try(){return this.geom.try}set try(t){this.geom.try=t,this.dirty=!0}get blx(){return this.geom.blx}set blx(t){this.geom.blx=t,this.dirty=!0}get bly(){return this.geom.bly}set bly(t){this.geom.bly=t,this.dirty=!0}get brx(){return this.geom.brx}set brx(t){this.geom.brx=t,this.dirty=!0}get bry(){return this.geom.bry}set bry(t){this.geom.bry=t,this.dirty=!0}get leftSidePoints(){return this.geom.leftSidePoints}get topSidePoints(){return this.geom.topSidePoints}get bottomSidePoints(){return this.geom.bottomSidePoints}get rightSidePoints(){return this.geom.rightSidePoints}}var T=function(t){t.length<=1||t.sort((function(t,i){return t.t-i.t}))};function D(t,i,e,s,r,o){var n=new A(this.scene,t,i,e,s,r,o);return this.scene.add.existing(n),n}Object.assign(A.prototype,y);const O=Phaser.Utils.Objects.GetAdvancedValue,L=Phaser.GameObjects.BuildGameObject;function R(t,i){void 0===t&&(t={}),void 0!==i&&(t.add=i);var e=O(t,"width",void 0),s=O(t,"height",e),r=O(t,"fillColor",void 0),o=O(t,"fillAlpha",void 0),n=new A(this.scene,0,0,e,s,r,o);return L(this.scene,n,t),n}var B=function(t){return null!==t&&"object"==typeof t};class C extends Phaser.Plugins.BasePlugin{constructor(t){super(t),t.registerGameObject("rexQuadShape",D,R)}start(){this.game.events.on("destroy",this.destroy,this)}}return function(t,i,e,s="."){if(!B(t))return t;if(function(t){return null==t}(i)||""===i||Array.isArray(i)&&0===i.length)return t;if("string"==typeof i&&-1===i.indexOf(s)||"number"==typeof i)return t[i]=e,t;var r=function(t,i){return Array.isArray(t)||(t="string"!=typeof t||""===t.trim()?[]:t.split(i).filter(Boolean)),t}(i,s);if(0===r.length)return t;for(var o=t,n=r.length,h=0;h<n-1;h++){var a=r[h],l=o[a];B(l)||(o[a]={}),o=o[a]}o[r[n-1]]=e}(window,"RexPlugins.GameObjects.QuadShape",A),C},"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).rexquadshapeplugin=i();