Good afternoon, I need to add several conditions to validate two fields in the current code.
- The Min field must not exceed the Max field. Min <Max / 5000 <4000 prohibited when entering.
- The Max field must not be less than the Min field. Max <Min / 4000 <5000 prohibited when entering.
- The Min field should not exceed the interval from the Max field, 500 units less can be entered. For instance: Min 4500 and Max 5000, 4400/5000, 4250/5000, 4501/5000 are prohibited to enter and above.
- The Min and Max fields must not be equal. Min = Max is prohibited when entering.
code:
import controlP5.*;
ControlP5 cp5;
Textfield O;
Textfield OO;
void setup() {
size(700, 400);
PFont font = createFont("arial", 18);
cp5 = new ControlP5(this);
O = cp5.addTextfield("MIN")
.setPosition(20, 100)
.setSize(200, 40);
O.setInputFilter(ControlP5.INTEGER)
.setFont(font)
.setColor(color(255, 0, 0));
OO = cp5.addTextfield("MAX")
.setPosition(20, 170)
.setSize(200, 40);
OO.setInputFilter(ControlP5.INTEGER)
.setFont(font);
textFont(font);
}
void draw() {
if (keyPressed && OO.isFocus()) {
float n;
try {
n = Float.parseFloat(OO.getText().replace(',', '.'));
if (!(n >= 1 && n <= 12000)) {
throw new NumberFormatException(); // throw to catch below
}
}
catch (Exception e2) {
String t;
if (OO.getText().length() > 1) {
t = OO.getText().substring(0, OO.getText().length() - 1);
} else {
t = "";
}
OO.setText(t);
}
}
if (keyPressed && O.isFocus()) {
float n;
try {
n = Float.parseFloat(O.getText().replace(',', '.'));
if (!(n >= 1 && n <= 11500)) {
throw new NumberFormatException(); // throw to catch below
}
}
catch (Exception e2) {
String t;
if (O.getText().length() > 1) {
t = O.getText().substring(0, O.getText().length() - 1);
} else {
t = "";
}
O.setText(t);
}
}
background(0);
fill(255);
}
Overall it sounds like you're trying to have the user enter a range of valid values (where the minimum is always smaller than the maximum). There's already a ControlP5 controller for that: Range
Other than allowing to set a min and max value within a range is the constraint to keep a difference between max and min value of at least 500.
You could get away with making the Range slider handles 0px wide, essentially disabling them which means the range you set at the start (via
setRangeValues
) will be maintained:The one limitation is that ranges can't be > 500. If that's a requirment, you can still manually constrain the values (by setting the range min(low)/max(high) values):
If that takes too much space you can use Numberbox which compared to the text field has a few advantages:
Here's an example:
The logic constrain values to a minimum 500 difference is not 100% tight, there may some other edge cases I haven't considered. It's more of a way to illustrate ways of solving the problem so you're better equipt to do so.
I would recommend going through Processing > Examples > Contributed Libraries > ControlP5 and running the examples, in particular the controllers. You can prioritise the ones that sound closer to your current problem, but it's worth getting experience with the options so you can choose the best controllers/UI element to fit your problem.
The example may not include usage of every method the controller has, however there's a comment list at the bottom you could easily copy/paste/run immediately. Additionally of course you have the full documentation