Skip to content

Commit

Permalink
Canvas Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioManoel committed Mar 6, 2023
1 parent e7eed67 commit 3667898
Show file tree
Hide file tree
Showing 27 changed files with 1,807 additions and 280 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
23 changes: 13 additions & 10 deletions Unity/Assets/Editor/Figma Convert/Code/API/APIService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using UnityEngine.Networking;
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;

Expand All @@ -9,10 +10,18 @@ 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}");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string json = reader.ReadToEnd();
return JsonUtility.FromJson<File>(json);
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) {
StreamReader reader = new StreamReader(response.GetResponseStream());
string json = reader.ReadToEnd();
return JsonUtility.FromJson<File>(json);
}

// HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// StreamReader reader = new StreamReader(response.GetResponseStream());
// string json = reader.ReadToEnd();
// return JsonUtility.FromJson<File>(json);
// return JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
// return JsonConvert.DeserializeObject(json);
}

public static string GetImage() {
Expand All @@ -37,12 +46,6 @@ public static bool DownloadImage(string url, string path) {
uwr.SendWebRequest();
while(!uwr.isDone){}
return uwr.responseCode == 200 ? true : false;

// HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// System.Drawing.Image img = System.Drawing.Image.FromStream(response.GetResponseStream());
// Response.ContentType = ;
// img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
}

public static string GetImageID(string id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ public class ObjectProperty {
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 @@ -15,7 +16,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
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
6 changes: 3 additions & 3 deletions Unity/Assets/Editor/Figma Convert/Code/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static void Start(int escala){

// Chamada da API
File apiDocument = APIService.GetDocument();
// Object apiDocument = APIService.GetDocument();
Global.apiImage = APIService.GetImage();

// Loop Pagina
Expand All @@ -26,10 +27,9 @@ public static void Start(int escala){
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.01f){
for(int j = 0; j<apiDocument.document.children[i].children.Length; j++){
ObjectProperty apiObj = apiDocument.document.children[i].children[j];
Builder objeto = new Builder(apiObj, empty, z, escala);
Builder objeto = new Builder(apiObj, empty, escala);
objeto.createObject();
}
empty.transform.Rotate(180.0f, 0f, 0f, Space.World);
Expand Down
40 changes: 10 additions & 30 deletions Unity/Assets/Editor/Figma Convert/Code/build/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,26 @@
public class Builder {
private ObjectProperty obj;
private GameObject parent;
private float z;
private int escala;
private GameObject gameObject;

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

public void createObject() {
if(!obj.visible)
return;
switch (obj.type) {
case "FRAME":
gameObject = new Frame(obj, z, escala).createObject();
break;
case "COMPONENT":
gameObject = new Frame(obj, z, escala).createObject();
break;
case "INSTANCE":
gameObject = new Frame(obj, z, escala).createObject();
break;
case "GROUP":
gameObject = new Frame(obj, z, escala).createObject();
break;
case "TEXT":
gameObject = new Text(obj, z).createObject();
break;
case "RECTANGLE":
gameObject = new Rectangle(obj, z, escala).createObject();
break;
case "VECTOR":
gameObject = new Vector(obj, z, escala).createObject();
break;
case "ELLIPSE":
gameObject = new Ellipse(obj, z, escala).createObject();
break;
}

if(obj.type == "TEXT")
gameObject = new Text(obj, escala).createObject();
else if(parent.name.Contains("Page"))
gameObject = new Canva(obj, escala).createObject();
else
gameObject = new Painel(obj, escala).createObject();

if(gameObject == null)
return;
setName();
Expand All @@ -56,6 +36,6 @@ private void setName() {
}

private void setParent() {
gameObject.transform.parent = parent.transform;
gameObject.transform.SetParent(parent.transform);
}
}
30 changes: 30 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,30 @@
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;
}

}
14 changes: 0 additions & 14 deletions Unity/Assets/Editor/Figma Convert/Code/build/Ellipse.cs

This file was deleted.

39 changes: 0 additions & 39 deletions Unity/Assets/Editor/Figma Convert/Code/build/Frame.cs

This file was deleted.

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;
}

}
23 changes: 0 additions & 23 deletions Unity/Assets/Editor/Figma Convert/Code/build/Rectangle.cs

This file was deleted.

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

public class Text : Object {

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

public GameObject createObject() {
gameObject = new GameObject("3D Text");
TextMesh textMesh = gameObject.AddComponent<TextMesh>() as TextMesh;
setCharacters(textMesh);
setFontSize(textMesh);
italicAndNegrito(textMesh);
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);
setPosition();
setColor();
return gameObject;
}

public void italicAndNegrito(TextMesh textMesh) {
if(obj.style.italic == true && obj.style.fontWeight >= 700)
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;
textMesh.fontStyle = FontStyle.Italic;*/
}

public void setFontSize(TextMesh textMesh) {
textMesh.fontSize = obj.style.fontSize;
}

public void setCharacters(TextMesh textMesh) {
textMesh.text = obj.characters;
}
public void setColorText() {}
}
Loading

0 comments on commit 3667898

Please sign in to comment.