Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --By Vlad [vladimir264]
- buttons={}
- --Settings--
- local debugMode=false
- local function buttonSetup(Name)
- buttons.Name["draw"]=function(outline,fill) drawButton(Name,outline,fill) end
- buttons.Name["setAction"]=function(action,params) setFunction(Name,action,params) end
- buttons.Name["setText"]=function(text,textColor) addTxt(Name,textColor) end
- buttons.Name["enable"]=function() setEnabled(Name,true) end
- buttons.Name["disable"]=function() setEnabled(Name,false) end
- _G[Name]=buttons.Name
- end
- function newButton(Name,X,Y,W,H)--Creates a new instance, Name is better off a string.
- 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}
- buttonSetup(buttons[Name])
- end
- function drawButton(Name,Outline,Fill)--Draws the button
- buttons.Name.outline=outLnClr
- buttons.Name.fill=fllClr
- x=getButton(Name).x
- y=getButton(Name).y
- x2=getButton(Name).x2
- y2=getButton(Name).y2
- paintutils.drawBox(x,y,x2,y2,Outline)
- paintutils.drawFilledBox(x+1,y+1,x2-1,y2-1,Fill)
- if buttons.Name.txt~=nil then
- writeTxt(buttonName)
- end
- end
- function eraseButton(Name)--Erases specified button, so that you dont see it
- x=getBtn(Name).x
- y=getBtn(Name).y
- x2=getBtn(Name).x2
- y2=getBtn(Name).y2
- paintutils.drawFilledBox(x,y,x2,y2,colors.black)
- buttons.Name.drawn=false
- end
- function waitForClick()--Waits untill the mouse is clicked
- a=true
- while a do
- event,button,xPos,yPos=os.pullEvent("mouse_click")
- for key,value in pairs(buttons) do
- button=buttons[key]
- if xPos>=button.x and xPos<=button.x2 and yPos>=button.y and yPos<=button.y2 then
- if button.func~=nil and button.enabled then
- --a=false
- --term.setBackgroundColor(term)
- --term.clear()
- button.func.action(button.func.params)
- elseif buttons[key].func==nil then
- error("No action set to button: ",buttons[key])
- --a=false
- end
- end
- end
- end
- end
- function setFunction(Name,Action,Params)--Sets a function to a button so when it's clicked, the corresponding function is run.
- buttons.Name.func={action=Action,params=Params}
- end
- function addTxt(Name,Text,TxtColor)--Sets text into the button *WIP*
- ln=string.len(Text)
- X=(buttons.Name.x2-(math.floor(buttons.Name.w/2)))-(ln/2)+1
- Y=(buttons.Name.y2-(math.floor(buttons.Name.h/2)))
- buttons.Name.txt={text=Txt,x=X,y=Y,txtColor=TxtColor}
- end
- function writeTxt(Name)--Writes the text in the button, max char length is the width of the button minus 2 *WIP*
- tX=buttons.Name.txt.x
- tY=buttons.Name.txt.y
- term.setCursorPos(tX,tY)
- term.setTextColor(buttons.Name.txt.txtColor)
- term.write(buttons.Name.txt.text)
- end
- function eraseTxt(Name)--Erases the text in the button
- x1=buttons.Name.x+1
- y1=buttons.Name.y+1
- x2=buttons.Name.x2-1
- y2=buttons.Name.y2-1
- color=buttons.Name.fill
- paintutils.drawFilledBox(x1,y1,x2,y2,color)
- end
- local setEnabled=function(Name,state)
- buttons.Name.enabled=state
- end
- --Extras--
- local getTextData=function(Name)
- btn=buttons.Name.txt
- return {x=btn.x,y=btn.y,text=btn.text,textColor=btn.txtColor}
- end
- getButton=function(Name)--returns array containing all data for specified button
- return buttons.Name
- end
- getBtn=function(Name)--alias for getButton()
- getButton(Name)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement