Renderer/src/main/java/electrosphere/renderer/ui/elements/ImagePanel.java
austin 7c8e536f80
All checks were successful
studiorailgun/Renderer/pipeline/head This commit looks good
bare minimum working ui update
2024-04-04 18:15:10 -04:00

165 lines
5.4 KiB
Java

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.OpenGLState;
import electrosphere.renderer.RenderPipelineState;
import electrosphere.renderer.debug.DebugRendering;
import electrosphere.renderer.model.Material;
import electrosphere.renderer.model.Model;
import electrosphere.renderer.texture.Texture;
import electrosphere.renderer.ui.elementtypes.DraggableElement;
import electrosphere.renderer.ui.elementtypes.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 extends StandardElement 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){
super();
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(
RenderPipelineState renderPipelineState,
OpenGLState openGLState,
int parentFramebufferPointer,
int parentPosX,
int parentPosY,
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 boolean visible = false;
public boolean getVisible() {
return visible;
}
public void setVisible(boolean draw) {
this.visible = draw;
}
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;
}
}