Advertisement
jig487

frameDrawer

Feb 22nd, 2022 (edited)
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.83 KB | None | 0 0
  1. --Returns the valid color of a base 16 color code
  2. local function toCol(a)
  3.     local col = 2^(tonumber(a, 16))
  4.     return col
  5. end
  6. --Returns the modified string 'str' w/ the character 'added' replaced at the string index 'n'
  7. local function setStringIndex(str,added,n)
  8.     return string.sub(str,1,n-1)..added..string.sub(str,n+1)
  9. end
  10. --Returns the modified string 'str' w/ the string 'added' replaced at the string indexs 'n' through 'n+#str'
  11. local function writeString(str,added,n)
  12.     return string.sub(str,1,n-1)..added..string.sub(str,n+#added)
  13. end
  14. --Returns the modified frame 'bf' (base frame) w/ the frame 'af' (added frame) inserted at coordinates 'x,y'
  15. local function addFrame(x,y,bf,af)
  16.     local endOffset = 0
  17.  
  18.     if af.w + x > bf.w then
  19.         endOffset = af.w + x - bf.w
  20.     end
  21.    
  22.     for i = 1, af.h do
  23.         local baseStart = bf.w*(y-1)+x
  24.         local addStart  = af.w*(i-1)
  25.         local addEnd    = af.w*i-endOffset
  26.  
  27.         bf.canvas  = writeString(bf.canvas,  string.sub(af.canvas,  addStart, addEnd), baseStart)
  28.         bf.txtCol  = writeString(bf.txtCol,  string.sub(af.txtCol,  addStart, addEnd), baseStart)
  29.         bf.backCol = writeString(bf.backCol, string.sub(af.backCol, addStart, addEnd), baseStart)
  30.     end
  31.     return bf
  32. end
  33. --Returns a button with coordinates 'x,y', a name of 'name', and a stored function of 'function'
  34. local function makeButton(x,y,name,doThing)
  35.     local str = "["..name.."]"
  36.     local txtCol = ""
  37.     local backCol = ""
  38.     for i = 1, #str do
  39.         local col = "w"
  40.         if i == 1 or i == #str then
  41.             col = "1"
  42.         end
  43.         txtCol = txtCol..col
  44.         backCol = backCol.."f"
  45.     end
  46.     return {
  47.         x = x,
  48.         y = y,
  49.         h = 1,
  50.         w = #name+2,
  51.         name = name,
  52.         canvas = str,
  53.         txtCol = txtCol,
  54.         backCol = backCol,
  55.         doThing = doThing
  56.     }
  57. end
  58. --Goes through a list of buttons made with the makeButton() function and checks to see whether the
  59. --  coordinates 'x,y' overlap with any of the buttons set coordinates. Returns 'true,i' where i is
  60. --  the index it found the first overlap. Else retuns 'false,nil' if unable to find an overlap.
  61. local function checkForButtons(buttons,x,y)
  62.     for i = 1, #buttons do
  63.         local b = buttons[i]
  64.         if b.x <= x and x <= b.x+#b.name and y == b.y then
  65.             return true,i
  66.         end
  67.     end
  68.     return false,nil
  69. end
  70. --Draws the frame 'frame' at coordinates 'x,y'
  71. local function drawFrame(x,y,frame)
  72.     local canvas  = frame.canvas
  73.     local txtCol  = frame.txtCol
  74.     local backCol = frame.backCol
  75.  
  76.     for i = 1, frame.h do
  77.         local offset = frame.w*(i-1)
  78.         term.setCursorPos(x,y+i-1)
  79.  
  80.         for j = 1, frame.w do
  81.             local index = offset + j
  82.  
  83.             local txtC = txtCol:sub(index,index)
  84.             local backC = backCol:sub(index,index)
  85.             local char = canvas:sub(index,index)
  86.  
  87.             term.setTextColor( toCol( txtC ) )
  88.             term.setBackgroundColor( toCol( backC ) )
  89.             term.write( char )
  90.         end
  91.     end
  92.  
  93.     term.setTextColor(colors.white)
  94.     term.setBackgroundColor(colors.black)
  95. end
  96. --Draws the frame 'frame' at coordinates 'x,y' without individual character colors
  97. local function drawFrameFast(x,y,frame,txtCol,backCol)
  98.     local canvas  = frame.canvas
  99.  
  100.     term.setTextColor(txtCol)
  101.     term.setBackgroundColor(backCol)
  102.  
  103.     for i = 1, frame.h do
  104.         local startIndex = frame.w*(i-1)+1
  105.         local endIndex = frame.w*i
  106.         local str = canvas:sub(startIndex,endIndex)
  107.  
  108.         term.setCursorPos( x,y+i-1 )
  109.         term.write( str )
  110.     end
  111.  
  112.     term.setTextColor(colors.white)
  113.     term.setBackgroundColor(colors.black)
  114. end
  115. --Waits for a click, drag, or release mouse event then returns the data
  116. local function getMouseEvent()
  117.     local eventData = {os.pullEvent()}
  118.     while eventData[1] ~= "mouse_click" and eventData[1] ~= "mouse_drag" and eventData[1] ~= "mouse_up" do
  119.         eventData = {os.pullEvent()}
  120.     end
  121.      
  122.     return eventData[1],eventData[2],eventData[3],eventData[4]
  123. end
  124. --Returns a frame with a width and height == 'w,h'
  125. local function makeFrame(w,h)
  126.         --Create an entire horizontal row
  127.     local tempCanv    = string.rep(".",w)
  128.     local tempTxtCol  = string.rep("0",w)
  129.     local tempBackCol = string.rep("f",w)
  130.  
  131.          --Copy each horizontal row for the required height
  132.     local canvas  = string.rep(tempCanv,h)
  133.     local txtCol  = string.rep(tempTxtCol,h)
  134.     local backCol = string.rep(tempBackCol,h)
  135.  
  136.     return {
  137.         canvas = canvas,
  138.         txtCol = txtCol,
  139.         backCol = backCol,
  140.         w = w,
  141.         h = h
  142.     }
  143. end
  144.  
  145. --[[
  146. ┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
  147. │1. Color codes: │
  148. └┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
  149.  
  150. 0 = white
  151. 1 = orange
  152. 2 = magenta
  153. 3 = light blue
  154. 4 = yellow
  155. 5 = lime
  156. 6 = pink
  157. 7 = gray
  158. 8 = light gray
  159. 9 = cyan
  160. a = purple
  161. b = blue
  162. c = brown
  163. d = green
  164. e = red
  165. f = black
  166.  
  167. ┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
  168. │2. Useful string.char's │
  169. └┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
  170.  
  171. string.char()
  172.  
  173. Char 127, 129-159, 168 are block and shading characters:
  174.  
  175. 127 = transluscent grid
  176. 129-159 = block patterns
  177. 168 = horizontal colon ( : )
  178.  
  179. ┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
  180. │3. Example use: │
  181. └┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
  182.  
  183. --if fs.exists("frameDrawer.lua") then fs.delete("frameDrawer.lua") end
  184.  
  185. --shell.run("pastebin get FRyEWAxh frameDrawer")
  186.  
  187. local frm = require("frameDrawer")
  188.  
  189. local tw,th = term.getSize() --term width, term height
  190. local frame = frm.makeFrame(10,10)
  191.  
  192. term.clear()
  193.  
  194. while true do
  195.     --frm.drawFrameFast(1,1,frame,colors.white,colors.black)
  196.     frm.drawFrame(1,1,frame)
  197.     local _,type,x,y = frm.getMouseEvent()
  198.     if x <= frame.w and y <= frame.h then
  199.         local targetIndex = (y-1)*frame.w + x
  200.  
  201.         if type == 1 then
  202.             --Left click
  203.             --example:
  204.             --frame.canvas = frm.pstr(frame.canvas, "HELLO WORLD!", targetIndex)
  205.             --frame.backCol = frm.ppx(frame.backCol, "1", targetIndex)
  206.         elseif type == 2 then
  207.             --Right click
  208.             --frame.canvas = frm.pstr(frame.canvas, "            ", targetIndex)
  209.             --frame.backCol = frm.ppx(frame.backCol, "f", targetIndex)
  210.         elseif type == 3 then
  211.             --Middle click
  212.         end
  213.     end
  214. end
  215.  
  216. ]]
  217.  
  218. --Expose functions
  219. local exposed = {
  220.     makeButton = makeButton,
  221.     checkForButtons = checkForButtons,
  222.  
  223.     makeFrame = makeFrame,
  224.     drawFrame = drawFrame,
  225.     drawFrameFast = drawFrameFast,
  226.  
  227.     getMouseEvent = getMouseEvent,
  228.  
  229.     ppx = setStringIndex,
  230.     pstr = writeString,
  231. }
  232.  
  233. return exposed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement