package com.asciiterminal.ui;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

/**
 * A simple button
 * 
 * @author julien MAITRE
 *
 */
public class AsciiTerminalButton extends Actor {
	protected final AsciiTerminal asciiTerminal;
	protected String label;
	protected int x;
	protected int y;
	protected ClickListener clickListener;
	protected boolean isDisabled = false;
	protected Color mouseDefaultColor;
	protected Color mouseClickedColor;
	protected Color mouseEnteredColor;
	protected Color mouseDisabledColor;
	protected Color mouseBackgroundColor;

	public AsciiTerminalButton(AsciiTerminal asciiTerminal, String label, int x, int y, Color mouseDefaultColor, Color mouseClickedColor) {
		this(asciiTerminal, label, x, y, mouseDefaultColor, mouseClickedColor, asciiTerminal.getDefaultCharacterBackgroundColor());
	}

	public AsciiTerminalButton(AsciiTerminal asciiTerminal, String label, int x, int y, Color mouseDefaultColor, Color mouseClickedColor, Color mouseBackgroundColor) {
		this(asciiTerminal, label, x, y, mouseDefaultColor, mouseClickedColor, mouseDefaultColor, mouseBackgroundColor);
	}

	public AsciiTerminalButton(AsciiTerminal asciiTerminal, String label, int x, int y, Color mouseDefaultColor, Color mouseClickedColor, Color mouseEnteredColor, Color mouseBackgroundColor) {
		this(asciiTerminal, label, x, y, mouseDefaultColor, mouseClickedColor, mouseEnteredColor, mouseDefaultColor, mouseBackgroundColor);
	}

	public AsciiTerminalButton(AsciiTerminal asciiTerminal, String label, int x, int y, Color mouseDefaultColor, Color mouseClickedColor, Color mouseEnteredColor, Color mouseDisabledColor, Color mouseBackgroundColor) {
		this.asciiTerminal = asciiTerminal;
		this.label = label;
		this.x = x;
		this.y = y;
		this.mouseDefaultColor = mouseDefaultColor;
		this.mouseClickedColor = mouseClickedColor;
		this.mouseEnteredColor = mouseEnteredColor;
		this.mouseDisabledColor = mouseDisabledColor;
		this.mouseBackgroundColor = mouseBackgroundColor;

		sizeChanged();
		initialize();
	}

	protected void initialize () {
		setTouchable(Touchable.enabled);
		addListener(clickListener = new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if(isDisabled()) return;
			}
		});
	}

	public void hasSizeChanged() {
		sizeChanged();
	}

	@Override
	protected void sizeChanged() {
		this.setX(x * asciiTerminal.getCharacterWidth() * asciiTerminal.getScale());
		this.setY(y * asciiTerminal.getCharacterHeight() * asciiTerminal.getScale());
		this.setWidth(label.length() * asciiTerminal.getCharacterWidth() * asciiTerminal.getScale());
		this.setHeight(asciiTerminal.getCharacterHeight() * asciiTerminal.getScale());
	}

	@Override
	public void draw(Batch batch, float parentAlpha) {
		boolean isDisabled = isDisabled();
		boolean isPressed = isPressed();
		boolean isOver = isOver();

		Color current = mouseDefaultColor;
		if(isDisabled) {
			current = mouseDisabledColor;
		}
		else if(isPressed) {
			current = mouseClickedColor;
		}
		else if(isOver) {
			current = mouseEnteredColor;
		}

		asciiTerminal.writeString(x, y, this.label, current, this.mouseBackgroundColor);
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public boolean isDisabled() {
		return isDisabled;
	}

	public void setDisabled(boolean disabled) {
		isDisabled = disabled;
	}

	public boolean isPressed() {
		return clickListener.isVisualPressed();
	}

	public boolean isOver() {
		return clickListener.isOver();
	}

	public ClickListener getClickListener() {
		return clickListener;
	}
}

@@ -3839,11 +3839,945 @@
 ener;%0A%09%7D
+%0A%0A%09public Color getMouseDefaultColor() %7B%0A%09%09return mouseDefaultColor;%0A%09%7D%0A%0A%09public void setMouseDefaultColor(Color mouseDefaultColor) %7B%0A%09%09this.mouseDefaultColor = mouseDefaultColor;%0A%09%7D%0A%0A%09public Color getMouseClickedColor() %7B%0A%09%09return mouseClickedColor;%0A%09%7D%0A%0A%09public void setMouseClickedColor(Color mouseClickedColor) %7B%0A%09%09this.mouseClickedColor = mouseClickedColor;%0A%09%7D%0A%0A%09public Color getMouseEnteredColor() %7B%0A%09%09return mouseEnteredColor;%0A%09%7D%0A%0A%09public void setMouseEnteredColor(Color mouseEnteredColor) %7B%0A%09%09this.mouseEnteredColor = mouseEnteredColor;%0A%09%7D%0A%0A%09public Color getMouseDisabledColor() %7B%0A%09%09return mouseDisabledColor;%0A%09%7D%0A%0A%09public void setMouseDisabledColor(Color mouseDisabledColor) %7B%0A%09%09this.mouseDisabledColor = mouseDisabledColor;%0A%09%7D%0A%0A%09public Color getMouseBackgroundColor() %7B%0A%09%09return mouseBackgroundColor;%0A%09%7D%0A%0A%09public void setMouseBackgroundColor(Color mouseBackgroundColor) %7B%0A%09%09this.mouseBackgroundColor = mouseBackgroundColor;%0A%09%7D
 %0A%7D%0A

Add button setter & getter