Advertisement
johnnic431

Button

Jun 25th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.52 KB | None | 0 0
  1. function init(this)
  2.         new={};
  3.         new.info={x=0,y=0,sizeX=0,sizeY=0,bCol=0,tCol=0,text="",visible=false,onClick=function() end};
  4.         for k,v in pairs(this) do
  5.                 if k~="init" then
  6.                         new[k]=v;
  7.                 end
  8.         end
  9.         return setmetatable(new,this);
  10. end
  11.  
  12.  
  13. function setSize(this,nX,nY)
  14.         this.info.sizeX=nX;
  15.         this.info.sizeY=nY;
  16.         return this;
  17. end
  18.  
  19. function setLoc(this,nX,nY)
  20.         this.info.x=nX;
  21.         this.info.y=nY;
  22.         return this;
  23. end
  24.  
  25. function setX(this,nX)
  26.         this.info.x=nX;
  27.         return this;
  28. end
  29.  
  30. function setY(this,nY)
  31.         this.info.y=nY;
  32.         return this;
  33. end
  34.  
  35. function setPos(this,nX,nY)
  36.         this.info.x=nX;
  37.         this.info.y=nY;
  38.         return this;
  39. end
  40.  
  41. function setBackColor(this,col)
  42.         this.info.bCol=col;
  43.         return this;
  44. end
  45.  
  46. function setBackgroundColor(this,col)
  47.         this.info.bCol=col;
  48.         return this;
  49. end
  50.  
  51. function setTextColor(this,col)
  52.         this.info.tCol=col;
  53.         return this;
  54. end
  55.  
  56. function setText(this,tx)
  57.         this.info.text=tx;
  58.         return this;
  59. end
  60.  
  61. function setVisible(this,bool)
  62.         this.info.visible=bool;
  63.         return this;
  64. end
  65.  
  66. function getInfo(this)
  67.         return this.info;
  68. end
  69.  
  70. function clickAt(this,x,y)
  71.         if x>=this.info.x and x<= this.info.x+this.info.sizeX-1 and y>=this.info.y and y<= this.info.y+this.info.sizeY-1 and this.info.visible then
  72.                 this.info.onClick();
  73.         end
  74. end
  75.  
  76. function setOnClick(this,oncl)
  77.         this.info.onClick=oncl;
  78.         return this;
  79. end
  80.  
  81. function onClick(this)
  82.         this.info.onClick();
  83. end
  84.  
  85. local function writeTextSpecificCentered(text,sX,eX,y,tCol)
  86.         if text==nil or y<=0 then return false; end
  87.         sValue=(((eX+1)-sX)-string.len(text))/2;
  88.         if tCol then term.setTextColor(tCol) end
  89.         term.setCursorPos(sValue+sX,y);
  90.         term.write(text);
  91.         term.setBackgroundColor(colors.green)
  92. end
  93.  
  94. function draw(this)
  95.         if this.info.visible then
  96.                 tX=this.info.x;
  97.                 tY=this.info.y;
  98.                 tX2=this.info.x+this.info.sizeX-1;
  99.                 tY2=this.info.y+this.info.sizeY-1;
  100.                 ok,err=pcall(paintutils.drawFilledBox,tX,tY,tX2,tY2,this.info.bCol or colors.black);
  101.                 ok,err=pcall(writeTextSpecificCentered,this.info.text or "",tX,tX2,tY+(this.info.sizeY/2),this.info.tCol or colors.white);
  102.         end
  103.         return true;
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement