Skip to content

Commit

Permalink
Added FormattableTextMeshPro.
Browse files Browse the repository at this point in the history
  • Loading branch information
Clark committed Jan 10, 2024
1 parent 09e7bdd commit c1ad9a4
Show file tree
Hide file tree
Showing 27 changed files with 474 additions and 532 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* MIT License
*
* Copyright (c) 2018 Clark Yang
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

using TMPro.EditorUtilities;
using UnityEditor;
using UnityEngine;

namespace Loxodon.Framework.Views.TextMeshPro.Editor
{
[CustomEditor(typeof(FormattableTextMeshPro), true), CanEditMultipleObjects]
public class FormattableTextMeshProEditorPanel : TMP_EditorPanel
{
static readonly GUIContent k_FormatLabel = new GUIContent("Format", "text formatting");
static readonly GUIContent k_ParameterCountLabel = new GUIContent("Parameter Count", "Parameter Count");

SerializedProperty m_FormatProp;
SerializedProperty m_ParameterCountProp;

protected override void OnEnable()
{
base.OnEnable();
m_FormatProp = serializedObject.FindProperty("m_Format");
m_ParameterCountProp = serializedObject.FindProperty("m_ParameterCount");
}

public override void OnInspectorGUI()
{
// Make sure Multi selection only includes TMP Text objects.
if (IsMixSelectionTypes()) return;

serializedObject.Update();

DrawFormatParameters();

DrawMainSettings();

DrawExtraSettings();

EditorGUILayout.Space();

if (serializedObject.ApplyModifiedProperties() || m_HavePropertiesChanged)
{
m_TextComponent.havePropertiesChanged = true;
m_HavePropertiesChanged = false;
EditorUtility.SetDirty(target);
}
}

protected void DrawFormatParameters()
{
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_FormatProp, k_FormatLabel);
if (EditorGUI.EndChangeCheck())
{
m_HavePropertiesChanged = true;
}

EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_ParameterCountProp, k_ParameterCountLabel);
if (EditorGUI.EndChangeCheck())
{
m_HavePropertiesChanged = true;
}
}
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,24 @@
* SOFTWARE.
*/

using TMPro;
using TMPro.EditorUtilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace Loxodon.Framework.Views.TextMeshPro.Editor
{
[CustomEditor(typeof(FormattableTextMeshProUGUI), true), CanEditMultipleObjects]
public class FormattableTextMeshProUIEditorPanel : TMP_BaseEditorPanel
public class FormattableTextMeshProUIEditorPanel : TMP_EditorPanelUI
{
static readonly GUIContent k_RaycastTargetLabel = new GUIContent("Raycast Target", "Whether the text blocks raycasts from the Graphic Raycaster.");
static readonly GUIContent k_FormatLabel = new GUIContent("Format", "text formatting");
static readonly GUIContent k_ParameterCountLabel = new GUIContent("Parameter Count", "Parameter Count");

SerializedProperty m_RaycastTargetProp;
SerializedProperty m_FormatProp;
SerializedProperty m_ParameterCountProp;

protected override void OnEnable()
{
base.OnEnable();
m_RaycastTargetProp = serializedObject.FindProperty("m_RaycastTarget");
m_FormatProp = serializedObject.FindProperty("m_Format");
m_ParameterCountProp = serializedObject.FindProperty("m_ParameterCount");
}
Expand All @@ -64,40 +59,12 @@ public override void OnInspectorGUI()

EditorGUILayout.Space();

if (m_HavePropertiesChanged)
if (serializedObject.ApplyModifiedProperties() || m_HavePropertiesChanged)
{
m_HavePropertiesChanged = false;
m_TextComponent.havePropertiesChanged = true;
m_TextComponent.ComputeMarginSize();
m_HavePropertiesChanged = false;
EditorUtility.SetDirty(target);
}

serializedObject.ApplyModifiedProperties();
}

protected override void DrawExtraSettings()
{
Foldout.extraSettings = EditorGUILayout.Foldout(Foldout.extraSettings, k_ExtraSettingsLabel, true, TMP_UIStyleManager.boldFoldout);
if (Foldout.extraSettings)
{
EditorGUI.indentLevel += 1;

DrawMargins();

DrawGeometrySorting();

DrawRichText();

DrawRaycastTarget();

DrawParsing();

DrawKerning();

DrawPadding();

EditorGUI.indentLevel -= 1;
}
}

protected void DrawFormatParameters()
Expand All @@ -116,50 +83,5 @@ protected void DrawFormatParameters()
m_HavePropertiesChanged = true;
}
}

protected void DrawRaycastTarget()
{
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_RaycastTargetProp, k_RaycastTargetLabel);
if (EditorGUI.EndChangeCheck())
{
// Change needs to propagate to the child sub objects.
Graphic[] graphicComponents = m_TextComponent.GetComponentsInChildren<Graphic>();
for (int i = 1; i < graphicComponents.Length; i++)
graphicComponents[i].raycastTarget = m_RaycastTargetProp.boolValue;

m_HavePropertiesChanged = true;
}
}

