Advertisement
slipers

Minecraft. OpenComputers. Cedit (/lib/invertedblink.lua).

Sep 1st, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local process = require("process")
  4. local term = require("term")
  5.  
  6. local invert_cursor_blink = false
  7. local invertedblink = {}
  8.  
  9. invertedblink.internal = {}
  10.  
  11. function invertedblink.internal.window()
  12.   return process.info().data.window
  13. end
  14.  
  15. local W = invertedblink.internal.window
  16.  
  17. local function toggleBlink()
  18.   local w = W()
  19.   if term.isAvailable() then
  20.     cursorBlink.state = not cursorBlink.state
  21.     if invert_cursor_blink then
  22.       local d = component.gpu.getDepth()
  23.       if cursorBlink.state then
  24.         local alt, fg, bg, fgp, bgp = component.gpu.get(w.x, w.y)
  25.         if fg == bg then
  26.           fg = 0xFFFFFF
  27.           bg = 0x000000
  28.         end
  29.         if fgp == bgp then
  30.           fgp = 0
  31.           bgp = 15
  32.         end        
  33.         cursorBlink.alt = alt or cursorBlink.alt
  34.         cursorBlink.altfg = fg or fgp
  35.         cursorBlink.altbg = bg or bgp
  36.         cursorBlink.altfgp = fgp or fg
  37.         cursorBlink.altbgp = bgp or bg
  38.         if d == 1 then
  39.           component.gpu.setForeground(cursorBlink.altbg)
  40.           component.gpu.setBackground(cursorBlink.altfg)
  41.         elseif d == 4 then
  42.           component.gpu.setForeground(cursorBlink.altbgp, true)
  43.           component.gpu.setBackground(cursorBlink.altfgp, true)
  44.         elseif d == 8 then
  45.           component.gpu.setForeground(cursorBlink.altbg)
  46.           component.gpu.setBackground(cursorBlink.altfg)
  47.         end
  48.         component.gpu.set(w.x, w.y, cursorBlink.alt)
  49.       else
  50.         if d == 1 then
  51.           component.gpu.setForeground(cursorBlink.altfg)
  52.           component.gpu.setBackground(cursorBlink.altbg)
  53.         elseif d == 4 then
  54.           component.gpu.setForeground(cursorBlink.altfgp, true)
  55.           component.gpu.setBackground(cursorBlink.altbgp, true)
  56.         elseif d == 8 then
  57.           component.gpu.setForeground(cursorBlink.altfg)
  58.           component.gpu.setBackground(cursorBlink.altbg)
  59.         end
  60.         component.gpu.set(w.x, w.y, cursorBlink.alt)
  61.       end
  62.     else
  63.       if cursorBlink.state then
  64.         cursorBlink.alt = component.gpu.get(w.x, w.y) or cursorBlink.alt
  65.         component.gpu.set(w.x, w.y, string.rep(unicode.char(0x2588), unicode.charWidth(cursorBlink.alt))) -- solid block
  66.       else
  67.         component.gpu.set(w.x, w.y, cursorBlink.alt)
  68.       end
  69.     end
  70.   end
  71. end
  72.  
  73. function invertedblink.setCursorBlinkInvert(enabled)
  74.   invert_cursor_blink = enabled
  75. end
  76.  
  77. function invertedblink.setCursorBlink(enabled)
  78.   if enabled then
  79.     if invert_cursor_blink then
  80.         if not cursorBlink then
  81.             cursorBlink = {}
  82.             cursorBlink.id = event.timer(0.5, toggleBlink, math.huge)
  83.             cursorBlink.state = false
  84.             cursorBlink.alt = " "
  85.             cursorBlink.altfg = 0xFFFFFF
  86.             cursorBlink.altbg = 0x000000
  87.             cursorBlink.altfgp = 0
  88.             cursorBlink.altbgp = 15
  89.         elseif not cursorBlink.state then
  90.             toggleBlink()
  91.         end
  92.     end
  93. elseif cursorBlink then
  94.     event.cancel(cursorBlink.id)
  95.     if cursorBlink.state then
  96.       toggleBlink()
  97.     end
  98.     cursorBlink = nil
  99.   end
  100.   W().blink=enabled
  101. end
  102.  
  103. function invertedblink.setCursor(x,y)
  104.   local w = W()
  105.   if cursorBlink and cursorBlink.state then
  106.     toggleBlink()
  107.   end
  108.   w.x,w.y=x,y
  109. end
  110.  
  111. return invertedblink
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement