Advertisement
Alakazard12

Drag GUI

Jul 5th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.93 KB | None | 0 0
  1. -- Nova GUI API + lightweight edition + detailed errors. By alakazard12.
  2. -- Don't use this, it's for private use >_>.
  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.     local Hag = Number - math.floor(Number)
  23.     if Hag > 0.5 then
  24.         return math.ceil(Number)
  25.     else
  26.         return math.floor(Number)
  27.     end
  28. end
  29.  
  30. local Check
  31. function Check(Object, BG, PX, PY)
  32.     if Object.Visible == true then
  33.         if Object.Class == "TextButton" or Object.Class == "TextBox" or Object.Class == "TextLabel" then
  34.             PX = PX + Object.Position[1] - 1
  35.             PY = PY + Object.Position[2] - 1
  36.             if Object.ShowBackground == true then
  37.                 BG = Object.BackgroundColor
  38.             end
  39.             if Object.Class == "TextBox" and Object.Focused == true then
  40.                 BG = Object.ActiveTextColor
  41.             end
  42.             term.setBackgroundColor(BG)
  43.             for Y = 1, Object.Size[2] do
  44.                 term.setCursorPos(PX, PY + Y - 1)
  45.                 term.write(string.rep(" ", Object.Size[1]))
  46.             end
  47.  
  48.             if Object.ShowText == true then
  49.                 term.setTextColor(Object.TextColor)
  50.  
  51.                 local Text = Object.Text
  52.                 if Object.TextChar then
  53.                     Text = string.rep(Object.TextChar, #Text)
  54.                 end
  55.  
  56.                 if Object.Focused == true then
  57.                     Text = Text .. " "
  58.                 end
  59.  
  60.                 if Object.MultiLine == true then
  61.                     local LastP = 1
  62.                     local LastS
  63.                     local Lines = {}
  64.                     local DidBreak = false
  65.                     local RPassed = false
  66.                     local ROnLine = false
  67.                     local ROnNum
  68.                     local RLine
  69.                     for On = 1, #Text do
  70.                         if Text:sub(On, On) == " " then
  71.                             LastS = On
  72.                         end
  73.  
  74.                         if On - LastP + 2 > Object.Size[1] then
  75.                             if #Lines == Object.Size[2] then
  76.                                 break
  77.                             end
  78.  
  79.                             if LastS then
  80.                                 table.insert(Lines, Text:sub(LastP, LastS))
  81.                                 LastP = LastS + 1
  82.                             else
  83.                                 table.insert(Lines, Text:sub(LastP, On))
  84.                                 LastP = On + 1
  85.                             end
  86.  
  87.                             LastS = nil
  88.  
  89.                             if #Lines == Object.Size[2] then
  90.                                 if Object.Focused == false then
  91.                                     DidBreak = true
  92.                                     break
  93.                                 else
  94.                                     table.remove(Lines, 1)
  95.                                 end
  96.                             end
  97.                         end
  98.                     end
  99.  
  100.                     if DidBreak == false then
  101.                         table.insert(Lines, Text:sub(LastP))
  102.                     elseif Object.Focused == true then
  103.                         table.remove(Lines, 1)
  104.                         table.insert(Lines, Text:sub(LastP))
  105.                     end
  106.  
  107.                     local TextCount = 0
  108.  
  109.                     for _,Line in pairs(Lines) do
  110.                         local XA = 0
  111.                         local YA = 0
  112.  
  113.                         if Object.TextXAlignment == 0 then
  114.                             XA = PX
  115.                         elseif Object.TextXAlignment == 1 then
  116.                             XA = PX + Round((Object.Size[1] - #Line) / 2)
  117.                         else
  118.                             XA = PX + Round(Object.Size[1] - #Line)
  119.                         end
  120.  
  121.                         if Object.TextYAlignment == 0 then
  122.                             YA = PY + _ - 1
  123.                         elseif Object.TextYAlignment == 1 then
  124.                             YA = PY + Round((Object.Size[2] - #Lines) / 2) + _ - 1
  125.                         else
  126.                             YA = PY + Round(Object.Size[2] - #Lines) + _ - 1
  127.                         end
  128.  
  129.                         term.setCursorPos(XA, YA)
  130.                         term.write(Line)
  131.                     end
  132.                 else
  133.                     if #Text > Object.Size[1] then
  134.                         if Object.Focused == false then
  135.                             Text = string.sub(Text, 1, Object.Size[1])
  136.                         -- elseif Object.RTextPos then
  137.                         -- Text = string.sub(Text, Object.RTextPos, Object.RTextPos + Object.Size[1] - 1)
  138.                         else
  139.                             Text = string.sub(Text, #Text - Object.Size[1] + 1, #Text)
  140.                         end
  141.                     end
  142.  
  143.                     local XA = 0
  144.                     local YA = 0
  145.  
  146.                     if Object.TextXAlignment == 0 then
  147.                         XA = PX
  148.                     elseif Object.TextXAlignment == 1 then
  149.                         XA = PX + Round((Object.Size[1] - #Text) / 2)
  150.                     else
  151.                         XA = PX + Round(Object.Size[1] - #Text)
  152.                     end
  153.  
  154.                     if Object.TextYAlignment == 0 then
  155.                         YA = PY
  156.                     elseif Object.TextYAlignment == 1 then
  157.                         YA = PY + Round((Object.Size[2] - 1) / 2)
  158.                     else
  159.                         YA = PY + Round(Object.Size[2] - 1)
  160.                     end
  161.  
  162.                     term.setCursorPos(XA, YA)
  163.                     term.write(Text)
  164.                 end
  165.                 -- if Object.RTextPos == nil then
  166.                     Object.TextPos = {term.getCursorPos()}
  167.                     Object.TextPos[1] = Object.TextPos[1] - 1
  168.                 -- end
  169.             end
  170.  
  171.             for _,Obj in pairs(Object.Children) do
  172.                 Check(Obj, BG, PX, PY)
  173.             end
  174.         end
  175.     end
  176. end
  177.  
  178. local function SortListen(Tbl)
  179.     local Clone = {}
  180.     for _,Item in pairs(Tbl) do
  181.         Clone[_] = Item
  182.     end
  183.     table.sort(Clone, function(A, B)
  184.         return A.ZIndex > B.ZIndex
  185.     end)
  186.     return Clone
  187. end
  188.  
  189. local function NewClass(Par, Class, Main)
  190.     if type(Class) ~= "string" then
  191.         error("bad argument #1: string expected, got " .. type(Class), 2)
  192.     end
  193.     if not VClasses[Class] then
  194.         error("Invalid class name.", 2)
  195.     end
  196.  
  197.     local PublicTable = {}
  198.     local LocalTable = {}
  199.  
  200.     function PublicTable:Destroy()
  201.         for _,Item in pairs(PublicTable) do
  202.             PublicTable[_] = nil
  203.         end
  204.         for _,Item in pairs(LocalTable) do
  205.             LocalTable[_] = nil
  206.         end
  207.  
  208.         for _,Object in pairs(Par.Children) do
  209.             if PublicTable == Object then
  210.                 table.remove(Par.Children, _)
  211.                 break
  212.             end
  213.         end
  214.         LocalTable = nil
  215.         PublicTable = nil
  216.     end
  217.  
  218.     LocalTable.Parent = Par
  219.     LocalTable.Class = Class
  220.  
  221.     if Class == "TextButton" or Class == "TextLabel" or Class == "TextBox" then
  222.         LocalTable.Size = {2, 2}
  223.         LocalTable.Position = {1, 1}
  224.         LocalTable.RawText = ""
  225.         LocalTable.Text = ""
  226.         LocalTable.BackgroundColor = colors.cyan
  227.         LocalTable.TextColor = colors.white
  228.         LocalTable.Visible = true
  229.         LocalTable.Active = true
  230.         LocalTable.Children = {}
  231.         LocalTable.ZIndex = 0
  232.         LocalTable.ShowBackground = true
  233.         LocalTable.ShowText = true
  234.         LocalTable.TextXAlignment = 1
  235.         LocalTable.TextYAlignment = 1
  236.         LocalTable.MultiLine = false
  237.  
  238.         if Class == "TextBox" then
  239.             LocalTable.ActiveTextColor = colors.blue
  240.             LocalTable.Overlap = false
  241.             LocalTable.Focused = false
  242.             LocalTable.ClearTextOnFocus = false
  243.             LocalTable.RTextPos = 8
  244.         end
  245.  
  246.         function PublicTable:TextXAlignment(Num)
  247.             if type(Num) == "string" then
  248.                 if Num:lower() == "left" then
  249.                     Num = 0
  250.                 elseif Num:lower() == "center" then
  251.                     Num = 1
  252.                 elseif Num:lower() == "right" then
  253.                     Num = 2
  254.                 else
  255.                     error("bad argument #1: '" .. Num .. "' is not a valid string", 2)
  256.                 end
  257.             end
  258.  
  259.             if type(Num) ~= "number" then
  260.                 error("bad argument #1: number expected, got " .. type(X), 2)
  261.             end
  262.             LocalTable.TextXAlignment = Num
  263.         end
  264.  
  265.         function PublicTable:GetTextXAlignment()
  266.             return LocalTable.TextXAlignment
  267.         end
  268.  
  269.         function PublicTable:TextYAlignment(Num)
  270.             if type(Num) == "string" then
  271.                 if Num:lower() == "top" then
  272.                     Num = 0
  273.                 elseif Num:lower() == "center" then
  274.                     Num = 1
  275.                 elseif Num:lower() == "bottom" then
  276.                     Num = 2
  277.                 else
  278.                     error("bad argument #1: '" .. Num .. "' is not a valid string", 2)
  279.                 end
  280.             end
  281.  
  282.             if type(Num) ~= "number" then
  283.                 error("bad argument #1: number expected, got " .. type(X), 2)
  284.             end
  285.             LocalTable.TextYAlignment = Num
  286.         end
  287.  
  288.         function PublicTable:GetTextYAlignment()
  289.             return LocalTable.TextYAlignment
  290.         end
  291.  
  292.         function PublicTable:Size(X, Y)
  293.             if type(X) ~= "number" then
  294.                 error("bad argument #1: number expected, got " .. type(X), 2)
  295.             end
  296.             if type(Y) ~= "number" then
  297.                 error("bad argument #2: number expected, got " .. type(Y), 2)
  298.             end
  299.  
  300.             LocalTable.Size = {X, Y}
  301.         end
  302.  
  303.         function PublicTable:GetSize()
  304.             return unpack(LocalTable.Size)
  305.         end
  306.  
  307.         function PublicTable:ShowBackground(Bool)
  308.             if type(Bool) ~= "boolean" then
  309.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  310.             end
  311.             LocalTable.ShowBackground = Bool
  312.         end
  313.  
  314.         function PublicTable:GetShowBackground()
  315.             return LocalTable.ShowBackground
  316.         end
  317.  
  318.         function PublicTable:ShowText(Bool)
  319.             if type(Bool) ~= "boolean" then
  320.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  321.             end
  322.             LocalTable.ShowText = Bool
  323.         end
  324.  
  325.         function PublicTable:GetShowText()
  326.             return LocalTable.ShowText
  327.         end
  328.  
  329.         function PublicTable:MultiLine(Bool)
  330.             if type(Bool) ~= "boolean" then
  331.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  332.             end
  333.             LocalTable.MultiLine = Bool
  334.         end
  335.  
  336.         function PublicTable:GetMultiLine()
  337.             return LocalTable.MultiLine
  338.         end
  339.  
  340.         function PublicTable:ZIndex(Number)
  341.             if type(Number) ~= "number" then
  342.                 error("bad argument #1: number expected, got " .. type(Number), 2)
  343.             end
  344.             LocalTable.ZIndex = Number
  345.             table.sort(Par.Children, function(A, B)
  346.                 return A.ZIndex < B.ZIndex
  347.             end)
  348.         end
  349.  
  350.         function PublicTable:GetZIndex()
  351.             return LocalTable.ZIndex
  352.         end
  353.  
  354.         function PublicTable:Position(X, Y)
  355.             if type(X) ~= "number" then
  356.                 error("bad argument #1: number expected, got " .. type(X), 2)
  357.             end
  358.             if type(Y) ~= "number" then
  359.                 error("bad argument #2: number expected, got " .. type(Y), 2)
  360.             end
  361.             LocalTable.Position = {X, Y}
  362.         end
  363.  
  364.         function PublicTable:GetPosition()
  365.             return unpack(LocalTable.Position)
  366.         end
  367.  
  368.         function PublicTable:Visible(Bool)
  369.             if type(Bool) ~= "boolean" then
  370.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  371.             end
  372.             LocalTable.Visible = Bool
  373.         end
  374.  
  375.         function PublicTable:GetVisible()
  376.             return LocalTable.Visible
  377.         end
  378.  
  379.         function PublicTable:Active(Bool)
  380.             if type(Bool) ~= "boolean" then
  381.                 error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  382.             end
  383.             LocalTable.Active = Bool
  384.         end
  385.  
  386.         function PublicTable:GetActive()
  387.             return LocalTable.Active
  388.         end
  389.  
  390.         function PublicTable:Text(Text)
  391.             if Text == nil then
  392.                 Text = ""
  393.             end
  394.             if type(Text) ~= "string" then
  395.                 error("bad argument #1: string expected, got " .. type(Text), 2)
  396.             end
  397.             LocalTable.Text = Text
  398.         end
  399.  
  400.         function PublicTable:GetText()
  401.             return LocalTable.Text
  402.         end
  403.  
  404.         function PublicTable:BackgroundColor(Color)
  405.             if type(Color) ~= "number" then
  406.                 error("bad argument #1: number expected, got " .. type(Color), 2)
  407.             end
  408.             if VColors[Color] ~= true then
  409.                 error("Invalid color.", 2)
  410.             end
  411.             LocalTable.BackgroundColor = Color
  412.         end
  413.  
  414.         function PublicTable:GetBackgroundColor()
  415.             return LocalTable.BackgroundColor
  416.         end
  417.  
  418.         function PublicTable:TextColor(Color)
  419.             if type(Color) ~= "number" then
  420.                 error("bad argument #1: number expected, got " .. type(Color), 2)
  421.             end
  422.             if VColors[Color] ~= true then
  423.                 error("Invalid color.", 2)
  424.             end
  425.             LocalTable.TextColor = Color
  426.         end
  427.  
  428.         function PublicTable:GetTextColor()
  429.             return LocalTable.TextColor
  430.         end
  431.  
  432.         if Class == "TextButton" then
  433.             function PublicTable:Button1Click(Function)
  434.                 if Function == nil then
  435.                     LocalTable.Button1Click = nil
  436.                 else
  437.                     if type(Function) ~= "function" then
  438.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  439.                     end
  440.                     LocalTable.Button1Click = Function
  441.                 end
  442.             end
  443.  
  444.             function PublicTable:GetButton1Click()
  445.                 return LocalTable.Button1Click
  446.             end
  447.  
  448.             function PublicTable:MouseScroll(Function)
  449.                 if Function == nil then
  450.                     LocalTable.MouseScroll = nil
  451.                 else
  452.                     if type(Function) ~= "function" then
  453.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  454.                     end
  455.                     LocalTable.MouseScroll = Function
  456.                 end
  457.             end
  458.  
  459.             function PublicTable:GetMouseScroll()
  460.                 return LocalTable.MouseScroll
  461.             end
  462.  
  463.             function PublicTable:MouseDrag1(Function)
  464.                 if Function == nil then
  465.                     LocalTable.MouseDrag1 = nil
  466.                 else
  467.                     if type(Function) ~= "function" then
  468.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  469.                     end
  470.                     LocalTable.MouseDrag1 = Function
  471.                 end
  472.             end
  473.  
  474.             function PublicTable:GetMouseDrag1()
  475.                 return LocalTable.MouseDrag1
  476.             end
  477.  
  478.             function PublicTable:MouseDrag2(Function)
  479.                 if Function == nil then
  480.                     LocalTable.MouseDrag2 = nil
  481.                 else
  482.                     if type(Function) ~= "function" then
  483.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  484.                     end
  485.                     LocalTable.MouseDrag2 = Function
  486.                 end
  487.             end
  488.  
  489.             function PublicTable:GetMouseDrag2()
  490.                 return LocalTable.MouseDrag2
  491.             end
  492.  
  493.             function PublicTable:MouseDrag3(Function)
  494.                 if Function == nil then
  495.                     LocalTable.MouseDrag3 = nil
  496.                 else
  497.                     if type(Function) ~= "function" then
  498.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  499.                     end
  500.                     LocalTable.MouseDrag3 = Function
  501.                 end
  502.             end
  503.  
  504.             function PublicTable:GetMouseDrag3()
  505.                 return LocalTable.MouseDrag3
  506.             end
  507.  
  508.             function PublicTable:Button2Click(Function)
  509.                 if Function == nil then
  510.                     LocalTable.Button2Click = nil
  511.                 else
  512.                     if type(Function) ~= "function" then
  513.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  514.                     end
  515.                     LocalTable.Button2Click = Function
  516.                 end
  517.             end
  518.  
  519.             function PublicTable:GetButton2Click()
  520.                 return LocalTable.Button2Click
  521.             end
  522.  
  523.             function PublicTable:Button3Click(Function)
  524.                 if Function == nil then
  525.                     LocalTable.Button3Click = nil
  526.                 else
  527.                     if type(Function) ~= "function" then
  528.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  529.                     end
  530.                     LocalTable.Button3Click = Function
  531.                 end
  532.             end
  533.  
  534.             function PublicTable:GetButton3Click()
  535.                 return LocalTable.Button3Click
  536.             end
  537.         elseif Class == "TextBox" then
  538.             function PublicTable:EnterPress(Function)
  539.                 if Function == nil then
  540.                     LocalTable.EnterPress = nil
  541.                 else
  542.                     if type(Function) ~= "function" then
  543.                         error("bad argument #1: function expected, got " .. type(Function), 2)
  544.                     end
  545.                     LocalTable.EnterPress = Function
  546.                 end
  547.             end
  548.  
  549.             function PublicTable:GetEnterPress()
  550.                 return LocalTable.EnterPress
  551.             end
  552.  
  553.             function PublicTable:Overlap(Bool)
  554.                 if type(Bool) ~= "boolean" then
  555.                     error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  556.                 end
  557.                 LocalTable.Overlap = Bool
  558.             end
  559.  
  560.             function PublicTable:GetOverlap()
  561.                 return LocalTable.Overlap
  562.             end
  563.  
  564.             function PublicTable:ClearTextOnFocus(Bool)
  565.                 if type(Bool) ~= "boolean" then
  566.                     error("bad argument #1: boolean expected, got " .. type(Bool), 2)
  567.                 end
  568.                 LocalTable.ClearTextOnFocus = Bool
  569.             end
  570.  
  571.             function PublicTable:GetClearTextOnFocus()
  572.                 return LocalTable.ClearTextOnFocus
  573.             end
  574.  
  575.             function PublicTable:Focus()
  576.                 Main.MakeFocus = LocalTable
  577.                 os.queueEvent("")
  578.             end
  579.  
  580.             function PublicTable:StopFocus()
  581.                 Main.StopFocus = true
  582.                 os.queueEvent("")
  583.             end
  584.  
  585.             function PublicTable:TextChar(Char)
  586.                 if Char == nil then
  587.                     LocalTable.TextChar = nil
  588.                 else
  589.                     if type(Char) ~= "string" then
  590.                         error("bad argument #1: string expected, got " .. type(Char), 2)
  591.                     end
  592.                     LocalTable.TextChar = Char
  593.                 end
  594.             end
  595.  
  596.             function PublicTable:GetTextChar()
  597.                 return LocalTable.TextChar
  598.             end
  599.         end
  600.  
  601.         function PublicTable:New(Class)
  602.             return NewClass(LocalTable, Class, Main)
  603.         end
  604.  
  605.         function PublicTable:Draw()
  606.             local BG = Main.BackgroundColor
  607.             local PX, PY = 1, 1
  608.  
  609.             local On = LocalTable.Parent
  610.             while On ~= Main do
  611.                 PX = PX + On.Position[1]
  612.                 PY = PY + On.Position[2]
  613.  
  614.                 if On.ShowBackground then
  615.                     BG = On.BackgroundColor
  616.                     break
  617.                 end
  618.                 On = On.Parent
  619.             end
  620.  
  621.             Check(LocalTable, BG, PX, PY)
  622.         end
  623.     end
  624.  
  625.     LocalTable.Pub = PublicTable
  626.  
  627.     table.insert(Par.Children, LocalTable)
  628.     return PublicTable
  629. end
  630.  
  631. local function NewUI()
  632.     local LocalTable = {}
  633.     local PublicTable = {}
  634.  
  635.     LocalTable.BackgroundColor = colors.red
  636.     LocalTable.Visible = true
  637.     LocalTable.Active = true
  638.     LocalTable.Children = {}
  639.     LocalTable.Listening = false
  640.  
  641.     function PublicTable:BackgroundColor(Color)
  642.         if type(Color) ~= "number" then
  643.             error("bad argument #1: number expected, got " .. type(Color), 2)
  644.         end
  645.         if not VColors[Color] then
  646.             error("Invalid color.", 2)
  647.         end
  648.         LocalTable.BackgroundColor = Color
  649.     end
  650.  
  651.     function PublicTable:New(Class)
  652.         return NewClass(LocalTable, Class, LocalTable)
  653.     end
  654.  
  655.     function PublicTable:Output(Function)
  656.         if type(Function) ~= "function" then
  657.             error("bad argument #1: function expected, got " .. type(Function), 2)
  658.         end
  659.  
  660.         LocalTable.Output = Function
  661.     end
  662.  
  663.     function PublicTable:Listen()
  664.         LocalTable.Listening = true
  665.         local ActiveBox
  666.         local LastClick1
  667.         local LastClick2
  668.         local LastClick3
  669.  
  670.         while LocalTable.Listening == true do
  671.             local Event, P1, P2, P3, P4, P5 = os.pullEvent()
  672.             if LocalTable.Listening == false then
  673.                 break
  674.             end
  675.  
  676.             if LocalTable.MakeFocus then
  677.                 if ActiveBox then
  678.                     ActiveBox.Focused = false
  679.                     ActiveBox.StopFocus = nil
  680.                     term.setCursorBlink(false)
  681.                     if ActiveBox.Overlap == true then
  682.                         ActiveBox.Pub:Draw()
  683.                     else
  684.                         PublicTable:Draw()
  685.                     end
  686.                 end
  687.                 ActiveBox = LocalTable.MakeFocus
  688.                 LocalTable.MakeFocus = nil
  689.                 ActiveBox.Focused = true
  690.                 if ActiveBox.Overlap == true then
  691.                     ActiveBox.Pub:Draw()
  692.                 else
  693.                     PublicTable:Draw()
  694.                 end
  695.  
  696.                 term.setCursorBlink(true)
  697.                 term.setCursorPos(unpack(ActiveBox.TextPos))
  698.             end
  699.  
  700.             if ActiveBox and ActiveBox.StopFocus then
  701.                 ActiveBox.Focused = false
  702.                 ActiveBox.StopFocus = nil
  703.                 term.setCursorBlink(false)
  704.                 if ActiveBox.Overlap == true then
  705.                     ActiveBox.Pub:Draw()
  706.                 else
  707.                     PublicTable:Draw()
  708.                 end
  709.                 if ActiveBox.EnterPress then
  710.                     ActiveBox.EnterPress()
  711.                 end
  712.             end
  713.  
  714.             if LocalTable.Output then
  715.                 LocalTable.Output(Event, P1, P2, P3, P4, P5)
  716.             end
  717.  
  718.             if Event == "char" then
  719.                 if ActiveBox then
  720.                     ActiveBox.Text = ActiveBox.Text .. P1
  721.                     if ActiveBox.Overlap == true then
  722.                         ActiveBox.Pub:Draw()
  723.                     else
  724.                         PublicTable:Draw()
  725.                     end
  726.  
  727.                     term.setCursorPos(unpack(ActiveBox.TextPos))
  728.                 end
  729.             elseif Event == "key" then
  730.                 if ActiveBox then
  731.                     if P1 == 14 then
  732.                         if #ActiveBox.Text > 0 then
  733.                             ActiveBox.Text = string.sub(ActiveBox.Text, 1, #ActiveBox.Text - 1)
  734.                             if ActiveBox.Overlap == true then
  735.                                 ActiveBox.Pub:Draw()
  736.                             else
  737.                                 PublicTable:Draw()
  738.                             end
  739.  
  740.                             term.setCursorPos(unpack(ActiveBox.TextPos))
  741.                         end
  742.                     elseif P1 == 28 then
  743.                         ActiveBox.Focused = false
  744.                         term.setCursorBlink(false)
  745.  
  746.                         if ActiveBox.Overlap == true then
  747.                             ActiveBox.Pub:Draw()
  748.                         else
  749.                             PublicTable:Draw()
  750.                         end
  751.  
  752.                         if ActiveBox.EnterPress then
  753.                             ActiveBox.EnterPress()
  754.                         end
  755.                     end
  756.                 end
  757.             elseif Event == "mouse_scroll" then
  758.                 local PStop = false
  759.                 local Check
  760.                 function Check(Object, Par, PX, PY)
  761.                     if Object.Active == true then
  762.                         if PStop == true then return end
  763.                         if Object.Class == "TextButton" then
  764.                             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
  765.                                 if Object.MouseScroll then
  766.                                     Object.MouseScroll(P1)
  767.                                 end
  768.  
  769.                                 PStop = true
  770.                             end
  771.                         end
  772.  
  773.                         for _,Object in pairs(SortListen(Object.Children)) do
  774.                             Check(Object, Object, PX + Object.Position[1] - 1, PY + Object.Position[2] - 1)
  775.                         end
  776.                     end
  777.                 end
  778.  
  779.                 for _,Object in pairs(SortListen(LocalTable.Children))  do
  780.                     Check(Object, LocalTable, 1, 1)
  781.                 end
  782.             elseif Event == "mouse_drag" then
  783.                 if P1 == 1 and LastClick1 then
  784.                     if LastClick1.MouseDrag1 then
  785.                         LastClick1.MouseDrag1(P2, P3)
  786.                     end
  787.                 elseif P1 == 2 and LastClick2 then
  788.                     if LastClick2.MouseDrag2 then
  789.                         LastClick2.MouseDrag2(P2, P3)
  790.                     end
  791.                 elseif P1 == 3 and LastClick3 then
  792.                     if LastClick3.MouseDrag3 then
  793.                         LastClick3.MouseDrag3(P2, P3)
  794.                     end
  795.                 end
  796.             elseif Event == "mouse_click" then
  797.                 LastClick1 = nil
  798.                 LastClick2 = nil
  799.                 LastClick3 = nil
  800.                 local PStop = false
  801.                 local StopFocus = true
  802.                 local Check
  803.                 function Check(Object, Par, PX, PY)
  804.                     if Object.Active == true then
  805.                         if PStop == true then return end
  806.                         if Object.Class == "TextButton" or Object.Class == "TextBox" then
  807.                             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
  808.                                 if ActiveBox and Object ~= ActiveBox then
  809.                                     ActiveBox.Focused = false
  810.                                     ActiveBox.StopFocus = nil
  811.                                     term.setCursorBlink(false)
  812.                                     if ActiveBox.Overlap == true then
  813.                                         ActiveBox.Pub:Draw()
  814.                                     else
  815.                                         PublicTable:Draw()
  816.                                     end
  817.  
  818.                                     if ActiveBox.EnterPress then
  819.                                         ActiveBox.EnterPress()
  820.                                     end
  821.                                 end
  822.                                 if P1 == 1 then
  823.                                     LastClick1 = Object
  824.                                     if Object.Class == "TextButton" and Object.Button1Click then
  825.                                         Object.Button1Click(P2, P3)
  826.                                     elseif Object.Class == "TextBox" then
  827.                                         ActiveBox = Object
  828.                                         Object.Focused = true
  829.  
  830.                                         if Object.ClearTextOnFocus == true then
  831.                                             Object.Text = ""
  832.                                         end
  833.  
  834.                                         Object.RawText = ""
  835.                                         Object.RTextPos = 6
  836.  
  837.                                         if Object.Overlap == true then
  838.                                             Object.Pub:Draw()
  839.                                         else
  840.                                             PublicTable:Draw()
  841.                                         end
  842.                                         term.setCursorBlink(true)
  843.                                         term.setCursorPos(unpack(Object.TextPos))
  844.                                     end
  845.                                 elseif P1 == 2 then
  846.                                     LastClick2 = Object
  847.                                     if Object.Class == "TextButton" and Object.Button2Click then
  848.                                         Object.Button2Click(P2, P3)
  849.                                     end
  850.                                 elseif P1 == 3 then
  851.                                     LastClick3 = Object
  852.                                     if Object.Class == "TextButton" and Object.Button3Click then
  853.                                         Object.Button3Click(P2, P3)
  854.                                     end
  855.                                 end
  856.  
  857.                                 PStop = true
  858.                             end
  859.                         end
  860.  
  861.                         for _,Object2 in pairs(SortListen(Object.Children)) do
  862.                             Check(Object2, Object, PX + Object.Position[1] - 1, PY + Object.Position[2] - 1)
  863.                         end
  864.                     end
  865.                 end
  866.  
  867.                 for _,Object in pairs(SortListen(LocalTable.Children))  do
  868.                     Check(Object, LocalTable, 1, 1)
  869.                 end
  870.             end
  871.         end
  872.     end
  873.  
  874.     function PublicTable:StopListen()
  875.         LocalTable.Listening = false
  876.     end
  877.  
  878.     function PublicTable:Draw()
  879.         term.setBackgroundColor(LocalTable.BackgroundColor)
  880.         term.clear()
  881.  
  882.         for _,Item in ipairs(LocalTable.Children) do
  883.             Check(Item, LocalTable.BackgroundColor, 1, 1)
  884.         end
  885.     end
  886.     return PublicTable
  887. end
  888.  
  889. -- End of Nova GUI API
  890.  
  891. local TestUI = NewUI()
  892.  
  893. local Win = TestUI:New("TextLabel")
  894. Win:Text("")
  895. Win:Size(10, 7)
  896. Win:BackgroundColor(colors.blue)
  897.  
  898. local Drag = Win:New("TextButton")
  899. Drag:Text("Drag")
  900. Drag:Size(10, 1)
  901. Drag:BackgroundColor(colors.purple)
  902.  
  903. local Re = Win:New("TextButton")
  904. Re:Text("%")
  905. Re:Size(1, 1)
  906. Re:Position(10, 7)
  907. Re:ShowBackground(false)
  908.  
  909. local Close = Win:New("TextButton")
  910. Close:Text("Close")
  911. Close:BackgroundColor(colors.blue)
  912. Close:Position(1, 2)
  913. Close:Size(10, 1)
  914.  
  915. local TB1 = Win:New("TextBox")
  916. TB1:Text("TB1")
  917. TB1:BackgroundColor(colors.black)
  918. TB1:Position(1, 3)
  919. TB1:Size(10, 1)
  920.  
  921. local TB2 = Win:New("TextBox")
  922. TB2:Text("TB2")
  923. TB2:BackgroundColor(colors.lime)
  924. TB2:Position(1, 4)
  925. TB2:Size(10, 1)
  926.  
  927. local PX, PY = 0, 0
  928.  
  929. Drag:Button1Click(function(X, Y)
  930.     local WX, WY = Win:GetPosition()
  931.     PX, PY = X - WX, Y - WY
  932. end)
  933.  
  934. Close:Button1Click(function(X, Y)
  935.     TestUI:StopListen()
  936. end)
  937.  
  938. Drag:MouseDrag1(function(X, Y)
  939.     Win:Position(X - PX, Y - PY)
  940.     TestUI:Draw()
  941. end)
  942.  
  943. Re:MouseDrag1(function(X, Y)
  944.     local WinPosition = {Win:GetPosition()}
  945.     local WinSize = {Win:GetSize()}
  946.     if X < WinPosition[1] + 4 then
  947.         X = WinPosition[1] + 4
  948.     end
  949.     if Y < WinPosition[2] + 4 then
  950.         Y = WinPosition[2] + 4
  951.     end
  952.  
  953.     Win:Size(X - WinPosition[1] + 1, Y - WinPosition[2] + 1)
  954.     Re:Position(Win:GetSize())
  955.     Drag:Size(X - WinPosition[1] + 1, 1)
  956.     Close:Size(X - WinPosition[1] + 1, 1)
  957.     TB1:Size(X - WinPosition[1] + 1, 1)
  958.     TB2:Size(X - WinPosition[1] + 1, 1)
  959.     TestUI:Draw()
  960. end)
  961.  
  962. -- But:Draw()
  963. TestUI:Draw()
  964. TestUI:Listen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement