ControlP5 setLock(true) doesn't work when Button is under mouse

edited March 2017 in Library Questions

Not sure if I am being completely daft here, but I am finding an issue where using setLock(true) to disable a ControlP5 button works only if the button you are modifying doesn't have the mouse over it when setLock(true) is called. Example code (originally by @andreas.schlegel from his answer here)

import controlP5.*;

ControlP5 controlP5;
int col;

void setup() {
  size(640,480);
  smooth();
  frameRate(30);
  controlP5 = new ControlP5(this);
  controlP5.addButton("buttonA",0,100,100,80,19);
  controlP5.addButton("buttonB",255,100,120,80,19);
  controlP5.addButton("buttonC",128,100,140,80,19);

  // store the original background color, we gonna need it later
  col = controlP5.getController("buttonC").getColor().getBackground();
  // lock Controller 'buttonC'
  setLock(controlP5.getController("buttonC"),true);
}


void setLock(Controller theController, boolean theValue) {
  theController.setLock(theValue);
  if(theValue) {
    theController.setColorBackground(color(100,100));
  } else {
    theController.setColorBackground(color(col));
  }
}

void keyPressed() {
  // temporarily enable/disable Controller 'buttonC'
  if(key=='1') {
    setLock(controlP5.getController("buttonC"), false);
  } else if(key=='2') {
    setLock(controlP5.getController("buttonC"), true);
  }
}

void draw() {
  background(0);
}

public void controlEvent(ControlEvent theEvent) {
  println(theEvent.controller().getName());
}

Run this and use the 1 and 2 keys to unlock and lock buttonC. Works well, except if you leave your mouse hovering over the buttonC while pressing 1 and 2. Has anyone come across this before? Is there a workaround? I am happy to try and work out a fix if anyone has any idea roughly where within ControlP5 to go hunting for this!!

FWIW I am using Processing 3.2.3 and ControlP5 2.2.6

Answers

  • Answer ✓

    This did it for me.

    Kf

    void setLock(Controller theController, boolean theValue) {
    
      if (theValue) {
        theController.setColorBackground(color(100, 100));
      } else {
        theController.setColorBackground(color(col));
      }
    
      theController.setMouseOver(false);
      theController.setLock(theValue); 
    }
    
  • Thanks for the answer @kfrajer (and sorry for the delayed reply!). Just had a chance to test this - works perfectly, thank you :-bd

Sign In or Register to comment.