LatvianModder

Button API

Jun 24th, 2014
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.57 KB | None | 0 0
  1. local mon = nil
  2. local button={}
  3. local defaultOnCol, defaultOffCol
  4.  
  5. function init(p)
  6.   defaultOnCol = colors.lime
  7.   defaultOffCol = colors.red
  8.   mon = p
  9.   mon.clear()
  10.   mon.setTextColor(colors.white)
  11.   mon.setBackgroundColor(colors.black)
  12.   button = {}
  13. end
  14.  
  15. function setTextScale(s)
  16.   mon.setTextScale(1)
  17. end
  18.  
  19. function clear()
  20.   button = {}
  21.   mon.clear()
  22. end
  23.  
  24. function add(id, name, x, y, w, h)
  25.   button[id] = {}
  26.   button[id]["name"] = name
  27.   button[id]["active"] = false
  28.   button[id]["xmin"] = x
  29.   button[id]["ymin"] = y
  30.   button[id]["xmax"] = x + w
  31.   button[id]["ymax"] = y + h
  32.   button[id]["colOn"] = defaultOnCol
  33.   button[id]["colOff"] = defaultOffCol
  34. end
  35.  
  36. function setDefaultColors(colOn, colOff)
  37.   defaultOnCol = colOn
  38.   defaultOffCol = colOff
  39. end
  40.  
  41. function setColors(id, colOn, colOff)
  42.   button[id]["colOn"] = colOn
  43.   button[id]["colOff"] = colOff
  44. end
  45.  
  46. function fill(text, color, bData)
  47.   mon.setBackgroundColor(color)
  48.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  49.   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  50.   for j = bData["ymin"], bData["ymax"] do
  51.     mon.setCursorPos(bData["xmin"], j)
  52.     if j == yspot then
  53.       for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  54.         if k == xspot then
  55.           mon.write(text)
  56.         else
  57.           mon.write(" ")
  58.         end
  59.       end
  60.     else
  61.       for i = bData["xmin"], bData["xmax"] do
  62.         mon.write(" ")
  63.       end
  64.     end
  65.   end
  66.   mon.setBackgroundColor(colors.black)
  67. end
  68.  
  69. function render()
  70.   for id,data in pairs(button) do
  71.     local currColor = colors.black
  72.     local on = data["active"]
  73.     if on == true then currColor = data["colOn"] else currColor = data["colOff"] end
  74.     fill(data["name"], currColor, data)
  75.   end
  76. end
  77.  
  78. function toggle(name)
  79.   setOn(name, not isOn(name))
  80.   return isOn(name)
  81. end
  82.  
  83. function isOn(name)
  84.   return button[name]["active"]
  85. end
  86.  
  87. function setOn(name, on)
  88.   button[name]["active"] = on
  89.   render()
  90. end
  91.  
  92. function flash(name)
  93.   setOn(name, true)
  94.   sleep(0.07)
  95.   setOn(name, false)
  96.   sleep(0.07)
  97. end
  98.  
  99. function checkxy(x, y)
  100.   for id, data in pairs(button) do
  101.     if y>=data["ymin"] and  y <= data["ymax"] then
  102.       if x>=data["xmin"] and x<= data["xmax"] then
  103.         os.queueEvent("button", id)
  104.         return true
  105.       end
  106.     end
  107.   end
  108.   return false
  109. end
  110.  
  111. function heading(text)
  112.   w, h = mon.getSize()
  113.   mon.setCursorPos((w-string.len(text))/2+1, 1)
  114.   mon.write(text)
  115. end
  116.  
  117. function label(w, h, text)
  118.   mon.setCursorPos(w, h)
  119.   mon.write(text)
  120. end
Advertisement
Add Comment
Please, Sign In to add comment