Skip to content

Commit

Permalink
Added auto import
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaGobble committed May 23, 2024
1 parent 5c1fffc commit a61e048
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 46 deletions.
3 changes: 2 additions & 1 deletion App/Roblox Figma Importer Assistant/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ figma.ui.onmessage = msg => {
let FigmaExportData = [];

function ReadNode(node) {
if ("opacity" in node) {
if ("opacity" in node || "frame" in node) {
let NodeExportData = {
type: node.type,

Expand All @@ -38,6 +38,7 @@ figma.ui.onmessage = msg => {
x: node.x,
y: node.y,
strokeWeight: node.strokeWeight,
opacity: node.opacity,

//rotation: node.rotation,
clipsContent: node.clipsContent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
local AppImportInterpreter = {}

local HttpService = game:GetService("HttpService")

local function ReadRecursive(ParentTable)
local ChildTable = {
Root = {},
}

for _, Child in ipairs(ParentTable) do
local Interpretation = {
Size = {
X = Child.width,
Y = Child.height
},

Position = {
X = Child.x,
Y = Child.y,
},

AnchorPoint = {
X = 0,
Y = 0
},

Name = Child.name or "AUTOIMPORT",
Image = "",
Stroke = Child.strokeWeight or 0,
Oblique = 0,

-- Unique data from import
Type = if Child.opacity == 0 and Child.strokeWeight == 0 then "Frame" else "ImageLabel"
}

if Child.children then
Interpretation.Children = ReadRecursive(Child.children)
else
Interpretation.Children = {}
end

table.insert(ChildTable.Root, Interpretation)
end

-- if ParentTable.children then
-- ChildTable.Children = ReadRecursive(ParentTable.children)
-- end

return ChildTable
end

function AppImportInterpreter:InterpretJSONData(JSONData : string)
local Data = HttpService:JSONDecode(JSONData)
local Interpretation = ReadRecursive(Data)

return Interpretation
end

return AppImportInterpreter
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
local CorrectionHandler = {}
local Applicator = {}

local SelectionService = game:GetService("Selection")

local Interface = require(script.Parent.Interface)
local Utility = require(script.Parent.Utility)
local Fusion = require(script.Parent.Parent.Packages.Fusion)
local Packages = script.Parent.Parent.Parent.Packages
local Fusion = require(Packages.Fusion)
local Utility = require(script.Parent.Parent.Utility)
local New = Fusion.New
local Hydrate = Fusion.Hydrate
local Children = Fusion.Children

local SelectedInstance = nil

function CorrectionHandler:Init()
Interface:OnApply(function(Data)
if SelectedInstance then
local Size = Vector2.new(Data.Size.X, Data.Size.Y)
function Applicator:ApplyChangesFromData(SelectedInstance : Instance, Data : {})
local Size = Vector2.new(Data.Size.X, Data.Size.Y)
local Position = Vector2.new(Data.Position.X, Data.Position.Y)

Utility.CreateUndoMarkerStart()
Expand Down Expand Up @@ -46,7 +40,6 @@ function CorrectionHandler:Init()
if SelectedInstance.Parent:GetAttribute("FigmaObliqueSize") then
local Oblique = SelectedInstance.Parent:GetAttribute("FigmaObliqueSize") - Oblique
FinalSize -= Vector2.new(0, Oblique)
--FinalPosition += Vector2.new(0, SelectedInstance.Parent:GetAttribute("FigmaObliqueSize"))
end

local ScaledSize = Utility.ConvertToContextualScale(SelectedInstance, FinalSize)
Expand Down Expand Up @@ -75,25 +68,6 @@ function CorrectionHandler:Init()
SelectedInstance:SetAttribute("FigmaObliqueSize", Oblique)

Utility.CreateUndoMarkerEnd()
end
end)

SelectionService.SelectionChanged:Connect(function()
local Selected = SelectionService:Get()
local SelectedCount = Utility.GetDictionaryLength(Selected)

if SelectedCount == 1 then
SelectedInstance = Selected[1]
else
SelectedInstance = nil
end

if SelectedInstance and not SelectedInstance:IsA("GuiObject") and not SelectedInstance:IsA("ScreenGui") then
SelectedInstance = nil
end

Interface:OnSelection(SelectedInstance)
end)
end

return CorrectionHandler
return Applicator
24 changes: 24 additions & 0 deletions src/FigmaImportAssistant/Main/CorrectionHandler/Creator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local Creator = {}

local Applicator = require(script.Parent.Applicator)

local function CreateRecursive(Parent, Data : {})
for _, Child in Data.Root do
local Object = Instance.new(Child.Type)
Object.ClipsDescendants = Child.clipsContent or true
Object.BackgroundTransparency = 1
Object.Parent = Parent

Applicator:ApplyChangesFromData(Object, Child)

if Child.Children then
CreateRecursive(Object, Child.Children)
end
end
end

function Creator:CreateFromData(SelectedInstance, Data : {})
CreateRecursive(SelectedInstance, Data)
end

return Creator
46 changes: 46 additions & 0 deletions src/FigmaImportAssistant/Main/CorrectionHandler/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
local CorrectionHandler = {}

local SelectionService = game:GetService("Selection")

local Interface = require(script.Parent.Interface)
local Utility = require(script.Parent.Utility)
local AppImportInterpreter = require(script.AppImportInterpreter)
local Creator = require(script.Creator)
local Applicator = require(script.Applicator)

local SelectedInstance = nil

function CorrectionHandler:Init()
Interface:OnApply(function(Data)
if SelectedInstance then
Applicator:ApplyChangesFromData(SelectedInstance, Data)
end
end)

SelectionService.SelectionChanged:Connect(function()
local Selected = SelectionService:Get()
local SelectedCount = Utility.GetDictionaryLength(Selected)

if SelectedCount == 1 then
SelectedInstance = Selected[1]
else
SelectedInstance = nil
end

if SelectedInstance and not SelectedInstance:IsA("GuiObject") and not SelectedInstance:IsA("ScreenGui") then
SelectedInstance = nil
end

Interface:OnSelection(SelectedInstance)
end)
end

function CorrectionHandler:BuildFromImportData(ImportDataJSON : string)
local InterpretedData = AppImportInterpreter:InterpretJSONData(ImportDataJSON)

if InterpretedData and #InterpretedData > 0 then
Creator:CreateFromData(InterpretedData)
end
end

return CorrectionHandler
12 changes: 12 additions & 0 deletions src/FigmaImportAssistant/Main/Interface/Build/ImportInputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"AutoImportData" : {
"PlaceholderText": "Import Data",
"Type": "string"
},

"AutoImportButton" : {
"Text": "Auto Import",
"Name": "AutoImportButton",
"Type": "BaseButton"
}
}
6 changes: 6 additions & 0 deletions src/FigmaImportAssistant/Main/Interface/Build/Sections.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@
"Name" : "Alignment",
"Build" : ["AlignmentInputs"],
"Collapsed" : true
},

{
"Name" : "Auto Import",
"Build" : ["ImportInputs"],
"Collapsed" : true
}
]
4 changes: 2 additions & 2 deletions src/FigmaImportAssistant/Main/Interface/TextInputSection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ return function(MainContentList, IsItemSelected, Inputs, SectionBuildData)

local SectionFrame = Hydrate(BuildSectionFrame(MainContentList)) {
Visible = Computed(function()
if IsItemSelected:get() and Selected:get() and (not Selected:get():IsA("ScreenGui") or (SectionBuildData.Name or SectionBuildData.SizeX or SectionBuildData.SizeY)) then
if IsItemSelected:get() and Selected:get() and (not Selected:get():IsA("ScreenGui") or (SectionBuildData.Name or SectionBuildData.SizeX or SectionBuildData.SizeY or SectionBuildData.AutoImportData)) then
return true
else
return false
Expand All @@ -48,7 +48,7 @@ return function(MainContentList, IsItemSelected, Inputs, SectionBuildData)

local Input; Input = Component "TextInput" {
Enabled = Computed(function()
return IsItemSelected:get() and Selected:get() and (not Selected:get():IsA("ScreenGui") or (Name == "Name" or Name == "SizeX" or Name == "SizeY"))
return IsItemSelected:get() and Selected:get() and (not Selected:get():IsA("ScreenGui") or (Name == "Name" or Name == "SizeX" or Name == "SizeY" or Name == "AutoImportData"))
end),
Name = Name,
PlaceholderText = Data.PlaceholderText or Name,
Expand Down
51 changes: 41 additions & 10 deletions src/FigmaImportAssistant/Main/Interface/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ local BUILD_SECTIONS_DATA = require(BUILD_DATA.Sections)
local TEXT_INPUT_BUILD_DATA = require(BUILD_DATA.TextInputBuildData)
local INSTANCE_BUTTON_BUILD_DATA = require(BUILD_DATA.InstanceCreationButtons)
local ALIGNMENT_INPUT_BUILD_DATA = require(BUILD_DATA.AlignmentInputs)
local IMPORT_INPUT_BUILD_DATA = require(BUILD_DATA.ImportInputs)

-- Imports
local Packages = script.Parent.Parent.Packages
local Component = require(script.Parent.Component)
local SearchWidget = require(script.SearchWidget)
local Keybinds = require(script.Keybinds)
local Fusion = require(Packages.Fusion)
local InstanceCreationButton = require(script.InstanceCreationButton)
local TextInputSection = require(script.TextInputSection)
local CorrectionHandler = script.Parent.CorrectionHandler
local AppImportInterpreter = require(CorrectionHandler.AppImportInterpreter)
local Creator = require(CorrectionHandler.Creator)
local New = Fusion.New
local Children = Fusion.Children
local OnEvent = Fusion.OnEvent
Expand Down Expand Up @@ -64,16 +69,6 @@ function BuildTypes.TextInputSections(SectionFrame)
}
end

-- for Index, InputBox in Inputs do
-- if Index ~= "Name" and Index ~= "Image" then
-- Hydrate(InputBox) {
-- Visible = Computed(function()
-- return SelectedItem:get() ~= nil and not SelectedItem:get():IsA("ScreenGui")
-- end)
-- }
-- end
-- end

Hydrate(Inputs["Image"]) {
Visible = Computed(function()
local Success, _ = pcall(function()
Expand All @@ -93,6 +88,42 @@ function BuildTypes.AlignmentInputs(SectionFrame)
end
end

function BuildTypes.ImportInputs(SectionFrame)
for Index, Data in IMPORT_INPUT_BUILD_DATA do
if Data.Type == "BaseButton" then
Inputs[Data.Name] = Component "Button" {
Enabled = IsItemSelected,
Name = Data.Name,
Text = Data.Text,
LayoutOrder = 1,
Size = UDim2.new(1, 0, 0, 30),
Parent = SectionFrame,
Visible = IsItemSelected,

[OnEvent "Activated"] = Keybinds:AddKeybind(`ipt`, {}, function()
if SelectedItem:get() and Inputs["AutoImportData"].Text ~= "" then
local ImportDataJSON = Inputs["AutoImportData"].Text

local InterpretedData = AppImportInterpreter:InterpretJSONData(ImportDataJSON)

if InterpretedData then
Creator:CreateFromData(SelectedItem:get(), InterpretedData)
end

Inputs["AutoImportData"].Text = ""
end
end).Callback
}
else
Hydrate(CreateTextInputSection({[Index] = Data})) {
LayoutOrder = 0,
Parent = SectionFrame,
Visible = IsItemSelected,
}
end
end
end

-- Functions (extended)
local function BuildSections()
for _, SectionData in BUILD_SECTIONS_DATA do
Expand Down

0 comments on commit a61e048

Please sign in to comment.