Advertisement
Guest User

Face Selector

a guest
Aug 22nd, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.56 KB | None | 0 0
  1. local toolbar = plugin:CreateToolbar("Face Selector")
  2. local button = toolbar:CreateButton("Show Selected Face", "Shows selected face of models and baseparts. Can also display directional vectors.", "rbxassetid://5602410390")
  3. local mouse = plugin:GetMouse()
  4.  
  5. local runService = game:GetService("RunService")
  6. local selection = game:GetService("Selection")
  7. local coreGUI = game:GetService("CoreGui")
  8.  
  9. if coreGUI:FindFirstChild("Tukars|FaceSelectingGUI") then coreGUI["Tukars|FaceSelectingGUI"]:Destroy() end
  10.  
  11. local gui = script.Parent["Tukars|FaceSelectingGUI"]:Clone()
  12. gui.Parent = coreGUI
  13. local enabled = false
  14. local tweening = false
  15.  
  16.  
  17. local connections = {}
  18.  
  19. if coreGUI:FindFirstChild("Tukars|Visuals") then coreGUI["Tukars|Visuals"]:Destroy() end
  20. local visuals = script.Parent["Tukars|Visuals"]:Clone()
  21. visuals.Parent = coreGUI
  22. local faces = visuals.Face
  23. local direction = visuals.Direction
  24. local binded = false
  25. local selectedFace = nil
  26. local currentlySelected = nil
  27. local active = true
  28. local showDirectionalVectors = false
  29. local inBounds = false
  30.  
  31. local gColor = Enum.StudioStyleGuideColor
  32. local gModifier = Enum.StudioStyleGuideModifier
  33.  
  34. local function getUIColor(guideColor, guideModifier)
  35.     return settings().Studio.Theme:GetColor(gColor[guideColor], gModifier[guideModifier])
  36. end
  37.  
  38. local function updatePluginTheme()
  39.     gui.Main.BackgroundColor3 = getUIColor("MainBackground", "Default")
  40.     gui.Main.Face.TextColor3 = getUIColor("MainText", "Default")
  41.     for _, ui in pairs(gui.Main.FaceSelector:GetChildren()) do
  42.         if ui ~= gui.Main.FaceSelector.UIListLayout then
  43.             local typ = ui.Name == selectedFace and "Selected" or "Default"
  44.             ui.BackgroundColor3 = getUIColor("RibbonButton", typ)
  45.             ui.TextColor3 = getUIColor("MainText", typ)
  46.             ui.BorderColor3 = getUIColor("Border", "Default")
  47.         end
  48.     end
  49.     for _, ui in pairs(gui.Main.CheckBoxes:GetDescendants()) do
  50.         if ui.Name == "CheckBox" then
  51.             ui.BackgroundColor3 = getUIColor("RibbonButton", "Selected")
  52.         elseif ui.Name == "Display" then
  53.             ui.BackgroundColor3 = getUIColor("Button", "Selected")
  54.         elseif ui:IsA("TextLabel") then
  55.             ui.TextColor3 = getUIColor("MainText", "Default")
  56.         end
  57.     end
  58. end
  59. updatePluginTheme()
  60.  
  61. local v3 = Vector3.new
  62.  
  63. local function withinBounds(obj)
  64.     local tx,ty = obj.AbsolutePosition.X, obj.AbsolutePosition.Y
  65.     local bx,by = tx + obj.AbsoluteSize.X, ty + obj.AbsoluteSize.Y
  66.     return (mouse.X >= tx and mouse.X <= bx and mouse.Y >= ty and mouse.Y <= by)
  67. end
  68.  
  69. local function update()
  70.     for _, ui in pairs(gui.Main.FaceSelector:GetChildren()) do
  71.         if ui ~= gui.Main.FaceSelector.UIListLayout then
  72.             if withinBounds(ui) and ui.Name ~= selectedFace then
  73.                 ui.BackgroundColor3 = getUIColor("RibbonButton", "Hover")
  74.                 ui.TextColor3 = getUIColor("MainText", "Hover")
  75.             elseif ui.Name ~= selectedFace then
  76.                 ui.BackgroundColor3 = getUIColor("RibbonButton", "Default")
  77.                 ui.TextColor3 = getUIColor("MainText", "Default")
  78.             end
  79.         end
  80.     end
  81.     if withinBounds(gui.ActivationZone) then
  82.         if not inBounds then
  83.             gui.Main:TweenPosition(UDim2.new(0.5, 0, 0, 6), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.15, true)
  84.             inBounds = true
  85.         end
  86.     else
  87.         if inBounds then
  88.             gui.Main:TweenPosition(UDim2.new(0.5, 0, 0, -175), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.15, true)
  89.             inBounds = false
  90.         end
  91.     end
  92.    
  93. end
  94.  
  95. local faceOffsets = {
  96.     Back = v3(0,0,1),
  97.     Bottom = v3(0,-1,0),
  98.     Front = v3(0,0,-1),
  99.     Left = v3(-1,0,0),
  100.     Right = v3(1,0,0),
  101.     Top = v3(0,1,0),
  102. }
  103.  
  104. local function getObjSize()
  105.     local objCFrame, objSize
  106.     if currentlySelected:IsA("Model") then
  107.         return currentlySelected:GetBoundingBox()
  108.     elseif currentlySelected:IsA("BasePart") then
  109.         return currentlySelected.CFrame, currentlySelected.Size
  110.     end
  111. end
  112.  
  113. local function updateLookVectorText(upda)
  114.     if upda then
  115.         for _,t in pairs(gui.Main.CheckBoxes.Vectors:GetChildren()) do
  116.             local vector = currentlySelected.CFrame[t.Name]
  117.             for _,d in pairs(t:GetChildren()) do
  118.                 if d.Name ~= "Text" then
  119.                     d.Text = string.sub(tostring(vector[d.Name]), 0, 6)
  120.                 end
  121.             end
  122.         end
  123.     else
  124.         for _,t in pairs(gui.Main.CheckBoxes.Vectors:GetChildren()) do
  125.             for _,d in pairs(t:GetChildren()) do
  126.                 if d.Name ~= "Text" then
  127.                     d.Text = ""
  128.                 end
  129.             end
  130.         end
  131.     end
  132. end
  133.  
  134. local function updateObject()
  135.     local targfaces = {"Top", "Bottom", "Left", "Right"}
  136.     if currentlySelected then
  137.         local objCFrame, objSize = getObjSize()
  138.         if selectedFace then
  139.             local index = 1
  140.             for i,v in pairs(faceOffsets) do
  141.                 if v:Cross(faceOffsets[selectedFace]) ~= v3() then
  142.                     local targetAdornee = faces[targfaces[index]]
  143.                     targetAdornee.Adornee = currentlySelected
  144.                     local targOffset = faceOffsets[selectedFace] + v
  145.                     targetAdornee.SizeRelativeOffset = targOffset
  146.                     local targetSize = v:Cross(faceOffsets[selectedFace])
  147.                     local xsize = targetSize.X ~= 0 and objSize.X + 0.15 or 0.15
  148.                     local ysize = targetSize.Y ~= 0 and objSize.Y + 0.15 or 0.15
  149.                     local zsize = targetSize.Z ~= 0 and objSize.Z + 0.15 or 0.15
  150.                     targetAdornee.Size = v3(xsize, ysize, zsize)
  151.                     index = index + 1
  152.                 end
  153.             end
  154.         else
  155.             for i,v in pairs(targfaces) do
  156.                 local targetAdornee = faces[v]
  157.                 targetAdornee.Adornee = nil
  158.             end
  159.         end
  160.         updateLookVectorText(true)
  161.         local targetSize = math.max(objSize.X, objSize.Y, objSize.Z)
  162.         local directions = visuals.Direction
  163.         for i,v in pairs(visuals.Direction:GetChildren()) do
  164.             v.Adornee = currentlySelected
  165.         end
  166.         direction.lookVector.Size = v3(0.1, 0.1, targetSize)
  167.         direction.lookVector.SizeRelativeOffset = v3(0, 0, -targetSize/objSize.Z)
  168.         direction.rightVector.Size = v3(targetSize, 0.1, 0.1)
  169.         direction.rightVector.SizeRelativeOffset = v3(targetSize/objSize.X, 0, 0)
  170.         direction.upVector.Size = v3(0.1, targetSize, 0.1)
  171.         direction.upVector.SizeRelativeOffset = v3(0, targetSize/objSize.Y, 0)
  172.     else
  173.         for i,v in pairs(targfaces) do
  174.             faces[v].Adornee = nil
  175.         end
  176.         for i,v in pairs(visuals.Direction:GetChildren()) do
  177.             v.Adornee = nil
  178.         end
  179.         updateLookVectorText(false)
  180.     end
  181. end
  182.  
  183. local selectedObjectEvent
  184.  
  185. local function connectSelectionChanged()
  186.     for i,v in pairs(selection:Get()) do
  187.         if v:IsA("BasePart") or v:IsA("Model") then
  188.             currentlySelected = v
  189.             updateObject()
  190.             selectedObjectEvent = currentlySelected.Changed:Connect(updateObject)
  191.             break
  192.         end
  193.     end
  194.     table.insert(connections, selection.SelectionChanged:Connect(function()
  195.         currentlySelected = nil
  196.         for i,v in pairs(selection:Get()) do
  197.             if v:IsA("BasePart") or v:IsA("Model") then
  198.                 currentlySelected = v
  199.                 updateObject()
  200.                 selectedObjectEvent = currentlySelected.Changed:Connect(updateObject)
  201.                 break
  202.             end
  203.         end
  204.         if not currentlySelected then
  205.             if selectedObjectEvent then
  206.                 selectedObjectEvent:Disconnect()
  207.                 selectedObjectEvent = nil
  208.             end
  209.             updateObject()
  210.         end
  211.     end))
  212. end
  213.  
  214. local function startPlugin()
  215.     plugin:Activate(true)
  216.     gui.Enabled = true
  217.     binded = true
  218.     runService:BindToRenderStep("FaceSelector by Tukars", Enum.RenderPriority.Last.Value, update)
  219.     table.insert(connections, settings().Studio.ThemeChanged:Connect(updatePluginTheme))
  220.     tweening = true
  221.     connectSelectionChanged()
  222.     for _, ui in pairs(gui.Main.FaceSelector:GetChildren()) do
  223.         if ui ~= gui.Main.FaceSelector.UIListLayout then
  224.             table.insert(connections, ui.MouseButton1Click:Connect(function()
  225.                 if gui.Main.FaceSelector:FindFirstChild("selectedFace") then
  226.                     gui.Main.FaceSelector[selectedFace].BackgroundColor3 = getUIColor("RibbonButton", "Default")
  227.                     gui.Main.FaceSelector[selectedFace].TextColor3 = getUIColor("MainText", "Default")
  228.                 end
  229.                 if ui.Name ~= selectedFace then
  230.                     selectedFace = ui.Name
  231.                     ui.BackgroundColor3 = getUIColor("RibbonButton", "Selected")
  232.                     ui.TextColor3 = getUIColor("MainText", "Selected")
  233.                 else
  234.                     selectedFace = nil
  235.                 end
  236.                 updateObject()
  237.             end))
  238.         end
  239.     end
  240.     table.insert(connections, gui.Main.CheckBoxes.ActiveGui.CheckBox.MouseButton1Click:Connect(function()
  241.         active = not active
  242.         local transparency = active and 0.5 or 1
  243.         gui.Main.CheckBoxes.ActiveGui.CheckBox.Display.Transparency = active and 0 or 1
  244.         for _,ad in pairs(visuals.Face:GetChildren()) do
  245.             ad.Transparency = transparency
  246.         end
  247.         for _,ad in pairs(visuals.Direction:GetChildren()) do
  248.             ad.Transparency = (active and showDirectionalVectors) and 0.5 or 1
  249.         end
  250.     end))
  251.     table.insert(connections, gui.Main.CheckBoxes.ShowVectors.CheckBox.MouseButton1Click:Connect(function()
  252.         showDirectionalVectors = not showDirectionalVectors
  253.         gui.Main.CheckBoxes.ShowVectors.CheckBox.Display.Transparency = showDirectionalVectors and 0 or 1
  254.         local transparency = (showDirectionalVectors and active) and 0.5 or 1
  255.         for _,ad in pairs(visuals.Direction:GetChildren()) do
  256.             ad.Transparency = transparency
  257.         end
  258.         --set all vector to transparency val, if active 1
  259.     end))
  260.     gui.Main:TweenPosition(UDim2.new(0.5, 0, 0, 6), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.15, true)
  261.     wait(0.25)
  262.     enabled = true
  263.     tweening = false
  264. end
  265.  
  266. local function stopPlugin()
  267.     for _,connection in pairs(connections) do
  268.         connection:Disconnect()
  269.     end
  270.     connections = {}
  271.     currentlySelected = nil
  272.     updateObject()
  273.     tweening = true
  274.     gui.Main:TweenPosition(UDim2.new(0.5, 0, 0, -175), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.15, true)
  275.     wait(0.25)
  276.     enabled = false
  277.     tweening = false
  278.     if binded then
  279.         runService:UnbindFromRenderStep("FaceSelector by Tukars")
  280.     end
  281.     binded = false
  282.     gui.Enabled = false
  283.     plugin:Deactivate()
  284. end
  285.  
  286. button.Click:connect(function()
  287.     if not tweening then
  288.         if enabled then
  289.             stopPlugin()
  290.         else
  291.             startPlugin()
  292.         end
  293.     end
  294. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement