| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Globalization; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Special type of entry to be used as date separator in chat conversations. |
| | 10 | | /// </summary> |
| | 11 | | public class DateSeparatorEntry : ChatEntry |
| | 12 | | { |
| | 13 | | [SerializeField] internal TextMeshProUGUI title; |
| | 14 | |
|
| | 15 | | [Header("Preview Mode")] |
| | 16 | | [SerializeField] private Image previewBackgroundImage; |
| | 17 | | [SerializeField] private Color previewBackgroundColor; |
| | 18 | | [SerializeField] private Color previewFontColor; |
| | 19 | |
|
| | 20 | | private DateTime timestamp; |
| | 21 | | private ChatEntryModel chatEntryModel; |
| | 22 | | private Color originalBackgroundColor; |
| | 23 | | private Color originalFontColor; |
| | 24 | |
|
| 0 | 25 | | public override ChatEntryModel Model => chatEntryModel; |
| | 26 | |
|
| | 27 | | private void Awake() |
| | 28 | | { |
| 2 | 29 | | originalBackgroundColor = previewBackgroundImage.color; |
| 2 | 30 | | originalFontColor = title.color; |
| 2 | 31 | | } |
| | 32 | |
|
| | 33 | | public override void Populate(ChatEntryModel model) |
| | 34 | | { |
| 2 | 35 | | chatEntryModel = model; |
| 2 | 36 | | title.text = GetDateFormat(GetDateTimeFromUnixTimestampMilliseconds(model.timestamp)); |
| 2 | 37 | | } |
| | 38 | |
|
| | 39 | | public override void SetFadeout(bool enabled) |
| | 40 | | { |
| 0 | 41 | | if (!enabled) |
| | 42 | | { |
| 0 | 43 | | group.alpha = 1; |
| 0 | 44 | | fadeEnabled = false; |
| 0 | 45 | | return; |
| | 46 | | } |
| | 47 | |
|
| 0 | 48 | | fadeEnabled = true; |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | public override void DeactivatePreview() |
| | 52 | | { |
| 0 | 53 | | if (!gameObject.activeInHierarchy) |
| | 54 | | { |
| 0 | 55 | | previewBackgroundImage.color = originalBackgroundColor; |
| 0 | 56 | | title.color = originalFontColor; |
| 0 | 57 | | return; |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | if (previewInterpolationRoutine != null) |
| 0 | 61 | | StopCoroutine(previewInterpolationRoutine); |
| | 62 | |
|
| 0 | 63 | | if (previewInterpolationAlphaRoutine != null) |
| 0 | 64 | | StopCoroutine(previewInterpolationAlphaRoutine); |
| | 65 | |
|
| 0 | 66 | | group.alpha = 1; |
| 0 | 67 | | previewInterpolationRoutine = |
| | 68 | | StartCoroutine(InterpolatePreviewColor(originalBackgroundColor, originalFontColor, 0.5f)); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public override void FadeOut() |
| | 72 | | { |
| 0 | 73 | | if (!gameObject.activeInHierarchy) |
| | 74 | | { |
| 0 | 75 | | group.alpha = 0; |
| 0 | 76 | | return; |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | if (previewInterpolationAlphaRoutine != null) |
| 0 | 80 | | StopCoroutine(previewInterpolationAlphaRoutine); |
| | 81 | |
|
| 0 | 82 | | previewInterpolationAlphaRoutine = StartCoroutine(InterpolateAlpha(0, 0.5f)); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public override void ActivatePreview() |
| | 86 | | { |
| 0 | 87 | | if (!gameObject.activeInHierarchy) |
| | 88 | | { |
| 0 | 89 | | ActivatePreviewInstantly(); |
| 0 | 90 | | return; |
| | 91 | | } |
| | 92 | |
|
| 0 | 93 | | if (previewInterpolationRoutine != null) |
| 0 | 94 | | StopCoroutine(previewInterpolationRoutine); |
| | 95 | |
|
| 0 | 96 | | previewInterpolationRoutine = StartCoroutine(InterpolatePreviewColor(previewBackgroundColor, previewFontColor, 0 |
| | 97 | |
|
| 0 | 98 | | previewInterpolationAlphaRoutine = StartCoroutine(InterpolateAlpha(1, 0.5f)); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public override void ActivatePreviewInstantly() |
| | 102 | | { |
| 0 | 103 | | if (previewInterpolationRoutine != null) |
| 0 | 104 | | StopCoroutine(previewInterpolationRoutine); |
| | 105 | |
|
| 0 | 106 | | previewBackgroundImage.color = previewBackgroundColor; |
| 0 | 107 | | title.color = previewFontColor; |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | public override void DeactivatePreviewInstantly() |
| | 111 | | { |
| 0 | 112 | | if (previewInterpolationRoutine != null) |
| 0 | 113 | | StopCoroutine(previewInterpolationRoutine); |
| | 114 | |
|
| 0 | 115 | | previewBackgroundImage.color = originalBackgroundColor; |
| 0 | 116 | | title.color = originalFontColor; |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | private string GetDateFormat(DateTime date) |
| | 120 | | { |
| 2 | 121 | | string result = string.Empty; |
| | 122 | |
|
| 2 | 123 | | if (date.Year == DateTime.Now.Year && |
| | 124 | | date.Month == DateTime.Now.Month && |
| | 125 | | date.Day == DateTime.Now.Day) |
| | 126 | | { |
| 1 | 127 | | result = "Today"; |
| 1 | 128 | | } |
| | 129 | | else |
| | 130 | | { |
| 1 | 131 | | result = date.ToString("D", DateTimeFormatInfo.InvariantInfo); |
| | 132 | | } |
| | 133 | |
|
| 2 | 134 | | return result; |
| | 135 | | } |
| | 136 | |
|
| | 137 | | private DateTime GetDateTimeFromUnixTimestampMilliseconds(ulong milliseconds) |
| | 138 | | { |
| 2 | 139 | | DateTime result = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); |
| 2 | 140 | | return result.AddMilliseconds(milliseconds); |
| | 141 | | } |
| | 142 | |
|
| | 143 | | private IEnumerator InterpolatePreviewColor(Color backgroundColor, Color fontColor, float duration) |
| | 144 | | { |
| 0 | 145 | | var t = 0f; |
| | 146 | |
|
| 0 | 147 | | while (t < duration) |
| | 148 | | { |
| 0 | 149 | | t += Time.deltaTime; |
| | 150 | |
|
| 0 | 151 | | previewBackgroundImage.color = Color.Lerp(previewBackgroundImage.color, backgroundColor, t / duration); |
| 0 | 152 | | title.color = Color.Lerp(title.color, fontColor, t / duration); |
| | 153 | |
|
| 0 | 154 | | yield return null; |
| | 155 | | } |
| | 156 | |
|
| 0 | 157 | | previewBackgroundImage.color = backgroundColor; |
| 0 | 158 | | title.color = fontColor; |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | } |