Advertisement
XZTablets

Drawing Library

Aug 7th, 2020 (edited)
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.92 KB | None | 0 0
  1. local Drawing = {}
  2.  
  3. Drawing.Services = {
  4.     TextService = game:GetService("TextService"),
  5.     RunService = game:GetService("RunService")
  6. }
  7.  
  8. Drawing.Colours = {
  9.     Red = Color3.fromRGB(255,0,0),
  10.     Green = Color3.fromRGB(0,255,0),
  11.     Blue = Color3.fromRGB(0,0,255),
  12.     Pink = Color3.fromRGB(215,0,255),
  13.     White = function(Value)
  14.         return Value and Color3.fromRGB(Value,Value,Value) or Color3.fromRGB(255,255,255)
  15.     end,
  16.     Black = function(Value)
  17.         return Value and Color3.fromRGB(Value, Value, Value) or Color3.fromRGB(0,0,0)
  18.     end
  19. }
  20.  
  21. local ValidTypes = {
  22.     "line",
  23.     "text",
  24.     "circle",
  25.     "square",
  26.     "quad",
  27.     "triangle"
  28. }
  29.  
  30. local function TableContains(Table, Value)
  31.     for _, TableValue in next, Table do
  32.         if TableValue:sub(1, #Value):lower() == Value:lower() then
  33.             return TableValue
  34.         end
  35.     end
  36.     return false
  37. end
  38.  
  39. function Angle(vectorA, vectorB)
  40.    return math.acos(vectorA.Unit:Dot(vectorB.Unit))
  41. end
  42.  
  43. local function GetUserResolution()
  44.     local IsStudio = Drawing.Services.RunService:IsStudio()
  45.     local ScreenGui = Instance.new("ScreenGui")
  46.     ScreenGui.Parent = IsStudio and game.Players.LocalPlayer.PlayerGui or game:GetService("CoreGui")
  47.     local Size = ScreenGui.AbsoluteSize
  48.     ScreenGui:Destroy()
  49.     return Size
  50. end
  51.  
  52. function Drawing.new(Type)
  53.    
  54.     --[[
  55.     Types:
  56.     - Line
  57.     - Text
  58.     - Circle
  59.     - Square
  60.     - Quad
  61.     - Triangle
  62.     ]]
  63.    
  64.     local TargetType = TableContains(ValidTypes, Type)
  65.    
  66.     assert(TargetType, "Invalid type: '"..Type.."'.")
  67.    
  68.     if TargetType == "line" then
  69.        
  70.         local ProxyProperties = {
  71.             Visible = true,
  72.             Transparency = 0,
  73.             Color = Drawing.Colours.White(),
  74.             Thickness = 5,
  75.             From = Vector2.new(0,0),
  76.             To = Vector2.new(0,0)
  77.         }
  78.        
  79.         local LinePart = Instance.new("ImageLabel")
  80.         LinePart.Image = "rbxassetid://3570695787"
  81.         LinePart.ScaleType = Enum.ScaleType.Slice
  82.         LinePart.SliceCenter = Rect.new(100,100,100,100)
  83.         LinePart.SliceScale = 1
  84.         LinePart.BackgroundTransparency = 1
  85.         LinePart.AnchorPoint = Vector2.new(0.5, 0.5)
  86.        
  87.         local LineProperties = {
  88.             Functions = {
  89.                 Visible = function(self, bool)
  90.                     ProxyProperties.Visible = bool
  91.                     self.Visible = bool
  92.                 end,
  93.                 Transparency = function(self, float)
  94.                     ProxyProperties.Transparency = float
  95.                     self.ImageTransparency = ProxyProperties.Transparency
  96.                 end,
  97.                 Color = function(self, color3)
  98.                     ProxyProperties.Color = color3
  99.                     self.ImageColor3 = ProxyProperties.Color
  100.                 end,
  101.                 Thickness = function(self, value)
  102.                     ProxyProperties.Thickness = value
  103.                     self.Size = UDim2.fromOffset(ProxyProperties.Thickness, self.AbsoluteSize.Y)
  104.                 end,
  105.                 From = function(self, value)
  106.                     ProxyProperties.From = value
  107.                     local Position = (ProxyProperties.From + ProxyProperties.To) / 2
  108.                     self.Position = UDim2.fromOffset(Position.X, Position.Y)
  109.                     self.Size = UDim2.fromOffset(ProxyProperties.Thickness,(ProxyProperties.To - ProxyProperties.From).magnitude)
  110.                     local DY = ProxyProperties.To.Y - ProxyProperties.From.Y
  111.                     local DX = ProxyProperties.To.X - ProxyProperties.From.X
  112.                     local Delta = math.atan(DY/DX) * (180 / math.pi)
  113.                     self.Rotation = Delta + 90
  114.                 end,
  115.                 To = function(self, value)
  116.                     ProxyProperties.To = value
  117.                     local Position = (ProxyProperties.From + ProxyProperties.To) / 2
  118.                     self.Position = UDim2.fromOffset(Position.X, Position.Y)
  119.                     self.Size = UDim2.fromOffset(ProxyProperties.Thickness,(ProxyProperties.To - ProxyProperties.From).magnitude)
  120.                     local DY = ProxyProperties.To.Y - ProxyProperties.From.Y
  121.                     local DX = ProxyProperties.To.X - ProxyProperties.From.X
  122.                     local Delta = math.atan(DY/DX) * (180 / math.pi)
  123.                     self.Rotation = Delta + 90
  124.                 end
  125.             },
  126.             Remove = function()
  127.                 LinePart.Visible = false
  128.                 LinePart:Destroy()
  129.             end
  130.         }
  131.        
  132.         local MetaTable = {
  133.             __newindex = function(self, index, value)
  134.                 if self.Functions[index] then
  135.                     return self.Functions[index](LinePart, value)
  136.                 else
  137.                     LinePart[index] = value
  138.                 end
  139.             end,
  140.             __index = function(self, index)
  141.                 local property = rawget(self, index)
  142.                 if not property then
  143.                     return rawget(ProxyProperties, index)
  144.                 else
  145.                     return property
  146.                 end
  147.             end
  148.         }
  149.        
  150.         setmetatable(LineProperties, MetaTable)
  151.        
  152.         return LineProperties
  153.     elseif TargetType == "text" then
  154.        
  155.         local ProxyProperties = {
  156.             Visible = true,
  157.             Transparency = 0,
  158.             Color = Drawing.Colours.White(),
  159.             Thickness = 5,
  160.             Text = "",
  161.             Size = 14,
  162.             Center = false,
  163.             Outline = false,
  164.             OutlineColor = Drawing.Colours.Black(),
  165.             Position = Vector2.new(0,0),
  166.             Font = Enum.Font.SourceSans
  167.         }
  168.        
  169.         local TextPart = Instance.new("TextLabel")
  170.         TextPart.Text = "Text"
  171.         TextPart.BackgroundTransparency = 1
  172.        
  173.         local TextProperties = {
  174.             Functions = {
  175.                 Visible = function(self, bool)
  176.                     ProxyProperties.Visible = bool
  177.                     TextPart.Visible = bool
  178.                 end,
  179.                 Center = function(self, bool)
  180.                     ProxyProperties.Center = bool
  181.                 end,
  182.                 Transparency = function(self, float)
  183.                     ProxyProperties.Transparency = float
  184.                     self.TextTransparency = ProxyProperties.Transparency
  185.                 end,
  186.                 Color = function(self, color3)
  187.                     ProxyProperties.Color = color3
  188.                     self.TextColor3 = ProxyProperties.Color
  189.                 end,
  190.                 Thickness = function(self, value)
  191.                     ProxyProperties.Thickness = value
  192.                     self.Size = UDim2.fromOffset(ProxyProperties.Thickness, self.AbsoluteSize.Y)
  193.                 end,
  194.                 Size = function(self, value)
  195.                     ProxyProperties.Size = value
  196.                     self.TextSize = ProxyProperties.Size
  197.                 end,
  198.                 Outline = function(self, bool)
  199.                     ProxyProperties.Outline = bool
  200.                     self.TextStrokeTransparency = ProxyProperties.Outline and 0 or 1
  201.                 end,
  202.                 OutlineColor = function(self, color3)
  203.                     ProxyProperties.OutlineColor = color3
  204.                     self.TextStrokeColor3 = ProxyProperties.OutlineColor
  205.                 end,
  206.                 Position = function(self, vector2)
  207.                     local TextSizeX = Drawing.Services.TextService:GetTextSize(ProxyProperties.Text, ProxyProperties.Size, ProxyProperties.Font, Vector2.new(0,0)).X
  208.                     ProxyProperties.Position = vector2
  209.                     self.Position = UDim2.fromOffset(ProxyProperties.Position.X-(TextSizeX/4), ProxyProperties.Position.Y)
  210.                 end,
  211.                 Font = function(self, variant)
  212.                     ProxyProperties.Font = variant
  213.                     self.Font = ProxyProperties.Font
  214.                 end,
  215.                 Text = function(self, string)
  216.                     local TextSizeX = Drawing.Services.TextService:GetTextSize(string, ProxyProperties.Size, ProxyProperties.Font, Vector2.new(0,0)).X
  217.                     ProxyProperties.Size = UDim2.fromOffset(TextSizeX, ProxyProperties.Size)
  218.                     self.Size = ProxyProperties.Size
  219.                     ProxyProperties.Text = string
  220.                     self.Text = ProxyProperties.Text
  221.                 end
  222.             },
  223.             Remove = function()
  224.                 TextPart.Visible = false
  225.                 TextPart:Destroy()
  226.             end
  227.         }
  228.        
  229.         local MetaTable = {
  230.             __newindex = function(self, index, value)
  231.                 if rawget(rawget(self, "Functions"), index) then
  232.                     return self.Functions[index](TextPart, value)
  233.                 else
  234.                     TextPart[index] = value
  235.                 end
  236.             end,
  237.             __index = function(self, index)
  238.                 local check = rawget(self, index)
  239.                 if not check then
  240.                     local newcheck = TextPart[index]
  241.                     if newcheck then
  242.                         return newcheck
  243.                     else
  244.                         return nil
  245.                     end
  246.                 else
  247.                     return check
  248.                 end
  249.             end
  250.         }
  251.        
  252.         setmetatable(TextProperties, MetaTable)
  253.        
  254.         return TextProperties
  255.     elseif TargetType == "circle" then
  256.        
  257.         local ProxyProperties = {
  258.             Visible = true,
  259.             Transparency = 0,
  260.             Color = Drawing.Colours.White(),
  261.             Thickness = 5,
  262.             NumSides = 360,
  263.             Filled = false,
  264.             Position = Vector2.new(0,0),
  265.             Radius = 100,
  266.             Parent = script.Parent.Parent
  267.         }
  268.        
  269.         local function EV(V2)
  270.             return UDim2.fromOffset(V2.X, V2.Y)
  271.         end
  272.  
  273.         local SidesContainer = Instance.new("Frame")
  274.         SidesContainer.BackgroundTransparency = 1
  275.         SidesContainer.Position = EV(ProxyProperties.Position)
  276.         SidesContainer.Size = UDim2.fromOffset(ProxyProperties.Radius, ProxyProperties.Radius)
  277.        
  278.         local function Render(value)
  279.             ProxyProperties.NumSides = value
  280.             SidesContainer:Destroy()
  281.             SidesContainer = Instance.new("Frame")
  282.             SidesContainer.Name = "SidesContainer"
  283.             SidesContainer.BackgroundTransparency = 1
  284.             local GoPosition = EV(ProxyProperties.Position) - EV(Vector2.new(ProxyProperties.Radius/ProxyProperties.Thickness, ProxyProperties.Radius/ProxyProperties.Thickness))
  285.             SidesContainer.Position = GoPosition
  286.             SidesContainer.Size = UDim2.fromOffset(ProxyProperties.Radius, ProxyProperties.Radius)
  287.             SidesContainer.Parent = ProxyProperties.Parent
  288.             local Positions = {}
  289.             local Step = 360 / value
  290.             for I = 1, 360, Step do
  291.                 local GhostContainer = Instance.new("Frame")
  292.                 GhostContainer.Name = "GhostContainer"
  293.                 GhostContainer.BackgroundTransparency = 1
  294.                 GhostContainer.Size = UDim2.fromScale(1,1)
  295.                 GhostContainer.AnchorPoint = Vector2.new(0.5,0.5)
  296.                 GhostContainer.Position = UDim2.fromScale(0.5,0.5)
  297.                 GhostContainer.Rotation = I
  298.                 GhostContainer.Parent = SidesContainer
  299.                
  300.                 local Pixel = Instance.new("Frame")
  301.                 Pixel.Name = "Pixel"
  302.                 Pixel.Size = UDim2.fromOffset(ProxyProperties.Thickness, ProxyProperties.Thickness)
  303.                 Pixel.Position = UDim2.new(0.5,-ProxyProperties.Thickness/2)
  304.                 Pixel.BackgroundTransparency = 1
  305.                 Pixel.Parent = GhostContainer
  306.                
  307.                 table.insert(Positions, Pixel.AbsolutePosition)
  308.                
  309.                 GhostContainer:Destroy()
  310.             end
  311.             for I, P in next, Positions do
  312.                 local Next = Positions[I + 1] or Positions[1]
  313.                 local NewLine = Drawing.new("li")
  314.                 NewLine.From = P - SidesContainer.AbsolutePosition
  315.                 NewLine.To = Next - SidesContainer.AbsolutePosition
  316.                 NewLine.Thickness = ProxyProperties.Thickness
  317.                 NewLine.Color = ProxyProperties.Color
  318.                 NewLine.Transparency = ProxyProperties.Transparency
  319.                 NewLine.Parent = SidesContainer
  320.             end
  321.         end
  322.        
  323.         local CircleProperties = {
  324.             Functions = {
  325.                 Visible = function(self, bool)
  326.                     ProxyProperties.Visible = bool
  327.                     for _, E in next, SidesContainer:GetChildren() do
  328.                         E.Visible = ProxyProperties.Visible
  329.                     end
  330.                 end,
  331.                 Transparency = function(self, float)
  332.                     ProxyProperties.Transparency = float
  333.                     for _, E in next, SidesContainer:GetChildren() do
  334.                         E.BackgroundTransparency = ProxyProperties.Transparency
  335.                     end
  336.                 end,
  337.                 Color = function(self, color3)
  338.                     ProxyProperties.Color = color3
  339.                     for _, E in next, SidesContainer:GetChildren() do
  340.                         E.BackgroundColor3 = ProxyProperties.Color
  341.                     end
  342.                 end,
  343.                 Thickness = function(self, value)
  344.                     ProxyProperties.Thickness = value
  345.                     for _, E in next, SidesContainer:GetChildren() do
  346.                         E.Size = UDim2.fromOffset(ProxyProperties.Thickness, E.AbsoluteSize.Y)
  347.                     end
  348.                 end,
  349.                 Position = function(self, vector2)
  350.                     ProxyProperties.Position = vector2
  351.                     Render(ProxyProperties.NumSides)
  352.                 end,
  353.                 NumSides = function(self, value)
  354.                     ProxyProperties.NumSides = value
  355.                     Render(value)
  356.                 end,
  357.                 Radius = function(self, value)
  358.                     ProxyProperties.Radius = value
  359.                     Render(ProxyProperties.NumSides)
  360.                 end,
  361.                 Parent = function(self, instance)
  362.                     ProxyProperties.Parent = instance
  363.                     SidesContainer.Parent = ProxyProperties.Parent
  364.                 end
  365.             },
  366.             Remove = function()
  367.                 SidesContainer.Visible = false
  368.                 SidesContainer:Destroy()
  369.             end
  370.         }
  371.        
  372.         local MetaTable = {
  373.             __newindex = function(self, index, value)
  374.                 if self.Functions[index] then
  375.                     return self.Functions[index](nil, value)
  376.                 else
  377.                     SidesContainer[index] = value
  378.                 end
  379.             end,
  380.             __index = function(self, index)
  381.                 local property = rawget(self, index)
  382.                 if not property then
  383.                     return rawget(ProxyProperties, index)
  384.                 else
  385.                     return property
  386.                 end
  387.             end
  388.         }
  389.        
  390.         setmetatable(CircleProperties, MetaTable)
  391.        
  392.         return CircleProperties
  393.     elseif TargetType == "square" then
  394.        
  395.         local ProxyProperties = {
  396.             Visible = true,
  397.             Transparency = 0,
  398.             Color = Drawing.Colours.White(),
  399.             Thickness = 5,
  400.             Size = 100,
  401.             Position = Vector2.new(0,0),
  402.             Filled = false
  403.         }
  404.     end
  405. end
  406.  
  407. return Drawing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement