Skip to content

Commit

Permalink
Merge pull request #12 from uramakilab/canva
Browse files Browse the repository at this point in the history
To the main!
  • Loading branch information
marcgc21 authored Apr 4, 2023
2 parents 67bcc87 + 9fb467a commit f8f3ccb
Show file tree
Hide file tree
Showing 54 changed files with 25,036 additions and 823 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
*.meta
/Unity/Library
/Unity/.vscode
/Unity/Logs
/Unity/Packages
/Unity/ProjectSettings
/Unity/Temp
/Unity/ProBuilder Data
/Unity/UserSettings
/Unity/Assets/Scenes
/Unity/Assets/TextMesh Pro
/Unity/Assets/Figma Convert/testBuilder
/Image Unity
/Figma-API.md
Expand Down
55 changes: 55 additions & 0 deletions Unity/Assets/Editor/Figma Convert/Code/API/APIService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using UnityEngine;
using UnityEngine.Networking;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;

abstract class APIService {

public static File GetDocument() {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"https://api.figma.com/v1/files{Global.documentID}");
request.Headers.Add("Authorization", $"Bearer {Global.token}");
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) {
StreamReader reader = new StreamReader(response.GetResponseStream());
string json = reader.ReadToEnd();
Regex regex = new Regex("(|(Distance)|(RotationX)|(RotationY))#[0-9]+:[0-9]+");
json = regex.Replace(json, "$1");
return JsonUtility.FromJson<File>(json);
}
}

public static string GetImage() {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"https://api.figma.com/v1/files{Global.documentID}/images");
request.Headers.Add("Authorization", $"Bearer {Global.token}");
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) {
StreamReader reader = new StreamReader(response.GetResponseStream());
return reader.ReadToEnd();
}
}

public static string ContentType(string url) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string type = response.ContentType;
return type.Remove(0, type.IndexOf("/")+1);
}

public static bool DownloadImage(string url, string path) {
UnityWebRequest uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
uwr.downloadHandler = new DownloadHandlerFile(path);
uwr.SendWebRequest();
while(!uwr.isDone){}
return uwr.responseCode == 200 ? true : false;
}

public static string GetImageID(string id) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"https://api.figma.com/v1/images{Global.documentID}?ids={id}");
request.Headers.Add("Authorization", "Bearer "+ Global.token);
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) {
StreamReader reader = new StreamReader(response.GetResponseStream());
return reader.ReadToEnd();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Collections.Generic;

[System.Serializable]
public class ObjectProperty {
public string id;
public string name;
public bool visible = true;
public string type;
public string scrollBehavior;
public ComponentProperty componentPropertyDefinitions;
public string blendMode;
public ObjectProperty[] children;
public ObjectProperty[] children = null;
public Absolute absoluteBoundingBox;
public Absolute absoluteRenderBounds;
public Constraints constraits;
Expand All @@ -14,7 +18,7 @@ public class ObjectProperty {
public Fills[] fills;
public Fills[] strokes;
public float cornerRadius;
public float strokeWeight;
public float strokeWeight = 0;
public string storekeAlign;
public Color backgroundColor;
// public LayoutGrids[] layoutGrids;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[System.Serializable]
public class ComponentProperty {
public PropertyDefinitions Distance;
public PropertyDefinitions RotationX;
public PropertyDefinitions RotationY;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
public class Fills {
public string blendMode;
public string type;
public bool visible = true;
public string scaleMode;
public string imageRef;
public Color color;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[System.Serializable]
public class PropertyDefinitions{
public string type;
public string defaultValue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static string startHttpListener() {
HttpListenerResponse response = context.Response;

// Construct a response.
string responseString = "<script>window.close()</script>";
string responseString = "<html><body><h1>Pode fechar e voltar para o Unity</h1></body></html>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
using UnityEngine;

public class Core {
public static void Start(string token, string documentID, int escala){
public static void Start(int escala){

int cont = 0;
for(int i = documentID.Length-1; i > 0; i--){
if(documentID[i] == '/'){
for(int i = Global.documentID.Length-1; i > 0; i--){
if(Global.documentID[i] == '/'){
cont++;
if(cont == 1)
documentID = documentID.Remove(i);
Global.documentID = Global.documentID.Remove(i);
else if(cont == 2){
documentID = documentID.Remove(0,i);
Global.documentID = Global.documentID.Remove(0,i);
break;
}
}
}

// Chamada da API
File apiDocument = APIService.GetDocument(token, documentID);
string apiImage = APIService.GetImages(token, documentID);
File apiDocument = APIService.GetDocument();
Global.apiImage = APIService.GetImage();

// Loop Pagina
for(int i = 0; i<apiDocument.document.children.Length; i++){

GameObject empty = new GameObject("Page " + (i+1));

// Loop Objeto
float z = 0;
for(int j = 0; j<apiDocument.document.children[i].children.Length; j++, z+=0.1f){
for(int j = 0; j<apiDocument.document.children[i].children.Length; j++){
ObjectProperty apiObj = apiDocument.document.children[i].children[j];
Builder objeto = new Builder(documentID, token, apiObj, apiImage, empty, z, escala);
Builder objeto = new Builder(apiObj, empty, escala);
objeto.createObject();
}
empty.transform.Rotate(180.0f, 0f, 0f, Space.World);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ private void OnGUI() {
EditorGUILayout.Space();

GUILayout.Label("Account", EditorStyles.label); //Escrita
token = EditorGUILayout.TextField("Enter Token:", token);
documentID = EditorGUILayout.TextField("Enter URL Document:", documentID);
Global.token = token = EditorGUILayout.TextField("Enter Token:", token);
Global.documentID = documentID = EditorGUILayout.TextField("Enter URL Document:", documentID);
EditorGUILayout.Space();

if(GUILayout.Button("Login Browser")) {
Expand All @@ -35,7 +35,7 @@ private void OnGUI() {

if(GUILayout.Button("Download Projet")) {
// try {
Core.Start(token, documentID, escala);
Core.Start(escala);
// }
// catch (Exception e){
// Debug.Log(e);
Expand Down
53 changes: 53 additions & 0 deletions Unity/Assets/Editor/Figma Convert/Code/build/Builder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using UnityEngine;
using System.Text;

public class Builder {
private ObjectProperty obj;
private GameObject parent;
private int escala;
private GameObject gameObject;

public Builder(ObjectProperty apiObj, GameObject parent, int escala) {
this.obj = apiObj;
this.parent = parent;
this.escala = escala;
}

public void createObject() {
if(!obj.visible)
return;

if(obj.type == "TEXT")
gameObject = new Text(obj, escala).createObject();
else if(parent.name.Contains("Page")) {
if(obj.componentPropertyDefinitions.Distance != null) {
Global.objEixoZ = (float)(float.Parse(obj.componentPropertyDefinitions.Distance.defaultValue) * -1);
Global.objRotationX = (float) float.Parse(obj.componentPropertyDefinitions.RotationX.defaultValue);
Global.objRotationY = (float) float.Parse(obj.componentPropertyDefinitions.RotationY.defaultValue);
}
else {
Global.objEixoZ = 0;
Global.objRotationX = 0;
Global.objRotationY = 0;
}
gameObject = new Canva(obj, escala).createObject();
gameObject.transform.Rotate(Global.objRotationX, Global.objRotationY, 0f, Space.World);
}
else
gameObject = new Painel(obj, escala).createObject();

if(gameObject == null)
return;
setName();
setParent();
Debug.Log(obj.name + " Criado com Sucesso");
}

private void setName() {
gameObject.name = obj.name;
}

private void setParent() {
gameObject.transform.SetParent(parent.transform);
}
}
29 changes: 29 additions & 0 deletions Unity/Assets/Editor/Figma Convert/Code/build/Canva.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class Canva : Object {

public Canva(ObjectProperty obj, int escala) : base(obj, escala) {}

public GameObject createObject() {
GameObject gameObject = new GameObject();
gameObject.AddComponent<Canvas>();

Canvas canvas = gameObject.GetComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
gameObject.AddComponent<CanvasScaler>();
gameObject.AddComponent<GraphicRaycaster>();
RectTransform rectTransform = canvas.GetComponent<RectTransform>();
setSize(rectTransform);
setPosition(rectTransform);

GameObject painel = new Painel(obj, escala).createObject();
painel.name = obj.name;
painel.transform.SetParent(gameObject.transform);
return gameObject;
}
}
27 changes: 27 additions & 0 deletions Unity/Assets/Editor/Figma Convert/Code/build/Painel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using UnityEngine;
using UnityEngine.UI;

public class Painel : Object {

public Painel(ObjectProperty obj, int escala) : base(obj, escala) {}

public GameObject createObject() {
gameObject = new GameObject();
Image painel = gameObject.AddComponent<Image>();
RectTransform rectTransform = painel.GetComponent<RectTransform>();
setSize(rectTransform);
setPosition(rectTransform);
setColor(painel);
setCornerRadius();
setBorder();

if(obj.children != null) {
for(int i = 0; i < obj.children.Length; i++) {
Builder objeto = new Builder(obj.children[i], gameObject, escala);
objeto.createObject();
}
}
return gameObject;
}

}
57 changes: 57 additions & 0 deletions Unity/Assets/Editor/Figma Convert/Code/build/Text.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using UnityEngine;
// using UnityEngine.UI;
using TMPro;

public class Text : Object {

public Text(ObjectProperty obj, int escala) : base(obj, escala){}

public GameObject createObject() {
gameObject = new GameObject();
TextMeshProUGUI text = gameObject.AddComponent<TextMeshProUGUI>();
setCharacters(text);
setFontSize(text);
italicAndNegrito(text);
RectTransform rectTransform = gameObject.GetComponent<RectTransform>();
setSizeFont();
setSizeRectFont(rectTransform);
setPosition(rectTransform);
setColorText();
gameObject.transform.Rotate(180.0f, 0f, 0f, Space.World);
return gameObject;
}

public void setCharacters(TextMeshProUGUI text) {
text.text = obj.characters;
}

public void setFontSize(TextMeshProUGUI text) {
text.fontSize = obj.style.fontSize;
}

public void setSizeFont() {
width = (float)1/escala;
height = (float)1/escala;
Vector3 size = new Vector3(width, height, 1);
gameObject.transform.localScale = size;
}

public void setSizeRectFont(RectTransform rectTransform) {
width = obj.absoluteBoundingBox.width;
height = obj.absoluteBoundingBox.height;
rectTransform.sizeDelta = new Vector2(width, height);
width = width/escala;
height = height/escala;
}

public void italicAndNegrito(TextMeshProUGUI text) {
/*if(obj.style.italic == true && obj.style.fontWeight >= 700)
textMesh.fontStyle = FontStyle.BoldAndItalic;
else if(obj.style.fontWeight >= 700)
textMesh.fontStyle = FontStyle.Bold;
else if(obj.style.italic == true)
textMesh.fontStyle = FontStyle.Italic;*/
}

public void setColorText() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ private void CreateTriangles() {
mesh.subMeshCount = 2;
mesh.SetTriangles(triangles, 0);
mesh.SetTriangles(trianglesBorder, 1);
// mesh.triangles = trianglesBorder;
}

private int CreateTopFace (int[] triangles, int[] trianglesBorder,int t, int ring) {
Expand Down
8 changes: 8 additions & 0 deletions Unity/Assets/Editor/Figma Convert/Code/classes/Global.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
static class Global {
public static string documentID;
public static string token;
public static string apiImage;
public static float objEixoZ;
public static float objRotationX;
public static float objRotationY;
}
Loading

0 comments on commit f8f3ccb

Please sign in to comment.