Skip to content

Commit

Permalink
Fixes for image and text widget changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Apr 14, 2024
1 parent 34a478e commit e0ef08f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
13 changes: 9 additions & 4 deletions Assets/Scripts/Save/MetadataUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public struct WidgetMetadata
public uint groupId;
public int layerId;
public bool twoSided;
public float extrusionDepth;
public Color extrusionColor;
}

/// Sanitizes potentially-invalid data coming from the .tilt file.
Expand Down Expand Up @@ -155,8 +157,9 @@ TiltText ConvertTextWidgetToTiltText(TextWidget widget)
Pinned = widget.Pinned,
GroupId = groupIdMapping.GetId(widget.Group),
StrokeColor = widget.StrokeColor,
Font = "Oswald-Regular", // Not currently used
ExtrudeDepth = 0 // Not currently used
Font = "Oswald-Regular",
ExtrudeDepth = 0,
Mode = widget.Mode,
};
return text;
}
Expand Down Expand Up @@ -273,6 +276,8 @@ public static TiltImages75[] GetTiltImages(GroupIdMapping groupIdMapping)
val.GroupIds = new uint[ordered.Length];
val.LayerIds = new int[ordered.Length];
val.TwoSidedFlags = new bool[ordered.Length];
val.ExtrusionDepths = new float[ordered.Length];
val.ExtrusionColors = new Color[ordered.Length];
for (int i = 0; i < ordered.Length; ++i)
{
val.PinStates[i] = ordered[i].pinned;
Expand All @@ -281,8 +286,8 @@ public static TiltImages75[] GetTiltImages(GroupIdMapping groupIdMapping)
val.GroupIds[i] = ordered[i].groupId;
val.LayerIds[i] = ordered[i].layerId;
val.TwoSidedFlags[i] = ordered[i].twoSided;
val.ExtrusionDepth[i] = ordered[i].extrusionDepth;
val.ExtrusionColor[i] = ordered[i].extrusionColor;
val.ExtrusionDepths[i] = ordered[i].extrusionDepth;
val.ExtrusionColors[i] = ordered[i].extrusionColor;
}
imageIndex.Add(val);
}
Expand Down
5 changes: 3 additions & 2 deletions Assets/Scripts/Save/SketchMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ public class TiltImages75
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public int[] LayerIds { get; set; }
public bool[] TwoSidedFlags { get; set; }
public float ExtrusionDepth { get; set; }
public Color ExtrusionColor { get; set; }
public float[] ExtrusionDepths { get; set; }
public Color[] ExtrusionColors { get; set; }
}

[Serializable]
Expand All @@ -670,6 +670,7 @@ public class TiltText
public Color StrokeColor { get; set; }
public string Font { get; set; }
public float ExtrudeDepth { get; set; }
public TextWidgetMode Mode { get; set; }
public bool Pinned { get; set; }
public uint GroupId { get; set; }
public int LayerId { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/Widgets/ImageWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ public static void FromTiltImage(TiltImages75 tiltImage)
var groupIds = tiltImage.GroupIds;
var layerIds = tiltImage.LayerIds;
var twoSidedFlags = tiltImage.TwoSidedFlags;
var extrusionDepth = tiltImage.ExtrusionDepth;
var extrusionColor = tiltImage.ExtrusionColor;
var extrusionDepths = tiltImage.ExtrusionDepths;
var extrusionColors = tiltImage.ExtrusionColors;
for (int i = 0; i < tiltImage.Transforms.Length; ++i)
{
ImageWidget image = Instantiate(WidgetManager.m_Instance.ImageWidgetPrefab);
Expand All @@ -254,7 +254,7 @@ public static void FromTiltImage(TiltImages75 tiltImage)
image.SetMissing(tiltImage.AspectRatio, tiltImage.FileName);
}
image.SetSignedWidgetSize(tiltImage.Transforms[i].scale);
image.SetExtrusion(extrusionDepth, extrusionColor);
image.SetExtrusion(extrusionDepths[i], extrusionColors[i]);
image.Show(bShow: true, bPlayAudio: false);
image.transform.localPosition = tiltImage.Transforms[i].translation;
image.transform.localRotation = tiltImage.Transforms[i].rotation;
Expand Down
51 changes: 51 additions & 0 deletions Assets/Scripts/Widgets/TextWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@

using TMPro;
using UnityEngine;
using UnityEngine.TextCore.Text;

namespace TiltBrush
{
public enum TextWidgetMode
{
TextMeshPro,
Geometry
}

public class TextWidget : Media2dWidget
{
public TextMeshPro m_TextMeshPro;
public MeshRenderer m_MeshRenderer;
public MeshFilter m_MeshFilter;

public override float? AspectRatio =>
m_TextMeshPro.renderedWidth / m_TextMeshPro.renderedHeight;
Expand All @@ -30,6 +39,47 @@ protected override void UpdateScale()
UpdateCollider();
}

public void AssignFont(TMP_FontAsset fontAsset)
{
m_TextMeshPro.font = fontAsset;
}

public void AssignFont(string path)
{
var fontAsset = SvgTextUtils.ConvertOpenTypeToTMPro(path);
m_TextMeshPro.font = fontAsset;
}

public void AssignMesh(Mesh mesh)
{
Mode = TextWidgetMode.Geometry;
m_MeshFilter.mesh = mesh;
}

public TextWidgetMode Mode
{
get
{
return m_TextMeshPro.enabled ?
TextWidgetMode.TextMeshPro : TextWidgetMode.Geometry;
}
set
{
switch (value)
{
case TextWidgetMode.TextMeshPro:
m_TextMeshPro.enabled = true;
m_MeshRenderer.enabled = false;
break;
case TextWidgetMode.Geometry:
m_TextMeshPro.enabled = false;
m_MeshRenderer.enabled = true;
break;
}
}
}


public string Text
{
get => m_TextMeshPro.text;
Expand Down Expand Up @@ -107,6 +157,7 @@ public static void FromTiltText(TiltText tiltText)
textWidget.TextColor = tiltText.FillColor;
textWidget.StrokeColor = tiltText.StrokeColor;
textWidget.Text = tiltText.Text;
textWidget.Mode = tiltText.Mode;
textWidget.Group = App.GroupManager.GetGroupFromId(tiltText.GroupId);
textWidget.SetCanvas(App.Scene.GetOrCreateLayer(tiltText.LayerId));

Expand Down

0 comments on commit e0ef08f

Please sign in to comment.