We’re working with the Processing script and touch designer:
import oscP5.;
import netP5.;
OscP5 oscP5;
NetAddress touchDesignerAddress;
String inputText = ""; // Stores the user input
String placeholderText = “Input Text”; // Placeholder text for input
int[] outputValues = new int[4]; // Array to store the randomized outputs
String[] labels = {“Angel: ”, “Hero: ”, “Feminist: ”, “Rebel: ”}; // Labels for outputs
int inputBoxX = 50, inputBoxY = 100, inputBoxWidth = 400, inputBoxHeight = 50;
int outputBoxY = 180; // Y position of the output boxes
boolean showCursor = true;
int cursorPosition = 0; // Position of cursor within inputText
int cursorTimer = 0;
int textOffset = 10; // X offset for text start inside input box
void setup() {
size(500, 500);
oscP5 = new OscP5(this, 12000);
touchDesignerAddress = new NetAddress(“127.0.0.1”, 9000);
textAlign(LEFT, CENTER);
textFont(createFont(“Helvetica”, 18));
}
void draw() {
background(245, 245, 245);
fill(50);
textSize(24);
text(“Florence Nightingale - Living Portrait”, 20, 50);
// Input box interaction
stroke(200);
fill(255);
rect(inputBoxX, inputBoxY, inputBoxWidth, inputBoxHeight, 10);
// Text rendering in input box
fill(0);
textSize(18);
if (inputText.equals("") && !focused) {
fill(150);
text(placeholderText, inputBoxX + textOffset, inputBoxY + inputBoxHeight / 2);
} else {
fill(0);
text(inputText.substring(0, cursorPosition) + (showCursor ? ”|” : "") + inputText.substring(cursorPosition), inputBoxX + textOffset, inputBoxY + inputBoxHeight / 2);
}
// Output boxes and labels
textSize(16);
for (int i = 0; i < 4; i++) {
int boxY = outputBoxY + i * 60;
fill(255);
stroke(200);
rect(inputBoxX, boxY, inputBoxWidth, 40, 10);
fill(0);
text(labels[i] + outputValues[i], inputBoxX + 10, boxY + 25);
}
// Cursor blink management
cursorTimer++;
if (cursorTimer >= 30) {
showCursor = !showCursor;
cursorTimer = 0;
}
}
void mousePressed() {
if (mouseX > inputBoxX && mouseX < inputBoxX + inputBoxWidth &&
mouseY > inputBoxY && mouseY < inputBoxY + inputBoxHeight) {
// User clicked inside the text box, set focus
focused = true;
// Calculate cursor position based on the click
float clickX = mouseX - (inputBoxX + textOffset); // X position within the text box
float textWidthUpToCursor = 0;
int newPosition = 0; // Default to start of the text
// Iterate through each character to find where the click occurred
for (int i = 0; i <= inputText.length(); i++) {
// Update the text width up to the cursor after each character
textWidthUpToCursor += textWidth(inputText.charAt(i - 1));
// Check if the accumulated width surpasses the click position
if (textWidthUpToCursor > clickX) {
// If the click is beyond the last character, set the cursor at the end
if (i == inputText.length()) {
newPosition = inputText.length();
cursorPosition = newPosition; // Update the cursor position
} else {
// Click outside the text box, remove focus
focused = false;
}
}
void keyPressed() {
if (!focused) return;
if (key == ENTER || key == RETURN) {
generateRandomOutputs();
sendOscMessage();
inputText = ""; // Clear the input text after sending
cursorPosition = 0;
} else if (key == BACKSPACE) {
if (cursorPosition > 0 && inputText.length() > 0) {
inputText = inputText.substring(0, cursorPosition - 1) + inputText.substring(cursorPosition);
cursorPosition—;
}
} else if (key == DELETE) {
if (cursorPosition < inputText.length()) {
inputText = inputText.substring(0, cursorPosition) + inputText.substring(cursorPosition + 1);
}
} else if (key == LEFT) {
if (cursorPosition > 0) cursorPosition—;
} else if (key == RIGHT) {
if (cursorPosition < inputText.length()) cursorPosition++;
} else if (key >= 32 && key <= 126) { // Printable characters
inputText = inputText.substring(0, cursorPosition) + key + inputText.substring(cursorPosition);
cursorPosition++;
}
}
void generateRandomOutputs() {
int baseSeed = inputText.length() + inputText.chars().sum();
randomSeed(baseSeed); // Resetting random seed with the base seed
random(0, 101); // Discard the first result
for (int i = 0; i < outputValues.length; i++) {
outputValues[i] = int(random(0, 101)); // Generate random numbers between 0 and 100
}
}
void sendOscMessage() {
if (outputValues.length == 4) {
OscMessage msg = new OscMessage(“/randomValues”);
for (int value : outputValues) {
msg.add(value);
}
oscP5.send(msg, touchDesignerAddress);
println(“Sent OSC message with random values: ” + outputValues[0] + ”, ” + outputValues[1] + ”, ” + outputValues[2] + ”, ” + outputValues[3]);
} else {
println(“Error: Incorrect number of output values. Expected 4 values.”);
}
}