Advertisement
RodrickLord

Horizon UI

Apr 9th, 2015
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. --[[  HorizonUI
  2.   Design or GUI api
  3.  Made by RodrickLord ]]
  4.  
  5. local termX,termY = term.getSize()
  6. local centerX,centerY = termX/2+1,termY/2
  7.  
  8. -- Simple Functions
  9.  
  10. function centerWrite(text,y,tColor)
  11.   if tColor then term.setTextColor(tColor) end
  12.   local _,cy = term.getCursorPos()
  13.   term.setCursorPos(centerX-#text/2,y or cy)
  14.   write(text)
  15. end
  16.  
  17. function clearLine(y,bColor)
  18.   local _,cy = term.getCursorPos()
  19.   if bColor then term.setBackgroundColor(bColor) end
  20.   term.setCursorPos(1,y or cy)
  21.   term.clearLine()
  22. end
  23.  
  24. function clear(bColor)
  25.   if bColor then term.setBackgroundColor(bColor) end
  26.   term.clear()
  27. end
  28.  
  29. -- Buttons
  30.  
  31. buttons = {
  32.   _default = {tColor = colors.white, bColor = colors.lightBlue, enabled = true}
  33. }
  34.  
  35. function createButton(id,text,x,y,x2,y2,action,tColor,bColor,enabled)
  36.   if not buttons[id] then
  37.     local default = buttons._default
  38.     buttons[id] = default
  39.     buttons[id] = {
  40.       text = text, x = x, y = y, x2 = x2 or x+#text+1, y2 = y2 or y+2, tColor = tColor or colors.white, bColor = bColor or colors.lightBlue, enabled = enabled or true
  41.     }
  42.     if enabled then drawButton(id) end
  43.     return true
  44.   else
  45.     return false
  46.   end
  47. end
  48.  
  49. function deleteButton(id)
  50.   if buttons[id] then
  51.     buttons[id] = nil
  52.     return true
  53.   else
  54.     return false
  55.   end
  56. end
  57.  
  58. function drawButton(id)
  59.   if buttons[id] then
  60.     paintutils.drawFilledBox(buttons[id].x,buttons[id].y,buttons[id].x2,buttons[id].y2,buttons[id].bColor)
  61.     term.setCursorPos(buttons[id].x+1,buttons[id].y+1)
  62.     term.setTextColor(buttons[id].tColor)
  63.     write(buttons[id].text)
  64.   else
  65.     return false
  66.   end
  67. end
  68.  
  69. function enableButton(...)
  70.   local args = {...}
  71.   for _,v in pairs(args) do
  72.     if button[v] then button[v].enabled = true end
  73.   end
  74. end
  75.  
  76. function disableButton(...)
  77.   local args = {...}
  78.   for _,v in pairs(args) do
  79.     if button[v] then button[v].enabled = false end
  80.   end
  81. end
  82.  
  83. function setButtonOption(id,key,value)
  84.   if buttons[id][key] then
  85.     buttons[id][key] = value
  86.   else
  87.     return false
  88.   end
  89. end
  90.  
  91. function handleClick()
  92.   buttons._running = true
  93.   while buttons._running do
  94.     local _,_,cx,cy = os.pullEvent("mouse_click")
  95.     for k,v in pairs(buttons) do
  96.       if v.x and v.y and v.x2 and v.y2 then
  97.         if (cx>=v.x and cx<=v.x2) and (cy>=v.y and cy<=v.y2) then v.action() end
  98.       end
  99.     end
  100.   end
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement