A good solution has been shown with arraylist
A list of char is also a String.
There are powerful commands, like charAt, indexOf … substring…
Example
Here is an example that shows how you can split a String at a cursor pos “a”
int a = 4; // cursor pos
String myText="ThisIsATextThatYouCanType";
void setup() {
size(600, 600);
background(0);
}
void draw() {
background(0);
text(myText+ " " + "Use Cursor left / right ", 12, 55);
text(myText.substring(0, a), 22, 77);
text(myText.substring(a), 54, 99);
}
void keyPressed() {
// For CODED --------------------------------------------
if (key==CODED) {
switch(keyCode) {
case UP:
break;
case DOWN:
break;
case LEFT:
a--;
break;
case RIGHT:
a++;
break;
}//switch
return; // leave
}// if CODED
//
}
New Version with more Editor Functions
- blinking cursor
- Backspace
- Delete
- enter text
int a = 4;
String myText="ThisIsATextThatYouCanType";
boolean blinkFlag;
void setup() {
size(600, 600);
background(0);
}
void draw() {
background(0);
text(myText+ " " + "Use Cursor left / right; Backspace, Delete and just type text ", 12, 55);
text(myText.substring(0, a), 22, 77);
text(myText.substring(a), 54, 99);
text(myText.substring(0, a)
+ blink()
+ myText.substring(a), 22, 192);
}
void keyPressed() {
// For CODED --------------------------------------------
if (key==CODED) {
//
switch(keyCode) {
case UP:
break;
case DOWN:
break;
case LEFT:
a--;
break;
case RIGHT:
a++;
break;
}//switch
return; // leave
}// if CODED
//
//
// not CODED: -----------------------------------
if (key==BACKSPACE) {
myText = myText.substring(0, a-1) +
myText.substring(a);
a--;
} else if (key==DELETE) {
myText = myText.substring(0, a) +
myText.substring(a+1);
} else if (key==ESC) {
key=0; // kill ESC
} else if (key>=32) {
// Enter normal text
myText = myText.substring(0, a)
+ key
+ myText.substring(a);
a++;
}
}//func
String blink() {
if (frameCount%19==0)
blinkFlag=
! blinkFlag;
if (blinkFlag)
return "|";
else return " ";
}
//
3rd version
bit more security checks for start and end of word…
// Mini Editor
String myText="ThisIsATextThatYouCanType";
int cursorPosition = 4;
boolean blinkFlag;
void setup() {
size(600, 600);
background(0);
}
void draw() {
background(0);
// Help text
fill(255, 0, 0);
text ( "Use Cursor left / right; Backspace, Delete, Return and just type text ", 12+22+189, 22);
// normal text
fill(0, 222, 111);
text(myText, 12, 55);
// text in two lines
fill(255);
text(myText.substring(0, cursorPosition), 22, 99);
text(myText.substring(cursorPosition), 54, 99+20);
// Text with cursor
text(myText.substring(0, cursorPosition)
+ blink()
+ myText.substring(cursorPosition), 22, 192);
}
// ----------------------------------------------------------------------------------------------------
void keyPressed() {
// For CODED ----------------------
if (key==CODED) {
//
switch(keyCode) {
case UP:
break;
case DOWN:
break;
case LEFT:
cursorPosition--;
break;
case RIGHT:
cursorPosition++;
break;
}//switch
// check cursorPosition
if (cursorPosition<0)
cursorPosition=0;
if (cursorPosition>=myText.length())
cursorPosition=myText.length();
return; // leave function
}// if CODED
//
//
// not CODED: -------------
if (key==BACKSPACE) {
if (cursorPosition==0)
return; // No
myText = myText.substring(0, cursorPosition-1) +
myText.substring(cursorPosition);
cursorPosition--;
} else if (key==DELETE) {
if (cursorPosition>=myText.length())
return;
myText = myText.substring(0, cursorPosition) +
myText.substring(cursorPosition+1);
} else if (key==ESC) {
key=0; // kill ESC
} else if (key==ENTER||key==RETURN) {
println(myText);
} else if (key>=32) {
// Enter normal text
myText = myText.substring(0, cursorPosition)
+ key
+ myText.substring(cursorPosition);
cursorPosition++;
}// else if
//
}//func
String blink() {
// blinking cursor |
if (frameCount%19==0)
blinkFlag=
! blinkFlag;
if (blinkFlag)
return "|";
else return " ";
} // func
//