Advertisement
vladimir264

ComputerCraft Button API 3.0

Dec 10th, 2021
1,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.61 KB | None | 0 0
  1. --Button 3.0 API
  2. --By Vlad [vladimir264]
  3.  
  4. local buttons={} --Database that contains all instances localy
  5.  
  6. newButton = function(Name,X,Y,W,H)
  7.     buttons[Name]={ object="button",
  8.     text={x,y,text="",color=colors.white,visible=false,enabled=false},--Text stuff
  9.     x=X,y=Y,x2=X+(W-1),y2=Y+(H-1),w=W,h=H,
  10.     outline=nil,fill=nil,
  11.     func=nil,
  12.     draw=function(outline,fill) drawButton(Name,outline,fill) end,
  13.     setFill=function(fill) setFill(Name,fill) end,
  14.     setOutline=function(outline) setOutline(Name,outline) end,
  15.     setText=function(text,textColor) addText(Name,text,textColor,true) end,
  16.     enableText=function() setTextEnabled(Name,true) end,
  17.     disableText=function() setTextEnabled(Name,false) end,
  18.     enable=function() setEnabled(Name,true) end,
  19.     disable=function() setEnabled(Name,false) end,
  20.     setAction=function(action) setFunction(Name,action) end
  21.     }
  22.    
  23.     updateGlobal(Name)
  24.     return buttons[Name]
  25. end
  26.  
  27. --[[
  28. function Button:new(X,Y,W,H)
  29.     newButton(Button,X,Y,W,H)
  30. end
  31. --]]    
  32.  
  33. function drawButton(Name,Outline,Fill)--Draws the button
  34.     b=getButton(Name)
  35.     b.outline=Outline
  36.     b.fill=Fill
  37.     x=b.x
  38.     y=b.y
  39.     x2=b.x2
  40.     y2=b.y2
  41.     paintutils.drawBox(x,y,x2,y2,Outline)
  42.     paintutils.drawFilledBox(x+1,y+1,x2-1,y2-1,Fill)
  43.     if b.text.enabled then
  44.         writeTxt(buttonName)
  45.     end
  46. end
  47.  
  48. function setOutline(Name,Color)
  49.     b=getButton(Name)
  50.     b.outline=Color
  51. end
  52.  
  53. function setFill(Name,Color)
  54.     b=getButton(Name)
  55.     b.fill=Color
  56. end
  57.  
  58. function eraseButton(Name)--Erases specified button, so that you dont see it
  59.     b=getButton(Name)
  60.     x=b.x
  61.     y=b.y
  62.     x2=b.x2
  63.     y2=b.y2
  64.     paintutils.drawFilledBox(x,y,x2,y2,term.getBackgroundColor())
  65.     b.drawn=false
  66.     b.enabled=false
  67.     updateGlobal(Name)
  68. end
  69.  
  70. function setFunction(Name,Action)--Sets a function to a button so when it's clicked, the corresponding function is run.
  71.     getButton(Name).func=Action
  72. end
  73.  
  74. function addText(Name,Text,TxtColor,Enabled)--Sets text into the button *WIP*
  75.     length=string.len(Text)
  76.     b=getButton(Name)
  77.     t=getButton(Name).text
  78.     t.x=(b.x2-(math.floor(b.w/2)))-(length/2)+1
  79.     t.y=(b.y2-(math.floor(b.h/2)))
  80.     t.text=Text
  81.     t.color=TxtColor
  82.     t.visible=false
  83.     t.enabled=Enabled
  84.     updateGlobal(Name)
  85. end
  86.  
  87. function writeTxt(Name)--Writes the text in the button, max char length is the width of the button minus 2 *WIP*
  88.     b=buttons[Name]
  89.     t=buttons[Name].text
  90.     if t.enabled then
  91.         term.setCursorPos(t.x,t.y)
  92.         term.setTextColor(t.color)
  93.         term.write(t.text)
  94.         t.visible=true
  95.         updateGlobal(Name)
  96.     end
  97. end
  98.  
  99. function eraseTxt(Name)--Erases the text in the button
  100.     b=getButton(Name)
  101.     t=b.text
  102.     x1=b.x+1
  103.     y1=b.y+1
  104.     x2=b.x2-1
  105.     y2=b.y2-1
  106.     color=b.fill
  107.     paintutils.drawFilledBox(x1,y1,x2,y2,color)
  108.     t.visible=false
  109.     updateGlobal(Name)
  110. end
  111.  
  112. setEnabled=function(Name,State)
  113.     buttons[Name].enabled=State
  114.     updateGlobal(Name)
  115.     return State
  116. end
  117.  
  118. setTextEnabled=function(Name,State)
  119.     buttons[Name].text.enabled=State
  120.     updateGlobal(Name)
  121.     return State
  122. end
  123. ---------------
  124. --Event Stuff--
  125. ---------------
  126. local checkClickPos = function(X,Y) --checks the given position to see if its on a button
  127.     isValid = false
  128.     button = nil
  129.     for k,v in pairs(buttons) do
  130.         if k.enabled then
  131.             if X>=k.x and X<=k.x2 and Y>=k.y and Y<=k.y2 then
  132.                 isValid = true
  133.                 button = k
  134.                 break
  135.             end
  136.         end
  137.     end
  138.     return isValid,button
  139. end
  140.  
  141. --Built-in event listener, args passed to it are
  142. --    passed to the clicked button's connected function:
  143. --button.func(button,mouse-button,mouse-x,mouse-y,args)
  144. function initListener(...)
  145.     while true do
  146.         e,b,mX,mY = os.pullEvent("mouse_click")
  147.         isValid,button = checkClickPos(mX,mY)
  148.         if isValid then
  149.             os.queueEvent("button_pressed",button,b,mX,mY)
  150.             button.func(button,b,mX,mY,arg)
  151.         end
  152.     end
  153. endd
  154.  
  155. --Extras--
  156. local checkExists=function(Name)
  157.     for k,v in pairs(buttons) do
  158.         if Name==k then
  159.             return true
  160.         end
  161.     end
  162.     return false
  163. end
  164.  
  165. local calcButtonCoords=function(X,Y,W,H)
  166.     X2=X+(W-1)
  167.     Y2=Y+(H-1)
  168.     return X,Y,X2,Y2
  169. end
  170.  
  171. function updateGlobal(Name)
  172.     _G[Name]=buttons[Name]
  173. end
  174. local updateAllGlobal=function()
  175.     for k,v in pairs(buttons) do
  176.         _G[k]=buttons[k]
  177.     end
  178. end
  179.  
  180. getButton=function(Name)--returns array containing all data for specified button
  181.     return buttons[Name]
  182. end
  183.  
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement