Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bitToColor(bits)
- if(type(bits) == "string" or type(bits) == "char")then
- bits = string.byte(bits)
- end
- -- Returns resulting text and back color
- return (2^( bit32.rshift(bits,4 ))), (2^(bit32.bxor(bits, bit32.lshift( bit32.rshift(bits,4),4))))
- end
- function colorToBit(tc,bc)
- local tBit = math.log(tc,2) / math.log(2)
- local bBit = math.log(bc,2) / math.log(2)
- local byte = bit32.lshift(tBit,4) + bBit
- return string.char(byte)
- end
- function getScreen()
- local mini_screen = {
- cursor = {
- xpos = 1,
- ypos = 1,
- },
- color = {
- bcol = colors.black,
- tcol = colors.white,
- },
- screen = {
- color={},
- },
- -- Bit logical stuff.
- SCBOffs = function(chr)
- -- Convert to base 10 soooo easy
- if(string.byte(chr) < 128)then
- error("SCB: Cannot generate offset on char: " .. chr)
- end
- local dec = string.byte(chr)-128
- local bin = ""
- local binkey = "01"
- local rim
- if(type(dec) == "string")then error("toBin -> Invalid type: " .. dec) end
- while dec > 0 do
- rim = math.floor(dec%2)
- bin = binkey:sub(rim+1,rim+1) .. bin
- dec = math.floor(dec/2)
- end
- if(#bin < 5)then
- bin = string.rep("0",8-#bin) .. bin
- end
- return bin:reverse()
- end,
- init = function(self)
- -- Determine size of screen
- local w, h = term.getSize()
- for y = 1, h do
- -- Fill screen with mini chars
- self.screen[y] = string.rep(string.char(128),w)
- self.screen.color[y] = string.rep(string.char(15),w) -- Normal color
- end
- end,
- -- Ease of use.
- clear = initialize,
- -- Screen functionality
- getSpecialChar = function(self,chbit)
- -- Add 5 bit binary to the index start of the
- -- "small" txt.
- if(type(chbit) == "string")then
- chbit = tonumber(chbit,2)
- end
- if(chbit > 31)then
- chbit = (64+(chbit * -1))-1
- end
- return string.char(128 + chbit)
- end,
- _BitRenderChar = function(self,chbit)
- local isMax = false
- if(chbit > 31)then
- isMax = true
- end
- -- Determine draw position, and check to invert colors.
- local tcol = self.color.tcol
- local bcol = self.color.bcol
- if(isMax)then
- tcol = self.color.bcol
- bcol = self.color.tcol
- end
- term.setCursorPos(self.cursor.xpos,self.cursor.ypos)
- term.setTextColor(tcol)
- term.setBackgroundColor(bcol)
- write(self:getCharAtCursor())
- end,
- setCursorPos = function(self,x,y)
- self.cursor.xpos = x
- self.cursor.ypos = y
- end,
- getCharAtCursor = function(self)
- return self.screen[self.cursor.ypos]:sub(self.cursor.xpos,self.cursor.xpos)
- end,
- blitPos = function(self,x,y,fnt,tc,bc)
- self.cursor.xpos = x
- self.cursor.ypos = y
- self:blit(fnt,tc,bc)
- end,
- writeScreen = function(self,x,y,chr)
- self.screen[y] = self.screen[y]:sub(1,x-1)
- .. chr .. self.screen[y]:sub(x+1,-1)
- self.screen.color[y] = self.screen.color[y]:sub(1,x-1)
- .. colorToBit(self.color.tcol,self.color.bcol) .. self.screen.color[y]:sub(x+1,-1)
- end,
- getCharAt = function(self,x,y)
- return self.screen[y]:sub(x,x), self.screen.color[y]:sub(x,x)
- end,
- drawScreenAt = function(self,x,y)
- local chbit = tonumber(self.SCBOffs(self.screen[y]:sub(x,x)),2)
- local isMax = false
- if(chbit > 31)then
- isMax = true
- end
- -- Determine draw position, and check to invert colors.
- local tcol, bcol = bitToColor(self.screen.color[y]:sub(x,x))
- if(isMax)then
- local bcol, tcol = bitToColor(self.screen.color[y]:sub(x,x))
- end
- term.setCursorPos(x,y)
- term.setTextColor(tcol)
- term.setBackgroundColor(bcol)
- write(self.screen[y]:sub(x,x))
- end,
- blit = function(self,fnt,tc,bc)
- local scrX = self.cursor.xpos
- local scrY = self.cursor.ypos
- if(tc)then self:setTextColor(tc) end
- if(bc)then self:setBackColor(bc) end
- local bChr = fnt
- if(type(fnt) == "table")then
- -- Convert char to binary for bit level manip.
- bChr = self.SCBOffs(self:getCharAtCursor())
- for i = 1, #fnt do
- if( type(fnt[1]) == "number" )then
- local bit = bChr:sub(fnt[i],fnt[i])
- if(bit == "1")then bit = "0" else bit = "1" end
- bChr = (bChr:sub(1,fnt[i]-1)
- .. bit .. bChr:sub(fnt[i]+1,-1) )
- else
- error("Numbers must be used for toggle-able bits!")
- end
- end
- end
- -- Get char as ascii
- local aChar = self:getSpecialChar(bChr:reverse())
- -- Re-insert char into screen table
- self:writeScreen(scrX,scrY,aChar)
- self:_BitRenderChar(tonumber(bChr:reverse(),2))
- end,
- blitBox = function(self,fx,fy,tx,ty,col)
- local bw = (tx - fx)
- local bh = (ty - fy) -2
- self:setTextColor(col)
- for y = 1, bh do
- self:blitPos(fx+1,fy+y,"101010")
- self:blitPos(fx+bw,fy+y,"010101")
- for x = 1, bw do
- self:blitPos(fx+x,fy,"110000")
- self:blitPos(fx+x,fy+bh,"000011")
- end
- end
- self:blitPos(fx+1,fy,"111010") -- Upper left
- self:blitPos(fx+bw,fy,"110101") -- Upper right
- self:blitPos(fx+1,fy+bh,"101011") -- Lower left
- self:blitPos(fx+bw,fy+bh,"010111") -- Lower right
- end,
- blitFilledBox = function(self,fx,fy,tx,ty,col)
- local bw = (tx - fx)
- local bh = (ty - fy) -1
- self:setTextColor(col)
- for y = 1, bh do
- for x = 1, bw do
- self:blitPos(fx+x,fy+y-1,"111111")
- end
- end
- end,
- setTextColor = function(self,c)
- self.color.tcol = c
- end,
- setBackColor = function(self,c)
- self.color.bcol = c
- end,
- getTextColor = function(self)
- return self.color.tcol
- end,
- getBackColor = function(self)
- return self.color.bcol
- end,
- }
- mini_screen:init()
- return mini_screen
- end
- function ccmini_demo(num)
- if(not num)then
- error("Ussage: ccmini_demo (int number) - n/2")
- end
- if(num == 1)then
- term.clear()
- local myScr = getScreen()
- myScr:setCursorPos(2,2)
- myScr:blit({1},colors.blue,colors.black)
- sleep(0.01)
- myScr:blit({3},colors.blue,colors.black)
- sleep(0.01)
- myScr:blit({5},colors.blue,colors.black)
- sleep(0.01)
- myScr:blit({4},colors.blue,colors.black)
- sleep(0.01)
- myScr:setCursorPos(3,2)
- myScr:blit({1},colors.blue,colors.black)
- sleep(0.01)
- myScr:blit({3},colors.blue,colors.black)
- sleep(0.01)
- myScr:blit({5},colors.blue,colors.black)
- sleep(0.01)
- myScr:setCursorPos(4,2)
- myScr:blit({1},colors.red,colors.black)
- sleep(0.01)
- myScr:blit({3},colors.red,colors.black)
- sleep(0.01)
- myScr:blit({5},colors.red,colors.black)
- sleep(0.01)
- elseif(num == 2)then
- term.clear()
- myScr = getScreen()
- myScr:setBackColor(colors.white)
- local xdir = 1
- local ydir = 1
- local xvel = 1
- local yvel = 1
- local w,h = term.getSize()
- while true do
- term.current().setVisible(false)
- myScr:blitFilledBox(5+xdir,5+ydir,11+xdir,10+ydir,colors.white)
- myScr:blitBox(5+xdir,5+ydir,11+xdir,10+ydir,colors.green)
- myScr:blitPos(8+xdir,6+ydir,"001111",colors.red,colors.white)
- myScr:blitPos(8+xdir,7+ydir,"111100",colors.green)
- myScr:blitPos(9+xdir,6+ydir,"001111",colors.yellow)
- myScr:blitPos(9+xdir,7+ydir,"111100",colors.blue)
- term.current().setVisible(true)
- myScr:blitBox(math.floor(w/2)-9,math.floor(h/2)-1,math.floor(w/2)+8,math.floor(h/2)+3,colors.gray)
- term.setCursorPos((w/2)-7,(h/2))
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- write(" Hold any key. ")
- os.queueEvent("Tick")
- e = {os.pullEvent()}
- if(e[1] == "key")then
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- sleep(0.3)
- return
- end
- sleep(0.01)
- myScr:blitFilledBox(5+xdir,5+ydir,11+xdir,10+ydir,colors.black)
- -- Change velocity in direction
- if(11+xdir >= w or 5+xdir < 2)then
- xvel = xvel * -1
- end
- if(10+ydir >= h or 5+ydir < 2)then
- yvel = yvel * -1
- end
- xdir = xdir + xvel
- ydir = ydir + yvel
- end
- end
- end
- ccmini_demo(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement