package xbhs.battleship.gui;

import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.util.Arrays;

import processing.core.PApplet;

public class GUI extends PApplet {
 
  GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  int initWidth = gd.getDisplayMode().getWidth()/2;
  int initHeight = gd.getDisplayMode().getHeight()/2;
  boolean mousePressedLastFrame = false;
  /**
   * Placeholder variable stores if a grid space
   * should be colored to test click detection
   */
  boolean[][] grid = new boolean[10][10];
  @Override
  public void setup() 
  {
	for(int i = 0; i < 10; i++)
		Arrays.fill(grid[i], false);
	size(initWidth,initHeight);
    background(0);
  }

  @Override
  public void draw() 
  {
    stroke(255);
    int sideLength = getHeight();
    int startX = (getWidth() - sideLength) / 2;
    int delta = sideLength / 10;
	if(mousePressed != mousePressedLastFrame && !mousePressedLastFrame)
	{
		int[] coords = getGridSquare(mouseX, mouseY);
		if(coords != null)
			grid[coords[0]][coords[1]] = !grid[coords[0]][coords[1]];
	}
	mousePressedLastFrame = mousePressed;
    for(int i = 0; i < 10; i++)
    	for(int j = 0; j < 10; j++)
    		if(grid[i][j])
    			fillSquare(i, j, Color.RED);
    		else
    			fillSquare(i, j, getForeground());
    for(int i = 0; i < 11; i++)
    {
    	line(startX, i * delta, startX + sideLength, i * delta);
    	line(i * delta + startX, 0, i * delta + startX, sideLength);
    }
  }
  
  /**
   * Gets the grid space a given coordinate is inside of
   */
  public int[] getGridSquare(int x, int y)
  {
	int sideLength = getHeight();
	int startX = (getWidth() - sideLength) / 2;
	int delta = sideLength / 10;
	if(x < startX || x > startX + sideLength)
		return null;
	int[] coords = new int[2];
	for(int i = 0; i < 11; i++)
		if(x < startX + (i * delta))
		{
			coords[0] = i;
			break;
		}
	for(int i = 0; i < 11; i++)
		if(y < i * delta)
		{
			coords[1] = i;
			break;
		}
	/**
	 * To avoid indexOutOfBoundsExceptions on the grid array
	 */
	coords[0] += coords[0] == 0 ? 0 : -1;
	coords[1] += coords[1] == 0 ? 0 : -1;
	return coords;
  }
  
  public void fillSquare(int x, int y, Color color)
  {
	  int sideLength = getHeight();
	  int startX = (getWidth() - sideLength) / 2;
	  int delta = sideLength / 10;
	  fill(color.getRGB());
	  beginShape();
	  	vertex((float)x * delta + startX, (float) y * delta);
	  	vertex((float)x * delta + startX, (float) (y + 1) * delta);
	  	vertex((float)(x + 1) * delta + startX, (float) (y + 1) * delta);
	  	vertex((float)(x + 1) * delta + startX, (float) y * delta);
	  endShape();
  }
  
  public static void main(String args[]) 
  {
    PApplet.main("xbhs.battleship.gui.GUI");
  }
}
@@ -137,17 +137,74 @@
 il.Array
-s
+List;%0Aimport java.util.Arrays;%0A%0Aimport javax.swing.JButton
 ;%0A%0Aimpor
@@ -513,16 +513,76 @@
  false;%0A
+  ArrayList%3CString%3E buttonTitles = new ArrayList%3CString%3E();%0A
   /**%0A  
@@ -870,16 +870,145 @@
 und(0);%0A
+    /**%0A    buttonTitles.add(%22A button%22);%0A    buttonTitles.add(%22Another button%22);%0A    buttonTitles.add(%22A third button%22);%0A    */%0A
   %7D%0A%0A  @
@@ -1170,24 +1170,86 @@
 ength / 10;%0A
+%09// Fixes issues with truncation%0A    sideLength = delta * 10;%0A
 %09if(mousePre
@@ -1827,16 +1827,36 @@
 ;%0A    %7D%0A
+    //addButtons();%0A
   %7D%0A  %0A 
@@ -2072,24 +2072,83 @@
 ength / 10;%0A
+%09// Fixes issues with truncation%0A%09sideLength = delta * 10;%0A
 %09if(x %3C star
@@ -2739,16 +2739,79 @@
 h / 10;%0A
+%09  // Fixes issues with truncation%0A%09  sideLength = delta * 10;%0A
 %09  fill(
@@ -3100,16 +3100,16 @@
 delta);%0A
-
 %09  endSh
@@ -3114,32 +3114,412 @@
 Shape();%0A  %7D%0A  %0A
+  public void addButtons()%0A  %7B%0A%09  int minX = 0, maxX = (getWidth() - getHeight()) / 2;%0A%09  int deltaY = getHeight()/buttonTitles.size();%0A%09  for(int i = 0; i %3C buttonTitles.size(); i++)%0A%09%09  addButton(minX, maxX, i * deltaY, i + 1 * deltaY, buttonTitles.get(i));%0A  %7D%0A  %0A  public void addButton(int minX, int maxX, int minY, int maxY, String text)%0A  %7B%0A%09  //TODO: Fill this out%0A  %7D%0A  %0A
   public static 

Work on buttons and Bugfixes