Advertisement
skypop

ui

Jul 6th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.62 KB | None | 0 0
  1. -----------------------
  2. -- API ui
  3. -- by SukaiPoppuGo
  4. --
  5.  
  6.  
  7. -----------------------
  8. -- initTerm
  9. -- Return current terminal as default output
  10. -- param:   object  terminal    (optional)
  11. -- return:  object  terminal
  12. local function initTerm(_term)
  13.     return _term or term.current()
  14. end
  15.  
  16. -----------------------
  17. -- screenCutString
  18. -- Keep last part of a string (screen constraint)
  19. -- param:   string  str Full line
  20. -- param:   object  terminal    (optional)
  21. -- return:  string      End of line
  22. function screenCutString(str, _term)
  23.     _term = initTerm(_term)
  24.     local screenW, screenH = _term.getSize()
  25.     return string.len(str) >= screenW - 1 and string.sub(str, string.len(str) - screenW +2) or str
  26. end
  27.  
  28. -----------------------
  29. -- output
  30. -- Write on chosen line, (and restore cursor)
  31. -- param:   string  str Text to write
  32. -- param:   number  ln  Chosen line
  33. -- param:   object  terminal    (optional)
  34. function output(str, ln, _term)
  35.     _term = initTerm(_term)
  36.     local x,y = _term.getCursorPos()
  37.     _term.setCursorPos(1, ln)
  38.     _term.clearLine()
  39.     _term.write(str)
  40.     _term.setCursorPos(x,y)
  41. end
  42.  
  43. -----------------------
  44. -- outputEndScreen
  45. -- Write on the last line, (and restore cursor)
  46. -- param:   string  str Text to write
  47. -- param:   object  terminal    (optional)
  48. function outputEndScreen(str, _term)
  49.     _term = initTerm(_term)
  50.     local screenW, screenH = _term.getSize()
  51.     output(string.rep(" ", screenW - string.len(str))..str, screenH, _term)
  52. end
  53.  
  54. -----------------------
  55. -- clearLines
  56. -- Clear multiple line, (and restore cursor)
  57. -- param:   number  startY  First line
  58. -- param:   number  endY    Last line
  59. -- param:   object  terminal    (optional)
  60. function clearLines(startY, endY, _term)
  61.     _term = initTerm(_term)
  62.     local x,y = _term.getCursorPos()
  63.     for i=startY,endY,1 do
  64.         _term.setCursorPos(1,i)
  65.         _term.clearLine()
  66.     end
  67.     _term.setCursorPos(x,y)
  68. end
  69.  
  70. -----------------------
  71. -- blit
  72. -- Custom blit function, lenght up to screen width
  73. -- -- do not require each three strings to be of the same length
  74. -- -- text_colors and background_colour could be repeated as patterns
  75. -- param:   string  text
  76. -- param:   string  text_colors
  77. -- param:   string  background_colour
  78. -- param:   object  terminal    (optional)
  79. function blit(text, textColor, backgroundColor, _term)
  80.     _term = initTerm(_term)
  81.     local screenW, screenH = _term.getSize()
  82.     text = string.sub(text..string.rep(" ", screenW), 0, screenW)
  83.     textColor = string.sub(string.rep(textColor, screenW), 0, screenW)
  84.     backgroundColor = string.sub(string.rep(backgroundColor, screenW), 0, screenW)
  85.     _term.blit(text, textColor, backgroundColor)
  86. end
  87.  
  88. -----------------------
  89. -- cblit (Constraint blit)
  90. -- Custom blit function, lenght up to max width of each 3 strings
  91. -- -- do not require each three strings to be of the same length
  92. -- -- text_colors and background_colour could be repeated as patterns
  93. -- param:   string  text
  94. -- param:   string  text_colors
  95. -- param:   string  background_colour
  96. -- param:   object  terminal    (optional)
  97. function cblit(text, textColor, backgroundColor, _term)
  98.     _term = initTerm(_term)
  99.     local screenW, screenH = _term.getSize()
  100.     local width = math.min(screenW, math.max(string.len(text), string.len(textColor), string.len(backgroundColor)))
  101.     text = string.sub(text..string.rep(" ", width), 0, width)
  102.     textColor = string.sub(string.rep(textColor, width), 0, width)
  103.     backgroundColor = string.sub(string.rep(backgroundColor, width), 0, width)
  104.     _term.blit(text, textColor, backgroundColor)
  105. end
  106.  
  107. -----------------------
  108. -- btn
  109. -- Enhanced button with a background colors
  110. -- param:   string  str         Text display inside button
  111. -- param:   boolean enabled     Change colors, as clicked effect
  112. -- param:   object  terminal    (optional)
  113. function btn(str, enabled, _term)
  114.     _term = initTerm(_term)
  115.     local C, L = string.char(149), string.len(str)
  116.     local SF, S8, S0 = string.rep("f", L), string.rep("8", L), string.rep("0", L)
  117.     if enabled then
  118.         _term.blit(C..str..C, "f"..SF.."8", "8"..S8.."f")
  119.     else
  120.         _term.blit(" "..str.." ", "0"..S0.."0", "f"..SF.."f")
  121.     end
  122. end
  123.  
  124. -----------------------
  125. -- waitPressAnyKey
  126. -- Handle complete "any" key event, from key press to key up
  127. function waitPressAnyKey()
  128.     os.pullEvent("key")
  129.     os.pullEvent("key_up")
  130. end
  131.  
  132. -----------------------
  133. -- box
  134. -- Draw boxes with outline
  135. -- param:   mixed   msg         Box centents, could be a string, or table of strings for multiple lines
  136. -- param:   number  minWidth    Extends box width (optional)
  137. -- param:   number  title       Add title on top bar
  138. -- param:   object  terminal    (optional)
  139. -- param:   boolean _debug      Debug trace
  140. function box(msg, minWidth, title, _term, _debug)
  141.     _term = initTerm(_term)
  142.     local screenW, screenH = _term.getSize()
  143.     _debug = _debug or false
  144.     minWidth = minWidth or 0
  145.     msg = (type(msg) == "string") and {msg} or msg
  146.     local x,y = _term.getCursorPos()
  147.     local txtColor, bgColor = "0", "7"
  148.     local maxLen = minWidth
  149.     for k,v in pairs(msg) do
  150.         maxLen = (maxLen < string.len(tostring(v))) and string.len(tostring(v)) or maxLen
  151.     end
  152.     maxLen = math.min(maxLen, screenW - 2)
  153.     local pad = string.rep(" ", screenW)
  154.     local borderColor = "8"
  155.     local txtColorLine, bgColorLine = string.rep(txtColor, maxLen), string.rep(bgColor, maxLen)
  156.     _term.blit(
  157.         string.char(151)..string.rep(string.char(131), maxLen)..string.char(148),
  158.         string.rep(borderColor, maxLen + 1)..bgColor,
  159.         bgColor..bgColorLine..borderColor
  160.     )
  161.     for k,v in pairs(msg) do
  162.         _term.setCursorPos(x, y + k)
  163.         local line1 = string.char(149)..string.sub(tostring(v)..pad, 0, maxLen)..string.char(149)
  164.         local line2 = borderColor..txtColorLine..bgColor
  165.         local line3 = bgColor..bgColorLine..borderColor
  166.         if _debug then
  167.             print("line1="..string.len(line1))
  168.             print("line2="..string.len(line2))
  169.             print("line3="..string.len(line3))
  170.             sleep(.1)
  171.             os.pullEvent("none")
  172.         end
  173.         _term.blit( line1, line2, line3)
  174.     end
  175.     _term.setCursorPos(x, y + #msg + 1)
  176.     _term.blit(
  177.         string.char(138)..string.rep(string.char(143), maxLen)..string.char(133),
  178.         bgColor..bgColorLine..bgColor,
  179.         string.rep(borderColor, maxLen + 2)
  180.     )
  181.     if title then
  182.         _term.setCursorPos(x, y)
  183.         _term.blit(
  184.             string.sub(title, 0, maxLen + 2),
  185.             string.sub(string.rep(txtColor, #title), 0, maxLen + 2),
  186.             string.sub(string.rep(bgColor, #title), 0, maxLen + 2)
  187.         )
  188.     end
  189.     _term.setCursorPos(x, y + #msg + 2)
  190. end
  191.  
  192. -----------------------
  193. -- inputFilename
  194. -- Act as read() input for filename
  195. -- -- Prevent overwriting, ask to confirm
  196. -- param:   string  msg         Message prefixe to input
  197. -- param:   number  line        Line where display the input, requires a free line below for warnings
  198. -- param:   string  filename    Default filename (optional)
  199. -- param:   object  terminal    (optional)
  200. function inputFilename(msg, line, filename, _term)
  201.     _term = initTerm(_term)
  202.     msg = msg or ""
  203.     filename = filename or ""
  204.     while true do
  205.         output(msg..filename, line, _term)
  206.         _term.setCursorPos(string.len(msg..filename) + 1, line)
  207.         _term.setCursorBlink(true)
  208.         local e, p = os.pullEvent()
  209.         if e == "key" and p == keys.backspace then
  210.             filename = string.sub(filename, 0, math.max(0, string.len(filename) - 1))
  211.         elseif e == "key_up" and p == keys.enter and string.len(filename) > 0 then
  212.             _term.setCursorBlink(false)
  213.             if fs.exists(filename) then
  214.                 output("File already exists. Overwrite ? Y/N", line+1, _term)
  215.                 while true do
  216.                     local e2, p2 = os.pullEvent("key")
  217.                     if p2 == keys.y then
  218.                         return filename
  219.                     elseif p2 == keys.n then
  220.                         output("", line+1, _term)
  221.                         break
  222.                     end
  223.                 end
  224.             else
  225.                 return filename
  226.             end
  227.         elseif e == "char" then
  228.             filename = filename..p
  229.         end
  230.     end
  231. end
  232.  
  233. -----------------------
  234. -- drawTurtleInventory
  235. -- Display a grid of 16 boxes, shape turtle inventory slots (4x4)
  236. -- -- Display a selected slot regardless the current selected slot of turtle
  237. -- -- (UI purpose, up to you to match the two if it's the desired result)
  238. -- -- Enhance non-empty slots
  239. -- param:   number  selection   Enhance one slot (refer to slot number)
  240. -- param:   object  terminal    (optional)
  241. function drawTurtleInventory(selection, _term)
  242.     _term = initTerm(_term)
  243.     selection = selection or 0
  244.     local padX, padY = 2, 3
  245.     local txtLine, txtColor, bgColor = "","","ffffff"
  246.     for i = 0, 15 do
  247.         local sLen = (i % 2 == 0) and 1 or 2
  248.         local sBox = (sLen == 1) and string.char(143) or string.char(138)..string.char(133)
  249.         txtLine = txtLine..sBox
  250.         local boxColor = (turtle.getItemCount(i + 1) > 0) and "8" or "7"
  251.         boxColor = (i == selection) and "0" or boxColor
  252.         txtColor = txtColor..string.rep(boxColor, sLen)
  253.         if i%4 == 3 then
  254.             _term.setCursorPos(padX, padY + math.floor(i / 4))
  255.             _term.blit(txtLine, txtColor, bgColor)
  256.             txtLine, txtColor, bgColor = "","","ffffff"
  257.         end
  258.     end
  259.     _term.setTextColor(colors.white)
  260.     _term.setBackgroundColor(colors.black)
  261. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement