AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
ChartLabel.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 
11 namespace XCharts
12 {
13  public class ChartLabel : ChartObject
14  {
15  private bool m_AutoHideIconWhenLabelEmpty = false;
16  private bool m_LabelAutoSize = true;
17  private float m_LabelPaddingLeftRight = 3f;
18  private float m_LabelPaddingTopBottom = 3f;
19  private ChartText m_LabelText;
20  private RectTransform m_LabelRect;
21  private RectTransform m_IconRect;
22  private RectTransform m_ObjectRect;
23  private Vector3 m_IconOffest;
24  private Align m_Align = Align.Left;
25 
26  private Image m_IconImage;
27 
28  public GameObject gameObject
29  {
30  get { return m_GameObject; }
31  set
32  {
33  m_GameObject = value;
34  m_ObjectRect = value.GetComponent<RectTransform>();
35  }
36  }
37  public Image icon
38  {
39  get { return m_IconImage; }
40  set { SetIcon(value); }
41  }
42  public ChartText label
43  {
44  get { return m_LabelText; }
45  set
46  {
47  m_LabelText = value;
48  if (value != null) m_LabelRect = m_LabelText.gameObject.GetComponent<RectTransform>();
49  }
50  }
51 
52  public bool autoHideIconWhenLabelEmpty { set { m_AutoHideIconWhenLabelEmpty = value; } }
53  public bool isIconActive { get; private set; }
54 
55  public ChartLabel()
56  {
57  }
58 
59  public void SetLabel(GameObject labelObj, bool autoSize, float paddingLeftRight, float paddingTopBottom)
60  {
61  m_GameObject = labelObj;
62  m_LabelAutoSize = autoSize;
63  m_LabelPaddingLeftRight = paddingLeftRight;
64  m_LabelPaddingTopBottom = paddingTopBottom;
65  m_LabelText = new ChartText(labelObj);
66  m_LabelRect = m_LabelText.gameObject.GetComponent<RectTransform>();
67  m_ObjectRect = labelObj.GetComponent<RectTransform>();
68  m_Align = Align.Left;
69  }
70 
71  public void SetAutoSize(bool flag)
72  {
73  m_LabelAutoSize = flag;
74  }
75 
76  public void SetIcon(Image image)
77  {
78  m_IconImage = image;
79  if (image != null)
80  {
81  m_IconRect = m_IconImage.GetComponent<RectTransform>();
82  }
83  }
84 
85  public void SetIconSprite(Sprite sprite)
86  {
87  if (m_IconImage != null) m_IconImage.sprite = sprite;
88  }
89 
90  public void SetIconSize(float width, float height)
91  {
92  if (m_IconRect != null) m_IconRect.sizeDelta = new Vector3(width, height);
93  }
94 
95  public void UpdateIcon(IconStyle iconStyle, Sprite sprite = null)
96  {
97  if (m_IconImage == null) return;
98  SetIconActive(iconStyle.show);
99  if (iconStyle.show)
100  {
101  m_IconImage.sprite = sprite == null ? iconStyle.sprite : sprite;
102  m_IconImage.color = iconStyle.color;
103  m_IconRect.sizeDelta = new Vector2(iconStyle.width, iconStyle.height);
104  m_IconOffest = iconStyle.offset;
105  m_Align = iconStyle.align;
106  m_AutoHideIconWhenLabelEmpty = iconStyle.autoHideWhenLabelEmpty;
107  AdjustIconPos();
108  if (iconStyle.layer == IconStyle.Layer.UnderLabel)
109  m_IconRect.SetSiblingIndex(0);
110  else
111  m_IconRect.SetSiblingIndex(m_GameObject.transform.childCount - 1);
112  }
113  }
114 
115  public float GetLabelWidth()
116  {
117  if (m_LabelRect) return m_LabelRect.sizeDelta.x;
118  else return 0;
119  }
120 
121  public float GetLabelHeight()
122  {
123  if (m_LabelRect) return m_LabelRect.sizeDelta.y;
124  return 0;
125  }
126 
127  public void SetLabelColor(Color color)
128  {
129  if (m_LabelText != null) m_LabelText.SetColor(color);
130  }
131 
132  public void SetLabelRotate(float rotate)
133  {
134  if (m_LabelText != null) m_LabelText.SetLocalEulerAngles(new Vector3(0, 0, rotate));
135  }
136 
137  public void SetPosition(Vector3 position)
138  {
139  if (m_GameObject != null)
140  {
141  m_GameObject.transform.localPosition = position;
142  }
143  }
144 
145  public void SetLabelPosition(Vector3 position)
146  {
147  if (m_LabelRect) m_LabelRect.localPosition = position;
148  }
149 
150  public void SetActive(bool flag)
151  {
152  if (m_GameObject) ChartHelper.SetActive(m_GameObject, flag);
153  }
154  public void SetLabelActive(bool flag)
155  {
156  if (m_LabelText != null) m_LabelText.SetActive(flag);
157  }
158  public void SetIconActive(bool flag)
159  {
160  isIconActive = flag;
161  if (m_IconImage) ChartHelper.SetActive(m_IconImage, flag);
162  }
163 
164  public bool SetText(string text)
165  {
166  if (m_LabelRect == null) return false;
167  if (m_LabelText != null && !m_LabelText.GetText().Equals(text))
168  {
169  m_LabelText.SetText(text);
170  if (m_LabelAutoSize)
171  {
172  var newSize = string.IsNullOrEmpty(text) ? Vector2.zero :
173  new Vector2(m_LabelText.GetPreferredWidth() + m_LabelPaddingLeftRight * 2,
174  m_LabelText.GetPreferredHeight() + m_LabelPaddingTopBottom * 2);
175  var sizeChange = newSize.x != m_LabelRect.sizeDelta.x || newSize.y != m_LabelRect.sizeDelta.y;
176  if (sizeChange)
177  {
178  m_LabelRect.sizeDelta = newSize;
179  AdjustIconPos();
180  }
181  return sizeChange;
182  }
183  AdjustIconPos();
184  if (m_AutoHideIconWhenLabelEmpty && isIconActive)
185  {
186  ChartHelper.SetActive(m_IconImage.gameObject, !string.IsNullOrEmpty(text));
187  }
188  }
189  return false;
190  }
191 
192  private void AdjustIconPos()
193  {
194  if (m_IconImage && m_IconRect)
195  {
196  var iconX = 0f;
197  switch (m_Align)
198  {
199  case Align.Left:
200  switch (m_LabelText.alignment)
201  {
202  case TextAnchor.LowerLeft:
203  case TextAnchor.UpperLeft:
204  case TextAnchor.MiddleLeft:
205  iconX = -m_ObjectRect.sizeDelta.x / 2 - m_IconRect.sizeDelta.x / 2;
206  break;
207  case TextAnchor.LowerRight:
208  case TextAnchor.UpperRight:
209  case TextAnchor.MiddleRight:
210  iconX = m_ObjectRect.sizeDelta.x / 2 - m_LabelText.GetPreferredWidth() - m_IconRect.sizeDelta.x / 2;
211  break;
212  case TextAnchor.LowerCenter:
213  case TextAnchor.UpperCenter:
214  case TextAnchor.MiddleCenter:
215  iconX = -m_LabelText.GetPreferredWidth() / 2 - m_IconRect.sizeDelta.x / 2;
216  break;
217  }
218  break;
219  case Align.Right:
220  switch (m_LabelText.alignment)
221  {
222  case TextAnchor.LowerLeft:
223  case TextAnchor.UpperLeft:
224  case TextAnchor.MiddleLeft:
225  iconX = m_ObjectRect.sizeDelta.x / 2 + m_IconRect.sizeDelta.x / 2;
226  break;
227  case TextAnchor.LowerRight:
228  case TextAnchor.UpperRight:
229  case TextAnchor.MiddleRight:
230  iconX = m_IconRect.sizeDelta.x / 2;
231  break;
232  case TextAnchor.LowerCenter:
233  case TextAnchor.UpperCenter:
234  case TextAnchor.MiddleCenter:
235  iconX = m_LabelText.GetPreferredWidth() / 2 + m_IconRect.sizeDelta.x / 2;
236  break;
237  }
238  break;
239  }
240  m_IconRect.anchoredPosition = m_IconOffest + new Vector3(iconX, 0);
241  }
242  }
243  }
244 }
XCharts.ChartLabel
Definition: ChartLabel.cs:13
XCharts.ChartObject
Definition: ChartObject.cs:12
XCharts.ChartText
Definition: ChartText.cs:16
XCharts.IconStyle.sprite
Sprite sprite
The image of icon. 图标的图片。
Definition: IconStyle.cs:55
XCharts.IconStyle.align
Align align
水平方向对齐方式。
Definition: IconStyle.cs:75
XCharts.IconStyle.height
float height
图标高。
Definition: IconStyle.cs:67
XCharts.Align
Align
对齐方式
Definition: Serie.cs:250
XCharts.IconStyle
Definition: IconStyle.cs:14
XCharts
Definition: RewardChart.cs:14
XCharts.IconStyle.color
Color color
图标颜色。
Definition: IconStyle.cs:59
XCharts.IconStyle.show
bool show
Whether the data icon is show. 是否显示图标。
Definition: IconStyle.cs:46
XCharts.IconStyle.offset
Vector3 offset
图标偏移。
Definition: IconStyle.cs:71
XCharts.IconStyle.width
float width
图标宽。
Definition: IconStyle.cs:63
XCharts.IconStyle.layer
Layer layer
显示在上层还是在下层。
Definition: IconStyle.cs:50
XCharts.IconStyle.autoHideWhenLabelEmpty
bool autoHideWhenLabelEmpty
当label内容为空时是否自动隐藏图标
Definition: IconStyle.cs:79