Hi All,
Sorry, I’m pretty new to all this. I have a project I’m working on that saves keyboard input to a StringDict with a keyword for each of a number of associated textfield objects.
It seems to be mostly operational, however, when I try to retrieve the associated string so that I can compare it to make things happen–in this case change the colour to green (PASS) or red (FAIL)–it doesn’t seem to work.
I note that I can simply .set() the string that works to the StringDict and it compares just fine, but when I .get() the string from keyboard input, nothing. What am I doing wrong here?
Also maybe there’s a much easier way to do this. Any help and advice would be much appreciated.
Code looks like this:
int border = 10;
//String txt = "";
Textfield[] textfields;
PFont f;
StringDict savedStrings;
color te = color(0); //text color
color ba = color(200); //background color
color bo = color(255); //border color
color se = color(230); //select color
color pf = ba; //pass/fail color
void setup() {
size(400,200);
String[] keyWords = new String[3];
keyWords[0] = "Test1";
keyWords[1] = "Test2";
keyWords[2] = "Test3";
//Initialise savedStrings with keywords to be associated with text input.
savedStrings = new StringDict();
for (int i = 0; i < keyWords.length; i++) {
savedStrings.set(keyWords[i], "");
}
//number of textfields
textfields = new Textfield[keyWords.length];
//Construct textfields
for (int i = 0; i < textfields.length; i++) {
String t = "Click then type PASS or FAIL";
String k = keyWords[i];
float w = width - border;
float h = (height / textfields.length) - border;
float x = width / 2;
float y = i * (height / textfields.length) + h/2 + border/2;
int li = 5;
textfields[i] = new Textfield(x,y,w,h,t,k,te,ba,bo,se,li);
}
}
void draw() {
for (int i = 0; i < textfields.length; i++) {
textfields[i].display();
}
}
void mouseClicked() {
for (int i = 0; i < textfields.length; i++) {
if (textfields[i].isFocused) {
textfields[i].click();
} else if (textfields[i].active == true) {
textfields[i].deactivate();
}
}
}
void keyTyped() {
for (int i = 0; i < textfields.length; i++) {
if (textfields[i].active == false) {
//do nothing
} else if (textfields[i].active == true) {
textfields[i].tKeyTyped();
//if (txt == "") {
// textfields[i].deactivate();
//}
}
}
}
//----------------------------------------------
class Textfield {
final color textC, baseC, bordC, slctC;
String txt = "";
boolean isFocused, active;
String t, keyWord, state;
float x, y, w, h;
int lim;
Textfield(
float _x, float _y, float _w, float _h,
String _t, String _k,
color te, color ba, color bo, color se,
int li) {
x = _x;
y = _y;
w = _w;
h = _h;
t = _t;
keyWord = _k;
textC = te;
baseC = ba;
bordC = bo;
slctC = se;
lim = li;
}
void display() {
stroke(bordC);
strokeWeight(border);
rectMode(CENTER);
fill(baseC);
rect(x,y,w,h);
state = savedStrings.get(keyWord);
//Display textfield with mouseover
if (checkFocus()) {
fill(slctC);
} else {
fill(baseC);
}
rect(x,y,w,h);
//Display initial text
if (state == "") {
f = createFont("Arial", 15);
textFont(f);
textAlign(CENTER, CENTER);
textSize(15);
fill(textC);
text(t, x, y);
}
//If textfield is clicked, display text being typed.
if (this.active == true) {
fill(slctC);
rect(x,y,w,h);
fill(textC);
text(txt, x, y);
//If textfield deactivated (pressed enter or clicked away) display saved text
//Color textfield if saved text meets PASS / FAIL criteria.
} else if (this.active == false && state != "") {
fill(pf);
rect(x,y,w,h);
fill(textC);
text(state, x, y);
}
}
void deactivate() {
//txt = "PASS"; //Uncomment to verify PASS / FAIL Operation
savedStrings.set(keyWord,txt);
println("deactivate_keyWord "+keyWord); println(savedStrings);
this.active = false;
//println(txt);
//Check saved text with pass fail criteria, green if PASS, red if FAIL.
if (savedStrings.get(keyWord) == "PASS") {
pf = color(0,255,0);
} else if (savedStrings.get(keyWord) == "FAIL") {
pf = color(255,0,0);
}
}
void click() {
this.active = true;
txt = savedStrings.get(keyWord);
println("click_keyWord "+keyWord); println("click_txt "+txt); println(savedStrings);
}
void tKeyTyped() {
char r = key;
if (r == CODED) return;
final int len = txt.length();
if (r == BACKSPACE) txt = txt.substring(0, max(0, len-1));
else if (len >= lim) return;
else if (r == ENTER || r == RETURN) {
this.deactivate();
} else if (r == TAB & len < lim-3) txt += " ";
else if (r == DELETE) txt = "";
else if (r >= ' ') txt += str(r);
}
String blinkChar() {
return (frameCount>>2 & 1) == 0 ? "_" : "";
}
boolean checkFocus() {
return isFocused = (mouseX > x - width/2 && mouseX < x + width/2 + h && mouseY > y - h/2 && mouseY < y + h/2);
}
}