Advertisement
jig487

engineDrawer

Mar 22nd, 2022 (edited)
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.49 KB | None | 0 0
  1. local string_char       = string.char
  2. local string_sub        = string.sub
  3. local term_setTextColor = term.setTextColor
  4. local term_write        = term.write
  5. local term_setCursorPos = term.setCursorPos
  6. local term_setBackgroundColor = term.setBackgroundColor
  7.  
  8. --local db = require("debugging")
  9.  
  10. --Returns the valid color of a base 16 color code
  11. local function toCol(a)
  12.     local col = 2^(tonumber(a, 16))
  13.     return col
  14. end
  15. --Returns the modified string 'str' w/ the character 'added' replaced at the string index 'n'
  16. local function setStrIndex(str,added,n)
  17.     return string_sub(str,1,n-1)..added..string_sub(str,n+1)
  18. end
  19. --Returns the modified string 'str' w/ the string 'added' replaced at the string indexs 'n' through 'n+#str'
  20. local function insertString(str,added,n)
  21.     return string_sub(str,1,n-1)..added..string_sub(str,n+#added)
  22. end
  23. --Returns the utf8 drawing character equivalent of 'str'
  24. --  'str' is any combination of 6 0's and 1's, each representing a block in a drawing character
  25. --   the first representing the top left block, the second the right, the third the middle left, and so on down the character
  26. --   props to JackMackWindows
  27. local function toChar(str)
  28.     local n = 128
  29.     for i = 0, 4 do
  30.         n = n + tonumber( string_sub(str,i+1,i+1) )*2^i
  31.     end
  32.     if string_sub(str,6, 6) == "1" then
  33.         return bit32.band(bit32.bnot(n), 0x1F) + 128, true
  34.     else
  35.         return n, false
  36.     end
  37. end
  38. --Waits for a click, drag, or release mouse event then returns the data
  39. local function getMouseEvent()
  40.     local eventData = {os.pullEvent()}
  41.     while eventData[1] ~= "mouse_click" and eventData[1] ~= "mouse_drag" and eventData[1] ~= "mouse_up" do
  42.         eventData = {os.pullEvent()}
  43.     end
  44.      
  45.     return eventData[1],eventData[2],eventData[3],eventData[4]
  46. end
  47. --Returns a frame with a width and height == 'w,h'
  48. local function makeFrame(w,h)
  49.         --Make an entire horizontal row
  50.     local tempCanv    = string.rep("000100",w)
  51.     local tempTxtCol  = string.rep("0",w)
  52.     local tempBackCol = string.rep("f",w)
  53.     --db.txt(1,1,"Debug: w="..w..", h="..h,colors.red,colors.white)
  54.     --db.txt(1,2,"Debug: "..#tempCanv..", "..#tempTxtCol..", "..#tempBackCol,colors.red,colors.white)
  55.  
  56.         --Copy each horizontal row for the required height
  57.     local canvas  = string.rep(tempCanv,h)
  58.     local txtCol  = string.rep(tempTxtCol,h)
  59.     local backCol = string.rep(tempBackCol,h)
  60.  
  61.     --db.debug(1,3,#canvas..", "..#txtCol..", "..#backCol)
  62.  
  63.  
  64.     return {
  65.         canvas = canvas,
  66.         txtCol = txtCol,
  67.         backCol = backCol,
  68.         w = w,
  69.         h = h
  70.     }
  71. end
  72. --Draws the frame 'frame' at coordinates 'x,y'
  73. local function drawFrame(x,y,frame)
  74.     local canvas  = frame.canvas
  75.     local txtCol  = frame.txtCol
  76.     local backCol = frame.backCol
  77.  
  78.     for i = 1, frame.h do
  79.         local offset = frame.w*(i-1)
  80.         term_setCursorPos(x,y+i-1)
  81.  
  82.         for j = 1, frame.w do
  83.             local index = offset + j
  84.             local charStrIndex = index*6
  85.  
  86.             local txtC  = string_sub(txtCol,index,index)
  87.             local backC = string_sub(backCol,index,index)
  88.             local charStr  = string_sub(canvas,charStrIndex-5,charStrIndex)
  89.  
  90.             local char,swap = toChar(charStr)
  91.  
  92.             if swap then
  93.                 txtC,backC = backC,txtC
  94.             end
  95.  
  96.             term_setTextColor( toCol( txtC ) )
  97.             term_setBackgroundColor( toCol( backC ) )
  98.             term_write( string_char( char ) )
  99.         end
  100.     end
  101.  
  102.     term_setTextColor(colors.white)
  103.     term_setBackgroundColor(colors.black)
  104. end
  105.  
  106. --[[
  107. ┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
  108. │1. Color codes: │
  109. └┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
  110.  
  111. 0 = white
  112. 1 = orange
  113. 2 = magenta
  114. 3 = light blue
  115. 4 = yellow
  116. 5 = lime
  117. 6 = pink
  118. 7 = gray
  119. 8 = light gray
  120. 9 = cyan
  121. a = purple
  122. b = blue
  123. c = brown
  124. d = green
  125. e = red
  126. f = black
  127.  
  128. ┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
  129. │2. Useful string.char's │
  130. └┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
  131.  
  132. string.char()
  133.  
  134. Char 127, 129-159, 168 are block and shading characters:
  135.  
  136. 127 = transluscent grid
  137. 129-159 = block patterns
  138. 168 = horizontal colon ( : )
  139.  
  140. ┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
  141. │3. Example use: │
  142. └┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
  143.  
  144. local frm = require("engineDrawer")
  145.  
  146. local frame = frm.makeFrame(10,10)
  147.  
  148. term.clear()
  149.  
  150. while true do
  151.     frm.drawFrame(1,1,frame)
  152.     local _,type,x,y = frm.getMouseEvent()
  153.     if x <= frame.w and y <= frame.h then
  154.         local targetIndex = (y-1)*frame.w + x
  155.         local canvasIndex = targetIndex*6-5
  156.  
  157.         if type == 1 then
  158.             --Left click
  159.                 --example:
  160.             --frame.canvas =  frm.insertString(frame.canvas, "110011", canvasIndex)
  161.             --frame.backCol = frm.setStrIndex(frame.backCol, "1", targetIndex)
  162.         elseif type == 2 then
  163.             --Right click
  164.                 --example:
  165.             --frame.canvas =  frm.insertString(frame.canvas, "001100", canvasIndex)
  166.             --frame.backCol = frm.setStrIndex(frame.backCol, "f", targetIndex)
  167.         elseif type == 3 then
  168.             --Middle click
  169.         end
  170.     end
  171. end
  172.  
  173. ]]
  174.  
  175. --Expose functions
  176. return {
  177.     makeFrame = makeFrame,
  178.     drawFrame = drawFrame,
  179.  
  180.     getMouseEvent = getMouseEvent,
  181.  
  182.     setStrIndex = setStrIndex,
  183.     insertString = insertString,
  184.     toChar = toChar,
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement