Advertisement
Guest User

button2

a guest
Sep 15th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. --By Vlad [vladimir264]
  2.  
  3. buttons={}
  4. --Settings--
  5. local debugMode=false
  6.  
  7. local function buttonSetup(Name)
  8.   buttons.Name["draw"]=function(outline,fill) drawButton(buttonName,outline,fill) end
  9.   buttons.Name["setAction"]=function(action) setFunction(buttonName,action) end
  10.   buttons.Name["setText"]=function(text,textColor) addTxt(Name,textColor) end
  11.   buttons.Name["enable"]=function() setEnabled(Name,true) end
  12.   buttons.Name["disable"]=function() setEnabled(Name,false) end
  13.  
  14.   _G[Name]=buttons.Name
  15.  
  16. end
  17.  
  18. function newButton(Name,X,Y,W,H)--Creates a new instance, Name is better off a string.
  19.   buttons[Name]={name=Name,x=X,y=Y,x2=X+W-1,y2=Y+H-1,w=W,h=H,txt={x=nil,y=nil,text=nil,txtColor=nil},func=nil,outline=nil,fill=nil,enabled=true}
  20.   buttonSetup(buttons[Name])
  21. end
  22.  
  23. function drawButton(Name,Outline,Fill)--Draws the button
  24.         buttons.Name.outline=outLnClr
  25.         buttons.Name.fill=fllClr
  26.   x=getButton(Name).x
  27.   y=getButton(Name).y
  28.   x2=getButton(Name).x2
  29.   y2=getButton(Name).y2
  30.         paintutils.drawBox(x,y,x2,y2,Outline)
  31.         paintutils.drawFilledBox(x+1,y+1,x2-1,y2-1,Fill)
  32.         if buttons.Name.txt~=nil then
  33.              writeTxt(buttonName)
  34.         end
  35. end
  36.  
  37. function eraseButton(Name)--Erases specified button, so that you dont see it
  38.   x=getBtn(Name).x
  39.   y=getBtn(Name).y
  40.   x2=getBtn(Name).x2
  41.   y2=getBtn(Name).y2
  42.   paintutils.drawFilledBox(x,y,x2,y2,colors.black)
  43.   buttons.Name.drawn=false
  44. end
  45.  
  46. function waitForClick()--Waits untill the mouse is clicked
  47.   a=true
  48.      while a do
  49.           event,button,xPos,yPos=os.pullEvent("mouse_click")
  50.           for key,value in pairs(buttons) do
  51.       button=buttons[key]
  52.             if xPos>=button.x and xPos<=button.x2 and yPos>=button.y and yPos<=button.y2 then
  53.                     if button.func~=nil and button.enabled then
  54.                          --a=false
  55.                          --term.setBackgroundColor(term)
  56.                          --term.clear()
  57.                          button.func()
  58.                     elseif buttons[key].func==nil then
  59.                          error("No action set to button: ",buttons[key])
  60.           --a=false
  61.                     end
  62.                end
  63.           end
  64.      end
  65. end
  66.  
  67. function setFunction(Name,Action)--Sets a function to a button so when it's clicked, the corresponding function is run.
  68.   buttons.Name.func=Action
  69. end
  70.  
  71. function addTxt(Name,Text,TxtColor)--Sets text into the button *WIP*
  72.   ln=string.len(Text)
  73.   X=(buttons.Name.x2-(math.floor(buttons.Name.w/2)))-(ln/2)+1
  74.   Y=(buttons.Name.y2-(math.floor(buttons.Name.h/2)))
  75.   buttons.Name.txt={text=Txt,x=X,y=Y,txtColor=TxtColor}
  76. end
  77.  
  78. function writeTxt(Name)--Writes the text in the button, max char length is the width of the button minus 2 *WIP*
  79.   tX=buttons.Name.txt.x
  80.   tY=buttons.Name.txt.y
  81.  
  82.   term.setCursorPos(tX,tY)
  83.   term.setTextColor(buttons.Name.txt.txtColor)
  84.   term.write(buttons.Name.txt.text)
  85. end
  86.  
  87. function eraseTxt(Name)--Erases the text in the button
  88.   x1=buttons.Name.x+1
  89.   y1=buttons.Name.y+1
  90.   x2=buttons.Name.x2-1
  91.   y2=buttons.Name.y2-1
  92.   color=buttons.Name.fill
  93.   paintutils.drawFilledBox(x1,y1,x2,y2,color)
  94. end
  95.  
  96. local setEnabled=function(Name,state)
  97.   buttons.Name.enabled=state
  98. end
  99.  
  100. --Extras--
  101. local getTextData=function(Name)
  102.   btn=buttons.Name.txt
  103.   return {x=btn.x,y=btn.y,text=btn.text,textColor=btn.txtColor}
  104. end
  105.  
  106. getButton=function(Name)--returns array containing all data for specified button
  107.   return buttons.Name
  108. end
  109. getBtn=function(Name)--alias for getButton()
  110.   getButton(Name)
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement