BioPrince

enderbutton

Oct 28th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.47 KB | None | 0 0
  1. local Button = {}
  2. Button.__index = Button
  3.  
  4. setmetatable(Button, {
  5.    __call = function(cls, ...)
  6.       return cls.new(...)
  7.    end
  8. })
  9.  
  10. for i, side in pairs(rs.getSides()) do
  11.    if peripheral.getType(side) == "monitor" then
  12.       local monitor = peripheral.wrap(side)
  13.       if monitor.isColor() then
  14.          Button.monitor = monitor
  15.          break
  16.       end
  17.    end
  18. end
  19.  
  20. if not Button.monitor then
  21.    error("Button api requires an Advanced Monitor")
  22. end
  23.  
  24. -- add a new button. colors are optional.
  25. function Button.new(text, callback, xMin, xMax, yMin, yMax, color)
  26.    local self = setmetatable({}, Button)
  27.  
  28.    self.text = text
  29.    self.callback = callback
  30.    self.x = { min = xMin, max = xMax }
  31.    self.y = { min = yMin, max = yMax }
  32.  
  33.    self.enabled = true
  34.    self.visible = true
  35.  
  36.    self.colors = { text = colors.white, background = colors.black, enabled = colors.lime, disabled = colors.red }
  37.    if color ~= nil and type(color) == "table" then
  38.       for k, v in pairs(color) do
  39.          self.colors[k] = v
  40.       end
  41.    end
  42.  
  43.    -- store button in table for easier click handling
  44.    if Button._buttons ~= nil then
  45.       table.insert(Button._buttons, self)
  46.    else -- first button being added
  47.       Button["_buttons"] = { self }
  48.    end
  49.  
  50.    self:display()
  51.  
  52.    return self
  53. end
  54.  
  55. function Button:display()
  56.    local color = self.visible and (self.enabled and self.colors.enabled or self.colors.disabled) or self.colors.background
  57.  
  58.    self.monitor.setBackgroundColor(color)
  59.    self.monitor.setTextColor(self.colors.text)
  60.  
  61.    local center = math.floor((self.y.min + self.y.max) / 2)
  62.  
  63.    for j = self.y.min, self.y.max do
  64.       self.monitor.setCursorPos(self.x.min, j)
  65.  
  66.       if j == center and self.visible then
  67.          local length = self.x.max - self.x.min
  68.          local space = string.rep(" ", (length - string.len(self.text)) / 2)
  69.  
  70.          self.monitor.write(space)
  71.          self.monitor.write(self.text)
  72.          self.monitor.write(space)
  73.  
  74.          if string.len(space) * 2 + string.len(self.text) < length then
  75.             self.monitor.write(" ")
  76.          end
  77.       else
  78.          self.monitor.write(string.rep(" ", self.x.max - self.x.min))
  79.       end
  80.    end
  81.  
  82.    self.monitor.setBackgroundColor(self.colors.background)
  83. end
  84.  
  85. function Button:enable()
  86.    self.enabled = true
  87.    self:display()
  88. end
  89.  
  90. function Button:disable()
  91.    self.enabled = false
  92.    self:display()
  93. end
  94.  
  95. function Button:flash(interval)
  96.    self:disable()
  97.    sleep(interval or 0.15)
  98.    self:enable()
  99. end
  100.  
  101. function Button:show()
  102.    self.visible = true
  103.    self:display()
  104. end
  105.  
  106. function Button:hide()
  107.    self.visible = false
  108.    self:display()
  109. end
  110.  
  111. function awaitClick()
  112.    local event, side, x, y = os.pullEvent("monitor_touch")
  113.  
  114.    for i, button in pairs(Button._buttons) do
  115.       if button.enabled and button.x.min <= x and button.x.max >= x and button.y.min <= y and button.y.max >= y then
  116.          button.callback(button)
  117.       end
  118.    end
  119. end
  120.  
  121. function setMonitor(monitor)
  122.    if monitor == nil or not monitor.isColor() then
  123.       error("Button api requires an Advanced Monitor")
  124.    end
  125.  
  126.    Button.monitor = monitor
  127. end
  128.  
  129. -- remove button from table and thus makes it unclickable. Probably not needed in most cases.
  130. function remove(button)
  131.    if Button._buttons then
  132.       for i, b in pairs(Button._buttons) do
  133.          if button == b then
  134.             table.remove(Button._buttons, i)
  135.             break
  136.          end
  137.       end
  138.    end
  139. end
  140.  
  141. new = Button.new
Add Comment
Please, Sign In to add comment