Advertisement
magik6000

MGTK

Jun 1st, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.60 KB | None | 0 0
  1.  
  2. local charMap =
  3. {
  4.     [2] = "1",
  5.     [3] = "2",
  6.     [4] = "3",
  7.     [5] = "4",
  8.     [6] = "5",
  9.     [7] = "6",
  10.     [8] = "7",
  11.     [9] = "8",
  12.     [10] = "9",
  13.     [11] = "0",
  14.     [12] = "-",
  15.     [13] = "=",
  16.    
  17.     [16] = "q",
  18.     [17] = "w",
  19.     [18] = "e",
  20.     [19] = "r",
  21.     [20] = "t",
  22.     [21] = "y",
  23.     [22] = "u",
  24.     [23] = "i",
  25.     [24] = "o",
  26.     [25] = "p",
  27.     [26] = "[",
  28.     [27] = "]",
  29.    
  30.     [30] = "a",
  31.     [31] = "s",
  32.     [32] = "d",
  33.     [33] = "f",
  34.     [34] = "g",
  35.     [35] = "h",
  36.     [36] = "j",
  37.     [37] = "k",
  38.     [38] = "l",
  39.     [39] = ";",
  40.     [40] = "'",
  41.    
  42.     [44] = "z",
  43.     [45] = "x",
  44.     [46] = "c",
  45.     [47] = "v",
  46.     [48] = "b",
  47.     [49] = "n",
  48.     [50] = "m",
  49.     [51] = ",",
  50.     [52] = ".",
  51.     [53] = "/",
  52.    
  53.     [57] = " ",
  54.     [71] = "7",
  55.     [72] = "8",
  56.     [73] = "9",
  57.     [75] = "4",
  58.     [76] = "5",
  59.     [77] = "6",
  60.     [79] = "1",
  61.     [81] = "2",
  62.     [82] = "3",
  63.    
  64.     [181] = "/",
  65.     [55] = "*",
  66.     [74] = "-",
  67.     [78] = "+",
  68.     [41] = "`"
  69.    
  70. }
  71.  
  72.  
  73. function newTermFrame(t)
  74.     local handle = {}
  75.     handle.term = t
  76.    
  77.     handle.frame = {}
  78.    
  79.     handle.put = function(frame)
  80.         handle.frame = frame
  81.         return handle
  82.     end
  83.    
  84.     handle.render = function()
  85.         term.clear()
  86.        
  87.         local render = {}
  88.         render.write = function(x,y,text)
  89.             handle.term.setCursorPos(x,y)
  90.             handle.term.write(text)
  91.         end
  92.        
  93.         if handle.frame then
  94.             handle.frame.render.render(render)
  95.         end
  96.         return handle
  97.     end
  98.    
  99.    
  100.     handle.setFocused = function(frame)
  101.         handle.activeFrame = frame
  102.     end
  103.    
  104.     handle.event = {}
  105.     handle.event.click = function(x,y)
  106.         if handle.frame then
  107.             handle.frame.event.click(x,y,handle)
  108.         end
  109.     end
  110.    
  111.     handle.event.key = function(c)
  112.         if handle.activeFrame then
  113.             if handle.activeFrame.event.key then
  114.                 handle.activeFrame.event.key(c,handle)
  115.             end
  116.         end
  117.     end
  118.    
  119.     return handle
  120. end
  121.  
  122. function newVframe(size)
  123.     local handle = {}
  124.    
  125.     handle.frames = {}
  126.     handle.size = size
  127.    
  128.     handle.put = function(n, frame)
  129.         handle.frames[n] = frame
  130.         return handle
  131.     end
  132.    
  133.     handle.render = {}
  134.    
  135.     handle.render.getWidth = function()
  136.         local max = 0
  137.         for n=1, handle.size do
  138.             if handle.frames[n] then
  139.                 local w = handle.frames[n].render.getWidth()
  140.                 if w > max then
  141.                     max = w
  142.                 end
  143.             end
  144.         end
  145.         return max
  146.     end
  147.    
  148.     handle.render.getHeight = function()
  149.         local height = 0
  150.         for n=1, handle.size do
  151.             if handle.frames[n] then
  152.                 height = height + handle.frames[n].render.getHeight()
  153.             end
  154.         end
  155.         return height
  156.     end
  157.    
  158.     handle.render.render = function(ctx)
  159.         local h = 0
  160.         for n=1, handle.size do
  161.             if handle.frames[n] then
  162.                 local render = {}
  163.                 render.write = function(x,y,txt)
  164.                     ctx.write(x,y+h,txt)
  165.                 end
  166.                 handle.frames[n].render.render(render)
  167.                 h = h + handle.frames[n].render.getHeight()
  168.             end
  169.         end
  170.     end
  171.  
  172.     handle.event = {}
  173.     handle.event.click = function(x,y, master)
  174.         local top = 1
  175.         for n=1, handle.size do
  176.             if handle.frames[n] then
  177.                 local bottom = top + handle.frames[n].render.getHeight() - 1
  178.                 local width = handle.frames[n].render.getWidth()
  179.                
  180.                 if x <= width and y >= top and y <= bottom then
  181.                     handle.frames[n].event.click(x, y - top + 1, master)
  182.                     break
  183.                 end
  184.                 top = bottom + 1
  185.             end
  186.         end
  187.     end
  188.    
  189.     return handle
  190. end
  191.  
  192. function newHframe(size)
  193.     local handle = {}
  194.    
  195.     handle.frames = {}
  196.     handle.size = size
  197.    
  198.     handle.put = function(n, frame)
  199.         handle.frames[n] = frame
  200.         return handle
  201.     end
  202.    
  203.     handle.render = {}
  204.    
  205.     handle.render.getWidth = function()
  206.         local width = 0
  207.         for n=1, handle.size do
  208.             if handle.frames[n] then
  209.                 width = width + handle.frames[n].render.getWidth()
  210.             end
  211.         end
  212.         return width
  213.     end
  214.    
  215.     handle.render.getHeight = function()
  216.         local max = 0
  217.         for n=1, handle.size do
  218.             if handle.frames[n] then
  219.                 local h = handle.frames[n].render.getHeight()
  220.                 if h > max then
  221.                     max = h
  222.                 end
  223.             end
  224.         end
  225.         return max
  226.     end
  227.    
  228.     handle.render.render = function(ctx)
  229.         local w = 0
  230.         for n=1, handle.size do
  231.             if handle.frames[n] then
  232.                 local render = {}
  233.                 render.write = function(x,y,txt)
  234.                     ctx.write(x+w,y,txt)
  235.                 end
  236.                 handle.frames[n].render.render(render)
  237.                 w = w + handle.frames[n].render.getWidth()
  238.             end
  239.         end
  240.     end
  241.    
  242.     handle.event = {}
  243.     handle.event.click = function(x,y,master)
  244.         local left = 1
  245.         for n=1, handle.size do
  246.             if handle.frames[n] then
  247.                 local right = left + handle.frames[n].render.getWidth() - 1
  248.                 local height = handle.frames[n].render.getHeight()
  249.                
  250.                 if y <= height and x >= left and x <= right then
  251.                     handle.frames[n].event.click(x - left + 1, y, master)
  252.                     break
  253.                 end
  254.                 left = right + 1
  255.             end
  256.         end
  257.     end
  258.    
  259.     return handle
  260. end
  261.  
  262. function newBorderFrame(p1,p2,p3,p4)
  263.     local handle = {}
  264.    
  265.     if p1 and not(p2 or p3 or p4) then
  266.         handle.top = p1
  267.         handle.bottom = p1
  268.         handle.left = p1
  269.         handle.right = p1
  270.     elseif (p1 and p2) and not(p3 or p4) then
  271.         handle.top = p1
  272.         handle.bottom = p1
  273.         handle.left = p2
  274.         handle.right = p2
  275.     elseif p1 and p2 and p3 and p4 then
  276.         handle.top = p1
  277.         handle.bottom = p2
  278.         handle.left = p3
  279.         handle.right = p4
  280.     else
  281.         error("MGTK:newBorderFrame: Invalid parameters")
  282.     end
  283.    
  284.     handle.put = function(frame)
  285.         handle.frame = frame
  286.         return handle
  287.     end
  288.    
  289.     handle.render = {}
  290.    
  291.     handle.render.getWidth = function()
  292.         local w = handle.left + handle.right
  293.         if handle.frame then
  294.             w = w + handle.frame.render.getWidth()
  295.         end
  296.         return w
  297.     end
  298.    
  299.     handle.render.getHeight = function()
  300.         local h = handle.top + handle.bottom
  301.         if handle.frame then
  302.             h = h + handle.frame.render.getHeight()
  303.         end
  304.         return h
  305.     end
  306.    
  307.     handle.render.render = function(ctx)
  308.         if handle.frame then
  309.             local render = {}
  310.             render.write = function(x,y,txt)
  311.                 ctx.write(x + handle.left, y + handle.top, txt)
  312.             end
  313.             handle.frame.render.render(render)
  314.         end
  315.     end
  316.    
  317.     handle.event = {}
  318.     handle.event.click = function(x,y,master)
  319.         if handle.frame then
  320.             handle.frame.event.click(x-handle.left,y-handle.top,master)
  321.         end
  322.     end
  323.    
  324.     return handle
  325. end
  326.  
  327. function newClickableFrame(action)
  328.     local handle = {}
  329.     handle.action = action
  330.     handle.render = {}
  331.    
  332.     handle.put = function(frame)
  333.         handle.frame = frame
  334.         handle.render = handle.frame.render
  335.         return handle
  336.     end
  337.    
  338.    
  339.     handle.event = {}
  340.     handle.event.click = function(x,y,master)
  341.         handle.action(handle,x,y)
  342.         if handle.frame then
  343.             handle.frame.event.click(x,y,master)
  344.         end
  345.     end
  346.    
  347.     return handle
  348. end
  349.  
  350. function newTextLabel(text)
  351.     local handle = {}
  352.     handle.text = text
  353.     handle.render = {}
  354.    
  355.     handle.render.getWidth = function()
  356.         return #handle.text
  357.     end
  358.    
  359.     handle.render.getHeight = function()
  360.         return 1
  361.     end
  362.    
  363.     handle.setText = function(text)
  364.         handle.text = text
  365.         return handle
  366.     end
  367.    
  368.     handle.getText = function(text)
  369.         return handle.text
  370.     end
  371.    
  372.     handle.render.render = function(ctx)
  373.         ctx.write(1,1,handle.text)
  374.     end
  375.    
  376.     handle.event = {}
  377.     handle.event.click = function()end
  378.    
  379.     return handle
  380. end
  381.  
  382. function newTextInput(width, default)
  383.     local handle = {}
  384.     handle.width = width
  385.     handle.render = {}
  386.     handle.text = default or ""
  387.    
  388.     handle.render.getWidth = function()
  389.         return handle.width+1
  390.     end
  391.    
  392.     handle.render.getHeight = function()
  393.         return 1
  394.     end
  395.    
  396.     handle.setText = function(text)
  397.         handle.text = text
  398.         return handle
  399.     end
  400.    
  401.     handle.getText = function(text)
  402.         return handle.text
  403.     end
  404.    
  405.     handle.render.render = function(ctx)
  406.         ctx.write(1,1,handle.text:sub(#handle.text-handle.width > 0 and #handle.text-handle.width+1 or 0,#handle.text))
  407.     end
  408.    
  409.     handle.event = {}
  410.     handle.event.click = function(x,y,master)master.setFocused(handle)end
  411.    
  412.     handle.event.key = function(key, master)
  413.         if charMap[key] then
  414.             handle.text = handle.text .. charMap[key]
  415.         elseif key == 14 then
  416.             handle.text = handle.text:sub(1,#handle.text-1)
  417.         end
  418.         master.render()
  419.     end
  420.    
  421.     return handle
  422. end
  423.  
  424. function newPasswordInput(width, default)
  425.     local handle = {}
  426.     handle.width = width
  427.     handle.render = {}
  428.     handle.text = default or ""
  429.     handle.rendered = default and string.rep("*",#default) or ""
  430.    
  431.     handle.render.getWidth = function()
  432.         return handle.width+1
  433.     end
  434.    
  435.     handle.render.getHeight = function()
  436.         return 1
  437.     end
  438.    
  439.     handle.setText = function(text)
  440.         handle.text = text
  441.         handle.rendered = string.rep("*",#text)
  442.         return handle
  443.     end
  444.    
  445.     handle.getText = function(text)
  446.         return handle.text
  447.     end
  448.    
  449.     handle.render.render = function(ctx)
  450.         ctx.write(1,1,handle.rendered:sub(#handle.rendered-handle.width > 0 and #handle.rendered-handle.width+1 or 0,#handle.rendered))
  451.     end
  452.    
  453.     handle.event = {}
  454.     handle.event.click = function(x,y,master)master.setFocused(handle)end
  455.    
  456.     handle.event.key = function(key, master)
  457.         if charMap[key] then
  458.             handle.text = handle.text .. charMap[key]
  459.             handle.rendered = handle.rendered .. "*"
  460.         elseif key == 14 then
  461.             handle.text = handle.text:sub(1,#handle.text-1)
  462.             handle.rendered = handle.rendered:sub(1,#handle.rendered-1)
  463.         end
  464.         master.render()
  465.     end
  466.    
  467.     return handle
  468. end
  469.  
  470. function newTextButton(text, action)
  471.     local handle = {}
  472.     handle.text = text
  473.     handle.action = action
  474.     handle.render = {}
  475.    
  476.     handle.render.getWidth = function()
  477.         return #handle.text
  478.     end
  479.    
  480.     handle.render.getHeight = function()
  481.         return 1
  482.     end
  483.    
  484.     handle.setText = function(text)
  485.         handle.text = text
  486.         return handle
  487.     end
  488.    
  489.     handle.getText = function(text)
  490.         return handle.text
  491.     end
  492.    
  493.     handle.render.render = function(ctx)
  494.         ctx.write(1,1,handle.text)
  495.     end
  496.    
  497.     handle.event = {}
  498.     handle.event.click = function(x,y,master)
  499.         master.setFocused(handle)
  500.         if handle.action then
  501.             handle.action(handle,x,y)
  502.         end
  503.     end
  504.    
  505.     return handle
  506. end
  507.  
  508. function newToggleButton(text1, text2, action)
  509.     local handle = {}
  510.     handle.text1 = text1
  511.     handle.text2 = text2
  512.     handle.state = true
  513.     handle.action = action
  514.     handle.render = {}
  515.    
  516.     handle.render.getWidth = function()
  517.         return #(handle.state and handle.text1 or handle.text2)
  518.     end
  519.    
  520.     handle.render.getHeight = function()
  521.         return 1
  522.     end
  523.    
  524.     handle.setText1 = function(text1)
  525.         handle.text1 = text1
  526.         return handle
  527.     end
  528.    
  529.     handle.setText2 = function(text2)
  530.         handle.text2 = text2
  531.         return handle
  532.     end
  533.    
  534.     handle.getText = function()
  535.         return handle.text1, handle.text1
  536.     end
  537.    
  538.     handle.getState = function()
  539.         return handle.state
  540.     end
  541.    
  542.     handle.setState = function(state)
  543.         handle.state = state
  544.         return handle
  545.     end
  546.    
  547.     handle.render.render = function(ctx)
  548.         ctx.write(1,1,handle.state and handle.text1 or handle.text2)
  549.     end
  550.    
  551.     handle.event = {}
  552.     handle.event.click = function(x,y,master)
  553.         master.setFocused(handle)
  554.         if handle.action then
  555.             handle.state = not handle.state
  556.             handle.action(handle,handle.state,x,y)
  557.         end
  558.     end
  559.    
  560.     return handle
  561. end
  562.  
  563. function newEventLoop(handler)
  564.     local handle = {}
  565.     handle.handler = handler
  566.     handle.running = true
  567.    
  568.     handle.click = {}
  569.     handle.key = {}
  570.    
  571.     handle.setClickEvent = function(name, processor)
  572.         handle.click.name = name
  573.         handle.click.processor = processor
  574.         return handle
  575.     end
  576.    
  577.     handle.setKeyEvent = function(name, processor)
  578.         handle.key.name = name
  579.         handle.key.processor = processor
  580.         return handle
  581.     end
  582.    
  583.     handle.stop = function()
  584.         handle.running = false
  585.         return handle
  586.     end
  587.    
  588.     handle.start = function()
  589.         while handle.running do
  590.             local e = {os.pullEvent()}
  591.             if handle.click.name and handle.click.name == e[1] then
  592.                 local x,y
  593.                 if handle.click.processor then
  594.                     x,y = handle.click.processor(unpack(e))
  595.                 else
  596.                     x = e[3]
  597.                     y = e[4]
  598.                 end
  599.                 if x and y then
  600.                     handle.handler.event.click(x,y)
  601.                 end
  602.         elseif handle.key.name and handle.key.name == e[1] then
  603.                 local c
  604.                 if handle.click.processor then
  605.                     c = handle.key.processor(unpack(e))
  606.                 else
  607.                     c = e[2]
  608.                 end
  609.  
  610.                 handle.handler.event.key(c)
  611.             end
  612.         end
  613.     end
  614.  
  615.    
  616.     return handle
  617. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement