Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This is the click API:
- -- This allows you to easily make clickable areas
- -- and buttons with or without borders.
- -- Chock500 - 22|12|12,016HE
- os.loadAPI("chockos/config")
- -- Simplifies the button function
- function fnButton()
- local event, button, X, Y = os.pullEvent( "mouse_click" )
- return button, X, Y
- end
- -- Checks if an area is clicked, rather than a point
- function ifButton(X,Y,minX,minY,maxX,maxY)
- -- X&Y is input from fnButton()
- -- The other variables are to define the square
- -- in which it checks
- if X >= minX and X <= maxX and Y >= minY and Y <= maxY then
- return true
- else
- return false
- end
- end
- -- Draws a button with or without a border
- function obButton(X,Y,label,oX,oY,length,border)
- -- X&Y (int) are inputs from fnButton()'s X&Y
- -- Label (str) is what the button displays
- -- oX&oY (int<16) are the position of the
- -- top-left corner of the button
- -- border (true/false) enables or disables the
- -- border
- width = 0
- if border == true then
- -- with border
- -- this is so bodged, I can't explain it, even
- -- just after finishing it. Also, the Y value
- -- cannot be greater than 15 for some reason.
- -- (It breaks if you do that).
- -- All you need to know is that it draws a box
- -- around the label text and includes it in its
- -- hitbox.
- -- x---------x
- -- |Like this|
- -- x---------x
- --
- -- Essentially, this is the ifButton() function,
- -- but it draws a label. Also, have the
- -- fnButton() function after all of the
- -- 'if obButton() then ... end's, otherwise they
- -- will not draw before you click once
- oX = oX + 1
- oY = oY + 2
- term.setCursorPos(oX-1,oY-1)
- print("|"..label.."|")
- width = 2
- term.setCursorPos(oX-1,oY-2)
- j = 0
- yOff = 0
- while j < 2 do
- i = 0
- term.setCursorPos(oX-1,(oY-2)+yOff)
- print("x")
- while i < length do
- term.setCursorPos(oX+i,(oY-2)+yOff)
- print("-")
- i = i + 1
- end
- term.setCursorPos(oX+length,(oY-2)+yOff)
- print("x")
- yOff = 2
- j = j + 1
- end
- return ifButton(X+1,Y+2,oX,oY,oX+(length)+1,oY+width)
- else
- -- Without border
- term.setCursorPos(oX,oY)
- print(label)
- return ifButton(X,Y,oX,oY,oX+(length-1),oY)
- end
- end
- -- Returns true or false
- -- Exactly the same as obButton(), except has
- -- colours
- function colObButton(X,Y,label,oX,oY,length,border,textcol,buttoncol)
- -- X&Y (int) are inputs from fnButton()'s X&Y
- -- Label (str) is what the button displays
- -- oX&oY (int<16) are the position of the
- -- top-left corner of the button
- -- border (true/false) enables or disables the
- -- border
- term.setBackgroundColour(buttoncol)
- term.setTextColour(textcol)
- width = 0
- if border == true then
- -- with border
- -- this is so bodged, I can't explain it, even
- -- just after finishing it. Also, the Y value
- -- cannot be greater than 15 for some reason.
- -- (It breaks if you do that).
- -- All you need to know is that it draws a box
- -- around the label text and includes it in its
- -- hitbox.
- -- x---------x
- -- |Like this|
- -- x---------x
- --
- -- Essentially, this is the ifButton() function,
- -- but it draws a label. Also, have the
- -- fnButton() function after all of the
- -- 'if obButton() then ... end's, otherwise they
- -- will not draw before you click once
- oX = oX + 1
- oY = oY + 2
- term.setCursorPos(oX-1,oY-1)
- print("|"..label.."|")
- width = 2
- term.setCursorPos(oX-1,oY-2)
- j = 0
- yOff = 0
- while j < 2 do
- i = 0
- term.setCursorPos(oX-1,(oY-2)+yOff)
- print("x")
- while i < length do
- term.setCursorPos(oX+i,(oY-2)+yOff)
- print("-")
- i = i + 1
- end
- term.setCursorPos(oX+length,(oY-2)+yOff)
- print("x")
- yOff = 2
- j = j + 1
- end
- defaultColours()
- return ifButton(X+1,Y+2,oX,oY,oX+(length)+1,oY+width)
- else
- -- Without border
- term.setCursorPos(oX,oY)
- print(label)
- defaultColours()
- return ifButton(X,Y,oX,oY,oX+(length-1),oY)
- end
- end
- function defaultColours()
- term.setBackgroundColour(config.defaultBackgroundColour())
- term.setTextColour(config.defaultTextColour())
- end
- -- Returns true or false
- function buttonArray(x,y,buttons)
- -- This function allows you to simplify menus
- -- to only allow buttons to be pushed.
- -- x&y are inputs from fnButton().
- -- buttons is a bit more complicated:
- -- First, you need to used obButton() to create
- -- a button inside a function. The function has
- -- to return an array:
- --
- -- function exampleButton(x,y)
- -- return {obButton(x,y,"eg",1,1,2,true),"exampledestination"}
- -- end
- --
- -- The destination will determine what
- -- page/function the os will go to next after
- -- pushing the button
- loops = 0
- pushed = false
- buttons[table.getn(buttons)+1] = {false,false}
- while loops < table.getn(buttons) and pushed == false do
- pushed = buttons[loops+1][1]
- loops = loops+1
- end
- return (buttons[loops][2])
- -- returns next destination or false, if no
- -- button is clicked
- end
- function listButtons(dx,dy,buttons,buttonData,buttonReturn)
- length = table.getn(buttons)
- buttonLoop = 1
- maxLen = 0
- while buttonLoop <= length do
- term.setCursorPos(dx,dy+buttonLoop-1)
- if buttonData ~= false then term.setTextColour(buttonData[buttonLoop]) end
- print(buttons[buttonLoop])
- if string.len(buttons[buttonLoop]) > maxLen then
- maxLen = string.len(buttons[buttonLoop])
- end
- buttonLoop = buttonLoop + 1
- end
- term.setTextColour(config.defaultTextColour())
- button, x, y = fnButton()
- while not ifButton(x,y,dx,dy,dx+maxLen-1,dy+length) do
- button,x,y=fnButton()
- end
- return buttonReturn[y-dy+1]
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement