package menu;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import org.lwjgl.input.Mouse;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.World;
import com.threed.jpct.util.Overlay;
import font.GLFont;
public class MenuItem{
private static final int BORDER_SIZE = 5;
private static final int LABEL_OFFSET = 5;
private static final int LABEL_WIDTH = 10;
private Overlay selectLabel;
private int x;
private int y;
private Rectangle focusArea;
private String text;
private GLFont font;
private FrameBuffer buffer;
private World world;
private List<ClickEventHandler> handlers;
private boolean buttonDown;
public MenuItem(World world, FrameBuffer buffer, GLFont font){
this.world = world;
this.buffer = buffer;
this.font = font;
text = "";
handlers = new ArrayList<ClickEventHandler>();
buttonDown = false;
focusArea = new Rectangle();
}
public void setPosition(int x, int y){
this.x = x;
this.y = y;
}
public void setText(String text){
this.text = text;
}
public void addClickListener(ClickEventHandler eh){
handlers.add(eh);
}
public void initialize(){
Dimension d = font.getStringBounds(text);
focusArea.x1 = x - BORDER_SIZE;
focusArea.y1 = y - d.height - BORDER_SIZE;
focusArea.x2 = x + d.width + BORDER_SIZE;
focusArea.y2 = y + BORDER_SIZE;
selectLabel = new Overlay(world, x - BORDER_SIZE - LABEL_OFFSET - LABEL_WIDTH, (int)(y - 0.70 * d.height), x - BORDER_SIZE - LABEL_OFFSET, y + 2, "orange");
selectLabel.setVisibility(false);
selectLabel.setDepth(1.1f);
}
public void update(){
if(checkFocus()){
selectLabel.setVisibility(true);
if(Mouse.isButtonDown(0) && !buttonDown){
buttonDown = true;
raiseClickEvent();
} else if(!Mouse.isButtonDown(0)){
buttonDown = false;
}
}else
selectLabel.setVisibility(false);
font.blitString(buffer, text, x, y, 100, Color.WHITE);
}
private void raiseClickEvent(){
for(ClickEventHandler eh : handlers)
eh.onClick();
}
private boolean checkFocus(){
if(Mouse.getX() > focusArea.x1 && Mouse.getX() < focusArea.x2 && buffer.getOutputHeight() - Mouse.getY() > focusArea.y1 && buffer.getOutputHeight() - Mouse.getY() < focusArea.y2)
return true;
else
return false;
}
public void dispose(){
selectLabel.dispose();
}
}