We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all,
I working on a code for a text area or so called a text box. The version seems to work fine for a single box, but for multiple boxes it is too much code. In addition, if code is running in the browser, if you press "backspace" the page restarts on Windows computers and the written text is lost (due to html / javascript focus issues).
I kindly ask you to help me with two issues:
to create a class similar to this function (not using any library, e.g. controlp5).
to figure out a solution to the "Backspace" key problem when the code (javascirpt) running in the browser (Windows).
Here is my function:
//====================================================
// VARIABLES
//----------------------------------------------------
final int sizeW = 600;
final int sizeH = 600;
String typedText = "";
color col;
//=========================================================
// SETUP
//.........................................................
void setup() {
size(600, 600);
smooth();
col = color(#778C85);
}
//=========================================================
// DRAW
//.........................................................
void draw() {
background(col);
manageTextBox();
}
//=========================================================
// FUNCTION
//.........................................................
void manageTextBox() {
background(col);
String taLabel = "Please, enter your comments here: (use \"Right\" arrow to delete)";
int taPadX = 10;
int taX = sizeW/2;
int taY = sizeH-sizeH/3;
int taSizeX = sizeW-sizeW/4;
int taSizeY = sizeH/4;
color taBaseColor = color(255, 30);
color taBorderColor = color(255, 100);
color taLabelColor = color(255, 200);
color taTextColor = color(0, 200);
stroke(taBorderColor);
strokeWeight(1);
fill(taBaseColor);
rectMode(CENTER);
rect(taX, taY, taSizeX, taSizeY);
fill(taLabelColor);
rectMode(CORNER);
textAlign(LEFT);
text(taLabel, sizeW/2 - (sizeW-sizeW/4)/2, sizeH-sizeH/3 - (sizeH/4)/2-taPadX*2, taSizeX, taSizeY);
fill(taTextColor);
text(typedText+(frameCount/10 % 2 == 0 ? "_" : ""), sizeW/2 - (sizeW-sizeW/4)/2, sizeH-sizeH/3 - (sizeH/4)/2, taSizeX-2*taPadX, taSizeY-2*taPadX);
}
//=========================================================
// KEY INPUTS FOR TEXT
//.........................................................
void keyPressed() {
if ((key != CODED)) {
switch(key) {
case BACKSPACE:
typedText = typedText.substring(0, Math.max(0, typedText.length()-1));
break;
case TAB:
typedText += " ";
break;
case ENTER:
case RETURN:
typedText += "\n"; //enable line breaks
break;
case ESC:
case DELETE:
break;
default:
typedText += str(key);
}
}
else if ((key == CODED)) {
if (keyCode == LEFT) {
typedText = typedText.substring(0, Math.max(0, typedText.length()-1));
}
}
if (key == ESC) {
key = 0;
}
}
Thanks in advance!
Answers
Hey! I've come up w/ a class very similar to your code above. Rest is up to ya! =:)
Anyways, tested it online w/ Firefox, Midori, Opera & Chromium under Lubuntu 13.04 (64-bit):
You can also check it online below:
http://studio.processingtogether.com/sp/pad/export/ro.9Zo$UbIWYZEDR/latest
And here's the source code: =P~ See next post...
A newer version! Now w/ keyPressed() using LEFT & RIGHT as additional band-aid for BACKSPACE & TAB! :bz
Also, DELETE clears a TextBox and lim to limit how many chars! :ar!
http://studio.processingtogether.com/sp/pad/export/ro.9Zo$UbIWYZEDR/latest
@GoToLoop, thank you very much for your timely help! Your solution is very elegant and advanced!
About making a class: see the Objects tutorial on the Processing site.
Make a new class. Declare as field all the global variables at the top of your code. Make your drawing logic a display() (or other name) method in the class. Add methods to handle user input.
That's the delicate part: the draw() function of any sketch using your textareas must call the keyPressed() method inside the class, so it updates its content.
A cleaner method would be for the class to register itself to the PApplet events, but I fear this won't work in JS mode.
[EDIT] Woops, I didn't notice GoToLoop already did the objectification of the entity... But as I wrote, I would move the logic of key handling to the class itself. It is a bit messy to manipulate the inners of a class from outside, like this. :-)
@PhiLho, thank you for your suggestions! I do need this applet to work in JS mode, so I guess this is the only way. However, you suggest to "move the logic of key handling to the class itself ".
Do you mean to create functions for key handling inside of a class?
And later place these functions inside
void keyPressed() {}
andvoid keyTyped() {}
?This is likely an excellent tip, but I am afraid to accidentally mess up the code that appears to be working.
I wrote the code above while you were writing your answer... :-)
One advantage of the new approach is that the sketch only keep track of the text boxes and doesn't care to keep track which one has focus.
http://jsfiddle.net/Vjqcx/21/
I fear my code doesn't work in JS mode, I will see later what went wrong...
[EDIT] Works in Chrome, looks like Firefox doesn't like foreach loops... [EDIT] Work in Firefox when replacing
with
and similar. http://jsfiddle.net/Vjqcx/23/
I didn't know this limitation in Firefox!
:) @PhiLho, you were quick to reedit the code! It is, indeed, an interesting problem! Your code works in Chrome, but if I press "Delete" it goes back to the initial search page... That would not work, as some people might use "delete" key because of habit and restart a page.
Yes, your code now works in Firefox too! Thanks!
I agree on moving style functions into a class:
EDITED: Not flickering/blinking anymore, it was a glitch in the other part of the sketch.
I knew it b/c I've been testing Java to JS conversion for a long time now! b-(
But I wouldn't be so sure about saying it's a "limitation" though! :(|)
Well, only for the Array data-type! The other composite structures work as always have! B-)
About Firefox v18 (take or remove 2 versions :-/), it got a little more strict in its JS than other browsers it seems.
Anyways, Processing.JS has already addressed that case "centuries" ago! It's just that they refuse to release a stable version!
And we and other host sites are stuck w/ JS Mode v1.4.1 like it's forever! X(
In short, I wanted to use the enhanced for loop like this:
But I've already knew that would break Firefox's compatibility: :((
In the point of view of OOP and encapsulation it's gr8! But it's a redundant portion nonetheless! [-(
You see, Processing has a setup() function just for preparing things before the "infinite" draw() "big loop"!
But I can't see anywhere those settings change to something else after setup()! X_X
Knowing your code is for running inside a browser, I've chosen the fastest performance algorithms and settings.
Like frameRate(20), to avoid rendering canvas too many times like default 60 FPS.
And if you didn't need that blinking cursor, I'd turn it off w/ noLoop()! :ar!
I keep lotsa tabs open myself. And if I had any app(s) running, I'd like them to use only the minimum necessary resources to work!
"I can't see anywhere those settings change to something else after setup()!"
Well, look at the start of my draw() function, I added this code specially for demonstrating the usefulness of pushStyle...
In a library (and, ideally, in any class):
So I simulated this by adding this stupid code. :-)
"One issue with your version, if you type... the box is blinking"
I don't see that in Firefox on Windows. Supposing you are not talking about the random rect() drawn all over the sketch area...
@PhiLho, regarding the inner flickering when typing inside the box... I tried to investigated it further, and you are right, by itself it is running nicely in Firefox. It was something wrong within the rest of my extended sketch causing this malfunction. Also, I understand the purpose and appreciate you including these colorful randomly-appearing rectangles.
By the way @PhiLho modification works better when the text boxes are used in multiple pages. The first version of @GoToLoop was working perfectly on the first page, however, on the second page, I would not be able to to enter text even though the box would appear as selected and marker would be blinking.
The modification I would add to @PhiLho version is adding the following code inside the TextBox class:
and resetting the text boxes when the NextPage button is clicked inside
mousePressed()
:This way the text typed in the first page does not carry on to the next page.
If someone is interested in sending the input of the text box to JavaScript follow these steps to modify the above code:
Add a the
getString()
function inside TextBox class:Place this code outside the
setup()
anddraw()
functions:Add the following code inside your sketch (for instance, inside
mousePressed()
in conjunction with some "Submit" button):