// Method to handle multi object selection
protected override bool IsMixSelectionTypes()
{
GameObject[] objects = Selection.gameObjects;
if (objects.Length > 1)
{
for (int i = 0; i < objects.Length; i++)
{
if (objects[i].GetComponent<FormattableTextMeshProUGUI>() == null)
return true;
}
}
return false;
}
protected override void OnUndoRedo()
{
int undoEventId = Undo.GetCurrentGroup();
int lastUndoEventId = s_EventId;

if (undoEventId != lastUndoEventId)
{
for (int i = 0; i < targets.Length; i++)
{
//Debug.Log("Undo & Redo Performed detected in Editor Panel. Event ID:" + Undo.GetCurrentGroup());
TMPro_EventManager.ON_TEXTMESHPRO_UGUI_PROPERTY_CHANGED(true, targets[i] as FormattableTextMeshProUGUI);
s_EventId = undoEventId;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
{
if (textProperty.objectReferenceValue != null)
{
FormattableTextMeshProUGUI formableText = (FormattableTextMeshProUGUI)textProperty.objectReferenceValue;
IFormattableText formableText = (IFormattableText)textProperty.objectReferenceValue;
if (capacityProperty != null)
{
int count = formableText.ParameterCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,22 @@
* SOFTWARE.
*/

using TMPro;
using TMPro.EditorUtilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace Loxodon.Framework.Views.TextMeshPro.Editor
{
[CustomEditor(typeof(TemplateTextMeshProUGUI), true), CanEditMultipleObjects]
public class TemplateTextMeshProUIEditorPanel : TMP_BaseEditorPanel
public class TemplateTextMeshProUIEditorPanel : TMP_EditorPanelUI
{
static readonly GUIContent k_RaycastTargetLabel = new GUIContent("Raycast Target", "Whether the text blocks raycasts from the Graphic Raycaster.");
static readonly GUIContent k_TemplateLabel = new GUIContent("Template", "text template");

SerializedProperty m_RaycastTargetProp;
SerializedProperty m_TemplateProp;

protected override void OnEnable()
{
base.OnEnable();
m_RaycastTargetProp = serializedObject.FindProperty("m_RaycastTarget");
m_TemplateProp = serializedObject.FindProperty("m_Template");
}

Expand All @@ -54,48 +49,19 @@ public override void OnInspectorGUI()
serializedObject.Update();

DrawTemplateInput();
//DrawTextInput();

DrawMainSettings();

DrawExtraSettings();

EditorGUILayout.Space();

if (m_HavePropertiesChanged)
if (serializedObject.ApplyModifiedProperties() || m_HavePropertiesChanged)
{
m_HavePropertiesChanged = false;
m_TextComponent.havePropertiesChanged = true;
m_TextComponent.ComputeMarginSize();
m_HavePropertiesChanged = false;
EditorUtility.SetDirty(target);
}

serializedObject.ApplyModifiedProperties();
}

protected override void DrawExtraSettings()
{
Foldout.extraSettings = EditorGUILayout.Foldout(Foldout.extraSettings, k_ExtraSettingsLabel, true, TMP_UIStyleManager.boldFoldout);
if (Foldout.extraSettings)
{
EditorGUI.indentLevel += 1;

DrawMargins();

DrawGeometrySorting();

DrawRichText();

DrawRaycastTarget();

DrawParsing();

DrawKerning();

DrawPadding();

EditorGUI.indentLevel -= 1;
}
}

protected void DrawTemplateInput()
Expand All @@ -107,50 +73,5 @@ protected void DrawTemplateInput()
m_HavePropertiesChanged = true;
}
}

protected void DrawRaycastTarget()
{
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_RaycastTargetProp, k_RaycastTargetLabel);
if (EditorGUI.EndChangeCheck())
{
// Change needs to propagate to the child sub objects.
Graphic[] graphicComponents = m_TextComponent.GetComponentsInChildren<Graphic>();
for (int i = 1; i < graphicComponents.Length; i++)
graphicComponents[i].raycastTarget = m_RaycastTargetProp.boolValue;

m_HavePropertiesChanged = true;
}
}

// Method to handle multi object selection
protected override bool IsMixSelectionTypes()
{
GameObject[] objects = Selection.gameObjects;
if (objects.Length > 1)
{
for (int i = 0; i < objects.Length; i++)
{
if (objects[i].GetComponent<FormattableTextMeshProUGUI>() == null)
return true;
}
}
return false;
}
protected override void OnUndoRedo()
{
int undoEventId = Undo.GetCurrentGroup();
int lastUndoEventId = s_EventId;

if (undoEventId != lastUndoEventId)
{
for (int i = 0; i < targets.Length; i++)
{
//Debug.Log("Undo & Redo Performed detected in Editor Panel. Event ID:" + Undo.GetCurrentGroup());
TMPro_EventManager.ON_TEXTMESHPRO_UGUI_PROPERTY_CHANGED(true, targets[i] as FormattableTextMeshProUGUI);
s_EventId = undoEventId;
}
}
}
}
}
Loading

0 comments on commit c1ad9a4

Please sign in to comment.