package electrosphere.renderer.ui.elements; import electrosphere.main.Globals; import electrosphere.renderer.Material; import electrosphere.renderer.Model; import electrosphere.engine.assetmanager.AssetDataStrings; import electrosphere.renderer.framebuffer.Framebuffer; import electrosphere.renderer.framebuffer.FramebufferUtils; import electrosphere.renderer.texture.Texture; import electrosphere.renderer.ui.DrawableElement; import electrosphere.renderer.ui.Element; import electrosphere.renderer.ui.FocusableElement; import electrosphere.renderer.ui.KeyEventElement; import electrosphere.renderer.ui.events.Event; import electrosphere.renderer.ui.events.FocusEvent; import electrosphere.renderer.ui.events.KeyboardEvent; import electrosphere.renderer.ui.font.FontUtils; import electrosphere.renderer.ui.font.bitmapchar.BitmapCharacter; import org.joml.Vector3f; import static org.lwjgl.opengl.GL30.GL_FRAMEBUFFER; import static org.lwjgl.opengl.GL30.glBindFramebuffer; import java.util.LinkedList; import java.util.List; import java.util.regex.Pattern; /** * * @author amaterasu */ public class TextInput implements DrawableElement, FocusableElement, KeyEventElement { 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; boolean focused = false; FocusEventCallback onFocusCallback; FocusEventCallback onLoseFocusCallback; KeyboardEventCallback onKeyPressCallback; String text = ""; int textPixelWidth = 0; float fontSize = 1.0f; List childrenElements = new LinkedList(); public TextInput(int x, int y, float fontSize){ this.positionX = x; this.positionY = y; this.width = 0; this.height = (int)(FontUtils.getFontHeight() * fontSize); this.fontSize = fontSize; } void generateLetters(){ childrenElements.clear(); int rollingOffset = 0; for(int i = 0; i < text.length(); i++){ char toDraw = text.charAt(i); Vector3f bitMapDimension = FontUtils.getDimensionOfCharacterDiscrete(toDraw); BitmapCharacter newLetter = new BitmapCharacter((int)(rollingOffset * fontSize) + positionX, positionY, (int)(bitMapDimension.x * fontSize), this.height, toDraw); rollingOffset += (int)bitMapDimension.x; childrenElements.add(newLetter); } } public void setText(String text){ this.text = text; textPixelWidth = 0; for(int i = 0; i < text.length(); i++){ Vector3f bitMapDimension = FontUtils.getDimensionOfCharacterDiscrete(text.charAt(i)); textPixelWidth = textPixelWidth + (int)bitMapDimension.x; } generateLetters(); } public void setColor(Vector3f color){ for(BitmapCharacter character : childrenElements){ character.setColor(color); } } public String getText(){ return text; } @Override public void draw(int parentFramebufferPointer, int parentWidth, int parentHeight) { for(DrawableElement child : childrenElements){ child.draw(parentFramebufferPointer, parentWidth, parentHeight); } } public int getWidth() { int minX = -1; int maxX = -1; for(BitmapCharacter child : childrenElements){ if(minX == -1){ minX = child.getPositionX(); } else if(child.getPositionX() < minX){ minX = child.getPositionX(); } if(maxX == -1){ maxX = child.getPositionX() + child.getWidth(); } else if(child.getPositionX() + child.getWidth() > maxX){ maxX = child.getPositionX() + child.getWidth(); } } if(minX == -1){ minX = 0; } if(maxX == -1){ maxX = 0; } return maxX - minX; } public int getHeight() { int minY = -1; int maxY = -1; for(BitmapCharacter child : childrenElements){ if(minY == -1){ minY = child.getPositionY(); } else if(child.getPositionY() < minY){ minY = child.getPositionY(); } if(maxY == -1){ maxY = child.getPositionY() + child.getHeight(); } else if(child.getPositionY() + child.getHeight() > maxY){ maxY = child.getPositionY() + child.getHeight(); } } if(minY == -1){ minY = 0; } if(maxY == -1){ maxY = 0; } return maxY - minY; } public int getPositionX() { int minX = -1; for(BitmapCharacter child : childrenElements){ if(minX == -1){ minX = child.getPositionX(); } else if(child.getPositionX() < minX){ minX = child.getPositionX(); } } if(minX == -1){ if(positionX == 0){ minX = 0; } else { minX = positionX; } } return minX; } public int getPositionY() { int minY = -1; for(BitmapCharacter child : childrenElements){ if(minY == -1){ minY = child.getPositionY(); } else if(child.getPositionY() < minY){ minY = child.getPositionY(); } } if(minY == -1){ if(positionY == 0){ minY = 0; } else { minY = positionY; } } return minY; } 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 posX) { int deltaX = posX - this.positionX; this.positionX = posX; for(Element child : childrenElements){ child.setPositionX(child.getPositionX() + deltaX); } } public void setPositionY(int posY) { int deltaY = posY - this.positionY; this.positionY = posY; for(Element child : childrenElements){ child.setPositionY(child.getPositionY() + deltaY); } } 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 FocusEvent){ FocusEvent focusEvent = (FocusEvent)event; if(focusEvent.isFocused()){ if(this.onFocusCallback != null){ this.onFocusCallback.execute(focusEvent); } else { this.setColor(new Vector3f(1,0,0)); propagate = false; } } else { if(this.onLoseFocusCallback != null){ this.onLoseFocusCallback.execute(focusEvent); } else { this.setColor(new Vector3f(1,1,1)); propagate = false; } } } else if(event instanceof KeyboardEvent){ KeyboardEvent keyEvent = (KeyboardEvent)event; if(onKeyPressCallback != null){ onKeyPressCallback.execute(keyEvent); } else { if(keyEvent.getKey().matches(Pattern.quote("bs"))){ if(this.text.length() > 0){ this.setText(this.text.substring(0, this.text.length() - 1)); } } else { this.setText(this.text + keyEvent.getKey()); } propagate = false; } } return propagate; } @Override public boolean isFocused() { return focused; } @Override public void setOnFocus(FocusEventCallback callback) { onFocusCallback = callback; } @Override public void setOnLoseFocus(FocusEventCallback callback) { onLoseFocusCallback = callback; } @Override public void setOnPress(KeyboardEventCallback callback) { onKeyPressCallback = callback; } }