Advertisement
Neon1432

terminal

Feb 12th, 2022 (edited)
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.28 KB | None | 0 0
  1. --------------------------------------------------------------------------------------------------------------------------------------------
  2. --Base Classes
  3. --------------------------------------------------------------------------------------------------------------------------------------------
  4.  
  5.  
  6.  
  7. local visual = {}
  8.  
  9.  
  10.  
  11. function visual:new(o)
  12.     o = o or {}
  13.     setmetatable(o, self)
  14.     self.__index = self
  15.     return o
  16. end
  17.  
  18.  
  19.  
  20. function visual:print(amask, acomp)
  21.     printError("visual: \""..self.name.."\":print() is not defined")
  22.     return false
  23. end
  24.  
  25.  
  26.  
  27. local component = {x = 1, y = 1, width = 0, height = 0, visuals = {}}
  28.  
  29.  
  30.  
  31. function component:new(o)
  32.     o = o or {}
  33.     setmetatable(o, self)
  34.     self.__index = self
  35.     local newv = {}
  36.     for I = 1, #self.visuals do
  37.         newv[I] = self.visuals[I]
  38.     end
  39.     o.visuals = newv
  40.     return o
  41. end
  42.  
  43.  
  44.  
  45. function component:print(amask)
  46.     for I = 1, #self.visuals do
  47.         self.visuals[I]:print(amask, self)
  48.     end
  49. end
  50.  
  51.  
  52.  
  53. function component:isTouched(x, y)
  54.     if x >= self.x and x < (self.x + self.width) then
  55.         if y >= self.y and y < (self.y + self.height) then
  56.             return true
  57.         else
  58.             return false
  59.         end
  60.     else
  61.         return false
  62.     end
  63. end
  64.  
  65.  
  66.  
  67. function component:touch()
  68.     return false
  69. end
  70.  
  71.  
  72.  
  73. local mask = {comps = {}, backgroundColor = 32768}
  74.  
  75.  
  76.  
  77. function mask:new(o)
  78.     o = o or {}
  79.     setmetatable(o, self)
  80.     self.__index = self
  81.     local newc = {}
  82.     for I = 1, #self.comps do
  83.         newc[I] = self.comps[I]
  84.     end
  85.     o.comps = newc
  86.     return o
  87. end
  88.  
  89.  
  90.  
  91. function mask:print()
  92.     --if self.terminal[1] == nil then printError("No terminal assigend") return false end
  93.     term.setBackgroundColor(self.backgroundColor)
  94.     term.clear()
  95.     for I = 1, #self.comps do
  96.         self.comps[I]:print(self)
  97.     end
  98. end
  99.  
  100.  
  101.  
  102. function mask:touch(x, y)
  103.     for I = 1, #self.comps do
  104.         if self.comps[I]:isTouched(x, y) then
  105.             self.comps[I]:touch()
  106.         end
  107.     end
  108. end
  109.  
  110.  
  111.  
  112. --------------------------------------------------------------------------------------------------------------------------------------------
  113. -- implemented components
  114. --------------------------------------------------------------------------------------------------------------------------------------------
  115.  
  116.  
  117.  
  118. local background = visual:new()
  119. background.color = 1
  120.  
  121.  
  122.  
  123. function background:print(amask, acomp)
  124.     local fillString = ""
  125.     for I = 1, acomp.width do
  126.         fillString = fillString.." "
  127.     end
  128.     local beforeColor = term.getBackgroundColor()
  129.     term.setBackgroundColor(self.color)
  130.     for I = 1, acomp.height do
  131.         term.setCursorPos(acomp.x, acomp.y + (I - 1))
  132.         term.write(fillString)
  133.     end
  134.     term.setBackgroundColor(beforeColor)
  135. end
  136.  
  137.  
  138.  
  139. local contour = visual:new()
  140. contour.color = 128
  141.  
  142.  
  143.  
  144. function contour:print(amask, acomp)
  145.     local fillString = ""
  146.     for I = 1, acomp.width do
  147.         fillString = fillString.." "
  148.     end
  149.     local beforeColor = term.getBackgroundColor()
  150.     term.setBackgroundColor(self.color)
  151.     term.setCursorPos(acomp.x, acomp.y)
  152.     term.write(fillString)
  153.     for I = 2, (acomp.height - 1) do
  154.         term.setCursorPos(acomp.x, acomp.y + (I - 1))
  155.         term.write(" ")
  156.         term.setCursorPos((acomp.x + acomp.width) - 1, acomp.y + (I - 1))
  157.         term.write(" ")
  158.     end
  159.     term.setCursorPos(acomp.x, (acomp.y + acomp.height) - 1)
  160.     term.write(fillString)
  161.     term.setBackgroundColor(beforeColor)
  162. end
  163.  
  164.  
  165.  
  166. local label = {x = 1, y = 1, text = "", textColor = 0, backgroundColor = 0}
  167. label = visual:new(label)
  168.  
  169.  
  170.  
  171. function label:print(amask, acomp)
  172.     local tcolor = ""
  173.     local bcolor = ""
  174.  
  175.     if self.textColor == 0 then
  176.         tcolor = term.getTextColor()
  177.     else
  178.         tcolor = self.textColor
  179.     end
  180.  
  181.     if self.backgroundColor == 0 then
  182.         bcolor = term.getBackgroundColor()
  183.     else
  184.         bcolor = self.backgroundColor
  185.     end
  186.  
  187.     term.setCursorPos(acomp.x + (self.x - 1), acomp.y + (self.y - 1))
  188.     term.setTextColor(tcolor)
  189.     term.setBackgroundColor(bcolor)
  190.     term.write(self.text)
  191. end
  192.  
  193.  
  194.  
  195. local button = component:new()
  196. local bbackground = background:new()
  197. button.background = bbackground
  198. local blabel = label:new()
  199. button.label = blabel
  200. button.label.text = "button"
  201. table.insert(button.visuals, bbackground)
  202. table.insert(button.visuals, blabel)
  203.  
  204.  
  205.  
  206. terminal =
  207. {
  208.     masks =         { base = mask },
  209.     components =    { base = component, button = button },
  210.     visuals =       { base = visual, background = background , label = label, contour = contour }
  211. }
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement