var Sparrow=Sparrow||{};Sparrow.UI=class{constructor(t){this.engine=t,this.gl=this.engine.gl,this.elements=[],this.devicePixelRatio=window.devicePixelRatio,this.uiScale=window.devicePixelRatio,this.uiScaleFactor=1,this.windowWidth=this.engine.gl.viewportWidth,this.windowHeight=this.engine.gl.viewportHeight,console.log(this.engine.canvas.width,this.engine.canvas.height),console.log(this.windowWidth,this.windowHeight),this.lastMouseClickListener=null,this.lastMouseMoveListener=null,this.activeKeyboardListener=null,this.#initWebGL()}delete(){this.#deleteWebGL()}add(t){return this.elements.push(t),t.ui=this,t.init(),this.sortElements(),t}mouseClick(i,e){console.log("UI > mouseClick: "+i+","+e),i*=window.devicePixelRatio,e*=window.devicePixelRatio;for(let t=0;t<this.elements.length;++t)if(this.elements[t].b_visible&&this.elements[t].b_active&&this.elements[t].inBounds(i,e)&&(this.lastMouseClickListener=this.elements[t].handleMouseClick(i,e),this.elements[t].b_canBeClicked)){if(null===this.lastMouseClickListener)break;return this.lastMouseClickListener.b_canHaveFocus?(null!==this.activeKeyboardListener&&(this.activeKeyboardListener.clearFocus(),this.activeKeyboardListener=null),this.activeKeyboardListener=this.lastMouseClickListener,this.activeKeyboardListener.focus()):null!==this.activeKeyboardListener&&(this.activeKeyboardListener.clearFocus(),this.activeKeyboardListener=null),!0}return null!==this.activeKeyboardListener&&(this.activeKeyboardListener.clearFocus(),this.activeKeyboardListener=null),!1}mouseRelease(t,i){null!==this.lastMouseClickListener&&(void 0!==this.lastMouseClickListener.release&&this.lastMouseClickListener.release(t,i),this.lastMouseClickListener=null)}mouseMove(i,e){i*=window.devicePixelRatio,e*=window.devicePixelRatio;var s=null;for(let t=0;t<this.elements.length;++t)if(this.elements[t].b_visible&&this.elements[t].b_active&&this.elements[t].inBounds(i,e)&&this.elements[t].b_canBeClicked){s=this.elements[t],null!=this.lastMouseMoveListener&&this.lastMouseMoveListener!=s&&this.lastMouseMoveListener.handleMouseLeave(i,e),this.elements[t].handleMouseMove(i,e),this.lastMouseMoveListener=this.elements[t];break}null==s&&null!=this.lastMouseMoveListener&&(this.lastMouseMoveListener.handleMouseLeave(i,e),this.lastMouseMoveListener=null),null!==this.lastMouseClickListener&&void 0!==this.lastMouseClickListener.pressed&&this.lastMouseClickListener.pressed(i,e)}mouseWheel(i,e,s){i*=window.devicePixelRatio,e*=window.devicePixelRatio;for(let t=0;t<this.elements.length;++t)if(this.elements[t].b_visible&&this.elements[t].b_active&&this.elements[t].inBounds(i,e))return this.elements[t].handleMouseWheel(i,e,s),!0;return!1}keyDown(t){null!==this.activeKeyboardListener&&this.activeKeyboardListener.keyDown(t)}windowResize(){if(console.log("UI > windowResize"),this.windowWidth=this.engine.gl.viewportWidth,this.windowHeight=this.engine.gl.viewportHeight,window.devicePixelRatio!==this.devicePixelRatio)this.devicePixelRatio=window.devicePixelRatio,this.#rescale();else for(let t=0;t<this.elements.length;++t)this.elements[t].handleWindowResize()}clearFocus(){null!==this.activeKeyboardListener&&(this.activeKeyboardListener.clearFocus(),this.activeKeyboardListener=null)}setUIScale(t){this.uiScaleFactor=t,this.#rescale()}#rescale(){this.uiScale=this.devicePixelRatio*this.uiScaleFactor,console.log(this.windowWidth,this.windowHeight);for(let t=0;t<this.elements.length;++t)this.elements[t].rescale()}draw(){this.shader.use();for(let t=this.elements.length-1;0<=t;--t)this.elements[t].b_visible&&this.elements[t].handleDraw()}getMaxZ(){var i=0;for(let t=0;t<this.elements.length;++t)this.elements[t].z>i&&(i=this.elements[t].z);return i}sortElements(){this.elements.sort((t,i)=>t.z<i.z?1:-1)}static init(t){Sparrow.UI.Style.init()}static shutdown(){}#initWebGL(){this.shader=Sparrow.Shader.create(this.engine,`#version 300 es
layout ( location = 0 ) in vec2 vertex_position;
layout ( location = 2 ) in vec2 vertex_uv;
uniform mat4 M_mvp;
out vec2 uv;
void main()
{
    gl_Position = M_mvp * vec4( vertex_position , 0 , 1 );
    uv = vertex_uv;
}`,`#version 300 es
precision mediump float;
in vec2 uv;
uniform float alpha;
uniform float gray;
uniform sampler2D sampler;
out vec4 fragColor;
void main()
{
    vec4 textureColor = texture( sampler , uv );
    float grayScale = dot( textureColor.rgb , vec3( 0.299 , 0.587 , 0.114 ) );
    fragColor = vec4( (1.0-gray) * textureColor.rgb + gray * grayScale * 0.6 , textureColor.a * alpha );
}`,"UIShader"),this.shader.use(),this.shader.u_M_mvp=this.shader.getUniformLocation("M_mvp"),this.shader.u_M_alpha=this.shader.getUniformLocation("alpha"),this.shader.u_M_gray=this.shader.getUniformLocation("gray"),this.shader.setUniformMVP=t=>{this.shader.uniformMatrix4fv(this.shader.u_M_mvp,t)},this.shader.setUniformAlpha=t=>{this.shader.uniform1f(this.shader.u_M_alpha,t)},this.shader.setUniformGray=t=>{this.shader.uniform1f(this.shader.u_M_gray,t)};var t=new Float32Array([0,0,0,1,1,0,1,1]);this.uvBuffer_1=new Sparrow.VBO(this.engine),this.uvBuffer_1.bind(),this.uvBuffer_1.bufferData(t,this.engine.gl.STATIC_DRAW),this.uvBuffers_2=[null,null],this.uvBuffers_2[0]=new Sparrow.VBO(this.engine),this.uvBuffers_2[1]=new Sparrow.VBO(this.engine),t[1]=0,t[3]=.5,t[5]=0,t[7]=.5,this.uvBuffers_2[0].bind(),this.uvBuffers_2[0].bufferData(t,this.engine.gl.STATIC_DRAW),t[1]=.5,t[3]=1,t[5]=.5,t[7]=1,this.uvBuffers_2[1].bind(),this.uvBuffers_2[1].bufferData(t,this.engine.gl.STATIC_DRAW),this.uvBuffers_4=[null,null,null,null],this.uvBuffers_4[0]=new Sparrow.VBO(this.engine),this.uvBuffers_4[1]=new Sparrow.VBO(this.engine),this.uvBuffers_4[2]=new Sparrow.VBO(this.engine),this.uvBuffers_4[3]=new Sparrow.VBO(this.engine),t[1]=0,t[3]=.25,t[5]=0,t[7]=.25,this.uvBuffers_4[0].bind(),this.uvBuffers_4[0].bufferData(t,this.engine.gl.STATIC_DRAW),t[1]=.25,t[3]=.5,t[5]=.25,t[7]=.5,this.uvBuffers_4[1].bind(),this.uvBuffers_4[1].bufferData(t,this.engine.gl.STATIC_DRAW),t[1]=.5,t[3]=.75,t[5]=.5,t[7]=.75,this.uvBuffers_4[2].bind(),this.uvBuffers_4[2].bufferData(t,this.engine.gl.STATIC_DRAW),t[1]=.75,t[3]=1,t[5]=.75,t[7]=1,this.uvBuffers_4[3].bind(),this.uvBuffers_4[3].bufferData(t,this.engine.gl.STATIC_DRAW)}#deleteWebGL(){this.uvBuffer_1.delete(),this.uvBuffers_2[0].delete(),this.uvBuffers_2[1].delete(),this.uvBuffers_4[0].delete(),this.uvBuffers_4[1].delete(),this.uvBuffers_4[2].delete(),this.uvBuffers_4[3].delete()}},(Sparrow=Sparrow||{}).UI.ElementType=class{static LABEL=0;static BUTTON=1;static EDIT=2;static WINDOW=3;static SCROLLBAR=4;static CONTAINER=5;static CHECKBOX=6},Sparrow.UI.Element=class{constructor(t,i,e){this.ui=null,this.type=t,this.x=0,this.y=0,this.width=100,this.height=100,this.M_model=new mat4,this.M_mvp=new mat4,this.parent=null,this.children=[],this.b_visible=!0,this.b_active=!0,this.b_scissor=!1,this.b_canBeClicked=!0,this.b_canHaveFocus=!1,this.lastMouseMoveListener=null,this.layout=null,this.texture=null,this.b_externalTexture=!1,this.style=new Sparrow.UI.Style,this.style.apply(i),this.style.apply(e),this.style.hasOwnProperty("z")?this.z=this.style.z:this.z=0}delete(){this.vao.delete(),this.vertexBuffer.delete(),this.b_externalTexture||this.texture.delete()}init(){this.#init(),this.vertices=new Float32Array(8),this.vertexBuffer=new Sparrow.VBO(this.ui.engine),this.uvBuffer=this.ui.uvBuffer_1,this.vao=new Sparrow.VAO(this.ui.engine),this.vao.bind(),this.ui.gl.enableVertexAttribArray(0),this.ui.gl.enableVertexAttribArray(2),this.vertexBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(0,2,this.ui.gl.FLOAT,!1,0,0),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0)}addChild(i){for(let t=0;t<this.children.length;++t)if(this.children[t]==i)return i;return this.children.push(i),i.ui=this.ui,i.parent=this,i.init(),i.z=this.z+1,this.sortChildren(),null!==this.layout&&this.layout.apply(!1),i}inBounds(t,i){return!(t<this.x||t>this.x+this.width||i<this.y||i>this.y+this.height)}handleMouseClick(i,e){for(let t=0;t<this.children.length;++t)if(this.children[t].b_visible&&this.children[t].b_active&&this.children[t].inBounds(i,e)){var s=this.children[t].handleMouseClick(i,e);if(null!==s)return s}return void 0!==this.click&&this.click(i,e)?this:null}handleMouseMove(i,e){var s=null;for(let t=0;t<this.children.length;++t)if(this.children[t].b_visible&&this.children[t].b_active&&this.children[t].inBounds(i,e)){s=this.children[t],null!=this.lastMouseMoveListener&&this.lastMouseMoveListener!=s&&this.lastMouseMoveListener.handleMouseLeave(i,e),this.children[t].handleMouseMove(i,e),this.lastMouseMoveListener=this.children[t];break}null==s&&(null==this.lastMouseMoveListener?(void 0!==this.mouseEnter&&this.mouseEnter(i,e),this.lastMouseMoveListener=this):this.lastMouseMoveListener!=this&&(this.lastMouseMoveListener.handleMouseLeave(i,e),void 0!==this.mouseEnter&&this.mouseEnter(i,e),this.lastMouseMoveListener=this),void 0!==this.mouseOver)&&this.mouseOver(i,e)}handleMouseLeave(t,i){return null!=this.lastMouseMoveListener&&(this.lastMouseMoveListener==this?void 0!==this.lastMouseMoveListener.mouseLeave&&this.lastMouseMoveListener.mouseLeave(t,i):this.lastMouseMoveListener.handleMouseLeave(t,i),this.lastMouseMoveListener=null),!0}handleMouseWheel(i,e,s){(s=1<s?1:s)<-1&&(s=-1),s*=-1;for(let t=0;t<this.children.length;++t)if(this.children[t].b_visible&&this.children[t].b_active&&this.children[t].inBounds(i,e)&&this.children[t].handleMouseWheel(i,e,s))return!0;return void 0!==this.mouseWheel&&this.mouseWheel(s)}handleWindowResize(){this.#initPosition();for(let t=0;t<this.children.length;++t)this.children[t].handleWindowResize();this.recalculateMatrices()}rescale(){this.setSize(this.style.width,this.style.height);for(let t=0;t<this.children.length;++t)this.children[t].rescale();this.#initPosition(),null!==this.layout&&this.layout.apply(!1),void 0!==this._onRescale&&this._onRescale()}handleDraw(){this.ui.shader.setUniformMVP(this.M_mvp),this.ui.shader.setUniformAlpha(1),this.ui.shader.setUniformGray(0),this.b_scissor?(this.ui.engine.gl.enable(this.ui.engine.gl.SCISSOR_TEST),this.ui.engine.gl.scissor(this.x,this.ui.windowHeight-this.y-this.height,this.width,this.height),this.draw(),this.ui.engine.gl.disable(this.ui.engine.gl.SCISSOR_TEST)):this.draw();for(let t=0;t<this.children.length;++t)this.children[t].b_visible&&(this.b_scissor?(this.ui.engine.gl.enable(this.ui.engine.gl.SCISSOR_TEST),this.ui.engine.gl.scissor(this.x+this.style.borderWidth,this.ui.windowHeight-this.y-this.height+this.style.borderWidth,this.width-2*this.style.borderWidth,this.height-2*this.style.borderWidth),this.children[t].handleDraw(),this.ui.engine.gl.disable(this.ui.engine.gl.SCISSOR_TEST)):this.children[t].handleDraw())}show(){this.b_visible=!0}hide(){this.b_visible=!1}setPosition(t,i){this.x=t,this.y=i,this.M_model.data[12]=this.x,this.M_model.data[13]=this.y;for(let t=0;t<this.children.length;++t)this.children[t].#initPosition();this.recalculateMatrices(),void 0!==this._onPositionChange&&this._onPositionChange()}setX(t){this.x=t,this.M_model.data[12]=this.x;for(let t=0;t<this.children.length;++t)this.children[t].#initPosition();this.recalculateMatrices()}setY(t){this.y=t,this.M_model.data[13]=this.y;for(let t=0;t<this.children.length;++t)this.children[t].#initPosition();this.recalculateMatrices()}setSize(t,i){this.#setSize(t,i),this._updateBuffers(),this._updateTexture()}#setSize(t,i){var e;"string"==typeof this.style.width?"%"===this.style.width.charAt(this.style.width.length-1)?(e=parseInt(this.style.width.substr(0,this.style.width.length-1))/100,null!==this.parent?this.width=Math.floor(this.parent.width*e):this.width=Math.floor(this.ui.windowWidth*e)):(console.log("UI > Element > setSize > invalid width string"),this.width=100*this.ui.uiScale):this.width=t*this.ui.uiScale,"string"==typeof this.style.height?"%"===this.style.height.charAt(this.style.height.length-1)?(e=parseInt(this.style.height.substr(0,this.style.height.length-1))/100,null!==this.parent?this.height=Math.floor(this.parent.height*e):this.height=Math.floor(this.ui.windowHeight*e)):(console.log("UI > Element > setSize > invalid height string"),this.height=100*this.ui.uiScale):this.height=i*this.ui.uiScale}setZ(i){this.z=i;for(let t=0;t<this.children.length;++t)this.children[t].setZ(i+1)}putInFront(){this.z<=this.ui.getMaxZ()&&(this.setZ(this.ui.getMaxZ()+1),this.ui.sortElements())}sortChildren(){this.children.sort((t,i)=>t.z>=i.z?1:-1)}recalculateMatrices(){mat4.multiplyInPlace(this.M_mvp,this.ui.engine.M_ortho,this.M_model)}setTexture(t){null===this.texture||this.b_externalTexture||this.texture.delete(),this.texture=t,this.b_externalTexture=!0}applyStyle(t){this.style.apply(t),this.#init(),this._updateBuffers(),this._updateTexture()}#init(){this.#setSize(this.style.width,this.style.height),this.#initPosition()}#initPosition(){var t,i;null!==this.parent&&null!==this.parent.layout&&this.type!==Sparrow.UI.ElementType.SCROLLBAR||this.type===Sparrow.UI.ElementType.WINDOW&&0!==this.x&&0!==this.y||(i=t=0,"left"==this.style.position?t=null!=this.parent?this.parent.x+this.style.x*this.ui.uiScale:this.style.x*this.ui.uiScale:"center"==this.style.position?t=null!=this.parent?this.parent.x+(this.parent.width-this.width)/2+this.style.x*this.ui.uiScale:(this.ui.windowWidth-this.width)/2+this.style.x*this.ui.uiScale:"right"==this.style.position&&(t=null!=this.parent?this.parent.x+this.parent.width-this.width-this.style.x*this.ui.uiScale:this.ui.windowWidth-this.width-this.style.x*this.ui.uiScale),"top"==this.style.verticalPosition?i=null!=this.parent?this.parent.y+this.style.y*this.ui.uiScale:this.style.y*this.ui.uiScale:"center"==this.style.verticalPosition?i=null!=this.parent?this.parent.y+(this.parent.height-this.height)/2+this.style.y*this.ui.uiScale:(this.ui.windowHeight-this.height)/2+this.style.y*this.ui.uiScale:"bottom"==this.style.verticalPosition&&(i=null!=this.parent?this.parent.y+this.parent.height-this.height-this.style.y*this.ui.uiScale:this.ui.windowHeight-this.height-this.style.y*this.ui.uiScale),this.setPosition(t,i))}},(Sparrow=Sparrow||{}).UI.Button=class extends Sparrow.UI.Element{constructor(t,i){super(Sparrow.UI.ElementType.BUTTON,Sparrow.UI.Style.DEFAULT_BUTTON,i),this.str=t}init(){super.init(),this.uvBuffer=this.ui.uvBuffers_2[0],this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0),this.texture=new Sparrow.Texture(this.ui.engine,"__button__"),this._updateBuffers(),this._updateTexture()}click(t,i){return void 0!==this.onclick&&this.onclick(t,i),!0}mouseEnter(){this.uvBuffer=this.ui.uvBuffers_2[1],this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0),void 0!==this.onmouseenter&&this.onmouseenter()}mouseOver(){void 0!==this.onmouseover&&this.onmouseover()}mouseLeave(){this.uvBuffer=this.ui.uvBuffers_2[0],this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0),void 0!==this.onmouseleave&&this.onmouseleave()}setStr(t){this.str=t,this._updateTexture()}draw(){this.vao.bind(),void 0!==this.texture&&this.texture.bind(),Sparrow.drawArrays(this.ui.engine,this.ui.engine.gl.TRIANGLE_STRIP,0,4)}_updateBuffers(){this.vertices[0]=0,this.vertices[1]=0,this.vertices[2]=0,this.vertices[3]=this.height,this.vertices[4]=this.width,this.vertices[5]=0,this.vertices[6]=this.width,this.vertices[7]=this.height,this.vertexBuffer.bind(),this.vertexBuffer.bufferData(this.vertices,this.ui.engine.gl.STATIC_DRAW)}_updateTexture(){var t,i;this.b_externalTexture||((t=new Sparrow.CanvasRenderer(this.width,2*this.height)).setFont(this.style.font,this.style.textSize*this.ui.uiScale),"background"==this.style.background&&(t.fillRect(0,0,this.width,this.height,this.style.borderColor),i=this.style.borderWidth*this.ui.uiScale,t.fillRect(i,i,this.width-2*i,this.height-2*i,this.style.backgroundColor)),t.fillTextAdvanced(this.str,0,0,this.width,this.height,this.style.textColor,this.style.textAlign,this.style.textVerticalAlign,this.style.textPadding*this.ui.uiScale),"background"==this.style.hover.background&&(t.fillRect(0,this.height,this.width,this.height,this.style.hover.borderColor),i=this.style.hover.borderWidth*this.ui.uiScale,t.fillRect(i,this.height+i,this.width-2*i,this.height-2*i,this.style.hover.backgroundColor)),t.fillTextAdvanced(this.str,0,this.height,this.width,this.height,this.style.textColor,this.style.textAlign,this.style.textVerticalAlign,this.style.textPadding*this.ui.uiScale),t.toTexture(this.texture))}},(Sparrow=Sparrow||{}).UI.Checkbox=class extends Sparrow.UI.Element{constructor(t){super(Sparrow.UI.ElementType.CHECKBOX,Sparrow.UI.Style.DEFAULT_CHECKBOX,t),this.b_checked=!1}init(){super.init(),this.uvBuffer=this.ui.uvBuffers_4[0],this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0),this.texture=new Sparrow.Texture(this.ui.engine,"__checkbox__"),this._updateBuffers(),this._updateTexture()}click(t,i){return this.b_checked?(this.b_checked=!1,this.uvBuffer=this.ui.uvBuffers_4[1]):(this.b_checked=!0,this.uvBuffer=this.ui.uvBuffers_4[3]),this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0),void 0!==this.onclick&&this.onclick(t,i),!0}mouseEnter(){this.b_checked?this.uvBuffer=this.ui.uvBuffers_4[3]:this.uvBuffer=this.ui.uvBuffers_4[1],this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0),void 0!==this.onmouseenter&&this.onmouseenter()}mouseOver(){void 0!==this.onmouseover&&this.onmouseover()}mouseLeave(){this.b_checked?this.uvBuffer=this.ui.uvBuffers_4[2]:this.uvBuffer=this.ui.uvBuffers_4[0],this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0),void 0!==this.onmouseleave&&this.onmouseleave()}draw(){this.vao.bind(),void 0!==this.texture&&this.texture.bind(),Sparrow.drawArrays(this.ui.engine,this.ui.engine.gl.TRIANGLE_STRIP,0,4)}setChecked(t){this.b_checked=t,this.b_checked?this.uvBuffer=this.ui.uvBuffers_4[2]:this.uvBuffer=this.ui.uvBuffers_4[0],this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0)}setStr(t,i){null==this.lbl_str?((i=void 0!==i?i:{}).x=this.width+5,this.lbl_str=this.addChild(new Sparrow.UI.Label(t,i))):this.lbl_str.setStr(t)}_updateBuffers(){this.vertices[0]=0,this.vertices[1]=0,this.vertices[2]=0,this.vertices[3]=this.height,this.vertices[4]=this.width,this.vertices[5]=0,this.vertices[6]=this.width,this.vertices[7]=this.height,this.vertexBuffer.bind(),this.vertexBuffer.bufferData(this.vertices,this.ui.engine.gl.STATIC_DRAW)}_updateTexture(){var t,i=new Sparrow.CanvasRenderer(this.width,4*this.height),e=("background"==this.style.background&&(i.fillRect(0,0,this.width,this.height,this.style.borderColor),t=this.style.borderWidth*this.ui.uiScale,i.fillRect(t,t,this.width-2*t,this.height-2*t,this.style.backgroundColor)),"background"==this.style.hover.background&&(i.fillRect(0,this.height,this.width,this.height,this.style.hover.borderColor),t=this.style.hover.borderWidth*this.ui.uiScale,i.fillRect(t,this.height+t,this.width-2*t,this.height-2*t,this.style.hover.backgroundColor)),Math.floor(this.width/4));"background"==this.style.checked.background&&(i.fillRect(0,2*this.height,this.width,this.height,this.style.checked.borderColor),t=this.style.checked.borderWidth*this.ui.uiScale,i.fillRect(t,2*this.height+t,this.width-2*t,this.height-2*t,this.style.checked.backgroundColor),i.fillRect(e,2*this.height+e,this.width-2*e,this.height-2*e,this.style.checked.borderColor)),"background"==this.style.checked.hover.background&&(i.fillRect(0,3*this.height,this.width,this.height,this.style.checked.hover.borderColor),t=this.style.checked.hover.borderWidth*this.ui.uiScale,i.fillRect(t,3*this.height+t,this.width-2*t,this.height-2*t,this.style.checked.hover.backgroundColor),i.fillRect(e,3*this.height+e,this.width-2*e,this.height-2*e,this.style.checked.hover.borderColor)),i.toTexture(this.texture)}},(Sparrow=Sparrow||{}).UI.Container=class extends Sparrow.UI.Element{constructor(t){super(Sparrow.UI.ElementType.CONTAINER,Sparrow.UI.Style.DEFAULT_CONTAINER,t),this.b_scissor=!0}init(){super.init(),this.texture=new Sparrow.Texture(this.ui.engine,"__container__"),this._updateBuffers(),this._updateTexture(),this.scrollbar=this.addChild(new Sparrow.UI.Scrollbar(this.style.scrollbar)),this.scrollbar.onChange=i=>{for(let t=0;t<this.children.length;++t)this.children[t]!==this.scrollbar&&this.children[t].setPosition(this.children[t].x,this.children[t].y+i)},this.layout=new Sparrow.UI.VerticalLayout(this.engine,this),this.layout.addListener(this.scrollbar)}mouseWheel(t){this.scrollbar.scroll(t)}draw(){this.vao.bind(),void 0!==this.texture&&this.texture.bind(),Sparrow.drawArrays(this.ui.engine,this.ui.engine.gl.TRIANGLE_STRIP,0,4)}_updateBuffers(){this.vertices[0]=0,this.vertices[1]=0,this.vertices[2]=0,this.vertices[3]=this.height,this.vertices[4]=this.width,this.vertices[5]=0,this.vertices[6]=this.width,this.vertices[7]=this.height,this.vertexBuffer.bind(),this.vertexBuffer.bufferData(this.vertices,this.ui.engine.gl.STATIC_DRAW)}_updateTexture(){var t,i=new Sparrow.CanvasRenderer(this.width,this.height);i.setFont(this.style.font,this.style.textSize*this.ui.uiScale),"background"==this.style.background&&(i.fillRect(0,0,this.width,this.height,this.style.borderColor),t=this.style.borderWidth*this.ui.uiScale,i.fillRect(t,t,this.width-2*t,this.height-2*t,this.style.backgroundColor)),i.toTexture(this.texture)}_onRescale(){this.scrollbar.scrollToTop()}},(Sparrow=Sparrow||{}).UI.Edit=class extends Sparrow.UI.Element{constructor(t,i){super(Sparrow.UI.ElementType.EDIT,Sparrow.UI.Style.DEFAULT_EDIT,i),this.str=t,this.b_canHaveFocus=!0,this.b_hasFocus=!1}init(){super.init(),this.uvBuffer=this.ui.uvBuffers_4[0],this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0),this.texture=new Sparrow.Texture(this.ui.engine,"__edit__"),this._updateBuffers(),this._updateTexture()}click(t,i){return void 0!==this.onclick&&this.onclick(t,i),!0}focus(){this.b_hasFocus=!0,this.uvBuffer=this.ui.uvBuffers_4[3],this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0)}clearFocus(){this.b_hasFocus=!1,this.uvBuffer=this.ui.uvBuffers_4[0],this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0)}mouseEnter(){this.b_hasFocus?this.uvBuffer=this.ui.uvBuffers_4[3]:this.uvBuffer=this.ui.uvBuffers_4[1],this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0),void 0!==this.onmouseenter&&this.onmouseenter()}mouseOver(){void 0!==this.onmouseover&&this.onmouseover()}mouseLeave(){this.b_hasFocus?this.uvBuffer=this.ui.uvBuffers_4[2]:this.uvBuffer=this.ui.uvBuffers_4[0],this.vao.bind(),this.uvBuffer.bind(),this.ui.engine.gl.vertexAttribPointer(2,2,this.ui.gl.FLOAT,!1,0,0),void 0!==this.onmouseleave&&this.onmouseleave()}keyDown(t){8==t.keyCode?(this.str=this.str.slice(0,-1),this._updateTexture()):32<=t.keyCode&&(t.ctrlKey&&"v"==t.key?navigator.clipboard.readText().then(t=>{this.str+=t,this._updateTexture()}):t.ctrlKey&&"c"==t.key?navigator.clipboard.writeText(this.str):1==t.key.length&&(this.str+=t.key,this._updateTexture()))}draw(){this.vao.bind(),void 0!==this.texture&&this.texture.bind(),Sparrow.drawArrays(this.ui.engine,this.ui.engine.gl.TRIANGLE_STRIP,0,4)}_updateBuffers(){this.vertices[0]=0,this.vertices[1]=0,this.vertices[2]=0,this.vertices[3]=this.height,this.vertices[4]=this.width,this.vertices[5]=0,this.vertices[6]=this.width,this.vertices[7]=this.height,this.vertexBuffer.bind(),this.vertexBuffer.bufferData(this.vertices,this.ui.engine.gl.STATIC_DRAW)}_updateTexture(){var t,i=new Sparrow.CanvasRenderer(this.width,4*this.height);i.setFont(this.style.font,this.style.textSize*this.ui.uiScale),"background"==this.style.background&&(i.fillRect(0,0,this.width,this.height,this.style.borderColor),t=this.style.borderWidth*this.ui.uiScale,i.fillRect(t,t,this.width-2*t,this.height-2*t,this.style.backgroundColor)),i.fillTextAdvanced(this.str,0,0,this.width,this.height,this.style.textColor,this.style.textAlign,this.style.textVerticalAlign,this.style.textPadding*this.ui.uiScale),"background"==this.style.hover.background&&(i.fillRect(0,this.height,this.width,this.height,this.style.hover.borderColor),t=this.style.hover.borderWidth*this.ui.uiScale,i.fillRect(t,this.height+t,this.width-2*t,this.height-2*t,this.style.hover.backgroundColor)),i.fillTextAdvanced(this.str,0,this.height,this.width,this.height,this.style.textColor,this.style.textAlign,this.style.textVerticalAlign,this.style.textPadding*this.ui.uiScale),"background"==this.style.focus.background&&(i.fillRect(0,2*this.height,this.width,this.height,this.style.focus.borderColor),t=this.style.focus.borderWidth*this.ui.uiScale,i.fillRect(t,2*this.height+t,this.width-2*t,this.height-2*t,this.style.focus.backgroundColor)),i.fillTextAdvanced(this.str,0,2*this.height,this.width,this.height,this.style.textColor,this.style.textAlign,this.style.textVerticalAlign,this.style.textPadding*this.ui.uiScale),"background"==this.style.focus.hover.background&&(i.fillRect(0,3*this.height,this.width,this.height,this.style.focus.hover.borderColor),t=this.style.focus.hover.borderWidth*this.ui.uiScale,i.fillRect(t,3*this.height+t,this.width-2*t,this.height-2*t,this.style.focus.hover.backgroundColor)),i.fillTextAdvanced(this.str,0,3*this.height,this.width,this.height,this.style.textColor,this.style.textAlign,this.style.textVerticalAlign,this.style.textPadding*this.ui.uiScale),i.toTexture(this.texture)}},(Sparrow=Sparrow||{}).UI.Label=class extends Sparrow.UI.Element{constructor(t,i){super(Sparrow.UI.ElementType.LABEL,Sparrow.UI.Style.DEFAULT_LABEL,i),this.str=t}init(){super.init(),this.texture=new Sparrow.Texture(this.ui.engine,"__label__"),this._updateBuffers(),this._updateTexture()}setStr(t){this.str=t,this._updateTexture()}draw(){this.vao.bind(),void 0!==this.texture&&this.texture.bind(),Sparrow.drawArrays(this.ui.engine,this.ui.engine.gl.TRIANGLE_STRIP,0,4)}_updateBuffers(){this.vertices[0]=0,this.vertices[1]=0,this.vertices[2]=0,this.vertices[3]=this.height,this.vertices[4]=this.width,this.vertices[5]=0,this.vertices[6]=this.width,this.vertices[7]=this.height,this.vertexBuffer.bind(),this.vertexBuffer.bufferData(this.vertices,this.ui.engine.gl.STATIC_DRAW)}_updateTexture(){var t,i=new Sparrow.CanvasRenderer(this.width,this.height);i.setFont(this.style.font,this.style.textSize*this.ui.uiScale),"background"==this.style.background&&(i.fillRect(0,0,this.width,this.height,this.style.borderColor),t=this.style.borderWidth*this.ui.uiScale,i.fillRect(t,t,this.width-2*t,this.height-2*t,this.style.backgroundColor)),i.fillTextAdvanced(this.str,0,0,this.width,this.height,this.style.textColor,this.style.textAlign,this.style.textVerticalAlign,this.style.textPadding*this.ui.uiScale),i.toTexture(this.texture)}},(Sparrow=Sparrow||{}).UI.Scrollbar=class extends Sparrow.UI.Element{constructor(t){super(Sparrow.UI.ElementType.SCROLLBAR,Sparrow.UI.Style.DEFAULT_SCROLLBAR,t),this.b_dragging=!1,this.dragStartY=0,this.dragStartFromTopY=0,this.scrollValue=0,this.scrollSpeed=10,this.filledHeight=0,this.M_barTranslation=new mat4,this.M_barMVP=new mat4,this.barQuad=null,this.barRect=null,this.areaRect=null}delete(){super.delete(),this.barTexture.delete(),this.upTexture.delete(),this.downTexture.delete(),this.barQuad.delete()}init(){super.init(),this.texture=new Sparrow.Texture(this.ui.engine,"__scrollbar__"),this.barTexture=new Sparrow.Texture(this.ui.engine,"__scrollbar_bar__"),this.upTexture=new Sparrow.Texture(this.ui.engine,"__scrollbar_up__"),this.downTexture=new Sparrow.Texture(this.ui.engine,"__scrollbar_down__"),this.btn_up=this.addChild(new Sparrow.UI.Button("u",this.style.button)),this.btn_up.applyStyle({width:this.style.width,height:this.style.width}),this.btn_up.onclick=()=>{this.scroll(1)},this.btn_up.setTexture(this.upTexture),this.btn_down=this.addChild(new Sparrow.UI.Button("d",this.style.button)),this.btn_down.applyStyle({verticalPosition:"bottom",width:this.style.width,height:this.style.width}),this.btn_down.onclick=()=>{this.scroll(-1)},this.btn_down.setTexture(this.downTexture),this.barQuad=new Sparrow.Quad(this.ui.engine,0,this.width,this.width,2*this.width,{texture:this.barTexture}),mat4.multiplyInPlace(this.M_barMVP,this.M_mvp,this.M_barTranslation),this.barRect=new Rect(this.parent.x+this.parent.width-this.width,this.y+this.width,this.width,2*this.width),this.areaRect=new Rect(this.parent.x+this.parent.width-this.width,this.y+this.width,this.width,this.height-2*this.width),this._updateBuffers(),this._updateTexture()}click(t,i){return this.barRect.inBounds(t,i)?(this.b_dragging||(this.dragStartY=i,this.dragStartFromTopY=this.barRect.y,this.b_dragging=!0),!0):!this.areaRect.inBounds(t,i)||(this.filledHeight<this.parent.height?void 0:((t=(t=i-this.areaRect.y)>this.height-4*this.width?this.height-4*this.width:t)<0&&(t=0),i=this.scrollValue,this.scrollValue=Math.floor(t/(this.height-4*this.width)*(this.parent.height-this.filledHeight)),i=this.scrollValue-i,this.M_barTranslation.data[13]=t,mat4.multiplyInPlace(this.M_barMVP,this.M_mvp,this.M_barTranslation),this.barRect.y=this.y+this.width+t,void 0!==this.onChange&&this.onChange(i),!0))}pressed(t,i){var e;!this.b_dragging||this.filledHeight<this.parent.height||(i=i-this.dragStartY,(i=(i=this.dragStartFromTopY+i-this.y-this.width)>this.height-4*this.width?this.height-4*this.width:i)<0&&(i=0),e=this.scrollValue,this.scrollValue=Math.floor(i/(this.height-4*this.width)*(this.parent.height-this.filledHeight)),e=this.scrollValue-e,this.M_barTranslation.data[13]=i,mat4.multiplyInPlace(this.M_barMVP,this.M_mvp,this.M_barTranslation),this.barRect.y=this.y+this.width+i,void 0!==this.onChange&&this.onChange(e))}release(){this.b_dragging=!1}scroll(t){var i=this.scrollValue,t=(this.scrollValue+=t*this.scrollSpeed,this.scrollValue<this.height-this.filledHeight&&(this.scrollValue=this.height-this.filledHeight),0<this.scrollValue&&(this.scrollValue=0),this.scrollValue-i),i=Math.floor(this.scrollValue/(this.height-this.filledHeight)*(this.height-4*this.width));return this.M_barTranslation.data[13]=i,mat4.multiplyInPlace(this.M_barMVP,this.M_mvp,this.M_barTranslation),this.barRect.y=this.y+this.width+i,void 0!==this.onChange&&this.onChange(t),!0}scrollToTop(){this.scrollValue=0;var t=this.scrollValue,i=Math.floor(this.scrollValue/(this.height-this.filledHeight)*(this.height-4*this.width));this.M_barTranslation.data[13]=i,mat4.multiplyInPlace(this.M_barMVP,this.M_mvp,this.M_barTranslation),this.barRect.y=this.y+this.width+i,void 0!==this.onChange&&this.onChange(t)}scrollToBottom(){this.scrollValue=this.height-this.filledHeight;var t=this.scrollValue,i=Math.floor(this.scrollValue/(this.height-this.filledHeight)*(this.height-4*this.width));this.M_barTranslation.data[13]=i,mat4.multiplyInPlace(this.M_barMVP,this.M_mvp,this.M_barTranslation),this.barRect.y=this.y+this.width+i,void 0!==this.onChange&&this.onChange(t)}onlayout(t){this.filledHeight=t}draw(){this.vao.bind(),void 0!==this.texture&&this.texture.bind(),Sparrow.drawArrays(this.ui.engine,this.ui.engine.gl.TRIANGLE_STRIP,0,4),this.ui.shader.setUniformMVP(this.M_barMVP),this.barQuad.draw()}_updateBuffers(){this.vertices[0]=0,this.vertices[1]=0,this.vertices[2]=0,this.vertices[3]=this.height,this.vertices[4]=this.width,this.vertices[5]=0,this.vertices[6]=this.width,this.vertices[7]=this.height,this.vertexBuffer.bind(),this.vertexBuffer.bufferData(this.vertices,this.ui.engine.gl.STATIC_DRAW)}_updateTexture(){this.scrollSpeed=10*this.ui.uiScale,this.barQuad.setPositionAndSize(0,this.width,this.width,2*this.width),mat4.multiplyInPlace(this.M_barMVP,this.M_mvp,this.M_barTranslation),this.barRect.setPosition(this.parent.x+this.parent.width-this.width,this.y+this.width),this.barRect.setSize(this.width,2*this.width),this.areaRect.setPosition(this.parent.x+this.parent.width-this.width,this.y+this.width),this.areaRect.setSize(this.width,this.height-2*this.width);var t=new Sparrow.CanvasRenderer(this.width,this.height),i=("background"==this.style.background&&(t.fillRect(0,0,this.width,this.height,this.style.borderColor),h=this.style.borderWidth*this.ui.uiScale,t.fillRect(h,h,this.width-2*h,this.height-2*h,this.style.backgroundColor)),t.toTexture(this.texture),t.setSize(this.width,2*this.width),"background"==this.style.bar.background&&(t.fillRect(0,0,this.width,2*this.width,this.style.bar.borderColor),h=this.style.bar.borderWidth*this.ui.uiScale,t.fillRect(h,h,this.width-2*h,2*this.width-2*h,this.style.bar.backgroundColor)),t.toTexture(this.barTexture),Math.floor(this.width/4)),e=Math.floor(this.width/2),s=this.width-i,h=(t.clear(),t.fillRect(0,0,this.width,this.width,this.style.button.borderColor),this.style.button.borderWidth*this.ui.uiScale),h=(t.fillRect(h,h,this.width-2*h,this.width-2*h,this.style.button.backgroundColor),t.fillTriangle(i,s,s,s,e,i,this.style.button.borderColor),t.fillRect(0,this.width,this.width,this.width,this.style.button.hover.borderColor),this.style.button.hover.borderWidth*this.ui.uiScale),h=(t.fillRect(h,this.width+h,this.width-2*h,this.width-2*h,this.style.button.hover.backgroundColor),t.fillTriangle(i,this.width+s,s,this.width+s,e,this.width+i,this.style.button.hover.borderColor),t.toTexture(this.upTexture),t.clear(),t.fillRect(0,0,this.width,this.width,this.style.button.borderColor),this.style.button.borderWidth*this.ui.uiScale),h=(t.fillRect(h,h,this.width-2*h,this.width-2*h,this.style.button.backgroundColor),t.fillTriangle(i,i,s,i,e,s,this.style.button.borderColor),t.fillRect(0,this.width,this.width,this.width,this.style.button.hover.borderColor),this.style.button.hover.borderWidth*this.ui.uiScale);t.fillRect(h,this.width+h,this.width-2*h,this.width-2*h,this.style.button.hover.backgroundColor),t.fillTriangle(i,this.width+i,s,this.width+i,e,this.width+s,this.style.button.hover.borderColor),t.toTexture(this.downTexture)}_onPositionChange(){null!==this.barQuad&&this.barQuad.setPosition(0,this.width),mat4.multiplyInPlace(this.M_barMVP,this.M_mvp,this.M_barTranslation),null!==this.barRect&&this.barRect.setPosition(this.parent.x+this.parent.width-this.width,this.y+this.width),null!==this.areaRect&&this.areaRect.setPosition(this.parent.x+this.parent.width-this.width,this.y+this.width)}},(Sparrow=Sparrow||{}).UI.Style=class{static#DEFAULT_LABEL={background:"none"};static#DEFAULT_BUTTON={backgroundColor:"#dddddd",borderWidth:1,textAlign:"center",textVerticalAlign:"center",hover:{backgroundColor:"#bbbbbb",borderWidth:1}};static#DEFAULT_EDIT={borderWidth:1,textPadding:5,hover:{backgroundColor:"#eeeeee",borderWidth:1},focus:{borderWidth:3,borderColor:"#77aaff",hover:{borderWidth:3,borderColor:"#99ccff"}}};static#DEFAULT_WINDOW={width:200,height:200,borderWidth:1,borderColor:"#77aaff",button:{hover:{}}};static#DEFAULT_SCROLLBAR={position:"right",width:24,height:"100%",backgroundColor:"#eeeeee",bar:{backgroundColor:"#dddddd",borderWidth:1},button:{backgroundColor:"#dddddd",borderWidth:1,hover:{backgroundColor:"#bbbbbb"}}};static#DEFAULT_CONTAINER={width:200,height:200,textPadding:5,scrollbar:{}};static#DEFAULT_CHECKBOX={width:30,borderWidth:1,hover:{borderWidth:1,backgroundColor:"#cccccc"},checked:{borderWidth:1,backgroundColor:"#ffffff",hover:{borderWidth:1,backgroundColor:"#cccccc"}}};static DEFAULT_LABEL=null;static DEFAULT_BUTTON=null;static DEFAULT_EDIT=null;static DEFAULT_WINDOW=null;static DEFAULT_SCROLLBAR=null;static DEFAULT_CONTAINER=null;static DEFAULT_CHECKBOX=null;constructor(){this.position="left",this.verticalPosition="top",this.x=0,this.y=0,this.width=150,this.height=30,this.background="background",this.backgroundColor="#ffffff",this.borderWidth=0,this.borderColor="#000000",this.borderRadius=0,this.font="sans-serif",this.textSize=20,this.textColor="#000000",this.textPadding=0,this.textAlign="left",this.textVerticalAlign="center"}apply(t){t.hasOwnProperty("position")&&(this.position=t.position),t.hasOwnProperty("verticalPosition")&&(this.verticalPosition=t.verticalPosition),t.hasOwnProperty("x")&&(this.x=t.x),t.hasOwnProperty("y")&&(this.y=t.y),t.hasOwnProperty("width")&&(this.width=t.width),t.hasOwnProperty("height")&&(this.height=t.height),t.hasOwnProperty("background")&&(this.background=t.background),t.hasOwnProperty("backgroundColor")&&(this.backgroundColor=t.backgroundColor),t.hasOwnProperty("borderWidth")&&(this.borderWidth=t.borderWidth),t.hasOwnProperty("borderColor")&&(this.borderColor=t.borderColor),t.hasOwnProperty("borderRadius")&&(this.borderRadius=t.borderRadius),t.hasOwnProperty("font")&&(this.font=t.font),t.hasOwnProperty("textSize")&&(this.textSize=t.textSize),t.hasOwnProperty("textColor")&&(this.textColor=t.textColor),t.hasOwnProperty("textPadding")&&(this.textPadding=t.textPadding),t.hasOwnProperty("textAlign")&&(this.textAlign=t.textAlign),t.hasOwnProperty("textVerticalAlign")&&(this.textVerticalAlign=t.textVerticalAlign),t.hasOwnProperty("hover")&&(this.hasOwnProperty("hover")||(this.hover=new Sparrow.UI.Style),this.hover.apply(t.hover)),t.hasOwnProperty("focus")&&(this.hasOwnProperty("focus")||(this.focus=new Sparrow.UI.Style),this.focus.apply(t.focus)),t.hasOwnProperty("checked")&&(this.hasOwnProperty("checked")||(this.checked=new Sparrow.UI.Style),this.checked.apply(t.checked)),t.hasOwnProperty("bar")&&(this.hasOwnProperty("bar")||(this.bar=new Sparrow.UI.Style),this.bar.apply(t.bar)),t.hasOwnProperty("button")&&(this.hasOwnProperty("button")||(this.button=new Sparrow.UI.Style),this.button.apply(Sparrow.UI.Style.DEFAULT_BUTTON),this.button.apply(t.button)),t.hasOwnProperty("scrollbar")&&(this.hasOwnProperty("scrollbar")||(this.scrollbar=new Sparrow.UI.Style),this.scrollbar.apply(Sparrow.UI.Style.DEFAULT_SCROLLBAR),this.scrollbar.apply(t.scrollbar))}print(){console.log(this)}static init(){Sparrow.UI.Style.DEFAULT_LABEL=new Sparrow.UI.Style,Sparrow.UI.Style.DEFAULT_LABEL.apply(Sparrow.UI.Style.#DEFAULT_LABEL),Sparrow.UI.Style.DEFAULT_BUTTON=new Sparrow.UI.Style,Sparrow.UI.Style.DEFAULT_BUTTON.apply(Sparrow.UI.Style.#DEFAULT_BUTTON),Sparrow.UI.Style.DEFAULT_EDIT=new Sparrow.UI.Style,Sparrow.UI.Style.DEFAULT_EDIT.apply(Sparrow.UI.Style.#DEFAULT_EDIT),Sparrow.UI.Style.DEFAULT_WINDOW=new Sparrow.UI.Style,Sparrow.UI.Style.DEFAULT_WINDOW.apply(Sparrow.UI.Style.#DEFAULT_WINDOW),Sparrow.UI.Style.DEFAULT_SCROLLBAR=new Sparrow.UI.Style,Sparrow.UI.Style.DEFAULT_SCROLLBAR.apply(Sparrow.UI.Style.#DEFAULT_SCROLLBAR),Sparrow.UI.Style.DEFAULT_CONTAINER=new Sparrow.UI.Style,Sparrow.UI.Style.DEFAULT_CONTAINER.apply(Sparrow.UI.Style.#DEFAULT_CONTAINER),Sparrow.UI.Style.DEFAULT_CHECKBOX=new Sparrow.UI.Style,Sparrow.UI.Style.DEFAULT_CHECKBOX.apply(Sparrow.UI.Style.#DEFAULT_CHECKBOX)}},(Sparrow=Sparrow||{}).UI.VerticalLayout=class{constructor(t,i){this.engine=t,this.element=i,this.listeners=[]}apply(t){var i=this.element.style.textPadding,e=i;for(let t=0;t<this.element.children.length;++t){var s=this.element.children[t];s.type!==Sparrow.UI.ElementType.SCROLLBAR&&s.b_visible&&(s.setPosition(this.element.x+i,this.element.y+e),e=e+s.height+i)}for(let t=0;t<this.listeners.length;++t)void 0!==this.listeners[t].onlayout&&this.listeners[t].onlayout(e);return e}getContentHeight(){var i=this.element.style.textPadding,e=i;for(let t=0;t<this.element.children.length;++t){var s=this.element.children[t];s.type!==Sparrow.UI.ElementType.SCROLLBAR&&s.b_visible&&(e=e+s.height+i)}return e}addListener(i){for(let t=0;t<this.listeners.length;++t)if(this.listeners[t]===i)return;this.listeners.push(i)}},(Sparrow=Sparrow||{}).UI.Window=class extends Sparrow.UI.Element{constructor(t,i){super(Sparrow.UI.ElementType.WINDOW,Sparrow.UI.Style.DEFAULT_WINDOW,i),this.str=t,this.titleBarRect=null,this.b_dragging=!1,this.dragStartX=0,this.dragStartY=0}init(){super.init(),this.titleBarHeight=30*this.ui.uiScale,this.titleBarRect=new Rect(this.x,this.y,this.width,this.titleBarHeight),this.texture=new Sparrow.Texture(this.ui.engine,"__window__"),this._updateBuffers(),this._updateTexture(),this.btn_close=this.addChild(new Sparrow.UI.Button("x",this.style.button)),this.btn_close.applyStyle({position:"right",width:30}),this.btn_close.onclick=()=>{this.hide(),void 0!==this.onclose&&this.onclose()}}click(t,i){return this.titleBarRect.inBounds(t,i)&&!this.b_dragging&&(this.dragStartX=t,this.dragStartY=i,this.b_dragging=!0),this.putInFront(),!0}pressed(t,i){var e,s;this.b_dragging&&(e=t-this.dragStartX,s=i-this.dragStartY,this.dragStartX=t,this.dragStartY=i,this.setPosition(this.x+e,this.y+s))}release(){this.b_dragging=!1}draw(){this.vao.bind(),void 0!==this.texture&&this.texture.bind(),Sparrow.drawArrays(this.ui.engine,this.ui.engine.gl.TRIANGLE_STRIP,0,4)}setPosition(t,i){null!==this.titleBarRect&&this.titleBarRect.setPosition(t,i),super.setPosition(t,i)}_updateBuffers(){this.vertices[0]=0,this.vertices[1]=0,this.vertices[2]=0,this.vertices[3]=this.height,this.vertices[4]=this.width,this.vertices[5]=0,this.vertices[6]=this.width,this.vertices[7]=this.height,this.vertexBuffer.bind(),this.vertexBuffer.bufferData(this.vertices,this.ui.engine.gl.STATIC_DRAW)}_updateTexture(){this.titleBarHeight=30*this.ui.uiScale,this.titleBarRect.setSize(this.width,this.titleBarHeight);var t,i=new Sparrow.CanvasRenderer(this.width,this.height);i.setFont(this.style.font,this.style.textSize*this.ui.uiScale),"background"==this.style.background&&(i.fillRect(0,0,this.width,this.height,this.style.borderColor),t=this.style.borderWidth*this.ui.uiScale,i.fillRect(t,t,this.width-2*t,this.height-2*t,this.style.backgroundColor),i.fillRect(0,0,this.width,this.titleBarHeight,this.style.borderColor)),i.fillTextAdvanced(this.str,0,0,this.width-this.titleBarHeight,this.titleBarHeight,this.style.textColor,this.style.textAlign,this.style.textVerticalAlign,this.style.textPadding*this.ui.uiScale),i.toTexture(this.texture)}};