Advertisement
StefanBashkir

Untitled

Jan 30th, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.84 KB | None | 0 0
  1. local Plugin = PluginManager():CreatePlugin()
  2. local Button = Plugin:CreateToolbar("Triangle Workshop"):CreateButton("", "Master", "tangle.png")
  3. local DEB = false;
  4. local active = false
  5. local ds = Vector3.new(10,0,10); -- default size
  6. local preId = "_TRIANGLE_"
  7. local preIdlen = preId:len()
  8. local tmodel = Workspace:FindFirstChild("Triangles") or Instance.new("Model", Workspace)
  9.     tmodel.Name = "Triangles"
  10. local handles = Instance.new("Handles")
  11.     handles.Style = Enum.HandlesStyle.Resize;
  12.     handles.Color = BrickColor.White();
  13.     handles.Parent = Game.CoreGui
  14. local previncrement;
  15. local controldown = false;
  16. local shiftdown = false;
  17. local cbyte = 50
  18. local shiftbyte = 48
  19.  
  20. local orangehandles = {}
  21. local triangles = {}
  22. local newtransparency = 1
  23.  
  24. local addconnectoricon = "rbxasset://textures\\CameraZoomIn_ovr.png"
  25. local mouseoverhandleicon = "rbxasset://textures\\advClosed-hand.png"
  26. local showninstructions = false
  27.  
  28. local connectors = {};
  29.  
  30. local increment = 1;
  31. local lastpress = 1e50;
  32. pcall(function() game:WaitForChild("CoreGui").MrNicNacTriangles:Destroy() end)
  33. local _sg = Instance['new']("ScreenGui", game:WaitForChild("CoreGui"));
  34. _sg.Name = "MrNicNacTriangles";
  35. local _f = Instance['new']("Frame", _sg);
  36.     _f.Size = UDim2['new'](.3, 0, .175,0);
  37.     _f.Style = "RobloxRound";
  38.     _f.Position = UDim2.new(.5 - (_f.Size.X.Scale/2) ,0, .5 - (_f.Size.Y.Scale/2),0);
  39. local _tl = Instance['new']("TextLabel", _f)
  40.     _tl.Size = UDim2.new(1,0,.4,0);
  41.     _tl.TextColor3 = Color3.new(1,1,1);
  42.     _tl.TextScaled = true;
  43.     _tl.BackgroundTransparency = 1;
  44.     _tl.Text = "Movement Increment"
  45. local _tb = Instance['new']("TextBox", _f)
  46.     _tb.Size = UDim2.new(1,0, .6,0)
  47.     _tb.Position = UDim2.new(0,0,.4,0);
  48.     _tb.TextColor3 = Color3.new(1,1,1)
  49.     _tb.BackgroundColor3 = Color3.new(.7,.7,.7);
  50.     _tb.Text = increment;
  51.     _tb.TextScaled = true;
  52.     _f.Visible = false;
  53.  
  54. wait(1)
  55. print("TRIANGLE WORKSHOP: GETTING EXISTING VALUES")
  56. -- get selection boxes and triangles
  57. for i,v in pairs(Workspace:GetChildren()) do
  58.     if v.Name == "Triangle" and v:IsA("Model") then
  59.         for k,j in pairs(v:GetChildren()) do
  60.             if j.Name:sub(1,4) == "Side" then
  61.                 for x,y in pairs(j:GetChildren()) do
  62.                     if y:FindFirstChild("SelectionBox") then
  63.                         y.SelectionBox:Destroy()
  64.                     end
  65.                     if y.Name:sub(1, preIdlen) == preId then
  66.                         y.Transparency = .65
  67.                         y.BrickColor = BrickColor.new("Deep orange");
  68.                         table.insert(orangehandles, y)
  69.                     end
  70.                     if y.Name:sub(1,1) == "P" then
  71.                         table.insert(triangles, y)
  72.                     end
  73.                 end
  74.             end
  75.         end
  76.     end
  77. end
  78. print("VALUES GATHERED");
  79.  
  80. Button.Click:connect(function()
  81.     if DEB then return end
  82.     DEB = true;
  83.     if not showninstructions then
  84.         showninstructions = true
  85.         print("------------------------------------------------------------------------")
  86.         print("Press 'n' to create a new triangle plate")
  87.         print("Click on the orange handles to activate dragging.")
  88.         print("Hold Ctrl+Click on the orange handles to select one. Select three of them simultaneously to fill in an area.")
  89.         print("Hold Shift+Click on a triangle surface to delete the triangle plate.")
  90.         print("Press 't' to toggle the orange handles.")
  91.         print("Press 'y' to finalize your work. WARNING: triangles are compiled into one model and orange handles completely destroyed.")
  92.         print("------------------------------------------------------------------------")
  93.     end
  94.    
  95.     Plugin:Activate(not active);
  96.     Button:SetActive(not active)
  97.     active = not active
  98.    
  99.     if active then
  100.         local Mouse = Plugin:GetMouse()
  101.         --[[ Icons don't work
  102.         mousemove = Mouse.Move:connect(function()
  103.             local target = Mouse.Target
  104.             if not target then return nil end
  105.             if target.Name:sub(1, preIdlen) == preId and target.Parent.Name:sub(1,4) == "Side" then
  106.                 print("over handle")
  107.                 Mouse.Icon = mouseoverhandleicon
  108.             else
  109.                 Mouse.Icon = ""
  110.             end
  111.         end)]]
  112.        
  113.         keydown = Mouse.KeyDown:connect(function(key)
  114.             --print("Key down: " .. key:byte())
  115.             if key == "n" then
  116.                 local triangle = Instance.new("Model", Workspace)
  117.                     triangle.Name = "Triangle"
  118.                 local center = Workspace.CurrentCamera.CoordinateFrame.p + (Workspace.CurrentCamera.CoordinateFrame.lookVector * 5) --Vector3.new(0,0,0)
  119.                     center = Vector3.new(math.floor(center.x), math.floor(center.y), math.floor(center.z))
  120.                 local c1, c2 = (center+Vector3.new(ds.X/2, 0, ds.Z/2)), (center+Vector3.new(-ds.X/2, 0, ds.Z/2))
  121.                 local c3, c4 = (center+Vector3.new(ds.X/2, 0, -ds.Z/2)),(center+Vector3.new(-ds.X/2, 0, -ds.Z/2))
  122.                 local side1 = draw(c1, c2, c3, 1); side1.Parent = triangle
  123.                 local side2 = draw(c4, c3, c2, 2); side2.Parent = triangle
  124.                     plothandle(c1, side1, 1)
  125.                     plothandle(c2, side1, 2)
  126.                     plothandle(c3, side1, 3)
  127.                     -- Next side
  128.                     plothandle(c4, side2, 1)
  129.                     --plothandle(c3, side2, 2)
  130.                     --plothandle(c2, side2, 3)
  131.             elseif key:byte() == cbyte then
  132.                 controldown = true
  133.             elseif key:byte() == shiftbyte then
  134.                 shiftdown = true;
  135.             elseif key == "t" then
  136.                 for i,v in pairs(orangehandles) do
  137.                     v.Transparency = newtransparency
  138.                 end
  139.                 if newtransparency == 1 then
  140.                     newtransparency = .65
  141.                 else
  142.                     newtransparency = 1
  143.                 end
  144.             elseif key == "y" then
  145.                 for i,v in pairs(orangehandles) do
  146.                     v:Destroy()
  147.                 end
  148.                 if not Workspace:FindFirstChild("Triangles") then
  149.                     tmodel = Instance.new("Model", Workspace)
  150.                     tmodel.Name = "Triangles"
  151.                 end
  152.                 for i,v in pairs(triangles) do
  153.                     if v.Parent then
  154.                         v.Parent = tmodel
  155.                     end
  156.                 end
  157.                 triangles = {}
  158.                 orangehandles = {}
  159.                 for i,v in pairs(Workspace:GetChildren()) do
  160.                     if v.Name == "Triangle" and v:IsA("Model") and v:FindFirstChild("Side_1") then
  161.                         pcall(function()
  162.                             v:Destroy()
  163.                         end)
  164.                     end
  165.                 end
  166.             --[[elseif key == "+" then
  167.                 increment = increment + .05
  168.                 _tb.Text = increment;]]
  169.             elseif key == "-" then
  170.                 if increment > 0 and increment-.05 > 0 then
  171.                     _f.Visible = true;
  172.                     lastpress = tick();
  173.                     increment = increment - .05
  174.                     _tb.Text = increment;
  175.                     wait(1)
  176.                     if tick()-lastpress >= .95 then
  177.                         _f.Visible = false;
  178.                     end
  179.                 end
  180.             end
  181.         end)
  182.         keyup = Mouse.KeyUp:connect(function(key)
  183.             print(key, key:byte());
  184.             if key:byte() == cbyte then
  185.                 controldown = false
  186.                 DisconnectConnectors();
  187.             elseif key:byte() == shiftbyte then
  188.                 shiftdown = false
  189.             elseif key == "=" then
  190.                 _f.Visible = true;
  191.                 lastpress = tick();
  192.                 increment = increment + .05
  193.                 _tb.Text = increment;
  194.                 wait(1)
  195.                 if tick()-lastpress >= .95 then
  196.                     _f.Visible = false;
  197.                 end
  198.             end
  199.         end)
  200.         button1down = Mouse.Button1Down:connect(function()
  201.             local target = Mouse.Target
  202.             if not target then handles.Adornee = nil return nil end
  203.             if target.Name:sub(1, preIdlen) == preId and target.Parent.Name:sub(1,4) == "Side" then
  204.                 if not controldown then
  205.                     handles.Adornee = target
  206.                 elseif controldown then
  207.                     handles.Adornee = nil
  208.                     if #connectors < 3 then
  209.                         if not TableFind(connectors, target) then
  210.                             local s = Instance.new("Sparkles", target)
  211.                             s.Name = "Sparkles"
  212.                             connectors[#connectors+1] = target
  213.                             if #connectors == 3 then
  214.                                 -- draw filling triangle
  215.                                 local triangle = Instance.new("Model", Workspace)
  216.                                     triangle.Name = "Triangle"
  217.                                 local side1 = draw(connectors[1].Position,connectors[2].Position,connectors[3].Position,1); side1.Parent = triangle
  218.                                 --local side2 = draw(connectors[4].Position,connectors[3].Position,connectors[2].Position,2); side2.Parent = triangle
  219.                                 plothandle(connectors[1].Position, side1, 1)
  220.                                 plothandle(connectors[2].Position, side1, 2)
  221.                                 plothandle(connectors[3].Position, side1, 3)
  222.                                 DisconnectConnectors()
  223.                             end
  224.                         end
  225.                     end
  226.                 end
  227.             elseif (target.Name == "P0" or target.Name == "P1") and (target.Parent.Name:sub(1,4) == "Side") then
  228.                 if shiftdown then
  229.                     target.Parent.Parent:Destroy()
  230.                 end
  231.             else
  232.                 handles.Adornee = nil
  233.             end
  234.         end)
  235.         mousedrag = handles.MouseDrag:connect(function(normalid, angle, radius)
  236.             local direction = handles.Adornee.CFrame:vectorToWorldSpace(Vector3.FromNormalId(normalid))
  237.             if not previncrement then
  238.                 if angle >= 1 then
  239.                     previncrement = angle
  240.                     handles.Adornee.CFrame = handles.Adornee.CFrame * CFrame.new(direction * (tonumber(increment) and tonumber(increment) or 1))
  241.                     RedrawSide(handles.Adornee.Parent)
  242.                 elseif angle <= -1 then
  243.                     previncrement = angle
  244.                     handles.Adornee.CFrame = handles.Adornee.CFrame * CFrame.new(direction * -(tonumber(increment) and tonumber(increment) or 1))
  245.                     RedrawSide(handles.Adornee.Parent)
  246.                 end
  247.             else
  248.                 if angle - previncrement >= 1 then
  249.                     previncrement = angle
  250.                     handles.Adornee.CFrame = handles.Adornee.CFrame * CFrame.new(direction * (tonumber(increment) and tonumber(increment) or 1))
  251.                     RedrawSide(handles.Adornee.Parent)
  252.                 elseif angle - previncrement <= -1 then
  253.                     previncrement = angle
  254.                     handles.Adornee.CFrame = handles.Adornee.CFrame * CFrame.new(direction * -(tonumber(increment) and tonumber(increment) or 1))
  255.                     RedrawSide(handles.Adornee.Parent)
  256.                 end
  257.             end
  258.         end)
  259.         handlesbutton1down = handles.MouseButton1Down:connect(function()
  260.             previncrement = nil;
  261.         end)
  262.     end
  263.    
  264. end)
  265.  
  266. Plugin.Deactivation:connect(function()
  267.     DEB = false;
  268.     Plugin:Activate(false);
  269.     Button:SetActive(false)
  270.     active = false
  271.     -- cleanup
  272.     previnvrement = nil
  273.     handles.Adornee = nil
  274.     pcall(function()
  275.         keydown:disconnect()
  276.         mousedrag:disconnect()
  277.         button1down:disconnect()
  278.         handlesbutton1down:disconnect()
  279.     end)
  280. end)
  281.  
  282.  
  283.  
  284. function TableFind(t, v)
  285.     for i,k in pairs(t) do
  286.         if k == v then
  287.             return true
  288.         end
  289.     end
  290. end
  291.  
  292. function DisconnectConnectors()
  293.     for i,v in pairs(connectors) do
  294.         if v:FindFirstChild("Sparkles") then
  295.             v.Sparkles:Destroy()
  296.         end
  297.     end
  298.     connectors = {}
  299. end
  300.  
  301.  
  302. function RedrawSide(side)
  303.  
  304.     local handles = GetHandles(side, "Side_1")
  305.     local parts = GetSides(side, "Side_1")
  306.     redraw(parts['P0'], parts['P1'], handles['c1'], handles['c2'], handles['c3']);
  307.    
  308.     if side.Parent:FindFirstChild("Side_2") then
  309.         local handle4 = GetHandles(side, "Side_2")
  310.         local parts = GetSides(side, "Side_2")
  311.         redraw(parts['P0'], parts['P1'], handle4, handles['c3'], handles['c2']);
  312.     end
  313.    
  314. end
  315.  
  316. function GetHandles(side, name)
  317.     local plots = {}
  318.     if name == "Side_1" then
  319.         for i,v in pairs(side.Parent[name]:GetChildren()) do
  320.             if v.Name:sub(1, preIdlen) == preId then
  321.                 plots['c' .. v.Name:sub(preIdlen+1,preIdlen+1)] = v.Position
  322.             end
  323.         end
  324.         return plots
  325.     else
  326.         return side.Parent.Side_2[preId .. "1"].Position
  327.     end
  328. end
  329.  
  330. function GetSides(side, name)
  331.     local plots = {}
  332.     for i,v in pairs(side.Parent[name]:GetChildren()) do
  333.         if v.Name:sub(1,1) == "P" then
  334.             plots['P' .. v.Name:sub(2,2)] = v
  335.         end
  336.     end
  337.     return plots
  338. end
  339.  
  340. function plothandle(point, triangle, id)
  341.     local part = Instance.new("Part", triangle)
  342.     part.Name = preId .. id
  343.     part.FormFactor = "Symmetric"
  344.     part.Size = Vector3.new(1,1,1)
  345.     part.Transparency = newtransparency == 1 and 0.65 or 1
  346.     part.CanCollide = false
  347.     part.CFrame = CFrame.new(point)
  348.     part.BrickColor = BrickColor.new("Deep orange");
  349.    
  350.     table.insert(orangehandles, part)
  351. end
  352.    
  353.  
  354. --[[ * DrawTriangle:
  355. -- Created by Stravant
  356. -- Made to use WedgeParts by nccvoyager
  357. -- Scaling sizing, and physical attributes corrected by MrNicNac]]
  358.  
  359. local ply = Instance.new("WedgePart")
  360. ply.formFactor = "Custom"
  361. ply.TopSurface = 0
  362. ply.BottomSurface = 0
  363. ply.Anchored = true
  364. ply.Size = Vector3.new(0.2,7,7)
  365.  
  366. local msh = Instance.new("SpecialMesh")
  367. msh.MeshType = 2
  368. msh.Parent = ply
  369.  
  370. function ParaD(a, b, c)
  371.     local dot = (b-a).x*(c-a).x + (b-a).y*(c-a).y + (b-a).z*(c-a).z
  372.     return dot / (a-b).magnitude
  373. end
  374.  
  375. function PerpD(a, b, c)
  376.     local par = ParaD(a, b, c)
  377.     return math.sqrt((c-a).magnitude^2 - par^2)
  378. end
  379.  
  380. local _P0, _P1 = nil, nil
  381.  
  382. function draw(vec1, vec2, vec3, id)
  383.     local A, B, C = nil, nil, nil
  384.     local p0, p1 = ply:clone(), ply:clone()
  385.     local rmodel = Instance.new("Model")
  386.     rmodel.Name = "Side_" .. id
  387.    
  388.     _P0 = p0
  389.     _P1 = p1
  390.    
  391.     local s1 = (vec1 - vec2).magnitude
  392.             s2 = (vec2 - vec3).magnitude
  393.             s3 = (vec3 - vec1).magnitude
  394.     local smax = math.max(s1, s2, s3)
  395.    
  396.     if (vec1 - vec2).magnitude == smax then
  397.         A = vec1
  398.         B = vec2
  399.         C = vec3
  400.     elseif (vec2 - vec3).magnitude == smax then
  401.         A = vec2
  402.         B = vec3
  403.         C = vec1   
  404.     elseif (vec3 - vec1).magnitude == smax then
  405.         A = vec3
  406.         B = vec1
  407.         C = vec2   
  408.     end
  409.    
  410.     local perp = PerpD(A, B, C)
  411.     local para = ParaD(A, B, C)
  412.     local dif_para = (A-B).magnitude - para
  413.     local ambig = false
  414.    
  415.     p0.Mesh.Scale = Vector3.new(0.1, 1,1)
  416.     p0.Size = Vector3.new(.2, perp, para)
  417.     p0.CFrame = CFrame.new(B, A)
  418.    
  419.     local Top_Look = (p0.CFrame * CFrame.Angles(math.pi/2, 0, 0)).lookVector
  420.     local Mid_Point = A + CFrame.new(A, B).lookVector * para
  421.     local Needed_Look = CFrame.new(Mid_Point, C).lookVector
  422.     local dot = (Top_Look.x * Needed_Look.x) + (Top_Look.y * Needed_Look.y) + (Top_Look.z * Needed_Look.z)
  423.    
  424.     p0.CFrame = p0.CFrame * CFrame.Angles(0, 0, math.acos(dot))
  425.    
  426.     if ((p0.CFrame * CFrame.Angles(math.pi/2, 0, 0)).lookVector - Needed_Look).magnitude > 0.01 then
  427.         p0.CFrame = p0.CFrame * CFrame.Angles(0, 0, -2*math.acos(dot))
  428.         ambig = true
  429.     end
  430.    
  431.     p0.CFrame = p0.CFrame * CFrame.new(0, perp/2, -(dif_para + para/2))
  432.     p0.Parent = rmodel
  433.     p0.Name = "P0"
  434.     p1.Mesh.Scale = Vector3.new(0, 1,1)
  435.     p1.Size = Vector3.new(.2, perp, dif_para)
  436.     p1.CFrame = CFrame.new(B, A)  * CFrame.Angles(0, 0, math.acos(dot)) * CFrame.Angles(0, math.pi, 0)
  437.    
  438.     if ((p1.CFrame * CFrame.Angles(math.pi/2, 0, 0)).lookVector - Needed_Look).magnitude > 0.01 then
  439.         p1.CFrame = p1.CFrame * CFrame.Angles(0, 0, 2*math.acos(dot))
  440.         ambig = true
  441.     end
  442.    
  443.     p1.CFrame = p1.CFrame * CFrame.new(0, perp/2, dif_para/2)
  444.     p1.Parent = rmodel
  445.     p1.Name = "P1"
  446.     table.insert(triangles, p0)
  447.     table.insert(triangles, p1)
  448.    
  449.     --p1.BrickColor = BrickColor.Green()
  450.     --p0.BrickColor = BrickColor.Green()
  451.    
  452.     return rmodel
  453.    
  454. end
  455. ---------------------------------------------------------
  456.  
  457. function redraw(p0, p1, vec1, vec2, vec3)
  458.     local A, B, C = nil, nil, nil
  459.    
  460.     _P0 = p0
  461.     _P1 = p1
  462.    
  463.     local s1 = (vec1 - vec2).magnitude
  464.             s2 = (vec2 - vec3).magnitude
  465.             s3 = (vec3 - vec1).magnitude
  466.     local smax = math.max(s1, s2, s3)
  467.    
  468.     if (vec1 - vec2).magnitude == smax then
  469.         A = vec1
  470.         B = vec2
  471.         C = vec3
  472.     elseif (vec2 - vec3).magnitude == smax then
  473.         A = vec2
  474.         B = vec3
  475.         C = vec1   
  476.     elseif (vec3 - vec1).magnitude == smax then
  477.         A = vec3
  478.         B = vec1
  479.         C = vec2   
  480.     end
  481.    
  482.     local perp = PerpD(A, B, C)
  483.     local para = ParaD(A, B, C)
  484.     local dif_para = (A-B).magnitude - para
  485.     local ambig = false
  486.     --print(perp, para);
  487.    
  488.     p0.Mesh.Scale = Vector3.new(0.1, 1,1)
  489.     p0.Size = Vector3.new(.2, perp, para)
  490.     p0.CFrame = CFrame.new(B, A)
  491.    
  492.     local Top_Look = (p0.CFrame * CFrame.Angles(math.pi/2, 0, 0)).lookVector
  493.     local Mid_Point = A + CFrame.new(A, B).lookVector * para
  494.     local Needed_Look = CFrame.new(Mid_Point, C).lookVector
  495.     local dot = (Top_Look.x * Needed_Look.x) + (Top_Look.y * Needed_Look.y) + (Top_Look.z * Needed_Look.z)
  496.    
  497.     p0.CFrame = p0.CFrame * CFrame.Angles(0, 0, math.acos(dot))
  498.    
  499.     if ((p0.CFrame * CFrame.Angles(math.pi/2, 0, 0)).lookVector - Needed_Look).magnitude > 0.01 then
  500.         p0.CFrame = p0.CFrame * CFrame.Angles(0, 0, -2*math.acos(dot))
  501.         ambig = true
  502.     end
  503.    
  504.     p0.CFrame = p0.CFrame * CFrame.new(0, perp/2, -(dif_para + para/2))
  505.    
  506.     p1.Mesh.Scale = Vector3.new(0, 1,1)
  507.     p1.Size = Vector3.new(.2, perp, dif_para)
  508.     p1.CFrame = CFrame.new(B, A)  * CFrame.Angles(0, 0, math.acos(dot)) * CFrame.Angles(0, math.pi, 0)
  509.    
  510.     if ((p1.CFrame * CFrame.Angles(math.pi/2, 0, 0)).lookVector - Needed_Look).magnitude > 0.01 then
  511.         p1.CFrame = p1.CFrame * CFrame.Angles(0, 0, 2*math.acos(dot))
  512.         ambig = true
  513.     end
  514.    
  515.     p1.CFrame = p1.CFrame * CFrame.new(0, perp/2, dif_para/2)
  516.    
  517.     --p1.BrickColor = BrickColor.Green()
  518.     --p0.BrickColor = BrickColor.Green()
  519.    
  520.     return rmodel
  521.    
  522. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement