Mouse x y coordinates (ControlP5)

Thank you for reading this. The following is a summary of the current code.

I have an array of circles that represent LEDs.

I have radio buttons, using the P5 library, to select a colour.

What I’d like to do is click on an LED to select it. From there I can change its status (whether it’s off or on), its on colour or its off colour.

I know the x and y position of the centre of the LEDs and I know its diameter and to avoid calculating the range of x and y values every time a LED is clicked on I could created a look-up table for each LED.

This might be OK for an array of 8 LEDs but it will soon get out of hand if I use the same idea for say 64 LEDS.

What I have created so far seems a bit clumsy. Is there a better way?

I wrote this yesterday and in the meantime I had an idea. I could create an array of P5 buttons instead of LEDs. That would mean, as far as I know, that I would then need a function for each button. Also, how do I create a button without a label?

Yes set the x y coordinates in an array and use something like code below…
For a radio button without label, just leave the string empty, like .addItem("",1)

void mousePressed() {
  for (int i = 0; i < 3; i++) {
    if (mouseX > bx_val[i] && mouseX < bx_val[i] +button_width && mouseY > bys && mouseY < bys+button_height) {
      switch(i) {
      case 0:
        if conditional;
        break;
      case 1:
        if conditional;
        break;
      case 2:
         if conditional;
        break;
      }
    }
  }

Thank noel you for your reply.

I was thinking that a long switch / case statement would be untidy but so be it.

Also , thank you for your .addItem() answer, I couldn’t see beyond something like the following:

cp.addRadioButton("radioButton")

void raioButton(int b)

when you have the index of the button you can use the index maybe as an index for your LEDs array

Thank you Chrisir,

I’ve made some headway with mouse x y idea. However, as you say, a button index would would select an LED without any calculation. I’ll experiment.

For example when the leds are in a array you can convert mouse position to index

Like with division or modulo

Similar problem:

Getting the color of a single pixel with get(x, y) is easy, but not as fast as grabbing the data directly from pixels[] . The equivalent statement to get(x, y) using pixels[] is pixels[y*width+x] .

Thank you again, Chrisir.

I have used get(x,y) to rotate colours through an array of LEDs based on knowing the colour of the pixel at the centre of the LED. I had seen the reference to pixels[] but haven’t tried it. I will have a think about your suggestion regarding mouse position to array index.

1 Like