Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --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 setStringIndex(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 writeString(str,added,n)
- return string.sub(str,1,n-1)..added..string.sub(str,n+#added)
- end
- --Returns the modified frame 'bf' (base frame) w/ the frame 'af' (added frame) inserted at coordinates 'x,y'
- local function addFrame(x,y,bf,af)
- local endOffset = 0
- if af.w + x > bf.w then
- endOffset = af.w + x - bf.w
- end
- for i = 1, af.h do
- local baseStart = bf.w*(y-1)+x
- local addStart = af.w*(i-1)
- local addEnd = af.w*i-endOffset
- bf.canvas = writeString(bf.canvas, string.sub(af.canvas, addStart, addEnd), baseStart)
- bf.txtCol = writeString(bf.txtCol, string.sub(af.txtCol, addStart, addEnd), baseStart)
- bf.backCol = writeString(bf.backCol, string.sub(af.backCol, addStart, addEnd), baseStart)
- end
- return bf
- end
- --Returns a button with coordinates 'x,y', a name of 'name', and a stored function of 'function'
- local function makeButton(x,y,name,doThing)
- local str = "["..name.."]"
- local txtCol = ""
- local backCol = ""
- for i = 1, #str do
- local col = "w"
- if i == 1 or i == #str then
- col = "1"
- end
- txtCol = txtCol..col
- backCol = backCol.."f"
- end
- return {
- x = x,
- y = y,
- h = 1,
- w = #name+2,
- name = name,
- canvas = str,
- txtCol = txtCol,
- backCol = backCol,
- doThing = doThing
- }
- end
- --Goes through a list of buttons made with the makeButton() function and checks to see whether the
- -- coordinates 'x,y' overlap with any of the buttons set coordinates. Returns 'true,i' where i is
- -- the index it found the first overlap. Else retuns 'false,nil' if unable to find an overlap.
- local function checkForButtons(buttons,x,y)
- for i = 1, #buttons do
- local b = buttons[i]
- if b.x <= x and x <= b.x+#b.name and y == b.y then
- return true,i
- end
- end
- return false,nil
- 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 txtC = txtCol:sub(index,index)
- local backC = backCol:sub(index,index)
- local char = canvas:sub(index,index)
- term.setTextColor( toCol( txtC ) )
- term.setBackgroundColor( toCol( backC ) )
- term.write( char )
- end
- end
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- --Draws the frame 'frame' at coordinates 'x,y' without individual character colors
- local function drawFrameFast(x,y,frame,txtCol,backCol)
- local canvas = frame.canvas
- term.setTextColor(txtCol)
- term.setBackgroundColor(backCol)
- for i = 1, frame.h do
- local startIndex = frame.w*(i-1)+1
- local endIndex = frame.w*i
- local str = canvas:sub(startIndex,endIndex)
- term.setCursorPos( x,y+i-1 )
- term.write( str )
- end
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- 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)
- --Create an entire horizontal row
- local tempCanv = string.rep(".",w)
- local tempTxtCol = string.rep("0",w)
- local tempBackCol = string.rep("f",w)
- --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)
- return {
- canvas = canvas,
- txtCol = txtCol,
- backCol = backCol,
- w = w,
- h = h
- }
- 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: │
- └┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
- --if fs.exists("frameDrawer.lua") then fs.delete("frameDrawer.lua") end
- --shell.run("pastebin get FRyEWAxh frameDrawer")
- local frm = require("frameDrawer")
- local tw,th = term.getSize() --term width, term height
- local frame = frm.makeFrame(10,10)
- term.clear()
- while true do
- --frm.drawFrameFast(1,1,frame,colors.white,colors.black)
- 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
- if type == 1 then
- --Left click
- --example:
- --frame.canvas = frm.pstr(frame.canvas, "HELLO WORLD!", targetIndex)
- --frame.backCol = frm.ppx(frame.backCol, "1", targetIndex)
- elseif type == 2 then
- --Right click
- --frame.canvas = frm.pstr(frame.canvas, " ", targetIndex)
- --frame.backCol = frm.ppx(frame.backCol, "f", targetIndex)
- elseif type == 3 then
- --Middle click
- end
- end
- end
- ]]
- --Expose functions
- local exposed = {
- makeButton = makeButton,
- checkForButtons = checkForButtons,
- makeFrame = makeFrame,
- drawFrame = drawFrame,
- drawFrameFast = drawFrameFast,
- getMouseEvent = getMouseEvent,
- ppx = setStringIndex,
- pstr = writeString,
- }
- return exposed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement