Advertisement
daverkex

Button API

Aug 11th, 2013
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.44 KB | None | 0 0
  1. --Buttons API for Computercraft mod of Minecraft. Create for Daverkex based on Direwolf20 Buttons API.
  2. --Structure:
  3. --    |
  4. --    |
  5. --    -> variable definitions
  6. --    |
  7. --    -> API functions in alphabetical order
  8. --    |
  9. --    ->Internal functions in alphabetical order
  10.  
  11.  
  12. --WIP:
  13. --Only render 1 label
  14. --buttons in html format?
  15. --Improve painbackground()
  16. --Documentation
  17.  
  18. local button = {}
  19. local data = {}
  20. data["side"] = "top"
  21. data["textScale"] = 1
  22. data["textColor"] = colors.white
  23. data["backgroundColor"] = colors.black
  24. data["onButtonColor"] = colors.lime
  25. data["offButtonColor"] = colors.red
  26.  
  27. local checkxy, fill, flash, null, paintBackground, paintHeading, paintLabel, paintButton, runFunc --Internal functions definitions
  28.  
  29. local mon = peripheral.wrap(data["side"])
  30. mon.setTextScale(data["textScale"])
  31. mon.setTextColor(data["textColor"])
  32. mon.setBackgroundColor(data["backgroundColor"])
  33.  
  34.  
  35. function advanceButton(id, onText, onFunc, xmin, xmax, ymin, ymax, type, offFunc, offText, onButtonColor, offButtonColor, onTextColor, offTextColor)
  36.     button[id] = {}
  37.     button[id]["onText"] = onText
  38.     if onFunc == nil then
  39.         button[id].onFunc = null()
  40.     else
  41.         button[id].onFunc = onFunc
  42.     end
  43.     button[id]["active"] = false
  44.     button[id]["xmin"] = xmin
  45.     button[id]["ymin"] = ymin
  46.     button[id]["xmax"] = xmax
  47.     button[id]["ymax"] = ymax
  48.     if type ~= nil then
  49.         if (type == "button" or type == "switch") then
  50.             button[id]["type"] = type
  51.         else
  52.             print(error("Button type invalid"))
  53.         end
  54.     else
  55.         button[id]["type"] = "button"
  56.     end
  57.     if offFunc == nil then
  58.         button[id].offFunc = null()
  59.     else
  60.         button[id].offFunc = offFunc
  61.     end
  62.     if offText ~= nil then
  63.         button[id]["offText"] = offText
  64.     end
  65.     if onButtonColor ~= nil then
  66.         button[id]["onButtonColor"] = onButtonColor
  67.     end
  68.     if offButtonColor ~= nil then
  69.         button[id]["offButtonColor"] = offButtonColor
  70.     end
  71.     if onTextColor ~= nil then
  72.         button[id]["onTextColor"] = onTextColor
  73.     end
  74.     if offTextColor ~= nil then
  75.         button[id]["offTextColor"] = offTextColor
  76.     end
  77. end
  78.  
  79. -- function changeBackgroundColors(onColor, offColor, id)
  80.     -- if id == nil then
  81.         -- if onColor ~= nil then
  82.             -- data["onButtonColor"] = onColor
  83.         -- end
  84.         -- if offColor ~= nil then
  85.             -- data["offButtonColor"] = offColor
  86.         -- end
  87.     -- else
  88.         -- if onColor ~= nil then
  89.             -- button[id]["onButtonColor"] = onColor
  90.         -- end
  91.         -- if offColor ~= nil then
  92.             -- button[id]["offButtonColor"] = offColor
  93.         -- end
  94.     -- end
  95. -- end
  96.  
  97. function clearTable()
  98.     button = {}
  99.     mon.clear()
  100.     return true
  101. end
  102.  
  103. function clickButton(id)
  104.     if button[id] == nil then
  105.         print (error("Button don't exist"))
  106.     end
  107.     local type = button[id]["type"]
  108.     if type == "button" then
  109.         runFunc(id, "onFunc")
  110.         toggle(id)
  111.         runFunc(id, "offFunc")
  112.         return true
  113.     elseif type == "switch" then
  114.         if button[id]["active"] == true then
  115.             runFunc(id, "offFunc")
  116.             toggle(id)
  117.         else
  118.             runFunc(id, "onFunc")
  119.             toggle(id)
  120.         end
  121.         return true
  122.     end
  123. end
  124.  
  125. function getClick()
  126.    event, side, x, y = os.pullEvent("monitor_touch")
  127.    checkxy(x,y)
  128.    return true
  129. end
  130.  
  131. function getVersion()
  132.     return 0.1
  133. end
  134.  
  135. function heading(text, textColor, backgroundColor)
  136.     data["heading"] = {}
  137.     data["heading"]["text"] = text
  138.     if textColor ~= nil then
  139.         data["heading"]["textColor"] = textColor
  140.     end
  141.     if backgroundColor ~= nil then
  142.         data["heading"]["backgroundColor"] = backgroundColor
  143.     end
  144.     return true
  145. end
  146.  
  147. function label(w, h, text, textColor, backgroundColor)
  148.     data["label"] = {}
  149.     data["label"]["text"] = text
  150.     data["label"]["w"] = w
  151.     data["label"]["h"] = h
  152.     if textColor ~= nil then
  153.         data["label"]["textColor"] = textColor
  154.     end
  155.     if backgroundColor ~= nil then
  156.         data["label"]["backgroundColor"] = backgroundColor
  157.     end
  158.     return true
  159. end
  160.  
  161. function monFormat(textScale, textColor, backgroundColor)
  162.     if (textScale == nil and textColor == nil and backgroundColor == nil) then
  163.         return data["textScale"], data["textColor"], data["backgroundColor"]
  164.     end
  165.     data["textScale"] = textScale
  166.     data["textColor"] = textColor
  167.     data["backgroundColor"] = backgroundColor
  168.     mon.setTextScale(data["textScale"])
  169.     mon.setTextColor(data["textColor"])
  170.     mon.setBackgroundColor(data["backgroundColor"])
  171.     return true
  172. end
  173.  
  174. function monSide(side)
  175.     if side == nil then
  176.         return data["side"]
  177.     else
  178.         data["side"] = side
  179.         mon = peripheral.wrap(data["side"])
  180.         return true
  181.     end
  182. end
  183.  
  184. function newButton(id, text, onFunc, xmin, xmax, ymin, ymax)
  185.     button[id] = {}
  186.     button[id]["onText"] = text
  187.     if onFunc == nil then
  188.         button[id].onFunc = null()
  189.     else
  190.         button[id].onFunc = onFunc
  191.     end
  192.     button[id]["active"] = false
  193.     button[id]["xmin"] = xmin
  194.     button[id]["ymin"] = ymin
  195.     button[id]["xmax"] = xmax
  196.     button[id]["ymax"] = ymax
  197.     button[id]["type"] = "button"
  198.     button[id]["offFunc"] = null()
  199. end
  200.  
  201. function paint()
  202.     if data["backgroundColor"] ~= colors.black then
  203.         paintBackground()
  204.     end
  205.     if data["heading"] ~= nil then
  206.         paintHeading()
  207.     end
  208.     if data["label"] ~= nil then
  209.         paintLabel()
  210.     end
  211.     for id, bData in pairs(button) do
  212.         paintButton(bData)
  213.     end
  214.     return true
  215. end
  216.  
  217. function toggle(id)
  218.     if button[id]["type"] == "switch" then
  219.         button[id]["active"] = not button[id]["active"]
  220.         paint()
  221.     else
  222.         flash(id)
  223.     end
  224. end
  225.  
  226.  
  227. function checkxy(x, y)
  228.     for id, data in pairs(button) do
  229.         if y >= data["ymin"] and  y <= data["ymax"] then
  230.             if x >= data["xmin"] and x <= data["xmax"] then
  231.                 return clickButton(id) --Return true if exist button
  232.             end
  233.         end
  234.     end
  235.     return false
  236. end
  237.  
  238. function flash(id)
  239.     button[id]["active"] = not button[id]["active"]
  240.     paint()
  241.     sleep(.15)
  242.     button[id]["active"] = not button[id]["active"]
  243.     paint()
  244.     return true
  245. end
  246.  
  247. function null()
  248.     return false
  249. end
  250.  
  251. function paintBackground()
  252.     local x, y = mon.getSize()
  253.     for i = 0, y do
  254.         for j = 0, x do
  255.             mon.setCursorPos(j, i)
  256.             mon.write(" ")
  257.         end
  258.     end
  259.     return true
  260. end
  261.  
  262. function paintButton(bData)
  263.     local text, textColor, buttonColor
  264.     local on = bData["active"]
  265.     if on == true then
  266.         if bData["onButtonColor"] ~= nil then
  267.             buttonColor = bData["onButtonColor"]
  268.         else
  269.             buttonColor = data["onButtonColor"]
  270.         end
  271.         if bData["onTextColor"] ~= nil then
  272.             textColor = bData["onTextColor"]
  273.         else
  274.             textColor = data["textColor"]
  275.         end
  276.         text = bData["onText"]
  277.     else
  278.         if bData["offButtonColor"] ~= nil then
  279.             buttonColor = bData["offButtonColor"]
  280.         else
  281.             buttonColor = data["offButtonColor"]
  282.         end
  283.         if bData["offTextColor"] ~= nil then
  284.             textColor = bData["offTextColor"]
  285.         else
  286.             textColor = data["textColor"]
  287.         end
  288.         if bData["offText"] ~= nil then
  289.             text = bData["offText"]
  290.         else
  291.             text = bData["onText"]
  292.         end
  293.     end
  294.     mon.setBackgroundColor(buttonColor)
  295.     mon.setTextColor(textColor)
  296.     local yspot = math.floor((bData["ymin"] + bData["ymax"]) / 2)
  297.     local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) / 2) + 1
  298.     for j = bData["ymin"], bData["ymax"] do
  299.         mon.setCursorPos(bData["xmin"], j)
  300.         if j == yspot then
  301.             for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) + 1 do
  302.                 if k == xspot then
  303.                     mon.write(text)
  304.                 else
  305.                     mon.write(" ")
  306.                 end
  307.             end
  308.         else
  309.             for i = bData["xmin"], bData["xmax"] do
  310.                 mon.write(" ")
  311.             end
  312.         end
  313.     end
  314.     mon.setBackgroundColor(data["backgroundColor"])
  315.     mon.setTextColor(data["textColor"])
  316. end
  317.  
  318. function paintHeading()
  319.     local w, h = mon.getSize()
  320.     mon.setCursorPos((w - string.len(data["heading"]["text"])) / 2 + 1, 1)
  321.     if data["heading"]["textColor"] ~= nil then
  322.         mon.setTextColor(data["heading"]["textColor"])
  323.     end
  324.     if data["heading"]["backgroundColor"] ~= nil then
  325.         mon.setBackgroundColor(data["heading"]["backgroundColor"])
  326.     end
  327.     mon.write(data["heading"]["text"])
  328.     if data["heading"]["textColor"] ~= nil then
  329.         mon.setTextColor(data["textColor"])
  330.     end
  331.     if data["heading"]["backgroundColor"] ~= nil then
  332.         mon.setBackgroundColor(data["backgroundColor"])
  333.     end
  334. end
  335.  
  336. function paintLabel()
  337.     mon.setCursorPos(data["label"]["w"], data["label"]["h"])
  338.     if data["label"]["textColor"] ~= nil then
  339.         mon.setTextColor(data["label"]["textColor"])
  340.     end
  341.     if data["label"]["backgroundColor"] ~= nil then
  342.         mon.setBackgroundColor(data["label"]["backgroundColor"])
  343.     end
  344.     mon.write(data["label"]["text"])
  345.     if data["label"]["textColor"] ~= nil then
  346.         mon.setTextColor(data["textColor"])
  347.     end
  348.     if data["label"]["backgroundColor"] ~= nil then
  349.         mon.setBackgroundColor(data["backgroundColor"])
  350.     end
  351. end
  352.  
  353. function runFunc(id, func)
  354.     return button[id][func]()
  355. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement