Advertisement
Guest User

Untitled

a guest
Jan 15th, 2017
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.53 KB | None | 0 0
  1. ArrayList<TextBox> textBoxes = new ArrayList<TextBox>();
  2. float scrollSpeed = 10;
  3.  
  4. void setup()
  5. {
  6.   size(1280, 720, P2D);
  7.   frameRate(60);
  8.  
  9.   String someText = "One two three four five six seven eight nine\n\n";
  10.   someText += someText;
  11.   someText += someText;
  12.   someText += someText;
  13.  
  14.   TextBox textBox1 = new TextBox(20,20, 200, 500);
  15.   textBox1 .setBackCol (color(0));
  16.   textBox1 .setEdgeCol (color(255,0,0));
  17.   textBox1 .setTextCol (color(0,255,0));
  18.   textBox1 .setFont    (createFont("Comic Sans MS", 24));
  19.   textBox1 .text = someText;
  20.  
  21.   TextBox textBox2 = new TextBox(600, 20, 500, 200);
  22.   textBox2 .setEdgeCol (color(0,0,0, 150));
  23.   textBox2 .setTextCol (color(0,0,0));
  24.   textBox2 .setFont    (createFont("Courier New", 18));
  25.   textBox2 .setMargin  (10, 10);
  26.   textBox2 .text = someText;
  27.  
  28.   textBoxes.add(textBox1);
  29.   textBoxes.add(textBox2);
  30. }
  31.  
  32. void draw()
  33. {
  34.   background(255);
  35.   for (TextBox textBox : textBoxes)
  36.   {
  37.     textBox.draw();
  38.   }
  39. }
  40.  
  41. void mouseWheel(MouseEvent event)
  42. {
  43.   for (TextBox textBox : textBoxes)
  44.   {
  45.     if (textBox.isInside(mouseX, mouseY))
  46.     {
  47.       textBox.scroll(event.getCount()*scrollSpeed);
  48.     }
  49.   }
  50. }
  51.  
  52. public class TextBox
  53. {
  54.   public  String    text   = "";
  55.  
  56.   private PGraphics buffer   = new PGraphics();
  57.   private PVector   pos      = new PVector();
  58.   private PVector   size     = new PVector();
  59.   private PVector   margin   = new PVector();
  60.   private float     scroll   = 0;
  61.   private color     backCol  = color(255,255,255,0);
  62.   private color     edgeCol  = color(255,255,255,0);
  63.   private color     textCol  = color(0);
  64.   private PFont     font     = new PFont();
  65.  
  66.   public PVector getPos()  {return pos;}
  67.   public PVector getSize() {return size;}
  68.  
  69.   public void setPos      (PVector pos)      {setPos(pos.x, pos.y);}
  70.   public void setPos      (float x, float y) {pos.x = x; pos.y = y;}
  71.   public void setMargin   (PVector margin)   {setMargin(margin.x, margin.y);}
  72.   public void setMargin   (float w, float h) {margin.x = w; margin.y = h;}
  73.   public void scroll      (float scroll)     {this.scroll = max(this.scroll+scroll, 0);}  // Scroll cannot go below 0.
  74.   public void setBackCol  (color c)          {this.backCol = c;}
  75.   public void setEdgeCol  (color c)          {this.edgeCol = c;}
  76.   public void setTextCol  (color c)          {this.textCol = c;}
  77.   public void setFont     (PFont font)       {this.font = font;}
  78.  
  79.   public void draw()
  80.   {
  81.     buffer.beginDraw();
  82.     {
  83.       buffer.clear();
  84.       buffer.background (backCol);
  85.       buffer.stroke     (edgeCol);
  86.       buffer.fill       (backCol);
  87.       buffer.rect       (0, 0, buffer.width-1, buffer.height-1);  // Border.
  88.       buffer.textFont   (font);
  89.       buffer.fill       (textCol);
  90.       buffer.textAlign  (LEFT, TOP);
  91.       buffer.text       (text, margin.x, margin.y-scroll, buffer.width-margin.x, buffer.height+scroll);
  92.     }
  93.     buffer.endDraw();
  94.  
  95.     image(buffer, pos.x, pos.y);
  96.   }
  97.  
  98.   public boolean isInside(PVector checkPos) {return isInside(checkPos.x, checkPos.y);}
  99.   public boolean isInside(float checkPosX, float checkPosY)
  100.   {
  101.     boolean inWidth  = checkPosX > pos.x && checkPosX < pos.x+size.x;
  102.     boolean inHeight = checkPosY > pos.y && checkPosY < pos.y+size.y;
  103.    
  104.     return inWidth && inHeight;
  105.   }
  106.  
  107.   public TextBox(PVector size, PVector pos) {this(pos.x, pos.y, size.x, size.y);}
  108.   public TextBox(float x, float y, float w, float h)
  109.   {
  110.     pos.x  = x; pos.y = y; size.x = w; size.y = h;
  111.     buffer = createGraphics(floor(w), floor(h), P2D);
  112.   }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement