|
| 1 | +local UserInputService = game:GetService("UserInputService") |
| 2 | + |
| 3 | +local Extension = require("@src/Extension") |
| 4 | +local ext = require(".") |
| 5 | + |
| 6 | +type TrussMaterial = { name: string, properties: { [string]: any } } |
| 7 | + |
| 8 | +local function createSurfaceProperties(surface: Enum.SurfaceType): { [string]: any } |
| 9 | + return { |
| 10 | + TopSurface = surface, |
| 11 | + BottomSurface = surface, |
| 12 | + RightSurface = surface, |
| 13 | + LeftSurface = surface, |
| 14 | + FrontSurface = surface, |
| 15 | + BackSurface = surface, |
| 16 | + } |
| 17 | +end |
| 18 | + |
| 19 | +local TRUSS_MATERIALS: { TrussMaterial } = { |
| 20 | + { name = "Neon", properties = { Material = Enum.Material.Neon } }, |
| 21 | + { name = "Granite", properties = { Material = Enum.Material.Granite } }, |
| 22 | + { name = "Glass", properties = { Material = Enum.Material.Glass, Transparency = 0.5 } }, |
| 23 | + { name = "Plastic", properties = { Material = Enum.Material.Plastic } }, |
| 24 | + { name = "Stud", properties = createSurfaceProperties(Enum.SurfaceType.Studs) }, |
| 25 | + { name = "Inlet", properties = createSurfaceProperties(Enum.SurfaceType.Inlet) }, |
| 26 | + { name = "Universal", properties = createSurfaceProperties(Enum.SurfaceType.Universal) }, |
| 27 | + { name = "Smooth", properties = createSurfaceProperties(Enum.SurfaceType.Smooth) }, |
| 28 | + { name = "Glue", properties = createSurfaceProperties(Enum.SurfaceType.Glue) }, |
| 29 | +} |
| 30 | + |
| 31 | +local TRUSS_MATERIAL_NAMES = {} |
| 32 | +local TRUSS_MATERIALS_BY_NAMES = {} |
| 33 | +for i, material in TRUSS_MATERIALS do |
| 34 | + TRUSS_MATERIAL_NAMES[i] = material.name |
| 35 | + TRUSS_MATERIALS_BY_NAMES[material.name] = material |
| 36 | +end |
| 37 | + |
| 38 | +local TRUSS_FILLER_TAG = `Rocket Geometry: Truss Filler` |
| 39 | + |
| 40 | +local function fill(truss: TrussPart, material: TrussMaterial) |
| 41 | + local filler: BasePart |
| 42 | + local fillerName = truss.Name .. "Fill" |
| 43 | + |
| 44 | + local existing = truss:FindFirstChild(fillerName) |
| 45 | + if existing and existing:HasTag(TRUSS_FILLER_TAG) and existing:IsA("BasePart") then |
| 46 | + filler = existing |
| 47 | + end |
| 48 | + |
| 49 | + filler = filler or Instance.new("Part") |
| 50 | + |
| 51 | + filler.Anchored = true |
| 52 | + filler.CanCollide = false |
| 53 | + filler.CastShadow = truss.CastShadow |
| 54 | + filler.CFrame = truss.CFrame |
| 55 | + filler.Color = truss.Color |
| 56 | + filler.Name = fillerName |
| 57 | + filler.Reflectance = truss.Reflectance |
| 58 | + filler.Size = truss.Size - Vector3.new(0.5, 0.5, 0.5) |
| 59 | + filler.Transparency = truss.Transparency |
| 60 | + |
| 61 | + for property, value in material.properties do |
| 62 | + (filler :: any)[property] = value |
| 63 | + end |
| 64 | + |
| 65 | + filler.Parent = truss |
| 66 | + filler:AddTag(TRUSS_FILLER_TAG) |
| 67 | +end |
| 68 | + |
| 69 | +local function fillRecursive(instances: { Instance }, material: TrussMaterial) |
| 70 | + for _, child in instances do |
| 71 | + if child:IsA("TrussPart") then |
| 72 | + fill(child, material) |
| 73 | + end |
| 74 | + |
| 75 | + fillRecursive(child:GetChildren(), material) |
| 76 | + end |
| 77 | +end |
| 78 | + |
| 79 | +for _, material in TRUSS_MATERIALS do |
| 80 | + ext:newCommand({ |
| 81 | + id = `fill-trusses-with-{string.lower(material.name)}`, |
| 82 | + title = `Fill Trusses with {material.name}`, |
| 83 | + description = `Fills selected TrussParts with {material.name} parts.`, |
| 84 | + |
| 85 | + run = function(ctx: Extension.CommandContext) |
| 86 | + ctx:recordChanges() |
| 87 | + fillRecursive(ctx:getSelection(), material) |
| 88 | + end, |
| 89 | + }) |
| 90 | +end |
| 91 | + |
| 92 | +ext:newCommand({ |
| 93 | + id = "fill-trusses", |
| 94 | + title = "Select and Fill Trusses", |
| 95 | + description = "Fills selected TrussParts with the given material", |
| 96 | + |
| 97 | + run = function(ctx: Extension.CommandContext) |
| 98 | + ctx:recordChanges() |
| 99 | + ctx:setState("firstRun", true) |
| 100 | + end, |
| 101 | + |
| 102 | + renderInViewport = function(ctx: Extension.CommandContext) |
| 103 | + local Iris = ctx.iris |
| 104 | + local window = Iris.Window( |
| 105 | + { "Select material to fill trusses" }, |
| 106 | + { position = UserInputService:GetMouseLocation() } :: any |
| 107 | + ) |
| 108 | + |
| 109 | + if window.closed() then |
| 110 | + ctx:cleanup() |
| 111 | + Iris.End() |
| 112 | + return |
| 113 | + end |
| 114 | + |
| 115 | + local search = "" |
| 116 | + local inputField = Iris.InputText({ "", "Search materials..." }) |
| 117 | + local input = inputField.Instance:FindFirstChildWhichIsA("TextBox") |
| 118 | + if input then |
| 119 | + search = input.Text |
| 120 | + if ctx:getState("firstRun") then |
| 121 | + ctx:setState("firstRun", false) |
| 122 | + input:CaptureFocus() |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + local searchResults: { string } = TRUSS_MATERIAL_NAMES |
| 127 | + if search ~= "" then |
| 128 | + local results = ctx.fzy.filter(search, TRUSS_MATERIAL_NAMES) |
| 129 | + searchResults = table.create(#results) :: { string } |
| 130 | + for i, result in results do |
| 131 | + searchResults[i] = TRUSS_MATERIAL_NAMES[result[1]] |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + local selectedMaterial: TrussMaterial? |
| 136 | + for _, name in searchResults do |
| 137 | + if Iris.Button(name, UDim2.fromScale(1, 0)).clicked() then |
| 138 | + selectedMaterial = TRUSS_MATERIALS_BY_NAMES[name] |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + if not selectedMaterial then |
| 143 | + if inputField.textChanged() then |
| 144 | + local first = searchResults[1] |
| 145 | + if first then |
| 146 | + selectedMaterial = TRUSS_MATERIALS_BY_NAMES[first] |
| 147 | + end |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + if selectedMaterial then |
| 152 | + fillRecursive(ctx:getSelection(), selectedMaterial) |
| 153 | + ctx:cleanup() |
| 154 | + end |
| 155 | + |
| 156 | + Iris.End() |
| 157 | + end, |
| 158 | +}) |
| 159 | + |
| 160 | +ext:newCommand({ |
| 161 | + id = "delete-truss-fill", |
| 162 | + title = "Delete Truss Fill", |
| 163 | + description = "Deletes fills in selected TrussParts that was added by this extension.", |
| 164 | + |
| 165 | + run = function(ctx: Extension.CommandContext) |
| 166 | + ctx:recordChanges() |
| 167 | + |
| 168 | + for _, selected in ctx:getSelection() do |
| 169 | + if selected:HasTag(TRUSS_FILLER_TAG) then |
| 170 | + selected:Destroy() |
| 171 | + continue |
| 172 | + end |
| 173 | + |
| 174 | + for _, descendant in selected:GetDescendants() do |
| 175 | + if descendant:HasTag(TRUSS_FILLER_TAG) then |
| 176 | + descendant:Destroy() |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | + end, |
| 181 | +}) |
| 182 | + |
| 183 | +return nil |
0 commit comments