Initialize function with CP5 button

I have this code that draws hexagons randomly in a grid and coloring them randomly from an array.

What I want to do is press a CP5 button to initialize the drawing function. If I press this button again, it will fill all drawn hexagons with black and re-initialize the drawing function.

Currently this controls the drawing:

void mousePressed() {

  if (active_counter == 0) {

    hexagons[int(wide/2)][int(tall/2)].active = true;

    active_counter++;
  }
}

So I tried this approach:

public void startDrawing(int theValue) {
    println("a button event from startDrawing: "+theValue);
    c1 = c2;
    c2 = color(0,0,0);

    if (active_counter == 40) {
      fill(color(0));
    }
}

Now, I know this is not working, because “fill” is not connected to “hexagons”, but I have difficulty appending it the right way.

This is the full code:

import controlP5.*;

ControlP5 cp5;

int myColor = color(255);

int c1,c2;

float n,n1;

// ------------- Hexagon grid -------------
Hexagon[] [] hexagons;

float rx = 51;
float ry = 56;
int wide;
int tall;

int hexagon;

int active_counter = 0;
boolean active;
int max_hexagons;

float Xoffset, Yoffset;
int randX;
int randY;
int randD;
int x;
int y;

int[] stepX = { -1, -1, 1, 0, 1, 1 };
int[][] stepY = {{ 0, 1, -1, 1, 0, 1 }, { 0, -1, -1, 1, 0, -1 } };

class Hexagon {
  float x;
  float y;
  int rand;
  color[] colors;
  boolean active;

  Hexagon (float ix, float iy) {
    x = ix;
    y = iy;
    active = false;

    // Array of colors
    colors = new color[10];
    colors[0] = color(#62563D); // Infantry green
    colors[1] = color(#2C2B2D); // Parisian night blue
    colors[2] = color(#3E2224); // Purple heart
    colors[3] = color(#A49F9B); // Wild dove grey
    colors[4] = color(#684F40); // Brown
    colors[5] = color(#5C573D); // Moss green
    colors[6] = color(#B9897F); // Pink
    colors[7] = color(#24283B); // Dark blue
    colors[8] = color(#1F1D20); // Black
    colors[9] = color(#C5A9A2); // Brazilian clay

    // Takes the colors array and output random colors
    rand = (int)random(colors.length);
  }
  // ------------- Nested draw -------------
  void draw() {
    if (active) {

      // Call the colors array in random order
      fill(colors[rand]);
    } else {
      noFill();
    }

    stroke(80);
    pushMatrix();
    translate(x, y);
    beginShape();
    vertex(-17, -28);
    vertex(17, -28);
    vertex(34, 0);
    vertex(17, 28);
    vertex(-17, 28);
    vertex(-34, 0);
    endShape(CLOSE);
    popMatrix();
  }
}

// ------------- setup -------------
void setup() {
  size(1000, 700);

  // ------------- CP5 control buttons -------------
  cp5 = new ControlP5(this);

  cp5.addButton("start drawing")
     .setValue(0)
     .setPosition(20,80)
     .setSize(200, 19)
     ;

  // ------------- Create hexagon grid -------------
  wide = int(width/rx) + 2;

  tall = int (height/ry) + 2;

  max_hexagons = 40;

  hexagons = new Hexagon [wide] [tall];

  for (int y = 0; y < tall; y++) {
    for (int x = 0; x < wide; x++) {

      if ( x % 2 == 0) {

        hexagons[x] [y] = new Hexagon(x*rx, y*ry);

      } else {

        hexagons[x] [y] = new Hexagon(x*rx, (y-.5)*ry);
      }
    }
  }

  float targetX = width/2;
  float targetY = height/2;

  Xoffset = hexagons [int(wide/2)] [int(tall/2)] .x - targetX;
  Yoffset = hexagons [int(wide/2)] [int(tall/2)] .y - targetY;
}

// ------------- draw -------------
void draw () {
  background(0);
  pushMatrix();
  translate(-Xoffset, -Yoffset);
  for (int y = 0; y < tall; y++) {
    for (int x = 0; x < wide; x++) {

      hexagons[x] [y] .draw();
    }
  }
  if (active_counter > 0 && active_counter < max_hexagons) {
    nextHex();
  }
  popMatrix();
  myColor = lerpColor(c1,c2,n);
    n += (1-n)* 0.1; 
} 
  public void controlEvent(ControlEvent theEvent) {
    println(theEvent.getController().getName());
    n = 0;
  }

  public void startDrawing(int theValue) {
    println("a button event from startDrawing: "+theValue);
    c1 = c2;
    c2 = color(0,0,0);

    if (active_counter == 40) {
      fill(color(0));
    }

  }

// ------------- Mouse interaction -------------
void mousePressed() {

  if (active_counter == 0) {

    hexagons[int(wide/2)][int(tall/2)].active = true;

    active_counter++;
  }
}

// ------------- Drawing next hexagon -------------
void nextHex() {
  int randX = int(random(wide));
  int randY = int(random(tall));
  while (!hexagons[randX][randY] .active) {
    randX = int(random(wide));
    randY = int(random(tall));
  } 
  int randD = int(random(6));
  if (
    randX + stepX[randD] >= 0 && 
    randX + stepX[randD] < wide &&
    randY + stepY[randX % 2][randD] >= 0 && 
    randY + stepY[randX % 2][randD] < tall
    ) {
    if (!hexagons[randX + stepX[randD]][randY + stepY[randX % 2][randD]] .active) {
      active_counter++;
    }
    hexagons[randX + stepX[randD]][randY + stepY[randX % 2][randD]] .active = true;
  }
}

Answers

  • edited May 2017 Answer ✓

    @kasperrubin

    Still unaswered, i have no time to debug you code, i just look at your code here,

    Some thoughts :

    You have a global var active_counter =0;
    and then you place this var at several places if you call a function the gloabl value is eval first and then it will do some work in function scope

    global scope
    var visibel
    first scope {
    visibel=new value
    }
    second scope {
    print(visibel) //global scope value
    }

    this is misleading, as i said i only look at you code here
    ...try to overload

    function a(int x)
    if(something good happens) active_counter=x
    else call function b(x)
    active_counter++
    end function a


    Try to use print (value)
    i don't think : - Mouse interaction - active_counter++; do anything

    you have a draw() function best would be after that if a simpel cls function would work

    myClass {
    draw()
    cls()
    }

    also you could reset the object by swapping "buffers" with an empty buffer

    Hexagon[] [] hexagons;
    Hexagon[] [] empyhexagons[NULL][NULL]
    if(armageddon==true) hexagons = emptyhexagons;

    worst case could also work:

    hexagons = null;

    Good Luck

  • Thank you @nabr

    I did get everything working, but it required an extensive amount of changes.

Sign In or Register to comment.