Advertisement
Redxone

[CC] - Special Chars Binary Demonstration.

Jul 17th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.77 KB | None | 0 0
  1. _selectBit = "000000"
  2.  
  3. local guiX = 5
  4. local guiY = 4
  5. local running = true
  6.  
  7. function bitSpecial(chbit)
  8.   -- Add 5 bit binary to the index start of the
  9.   -- "small" txt.
  10.   if(chbit > 31)then
  11.     chbit = (64+(chbit * -1))-1
  12.   end
  13.   return string.char(128 + chbit)
  14. end
  15.  
  16. function draw()
  17.   term.setBackgroundColor(colors.black)
  18.   term.setTextColor(colors.white)
  19.   term.clear()
  20.   -- Draws the GUI for the mouse event stuff.
  21.   paintutils.drawFilledBox(guiX,guiY,guiX+3,guiY+4,colors.red)
  22.   paintutils.drawFilledBox(guiX+1,guiY+1,guiX+2,guiY+3,colors.gray)
  23.  
  24. end
  25.  
  26. function drawBit(ind)
  27.   -- Bit position based on reverse index logic.
  28.   if(ind > 2)then
  29.     posX = (ind%2 *-1) + 2
  30.     posY = ind/2 - ( (ind%2 *-1) +1)
  31.   else
  32.     posX = ind
  33.     posY = 0
  34.   end
  35.   term.setCursorPos(guiX+posX,guiY+posY+1)
  36.   if(_selectBit:sub(ind,ind) == "1")then
  37.     term.blit(" ", "F","0")
  38.   else
  39.     term.blit(" ", "F","7")
  40.   end
  41.   term.setCursorPos(1,2)
  42.   write("mX: " .. posX .. " mY: " .. posY)
  43. end
  44.  
  45. function update(e)
  46.     if(e[1] == "key")then running = false return end
  47.     if(e[1] == "mouse_click" or e[1] == "mouse_drag")then
  48.         local mb, mx, my = e[2], e[3], e[4]
  49.         -- Figure location to insert into bits.
  50.         if(mx >= guiX+1 and mx < guiX+3 and
  51.            my >= guiY+1 and my < guiY+4)then
  52.          
  53.            -- Decode absolute mouse to relative bit
  54.            -- offset.
  55.            
  56.            local indx = (mx-(guiX+1)+1) -- Offs.
  57.            local indy = my-(guiY+1) -- Entry point.
  58.            local ind = indx+(indy*2)
  59.            -- Insert data for bit respective to button.
  60.            local chg = "1"
  61.            if(mb == 2)then chg = "0" end
  62.            _selectBit = (_selectBit:sub(1,ind-1)
  63.                .. chg .. _selectBit:sub(ind+1,-1) )
  64.            drawBit(ind)
  65.                    -- Convert binary edit to decimal.
  66.            local spStr = bitSpecial(tonumber(_selectBit:reverse(),2))
  67.            term.setCursorPos(1,1)
  68.            write("Index: " .. ind .. " :> " ..tonumber(_selectBit:reverse(),2) .. "   ")
  69.            term.setCursorPos(1,3)
  70.            write("Char: " .. string.byte(spStr))
  71.            term.setCursorPos(guiX+5,guiY+2)
  72.             local cols = {colors.white,colors.gray}
  73.             if(tonumber(_selectBit:reverse(),2) > 31)then
  74.                 local c1 = cols[1]
  75.                 local c2 = cols[2]
  76.                 cols[2] = c1
  77.                 cols[1] = c2
  78.             end
  79.             term.setTextColor(cols[1])
  80.             term.setBackgroundColor(cols[2])
  81.             write(spStr)
  82.         end
  83.     end
  84. end
  85. draw()
  86.  
  87. while running do
  88.   term.setBackgroundColor(colors.gray)
  89.   term.setTextColor(colors.white)
  90.   local ev = {os.pullEvent()}
  91.   update(ev)
  92.   term.setCursorPos(12,10)
  93.   write("Press any key to end demo.")
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement