Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Button API
- -- Version: 1.0
- -- Tested in: computercraft 1.58 (Minecraft 1.6.4)
- -- By: Termanater13 (of the computercraft forums)
- -- Link: http://www.computercraft.info/forums2/index.php?/topic/17346-button-api/
- -- Description: Turn your advanced monitor into a touch screen with
- -- with fully customizable buttons and labels.
- -- Support: if you are having any issues with This API, use the link
- -- and follow the directions there.
- -- Credits: Direwolf20 for the button API he made that made me want
- -- to make my own, the display code is this is based on his code
- -- since ones I tried would not work.
- -- set variables for API
- local buttonData = {}
- local buttonFunc = {}
- local mon = {}
- local chk = {}
- new = {}
- e = {}
- e.s={}
- get = {}
- -- initioal settings for buttonSetting
- local buttonSetting = {}
- buttonSetting.back = {}
- buttonSetting.back.on = colors.lime
- buttonSetting.back.off = colors.red
- buttonSetting.back.non = colors.black
- buttonSetting.text = {}
- buttonSetting.text.on = colors.white
- buttonSetting.text.off = colors.white
- buttonSetting.text.non = colors.white
- buttonSetting.scale = 1
- buttonSetting.size = {}
- buttonSetting.size.x = nil
- buttonSetting.size.y = nil
- -- set Side for the button to be displayed
- function wrap (side)
- mon = peripheral.wrap(side)
- buttonSetting.size.x, buttonSetting.size.y = mon.getSize()
- end
- -- find coordinates middle value
- local function fx (l,h,t)
- local n = string.len(t)
- local f = h-l-n
- if (f%2) == 0 then
- return (f/2)
- else
- return (math.floor(f/2)+1)
- end
- end
- local function fy (l,h)
- return math.floor((h+l)/2)
- end
- -- check functions
- function chk.color(color)
- if type(color) == "number" then
- for i = 0,15,1 do
- if color == (2^i) then
- return color
- end
- end
- elseif type(color) == "string" then
- color = string.lower(color)
- if color == "lightgray" or color == "light grey" then
- color = "lightGray"
- end
- if color == "lightblue" or color == "light blue" then
- color = "lightBlue"
- end
- if colors[color] then
- return colors[color]
- end
- else
- return false
- end
- end
- -- make a new button
- function new.b (id,text,xmin,xmax,ymin,ymax,func,argpass)
- buttonData[id]={}
- buttonData[id].x={}
- buttonData[id].x.min = xmin
- buttonData[id].x.max = xmax
- buttonData[id].y={}
- buttonData[id].y.min = ymin
- buttonData[id].y.max = ymax
- buttonData[id].text = {}
- buttonData[id].text.on = nil
- buttonData[id].text.off = nil
- buttonData[id].text.show = text
- buttonData[id].back = {}
- buttonData[id].back.on = nil
- buttonData[id].back.off = nil
- buttonData[id].display = true
- buttonData[id].click = true
- buttonData[id].hidden = false
- buttonData[id].label = "B"
- buttonData[id].status = false
- buttonFunc[id] = {}
- buttonFunc[id].func = func or nil
- buttonFunc[id].argpass = argpass or nil
- end
- function new.l (id,text,x,y,func,argpass)
- buttonData[id]={}
- buttonData[id].x={}
- buttonData[id].x.min = x
- buttonData[id].x.max = x + string.len(text)
- buttonData[id].y={}
- buttonData[id].y.min = y
- buttonData[id].y.max = y
- buttonData[id].text = {}
- buttonData[id].text.on = nil
- buttonData[id].text.off = nil
- buttonData[id].text.show = text
- buttonData[id].back = {}
- buttonData[id].back.on = nil
- buttonData[id].back.off = nil
- buttonData[id].display = true
- buttonData[id].click = false
- buttonData[id].hidden = false
- buttonData[id].label = "L"
- buttonData[id].status = false
- buttonFunc[id] = {}
- buttonFunc[id].func = func or nil
- buttonFunc[id].argpass = argpass or nil
- end
- -- edit commands to directly set the values
- function e.color (id, T, on, off)
- T = string.lower(T)
- if T == "back" or T == "background" then
- buttonData[id].back.on = chk.color(on)
- buttonData[id].back.off = chk.color(off)
- elseif T == "text" then
- buttonData[id].text.on = chk.color(on)
- buttonData[id].text.off = chk.color(off)
- end
- end
- function e.x (id, T, value)
- if type(T) == "number" then
- if T > value then
- value, T = T, value
- end
- buttonData[id].x.min = T
- buttonData[id].x.max = value
- elseif type(T) == "string" then
- if string.lower(T) == "min" then
- buttonData[id].x.min = value
- elseif string.lower(T) == "max" then
- buttonData[id].x.max = value
- end
- end
- end
- function e.y (id, T, value)
- if type(T) == "number" then
- if T > value then
- value, T = T, value
- end
- buttonData[id].x.min = T
- buttonData[id].x.max = value
- elseif type(T) == "string" then
- if string.lower(T) == "min" then
- buttonData[id].x.min = value
- elseif string.lower(T) == "max" then
- buttonData[id].x.max = value
- end
- end
- end
- function e.text (id, value)
- if type(value) == "string" then
- buttonData[id].text.show = value
- end
- end
- function e.label (id, value)
- value = string.lower(value)
- if value == "b" or value == "button" then
- buttonData[id].label = "B"
- elseif value == "l" or value == "label" then
- buttonData[id].label = "L"
- end
- end
- function e.func (id, value)
- if type(value) == "function" then
- buttonFunc[id].func = value
- end
- end
- function e.argpass(id, value)
- buttonFunc[id].argpass = argpass
- end
- -- edit commands to set or toggle values
- function e.display (id, value)
- if type(value) == "string" then
- if string.lower(value) == "on" or string.lower(value) == "yes" then
- value = true
- elseif string.lower(value) == "off" or string.lower(value) == "no" then
- value = false
- end
- end
- if value == nil or type(value) ~= "boolean" then
- buttonData[id].display = not buttonData[id].display
- elseif type(value) == "boolean" then
- buttonData[id].display = value
- end
- end
- function e.click (id, value)
- if type(value) == "string" then
- if string.lower(value) == "on" or string.lower(value) == "yes" then
- value = true
- elseif string.lower(value) == "off" or string.lower(value) == "no" then
- value = false
- end
- end
- if value == nil or type(value) ~= "boolean" then
- buttonData[id].click = not buttonData[id].click
- elseif type(value) == "boolean" then
- buttonData[id].click = value
- end
- end
- function e.status (id, value)
- if type(value) == "string" then
- if string.lower(value) == "on" or string.lower(value) == "yes" then
- value = true
- elseif string.lower(value) == "off" or string.lower(value) == "no" then
- value = false
- end
- end
- if value == nil or type(value) ~= "boolean" then
- buttonData[id].status = not buttonData[id].status
- elseif type(value) == "boolean" then
- buttonData[id].status = value
- end
- end
- function e.hidden (id, value)
- if type(value) == "string" then
- if string.lower(value) == "on" or string.lower(value) == "yes" then
- value = true
- elseif string.lower(value) == "off" or string.lower(value) == "no" then
- value = false
- end
- end
- if value == nil or type(value) ~= "boolean" then
- buttonData[id].hidden = not buttonData[id].hidden
- elseif type(value) == "boolean" then
- buttonData[id].hidden = value
- end
- end
- -- used to edit the settings
- function e.s.color(T, S, color)
- T = string.lower(T)
- S = string.lower(S)
- local test = {}
- if T == "back" or T == "background" or T == "text" then
- test.t = true
- if T == "background" then
- T = "back"
- end
- end
- if S == "on" or S == "Off" or S == "non" then
- test.s = true
- end
- if test.t and test.s and chk.color(color) then
- buttonSetting[T][S] = chk.color(color)
- end
- end
- function e.s.scale(scale)
- buttonSetting.scale = scale
- mon.setTextScale(scale)
- end
- -- used to get data for buttons
- function get.b (id)
- if id == nil then
- return buttonData
- else
- return buttonData[id]
- end
- end
- get.l = get.b
- function get.s ()
- return buttonSetting
- end
- function get.f (id)
- return buttonFunc[id]
- end
- -- color select
- local function colorselect(value, T)
- if value.status then
- if value.label == "B" then
- if value[T].on == nil then
- return buttonSetting[T].on
- else
- return value[T].on
- end
- elseif value.label == "L" then
- if value[T].on == nil then
- return buttonSetting[T].non
- else
- return value[T].on
- end
- end
- else
- if value.label == "B" then
- if value[T].off == nil then
- return buttonSetting[T].off
- else
- return value[T].off
- end
- elseif value.label == "L" then
- if value[T].off == nil then
- return buttonSetting[T].non
- else
- return value[T].off
- end
- end
- end
- end
- -- display code
- function display ()
- mon.setBackgroundColor(buttonSetting.back.non)
- mon.setTextColor(buttonSetting.text.non)
- mon.clear()
- for key, value in pairs(buttonData) do
- if value.display then
- ys = fy(value.y.min,value.y.max)
- xs = fx(value.x.min,value.x.max,value.text.show)
- mon.setBackgroundColor(colorselect(value, "back"))
- mon.setTextColor(colorselect(value, "text"))
- for y = value.y.min, value.y.max, 1 do
- mon.setCursorPos(value.x.min, y)
- if y == ys then
- for x = 0, (value.x.max-value.x.min-string.len(value.text.show)+1), 1 do
- if x == xs then
- mon.write(value.text.show)
- else
- mon.write(" ")
- end
- end
- else
- for x = 0, (value.x.max-value.x.min), 1 do
- mon.write(" ")
- end
- end
- end
- end
- end
- end
- -- check if button is clicked
- function check(x,y)
- for key, value in pairs(buttonData) do
- if value.y.min <= y and y <= value.y.max then
- if value.x.min <= x and x <= value.x.max then
- if ((value.hidden and value.click) or (value.display and value.click)) and buttonFunc[key].func ~= nil then
- if buttonFunc[key].argpass ~= nil then
- buttonFunc[key].func(buttonFunc[key].argpass)
- else
- buttonFunc[key].func()
- end
- end
- end
- end
- end
- end
- -- cycle command (not recommended but works)
- function cycle ()
- local cychk = {}
- while true do
- display()
- cychk["event"],cychk["side"],cychk["x"],cychk["y"] = os.pullEvent("monitor_touch")
- check(cychk["x"],cychk["y"])
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment