Guest User

Untitled

a guest
Jan 15th, 2017
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. public class TextBox
  2. {
  3.   public  String    text   = "";
  4.  
  5.   private PGraphics buffer   = new PGraphics();
  6.   private PVector   pos      = new PVector();
  7.   private PVector   size     = new PVector();
  8.   private PVector   margin   = new PVector();
  9.   private float     scroll   = 0;
  10.   private color     backCol  = color(255,255,255,0);
  11.   private color     edgeCol  = color(255,255,255,0);
  12.   private color     textCol  = color(0);
  13.   private PFont     font     = new PFont();
  14.  
  15.   public PVector getPos()  {return pos;}
  16.   public PVector getSize() {return size;}
  17.  
  18.   public void setPos      (PVector pos)      {setPos(pos.x, pos.y);}
  19.   public void setPos      (float x, float y) {pos.x = x; pos.y = y;}
  20.   public void setMargin   (PVector margin)   {setMargin(margin.x, margin.y);}
  21.   public void setMargin   (float w, float h) {margin.x = w; margin.y = h;}
  22.   public void scroll      (float scroll)     {this.scroll = max(this.scroll+scroll, 0);}  // Scroll cannot go below 0.
  23.   public void setBackCol  (color c)          {this.backCol = c;}
  24.   public void setEdgeCol  (color c)          {this.edgeCol = c;}
  25.   public void setTextCol  (color c)          {this.textCol = c;}
  26.   public void setFont     (PFont font)       {this.font = font;}
  27.  
  28.   public void draw()
  29.   {
  30.     buffer.beginDraw();
  31.     {
  32.       buffer.clear();
  33.       buffer.background (backCol);
  34.       buffer.stroke     (edgeCol);
  35.       buffer.fill       (backCol);
  36.       buffer.rect       (0, 0, buffer.width-1, buffer.height-1);  // Border.
  37.       buffer.textFont   (font);
  38.       buffer.fill       (textCol);
  39.       buffer.textAlign  (LEFT, TOP);
  40.       buffer.text       (text, margin.x, margin.y-scroll, buffer.width-margin.x, buffer.height+scroll);
  41.     }
  42.     buffer.endDraw();
  43.  
  44.     image(buffer, pos.x, pos.y);
  45.   }
  46.  
  47.   public boolean isInside(PVector checkPos) {return isInside(checkPos.x, checkPos.y);}
  48.   public boolean isInside(float checkPosX, float checkPosY)
  49.   {
  50.     boolean inWidth  = checkPosX > pos.x && checkPosX < pos.x+size.x;
  51.     boolean inHeight = checkPosY > pos.y && checkPosY < pos.y+size.y;
  52.    
  53.     return inWidth && inHeight;
  54.   }
  55.  
  56.   public TextBox(PVector size, PVector pos) {this(pos.x, pos.y, size.x, size.y);}
  57.   public TextBox(float x, float y, float w, float h)
  58.   {
  59.     pos.x  = x; pos.y = y; size.x = w; size.y = h;
  60.     buffer = createGraphics(floor(w), floor(h), P2D);
  61.   }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment