Check boolean method in all instances for true/false

This is not scalable, what is the correct way to go about checking to see if the boolean method in all instances are false, rather than && each instance?

for (Button b : buttons) {
    b.render();

    if (b.isHovering()) {
      cursor(HAND);
    } else if (!buttons[0].isHovering() && !buttons[1].isHovering() && !buttons[2].isHovering() && !buttons[3].isHovering()) { //there has got to be a shorthand way of doing this, i am just unaware of how
      cursor(ARROW);
    }
  }
1 Like

Hello,

I am not sure what the best way is…

I wrote this to check for a false in the array:

// Check for True False in an array
// v1.0.0
// GLV 2020-MM-DD

boolean [] test;
boolean state = false;
int count = 0;

void setup() 
	{
  size(200, 200);
  
  //create a test array that is all true
  test = new boolean [100];
  for (int i = 0; i<100; i++)
    {
    test[i] = true; 
    }
  // randomly assign a false to one element of test array 
  test[int(random(0, 100))] = false;  
	
  //check for false element in test and set state and return
  //for(boolean j: test)
  //  {    
  //  println(j, count);  
  //  if (!j)
  //    {
  //    state = true;    
  //    break;            //exits when false
  //    }
  //  else
  //    state = false; 
  //  count++; 
  //  }
  
  //Replaces above
  println(checkState(test), count);  
  }

void draw() 
	{
  background(0);
	}

//returns true if it finds a false
boolean checkState(boolean [] arr) 
  {
   for(boolean j:arr)
    {    
    println(j, count);  
    if (!j)
      {
      state = true;    
      break;
      }
    else
      state = false; 
    count++; 
    }
    
  return state;
  }

:)

1 Like

Works for now, still doesn’t feel very SOLID, there has to be a way to iterate through each instance and check the methods return value.

for (int i = 0; i < buttons.length; i++) {
    hovers[i] = buttons[i].isHovering();
  }

 for (Button b : buttons) {
    b.render();
  }
  
  if (checkHoverState(hovers)) {
      cursor(HAND);
    } else {
      cursor(ARROW);
    }
}

boolean checkHoverState(boolean [] arr) {
  for (boolean a : arr) {
    if (a) {
      hoverState= true;
      break;
    } else {
      hoverState= false;
    }
  }
  return hoverState;
}

I appreciate your help … this feels better than where I started.

1 Like

I had a break in there for a reason:
https://processing.org/reference/break.html

If I am checking for all true I break when I find the first false and vice versa.

Feel free to adapt for your own use.

:)

1 Like

You have a for loop inside the for loop now

Bad

1 Like

ahh yeah. makes sense. updated. ty

1 Like

accident … fixed now. ty!

1 Like

looks still very bad…

not needed.

You got 3 for loops instead of 1.

1 Like

how would you optimize it?

1 Like

That’s your good loop. Try to implement the other steps inside it

2 Likes

k … i see what your getting at

1 Like

say this before the for loop - that’s your default

Don’t use break obviously. (You want all buttons to render.)

Then


if (b.isHovering()) {
      hoverState= true;
} 

(without the else-part)

This means, when only ONE b has the mouse hovering, hoverState gets true and stays true.

Evaluate hoverState after the for loop

Chrisir

3 Likes

Sorry I was getting confused with the method glv proposed and yours …

this is perfect! ty!!

hoverState = false;
  for (Button b : buttons) {
    b.render();
    if (b.isHovering()) {
      hoverState = true;
    }
  }

  if (hoverState) {
    cursor(HAND);
  } else {
    cursor(ARROW);
  }
3 Likes