pluggeman

Gui

Apr 11th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.71 KB | None | 0 0
  1. --tables--
  2. buttons = {}
  3. labels = {}
  4. bars = {}
  5. frames = {}
  6.  
  7.  
  8.  
  9. --Button--
  10. Button = {
  11.     name = "btn",
  12.     text = "Button",
  13.     bounds = {
  14.         x1 = 1,
  15.         x2 = 0,
  16.         y1 = 1,
  17.         y2 = 1
  18.     },
  19.     colorActive = colors.green,
  20.     colorInactive = colors.red,
  21.     foreColor = colors.white,
  22.     active = false,
  23.     onchange = nil
  24. }
  25.  
  26. function Button:new(data)
  27.     setmetatable({}, self)
  28.     self.__index = self
  29.     for k, v in pairs(data) do
  30.         self[k] = v
  31.     end
  32.     if self.bounds.x2 == 0 then
  33.         self.bounds.x2 = self.bounds.x1 + #tostring(self.text) - 1
  34.     end
  35.     buttons[self.name] = self
  36.     return self
  37. end
  38.  
  39. function Button:toggle()
  40.     self.active = not self.active
  41.     self:draw()
  42. end
  43.  
  44. function Button:checkTouchEvent(x, y)
  45.     if x >= self.bounds.x1 and x <= self.bounds.x2 and y >= self.bounds.y1 and y <= self.bounds.y2 then
  46.         if self['onchange'] ~= nil then
  47.             self.onchange(self, x, y)
  48.         end
  49.         return true
  50.     end
  51.     return false
  52. end
  53.  
  54. function Button:draw()
  55.     local xSpot = math.floor((self.bounds.x2 - self.bounds.x1 - #tostring(self.text)) /2) + 1 + self.bounds.x1
  56.     local ySpot = math.floor((self.bounds.y1 + self.bounds.y2)/2)
  57.     paintutils.drawFilledBox(self.bounds.x1, self.bounds.y1, self.bounds.x2, self.bounds.y2, self.active and self.colorActive or self.colorInactive)
  58.     term.setCursorPos(xSpot, ySpot)
  59.     term.setTextColor(self.foreColor)
  60.     term.write(self.text)
  61. end
  62.  
  63.  
  64.  
  65. --Frame--
  66. Frame = {
  67.     name = "frm",
  68.     bounds = {
  69.         x1 = 1,
  70.         x2 = 10,
  71.         y1 = 1,
  72.         y2 = 10
  73.     },
  74.     backColor = colors.black,
  75.     borderColor = colors.white
  76. }
  77.  
  78. function Frame:new(data)
  79.     setmetatable({}, self)
  80.     self.__index = self
  81.     for k, v in pairs(data) do
  82.         self[k] = v
  83.     end
  84.     frames[self.name] = self
  85.     return self
  86. end
  87.  
  88. function Frame:draw()
  89.     paintutils.drawFilledBox(self.bounds.x1, self.bounds.y1, self.bounds.x2, self.bounds.y2, self.backColor)
  90.     paintutils.drawBox(self.bounds.x1, self.bounds.y1, self.bounds.x2, self.bounds.y2, self.borderColor)
  91. end
  92.  
  93.  
  94.  
  95. --Bar--
  96. Bar = {
  97.     name = "bar",
  98.     bounds = {
  99.         x1 = 1,
  100.         x2 = 5,
  101.         y1 = 1,
  102.         y2 = 1
  103.     },
  104.     percent = 0,
  105.     direction = 'right',
  106.     backColor = colors.gray,
  107.     foreColor = colors.lime
  108. }
  109.  
  110. function Bar:new(data)
  111.     setmetatable({}, self)
  112.     self.__index = self
  113.     for k, v in pairs(data) do
  114.         self[k] = v
  115.     end
  116.     bars[self.name] = self
  117.     return self
  118. end
  119.  
  120. function Bar:draw()
  121.     paintutils.drawFilledBox(self.bounds.x1, self.bounds.y1, self.bounds.x2, self.bounds.y2, self.backColor)
  122.    
  123.     if self.direction == 'right' then
  124.         f = self.bounds.x1 + math.floor((self.bounds.x2 - self.bounds.x1 + 1) / 100 * self.percent + 0.5) - 1
  125.         paintutils.drawFilledBox(self.bounds.x1, self.bounds.y1, f, self.bounds.y2, self.foreColor)
  126.     elseif self.direction == 'left' then
  127.         f = self.bounds.x2 - math.floor((self.bounds.x2 - self.bounds.x1 + 1) / 100 * self.percent + 0.5) + 1
  128.         paintutils.drawFilledBox(f, self.bounds.y1, self.bounds.x2, self.bounds.y2, self.foreColor)
  129.     elseif self.direction == 'up' then
  130.         f = self.bounds.y2 - math.floor((self.bounds.y2 - self.bounds.y1 + 1) / 100 * self.percent + 0.5) + 1
  131.         paintutils.drawFilledBox(self.bounds.x1, f, self.bounds.x2, self.bounds.y2, self.foreColor)
  132.     elseif self.direction == 'down' then
  133.         f = self.bounds.y1 + math.floor((self.bounds.x2 - self.bounds.x1 + 1) / 100 * self.percent + 0.5) - 1
  134.         paintutils.drawFilledBox(self.bounds.x1, self.bounds.y1, self.bounds.x2, f, self.foreColor)
  135.     end
  136. end
  137.  
  138.  
  139.  
  140. --Label--
  141. Label = {
  142.     name = "lbl",
  143.     text = "Label",
  144.     bounds = {
  145.         x1 = 1,
  146.         x2 = 0,
  147.         y1 = 1,
  148.         y2 = 1
  149.     },
  150.     backColor = colors.black,
  151.     foreColor = colors.white
  152. }
  153.  
  154. function Label:new(data)
  155.     setmetatable({}, self)
  156.     self.__index = self
  157.     for k, v in pairs(data) do
  158.         self[k] = v
  159.     end
  160.     if self.bounds.x2 == 0 then
  161.         self.bounds.x2 = self.bounds.x1 + #self.text - 1
  162.     end
  163.     labels[self.name] = self
  164.     return self
  165. end
  166.  
  167. function Label:draw()
  168.     local xSpot = math.floor((self.bounds.x2 - self.bounds.x1 - #tostring(self.text)) /2) + 1 + self.bounds.x1
  169.     local ySpot = math.floor((self.bounds.y1 + self.bounds.y2)/2)
  170.     paintutils.drawFilledBox(self.bounds.x1, self.bounds.y1, self.bounds.x2, self.bounds.y2, self.backColor)
  171.     term.setCursorPos(xSpot, ySpot)
  172.     term.setTextColor(self.foreColor)
  173.     term.write(self.text)
  174. end
  175.  
  176.  
  177.  
  178. --Draw function--
  179. function draw()
  180.     term.setBackgroundColor(colors.black)
  181.     term.setTextColor(colors.white)
  182.     term.clear()
  183.     term.setCursorPos(1,1)
  184.  
  185.     for k, v in pairs(frames) do
  186.         v:draw()
  187.     end
  188.     for k, v in pairs(bars) do
  189.         v:draw()
  190.     end
  191.     for k, v in pairs(buttons) do
  192.         v:draw()
  193.     end
  194.    
  195.     for k, v in pairs(labels) do
  196.         v:draw()
  197.     end
  198. end
  199.  
  200.  
  201.  
  202. --Check for a touch event--
  203. function run()
  204.   while true do
  205.     local e, side, x, y = os.pullEvent("monitor_touch")
  206.     for k, v in pairs(buttons) do
  207.         v:checkTouchEvent(x, y)
  208.     end
  209.   end
  210. end
Add Comment
Please, Sign In to add comment