package plot;

import java.awt.*;
import real.Formats;

public class PixelBox {
	RealGraphics gfx;
	double x[];
	int n;
	Color c = Color.lightGray;
	
	public PixelBox(RealGraphics gfx, double x[], int n) {
		this.gfx = gfx;
		this.x = x;
		this.n = n;
		active = true;
	}
	
	public PixelBox(RealGraphics gfx, double x[], int n, Color c) {
		this.gfx = gfx;
		this.x = x;
		this.n = n;
		this.c = c;
		active = true;
	}
	
	public double[] location() {
		return(x);
	}
	
	public void setColor(Color c) {
		this.c = c;
	}
	
	public void draw() {
		if (active) {
			int centre[] = gfx.toPoint(x);
			centre[0] -= n/2;
			centre[1] -= n/2;
			Color d = gfx.getColor();
			gfx.setColor(c);
			gfx.gfx.fillRect(centre[0], centre[1], n, n);
			gfx.setColor(Color.black);
			gfx.gfx.drawRect(centre[0], centre[1], n, n);
			gfx.setColor(d);
		}
	}
	
	boolean active;
	public void setActive(boolean a) {
		active = a;
	}
	
	public boolean isActive() {
		return(active);
	}
	
	double offset[];
	public Object mouseDown(double y[]) {
		if (active) {
			int centre[] = gfx.toPoint(x);
			centre[0] -= n/2;
			centre[1] -= n/2;
			int z[] = gfx.toPoint(y);
			offset = new double[2];
			offset[0] = x[0] - y[0];
			offset[1] = x[1] - y[1];
			// offset = centre - mouse
			boolean grabbed = ( z[0] >= centre[0] 
				&& z[0] <  centre[0] + n
				&& z[1] >= centre[1]
				&& z[1] <  centre[1] + n );
			if (grabbed) return(this);
		} 
		return(null);
	}
	
	public Object mouseDrag(double y[]) {
		if (active) {
			x[0] = y[0] + offset[0];
			x[1] = y[1] + offset[1];
			return(this);
		} else return(null);
	}
	
	public Object mouseUp(double y[]) {
		if (active) return(this);
		else return(null);
	}
	
	public void locate(double x[]) {
		this.x = x;
	}
}

