I made this into a class ClassTextBox
.
It also has 2 buttons (Top and Bottom, without text) and a slider.
Chrisir
/*
From: https://forum.processing.org/two/discussion/22527/simple-scrollable-textbox-is-there-such-a-thing
https://discourse.processing.org/t/embed-txt-file-in-a-sketch/24542/4
*/
ClassTextBox textBox1;
// -----------------------------------------------------------------------------------------------------
void setup() {
size(1420, 920);
textBox1 = new ClassTextBox(220, 220);
}
void draw() {
background(255);
textBox1.display();
}
// -----------------------------------------------------------------------------------------------------
void mousePressed() {
textBox1.mousePressedClass();
}
void mouseReleased() {
textBox1.mouseReleasedClass();
}
void mouseWheel(MouseEvent event) {
textBox1.mouseWheelClass(event);
}
void keyPressed() {
textBox1.keyPressedClass();
}
// ===============================================================================================================
class ClassTextBox {
PGraphics textBox = new PGraphics();
PVector textBoxPos ; // = new PVector(20, 20);
float textX = 0;
float textY = 0;
float scroll = 0;
float textHeight = 20;
float textLeading = 20;
StringList messages;
String displayText = "";
Slider slider;
Button btn1, btn2;
// constr
ClassTextBox(float x_, float y_) {
textBoxPos = new PVector(x_, y_);
textBox = createGraphics(380, 380);
messages = new StringList();
btn1=new Button( textBoxPos.x+textBox.width-13, textBoxPos.y+1); // upper corner
btn2=new Button(textBoxPos.x+textBox.width-13, textBoxPos.y+textBox.height-13); // lower corner
slider = new Slider(textBoxPos.x+textBox.width-13+3, textBoxPos.y+1 +12+3,
textBoxPos.y+1 +12+3, textBoxPos.y+textBox.height-13-6);
}// constr
void display() {
textBox.beginDraw();
// textBox.background(0, 0, 0, 0); // Transparent background but you could have it coloured.
textBox.background(255); // WHITE background
textBox.stroke(0, 0, 0);
textBox.fill(0, 0, 0, 0);
textBox.rect(0, 0, textBox.width-1, textBox.height-1); // Black rectangle around the outside.
textBox.textSize(textHeight);
textBox.fill(0);
textBox.textAlign(LEFT, TOP);
textBox.textLeading(textLeading);
textBox.text(displayText, textX, textY+scroll);
textBox.endDraw();
image(textBox, textBoxPos.x, textBoxPos.y);
btn1.display();
btn2.display();
slider.display();
slider.move();
// when we drag the slider, we read it
if (slider.sliderHold) {
scroll = map(slider.value, 0, 100, -textY, 0);
scroll = constrain(scroll, 0, -textY);
} else {
// set slider when we have a scroll
slider.sliderY = map(scroll,
-textY, 0,
slider.minY, slider.maxY);
}
//debug
// fill(0);
// text(scroll, 100, 500);
}//method
void keyPressedClass() {
// PROXY FOR INCOMING MESSAGES
if (keyPressed) {
messages.append("ARE WE NEARLY THERE YET?...."
+messages.size()); // add new message line (with line number)
displayText = displayText
+messages.get(messages.size()-1)
+"\n"; // display the updated messages
//--->> SHIFT LINES UP IF MORE LINES THAN BOX
if (messages.size()>(textBox.height/textLeading)) {
textY -= textLeading;
}
}
}
void mouseWheelClass(MouseEvent event) {
scroll -= event.getCount()*4; //add mouse wheel with a bit of gearing
scroll = constrain(scroll, 0, -textY);
}
void mousePressedClass() {
if (slider.mousePressedSliderClass())
return;
// check 2 buttons
if (btn1.inside()) {
// UP (scroll to text start)
scroll=-textY;
slider.sliderY=slider.minY;
} else if (btn2.inside()) {
// DOWN (scroll to end)
scroll=0;
slider.sliderY=slider.maxY;
}
scroll = constrain(scroll, 0, -textY);
}
void mouseReleasedClass() {
slider.sliderHold=false;
}
//
}//class
// ====================================================================
class Button {
float x, y;
Button (float x_, float y_) {
x=x_;
y=y_;
}
void display() {
noFill();
stroke(0);
rect(x, y,
12, 12);
}
boolean inside() {
return
mouseX>x &&
mouseX<x+12 &&
mouseY>y &&
mouseY<y+12;
}//method
//
}//class
// =================================================================
class Slider {
float sliderX=12;
float sliderY=300;
float minY, maxY;
boolean sliderHold=false;
float value=0;
Slider(float x_, float y_,
float minY_, float maxY_) {
sliderX=x_;
sliderY=y_;
minY=minY_;
maxY=maxY_;
}
void display() {
// vertical line
stroke(0, 255, 0); // Green
line(sliderX+3, minY,
sliderX+3, maxY);
// rect
fill(0, 255, 0); // Green
noStroke();
rect(sliderX, sliderY,
6, 6);
}
void move() {
// drag
if (sliderHold) {
sliderY=map(mouseY, 0, height, 15, height-15-8);
sliderY=constrain(sliderY, minY, maxY);
value = int(map(sliderY, minY, maxY, 0, 100));
}
}
boolean mousePressedSliderClass() {
if (dist(mouseX, mouseY, sliderX, sliderY) < 12) {
sliderHold=true;
return true;
}
return false;
}
//
}// class
//