Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Coded by HPWebcamAble--
- --[[
- Note: This is designed to create buttons
- on an ADVANCED monitor
- How to use:
- 1. In your program, do button.wrapMon(<sideofyourmonitor>)
- 2. Then use button.draw(<parameters>) to add buttons
- name - The name of the button (as a string)
- x - The x cord of the FIRST LETTER of the NAME OF THE BUTTON
- Y - The y cord of the FIRST LETTER of the NAME OF THE BUTTON
- bOnColor - The color of the background of the button when its on
- bOffColor - The color of the background of the button when its off
- tOnColor - The color of the text of the button when its on
- tOffColor - The color of the text of the button when its off
- borderX - The number of pixels of the boarder on the x axis
- borderY - The number of pixels of the boarder on the y axis
- func - The only optional one, any function the button should run when activated
- 3. With an os.pullEvent(), find when someone touches the monitor, and then use
- button.checkCords(x,y) <-- x,y is the cords of the touch
- It returns the name of any button in the cords, or runs the function specified by draw()
- Still confused?
- Message me on the ComputerCraft Forums:
- http://www.computercraft.info/forums2/index.php?/user/25696-hpwebcamable/
- ]]
- function monClear()
- m.clear()
- end
- function wrapMon(side)
- m = peripheral.wrap(side)
- monClear()
- buttons = {}
- end
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function getButtons()
- return buttons
- end
- function setState(name,state)
- buttons[name]["state"] = state or false
- end
- function draw(name,x,y,bOnColor,bOffColor,tOnColor,tOffColor,borderX,borderY,func)
- buttons[name] = {}
- buttons[name]["xpos"] = x
- buttons[name]["ypos"] = y
- buttons[name]["bOnColor"] = bOnColor
- buttons[name]["bOffColor"] = bOffColor
- buttons[name]["tOnColor"] = tOnColor
- buttons[name]["tOffColor"] = tOffColor
- buttons[name]["borderX"] = borderX
- buttons[name]["borderY"] = borderY
- buttons[name]["state"] = false
- buttons[name]["func"] = func
- drawButtons()
- end
- function toggleButton(name)
- buttons[name]["state"] = not buttons[name]["state"]
- drawButtons()
- return buttons[name]["state"]
- end
- function flashButton(name,time)
- buttons[name]["state"] = not buttons[name]["state"]
- drawButtons()
- sleep(time)
- buttons[name]["state"] = not buttons[name]["state"]
- end
- function drawButtons()
- for name,data in pairs(buttons) do
- local xpos = buttons[name]["xpos"]
- local ypos = buttons[name]["ypos"]
- local xborder = buttons[name]["borderX"]
- local yborder = buttons[name]["borderY"]
- m.setCursorPos(xpos,ypos)
- if buttons[name]["state"] then
- m.setBackgroundColor(buttons[name]["bOnColor"])
- m.setTextColor(buttons[name]["tOnColor"])
- else
- m.setBackgroundColor(buttons[name]["bOffColor"])
- m.setTextColor(buttons[name]["tOffColor"])
- end
- m.write(name)
- m.setCursorPos(xpos-xborder,ypos-yborder)
- tempx,tempy = m.getCursorPos()
- tempx2 = tempx
- for i = 1, yborder do
- tempx = tempx2
- for j = 1, buttons[name]["borderX"]*2+string.len(name) do
- m.setCursorPos(tempx,tempy)
- m.write(" ")
- tempx = tempx+1
- end
- tempy = tempy+1
- end
- m.setCursorPos(buttons[name]["xpos"]-xborder,buttons[name]["ypos"])
- for i = 1, xborder do
- m.write(" ")
- end
- m.setCursorPos(buttons[name]["xpos"]+string.len(name),buttons[name]["ypos"])
- for i = 1, xborder do
- m.write(" ")
- end
- tempx = buttons[name]["xpos"]-xborder
- tempy = buttons[name]["ypos"]+1
- for i = 1, buttons[name]["borderY"] do
- tempx = tempx2
- for j = 1, xborder*2+string.len(name) do
- m.setCursorPos(tempx,tempy)
- m.write(" ")
- tempx = tempx+1
- end
- tempy = tempy+1
- end
- end
- m.setBackgroundColor(colors.black)
- end
- function checkCords(x,y)
- for name,data in pairs(buttons) do
- tempx = buttons[name]["xpos"]
- tempy = buttons[name]["ypos"]
- xborder = buttons[name]["borderX"]
- yborder = buttons[name]["borderY"]
- minx = tempx-xborder
- miny = tempy-yborder
- maxx = tempx+string.len(name)+xborder
- maxy = tempy+yborder
- if x >= minx and x <= maxx and y >= miny and y <= maxy then
- if buttons[name]["func"] ~= nil then
- buttons[name]["func"]()
- else
- return name
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement