272 lines
7.4 KiB
Java
272 lines
7.4 KiB
Java
package electrosphere.renderer.ui.elements;
|
|
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
|
|
import org.joml.Vector3f;
|
|
|
|
import electrosphere.renderer.ui.ClickableElement;
|
|
import electrosphere.renderer.ui.ContainerElement;
|
|
import electrosphere.renderer.ui.DrawableElement;
|
|
import electrosphere.renderer.ui.Element;
|
|
import electrosphere.renderer.ui.FocusableElement;
|
|
import electrosphere.renderer.ui.events.ClickEvent;
|
|
import electrosphere.renderer.ui.events.Event;
|
|
import electrosphere.renderer.ui.events.FocusEvent;
|
|
import electrosphere.renderer.ui.events.MouseEvent;
|
|
|
|
public class Button implements DrawableElement, FocusableElement, ContainerElement, ClickableElement {
|
|
|
|
List<Element> childList = new LinkedList<Element>();
|
|
|
|
int width = -1;
|
|
int height = -1;
|
|
|
|
int positionX = -1;
|
|
int positionY = -1;
|
|
|
|
int parentWidth = 1;
|
|
int parentHeight = 1;
|
|
|
|
boolean visible = false;
|
|
boolean focused = false;
|
|
|
|
FocusEventCallback onFocusCallback;
|
|
FocusEventCallback onLoseFocusCallback;
|
|
ClickEventCallback clickCallback;
|
|
|
|
public int getWidth() {
|
|
if(width == -1){
|
|
int minX = -1;
|
|
int maxX = -1;
|
|
for(Element child : childList){
|
|
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;
|
|
} else {
|
|
return width;
|
|
}
|
|
}
|
|
|
|
public int getHeight() {
|
|
if(height == -1){
|
|
int minY = -1;
|
|
int maxY = -1;
|
|
for(Element child : childList){
|
|
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;
|
|
} else {
|
|
return height;
|
|
}
|
|
}
|
|
|
|
public int getPositionX() {
|
|
if(positionX == -1){
|
|
int minX = -1;
|
|
for(Element child : childList){
|
|
if(minX == -1){
|
|
minX = child.getPositionX();
|
|
} else if(child.getPositionX() < minX){
|
|
minX = child.getPositionX();
|
|
}
|
|
}
|
|
if(minX == -1){
|
|
minX = 0;
|
|
}
|
|
return minX;
|
|
} else {
|
|
return positionX;
|
|
}
|
|
}
|
|
|
|
public int getPositionY() {
|
|
if(positionY == -1){
|
|
int minY = -1;
|
|
for(Element child : childList){
|
|
if(minY == -1){
|
|
minY = child.getPositionY();
|
|
} else if(child.getPositionY() < minY){
|
|
minY = child.getPositionY();
|
|
}
|
|
}
|
|
if(minY == -1){
|
|
minY = 0;
|
|
}
|
|
return minY;
|
|
} else {
|
|
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 posX) {
|
|
int deltaX = posX - getPositionX();
|
|
this.positionX = posX;
|
|
for(Element child : childList){
|
|
child.setPositionX(child.getPositionX() + deltaX);
|
|
}
|
|
}
|
|
|
|
public void setPositionY(int posY) {
|
|
int deltaY = posY - getPositionY();
|
|
this.positionY = posY;
|
|
for(Element child : childList){
|
|
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;
|
|
}
|
|
|
|
@Override
|
|
public boolean isFocused() {
|
|
return focused;
|
|
}
|
|
|
|
void onFocus(FocusEvent event) {
|
|
if(onFocusCallback != null){
|
|
onFocusCallback.execute(event);
|
|
} else {
|
|
for(Element child : childList){
|
|
if(child instanceof Label){
|
|
Label childLabel = (Label) child;
|
|
childLabel.setColor(new Vector3f(1,0,0));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void onLoseFocus(FocusEvent event) {
|
|
if(onLoseFocusCallback != null){
|
|
onLoseFocusCallback.execute(event);
|
|
} else {
|
|
for(Element child : childList){
|
|
if(child instanceof Label){
|
|
Label childLabel = (Label) child;
|
|
childLabel.setColor(new Vector3f(1,1,1));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void draw(int parentFramebufferPointer, int parentWidth, int parentHeight) {
|
|
for(Element child : childList){
|
|
if(child instanceof DrawableElement){
|
|
DrawableElement drawableChild = (DrawableElement) child;
|
|
drawableChild.draw(parentFramebufferPointer,parentWidth,parentHeight);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void addChild(Element child) {
|
|
childList.add(child);
|
|
if(child instanceof DrawableElement){
|
|
DrawableElement drawableChild = (DrawableElement) child;
|
|
drawableChild.setParentWidth(width);
|
|
drawableChild.setParentHeight(height);
|
|
drawableChild.setVisible(false);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<Element> getChildren() {
|
|
return childList;
|
|
}
|
|
|
|
@Override
|
|
public void removeChild(Element child) {
|
|
childList.remove(child);
|
|
}
|
|
|
|
@Override
|
|
public void setOnFocus(FocusEventCallback callback) {
|
|
onFocusCallback = callback;
|
|
}
|
|
|
|
@Override
|
|
public void setOnLoseFocus(FocusEventCallback callback) {
|
|
onLoseFocusCallback = callback;
|
|
}
|
|
|
|
@Override
|
|
public void setOnClick(ClickEventCallback callback) {
|
|
clickCallback = callback;
|
|
}
|
|
|
|
public boolean handleEvent(Event event){
|
|
if(event instanceof MouseEvent){
|
|
return false;
|
|
}
|
|
if(event instanceof FocusEvent){
|
|
FocusEvent focusEvent = (FocusEvent) event;
|
|
if(focusEvent.isFocused()){
|
|
this.focused = true;
|
|
onFocus(focusEvent);
|
|
} else {
|
|
this.focused = false;
|
|
onLoseFocus(focusEvent);
|
|
}
|
|
return false;
|
|
}
|
|
if(event instanceof ClickEvent){
|
|
if(clickCallback != null){
|
|
clickCallback.execute((ClickEvent)event);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|