sojamo / controlp5

A gui library for processing.org
GNU Lesser General Public License v2.1
490 stars 142 forks source link

Controller "hitboxes" don't update when the dimension of the controller is changed #156

Closed BenJourdan closed 5 years ago

BenJourdan commented 5 years ago

I've been trying to create a gui which can respond to the window being resized by the user. I've noticed that when the user resizes the window, after I update the dimensions of a controller with setWidth etc the visual dimensions of the controller are updated but not the "hitbox" used to determine if the mouse is over it. Here's some code which should demonstrate this:

` import controlP5.*;

ControlP5 cp5; Controller bang;

int currWidth; int currHeight;

void settings(){ size(800,400); }

void setup(){

cp5 = new ControlP5(this); surface.setResizable(true);

bang = cp5.addBang("bang");

registerMethod("pre",this); }

void draw(){

}

public void pre() {

// Fix Window resizing if it has occured
if (currWidth != width || currHeight != height) {
    currWidth = width;
    currHeight = height;

    //scale bang to match the window
    bang.setSize((int)(0.25*(float)width),(int)(0.25*(float)height));
    bang.setPosition((int)(0.5*(float)width),(int)(0.5*(float)height));

}

} `

Try making the window fullscreen and clicking on the bang. I couldn't. I've had a bit of a look through the Controller class and I could be wrong but I think the function inside(), implemented on line 813 is where things start going dodgy. I think var1 and var3 may be ok as I think the issue is that the "hitbox" starts in the right place but is too small, hence why I think var1 and var3 may be ok. Var2 and Var4 on the other hand... I think the issue must be with those. I could be wrong of course.

BenJourdan commented 5 years ago

I think I've found a fix. "cp5.setGraphics(this,0,0);" forces controlp5 to update what it thinks the width and height of the window is. The two zeros are used to initialize variables "pgx" and "pgy" which happens at the same time (line 173 of ControlP5). I don't have a clue what these are. Does setting them back to zero break anything else?