Advertisement
neonerz

guiAPI

Mar 28th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1. --
  2. --GUI API by neonerZ v1.0
  3. --http://youtube.com/neonerz
  4. --
  5.  
  6. -- use os.loadAPI("guiAPI") (or whatever you named it) to include this
  7. -- API in your script. To call a function, you'd use guiAPI.function(vairables)
  8. -- If you named this script somthing other than guiAPI, adjust the above command accordingly
  9. -- example usage: register a button
  10. -- guiAPI.setTable("button1", "Button Name", 3, 4, 10, 3, 4000, 4001, colors.blue, colors.white)
  11. -- This script must be in the same directory as the script calling it
  12. -- you can also add this to your turtles rom/api folder to use it like a standard computercraft API
  13.  
  14.  
  15. local button={}
  16.  
  17. --Function to create button table
  18. function setTable(name, text, xmin, ymin, width, height, onFreq, offFreq, bcolor, tcolor)
  19.    button[name] = {}
  20.    button[name]["text"] = text
  21.    button[name]["active"] = false
  22.    button[name]["xmin"] = xmin
  23.    button[name]["ymin"] = ymin
  24.    button[name]["width"] = width
  25.    button[name]["height"] = height
  26.    button[name]["onFreq"] = onFreq
  27.    button[name]["offFreq"] = offFreq
  28.    button[name]["bcolor"] = bcolor
  29.    button[name]["tcolor"] = tcolor
  30. end
  31.  
  32. --Toggle button on and off
  33. function toggleButton(name, state)
  34.     if state=="on" then
  35.         button[name]["active"] = true
  36.     else
  37.         button[name]["active"] = false
  38.     end
  39. end    
  40.  
  41. --Draw button on screen
  42. function drawButton(x, y, width, height, bcolor, tcolor, text)
  43.     m.setBackgroundColor(bcolor)
  44.     --Draw the background
  45.     for i=1,height do
  46.         m.setCursorPos(x,y+i-1)
  47.         m.write(string.rep(" ", width))
  48.     end
  49. --  m.setBackgroundColor(colors.black)
  50.     --Write the text
  51.      m.setTextColor(tcolor)
  52.     local textX = x + math.floor(width/2 - string.len(text)/2)
  53.     local textY = y + math.floor(height/2)
  54.     m.setCursorPos(textX, textY)
  55.     m.write(text)
  56. end
  57.  
  58.  --Draw a box on the monitor
  59. function drawBox(xpos, ypos, height, width, title, bcolor, tcolor, tbcolor)
  60. --  if xpos == 1 then xpos=2 end
  61. --  if ypos == 1 then ypos=2 end
  62.     if ypos > height then height = height+ypos end
  63.     if xpos > width then rwidth = xpos+width -2 else rwidth = width end
  64.     for h = ypos,height+1 do
  65.         for i = xpos,rwidth+1 do
  66.             m.setCursorPos(i,h)
  67.             m.setBackgroundColor(bcolor)
  68.             m.write(" ")
  69.         end
  70.     end
  71.     titleCount = #title
  72.     titlePos = math.floor((width-titleCount)/2)+xpos
  73.     m.setBackgroundColor(tbcolor)
  74.     m.setTextColor(tcolor)
  75.     m.setCursorPos(titlePos,ypos)
  76.     m.write(title)
  77.     m.setBackgroundColor(defaultMonColor)
  78.     h=nil
  79.     i=nil
  80. end
  81.  
  82. --Draw text on the monitor
  83. function drawText(xpos,ypos,tcolor,bcolor,text)
  84.     m.setCursorPos(xpos,ypos)
  85.     m.setTextColor(tcolor)
  86.     m.setBackgroundColor(bcolor)
  87.     m.write(text)  
  88.     m.setBackgroundColor(defaultMonColor)
  89. end
  90.  
  91. --Check if a button within the table has been clicked
  92. --(returns button name if matches, or false if not)
  93. function checkxy(x, y)
  94.     returnCheckxy=false
  95.     for name, data in pairs(button) do
  96.         if data["height"] == 1 then
  97.             ymax = data["ymin"]
  98.         else
  99.             ymax = data["height"] + data["ymin"]
  100.         end
  101.        
  102.         if data["width"] == 1 then
  103.             xmax = data["xmin"]
  104.         else
  105.             xmax = data["width"] + data["xmin"]
  106.         end
  107.         if y>=data["ymin"] and  y <= ymax then
  108.             if x>=data["xmin"] and x<= xmax then
  109.                 returnCheckxy = {name = name,
  110.                                 text = data["text"],
  111.                                 onFreq = data["onFreq"],
  112.                                 offFreq = data["offFreq"],
  113.                                 bcolor = data["bcolor"],
  114.                                 tcolor = data["tcolor"],
  115.                                 active = data["active"]}
  116.             end
  117.         end
  118.     end
  119. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement