My project will involve multiple text input boxes which must move and adjust position dynamically when I resize the window.
This bare-to-basics code only includes two boxes, when the mouse is over a box, the box is highlighted in Red.
If I were to initialise the class in the void setup() as is the norm, it all works, but naturally the boxes will be static, they will not adjust position. So I tried initialising the class in draw(), it won’t work properly as the highlighted box is redrawn and it must keep the current highlighted condition ON whilst the window is resized.
I also tried using this “registerMethod” that Quarks posted a while ago to detect the window resize, and execute something. It almost works, but when the window is resized, and the box is currently highlighted, the highlight in red is lost. In other words, the value of the Boolean boxStroke variable reverts to default (false), highlight OFF.
I also tried a similar test using text input (variable=variable + key), and unfortunately the String variable holding the text also loses value when resizing the screen, i.e. the text disappears.
Is there a way to solve this?
Many thanks.
Test myTest, myTest2 ;
int wid, high;
void setup() {
size(600, 400);
surface.setResizable(true);
registerMethod("pre", this);
}
void pre() {//Detect window resize
if (wid != width || high != height) {
wid = width;
high = height;
myTest = new Test(wid*2/3-30, high*1/3, 60, 20);
myTest2 = new Test(wid*1/3-30, high*1/3, 60, 20);
}
}
void draw() {
background(255);
myTest.display();
myTest2.display();
}
class Test {
String digits = "";
int x;
int y;
int w;
int h;
boolean boxStroke=false;
Test(int x, int y, int w, int h ) {
this.x=x;
this.y=y;
this.w=w;
this.h=h;
}
void display() {
stroke(0);
strokeWeight(1);
noFill();
if (boxStroke) {
stroke(255, 0, 0);
strokeWeight(2);
}
rect(x, y, w, h);
}//end display
void pressMouse() {
if (mouseX<x+w && mouseX>x && mouseY>y && mouseY<y+h) {
boxStroke=true;
} else {
boxStroke=false;
}
}//end presMouse
}//end Class
void mousePressed() {
myTest.pressMouse();
myTest2.pressMouse();
}