package electrosphere.renderer.ui.elements; import static org.lwjgl.opengl.GL30.GL_FRAMEBUFFER; import static org.lwjgl.opengl.GL30.glBindFramebuffer; import org.joml.Vector3f; import electrosphere.engine.Globals; import electrosphere.logger.LoggerInterface; import electrosphere.renderer.debug.DebugRendering; import electrosphere.renderer.model.Material; import electrosphere.renderer.model.Model; import electrosphere.renderer.texture.Texture; import electrosphere.renderer.ui.DraggableElement; import electrosphere.renderer.ui.DrawableElement; import electrosphere.renderer.ui.events.DragEvent; import electrosphere.renderer.ui.events.DragEvent.DragEventType; import electrosphere.renderer.ui.events.Event; /** * * @author amaterasu */ public class ImagePanel implements DrawableElement, DraggableElement { public static String imagePanelModelPath; String texturePath; Material customMat = new Material(); boolean hasLoadedTexture = false; Texture texture = null; Vector3f texPosition = new Vector3f(0,0,0); Vector3f texScale = new Vector3f(1,1,0); DragEventCallback onDragStart; DragEventCallback onDrag; DragEventCallback onDragRelease; static final Vector3f windowDrawDebugColor = new Vector3f(0.0f,0.5f,1.0f); public ImagePanel(int x, int y, int width, int height, String texturePath){ this.texturePath = texturePath; texture = Globals.assetManager.fetchTexture(this.texturePath); if(texture != null){ customMat.setTexturePointer(texture.getTexturePointer()); hasLoadedTexture = true; } else { customMat.setTexturePointer(Globals.assetManager.fetchTexture(Globals.blackTexture).getTexturePointer()); } this.positionX = x; this.positionY = y; this.width = width; this.height = height; } public void setTexture(Texture texture){ customMat.setTexturePointer(texture.getTexturePointer()); } public Texture getTexture(){ return texture; } @Override public void draw(int parentFramebufferPointer, int parentWidth, int parentHeight) { if(!hasLoadedTexture){ texture = Globals.assetManager.fetchTexture(this.texturePath); if(texture != null){ customMat.setTexturePointer(texture.getTexturePointer()); hasLoadedTexture = true; } } //this call binds the screen as the "texture" we're rendering to //have to call before actually rendering glBindFramebuffer(GL_FRAMEBUFFER, parentFramebufferPointer); float ndcX = (float)positionX/parentWidth; float ndcY = (float)positionY/parentHeight; float ndcWidth = (float)width/parentWidth; float ndcHeight = (float)height/parentHeight; Vector3f boxPosition = new Vector3f(ndcX,ndcY,0); Vector3f boxDimensions = new Vector3f(ndcWidth,ndcHeight,0); Model planeModel = Globals.assetManager.fetchModel(imagePanelModelPath); if(planeModel != null){ planeModel.pushUniformToMesh("plane", "mPosition", boxPosition); planeModel.pushUniformToMesh("plane", "mDimension", boxDimensions); planeModel.pushUniformToMesh("plane", "tPosition", texPosition); planeModel.pushUniformToMesh("plane", "tDimension", texScale); planeModel.getMeshes().get(0).setMaterial(customMat); planeModel.drawUI(); } else { LoggerInterface.loggerRenderer.ERROR("Image Panel unable to find plane model!!", new Exception()); } if(Globals.RENDER_FLAG_RENDER_UI_BOUNDS){ DebugRendering.drawUIBounds(parentFramebufferPointer, boxPosition, boxDimensions, windowDrawDebugColor); } } public int width = 1; public int height = 1; public int positionX = 0; public int positionY = 0; public int parentWidth = 1; public int parentHeight = 1; public boolean visible = false; public int getWidth() { return width; } public int getHeight() { return height; } public int getPositionX() { return positionX; } public int getPositionY() { return positionY; } public boolean getVisible() { return visible; } public void setWidth(int width) { this.width = width; } public void setHeight(int height) { this.height = height; } public void setPositionX(int positionX) { this.positionX = positionX; } public void setPositionY(int positionY) { this.positionY = positionY; } public void setVisible(boolean draw) { this.visible = draw; } public void setParentWidth(int width){ parentWidth = width; } public void setParentHeight(int height){ this.parentHeight = height; } public boolean handleEvent(Event event){ boolean propagate = true; if(event instanceof DragEvent){ if(onDragStart != null && ((DragEvent)event).getType() == DragEventType.START){ if(!onDragStart.execute((DragEvent)event)){ propagate = false; } } if(onDrag != null && ((DragEvent)event).getType() == DragEventType.DRAG){ if(!onDrag.execute((DragEvent)event)){ propagate = false; } } if(onDragRelease != null && ((DragEvent)event).getType() == DragEventType.RELEASE){ if(!onDragRelease.execute((DragEvent)event)){ propagate = false; } } } return propagate; } @Override public void setOnDragStart(DragEventCallback callback) { onDragStart = callback; } @Override public void setOnDrag(DragEventCallback callback) { onDrag = callback; } @Override public void setOnDragRelease(DragEventCallback callback) { onDragRelease = callback; } }