Good afternoon, I was already prompted how this can be done with cp5.addNumberbox, but I tried different options and still could not beat cp5.addTextfield.
Nothing works!
The tasks were as follows:
1). The minValue field must not exceed the MaxValue field when entering numbers.
2). The MaxValue field must not be less than the minValue field when entering numbers.
3). The minValue field must not exceed the range from the maxValue field by 500 units.
I post the simplest version of the code without my fanaticism:
import controlP5.*;
ControlP5 cp5;
// range constants
final int RANGE_MIN = 4000;
final int RANGE_MAX = 5000;
// the smallest allowed difference between min/max values
final int RANGE_MIN_DIFFERENCE = 500;
final int RANGE_MID = RANGE_MIN + ((RANGE_MAX - RANGE_MIN) / 2);
int minValue;
int maxValue;
Textfield inputMin;
Textfield inputMax;
void setup() {
size(700, 400);
PFont font = createFont("arial", 18);
cp5 = new ControlP5(this);
inputMin = cp5.addTextfield("minValue")
.setPosition(100, 100)
.setSize(100, 20)
.setFont(font)
//.setScrollSensitivity(1.1)
// set initial acceptable range
.setMin(RANGE_MIN)
.setMax(RANGE_MAX)
// set default value
.setValue(4000)
;
inputMax = cp5.addTextfield("maxValue")
.setPosition(100, 150)
.setSize(100, 20)
.setFont(font)
//.setScrollSensitivity(1.1)
// set initial acceptable range
.setMin(RANGE_MIN)
.setMax(RANGE_MAX)
// set default value
.setValue(RANGE_MID + 1)
;
textFont(font);
}
void draw() {
constrainRangeInputs();
background(0);
fill(255);
text("minValue: " + minValue + "\n" +
"maxValue: " + maxValue, 10, 15);
}
void constrainRangeInputs() {
int rangeMinInt = (int)inputMin.getValue();
int rangeMaxInt = (int)inputMax.getValue();
//
if (abs(rangeMaxInt - rangeMinInt) < RANGE_MIN_DIFFERENCE) {
if (rangeMaxInt > RANGE_MID) {
inputMin.setValue(rangeMaxInt - RANGE_MIN_DIFFERENCE);
} else {
inputMax.setValue(rangeMinInt + RANGE_MIN_DIFFERENCE);
}
}
}
The way you phrased the question sounds like you're giving instructions to someone else to do your work for you. Although you've stated your goals, you have not stated:
I've ran your code and after typing into a field I get this error:
I will assume this is your problem.
It appears you have simply replaced the
Numberbox
components from the 3rd example in this answer withTextField
however it appears you have not paid attention to the differences between components:As previously mentioned, there's ample ControlP5 documentation (as well as examples). Learn how to use documentation (here's an example video, same applies to java/Processing/any other language/library with documentation) and use it.
The least you could try is something like:
Notice this part:
This is what caused the error previously. ControlP5 would've attempted to map
minValue
from theNumberbox
component name to the sketch'sint minValue;
property at the top, however it's handling a String now (as it's aTextField
), not anint
(likeNumberbox
does)Also handling String to int:
Personally, I can not recommend going on this inefficient and error prone path or converting back and forth between String and integers making for a very awkward user experience entering data. You're on your own in terms of handling String text input to integer value within 4000 / 5000 range, etc.
This seems related to your questions from August onwards when I have suggested using better suited UI elements and data handling (like
Numberbox
) instead ofTextField
. Additionally I have also made suggestions regarding text formatting, naming variables, using documentation, etc. however they appear to have been dismissed.I get that programming at the beginning can get frustrating when things don't work, but these are opportunities to slow down, learn how to debug, understand the problem in detail and try again. A program will probably not do what you want it to do, but what you tell it to do. Somewhere in there there must be a simple explanation and solution to move forward (to the next problem, until the program works as expected). You will learn a lot more when things go wrong and you fix them as opposed to situations when things just work, but you don't know why.