Skip to content

Commit

Permalink
refactor(TextObject): use text object interface to reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Sep 3, 2022
1 parent 54c4656 commit 760c6ac
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 118 deletions.
5 changes: 0 additions & 5 deletions Assets/JCSUnity/Scripts/Effects/JCS_AlphaObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
#define TMP_PRO

using UnityEngine;
using UnityEngine.UI;

#if TMP_PRO
using TMPro;
#endif

namespace JCSUnity
{
Expand Down
23 changes: 1 addition & 22 deletions Assets/JCSUnity/Scripts/UI/JCS_TextDeltaNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@

using System;
using UnityEngine;
using UnityEngine.UI;

#if TMP_PRO
using TMPro;
#endif

namespace JCSUnity
{
/// <summary>
/// Like `JCS_DeltaNumber`, but instead of altering the sprite we
/// alter text instead.
/// </summary>
public class JCS_TextDeltaNumber : MonoBehaviour
public class JCS_TextDeltaNumber : JCS_TextObject
{
/* Variables */

Expand Down Expand Up @@ -59,18 +54,6 @@ public class JCS_TextDeltaNumber : MonoBehaviour
[SerializeField]
private string mFullString = "";

[Header("** Initialize Variables (JCS_TextDeltaNumber) **")]

[Tooltip("Target text renderer.")]
[SerializeField]
private Text mTextContainer = null;

#if TMP_PRO
[Tooltip("Target text renderer.")]
[SerializeField]
private TextMeshPro mTextMesh = null;
#endif

[Header("** Runtime Variables (JCS_TextDeltaNumber) **")]

[Tooltip("Current number that will turn into string.")]
Expand Down Expand Up @@ -130,10 +113,6 @@ public class JCS_TextDeltaNumber : MonoBehaviour

/* Setter/Getter */

public Text TextContainer { get { return this.mTextContainer; } set { this.mTextContainer = value; } }
#if TMP_PRO
public TextMeshPro TextMesh { get { return this.mTextMesh; } set { this.mTextMesh = value; } }
#endif
public float CurrentNumber { get { return this.mCurrentNumber; } set { this.mCurrentNumber = value; } }
public float TargetNumber
{
Expand Down
6 changes: 4 additions & 2 deletions Assets/JCSUnity/Scripts/UI/JCS_TextObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public class JCS_TextObject : MonoBehaviour

[Tooltip("Target text renderer.")]
[SerializeField]
private Text mTextContainer = null;
protected Text mTextContainer = null;

#if TMP_PRO
[Tooltip("Target text renderer. (TMP)")]
[SerializeField]
private TextMeshPro mTextMesh = null;
protected TextMeshPro mTextMesh = null;
#endif

/* Setter & Getter */
Expand All @@ -54,7 +54,9 @@ public string text
set
{
if (this.mTextContainer) this.mTextContainer.text = value;
#if TMP_PRO
if (this.mTextMesh) this.mTextMesh.text = value;
#endif
}
}
}
Expand Down
25 changes: 2 additions & 23 deletions Assets/JCSUnity/Scripts/UI/Language/JCS_LangText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
#define TMP_PRO

using UnityEngine;
using UnityEngine.UI;

#if TMP_PRO
using TMPro;
#endif

namespace JCSUnity
{
Expand All @@ -33,32 +28,16 @@ namespace JCSUnity
/// 3. Register to `JCS_ApplicationManager`, its' component will take
/// case of the rest.
/// </summary>
public class JCS_LangText : MonoBehaviour
public class JCS_LangText : JCS_TextObject
{
/* Variables */

[Header("** Initialize Variables (JCS_LangText) **")]

[Tooltip("Text to display lang data.")]
[SerializeField]
private Text mText = null;

#if TMP_PRO
[Tooltip("Text to display lang data.")]
[SerializeField]
private TextMeshPro mTextMesh = null;
#endif

[Tooltip("List of languages with translation data.")]
[SerializeField]
private JCS_LangDataList mLangData = null;

/* Setter & Getter */

public Text text { get { return this.mText; } }
#if TMP_PRO
public TextMeshPro TextMesh { get { return this.mTextMesh; } }
#endif
public JCS_LangDataList LangData { get { return this.mLangData; } }

/* Functions */
Expand All @@ -74,7 +53,7 @@ private void Start()
/// </summary>
public void Refresh()
{
JCS_UIUtil.SetLangText(this.mLangData, this.mText);
JCS_UIUtil.SetLangText(this.mLangData, this.mTextContainer);
#if TMP_PRO
JCS_UIUtil.SetLangText(this.mLangData, this.mTextMesh);
#endif
Expand Down
35 changes: 2 additions & 33 deletions Assets/JCSUnity/Scripts/UI/Text/JCS_TextAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,15 @@
* $Notice: See LICENSE.txt for modification and distribution information
* Copyright © 2019 by Shen, Jen-Chieh $
*/

/* NOTE: If you are using `TextMesh Pro` uncomment this line.
*/
#define TMP_PRO

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

#if TMP_PRO
using TMPro;
#endif

namespace JCSUnity
{
/// <summary>
/// Text animation that will display text accordingly.
/// </summary>
public class JCS_TextAnimation : MonoBehaviour
public class JCS_TextAnimation : JCS_TextObject
{
/* Variables */

Expand All @@ -34,18 +24,6 @@ public class JCS_TextAnimation : MonoBehaviour
[SerializeField]
private int mCurrentFrame = 0;

[Header("** Initialize Variables (JCS_TextAnimation) **")]

[Tooltip("Target text renderer.")]
[SerializeField]
private Text mTextContainer = null;

#if TMP_PRO
[Tooltip("Target text renderer.")]
[SerializeField]
private TextMeshPro mTextMesh = null;
#endif

[Header("** Runtime Variables (JCS_TextAnimation) **")]

[Tooltip("Animation active or not active.")]
Expand All @@ -67,10 +45,6 @@ public class JCS_TextAnimation : MonoBehaviour
/* Setter & Getter */

public bool Active { get { return this.mActive; } set { this.mActive = value; } }
public Text TextContainer { get { return this.mTextContainer; } set { this.mTextContainer = value; } }
#if TMP_PRO
public TextMeshPro TextMesh { get { return this.mTextMesh; } set { this.mTextMesh = value; } }
#endif
public int CurrentFrame { get { return this.mCurrentFrame; } }
public float SPF { get { return this.mSPF; } set { this.mSPF = value; } }

Expand Down Expand Up @@ -116,12 +90,7 @@ public void UpdateTextFrame(int frameIndex)
else if (this.mCurrentFrame < 0)
this.mCurrentFrame = 0;

if (mTextContainer)
mTextContainer.text = textFrame[this.mCurrentFrame];
#if TMP_PRO
if (mTextMesh)
mTextMesh.text = textFrame[this.mCurrentFrame];
#endif
text = textFrame[this.mCurrentFrame];
}

/// <summary>
Expand Down
32 changes: 3 additions & 29 deletions Assets/JCSUnity/Scripts/UI/Timer/JCS_TextTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@
#define TMP_PRO

using UnityEngine;
using UnityEngine.UI;

#if TMP_PRO
using TMPro;
#endif

namespace JCSUnity
{
/// <summary>
/// Render timer in the text.
/// </summary>
public class JCS_TextTimer : MonoBehaviour
public class JCS_TextTimer : JCS_TextObject
{
/* Variables */

Expand All @@ -37,7 +32,7 @@ public class JCS_TextTimer : MonoBehaviour
private const float MIN_MINUTE_TIME = 0.0f;
private const float MIN_SECOND_TIME = 0.0f;

[Header("** Check Variables (JCS_SpriteTimer) **")]
[Header("** Check Variables (JCS_TextTimer) **")]

[SerializeField]
private bool mDoTimeIsUpCallback = false;
Expand All @@ -51,18 +46,6 @@ public class JCS_TextTimer : MonoBehaviour
[SerializeField]
private string mSecondsText = "";

[Header("** Initialize Variables (JCS_TextTimer) **")]

[Tooltip("Target text renderer.")]
[SerializeField]
private Text mTextContainer = null;

#if TMP_PRO
[Tooltip("Target text renderer.")]
[SerializeField]
private TextMeshPro mTextMesh = null;
#endif

[Header("** Runtime Variables (JCS_TextTimer) **")]

[Tooltip("Timer active or not active.")]
Expand Down Expand Up @@ -123,10 +106,6 @@ public class JCS_TextTimer : MonoBehaviour
public bool Active { get { return this.mActive; } set { this.mActive = value; } }
public bool RoundUp { get { return this.mRoundUp; } set { this.mRoundUp = value; } }
public bool HideWhenZero { get { return this.mHideWhenZero; } set { this.mHideWhenZero = value; } }
public Text TextContainer { get { return this.mTextContainer; } set { this.mTextContainer = value; } }
#if TMP_PRO
public TextMeshPro TextMesh { get { return this.mTextMesh; } set { this.mTextMesh = value; } }
#endif
public string DelimiterText { get { return this.mDelimiterText; } set { this.mDelimiterText = value; } }

public AudioClip HourSound { get { return this.mHourSound; } set { this.mHourSound = value; } }
Expand Down Expand Up @@ -227,12 +206,7 @@ public void UpdateTimeUI(float hour, float minute, float second)
DoMinuteUI(minute);
DoSecondUI(second);

if (mTextContainer)
mTextContainer.text = mHoursText + mMinutesText + mSecondsText;
#if TMP_PRO
if (mTextMesh)
mTextMesh.text = mHoursText + mMinutesText + mSecondsText;
#endif
text = mHoursText + mMinutesText + mSecondsText;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Assets/_Project/Scenes/System/FT_MultipleLangs.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1d9d6914fa00fbb4cb4c7c2e078491e3, type: 3}
m_Name:
m_EditorClassIdentifier:
mText: {fileID: 0}
mTextContainer: {fileID: 0}
mTextMesh: {fileID: 1593543890}
mLangData:
mLangData:
Expand Down Expand Up @@ -2009,7 +2009,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1d9d6914fa00fbb4cb4c7c2e078491e3, type: 3}
m_Name:
m_EditorClassIdentifier:
mText: {fileID: 1825750605}
mTextContainer: {fileID: 1825750605}
mTextMesh: {fileID: 0}
mLangData:
mLangData:
Expand Down
4 changes: 2 additions & 2 deletions UserSettings/EditorUserSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ EditorUserSettings:
value: 0007035056540f095d5c5f7113205d4445154e7878797435742d1c31b5b66239
flags: 0
RecentlyUsedSceneGuid-8:
value: 510901525301510b5a59597b417607444f1619722f2b70607e7b4f37e0e46c3b
value: 530857545707085d5e565c73437a0644444f407e7d2b77602f7f1f30b0e5633e
flags: 0
RecentlyUsedSceneGuid-9:
value: 530857545707085d5e565c73437a0644444f407e7d2b77602f7f1f30b0e5633e
value: 510901525301510b5a59597b417607444f1619722f2b70607e7b4f37e0e46c3b
flags: 0
RecentlyUsedScenePath-0:
value: 22424703114646712e3d392c1937465f25141d24293b691a0e1a2212f2f03831e1e622e0e8341c303611ea0f0d331433f31e0c45e305031f08
Expand Down

0 comments on commit 760c6ac

Please sign in to comment.