KDKiller

CC GUI Lib

Jan 17th, 2021 (edited)
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.54 KB | None | 0 0
  1. --Types: select, int, u_int, options, bool, text
  2. --Meta: value, max_length, position (xy), fg, bg,
  3.  
  4. default = {
  5.     bg = colors.black,
  6.     fg = colors.white
  7. }
  8. default.width,default.height = term.getSize()
  9.  
  10. function createField(t)
  11.     setmetatable(t,{__index={typ='int', cursurPos={x=1,y=1},maxLength=7,fg=colors.white,bg=colors.gray}})
  12.     local ptr, typ, options, func, cursurPos, maxLength, fg, bg =
  13.       t[1] or t.ptr,
  14.       t[2] or t.typ,
  15.       t[3] or t.options,
  16.       t[4] or t.func,
  17.       t[5] or t.cursurPos,
  18.       t[6] or t.maxLength,
  19.       t[7] or t.fg,
  20.       t[8] or t.bg
  21.    
  22.     if ptr == nil then return nil end
  23.    
  24.     local active = false
  25.     local negative = typ == 'int' and ptr.val < 0
  26.  
  27.     return {update = function(key)
  28.         if typ == 'int' then
  29.             local edit = string.len(tostring(ptr.val))
  30.             if ptr.val == 0 then edit = 0 end
  31.  
  32.             if key <= 11 and (edit < maxLength-1 or (edit < maxLength and ptr.val<0)) then
  33.                 val = (key-1)%10
  34.                 if edit == 0 then
  35.                     ptr.val = val
  36.                     if negative and ptr.val > 0 then
  37.                         ptr.val = ptr.val * -1
  38.                     end
  39.                 else
  40.                     ptr.val = ptr.val*10
  41.                     if not negative then ptr.val = ptr.val + val
  42.                     else ptr.val = ptr.val - val end
  43.                 end
  44.                 return true
  45.             end
  46.             if keys.getName(key) == 'minus' and edit == 0 then
  47.                 negative = true
  48.                 ptr.val = 0
  49.                 return true
  50.             end
  51.             if keys.getName(key) == 'backspace' then
  52.                 if ptr.val > 0 then ptr.val = math.floor(ptr.val/10)
  53.                 else ptr.val = math.ceil(ptr.val/10) end
  54.  
  55.                 if edit == 0 and negative then
  56.                     negative = false
  57.                 end
  58.                 return true
  59.             end
  60.         elseif typ == 'bool' then
  61.             if keys.getName(key) == 'left' or keys.getName(key) == 'right' then
  62.                 ptr.val = not ptr.val
  63.                 return true
  64.             end
  65.         elseif typ == 'string' then
  66.             if string.len(keys.getName(key)) == 1 then b = string.byte(keys.getName(key))
  67.             else b = 0 end
  68.            
  69.             if keys.getName(key) == 'backspace' then
  70.                 ptr.val = string.sub(ptr.val,1,-2)
  71.                 return true
  72.             elseif b >= string.byte('a') and b <= string.byte('z') and string.len(ptr.val)<maxLength then
  73.                 ptr.val = ptr.val..keys.getName(key)
  74.                 return true
  75.             elseif key>=1 and key <= 11 and string.len(ptr.val)<maxLength then
  76.                 ptr.val = ptr.val..(key-1)%10
  77.                 return true
  78.             end
  79.         elseif typ == 'options' then
  80.             if keys.getName(key) == 'left' then
  81.                 ptr.val = (ptr.val - 1)%table.getn(options)
  82.                 return true
  83.             elseif keys.getName(key) == 'right' then
  84.                 ptr.val = (ptr.val + 1)%table.getn(options)
  85.                 return true
  86.             end
  87.         elseif typ == 'select' then
  88.             if keys.getName(key) == 'enter' then
  89.                 func()
  90.             end
  91.         end
  92.         return false
  93.     end,
  94.     render = function()
  95.         if active then term.setBackgroundColor( bg )
  96.         else term.setBackgroundColor( default.bg ) end
  97.  
  98.         term.setCursorPos(cursurPos.x,cursurPos.y)
  99.         for i=1,maxLength do write(' ') end
  100.  
  101.         -- term.setBackgroundColor( default.bg )
  102.         term.setCursorPos(cursurPos.x,cursurPos.y)
  103.         if active and typ == 'int' and negative then write('-') if ptr.val~= 0 then print(math.abs(ptr.val)) end
  104.         elseif typ == 'options' then print(options[ptr.val+1])
  105.         else print(ptr.val) end
  106.         term.setBackgroundColor( default.bg )
  107.         return
  108.     end,
  109.     getVal = function() return ptr.val end,
  110.     setVal = function(_val) ptr.val = _val end,
  111.     isActive = function() return active end,
  112.     setActive = function(_active) active=_active end,
  113.     getMaxLength = function() return maxLength end,
  114.     getCursurPos = function() return cursurPos end}
  115.  
  116. end
  117.  
  118. function drawBox(t)
  119.     setmetatable(t,{__index={bg=default.bg}})
  120.     local bg, pos1, pos2 =
  121.       t[1] or t.bg,
  122.       t[2] or t.pos1,
  123.       t[3] or t.pos2
  124.  
  125.     term.setBackgroundColor(bg)
  126.     for i=0,pos2[2]-pos1[2] do
  127.         term.setCursorPos(pos1[1],pos1[2]+i)
  128.         for i=1,pos2[1]-pos1[1]+1 do write(' ') end
  129.     end
  130.     term.setBackgroundColor(default.bg)
  131. end
  132.  
  133. function writeText(t)
  134.     setmetatable(t,{__index={fg=default.fg, bg=default.bg}})
  135.     local text, cursurPos, fg, bg =
  136.       t[1] or t.text,
  137.       t[2] or t.cursurPos,
  138.       t[3] or t.fg,
  139.       t[4] or t.bg
  140.  
  141.     if cursurPos then
  142.         term.setCursorPos(cursurPos.x,cursurPos.y)
  143.     end
  144.     term.setBackgroundColor(bg)
  145.     term.setTextColor(fg)
  146.     write(text)
  147.     term.setBackgroundColor(default.bg)
  148.     term.setTextColor(default.fg)
  149. end
  150.  
  151. function createMenuListener(t)
  152.     setmetatable(t,{__index={run_ptr={val=true},sel={val=0}}})
  153.     local fields, run_ptr, onChange, onScroll, sel =
  154.       t[1] or t.fields,
  155.       t[2] or t.run_ptr,
  156.       t[3] or t.onChange,
  157.       t[4] or t.onScroll,
  158.       t[5] or t.sel
  159.    
  160.     fields[sel.val+1].setActive(true)
  161.    
  162.     return function()
  163.         renderFields(fields)
  164.         while run_ptr.val do
  165.             local event,key,isHeld = os.pullEvent('key')
  166.  
  167.             if keys.getName(key) == 'down' then
  168.                 fields[sel.val+1].setActive(false)
  169.                 sel.val = (sel.val + 1)%table.getn(fields)
  170.                 fields[sel.val+1].setActive(true)
  171.                 if onScroll then onScroll() end
  172.                 renderFields(fields)
  173.             elseif keys.getName(key) == 'up' then
  174.                 fields[sel.val+1].setActive(false)
  175.                 sel.val = (sel.val - 1)%table.getn(fields)
  176.                 fields[sel.val+1].setActive(true)
  177.                 if onScroll then onScroll() end
  178.                 renderFields(fields)
  179.             else
  180.                 if fields[sel.val+1].update(key) and onChange then onChange() end
  181.                 -- fields[sel+1].render()
  182.                 renderFields(fields)
  183.             end
  184.             -- break
  185.         end
  186.     end
  187. end
  188.  
  189. function renderFields(fields)
  190.     for i=1,table.getn(fields) do
  191.         fields[i].render()
  192.     end
  193. end
  194.  
  195. function createMenuClickListener(t)
  196.     setmetatable(t,{__index={run_ptr={val=true},sel={val=0}}})
  197.     local fields, run_ptr, sel =
  198.       t[1] or t.fields,
  199.       t[2] or t.run_ptr,
  200.       t[3] or t.sel
  201.  
  202.     return function()
  203.         while run_ptr.val do
  204.             local event,btn,x,y = os.pullEvent('mouse_click')
  205.             if btn == 1 then
  206.                 for i=1,table.getn(fields) do
  207.                     pos = fields[i].getCursurPos()
  208.                     for j=1,fields[i].getMaxLength() do
  209.                         if y == pos.y and x == pos.x + j-1 then
  210.                             fields[sel.val+1].setActive(false)
  211.                             sel.val = i-1
  212.                             fields[sel.val+1].setActive(true)
  213.                             fields[sel.val+1].update(28) -- Enter Key
  214.                             renderFields(fields)
  215.                         end
  216.                     end
  217.                 end
  218.             end
  219.         end
  220.     end
  221. end
  222.  
  223. function reset()
  224.     term.clear()
  225.     term.setCursorPos(1,1)
  226.     term.setBackgroundColor(default.bg)
  227.     term.setTextColor(default.fg)
  228. end
Add Comment
Please, Sign In to add comment