Embed .txt file in a sketch

I’m making a simple desktop app which links to a few websites, but I also want it to be able to display a .txt file embedded in the sketch if possible. I’d prefer not to rebuild the document by doing loadlines(), but instead simply displaying the .txt file whole, or embedded.

Any way of doing this?

Hello and welcome to the forum!

Great to have you here.

Not that I am aware of.

Below how to do it “manually”.

OR check the libraries G4P or controlP5.

Warm regards,

Chrisir


final int SPLASH_SCREEN = 0;
final int MAIN_SCREEN   = 1;  
int state = MAIN_SCREEN;

String[] txt1; 
//
// ---------------------------------------------------------------
//
void setup() {
  // init
  size(800, 600);
  txt1 = loadStrings("read.txt");
} // func 

void draw() { 
  background(255);

  switch (state) {

  case SPLASH_SCREEN:

    break; 

  case MAIN_SCREEN:
    //
    fill(0);
    int i=0; 
    for (String s1 : txt1) {
      text(s1, 
        313, 331+i*16, 
        150, 2000);
      i++;
    }
    break;
  }//switch
  //
} // func 
//

void mousePressed() {

  switch (state) {

  case SPLASH_SCREEN:
    state=MAIN_SCREEN;
    break; 

  case MAIN_SCREEN:
    //
    break;
  }//switch
}//func 
//

Hi Chrisir,

The p5 suggestion is magnificent. It’s been nigh-on a decade since I’ve used processing and had never heard of the library. Dusting it off for a project at work, and this library solves a few different minor obstacles much better than what I’d already put together.

2 Likes

Hi @kurtis452,

I have used the following code to make a scrollable textBox using PGraphics. Not sure if this is what you are looking for, but it may help you. With a few changes you can probably load the text file to the text box a draw it on the screen :slight_smile:
For now, just press any key to add a line to the text box

Best regards

/*
From: https://forum.processing.org/two/discussion/22527/simple-scrollable-textbox-is-there-such-a-thing
*/


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 = "";
 
void setup()
{
  size(420, 420, P2D);
  textBox = createGraphics(380, 380, P2D);
  messages = new StringList();
}
 
void draw()
{
  background(255);
  textBox.beginDraw();
  textBox.background(0, 0, 0, 0);  // Transparent background but you could have it coloured.
  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);
}
 
void mouseWheel(MouseEvent event)
{
  scroll -= event.getCount()*4;  //add mouse wheel with a bit of gearing
  scroll = constrain(scroll, 0, -textY);
}
 
void keyPressed() {   // 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;
    }
  }
}

image

1 Like

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  
//

1 Like