> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.BlsXmXoE6I9/rev.6
 * 
 * authors: 
 *   GoToLoop

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/**
 * Rectangle Toggle 4 (v4.13)
 * mod GoToLoop (2013/Jun)
 * for GreenMoonSol
 *
 * http://forum.processing.org/topic/problem-with-my-rectangle-class
 *
 * http://forum.processing.org/topic/draggable-balls-to-any-cell
 *
 * http://forum.processing.org/topic
 * /how-to-change-the-colour-of-an-objec-in-an-2d-arrayt-permanently
 *
 * http://forum.processing.org/topic
 * /clicking-a-square-in-a-matrix-of-squares-does-not-work-consistently
 *
 * http://studio.processingtogether.com/sp/pad/export/ro.9L5jAtga7SpyF/latest
 */

final static int DIM = 40, GAP = 15, LEN = DIM + GAP;
final static int GRID = 8, MARGIN = 20;
final boolean[][] squares = new boolean[GRID][GRID];

final static color BG = 0100, FG = #0000FF;
final static color BRIGHT = 050, BLUISH = 0100;

final static String GFX = JAVA2D; // Use JAVA2D or P2D

boolean isMouseOverRect(int x, int y, int w, int h) {
  return mouseX > x & mouseX < x+w & mouseY > y & mouseY < y+h;
}

void setup() {
  size(GRID*LEN + MARGIN*2 - GAP, GRID*LEN + MARGIN*2 - GAP, GFX);
  noLoop();

  noStroke();
  smooth();
  background(BG);

  keyPressed();
}

void draw() {
}

void mousePressed() {
  for (int row=0; row!=GRID; ++row) {
    final int y = row*LEN + MARGIN;

    for (int col=0; col!=GRID; ++col) {
      final int x = col*LEN + MARGIN;

      if ( isMouseOverRect(x, y, DIM, DIM) ) {
        final boolean isSelected = squares[row][col] = !squares[row][col];
        final color c = color(map(x, 0, width, BRIGHT, 0400), 
        map(y, 0, height, BRIGHT, 0400), BLUISH);

        fill(isSelected? FG : c);
        rect(x, y, DIM, DIM);

        redraw();
        return;
      }
    }
  }
}

void keyPressed() {
  for (int row=0; row!=GRID; ++row) {
    final int y = row*LEN + MARGIN;

    for (int col=0; col!=GRID; ++col) {
      final int x = col*LEN + MARGIN;

      fill(map(x, 0, width, BRIGHT, 0400), 
      map(y, 0, height, BRIGHT, 0400), BLUISH);

      rect(x, y, DIM, DIM);

      squares[row][col] = false;
    }
  }

  redraw();
}