Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local string_char = string.char
- local string_sub = string.sub
- local term_setTextColor = term.setTextColor
- local term_write = term.write
- local term_setCursorPos = term.setCursorPos
- local term_setBackgroundColor = term.setBackgroundColor
- --local db = require("debugging")
- --Returns the valid color of a base 16 color code
- local function toCol(a)
- local col = 2^(tonumber(a, 16))
- return col
- end
- --Returns the modified string 'str' w/ the character 'added' replaced at the string index 'n'
- local function setStrIndex(str,added,n)
- return string_sub(str,1,n-1)..added..string_sub(str,n+1)
- end
- --Returns the modified string 'str' w/ the string 'added' replaced at the string indexs 'n' through 'n+#str'
- local function insertString(str,added,n)
- return string_sub(str,1,n-1)..added..string_sub(str,n+#added)
- end
- --Returns the utf8 drawing character equivalent of 'str'
- -- 'str' is any combination of 6 0's and 1's, each representing a block in a drawing character
- -- the first representing the top left block, the second the right, the third the middle left, and so on down the character
- -- props to JackMackWindows
- local function toChar(str)
- local n = 128
- for i = 0, 4 do
- n = n + tonumber( string_sub(str,i+1,i+1) )*2^i
- end
- if string_sub(str,6, 6) == "1" then
- return bit32.band(bit32.bnot(n), 0x1F) + 128, true
- else
- return n, false
- end
- end
- --Waits for a click, drag, or release mouse event then returns the data
- local function getMouseEvent()
- local eventData = {os.pullEvent()}
- while eventData[1] ~= "mouse_click" and eventData[1] ~= "mouse_drag" and eventData[1] ~= "mouse_up" do
- eventData = {os.pullEvent()}
- end
- return eventData[1],eventData[2],eventData[3],eventData[4]
- end
- --Returns a frame with a width and height == 'w,h'
- local function makeFrame(w,h)
- --Make an entire horizontal row
- local tempCanv = string.rep("000100",w)
- local tempTxtCol = string.rep("0",w)
- local tempBackCol = string.rep("f",w)
- --db.txt(1,1,"Debug: w="..w..", h="..h,colors.red,colors.white)
- --db.txt(1,2,"Debug: "..#tempCanv..", "..#tempTxtCol..", "..#tempBackCol,colors.red,colors.white)
- --Copy each horizontal row for the required height
- local canvas = string.rep(tempCanv,h)
- local txtCol = string.rep(tempTxtCol,h)
- local backCol = string.rep(tempBackCol,h)
- --db.debug(1,3,#canvas..", "..#txtCol..", "..#backCol)
- return {
- canvas = canvas,
- txtCol = txtCol,
- backCol = backCol,
- w = w,
- h = h
- }
- end
- --Draws the frame 'frame' at coordinates 'x,y'
- local function drawFrame(x,y,frame)
- local canvas = frame.canvas
- local txtCol = frame.txtCol
- local backCol = frame.backCol
- for i = 1, frame.h do
- local offset = frame.w*(i-1)
- term_setCursorPos(x,y+i-1)
- for j = 1, frame.w do
- local index = offset + j
- local charStrIndex = index*6
- local txtC = string_sub(txtCol,index,index)
- local backC = string_sub(backCol,index,index)
- local charStr = string_sub(canvas,charStrIndex-5,charStrIndex)
- local char,swap = toChar(charStr)
- if swap then
- txtC,backC = backC,txtC
- end
- term_setTextColor( toCol( txtC ) )
- term_setBackgroundColor( toCol( backC ) )
- term_write( string_char( char ) )
- end
- end
- term_setTextColor(colors.white)
- term_setBackgroundColor(colors.black)
- end
- --[[
- ┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
- │1. Color codes: │
- └┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
- 0 = white
- 1 = orange
- 2 = magenta
- 3 = light blue
- 4 = yellow
- 5 = lime
- 6 = pink
- 7 = gray
- 8 = light gray
- 9 = cyan
- a = purple
- b = blue
- c = brown
- d = green
- e = red
- f = black
- ┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
- │2. Useful string.char's │
- └┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
- string.char()
- Char 127, 129-159, 168 are block and shading characters:
- 127 = transluscent grid
- 129-159 = block patterns
- 168 = horizontal colon ( : )
- ┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
- │3. Example use: │
- └┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
- local frm = require("engineDrawer")
- local frame = frm.makeFrame(10,10)
- term.clear()
- while true do
- frm.drawFrame(1,1,frame)
- local _,type,x,y = frm.getMouseEvent()
- if x <= frame.w and y <= frame.h then
- local targetIndex = (y-1)*frame.w + x
- local canvasIndex = targetIndex*6-5
- if type == 1 then
- --Left click
- --example:
- --frame.canvas = frm.insertString(frame.canvas, "110011", canvasIndex)
- --frame.backCol = frm.setStrIndex(frame.backCol, "1", targetIndex)
- elseif type == 2 then
- --Right click
- --example:
- --frame.canvas = frm.insertString(frame.canvas, "001100", canvasIndex)
- --frame.backCol = frm.setStrIndex(frame.backCol, "f", targetIndex)
- elseif type == 3 then
- --Middle click
- end
- end
- end
- ]]
- --Expose functions
- return {
- makeFrame = makeFrame,
- drawFrame = drawFrame,
- getMouseEvent = getMouseEvent,
- setStrIndex = setStrIndex,
- insertString = insertString,
- toChar = toChar,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement