aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Lean/Localization/Scripts/LeanToken.cs
blob: c6ca7f8bfc84d9d81f1ecbcf70320b1e29073278 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using UnityEngine;
using System.Collections.Generic;
using Lean.Common;
#if UNITY_EDITOR
using UnityEditor;

namespace Lean.Localization
{
	[CustomEditor(typeof(LeanToken))]
	public class LeanToken_Inspector : LeanInspector<LeanToken>
	{
		protected override void DrawInspector()
		{
			Draw("value");
		}
	}
}
#endif

namespace Lean.Localization
{
	/// <summary>The class stores a token name (e.g. "AGE"), allowing it to be replaced with the token value (e.g. "20").
	/// To use the token in your text, simply include the token name surrounded by braces (e.g. "I am {AGE} years old!")</summary>
	[ExecuteInEditMode]
	[HelpURL(LeanLocalization.HelpUrlPrefix + "LeanToken")]
	[AddComponentMenu(LeanLocalization.ComponentPathPrefix + "Token")]
	public class LeanToken : LeanSource
	{
		[SerializeField]
		private string value;

		[System.NonSerialized]
		private HashSet<LeanLocalizedBehaviour> behaviours;

		[System.NonSerialized]
		private static HashSet<LeanLocalizedBehaviour> tempBehaviours = new HashSet<LeanLocalizedBehaviour>();

		public string Value
		{
			set
			{
				if (this.value != value)
				{
					this.value = value;

					if (behaviours != null)
					{
						tempBehaviours.Clear();

						tempBehaviours.UnionWith(behaviours);

						foreach (var behaviour in tempBehaviours)
						{
							behaviour.UpdateLocalization();
						}
					}
				}
			}

			get
			{
				return value;
			}
		}

		public void SetValue(float value)
		{
			Value = value.ToString();
		}

		public void SetValue(string value)
		{
			Value = value;
		}

		public void SetValue(int value)
		{
			Value = value.ToString();
		}

		public void Register(LeanLocalizedBehaviour behaviour)
		{
			if (behaviour != null)
			{
				if (behaviours == null)
				{
					behaviours = new HashSet<LeanLocalizedBehaviour>();
				}

				behaviours.Add(behaviour);
			}
		}

		public void Unregister(LeanLocalizedBehaviour behaviour)
		{
			if (behaviours != null)
			{
				behaviours.Remove(behaviour);
			}
		}

		public void UnregisterAll()
		{
			if (behaviours != null)
			{
				foreach (var behaviour in behaviours)
				{
					behaviour.Unregister(this);
				}

				behaviours.Clear();
			}
		}

		public override void Compile(string primaryLanguage, string secondaryLanguage)
		{
			if (string.IsNullOrEmpty(name) == false)
			{
				LeanLocalization.CurrentTokens.Add(name, this);
			}
		}

		protected override void OnDisable()
		{
			base.OnDisable();

			UnregisterAll();
		}
	}
}