AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
ChartText.cs
1 /************************************************/
2 /* */
3 /* Copyright (c) 2018 - 2021 monitor1394 */
4 /* https://github.com/monitor1394 */
5 /* */
6 /************************************************/
7 
8 using UnityEngine;
9 using UnityEngine.UI;
10 #if dUI_TextMeshPro
11 using TMPro;
12 #endif
13 
14 namespace XCharts
15 {
16  public class ChartText
17  {
18  private Text m_Text;
19  private TextGenerationSettings m_RelatedTextSettings;
20  public Text text
21  {
22  get { return m_Text; }
23  set
24  {
25  m_Text = value;
26  if (value != null)
27  {
28  m_RelatedTextSettings = m_Text.GetGenerationSettings(Vector2.zero);
29  }
30  }
31  }
32 #if dUI_TextMeshPro
33  private TextMeshProUGUI m_TMPText;
34  public TextMeshProUGUI tmpText { get { return m_TMPText; } set { m_TMPText = value; } }
35 #endif
36  public GameObject gameObject
37  {
38  get
39  {
40 #if dUI_TextMeshPro
41  if (m_TMPText != null) return m_TMPText.gameObject;
42 #else
43  if (m_Text != null) return m_Text.gameObject;
44 #endif
45  return null;
46  }
47  }
48 
49  public TextAnchor alignment
50  {
51  get
52  {
53 #if dUI_TextMeshPro
54  if (m_TMPText == null) return TextAnchor.MiddleCenter;
55  switch (m_TMPText.alignment)
56  {
57  case TextAlignmentOptions.Bottom: return TextAnchor.LowerCenter;
58  case TextAlignmentOptions.BottomLeft: return TextAnchor.LowerLeft;
59  case TextAlignmentOptions.BottomRight: return TextAnchor.LowerRight;
60  case TextAlignmentOptions.Center: return TextAnchor.MiddleCenter;
61  case TextAlignmentOptions.Left: return TextAnchor.MiddleLeft;
62  case TextAlignmentOptions.Right: return TextAnchor.MiddleRight;
63  case TextAlignmentOptions.Top: return TextAnchor.UpperCenter;
64  case TextAlignmentOptions.TopLeft: return TextAnchor.UpperLeft;
65  case TextAlignmentOptions.TopRight: return TextAnchor.UpperRight;
66  default: return TextAnchor.MiddleCenter;
67  }
68 #else
69  if (m_Text != null) return m_Text.alignment;
70  else return TextAnchor.MiddleCenter;
71 #endif
72  }
73  set
74  {
75  SetAlignment(alignment);
76  }
77  }
78 
79  public ChartText()
80  {
81  }
82 
83  public ChartText(GameObject textParent)
84  {
85 #if dUI_TextMeshPro
86  m_TMPText = textParent.GetComponentInChildren<TextMeshProUGUI>();
87  if (m_TMPText == null)
88  {
89  Debug.LogError("can't find TextMeshProUGUI component:" + textParent);
90  }
91 #else
92  m_Text = textParent.GetComponentInChildren<Text>();
93  if (m_Text == null)
94  {
95  Debug.LogError("can't find Text component:" + textParent);
96  }
97 #endif
98  }
99 
100  public void SetFontSize(float fontSize)
101  {
102 #if dUI_TextMeshPro
103  if (m_TMPText != null) m_TMPText.fontSize = fontSize;
104 #else
105  if (m_Text != null) m_Text.fontSize = (int)fontSize;
106 #endif
107  }
108 
109  public void SetText(string text)
110  {
111  if (text == null) text = string.Empty;
112  else text = text.Replace("\\n", "\n");
113 #if dUI_TextMeshPro
114  if(m_TMPText != null) m_TMPText.text = text;
115 #else
116  if (m_Text != null) m_Text.text = text;
117 #endif
118  }
119 
120  public string GetText()
121  {
122 #if dUI_TextMeshPro
123  if (m_TMPText != null) return m_TMPText.text;
124 #else
125  if (m_Text != null) return m_Text.text;
126 #endif
127  return string.Empty;
128  }
129 
130  public void SetColor(Color color)
131  {
132 #if dUI_TextMeshPro
133  if (m_TMPText != null) m_TMPText.color = color;
134 #else
135  if (m_Text != null) m_Text.color = color;
136 #endif
137  }
138 
139  public void SetLineSpacing(float lineSpacing)
140  {
141 #if dUI_TextMeshPro
142  if (m_TMPText != null) m_TMPText.lineSpacing = lineSpacing;
143 #else
144  if (m_Text != null) m_Text.lineSpacing = lineSpacing;
145 #endif
146  }
147 
148  public void SetActive(bool flag)
149  {
150 #if dUI_TextMeshPro
151  //m_TMPText.gameObject.SetActive(flag);
152  if (m_TMPText != null) ChartHelper.SetActive(m_TMPText.gameObject, flag);
153 #else
154  //m_Text.gameObject.SetActive(flag);
155  if (m_Text != null) ChartHelper.SetActive(m_Text.gameObject, flag);
156 #endif
157  }
158 
159  public void SetLocalPosition(Vector3 position)
160  {
161 #if dUI_TextMeshPro
162  if (m_TMPText != null) m_TMPText.transform.localPosition = position;
163 #else
164  if (m_Text != null) m_Text.transform.localPosition = position;
165 #endif
166  }
167  public void SetSizeDelta(Vector2 sizeDelta)
168  {
169 #if dUI_TextMeshPro
170  if (m_TMPText != null) m_TMPText.GetComponent<RectTransform>().sizeDelta = sizeDelta;
171 #else
172  if (m_Text != null) m_Text.GetComponent<RectTransform>().sizeDelta = sizeDelta;
173 #endif
174  }
175 
176  public void SetLocalEulerAngles(Vector3 position)
177  {
178 #if dUI_TextMeshPro
179  if (m_TMPText != null) m_TMPText.transform.localEulerAngles = position;
180 #else
181  if (m_Text != null) m_Text.transform.localEulerAngles = position;
182 #endif
183  }
184 
185  public void SetAlignment(TextAnchor alignment)
186  {
187 #if dUI_TextMeshPro
188  if (m_TMPText == null) return;
189  switch (alignment)
190  {
191  case TextAnchor.LowerCenter: m_TMPText.alignment = TextAlignmentOptions.Bottom; break;
192  case TextAnchor.LowerLeft: m_TMPText.alignment = TextAlignmentOptions.BottomLeft; break;
193  case TextAnchor.LowerRight: m_TMPText.alignment = TextAlignmentOptions.BottomRight; break;
194  case TextAnchor.MiddleCenter: m_TMPText.alignment = TextAlignmentOptions.Center; break;
195  case TextAnchor.MiddleLeft: m_TMPText.alignment = TextAlignmentOptions.Left; break;
196  case TextAnchor.MiddleRight: m_TMPText.alignment = TextAlignmentOptions.Right; break;
197  case TextAnchor.UpperCenter: m_TMPText.alignment = TextAlignmentOptions.Top; break;
198  case TextAnchor.UpperLeft: m_TMPText.alignment = TextAlignmentOptions.TopLeft; break;
199  case TextAnchor.UpperRight: m_TMPText.alignment = TextAlignmentOptions.TopRight; break;
200  }
201 #else
202  if (m_Text != null) m_Text.alignment = alignment;
203 #endif
204  }
205 
206  public void SetFont(Font font)
207  {
208  if (m_Text) m_Text.font = font;
209  }
210 
211  public void SetFontStyle(FontStyle fontStyle)
212  {
213 #if dUI_TextMeshPro
214  if (m_TMPText == null) return;
215  switch (fontStyle)
216  {
217  case FontStyle.Normal: m_TMPText.fontStyle = FontStyles.Normal; break;
218  case FontStyle.Bold: m_TMPText.fontStyle = FontStyles.Bold; break;
219  case FontStyle.BoldAndItalic: m_TMPText.fontStyle = FontStyles.Bold | FontStyles.Italic; break;
220  case FontStyle.Italic: m_TMPText.fontStyle = FontStyles.Italic; break;
221  }
222 #else
223  if (m_Text != null) m_Text.fontStyle = fontStyle;
224 #endif
225  }
226 
227  public void SetFontAndSizeAndStyle(TextStyle textStyle, ComponentTheme theme)
228  {
229 #if dUI_TextMeshPro
230  if (m_TMPText == null) return;
231  m_TMPText.font = textStyle.tmpFont == null ? theme.tmpFont : textStyle.tmpFont;
232  m_TMPText.fontSize = textStyle.fontSize == 0 ? theme.fontSize : textStyle.fontSize;
233  m_TMPText.fontStyle = textStyle.tmpFontStyle;
234 #else
235  if (m_Text != null)
236  {
237  m_Text.font = textStyle.font == null ? theme.font : textStyle.font;
238  m_Text.fontSize = textStyle.fontSize == 0 ? theme.fontSize : textStyle.fontSize;
239  m_Text.fontStyle = textStyle.fontStyle;
240  }
241 #endif
242  }
243 
244  public float GetPreferredWidth(string content)
245  {
246 #if dUI_TextMeshPro
247  if (m_TMPText != null) return 0; // TODO:
248 #else
249  if (m_Text != null)
250  {
251  var tg = m_Text.cachedTextGeneratorForLayout;
252  var setting = m_Text.GetGenerationSettings(Vector2.zero);
253  return tg.GetPreferredWidth(content, setting) / m_Text.pixelsPerUnit;
254  }
255 #endif
256  return 0;
257  }
258 
259  public float GetPreferredWidth()
260  {
261 #if dUI_TextMeshPro
262  if (m_TMPText != null) return m_TMPText.preferredWidth;
263 #else
264  if (m_Text != null) return m_Text.preferredWidth;
265 #endif
266  return 0;
267  }
268  public float GetPreferredHeight()
269  {
270 #if dUI_TextMeshPro
271  if (m_TMPText != null) return m_TMPText.preferredHeight;
272 #else
273  if (m_Text != null) return m_Text.preferredHeight;
274 #endif
275  return 0;
276  }
277 
278  public string GetPreferredText(string content, string suffix, float maxWidth)
279  {
280 #if dUI_TextMeshPro
281  if (m_TMPText != null) return content; // TODO:
282 #else
283  if (m_Text != null)
284  {
285  var sourWid = GetPreferredWidth(content);
286  if (sourWid < maxWidth) return content;
287  var suffixWid = GetPreferredWidth(suffix);
288  var textWid = maxWidth - 1.3f * suffixWid;
289  for (int i = content.Length; i > 0; i--)
290  {
291  var temp = content.Substring(0, i);
292  if (GetPreferredWidth(temp) < textWid)
293  {
294  return temp + suffix;
295  }
296  }
297  }
298 #endif
299  return string.Empty;
300  }
301 
302 #if dUI_TextMeshPro
303 
304  public void SetFont(TMP_FontAsset font)
305  {
306  if (m_TMPText != null) m_TMPText.font = font;
307  }
308 #endif
309  }
310 }
XCharts.ChartText
Definition: ChartText.cs:16
XCharts.TextStyle
Settings related to text. 文本的相关设置。
Definition: TextStyle.cs:21
XCharts.ComponentTheme.fontSize
int fontSize
the font size of text. 文本字体大小。
Definition: ComponentTheme.cs:59
XCharts
Definition: RewardChart.cs:14
XCharts.TextStyle.fontStyle
FontStyle fontStyle
font style. 文本字体的风格。 [default: FontStyle.Normal]
Definition: TextStyle.cs:108
XCharts.TextStyle.font
Font font
the font of text. When null, the theme's font is used by default. 文本字体。 [default: null]
Definition: TextStyle.cs:88
XCharts.TextStyle.fontSize
int fontSize
font size. 文本字体大小。 [default: 18]
Definition: TextStyle.cs:98
XCharts.ComponentTheme
Definition: ComponentTheme.cs:17
XCharts.ComponentTheme.font
Font font
the font of text. 字体。
Definition: ComponentTheme.cs:32