Advertisement
Alakazard12

GUI

Aug 18th, 2013
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 45.41 KB | None | 0 0
  1. -- Nova GUI API lightweight edition. By alakazard12
  2. -- Feel free to use this as long as you give me credit, but there's no documentation yet
  3.  
  4. local LGW, LGH = term.getSize()
  5.  
  6. local VColors = {}
  7. local VClasses = {
  8.     ["TextButton"] = true;
  9.     ["TextLabel"] = true;
  10.     ["TextBox"] = true;
  11.     ["ImageButton"] = true;
  12.     ["ImageLabel"] = true;
  13. }
  14.  
  15. for _,Color in pairs(colors) do
  16.     if type(Color) == "number" then
  17.         VColors[Color] = true
  18.     end
  19. end
  20.  
  21. local function Round(Number)
  22.     return math.floor(Number + 0.5)
  23. end
  24.  
  25. local Check
  26. function Check(Object, BG, PX, PY, ChangeP)
  27.     if Object.Visible == true then
  28.         if Object.Class == "TextButton" or Object.Class == "TextBox" or Object.Class == "TextLabel" or Object.Class == "ImageButton" or Object.Class == "ImageLabel" then
  29.             -- if ChangeP ~= true then
  30.                 PX = PX + Object.Position[1] - 1
  31.                 PY = PY + Object.Position[2] - 1
  32.             -- end
  33.             if Object.ShowBackground == true then
  34.                 BG = Object.BackgroundColor
  35.             end
  36.             if Object.Class == "TextBox" and Object.Focused == true then
  37.                 BG = Object.ActiveTextColor
  38.             end
  39.             term.setBackgroundColor(BG)
  40.             for Y = 1, Object.Size[2] do
  41.                 term.setCursorPos(PX, PY + Y - 1)
  42.                 term.write(string.rep(" ", Object.Size[1]))
  43.             end
  44.  
  45.             if Object.Class == "ImageButton" or Object.Class == "ImageLabel" and Object.Image then
  46.                 paintutils.drawImage(Object.Image, PX, PY)
  47.             end
  48.  
  49.             if Object.Class == "TextButton" or Object.Class == "TextBox" or Object.Class == "TextLabel" then
  50.                 if Object.ShowText == true then
  51.                     term.setTextColor(Object.TextColor)
  52.  
  53.                     local Text = Object.Text
  54.                     if Object.TextChar then
  55.                         Text = string.rep(Object.TextChar, #Text)
  56.                     end
  57.  
  58.                     if Object.Focused == true then
  59.                         Text = Text .. " "
  60.                     end
  61.  
  62.                     if Object.MultiLine == true then
  63.                         local LastP = 1
  64.                         local LastS
  65.                         local Lines = {}
  66.                         local DidBreak = false
  67.                         local RPassed = false
  68.                         local ROnLine = false
  69.                         local ROnNum
  70.                         local RLine
  71.                         for On = 1, #Text do
  72.                             if Text:sub(On, On) == " " then
  73.                                 LastS = On
  74.                             end
  75.  
  76.                             if On - LastP + 2 > Object.Size[1] then
  77.                                 if #Lines == Object.Size[2] then
  78.                                     break
  79.                                 end
  80.  
  81.                                 if LastS then
  82.                                     table.insert(Lines, Text:sub(LastP, LastS))
  83.                                     LastP = LastS + 1
  84.                                 else
  85.                                     table.insert(Lines, Text:sub(LastP, On))
  86.                                     LastP = On + 1
  87.                                 end
  88.  
  89.                                 LastS = nil
  90.  
  91.                                 if #Lines == Object.Size[2] then
  92.                                     if Object.Focused == false then
  93.                                         DidBreak = true
  94.                                         break
  95.                                     else
  96.                                         table.remove(Lines, 1)
  97.                                     end
  98.                                 end
  99.                             end
  100.                         end
  101.  
  102.                         if DidBreak == false then
  103.                             table.insert(Lines, Text:sub(LastP))
  104.                         elseif Object.Focused == true then
  105.                             table.remove(Lines, 1)
  106.                             table.insert(Lines, Text:sub(LastP))
  107.                         end
  108.  
  109.                         local TextCount = 0
  110.  
  111.                         for _,Line in pairs(Lines) do
  112.                             local XA = 0
  113.                             local YA = 0
  114.  
  115.                             if Object.TextXAlignment == 0 then
  116.                                 XA = PX
  117.                             elseif Object.TextXAlignment == 1 then
  118.                                 XA = PX + Round((Object.Size[1] - #Line) / 2)
  119.                             else
  120.                                 XA = PX + Round(Object.Size[1] - #Line)
  121.                             end
  122.  
  123.                             if Object.TextYAlignment == 0 then
  124.                                 YA = PY + _ - 1
  125.                             elseif Object.TextYAlignment == 1 then
  126.                                 YA = PY + Round((Object.Size[2] - #Lines) / 2) + _ - 1
  127.                             else
  128.                                 YA = PY + Round(Object.Size[2] - #Lines) + _ - 1
  129.                             end
  130.  
  131.                             term.setCursorPos(XA, YA)
  132.                             term.write(Line)
  133.                         end
  134.                     else
  135.                         if #Text > Object.Size[1] then
  136.                             if Object.Focused == false then
  137.                                 Text = string.sub(Text, 1, Object.Size[1])
  138.                             -- elseif Object.RTextPos then
  139.                             -- Text = string.sub(Text, Object.RTextPos, Object.RTextPos + Object.Size[1] - 1)
  140.                             else
  141.                                 Text = string.sub(Text, #Text - Object.Size[1] + 1, #Text)
  142.                             end
  143.                         end
  144.  
  145.                         local XA = 0
  146.                         local YA = 0
  147.  
  148.                         if Object.TextXAlignment == 0 then
  149.                             XA = PX
  150.                         elseif Object.TextXAlignment == 1 then
  151.                             XA = PX + Round((Object.Size[1] - #Text) / 2)
  152.                         else
  153.                             XA = PX + Round(Object.Size[1] - #Text)
  154.                         end
  155.  
  156.                         if Object.TextYAlignment == 0 then
  157.                             YA = PY
  158.                         elseif Object.TextYAlignment == 1 then
  159.                             YA = PY + Round((Object.Size[2] - 1) / 2)
  160.                         else
  161.                             YA = PY + Round(Object.Size[2] - 1)
  162.                         end
  163.  
  164.                         term.setCursorPos(XA, YA)
  165.                         term.write(Text)
  166.                     end
  167.                     -- if Object.RTextPos == nil then
  168.                         Object.TextPos = {term.getCursorPos()}
  169.                         Object.TextPos[1] = Object.TextPos[1] - 1
  170.                     -- end
  171.                 end
  172.             end
  173.  
  174.             for _,Obj in pairs(Object.Children) do
  175.                 Check(Obj, BG, PX, PY)
  176.             end
  177.         end
  178.     end
  179. end
  180.  
  181. local function SortListen(Tbl)
  182.     local Clone = {}
  183.     for _,Item in pairs(Tbl) do
  184.         Clone[_] = Item
  185.     end
  186.     table.sort(Clone, function(A, B)
  187.         return A.ZIndex > B.ZIndex
  188.     end)
  189.     return Clone
  190. end
  191.  
  192. local function NewClass(Par, Class, Main)
  193.     if type(Class) ~= "string" then
  194.         error("bad argument #1: string expected, got " .. type(Class), 2)
  195.     end
  196.     if not VClasses[Class] then
  197.         error("Invalid class name.", 2)
  198.     end
  199.  
  200.     local PublicTable = {}
  201.     local LocalTable = {}
  202.  
  203.     LocalTable.Parent = Par
  204.     LocalTable.Class = Class
  205.  
  206.     function PublicTable:ClearChildren()
  207.         LocalTable.Children = {}
  208.     end
  209.  
  210.     if Class == "ImageButton" or Class == "ImageLabel" then
  211.         LocalTable.Size = {2, 2}
  212.         LocalTable.Position = {1, 1}
  213.         LocalTable.Visible = true
  214.         LocalTable.Active = true
  215.         LocalTable.Children = {}
  216.         LocalTable.ZIndex = 0
  217.         LocalTable.ShowBackground = true
  218.  
  219.         function PublicTable:Size(X, Y)
  220.             if type(X) ~= "number" then
  221.                 error("bad argument #1: number expected, got " .. type(X), 2)
  222.             end
  223.             if type(Y) ~= "number" then
  224.                 error("bad argument #2: number expected, got " .. type(Y), 2)
  225.             end
  226.  
  227.             LocalTable.Size = {X, Y}
  228.         end
  229.  
  230.         function PublicTable:Image(Image)
  231.             if type(Image) ~= "table" then
  232.                 error("bad argument #1: table expected, got " .. type(Image), 2)
  233.             end
  234.  
  235.             LocalTable.Image = Image
  236.         end
  237.  
  238.         function PublicTable:GetSize()
  239.             return unpack(LocalTable.Size)
  240.         end
  241.  
  242.         function PublicTable:ShowBackground(Bool)
  243.             if type(Bool) ~= "boolean" then
  244.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  245.             end
  246.             LocalTable.ShowBackground = Bool
  247.         end
  248.  
  249.         function PublicTable:GetShowBackground()
  250.             return LocalTable.ShowBackground
  251.         end
  252.  
  253.         function PublicTable:ZIndex(Number)
  254.             if type(Number) ~= "number" then
  255.                 error("bad argument #1: number expected, got " .. type(Number), 2)
  256.             end
  257.             LocalTable.ZIndex = Number
  258.             table.sort(Par.Children, function(A, B)
  259.                 return A.ZIndex < B.ZIndex
  260.             end)
  261.         end
  262.  
  263.         function PublicTable:GetZIndex()
  264.             return LocalTable.ZIndex
  265.         end
  266.  
  267.         function PublicTable:Position(X, Y)
  268.             if type(X) ~= "number" then
  269.                 error("bad argument #1: number expected, got " .. type(X), 2)
  270.             end
  271.             if type(Y) ~= "number" then
  272.                 error("bad argument #2: number expected, got " .. type(Y), 2)
  273.             end
  274.             LocalTable.Position = {X, Y}
  275.         end
  276.  
  277.         function PublicTable:GetPosition()
  278.             return unpack(LocalTable.Position)
  279.         end
  280.  
  281.         function PublicTable:Visible(Bool)
  282.             if type(Bool) ~= "boolean" then
  283.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  284.             end
  285.             LocalTable.Visible = Bool
  286.         end
  287.  
  288.         function PublicTable:GetVisible()
  289.             return LocalTable.Visible
  290.         end
  291.  
  292.         function PublicTable:Active(Bool)
  293.             if type(Bool) ~= "boolean" then
  294.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  295.             end
  296.             LocalTable.Active = Bool
  297.         end
  298.  
  299.         function PublicTable:GetActive()
  300.             return LocalTable.Active
  301.         end
  302.  
  303.         function PublicTable:BackgroundColor(Color)
  304.             if type(Color) ~= "number" then
  305.                 error("bad argument #1: number expected, got " .. type(Color), 2)
  306.             end
  307.             if VColors[Color] ~= true then
  308.                 error("Invalid color.", 2)
  309.             end
  310.             LocalTable.BackgroundColor = Color
  311.         end
  312.  
  313.         function PublicTable:GetBackgroundColor()
  314.             return LocalTable.BackgroundColor
  315.         end
  316.  
  317.         if Class == "ImageButton" then
  318.             function PublicTable:Button1Click(Function)
  319.                 if Function == nil then
  320.                     LocalTable.Button1Click = nil
  321.                 else
  322.                     if type(Function) ~= "function" then
  323.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  324.                     end
  325.                     LocalTable.Button1Click = Function
  326.                 end
  327.             end
  328.  
  329.             function PublicTable:GetButton1Click()
  330.                 return LocalTable.Button1Click
  331.             end
  332.  
  333.             function PublicTable:MouseScroll(Function)
  334.                 if Function == nil then
  335.                     LocalTable.MouseScroll = nil
  336.                 else
  337.                     if type(Function) ~= "function" then
  338.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  339.                     end
  340.                     LocalTable.MouseScroll = Function
  341.                 end
  342.             end
  343.  
  344.             function PublicTable:GetMouseScroll()
  345.                 return LocalTable.MouseScroll
  346.             end
  347.  
  348.             function PublicTable:MouseDrag1(Function)
  349.                 if Function == nil then
  350.                     LocalTable.MouseDrag1 = nil
  351.                 else
  352.                     if type(Function) ~= "function" then
  353.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  354.                     end
  355.                     LocalTable.MouseDrag1 = Function
  356.                 end
  357.             end
  358.  
  359.             function PublicTable:GetMouseDrag1()
  360.                 return LocalTable.MouseDrag1
  361.             end
  362.  
  363.             function PublicTable:MouseDrag2(Function)
  364.                 if Function == nil then
  365.                     LocalTable.MouseDrag2 = nil
  366.                 else
  367.                     if type(Function) ~= "function" then
  368.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  369.                     end
  370.                     LocalTable.MouseDrag2 = Function
  371.                 end
  372.             end
  373.  
  374.             function PublicTable:GetMouseDrag2()
  375.                 return LocalTable.MouseDrag2
  376.             end
  377.  
  378.             function PublicTable:MouseDrag3(Function)
  379.                 if Function == nil then
  380.                     LocalTable.MouseDrag3 = nil
  381.                 else
  382.                     if type(Function) ~= "function" then
  383.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  384.                     end
  385.                     LocalTable.MouseDrag3 = Function
  386.                 end
  387.             end
  388.  
  389.             function PublicTable:GetMouseDrag3()
  390.                 return LocalTable.MouseDrag3
  391.             end
  392.  
  393.             function PublicTable:Button2Click(Function)
  394.                 if Function == nil then
  395.                     LocalTable.Button2Click = nil
  396.                 else
  397.                     if type(Function) ~= "function" then
  398.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  399.                     end
  400.                     LocalTable.Button2Click = Function
  401.                 end
  402.             end
  403.  
  404.             function PublicTable:GetButton2Click()
  405.                 return LocalTable.Button2Click
  406.             end
  407.  
  408.             function PublicTable:Button3Click(Function)
  409.                 if Function == nil then
  410.                     LocalTable.Button3Click = nil
  411.                 else
  412.                     if type(Function) ~= "function" then
  413.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  414.                     end
  415.                     LocalTable.Button3Click = Function
  416.                 end
  417.             end
  418.  
  419.             function PublicTable:GetButton3Click()
  420.                 return LocalTable.Button3Click
  421.             end
  422.         end
  423.  
  424.         function PublicTable:New(Class)
  425.             return NewClass(LocalTable, Class, Main)
  426.         end
  427.  
  428.         function PublicTable:Draw()
  429.             local BG = Main.BackgroundColor
  430.             local FoundBG = false
  431.             local PX, PY = 1, 1
  432.  
  433.             local On = LocalTable.Parent
  434.             while On ~= Main do
  435.                 PX = PX + On.Position[1] - 1
  436.                 PY = PY + On.Position[2] - 1
  437.  
  438.                 if On.ShowBackground and FoundBG == false then
  439.                     BG = On.BackgroundColor
  440.                     FoundBG = true
  441.                 end
  442.                 On = On.Parent
  443.             end
  444.  
  445.             Check(LocalTable, BG, PX, PY)
  446.         end
  447.     elseif Class == "TextButton" or Class == "TextLabel" or Class == "TextBox" then
  448.         LocalTable.Size = {2, 2}
  449.         LocalTable.Position = {1, 1}
  450.         LocalTable.RawText = ""
  451.         LocalTable.Text = ""
  452.         LocalTable.BackgroundColor = colors.cyan
  453.         LocalTable.TextColor = colors.white
  454.         LocalTable.Visible = true
  455.         LocalTable.Active = true
  456.         LocalTable.Children = {}
  457.         LocalTable.ZIndex = 0
  458.         LocalTable.ShowBackground = true
  459.         LocalTable.ShowText = true
  460.         LocalTable.TextXAlignment = 1
  461.         LocalTable.TextYAlignment = 1
  462.         LocalTable.MultiLine = false
  463.  
  464.         if Class == "TextBox" then
  465.             LocalTable.ActiveTextColor = colors.blue
  466.             LocalTable.Overlap = false
  467.             LocalTable.Focused = false
  468.             LocalTable.ClearTextOnFocus = false
  469.             LocalTable.RTextPos = 8
  470.         end
  471.  
  472.         function PublicTable:TextXAlignment(Num)
  473.             if type(Num) == "string" then
  474.                 if Num:lower() == "left" then
  475.                     Num = 0
  476.                 elseif Num:lower() == "center" then
  477.                     Num = 1
  478.                 elseif Num:lower() == "right" then
  479.                     Num = 2
  480.                 else
  481.                     error("bad argument #1: '" .. Num .. "' is not a valid string", 2)
  482.                 end
  483.             end
  484.  
  485.             if type(Num) ~= "number" then
  486.                 error("bad argument #1: number expected, got " .. type(X), 2)
  487.             end
  488.             LocalTable.TextXAlignment = Num
  489.         end
  490.  
  491.         function PublicTable:GetTextXAlignment()
  492.             return LocalTable.TextXAlignment
  493.         end
  494.  
  495.         function PublicTable:TextYAlignment(Num)
  496.             if type(Num) == "string" then
  497.                 if Num:lower() == "top" then
  498.                     Num = 0
  499.                 elseif Num:lower() == "center" then
  500.                     Num = 1
  501.                 elseif Num:lower() == "bottom" then
  502.                     Num = 2
  503.                 else
  504.                     error("bad argument #1: '" .. Num .. "' is not a valid string", 2)
  505.                 end
  506.             end
  507.  
  508.             if type(Num) ~= "number" then
  509.                 error("bad argument #1: number expected, got " .. type(X), 2)
  510.             end
  511.             LocalTable.TextYAlignment = Num
  512.         end
  513.  
  514.         function PublicTable:GetTextYAlignment()
  515.             return LocalTable.TextYAlignment
  516.         end
  517.  
  518.         function PublicTable:Size(X, Y)
  519.             if type(X) ~= "number" then
  520.                 error("bad argument #1: number expected, got " .. type(X), 2)
  521.             end
  522.             if type(Y) ~= "number" then
  523.                 error("bad argument #2: number expected, got " .. type(Y), 2)
  524.             end
  525.  
  526.             LocalTable.Size = {X, Y}
  527.         end
  528.  
  529.         function PublicTable:GetSize()
  530.             return unpack(LocalTable.Size)
  531.         end
  532.  
  533.         function PublicTable:ShowBackground(Bool)
  534.             if type(Bool) ~= "boolean" then
  535.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  536.             end
  537.             LocalTable.ShowBackground = Bool
  538.         end
  539.  
  540.         function PublicTable:GetShowBackground()
  541.             return LocalTable.ShowBackground
  542.         end
  543.  
  544.         function PublicTable:ShowText(Bool)
  545.             if type(Bool) ~= "boolean" then
  546.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  547.             end
  548.             LocalTable.ShowText = Bool
  549.         end
  550.  
  551.         function PublicTable:GetShowText()
  552.             return LocalTable.ShowText
  553.         end
  554.  
  555.         function PublicTable:MultiLine(Bool)
  556.             if type(Bool) ~= "boolean" then
  557.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  558.             end
  559.             LocalTable.MultiLine = Bool
  560.         end
  561.  
  562.         function PublicTable:GetMultiLine()
  563.             return LocalTable.MultiLine
  564.         end
  565.  
  566.         function PublicTable:ZIndex(Number)
  567.             if type(Number) ~= "number" then
  568.                 error("bad argument #1: number expected, got " .. type(Number), 2)
  569.             end
  570.             LocalTable.ZIndex = Number
  571.             table.sort(Par.Children, function(A, B)
  572.                 return A.ZIndex < B.ZIndex
  573.             end)
  574.         end
  575.  
  576.         function PublicTable:GetZIndex()
  577.             return LocalTable.ZIndex
  578.         end
  579.  
  580.         function PublicTable:Position(X, Y)
  581.             if type(X) ~= "number" then
  582.                 error("bad argument #1: number expected, got " .. type(X), 2)
  583.             end
  584.             if type(Y) ~= "number" then
  585.                 error("bad argument #2: number expected, got " .. type(Y), 2)
  586.             end
  587.             LocalTable.Position = {X, Y}
  588.         end
  589.  
  590.         function PublicTable:GetPosition()
  591.             return unpack(LocalTable.Position)
  592.         end
  593.  
  594.         function PublicTable:Visible(Bool)
  595.             if type(Bool) ~= "boolean" then
  596.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  597.             end
  598.             LocalTable.Visible = Bool
  599.         end
  600.  
  601.         function PublicTable:GetVisible()
  602.             return LocalTable.Visible
  603.         end
  604.  
  605.         function PublicTable:Active(Bool)
  606.             if type(Bool) ~= "boolean" then
  607.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  608.             end
  609.             LocalTable.Active = Bool
  610.         end
  611.  
  612.         function PublicTable:GetActive()
  613.             return LocalTable.Active
  614.         end
  615.  
  616.         function PublicTable:Text(Text)
  617.             if Text == nil then
  618.                 Text = ""
  619.             end
  620.             if type(Text) ~= "string" then
  621.                 error("bad argument #1: string expected, got " .. type(Text), 2)
  622.             end
  623.             LocalTable.Text = Text
  624.         end
  625.  
  626.         function PublicTable:GetText()
  627.             return LocalTable.Text
  628.         end
  629.  
  630.         function PublicTable:BackgroundColor(Color)
  631.             if type(Color) ~= "number" then
  632.                 error("bad argument #1: number expected, got " .. type(Color), 2)
  633.             end
  634.             if VColors[Color] ~= true then
  635.                 error("Invalid color.", 2)
  636.             end
  637.             LocalTable.BackgroundColor = Color
  638.         end
  639.  
  640.         function PublicTable:GetBackgroundColor()
  641.             return LocalTable.BackgroundColor
  642.         end
  643.  
  644.         function PublicTable:TextColor(Color)
  645.             if type(Color) ~= "number" then
  646.                 error("bad argument #1: number expected, got " .. type(Color), 2)
  647.             end
  648.             if VColors[Color] ~= true then
  649.                 error("Invalid color.", 2)
  650.             end
  651.             LocalTable.TextColor = Color
  652.         end
  653.  
  654.         function PublicTable:GetTextColor()
  655.             return LocalTable.TextColor
  656.         end
  657.  
  658.         function PublicTable:MouseScroll(Function)
  659.                 if Function == nil then
  660.                     LocalTable.MouseScroll = nil
  661.                 else
  662.                 if type(Function) ~= "function" then
  663.                     error("bad argument #1: function expected, got " .. type(Function), 2)
  664.                 end
  665.                 LocalTable.MouseScroll = Function
  666.             end
  667.         end
  668.  
  669.         if Class == "TextButton" then
  670.             function PublicTable:Button1Click(Function)
  671.                 if Function == nil then
  672.                     LocalTable.Button1Click = nil
  673.                 else
  674.                     if type(Function) ~= "function" then
  675.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  676.                     end
  677.                     LocalTable.Button1Click = Function
  678.                 end
  679.             end
  680.  
  681.             function PublicTable:GetButton1Click()
  682.                 return LocalTable.Button1Click
  683.             end
  684.  
  685.             function PublicTable:GetMouseScroll()
  686.                 return LocalTable.MouseScroll
  687.             end
  688.  
  689.             function PublicTable:MouseDrag1(Function)
  690.                 if Function == nil then
  691.                     LocalTable.MouseDrag1 = nil
  692.                 else
  693.                     if type(Function) ~= "function" then
  694.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  695.                     end
  696.                     LocalTable.MouseDrag1 = Function
  697.                 end
  698.             end
  699.  
  700.             function PublicTable:GetMouseDrag1()
  701.                 return LocalTable.MouseDrag1
  702.             end
  703.  
  704.             function PublicTable:MouseDrag2(Function)
  705.                 if Function == nil then
  706.                     LocalTable.MouseDrag2 = nil
  707.                 else
  708.                     if type(Function) ~= "function" then
  709.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  710.                     end
  711.                     LocalTable.MouseDrag2 = Function
  712.                 end
  713.             end
  714.  
  715.             function PublicTable:GetMouseDrag2()
  716.                 return LocalTable.MouseDrag2
  717.             end
  718.  
  719.             function PublicTable:MouseDrag3(Function)
  720.                 if Function == nil then
  721.                     LocalTable.MouseDrag3 = nil
  722.                 else
  723.                     if type(Function) ~= "function" then
  724.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  725.                     end
  726.                     LocalTable.MouseDrag3 = Function
  727.                 end
  728.             end
  729.  
  730.             function PublicTable:GetMouseDrag3()
  731.                 return LocalTable.MouseDrag3
  732.             end
  733.  
  734.             function PublicTable:Button2Click(Function)
  735.                 if Function == nil then
  736.                     LocalTable.Button2Click = nil
  737.                 else
  738.                     if type(Function) ~= "function" then
  739.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  740.                     end
  741.                     LocalTable.Button2Click = Function
  742.                 end
  743.             end
  744.  
  745.             function PublicTable:GetButton2Click()
  746.                 return LocalTable.Button2Click
  747.             end
  748.  
  749.             function PublicTable:Button3Click(Function)
  750.                 if Function == nil then
  751.                     LocalTable.Button3Click = nil
  752.                 else
  753.                     if type(Function) ~= "function" then
  754.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  755.                     end
  756.                     LocalTable.Button3Click = Function
  757.                 end
  758.             end
  759.  
  760.             function PublicTable:GetButton3Click()
  761.                 return LocalTable.Button3Click
  762.             end
  763.         elseif Class == "TextBox" then
  764.             function PublicTable:ActiveTextColor(Color)
  765.                 if type(Color) ~= "number" then
  766.                     error("bad argument #1: number expected, got " .. type(Color), 2)
  767.                 end
  768.                 if VColors[Color] ~= true then
  769.                     error("Invalid color.", 2)
  770.                 end
  771.                 LocalTable.ActiveTextColor = Color
  772.             end
  773.  
  774.             function PublicTable:EnterPress(Function)
  775.                 if Function == nil then
  776.                     LocalTable.EnterPress = nil
  777.                 else
  778.                     if type(Function) ~= "function" then
  779.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  780.                     end
  781.                     LocalTable.EnterPress = Function
  782.                 end
  783.             end
  784.  
  785.             function PublicTable:GetEnterPress()
  786.                 return LocalTable.EnterPress
  787.             end
  788.  
  789.             function PublicTable:Overlap(Bool)
  790.                 if type(Bool) ~= "boolean" then
  791.                     error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  792.                 end
  793.                 LocalTable.Overlap = Bool
  794.             end
  795.  
  796.             function PublicTable:GetOverlap()
  797.                 return LocalTable.Overlap
  798.             end
  799.  
  800.             function PublicTable:ClearTextOnFocus(Bool)
  801.                 if type(Bool) ~= "boolean" then
  802.                     error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  803.                 end
  804.                 LocalTable.ClearTextOnFocus = Bool
  805.             end
  806.  
  807.             function PublicTable:GetClearTextOnFocus()
  808.                 return LocalTable.ClearTextOnFocus
  809.             end
  810.  
  811.             function PublicTable:Focus()
  812.                 Main.MakeFocus = LocalTable
  813.                 os.queueEvent("")
  814.             end
  815.  
  816.             function PublicTable:StopFocus()
  817.                 Main.StopFocus = true
  818.                 os.queueEvent("")
  819.             end
  820.  
  821.             function PublicTable:TextChar(Char)
  822.                 if Char == nil then
  823.                     LocalTable.TextChar = nil
  824.                 else
  825.                     if type(Char) ~= "string" then
  826.                         error("bad argument #1: string expected, got " .. type(Char), 2)
  827.                     end
  828.                     LocalTable.TextChar = Char
  829.                 end
  830.             end
  831.  
  832.             function PublicTable:GetTextChar()
  833.                 return LocalTable.TextChar
  834.             end
  835.         end
  836.  
  837.         function PublicTable:New(Class)
  838.             return NewClass(LocalTable, Class, Main)
  839.         end
  840.  
  841.         function PublicTable:Draw()
  842.             local BG = Main.BackgroundColor
  843.             local FoundBG = false
  844.             local PX, PY = 1, 1
  845.  
  846.             local On = LocalTable.Parent
  847.             while On ~= Main do
  848.                 PX = PX + On.Position[1] - 1
  849.                 PY = PY + On.Position[2] - 1
  850.  
  851.                 if On.ShowBackground and FoundBG == false then
  852.                     BG = On.BackgroundColor
  853.                     FoundBG = true
  854.                 end
  855.                 On = On.Parent
  856.             end
  857.  
  858.             Check(LocalTable, BG, PX, PY , true)
  859.         end
  860.     end
  861.  
  862.     LocalTable.Pub = PublicTable
  863.  
  864.     table.insert(Par.Children, LocalTable)
  865.  
  866.     function PublicTable:Destroy()
  867.         for _,Object in pairs(Par.Children) do
  868.             if LocalTable == Object then
  869.                 table.remove(Par.Children, _)
  870.                 break
  871.             end
  872.         end
  873.         LocalTable = nil
  874.         PublicTable = nil
  875.     end
  876.  
  877.     return PublicTable
  878. end
  879.  
  880. function NewUI()
  881.     local LocalTable = {}
  882.     local PublicTable = {}
  883.  
  884.     LocalTable.BackgroundColor = colors.red
  885.     LocalTable.Visible = true
  886.     LocalTable.Active = true
  887.     LocalTable.Children = {}
  888.     LocalTable.Listening = false
  889.  
  890.     function PublicTable:BackgroundColor(Color)
  891.         if type(Color) ~= "number" then
  892.             error("bad argument #1: number expected, got " .. type(Color), 2)
  893.         end
  894.         if not VColors[Color] then
  895.             error("Invalid color.", 2)
  896.         end
  897.         LocalTable.BackgroundColor = Color
  898.     end
  899.  
  900.     function PublicTable:New(Class)
  901.         return NewClass(LocalTable, Class, LocalTable)
  902.     end
  903.  
  904.     function PublicTable:ClearChildren()
  905.         LocalTable.Children = {}
  906.     end
  907.  
  908.     function PublicTable:ErrorHandle(Function)
  909.         if type(Function) ~= "function" and Function ~= nil then
  910.             error("bad argument #1: function expected, got " .. type(Function), 2)
  911.         end
  912.  
  913.         LocalTable.ErrorHandle = Function
  914.     end
  915.  
  916.     function PublicTable:Output(Function)
  917.         if type(Function) ~= "function" and Function ~= nil then
  918.             error("bad argument #1: function expected, got " .. type(Function), 2)
  919.         end
  920.  
  921.         LocalTable.Output = Function
  922.     end
  923.  
  924.     function PublicTable:Listen()
  925.         LocalTable.Listening = true
  926.         local ActiveBox
  927.         local LastClick1
  928.         local LastClick2
  929.         local LastClick3
  930.  
  931.         while LocalTable.Listening == true do
  932.             local Event, P1, P2, P3, P4, P5 = os.pullEvent()
  933.             if LocalTable.Listening == false then
  934.                 break
  935.             end
  936.  
  937.             if LocalTable.MakeFocus then
  938.                 if ActiveBox then
  939.                     ActiveBox.Focused = false
  940.                     ActiveBox.StopFocus = nil
  941.                     term.setCursorBlink(false)
  942.                     if ActiveBox.Overlap == true then
  943.                         ActiveBox.Pub:Draw()
  944.                     else
  945.                         PublicTable:Draw()
  946.                     end
  947.                 end
  948.                 ActiveBox = LocalTable.MakeFocus
  949.                 LocalTable.MakeFocus = nil
  950.                 ActiveBox.Focused = true
  951.                 if ActiveBox.Overlap == true then
  952.                     ActiveBox.Pub:Draw()
  953.                 else
  954.                     PublicTable:Draw()
  955.                 end
  956.  
  957.                 term.setCursorBlink(true)
  958.                 term.setTextColor(ActiveBox.TextColor)
  959.                 term.setCursorPos(unpack(ActiveBox.TextPos))
  960.             end
  961.  
  962.             if ActiveBox and ActiveBox.StopFocus then
  963.                 ActiveBox.Focused = false
  964.                 ActiveBox.StopFocus = nil
  965.                 term.setCursorBlink(false)
  966.                 if ActiveBox.Overlap == true then
  967.                     ActiveBox.Pub:Draw()
  968.                 else
  969.                     PublicTable:Draw()
  970.                 end
  971.                 if ActiveBox.EnterPress then
  972.                     ActiveBox.EnterPress()
  973.                 end
  974.                 ActiveBox = nil
  975.             end
  976.  
  977.             if LocalTable.Output then
  978.                 if LocalTable.ErrorHandle then
  979.                     local Success, Err = pcall(LocalTable.Output, Event, P1, P2, P3, P4, P5)
  980.                     if not Success then
  981.                         LocalTable.ErrorHandle(Err)
  982.                     end
  983.                 else
  984.                     LocalTable.Output(Event, P1, P2, P3, P4, P5)
  985.                 end
  986.             end
  987.  
  988.             if Event == "char" then
  989.                 if ActiveBox then
  990.                     ActiveBox.Text = ActiveBox.Text .. P1
  991.                     if ActiveBox.Overlap == true then
  992.                         ActiveBox.Pub:Draw()
  993.                     else
  994.                         PublicTable:Draw()
  995.                     end
  996.                     term.setTextColor(ActiveBox.TextColor)
  997.  
  998.                     term.setCursorPos(unpack(ActiveBox.TextPos))
  999.                 end
  1000.             elseif Event == "key" then
  1001.                 if ActiveBox then
  1002.                     if P1 == 14 then
  1003.                         if #ActiveBox.Text > 0 then
  1004.                             ActiveBox.Text = string.sub(ActiveBox.Text, 1, #ActiveBox.Text - 1)
  1005.                             if ActiveBox.Overlap == true then
  1006.                                 ActiveBox.Pub:Draw()
  1007.                             else
  1008.                                 PublicTable:Draw()
  1009.                             end
  1010.                             term.setTextColor(ActiveBox.TextColor)
  1011.  
  1012.                             term.setCursorPos(unpack(ActiveBox.TextPos))
  1013.                         end
  1014.                     elseif P1 == 28 then
  1015.                         ActiveBox.Focused = false
  1016.                         term.setCursorBlink(false)
  1017.  
  1018.                         if ActiveBox.Overlap == true then
  1019.                             ActiveBox.Pub:Draw()
  1020.                         else
  1021.                             PublicTable:Draw()
  1022.                         end
  1023.  
  1024.                         if ActiveBox.EnterPress then
  1025.                             if LocalTable.ErrorHandle then
  1026.                                 local Success, Err = pcall(ActiveBox.EnterPress)
  1027.                                 if not Success then
  1028.                                     LocalTable.ErrorHandle(Err)
  1029.                                 end
  1030.                             else
  1031.                                 ActiveBox.EnterPress()
  1032.                             end
  1033.                         end
  1034.                         ActiveBox = nil
  1035.                     end
  1036.                 end
  1037.             elseif Event == "mouse_scroll" then
  1038.                 local PStop = false
  1039.                 local Check
  1040.                 function Check(Object, Par, PX, PY)
  1041.                     if Object.Active == true then
  1042.                         if PStop == true then return end
  1043.                         if Object.Class == "TextLabel" or Object.Class == "TextButton" or Object.Class == "ImageButton" then
  1044.                             if P2 >= Object.Position[1] + PX - 1 and P2 <= Object.Position[1] + Object.Size[1] + PX - 2 and P3 >= Object.Position[2] + PY - 1 and P3 <= Object.Position[2] + Object.Size[2] + PY - 2 then
  1045.                                 if Object.MouseScroll then
  1046.                                     if LocalTable.ErrorHandle then
  1047.                                         local Success, Err = pcall(Object.MouseScroll, P1)
  1048.                                         if not Success then
  1049.                                             LocalTable.ErrorHandle(Err)
  1050.                                         end
  1051.                                     else
  1052.                                         Object.MouseScroll(P1)
  1053.                                     end
  1054.                                     PStop = true
  1055.                                 end
  1056.                             end
  1057.                         end
  1058.  
  1059.                         for _,Object in pairs(SortListen(Object.Children)) do
  1060.                             Check(Object, Object, PX + Object.Position[1] - 1, PY + Object.Position[2] - 1)
  1061.                         end
  1062.                     end
  1063.                 end
  1064.  
  1065.                 for _,Object in pairs(SortListen(LocalTable.Children))  do
  1066.                     Check(Object, LocalTable, 1, 1)
  1067.                 end
  1068.             elseif Event == "mouse_drag" then
  1069.                 if P1 == 1 and LastClick1 then
  1070.                     if LastClick1.MouseDrag1 then
  1071.                         if LocalTable.ErrorHandle then
  1072.                             local Success, Err = pcall(LastClick1.MouseDrag1, P2, P3)
  1073.                             if not Success then
  1074.                                 LocalTable.ErrorHandle(Err)
  1075.                             end
  1076.                         else
  1077.                             LastClick1.MouseDrag1(P2, P3)
  1078.                         end
  1079.                     end
  1080.                 elseif P1 == 2 and LastClick2 then
  1081.                     if LastClick2.MouseDrag2 then
  1082.                         if LocalTable.ErrorHandle then
  1083.                             local Success, Err = pcall(Object.MouseDrag2, P2, P3)
  1084.                             if not Success then
  1085.                                 LocalTable.ErrorHandle(Err)
  1086.                             end
  1087.                         else
  1088.                             Object.MouseDrag2(P2, P3)
  1089.                         end
  1090.                     end
  1091.                 elseif P1 == 3 and LastClick3 then
  1092.                     if LastClick3.MouseDrag3 then
  1093.                         if LocalTable.ErrorHandle then
  1094.                             local Success, Err = pcall(Object.MouseDrag3, P2, P3)
  1095.                             if not Success then
  1096.                                 LocalTable.ErrorHandle(Err)
  1097.                             end
  1098.                         else
  1099.                             Object.MouseDrag3(P2, P3)
  1100.                         end
  1101.                     end
  1102.                 end
  1103.             elseif Event == "mouse_click" then
  1104.                 LastClick1 = nil
  1105.                 LastClick2 = nil
  1106.                 LastClick3 = nil
  1107.                 local PStop = false
  1108.                 local StopFocus = true
  1109.                 local Check
  1110.                 function Check(Object, Par, PX, PY)
  1111.                     if Object.Active == true then
  1112.                         if PStop == true then return end
  1113.                         if Object.Class == "TextButton" or Object.Class == "ImageButton" or Object.Class == "TextBox" then
  1114.                             if P2 >= Object.Position[1] + PX - 1 and P2 <= Object.Position[1] + Object.Size[1] + PX - 2 and P3 >= Object.Position[2] + PY - 1 and P3 <= Object.Position[2] + Object.Size[2] + PY - 2 then
  1115.                                 if ActiveBox and Object ~= ActiveBox then
  1116.                                     ActiveBox.Focused = false
  1117.                                     ActiveBox.StopFocus = nil
  1118.                                     term.setCursorBlink(false)
  1119.                                     if ActiveBox.Overlap == true then
  1120.                                         ActiveBox.Pub:Draw()
  1121.                                     else
  1122.                                         PublicTable:Draw()
  1123.                                     end
  1124.  
  1125.                                     if ActiveBox.EnterPress then
  1126.                                         if LocalTable.ErrorHandle then
  1127.                                             local Success, Err = pcall(ActiveBox.EnterPress)
  1128.                                             if not Success then
  1129.                                                 LocalTable.ErrorHandle(Err)
  1130.                                             end
  1131.                                         else
  1132.                                             ActiveBox.EnterPress()
  1133.                                         end
  1134.                                     end
  1135.                                     ActiveBox = nil
  1136.                                 end
  1137.                                 if P1 == 1 then
  1138.                                     LastClick1 = Object
  1139.                                     if (Object.Class == "TextButton" or Object.Class == "ImageButton") and Object.Button1Click then
  1140.                                         if LocalTable.ErrorHandle then
  1141.                                             local Success, Err = pcall(Object.Button1Click, P2, P3)
  1142.                                             if not Success then
  1143.                                                 LocalTable.ErrorHandle(Err)
  1144.                                             end
  1145.                                         else
  1146.                                             Object.Button1Click(P2, P3)
  1147.                                         end
  1148.                                     elseif Object.Class == "TextBox" then
  1149.                                         ActiveBox = Object
  1150.                                         Object.Focused = true
  1151.  
  1152.                                         if Object.ClearTextOnFocus == true then
  1153.                                             Object.Text = ""
  1154.                                         end
  1155.  
  1156.                                         Object.RawText = ""
  1157.                                         Object.RTextPos = 6
  1158.  
  1159.                                         if Object.Overlap == true then
  1160.                                             Object.Pub:Draw()
  1161.                                         else
  1162.                                             PublicTable:Draw()
  1163.                                         end
  1164.                                         term.setCursorBlink(true)
  1165.                                         term.setTextColor(ActiveBox.TextColor)
  1166.                                         term.setCursorPos(unpack(Object.TextPos))
  1167.                                     end
  1168.                                 elseif P1 == 2 then
  1169.                                     LastClick2 = Object
  1170.                                     if (Object.Class == "TextButton" or Object.Class == "ImageButton") and Object.Button2Click then
  1171.                                         if LocalTable.ErrorHandle then
  1172.                                             local Success, Err = pcall(Object.Button2Click, P2, P3)
  1173.                                             if not Success then
  1174.                                                 LocalTable.ErrorHandle(Err)
  1175.                                             end
  1176.                                         else
  1177.                                             Object.Button2Click(P2, P3)
  1178.                                         end
  1179.                                     end
  1180.                                 elseif P1 == 3 then
  1181.                                     LastClick3 = Object
  1182.                                     if (Object.Class == "TextButton" or Object.Class == "ImageButton") and Object.Button3Click then
  1183.                                         if LocalTable.ErrorHandle then
  1184.                                             local Success, Err = pcall(Object.Button3Click, P2, P3)
  1185.                                             if not Success then
  1186.                                                 LocalTable.ErrorHandle(Err)
  1187.                                             end
  1188.                                         else
  1189.                                             Object.Button3Click(P2, P3)
  1190.                                         end
  1191.                                     end
  1192.                                 end
  1193.  
  1194.                                 PStop = true
  1195.                             end
  1196.                         end
  1197.  
  1198.                         for _,Object2 in pairs(SortListen(Object.Children)) do
  1199.                             Check(Object2, Object, PX + Object.Position[1] - 1, PY + Object.Position[2] - 1)
  1200.                         end
  1201.                     end
  1202.                 end
  1203.  
  1204.                 for _,Object in pairs(SortListen(LocalTable.Children))  do
  1205.                     Check(Object, LocalTable, 1, 1)
  1206.                 end
  1207.             end
  1208.         end
  1209.     end
  1210.  
  1211.     function PublicTable:StopListen()
  1212.         LocalTable.Listening = false
  1213.     end
  1214.  
  1215.     function PublicTable:Draw()
  1216.         term.setBackgroundColor(LocalTable.BackgroundColor)
  1217.         term.clear()
  1218.  
  1219.         for _,Item in ipairs(LocalTable.Children) do
  1220.             Check(Item, LocalTable.BackgroundColor, 1, 1)
  1221.         end
  1222.     end
  1223.     return PublicTable
  1224. end
  1225.  
  1226. -- End of alakazard12's Nova GUI API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement