Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Button 3.0 API
- --By Vlad [vladimir264]
- local buttons={} --Database that contains all instances localy
- newButton = function(Name,X,Y,W,H)
- buttons[Name]={ object="button",
- text={x,y,text="",color=colors.white,visible=false,enabled=false},--Text stuff
- x=X,y=Y,x2=X+(W-1),y2=Y+(H-1),w=W,h=H,
- outline=nil,fill=nil,
- func=nil,
- draw=function(outline,fill) drawButton(Name,outline,fill) end,
- setFill=function(fill) setFill(Name,fill) end,
- setOutline=function(outline) setOutline(Name,outline) end,
- setText=function(text,textColor) addText(Name,text,textColor,true) end,
- enableText=function() setTextEnabled(Name,true) end,
- disableText=function() setTextEnabled(Name,false) end,
- enable=function() setEnabled(Name,true) end,
- disable=function() setEnabled(Name,false) end,
- setAction=function(action) setFunction(Name,action) end
- }
- updateGlobal(Name)
- return buttons[Name]
- end
- --[[
- function Button:new(X,Y,W,H)
- newButton(Button,X,Y,W,H)
- end
- --]]
- function drawButton(Name,Outline,Fill)--Draws the button
- b=getButton(Name)
- b.outline=Outline
- b.fill=Fill
- x=b.x
- y=b.y
- x2=b.x2
- y2=b.y2
- paintutils.drawBox(x,y,x2,y2,Outline)
- paintutils.drawFilledBox(x+1,y+1,x2-1,y2-1,Fill)
- if b.text.enabled then
- writeTxt(buttonName)
- end
- end
- function setOutline(Name,Color)
- b=getButton(Name)
- b.outline=Color
- end
- function setFill(Name,Color)
- b=getButton(Name)
- b.fill=Color
- end
- function eraseButton(Name)--Erases specified button, so that you dont see it
- b=getButton(Name)
- x=b.x
- y=b.y
- x2=b.x2
- y2=b.y2
- paintutils.drawFilledBox(x,y,x2,y2,term.getBackgroundColor())
- b.drawn=false
- b.enabled=false
- updateGlobal(Name)
- end
- function setFunction(Name,Action)--Sets a function to a button so when it's clicked, the corresponding function is run.
- getButton(Name).func=Action
- end
- function addText(Name,Text,TxtColor,Enabled)--Sets text into the button *WIP*
- length=string.len(Text)
- b=getButton(Name)
- t=getButton(Name).text
- t.x=(b.x2-(math.floor(b.w/2)))-(length/2)+1
- t.y=(b.y2-(math.floor(b.h/2)))
- t.text=Text
- t.color=TxtColor
- t.visible=false
- t.enabled=Enabled
- updateGlobal(Name)
- end
- function writeTxt(Name)--Writes the text in the button, max char length is the width of the button minus 2 *WIP*
- b=buttons[Name]
- t=buttons[Name].text
- if t.enabled then
- term.setCursorPos(t.x,t.y)
- term.setTextColor(t.color)
- term.write(t.text)
- t.visible=true
- updateGlobal(Name)
- end
- end
- function eraseTxt(Name)--Erases the text in the button
- b=getButton(Name)
- t=b.text
- x1=b.x+1
- y1=b.y+1
- x2=b.x2-1
- y2=b.y2-1
- color=b.fill
- paintutils.drawFilledBox(x1,y1,x2,y2,color)
- t.visible=false
- updateGlobal(Name)
- end
- setEnabled=function(Name,State)
- buttons[Name].enabled=State
- updateGlobal(Name)
- return State
- end
- setTextEnabled=function(Name,State)
- buttons[Name].text.enabled=State
- updateGlobal(Name)
- return State
- end
- ---------------
- --Event Stuff--
- ---------------
- local checkClickPos = function(X,Y) --checks the given position to see if its on a button
- isValid = false
- button = nil
- for k,v in pairs(buttons) do
- if k.enabled then
- if X>=k.x and X<=k.x2 and Y>=k.y and Y<=k.y2 then
- isValid = true
- button = k
- break
- end
- end
- end
- return isValid,button
- end
- --Built-in event listener, args passed to it are
- -- passed to the clicked button's connected function:
- --button.func(button,mouse-button,mouse-x,mouse-y,args)
- function initListener(...)
- while true do
- e,b,mX,mY = os.pullEvent("mouse_click")
- isValid,button = checkClickPos(mX,mY)
- if isValid then
- os.queueEvent("button_pressed",button,b,mX,mY)
- button.func(button,b,mX,mY,arg)
- end
- end
- endd
- --Extras--
- local checkExists=function(Name)
- for k,v in pairs(buttons) do
- if Name==k then
- return true
- end
- end
- return false
- end
- local calcButtonCoords=function(X,Y,W,H)
- X2=X+(W-1)
- Y2=Y+(H-1)
- return X,Y,X2,Y2
- end
- function updateGlobal(Name)
- _G[Name]=buttons[Name]
- end
- local updateAllGlobal=function()
- for k,v in pairs(buttons) do
- _G[k]=buttons[k]
- end
- end
- getButton=function(Name)--returns array containing all data for specified button
- return buttons[Name]
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement