AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
LegendItem.cs
1 /************************************************/
2 /* */
3 /* Copyright (c) 2018 - 2021 monitor1394 */
4 /* https://github.com/monitor1394 */
5 /* */
6 /************************************************/
7 using System;
8 using UnityEngine;
9 using UnityEngine.UI;
10 
11 namespace XCharts
12 {
13  public class LegendItem
14  {
15  private int m_Index;
16  private string m_Name;
17  private string m_LegendName;
18  private GameObject m_GameObject;
19  private Button m_Button;
20  private Image m_Icon;
21  private ChartText m_Text;
22  private Image m_TextBackground;
23  private RectTransform m_Rect;
24  private RectTransform m_IconRect;
25  private RectTransform m_TextRect;
26  private RectTransform m_TextBackgroundRect;
27  private float m_Gap = 0f;
28  private float m_LabelPaddingLeftRight = 0f;
29  private float m_LabelPaddingTopBottom = 0f;
30  private bool m_LabelAutoSize = true;
31 
32  public int index { get { return m_Index; } set { m_Index = value; } }
33  public string name { get { return m_Name; } set { m_Name = value; } }
34  public string legendName { get { return m_LegendName; } set { m_LegendName = value; } }
35  public GameObject gameObject { get { return m_GameObject; } }
36  public Button button { get { return m_Button; } }
37  public float width
38  {
39  get
40  {
41  if (m_IconRect && m_TextBackgroundRect)
42  {
43  return m_IconRect.sizeDelta.x + m_Gap + m_TextBackgroundRect.sizeDelta.x;
44  }
45  else
46  {
47  return 0;
48  }
49  }
50  }
51 
52  public float height
53  {
54  get
55  {
56  if (m_IconRect && m_TextBackgroundRect)
57  {
58  return Mathf.Max(m_IconRect.sizeDelta.y, m_TextBackgroundRect.sizeDelta.y);
59  }
60  else
61  {
62  return 0;
63  }
64  }
65  }
66 
67  public void SetObject(GameObject obj)
68  {
69  m_GameObject = obj;
70  m_Button = obj.GetComponent<Button>();
71  m_Rect = obj.GetComponent<RectTransform>();
72  m_Icon = obj.transform.Find("icon").gameObject.GetComponent<Image>();
73  m_TextBackground = obj.transform.Find("content").gameObject.GetComponent<Image>();
74  m_Text = new ChartText(obj);
75  m_IconRect = m_Icon.gameObject.GetComponent<RectTransform>();
76  m_TextRect = m_Text.gameObject.GetComponent<RectTransform>();
77  m_TextBackgroundRect = m_TextBackground.gameObject.GetComponent<RectTransform>();
78  }
79 
80  public void SetButton(Button button)
81  {
82  m_Button = button;
83  }
84 
85  public void SetIcon(Image icon)
86  {
87  m_Icon = icon;
88  }
89 
90  public void SetText(ChartText text)
91  {
92  m_Text = text;
93  }
94 
95  public void SetTextBackground(Image image)
96  {
97  m_TextBackground = image;
98  }
99 
100  public void SetIconSize(float width, float height)
101  {
102  if (m_IconRect)
103  {
104  m_IconRect.sizeDelta = new Vector2(width, height);
105  }
106  }
107 
108  public Rect GetIconRect()
109  {
110  if (m_GameObject && m_IconRect)
111  {
112  var pos = m_GameObject.transform.localPosition;
113  var sizeDelta = m_IconRect.sizeDelta;
114  var y = pos.y - (m_Rect.sizeDelta.y - sizeDelta.y) / 2 - sizeDelta.y;
115  return new Rect(pos.x, y, m_IconRect.sizeDelta.x, m_IconRect.sizeDelta.y);
116  }
117  else
118  {
119  return Rect.zero;
120  }
121  }
122 
123  public Color GetIconColor()
124  {
125  if (m_Icon) return m_Icon.color;
126  else return Color.clear;
127  }
128 
129 
130  public void SetIconColor(Color color)
131  {
132  if (m_Icon)
133  {
134  m_Icon.color = color;
135  }
136  }
137 
138  public void SetIconImage(Sprite image)
139  {
140  if (m_Icon)
141  {
142  m_Icon.sprite = image;
143  }
144  }
145 
146  public void SetIconActive(bool active)
147  {
148  if (m_Icon)
149  {
150  m_Icon.gameObject.SetActive(active);
151  }
152  }
153 
154  public void SetContentColor(Color color)
155  {
156  if (m_Text != null)
157  {
158  m_Text.SetColor(color);
159  }
160  }
161 
162  public void SetContentBackgroundColor(Color color)
163  {
164  if (m_TextBackground)
165  {
166  m_TextBackground.color = color;
167  }
168  }
169 
170  public void SetContentPosition(Vector3 offset)
171  {
172  m_Gap = offset.x;
173  if (m_TextBackgroundRect)
174  {
175  var posX = m_IconRect.sizeDelta.x + offset.x;
176  m_TextBackgroundRect.anchoredPosition3D = new Vector3(posX, offset.y, 0);
177  }
178  }
179 
180  public bool SetContent(string content)
181  {
182  if (m_Text != null && !m_Text.GetText().Equals(content))
183  {
184  m_Text.SetText(content);
185  if (m_LabelAutoSize)
186  {
187  var newSize = string.IsNullOrEmpty(content) ? Vector2.zero :
188  new Vector2(m_Text.GetPreferredWidth(), m_Text.GetPreferredHeight());
189  var sizeChange = newSize.x != m_TextRect.sizeDelta.x || newSize.y != m_TextRect.sizeDelta.y;
190  if (sizeChange)
191  {
192  m_TextRect.sizeDelta = newSize;
193  m_TextRect.anchoredPosition3D = new Vector3(m_LabelPaddingLeftRight, 0);
194  m_TextBackgroundRect.sizeDelta = new Vector2(m_Text.GetPreferredWidth() + m_LabelPaddingLeftRight * 2,
195  m_Text.GetPreferredHeight() + m_LabelPaddingTopBottom * 2 - 4);
196  m_Rect.sizeDelta = new Vector3(width, height);
197  }
198  return sizeChange;
199  }
200  }
201  return false;
202  }
203 
204  public void SetPosition(Vector3 position)
205  {
206  if (m_GameObject)
207  {
208  m_GameObject.transform.localPosition = position;
209  }
210  }
211 
212  public void SetActive(bool active)
213  {
214  if (m_GameObject)
215  {
216  m_GameObject.SetActive(active);
217  }
218  }
219  }
220 }
XCharts.LegendItem
Definition: LegendItem.cs:13
XCharts.ChartText
Definition: ChartText.cs:16
XCharts
Definition: RewardChart.cs:14
XCharts.SerieSymbolType.Rect
@ Rect
正方形。可通过设置itemStyle的cornerRadius变成圆角矩形。