Advertisement
Guest User

gui.lua

a guest
May 19th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.22 KB | None | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local unicode = require("unicode")
  4. local event = require("event")
  5. local term = require("term")
  6. local os = require("os")
  7. local mk = require("multikey")
  8.  
  9. local null = "n"
  10. local WIDTH, HEIGHT = gpu.maxResolution()
  11.  
  12. local lines = {
  13.   lightDouble = {
  14.     hor = unicode.char(0x2550),
  15.     vert = unicode.char(0x2551),
  16.     topLeft = unicode.char(0x2554),
  17.     topRight = unicode.char(0x2557),
  18.     bottomRight = unicode.char(0x255D),
  19.     bottomLeft = unicode.char(0x255A)
  20.   },
  21.   light = {
  22.     hor = unicode.char(0x2500),
  23.     vert = unicode.char(0x2502),
  24.     topLeft = unicode.char(0x250C),
  25.     topRight = unicode.char(0x2510),
  26.     bottomRight = unicode.char(0x2518),
  27.     bottomLeft = unicode.char(0x2514)
  28.   },
  29.   lightDotted = {
  30.     hor = unicode.char(0x2504),
  31.     vert = unicode.char(0x2506),
  32.     topLeft = unicode.char(0x250C),
  33.     topRight = unicode.char(0x2510),
  34.     bottomRight = unicode.char(0x2518),
  35.     bottomLeft = unicode.char(0x2514)
  36.   },
  37.   thin = {
  38.     hor = unicode.char(0x2501),
  39.     vert = unicode.char(0x2503),
  40.     topLeft = unicode.char(0x250F),
  41.     topRight = unicode.char(0x2513),
  42.     bottomRight = unicode.char(0x251B),
  43.     bottomLeft = unicode.char(0x2517)
  44.   },
  45.   thinDotted = {
  46.     hor = unicode.char(0x2505),
  47.     vert = unicode.char(0x2507),
  48.     topLeft = unicode.char(0x250F),
  49.     topRight = unicode.char(0x2510),
  50.     bottomRight = unicode.char(0x251B),
  51.     bottomLeft = unicode.char(0x2517)
  52.   },
  53.   thinAlternate = {
  54.     top = unicode.char(0x2594),
  55.     right = unicode.char(0x2595),
  56.     bottom = unicode.char(0x2581),
  57.     left = unicode.char(0x258F)
  58.   },
  59.   medium = {
  60.     top = unicode.char(0x2580),
  61.     right = unicode.char(0x2590),
  62.     bottom = unicode.char(0x2584),
  63.     left = unicode.char(0x258C),
  64.     topLeft = unicode.char(0x2597),
  65.     topRight = unicode.char(0x2596),
  66.     bottomRight = unicode.char(0x2598),
  67.     bottomLeft = unicode.char(0x259D)
  68.   }
  69. }
  70.  
  71. local intersections = {}
  72.  
  73. -- Clockwise: Top, Right, Bottom, Left
  74. -- LIGHT / THIN 2 LINES
  75. mk.put(intersections, null, "light", null, "light", unicode.char(0x2500))
  76. mk.put(intersections, null, "thin", null, "thin", unicode.char(0x2501))
  77. mk.put(intersections, "light", null, "light", null, unicode.char(0x2502))
  78. mk.put(intersections, "thin", null, "thin", null, unicode.char(0x2503))
  79. mk.put(intersections, null, "light", "light", null, unicode.char(0x250C))
  80. mk.put(intersections, null, "thin", "light", null, unicode.char(0x250D))
  81. mk.put(intersections, null, "light", "thin", null, unicode.char(0x250E))
  82. mk.put(intersections, null, "thin", "thin", null, unicode.char(0x250F))
  83. mk.put(intersections, null, null, "light", "light", unicode.char(0x2510))
  84. mk.put(intersections, null, null, "light", "thin", unicode.char(0x2511))
  85. mk.put(intersections, null, null, "thin", "light", unicode.char(0x2512))
  86. mk.put(intersections, null, null, "thin", "thin", unicode.char(0x2513))
  87. mk.put(intersections, "light", "light", null, null, unicode.char(0x2514))
  88. mk.put(intersections, "light", "thin", null, null, unicode.char(0x2515))
  89. mk.put(intersections, "thin", "light", null, null, unicode.char(0x2516))
  90. mk.put(intersections, "thin", "thin", null, null, unicode.char(0x2517))
  91. mk.put(intersections, "light", null, null, "light", unicode.char(0x2518))
  92. mk.put(intersections, "light", null, null, "thin", unicode.char(0x2519))
  93. mk.put(intersections, "thin", null, null, "light", unicode.char(0x251A))
  94. mk.put(intersections, "thin", null, null, "thin", unicode.char(0x251B))
  95.  
  96. -- LIGHT/DOUBLE 2 LINES
  97. mk.put(intersections, null, "double", null, "double", unicode.char(0x2550))
  98. mk.put(intersections, "double", null, "double", null, unicode.char(0x2551))
  99. mk.put(intersections, null, "double", "light", "null", unicode.char(0x2552))
  100. mk.put(intersections, null, "light", "double", null, unicode.char(0x2553))
  101. mk.put(intersections, null, "double", "double", null, unicode.char(0x2554))
  102. mk.put(intersections, null, null, "light", "double", unicode.char(0x2555))
  103. mk.put(intersections, null, null, "double", "light", unicode.char(0x2556))
  104. mk.put(intersections, null, null, "double", "double", unicode.char(0x2557))
  105. mk.put(intersections, "light", "double", null, null, unicode.char(0x2558))
  106. mk.put(intersections, "double", "light", null, null, unicode.char(0x2559))
  107. mk.put(intersections, "double", "double", null, null, unicode.char(0x255A))
  108.  
  109. mk.put(intersections, "light", null, null, "double", unicode.char(0x255B))
  110. mk.put(intersections, "double", null, null, "light", unicode.char(0x255C))
  111. mk.put(intersections, "double", null, null, "double", unicode.char(0x255D))
  112.  
  113. -- LIGHT / THIN 3 LINES
  114. mk.put(intersections, "light", "light", "light", null, unicode.char(0x251C))
  115. mk.put(intersections, "light", "thin", "light", null, unicode.char(0x251D))
  116. mk.put(intersections, "thin", "light", "light", null, unicode.char(0x251E))
  117. mk.put(intersections, "light", "light", "thin", null, unicode.char(0x251F))
  118. mk.put(intersections, "thin", "light", "thin", null, unicode.char(0x2520))
  119. mk.put(intersections, "thin", "thin", "light", null, unicode.char(0x2521))
  120. mk.put(intersections, "light", "thin", "thin", null, unicode.char(0x2522))
  121. mk.put(intersections, "thin", "thin", "thin", null, unicode.char(0x2523))
  122. mk.put(intersections, "light", null, "light", "light", unicode.char(0x2524))
  123. mk.put(intersections, "light", null, "light", "thin", unicode.char(0x2525))
  124. mk.put(intersections, "thin", null, "light", "light", unicode.char(0x2526))
  125. mk.put(intersections, "light", null, "thin", "light", unicode.char(0x2527))
  126. mk.put(intersections, "thin", null, "thin", "light", unicode.char(0x2528))
  127. mk.put(intersections, "thin", null, "light", "thin", unicode.char(0x2529))
  128. mk.put(intersections, "light", null, "thin", "thin", unicode.char(0x252A))
  129. mk.put(intersections, "thin", null, "thin", "thin", unicode.char(0x252B))
  130. mk.put(intersections, null, "light", "light", "light", unicode.char(0x252C))
  131. mk.put(intersections, null, "light", "light", "thin", unicode.char(0x252D))
  132. mk.put(intersections, null, "thin", "light", "light", unicode.char(0x252E))
  133. mk.put(intersections, null, "thin", "light", "thin", unicode.char(0x252F))
  134. mk.put(intersections, null, "light", "thin", "light", unicode.char(0x2530))
  135. mk.put(intersections, null, "light", "thin", "thin", unicode.char(0x2531))
  136. mk.put(intersections, null, "thin", "thin", "light", unicode.char(0x2532))
  137. mk.put(intersections, null, "thin", "thin", "thin", unicode.char(0x2533))
  138. mk.put(intersections, "light", "light", null, "light", unicode.char(0x2534))
  139. mk.put(intersections, "light", "light", null, "thin", unicode.char(0x2535))
  140. mk.put(intersections, "light", "thin", null, "light", unicode.char(0x2356))
  141. mk.put(intersections, "light", "thin", null, "thin", unicode.char(0x2357))
  142. mk.put(intersections, "thin", "light", null, "light", unicode.char(0x2358))
  143. mk.put(intersections, "thin", "light", null, "thin", unicode.char(0x2359))
  144. mk.put(intersections, "thin", "thin", null, "light", unicode.char(0x235A))
  145. mk.put(intersections, "thin", "thin", null, "thin", unicode.char(0x235B))
  146.  
  147. -- LIGHT / DOUBLE 3 LINES
  148. mk.put(intersections, "light", "double", "light", null, unicode.char(0x255E))
  149. mk.put(intersections, "double", "light", "double", null, unicode.char(0x255F))
  150. mk.put(intersections, "double", "double", "double", null, unicode.char(0x2560))
  151. mk.put(intersections, "light", null, "light", "double", unicode.char(0x2561))
  152. mk.put(intersections, "double", null, "double", "light", unicode.char(0x2562))
  153. mk.put(intersections, "double", null, "double", "double", unicode.char(0x2563))
  154. mk.put(intersections, null, "double", "light", "double", unicode.char(0x2564))
  155. mk.put(intersections, null, "light", "double", "light", unicode.char(0x2565))
  156. mk.put(intersections, null, "double", "double", "double", unicode.char(0x2566))
  157. mk.put(intersections, "light", "double", null, "double", unicode.char(0x2567))
  158. mk.put(intersections, "double", "light", null, "light", unicode.char(0x2568))
  159. mk.put(intersections, "double", "double", null, "double", unicode.char(0x2569))
  160.  
  161. -- LIGHT / THIN 4 LINES
  162. mk.put(intersections, "light", "light", "light", "light", unicode.char(0x253C))
  163. mk.put(intersections, "light", "light", "light", "thin", unicode.char(0x253D))
  164. mk.put(intersections, "light", "thin", "light", "light", unicode.char(0x253E))
  165. mk.put(intersections, "light", "thin", "light", "thin", unicode.char(0x253F))
  166. mk.put(intersections, "thin", "light", "light", "light", unicode.char(0x2540))
  167. mk.put(intersections, "thin", "thin", "light", "thin", unicode.char(0x2541))
  168. mk.put(intersections, "thin", "light", "thin", "light", unicode.char(0x2542))
  169. mk.put(intersections, "thin", "light", "light", "thin", unicode.char(0x2543))
  170. mk.put(intersections, "thin", "thin", "light", "light", unicode.char(0x2544))
  171. mk.put(intersections, "light", "light", "thin", "thin", unicode.char(0x2545))
  172. mk.put(intersections, "light", "thin", "thin", "light", unicode.char(0x2546))
  173. mk.put(intersections, "thin", "thin", "light", "thin", unicode.char(0x2547))
  174. mk.put(intersections, "light", "thin", "thin", "thin", unicode.char(0x2548))
  175. mk.put(intersections, "thin", "light", "thin", "thin", unicode.char(0x2549))
  176. mk.put(intersections, "thin", "thin", "thin", "light", unicode.char(0x254A))
  177. mk.put(intersections, "thin", "thin", "thin", "thin", unicode.char(0x254B))
  178.  
  179. -- LIGHT / DOUBLE 4 LINES
  180. mk.put(intersections, "light", "double", "light", "double", unicode.char(0x256A))
  181. mk.put(intersections, "double", "light", "double", "light", unicode.char(0x256B))
  182. mk.put(intersections, "double", "double", "double", "double", unicode.char(0x256C))
  183.  
  184. points = {}
  185. function generateBoxPoints(boxes, boxDebug)
  186.   boxDebug = boxDebug or false
  187.   --points= {}
  188.  
  189.   for i,box in ipairs(boxes) do
  190.  
  191.     for i = box.startX, box.endX do
  192.       mk.put(points, i, box.startY, {lineType = box.lineType})
  193.       mk.put(points, i, box.endY, {lineType = box.lineType})
  194.     end
  195.     for i = box.startY, box.endY do
  196.       mk.put(points, box.startX, i, {lineType = box.lineType})
  197.       mk.put(points, box.endX, i, {lineType = box.lineType})
  198.     end
  199.  
  200.   end
  201.  
  202.   if boxDebug == true then
  203.     for _,x,y,point in mk.tuples(points) do
  204.       print(x,y,point)
  205.     end
  206.   end
  207.  
  208.   for _,x,y,point in mk.tuples(points) do
  209.  
  210.     local top = mk.get(points, x, y - 1) or null
  211.     local right = mk.get(points, x + 1, y) or null
  212.     local bottom = mk.get(points, x, y + 1) or null
  213.     local left = mk.get(points, x - 1, y) or null
  214.  
  215.     top = top.lineType or null
  216.     right = right.lineType or null
  217.     bottom = bottom.lineType or null
  218.     left = left.lineType or null
  219.  
  220.     local expChar = mk.get(intersections, top, right, bottom, left)
  221.     if expChar == nil then
  222.       if top ~= null or bottom ~= null then
  223.         expChar = mk.get(intersections, point.lineType, null, point.lineType, null)
  224.       else
  225.         expChar = mk.get(intersections, null, point.lineType, null, point.lineType)
  226.       end
  227.      end
  228.     mk.put(points, x, y, {lineType = point.lineType, char = expChar})
  229.   end
  230.  
  231.   --return points
  232. end
  233.  
  234. function drawBoxes(points)
  235.   for _,x,y,point in mk.tuples(points) do
  236.     gpu.set(x, y, point.char or "n")
  237.   end
  238. end
  239.  
  240. local tabs = {}
  241. function drawWindow(w, h, numTabs, tabWidth, fill)
  242.   local ranges = {
  243.     {2, 1, w - (tabWidth + 1), 1},
  244.     {w - tabWidth, 2, w - (tabWidth + 1), h-1},
  245.     {2, h, w - (tabWidth + 1), h},
  246.     {1, 2, 1, h - 1}
  247.   }
  248.   for k,v in ipairs(ranges) do
  249.     if k == 1 then
  250.       for i = v[1], v[3] do
  251.         gpu.set(i, v[2], lines.thinAlternate.bottom)
  252.       end
  253.     elseif k == 2 then
  254.       for i = v[2], v[4] do
  255.         gpu.set(v[1], i, lines.thinAlternate.left)
  256.       end
  257.     elseif k == 3 then
  258.       for i = v[1], v[3] do
  259.         gpu.set(i, v[2], lines.thinAlternate.top)
  260.       end
  261.     elseif k == 4 then
  262.       for i = v[2], v[4] do
  263.         gpu.set(v[1], i, lines.thinAlternate.right)
  264.       end
  265.     end
  266.   end
  267.  
  268.   local tabHeight = math.floor(h / numTabs)
  269.   for tab = 1, numTabs do
  270.     tabs[tab] = {
  271.       --active = false,
  272.       w = tabWidth,
  273.       h = tabHeight,
  274.       coords = {w - tabWidth + 1, ((tab * tabHeight) - tabHeight) + 1, w - 1, tab * tabHeight},
  275.       borderRanges = {
  276.         {w - tabWidth + 1, ((tab * tabHeight) - tabHeight) + 1, w - 1, ((tab * tabHeight) - tabHeight) + 1},
  277.         {w, ((tab * tabHeight) - tabHeight) + 2, w, tab * tabHeight - 1},
  278.         {w - tabWidth + 1, tab * tabHeight, w - 1, tab * tabHeight},
  279.         {w - tabWidth, ((tab * tabHeight) - tabHeight) + 2, w - tabWidth, tab * tabHeight - 1}
  280.       }
  281.     }
  282.  
  283.     for k,v in ipairs(tabs[tab].borderRanges) do
  284.       if k == 1 then
  285.         for i = v[1], v[3] do
  286.           gpu.set(i, v[2], lines.thinAlternate.bottom)
  287.         end
  288.       elseif k == 2 then
  289.         for i = v[2], v[4] do
  290.           gpu.set(v[1], i, lines.thinAlternate.left)
  291.         end
  292.       elseif k == 3 then
  293.         for i = v[1], v[3] do
  294.           gpu.set(i, v[2], lines.thinAlternate.top)
  295.         end
  296.       elseif k == 4 then
  297.         --[[for i = v[2], v[4] do
  298.           gpu.set(v[1], i, lines.thinAlternate.right)
  299.         end]]--
  300.       end
  301.     end
  302.   end
  303.  
  304.   gpu.setBackground(fill)
  305.   gpu.fill(2, 2, w - 20, h - 2, " ")
  306. end
  307.  
  308. local activeTab = 0
  309. local wasActiveTab = 0
  310. function tabClick(x, y)
  311.   for k,v in ipairs(tabs) do
  312.     if x > v.coords[1] and x < v.coords[3] and y > v.coords[2] and y < v.coords[4] then
  313.       if activeTab ~= 0 then
  314.         wasActiveTab = activeTab
  315.       end
  316.       activeTab = k
  317.     end
  318.   end
  319. end
  320.  
  321. function paint(fill)
  322.   if activeTab ~= 0 then
  323.     gpu.setBackground(fill)
  324.     gpu.fill(tabs[activeTab].coords[1], tabs[activeTab].coords[2] + 1, tabs[activeTab].w - 1, tabs[activeTab].h - 2, " ")
  325.     gpu.setForeground(fill)
  326.     gpu.fill(tabs[activeTab].coords[1] - 1, tabs[activeTab].coords[2] + 1, 1, tabs[activeTab].h - 2, lines.thinAlternate.left)
  327.     gpu.setForeground(0xFFFFFF)
  328.     gpu.setBackground(0x000000)
  329.   end
  330.   if wasActiveTab ~= 0 then
  331.     gpu.fill(tabs[wasActiveTab].coords[1], tabs[wasActiveTab].coords[2] + 1, tabs[wasActiveTab].w - 1, tabs[wasActiveTab].h - 2, " ")
  332.     wasActiveTab = 0
  333.   end
  334. end
  335.  
  336. -- In boxes with different styles that share sides, the boxes that are added later will take priority over boxes added earlier.
  337. generateBoxPoints({
  338.   {startX = 1, startY = 1, endX = 80, endY = 50, lineType = "light"},
  339.   {startX = 80, startY = 1, endX = 160, endY = 45, lineType = "thin"}
  340. })
  341.  
  342. --mergedPoints = mergeBoxes(returnedBoxPoints)
  343. term.clear()
  344.  
  345. while true do
  346.   gpu.setBackground(0x000000)
  347.   ev, e1, e2, e3 = event.pull(1)
  348.   if ev == "touch" then
  349.     tabClick(e2, e3)
  350.     --[[term.clear()
  351.     os.exit()]]--
  352.   end
  353.   --drawBoxes(points)
  354.   paint(0x00B6FF)
  355.   drawWindow(WIDTH, HEIGHT, 4, 18, 0x00B6FF)
  356. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement