Advertisement
Sewbacca

Computercraft ASCII Visualizer

Sep 4th, 2018
1,320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1.  
  2. --[[
  3.     MIT License
  4.  
  5.     Copyright (c) 2018 Sewbacca
  6.  
  7.     Permission is hereby granted, free of charge, to any person obtaining a copy
  8.     of this software and associated documentation files (the "Software"), to deal
  9.     in the Software without restriction, including without limitation the rights
  10.     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.     copies of the Software, and to permit persons to whom the Software is
  12.     furnished to do so, subject to the following conditions:
  13.  
  14.     The above copyright notice and this permission notice shall be included in all
  15.     copies or substantial portions of the Software.
  16.  
  17.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23.     SOFTWARE.
  24. --]]
  25.  
  26. local nPos = 0
  27. local textCol = "0"
  28. local backCol = "f"
  29. local highCol = "b"
  30.  
  31. local function update()
  32.     term.clear()
  33.     term.setCursorPos(2, 2)
  34.  
  35.     for i=0,255 do
  36.         local char = string.char(i)
  37.        
  38.         if nPos == i then
  39.             term.blit(char, textCol, highCol)
  40.         else
  41.             term.blit(char, textCol, backCol)
  42.         end
  43.  
  44.         if (i + 1) % 16 == 0 then    
  45.             term.setCursorPos(2, math.floor(2 + ((i + 1) / 16)))
  46.         end
  47.     end
  48.  
  49.     term.setCursorPos(4, 19)
  50.     write("Char: " .. nPos)
  51. end
  52.  
  53. local bRunning = true
  54. function handleEvent()
  55.     local e, k, x, y = os.pullEvent()
  56.  
  57.     if e == "key" then
  58.         if k == keys.left or k == keys.right then
  59.             nPos = nPos + (k == keys.left and -1 or 1)
  60.         elseif k == keys.up or k == keys.down then
  61.             nPos = nPos + (k == keys.up and -16 or 16)
  62.         elseif k == keys.backspace then
  63.             bRunning = false
  64.         end
  65.         nPos = nPos < 0 and 0 or nPos
  66.         nPos = nPos > 255 and 255 or nPos
  67.     elseif e:match "mouse" then
  68.         x = x - 2
  69.         y = y - 2
  70.  
  71.         if x < 0 or x > 15 or y < 0 or y > 255 / 16 then
  72.             return
  73.         end
  74.  
  75.         nPos = y * 16 + x
  76.     end
  77. end
  78.  
  79. while bRunning do
  80.     update()
  81.     handleEvent()
  82. end
  83. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement