Skip to content

Commit

Permalink
Minimal port of text tool - just enough to allow API creation of text…
Browse files Browse the repository at this point in the history
… objects and save/load support
  • Loading branch information
andybak committed Apr 13, 2024
1 parent ce18925 commit 569f30e
Show file tree
Hide file tree
Showing 13 changed files with 942 additions and 2 deletions.
646 changes: 646 additions & 0 deletions Assets/Prefabs/TextWidget.prefab

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Assets/Prefabs/TextWidget.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -30934,6 +30934,7 @@ MonoBehaviour:
type: 3}
m_VideoWidgetPrefab: {fileID: 854942063617328480, guid: a1bf54eee52cec14e8207351740867f3,
type: 3}
m_TextWidgetPrefab: {fileID: 11466202, guid: c28bc4e3ed71248b889df2a70d873d0b, type: 3}
m_CameraPathWidgetPrefab: {fileID: 114268259032398848, guid: a933b2a19614a6c4da08a0143f0ec912,
type: 3}
m_CameraPathPositionKnotPrefab: {fileID: 1334758335721002, guid: 38b506951b7a81e4e9a6c65a9a20efa9,
Expand Down
6 changes: 6 additions & 0 deletions Assets/Scripts/API/ApiMethods.Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ private static ImageWidget _GetActiveImage(int index)
return WidgetManager.m_Instance.ActiveImageWidgets[index].WidgetScript;
}

private static TextWidget _GetActiveTextWidget(int index)
{
index = _NegativeIndexing(index, WidgetManager.m_Instance.ActiveImageWidgets);
return WidgetManager.m_Instance.ActiveTextWidgets[index].WidgetScript;
}

private static VideoWidget _GetActiveVideo(int index)
{
index = _NegativeIndexing(index, WidgetManager.m_Instance.ActiveVideoWidgets);
Expand Down
27 changes: 27 additions & 0 deletions Assets/Scripts/API/ApiMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,33 @@ private static ReferenceImage _LoadReferenceImage(string location)
return image;
}

[ApiEndpoint("text.add", "Adds a text widget to the sketch")]
public static void AddText(string text)
{
var tr = TrTransform.TR(
ApiManager.Instance.BrushPosition,
ApiManager.Instance.BrushRotation
);

var cmd = new CreateWidgetCommand(
WidgetManager.m_Instance.TextWidgetPrefab, tr, null, true
);

SketchMemoryScript.m_Instance.PerformAndRecordCommand(cmd);

var textWidget = cmd.Widget as TextWidget;
if (textWidget != null)
{
textWidget.Text = text;
textWidget.Show(true);
cmd.SetWidgetCost(textWidget.GetTiltMeterCost());
}

WidgetManager.m_Instance.WidgetsDormant = false;
SketchControlsScript.m_Instance.EatGazeObjectInput();
SelectionManager.m_Instance.RemoveFromSelection(false);
}

[ApiEndpoint("video.import", "Imports a video given a url or a filename in Media Library\\Videos")]
public static VideoWidget ImportVideo(string location)
{
Expand Down
6 changes: 6 additions & 0 deletions Assets/Scripts/Commands/CreateWidgetCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ protected override void OnRedo()
m_Widget.transform.position = m_SpawnXf.translation;

// Widget type specific initialization.
// Should we have a default case here? Would need testing.
if (m_Widget is StencilWidget)
{
m_Widget.transform.parent = m_Canvas.transform;
Expand All @@ -110,6 +111,11 @@ protected override void OnRedo()
m_Widget.transform.parent = m_Canvas.transform;
m_Widget.Show(true);
}
else if (m_Widget is TextWidget)
{
m_Widget.transform.parent = m_Canvas.transform;
m_Widget.Show(true);
}
else if (m_Widget is VideoWidget)
{
m_Widget.transform.parent = m_Canvas.transform;
Expand Down
21 changes: 21 additions & 0 deletions Assets/Scripts/Save/MetadataUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,27 @@ public static TiltModels75[] GetTiltModels(GroupIdMapping groupIdMapping)
.OrderBy(ByModelLocation).ToArray();
}

public static TiltText[] GetTiltText(GroupIdMapping groupIdMapping)
{
return WidgetManager.m_Instance.TextWidgets.Where(x => x.gameObject.activeSelf).Select(x => ConvertTextWidgetToTiltText(x)).ToArray();

TiltText ConvertTextWidgetToTiltText(TextWidget widget)
{
TiltText text = new TiltText
{
Transform = widget.SaveTransform,
Text = widget.Text,
FillColor = widget.TextColor,
Pinned = widget.Pinned,
GroupId = groupIdMapping.GetId(widget.Group),
StrokeColor = widget.StrokeColor,
Font = "Oswald-Regular", // Not currently used
ExtrudeDepth = 0 // Not currently used
};
return text;
}
}

public static TiltVideo[] GetTiltVideos(GroupIdMapping groupIdMapping)
{
return WidgetManager.m_Instance.VideoWidgets.Where(x => x.gameObject.activeSelf).Select(x => ConvertVideoToTiltVideo(x)).ToArray();
Expand Down
4 changes: 4 additions & 0 deletions Assets/Scripts/Save/SaveLoadScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,10 @@ public bool Load(SceneFileInfo fileInfo, bool bAdditive = false)
{
WidgetManager.m_Instance.SetDataFromTilt(jsonData.Videos);
}
if (jsonData.TextWidgets != null)
{
WidgetManager.m_Instance.SetDataFromTilt(jsonData.TextWidgets);
}
}
if (jsonData.Mirror != null)
{
Expand Down
17 changes: 17 additions & 0 deletions Assets/Scripts/Save/SketchMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,20 @@ public class Mirror
public TrTransform Transform { get; set; }
}

[Serializable]
public class TiltText
{
public TrTransform Transform { get; set; }
public string Text { get; set; }
public Color FillColor { get; set; }
public Color StrokeColor { get; set; }
public string Font { get; set; }
public float ExtrudeDepth { get; set; }
public bool Pinned { get; set; }
public uint GroupId { get; set; }
public int LayerId { get; set; }
}

[Serializable]
public class TiltVideo
{
Expand Down Expand Up @@ -787,5 +801,8 @@ public TiltImages75b[] Images
public string ApplicationName { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string ApplicationVersion { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public TiltText[] TextWidgets { get; set; }
}
} // namespace TiltBrush
1 change: 1 addition & 0 deletions Assets/Scripts/Save/SketchSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public SketchMetadata GetSketchMetadata()
ModelIndex = MetadataUtils.GetTiltModels(m_GroupIdMapping),
ImageIndex = MetadataUtils.GetTiltImages(m_GroupIdMapping),
Videos = MetadataUtils.GetTiltVideos(m_GroupIdMapping),
TextWidgets = MetadataUtils.GetTiltText(m_GroupIdMapping),
Mirror = PointerManager.m_Instance.SymmetryWidgetToMirror(),
GuideIndex = MetadataUtils.GetGuideIndex(m_GroupIdMapping),
Palette = CustomColorPaletteStorage.m_Instance.GetPaletteForSaving(),
Expand Down
75 changes: 73 additions & 2 deletions Assets/Scripts/WidgetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public class WidgetManager : MonoBehaviour
[SerializeField] GameObject m_WidgetPinPrefab;
[SerializeField] ImageWidget m_ImageWidgetPrefab;
[SerializeField] VideoWidget m_VideoWidgetPrefab;
[SerializeField] TextWidget m_TextWidgetPrefab;
[SerializeField] CameraPathWidget m_CameraPathWidgetPrefab;
[SerializeField] private GameObject m_CameraPathPositionKnotPrefab;
[SerializeField] private GameObject m_CameraPathRotationKnotPrefab;
Expand Down Expand Up @@ -144,6 +145,7 @@ public class WidgetManager : MonoBehaviour
private List<TypedWidgetData<ModelWidget>> m_ModelWidgets;
private List<TypedWidgetData<StencilWidget>> m_StencilWidgets;
private List<TypedWidgetData<ImageWidget>> m_ImageWidgets;
private List<TypedWidgetData<TextWidget>> m_TextWidgets;
private List<TypedWidgetData<VideoWidget>> m_VideoWidgets;
private List<TypedWidgetData<CameraPathWidget>> m_CameraPathWidgets;

Expand Down Expand Up @@ -300,6 +302,7 @@ public void Init()
m_ModelWidgets = new List<TypedWidgetData<ModelWidget>>();
m_StencilWidgets = new List<TypedWidgetData<StencilWidget>>();
m_ImageWidgets = new List<TypedWidgetData<ImageWidget>>();
m_TextWidgets = new List<TypedWidgetData<TextWidget>>();
m_VideoWidgets = new List<TypedWidgetData<VideoWidget>>();
m_CameraPathWidgets = new List<TypedWidgetData<CameraPathWidget>>();

Expand Down Expand Up @@ -327,6 +330,7 @@ public void Init()
public ModelWidget ModelWidgetPrefab { get { return m_ModelWidgetPrefab; } }
public ImageWidget ImageWidgetPrefab { get { return m_ImageWidgetPrefab; } }
public VideoWidget VideoWidgetPrefab { get { return m_VideoWidgetPrefab; } }
public TextWidget TextWidgetPrefab { get { return m_TextWidgetPrefab; } }
public CameraPathWidget CameraPathWidgetPrefab { get { return m_CameraPathWidgetPrefab; } }
public GameObject CameraPathPositionKnotPrefab { get { return m_CameraPathPositionKnotPrefab; } }
public GameObject CameraPathRotationKnotPrefab { get { return m_CameraPathRotationKnotPrefab; } }
Expand Down Expand Up @@ -377,6 +381,13 @@ private IEnumerable<GrabWidgetData> GetAllActiveGrabWidgets()
yield return m_ImageWidgets[i];
}
}
for (int i = 0; i < m_TextWidgets.Count; ++i)
{
if (m_TextWidgets[i].m_WidgetObject.activeSelf)
{
yield return m_TextWidgets[i];
}
}
for (int i = 0; i < m_VideoWidgets.Count; ++i)
{
if (m_VideoWidgets[i].m_WidgetObject.activeSelf)
Expand All @@ -398,7 +409,7 @@ public IEnumerable<GrabWidgetData> MediaWidgets
get
{
IEnumerable<GrabWidgetData> ret = m_ModelWidgets;
return ret.Concat(m_ImageWidgets).Concat(m_VideoWidgets);
return ret.Concat(m_ImageWidgets).Concat(m_VideoWidgets).Concat(m_TextWidgets);
}
}

Expand Down Expand Up @@ -581,7 +592,10 @@ public bool CameraPathsVisible

public bool HasSelectableWidgets()
{
return (m_ModelWidgets.Count > 0) || (m_ImageWidgets.Count > 0) || (m_VideoWidgets.Count > 0) ||
return m_ModelWidgets.Count > 0 ||
m_ImageWidgets.Count > 0 ||
m_TextWidgets.Count > 0 ||
m_VideoWidgets.Count > 0 ||
(!m_StencilsDisabled && m_StencilWidgets.Count > 0);
}

Expand Down Expand Up @@ -696,6 +710,15 @@ public void SetDataFromTilt(CameraPathMetadata[] cameraPaths)
}
}

public void SetDataFromTilt(TiltText[] tiltText)
{
for (int i = 0; i < tiltText.Length; ++i)
{
TextWidget.FromTiltText(tiltText[i]);
}
}


public void SetDataFromTilt(TiltVideo[] value)
{
m_loadingTiltVideos = value;
Expand Down Expand Up @@ -954,6 +977,16 @@ public IEnumerable<VideoWidget> VideoWidgets
}
}

public IEnumerable<TextWidget> TextWidgets
{
get
{
return m_TextWidgets
.Select(w => w == null ? null : w.WidgetScript)
.Where(w => w != null);
}
}

public IEnumerable<ModelWidget> NonExportableModelWidgets
{
get
Expand Down Expand Up @@ -1013,6 +1046,7 @@ public List<GrabWidget> GetAllUnselectedActiveWidgets(CanvasScript canvas)
if (canvas == null) return widgets; // Return empty list
GetUnselectedActiveWidgetsInList(m_ModelWidgets);
GetUnselectedActiveWidgetsInList(m_ImageWidgets);
GetUnselectedActiveWidgetsInList(m_TextWidgets);
GetUnselectedActiveWidgetsInList(m_VideoWidgets);
if (!m_StencilsDisabled)
{
Expand Down Expand Up @@ -1043,6 +1077,7 @@ public void RefreshPinAndUnpinLists()

RefreshPinUnpinWidgetList(m_ModelWidgets);
RefreshPinUnpinWidgetList(m_ImageWidgets);
RefreshPinUnpinWidgetList(m_TextWidgets);
RefreshPinUnpinWidgetList(m_VideoWidgets);
RefreshPinUnpinWidgetList(m_StencilWidgets);

Expand Down Expand Up @@ -1117,6 +1152,10 @@ public void RegisterGrabWidget(GameObject rWidget)
{
m_ImageWidgets.Add(new TypedWidgetData<ImageWidget>(image));
}
else if (generic is TextWidget textWidget)
{
m_TextWidgets.Add(new TypedWidgetData<TextWidget>(textWidget));
}
else if (generic is VideoWidget video)
{
m_VideoWidgets.Add(new TypedWidgetData<VideoWidget>(video));
Expand Down Expand Up @@ -1170,11 +1209,40 @@ public void UnregisterGrabWidget(GameObject rWidget)
if (RemoveFrom(m_ModelWidgets, rWidget)) { return; }
if (RemoveFrom(m_StencilWidgets, rWidget)) { return; }
if (RemoveFrom(m_ImageWidgets, rWidget)) { return; }
if (RemoveFrom(m_TextWidgets, rWidget)) { return; }
if (RemoveFrom(m_VideoWidgets, rWidget)) { return; }
if (RemoveFrom(m_CameraPathWidgets, rWidget)) { return; }
RemoveFrom(m_GrabWidgets, rWidget);
}

public TextWidget GetNearestTextWidget(Vector3 pos, float maxDepth)
{
var widgetList = ActiveTextWidgets.Select(x => x.m_WidgetScript);
return GetNearestGrabWidget(pos, maxDepth, widgetList) as TextWidget;
}

public GrabWidget GetNearestGrabWidget(Vector3 pos, float maxDepth, IEnumerable<GrabWidget> widgetList)
{
GrabWidget bestWidget = null;
float leastDistance = float.MaxValue;
foreach (var widget in widgetList)
{
Vector3 dropper_QS;
Vector3 dropper_GS = pos;
Matrix4x4 xfQuadFromGlobal = widget.transform.worldToLocalMatrix;
dropper_QS = xfQuadFromGlobal.MultiplyPoint3x4(dropper_GS);
if (Mathf.Abs(dropper_QS.z) < leastDistance
&& Mathf.Abs(dropper_QS.x) <= 0.5f
&& Mathf.Abs(dropper_QS.y) <= 0.5f
&& Mathf.Abs(dropper_QS.z) <= maxDepth / Mathf.Abs(widget.GetSignedWidgetSize()))
{
bestWidget = widget;
leastDistance = Mathf.Abs(dropper_QS.z);
}
}
return bestWidget;
}

public ImageWidget GetNearestImage(Vector3 pos, float maxDepth, ref Vector3 sampleLoc)
{
ImageWidget bestImage = null;
Expand Down Expand Up @@ -1316,6 +1384,7 @@ public void DestroyAllWidgets()
{
DestroyWidgetList(m_ModelWidgets);
DestroyWidgetList(m_ImageWidgets);
DestroyWidgetList(m_TextWidgets);
DestroyWidgetList(m_VideoWidgets);
DestroyWidgetList(m_StencilWidgets);
DestroyWidgetList(m_CameraPathWidgets, false);
Expand Down Expand Up @@ -1483,6 +1552,8 @@ public void TossNearestWidget()

public List<TypedWidgetData<ImageWidget>> ActiveImageWidgets =>
m_ImageWidgets.Where(w => w.WidgetScript.gameObject.activeSelf).ToList();
public List<TypedWidgetData<TextWidget>> ActiveTextWidgets =>
m_TextWidgets.Where(w => w.WidgetScript.gameObject.activeSelf).ToList();
public List<TypedWidgetData<ModelWidget>> ActiveModelWidgets =>
m_ModelWidgets.Where(w => w.WidgetScript.gameObject.activeSelf).ToList();
public List<TypedWidgetData<VideoWidget>> ActiveVideoWidgets =>
Expand Down
Loading

0 comments on commit 569f30e

Please sign in to comment.